diff mbox series

[02/11] Lua: hide but transport SWUpdate-internal attributes

Message ID 20171103123009.18705-2-christian.storm@siemens.com
State Accepted
Headers show
Series [01/11] Lua: expose get_tmpdir() to Lua | expand

Commit Message

Storm, Christian Nov. 3, 2017, 12:30 p.m. UTC
Some SWUpdate-internal img_type attributes should not be
"officially" exposed to the Lua realm but they may be
required when calling, e.g., copyfile() from within a Lua
handler or when chaining handlers, i.e., calling a C handler
from within a Lua handler.

One such example is img_type's offset attribute designating
the offset in the cpio file which is used by, e.g., copyfile().

"Hide" those SWUpdate-internal attributes in a metatable so
that they're not "visible" in the Lua realm, e.g., by
pairs() enumeration, but still can be transported from the
C to the Lua realm and vice versa. Although these "hidden"
attributes can be accessed directly from the Lua realm, the
access pattern, e.g., image["_private"]["offset"], strongly
hints not to mess with the image["_private"] table values
from within the Lua realm.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/lua_interface.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

Comments

Stefano Babic Nov. 7, 2017, 4:18 p.m. UTC | #1
On 03/11/2017 13:30, Christian Storm wrote:
> Some SWUpdate-internal img_type attributes should not be
> "officially" exposed to the Lua realm but they may be
> required when calling, e.g., copyfile() from within a Lua
> handler or when chaining handlers, i.e., calling a C handler
> from within a Lua handler.
> 
> One such example is img_type's offset attribute designating
> the offset in the cpio file which is used by, e.g., copyfile().
> 
> "Hide" those SWUpdate-internal attributes in a metatable so
> that they're not "visible" in the Lua realm, e.g., by
> pairs() enumeration, but still can be transported from the
> C to the Lua realm and vice versa. Although these "hidden"
> attributes can be accessed directly from the Lua realm, the
> access pattern, e.g., image["_private"]["offset"], strongly
> hints not to mess with the image["_private"] table values
> from within the Lua realm.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index a41629f..8b8851e 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -267,6 +267,26 @@ static void lua_number_to_img(struct img_type *img, const char *key,
>  static void image2table(lua_State* L, struct img_type *img) {
>  	if (L && img) {
>  		lua_newtable (L);
> +
> +		/*
> +		 * Create a metatable to "hide" SWUpdate-internal attributes.
> +		 * These are not "visible", e.g., by pairs() enumeration but
> +		 * may be accessed directly, knowing the attribute paths.
> +		 * An example is img_type's offset designating the offset
> +		 * in the cpio file which is used by, e.g., copyfile().
> +		 * While not visible in pairs() enumeration, it is directly
> +		 * accessible by image["_private"]["offset"].
> +		 * This access pattern strongly hints not to mess with the
> +		 * image["_private"] table values from within the Lua realm.
> +		 */
> +		lua_newtable(L);
> +		lua_pushvalue(L, -1);
> +		lua_pushstring(L, "_private");
> +		lua_newtable(L);
> +		lua_settable(L, -3);
> +		lua_setfield(L, -2, "__index");
> +		lua_setmetatable(L, -2);
> +
>  		LUA_PUSH_IMG_STRING(img, "name", id.name);
>  		LUA_PUSH_IMG_STRING(img, "version", id.version);
>  		LUA_PUSH_IMG_STRING(img, "filename", fname);
> @@ -289,6 +309,10 @@ static void image2table(lua_State* L, struct img_type *img) {
>  		LUA_PUSH_IMG_NUMBER(img, "size", size);
>  		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
>  
> +		lua_getfield(L, -1, "_private");
> +		LUA_PUSH_IMG_NUMBER(img, "offset", offset);
> +		lua_pop(L, 1);
> +
>  		char *hashstring = alloca(2 * SHA256_HASH_LENGTH + 1);
>  		hash_to_ascii(img->sha256, hashstring);
>  		lua_pushstring(L, "sha256");
> @@ -315,6 +339,11 @@ static void table2image(lua_State* L, struct img_type *img) {
>  			}
>  			lua_pop(L, 1);
>  		}
> +
> +		lua_getfield(L, -1, "_private");
> +		lua_getfield(L, -1, "offset");
> +		img->offset = (off_t)luaL_checknumber(L, -1);
> +		lua_pop(L,2);
>  	}
>  }
>  
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index a41629f..8b8851e 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -267,6 +267,26 @@  static void lua_number_to_img(struct img_type *img, const char *key,
 static void image2table(lua_State* L, struct img_type *img) {
 	if (L && img) {
 		lua_newtable (L);
+
+		/*
+		 * Create a metatable to "hide" SWUpdate-internal attributes.
+		 * These are not "visible", e.g., by pairs() enumeration but
+		 * may be accessed directly, knowing the attribute paths.
+		 * An example is img_type's offset designating the offset
+		 * in the cpio file which is used by, e.g., copyfile().
+		 * While not visible in pairs() enumeration, it is directly
+		 * accessible by image["_private"]["offset"].
+		 * This access pattern strongly hints not to mess with the
+		 * image["_private"] table values from within the Lua realm.
+		 */
+		lua_newtable(L);
+		lua_pushvalue(L, -1);
+		lua_pushstring(L, "_private");
+		lua_newtable(L);
+		lua_settable(L, -3);
+		lua_setfield(L, -2, "__index");
+		lua_setmetatable(L, -2);
+
 		LUA_PUSH_IMG_STRING(img, "name", id.name);
 		LUA_PUSH_IMG_STRING(img, "version", id.version);
 		LUA_PUSH_IMG_STRING(img, "filename", fname);
@@ -289,6 +309,10 @@  static void image2table(lua_State* L, struct img_type *img) {
 		LUA_PUSH_IMG_NUMBER(img, "size", size);
 		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
 
+		lua_getfield(L, -1, "_private");
+		LUA_PUSH_IMG_NUMBER(img, "offset", offset);
+		lua_pop(L, 1);
+
 		char *hashstring = alloca(2 * SHA256_HASH_LENGTH + 1);
 		hash_to_ascii(img->sha256, hashstring);
 		lua_pushstring(L, "sha256");
@@ -315,6 +339,11 @@  static void table2image(lua_State* L, struct img_type *img) {
 			}
 			lua_pop(L, 1);
 		}
+
+		lua_getfield(L, -1, "_private");
+		lua_getfield(L, -1, "offset");
+		img->offset = (off_t)luaL_checknumber(L, -1);
+		lua_pop(L,2);
 	}
 }