From patchwork Fri Jun 26 21:10:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Watt X-Patchwork-Id: 1318054 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::33c; helo=mail-ot1-x33c.google.com; envelope-from=swupdate+bncbcz3hh5gu4jbbugi3h3qkgqet2i36sy@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=f79mNoWx; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=kPEdejuf; dkim-atps=neutral Received: from mail-ot1-x33c.google.com (mail-ot1-x33c.google.com [IPv6:2607:f8b0:4864:20::33c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49tqKw0TbDz9sTP for ; Sat, 27 Jun 2020 07:10:43 +1000 (AEST) Received: by mail-ot1-x33c.google.com with SMTP id t17sf6716376otp.22 for ; Fri, 26 Jun 2020 14:10:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1593205840; cv=pass; d=google.com; s=arc-20160816; b=wO1jlrBDLh/GOATaHoRHgXMZrOR7265mW9o/UnDYIe97DcZigzGMxC0VpCoR7dn4di a9kDvHLDyeTPYQfJdnM8uJxIG7UApZyPSA3rOp1lg9WJoExYTjzKEuiGAvSl+urXLrx0 rCA5zP6m1z6Of9d0ZLPtiahRvLkYhicWHW7iYKqOrgZLaFpyop6vUJGEyswFc9GpUVP0 zSEB+4PSw40d1+UScrU6i7AT5E1c1bOLj+dMb97DXvs2RipdEJXffYah8KlWJbMR1PQh 4N7LwQiXyVj3DRGWOeMFU5JnjMmeMN6l3VBnl3fpB87YLlCk6gpGYiJw7bs9K81nkIwX Amzw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:message-id:date :subject:cc:to:from:sender:dkim-signature:dkim-signature; bh=r3r2iCotmwa5w0FIKDj3D690rZ9z2z7tl7//KTp30UY=; b=qyyfS1T6IIhfiL3D3LBdJsvUJcUm96WlAKB/sL7kpj/6X1+ei6G/6pfWlc4bAeDW3y 2Yg3wcyMiOUVUwWmGzeQPXo3GgyLNw8t+KbD60kMRNMWz+eeyOEpS5SsR1SOH1nRku2d KtQY5AxOWdamVjb05J+xYR54EzBxbep3YsYXUVHKPuGUjY/JXMpjfXUWAFUvJdThDk7T XZJrxb2jtlY1xc8cpN93kh9BG0QMIz/x4WDYMkMpWs/emdiSDryS8c0toqOyblT2AWwR wSSjv822FjEHnkQfvxHGYQXSXj2iT2cg/8HA2C5CgFjmkgj0B3mBIKmwe4uT8nrJjjvY mAFw== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HNEwlAX+; spf=pass (google.com: domain of jpewhacker@gmail.com designates 2607:f8b0:4864:20::d44 as permitted sender) smtp.mailfrom=jpewhacker@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=r3r2iCotmwa5w0FIKDj3D690rZ9z2z7tl7//KTp30UY=; b=f79mNoWxwYvzCHgT88D1PsLkc37NTqXKJequ0iw/werCEWFapZRzn9KWSRiJflGNa7 SOxtYxKN4QpKtoxZ2jET8O2XyCA7UWYF33HLao+7V3g6D/mhxxLaRs3KbOjSYw52THnG aAqrFo6lYtKW0IGHZ5NsCawROzvmIu4enFIbeS08NyGIOpjtrg/9G8K5McTDZAQ/0Fpa dehm85XddNL1mhhQjDy+gN8DQNHGrvRczWPCQjU/YvpAVW/Q0tN4lSPs8eoUdhxb0Yj7 +/l32B2A8mGIAm9c0DvNh34zFpQEwGEhMiO/A/EZf47hbFpeOI/aluOmxKNO0NjarOzI K+NQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=r3r2iCotmwa5w0FIKDj3D690rZ9z2z7tl7//KTp30UY=; b=kPEdejufknLpgmnhK8OiXBjWiPSgpoRm+u+T7czOYeSW5NPaqX0k+vcFuDpQ45nxf7 XHNmajl//O1nTPb4f6mEFSS33clrvcDR9jglt5Gh24xydgbiMe0HkeUXjs4UZQyZY/9t 4qd3hn5J3uW6xPHyLPEXvR2xv0804h3Rnzw3uVKnxgipoAATnj6UqTouUnpuFOlfMeao JcYR3JUTLNdiUeHrq2WBCCWLoOZR5ySFoV9zhSJ/lPsRxqxZIfNWBU+UfvsTMDY3YFem WO7QMPtHNzqZPufacRV8GD4PMhuNiDXPkTeGldwYLXPAp13YOGWe5VpLD3lxOxdu+ntA Peug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=r3r2iCotmwa5w0FIKDj3D690rZ9z2z7tl7//KTp30UY=; b=OhsRbDkUhh+cVEap9vxPb61MnCu09H+kNQGp/L5jk6QBt8b9GMTg3MgeMNOB+wKo1N YoPjSdGf7FfLfORnrDi2lyJaMIAW+GUvjNk3QzubepD+H6v51dgggymGYhQJw8ZiOCJh GFoVkroHvMfoHkslW7+dJ1Kh0fc0afe7hSV0r7XIu6gbG+IRo7sOpXHUh4zOzSJEZ+n7 Fe8Kg9H+VrdaaA1rDIM5cC09yv5z5lnSmP/oUH4QmlqqiMF+o1NnvZU0ruSA8hqtJCwp q87jftZpo+zoMgPWXqQuK+wwqNuzSeWOHBAnVXZiLCsTIbVaEY3YYiRyPHfV0Q+0Jd8q Ka6w== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532r9WnjMVXb/cB8+PmwMwh8MIZZFrWlKxYYPv1nnNjfF1TPJaAn XwWQ2RJUdP/yDXrwCk9YQ3g= X-Google-Smtp-Source: ABdhPJydIXmilY5STtn9p+UBj8OzvlhWxlo6jRi9XoYU7yIrUVMfrjmE3SLn0z3zI5hi70/wIscWZw== X-Received: by 2002:aca:db44:: with SMTP id s65mr4097964oig.24.1593205840713; Fri, 26 Jun 2020 14:10:40 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:aca:eac1:: with SMTP id i184ls2079032oih.4.gmail; Fri, 26 Jun 2020 14:10:40 -0700 (PDT) X-Received: by 2002:aca:ad56:: with SMTP id w83mr2538542oie.9.1593205840113; Fri, 26 Jun 2020 14:10:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593205840; cv=none; d=google.com; s=arc-20160816; b=H0jMABpaF7KjVvXm1liJnSi4YuocoAG9PCn7J7EPG5sre/y4FgTRZTKK3PEwietEaf FAlovSstkufqQjV1sOLHpxzPnmp4738Kbh9tn40CNEZZtoId39x4jkzY4tTRLwS5aB6c AKm5OZDSo/t8Sb7myXKRIsLdRsg5F90Ozh9i6Fzv4tbkXdsd4dGwsmCO7IakptPnfczw Qe8C4oFT5GPVdgBTe721WN2ozwT/PuOY0ROL17X7sx6wMo5+JCxuFOntmALo7azu/fq0 9IdqyfHJP9TThqeyQgjtQspp2og1dKGwu79uucmTpZ1a1RQ1qMOhqQBKWrCQLQDtBoeq MCJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=6MYo0kcPqrnDr/W6Jbcz+USVffTDiIvoLDwPQnkzGdU=; b=cqphtaH6iANWMp3EDMnqucAsTZx2gPKWxKN5VqbFcbtmU+aItOnjNA0MbVI+luhtCj MBmSt/n0//goZUGg38eLA44ehrqkfXaciNV+J9beKI+7U5wqHjDWF95eB+/Jn0kuohTo 09vcgkDQxzxeK8E05lPeJNhVfBiGSsWC9GrRLK7kaAgH5skg53bIjB8UbkA0aac4qvG8 6l5eYqe183I58Z5M2I7DBtlyr/MBM7rryHWcRuakKNJUINE42DNnc1YfAaYEQEAtVjN8 O2ge9JQEQOF718tJ3xx1ki2VIkqlkJsfOCGag/Cj/onFrMSvFD8AFCPBPd6LvKfeHvpC oUSA== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HNEwlAX+; spf=pass (google.com: domain of jpewhacker@gmail.com designates 2607:f8b0:4864:20::d44 as permitted sender) smtp.mailfrom=jpewhacker@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com. [2607:f8b0:4864:20::d44]) by gmr-mx.google.com with ESMTPS id b128si109604oii.3.2020.06.26.14.10.40 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Jun 2020 14:10:40 -0700 (PDT) Received-SPF: pass (google.com: domain of jpewhacker@gmail.com designates 2607:f8b0:4864:20::d44 as permitted sender) client-ip=2607:f8b0:4864:20::d44; Received: by mail-io1-xd44.google.com with SMTP id m81so11313428ioa.1 for ; Fri, 26 Jun 2020 14:10:40 -0700 (PDT) X-Received: by 2002:a6b:7210:: with SMTP id n16mr5404358ioc.177.1593205839476; Fri, 26 Jun 2020 14:10:39 -0700 (PDT) Received: from localhost.localdomain ([2605:a601:ac3d:c100:b57b:f342:9dbb:77c7]) by smtp.gmail.com with ESMTPSA id s2sm11179634iob.17.2020.06.26.14.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jun 2020 14:10:38 -0700 (PDT) From: Joshua Watt To: swupdate@googlegroups.com Cc: Joshua Watt Subject: [swupdate] [PATCH] lua: Prevent mtdname and path from clobering each other Date: Fri, 26 Jun 2020 16:10:36 -0500 Message-Id: <20200626211036.7535-1-JPEWhacker@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Original-Sender: JPEWhacker@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HNEwlAX+; spf=pass (google.com: domain of jpewhacker@gmail.com designates 2607:f8b0:4864:20::d44 as permitted sender) smtp.mailfrom=jpewhacker@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , The lua properties "mtdname" and "path" are both initialized to the path property in struct img_type, and both values were written back to the path property when update struct img_type from the lua table. This means that which ever one was encountered last when iterating would be the one assigned to actual path property. This in particular causes problems when attempting to chain to another handler that uses the path property from lua, for example the following lua code could randomly fail if mtdname was parsed last when chaining handlers: function my_handler(image) image.path = "/foo/bar" swupdate.call_handler("archive", image) end To resolve this, implement custom __index and __newindex metamethods that alias "mtdname" to "path". This also fixed that "private" properties could be accessed directly as image["offset"], since the __index table was the private table (meaning any missing key lookups would fall back to keys of the private table). Now, they can only be accessed through the "_private" property, as in image["_private"]["offset"] (and as documented) Signed-off-by: Joshua Watt --- corelib/lua_interface.c | 67 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c index 219ee41..e4c300a 100644 --- a/corelib/lua_interface.c +++ b/corelib/lua_interface.c @@ -278,9 +278,6 @@ static void lua_string_to_img(struct img_type *img, const char *key, if (!strcmp(key, "device")) strncpy(img->device, value, sizeof(img->device)); - if (!strcmp(key, "mtdname")) - strncpy(img->path, value, - sizeof(img->path)); if (!strcmp(key, "path")) strncpy(img->path, value, sizeof(img->path)); @@ -477,7 +474,6 @@ static void update_table(lua_State* L, struct img_type *img) LUA_PUSH_IMG_STRING(img, "type", type); LUA_PUSH_IMG_STRING(img, "device", device); LUA_PUSH_IMG_STRING(img, "path", path); - LUA_PUSH_IMG_STRING(img, "mtdname", path); LUA_PUSH_IMG_STRING(img, "data", type_data); LUA_PUSH_IMG_STRING(img, "filesystem", filesystem); LUA_PUSH_IMG_STRING(img, "ivt", ivt_ascii); @@ -562,6 +558,58 @@ static int l_istream_fclose(lua_State *L) #endif #endif +static int imagetable_getindex(lua_State* L) +{ + if (lua_isstring(L, -1)) { + char const* key = lua_tostring(L, -1); + + if (!strcmp(key, "_private")) { + /* Look up _private in the metatable */ + lua_getmetatable(L, -2); + lua_pushstring(L, key); + lua_rawget(L, -2); + /* Remove metatable from stack */ + lua_remove(L, -2); + return 1; + } + + if (!strcmp(key, "mtdname")) + key = "path"; + + lua_pushstring(L, key); + } else { + lua_pushvalue(L, -1); + } + + lua_rawget(L, -3); + + return 1; +} + +static int imagetable_newindex(lua_State* L) +{ + if (lua_isstring(L, -2)) { + char const* key = lua_tostring(L, -2); + + /* Private cannot be set */ + if (!strcmp(key, "_private")) + return 0; + + if (!strcmp(key, "mtdname")) + key = "path"; + + lua_pushstring(L, key); + lua_pushvalue(L, -2); + } else { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + } + + lua_rawset(L, -5); + + return 0; +} + static void image2table(lua_State* L, struct img_type *img) { if (L && img) { @@ -577,13 +625,22 @@ static void image2table(lua_State* L, struct img_type *img) * accessible by image["_private"]["offset"]. * This access pattern strongly hints not to mess with the * image["_private"] table values from within the Lua realm. + * + * Custom __index and __newindex methods are used so that + * "mtdname" can be an alias of "path" */ lua_newtable(L); - lua_pushvalue(L, -1); + lua_pushstring(L, "_private"); lua_newtable(L); lua_settable(L, -3); + + lua_pushcfunction(L, imagetable_getindex); lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, imagetable_newindex); + lua_setfield(L, -2, "__newindex"); + lua_setmetatable(L, -2); update_table(L, img);