diff mbox series

Lua: use explicit size for embedded Lua handler script

Message ID 20180305152316.14928-1-christian.storm@siemens.com
State Accepted
Headers show
Series Lua: use explicit size for embedded Lua handler script | expand

Commit Message

Storm, Christian March 5, 2018, 3:23 p.m. UTC
The embedded Lua handler script string may not be null-
terminated in the .data section, depending on a padding
inserted or not. Hence, don't let Lua rely on strlen()
when loading the script but calculate the size explicitly.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 Makefile.flags          | 3 ++-
 corelib/lua_interface.c | 8 +++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

Comments

Stefano Babic March 5, 2018, 6:12 p.m. UTC | #1
On 05/03/2018 16:23, Christian Storm wrote:
> The embedded Lua handler script string may not be null-
> terminated in the .data section, depending on a padding
> inserted or not. Hence, don't let Lua rely on strlen()
> when loading the script but calculate the size explicitly.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  Makefile.flags          | 3 ++-
>  corelib/lua_interface.c | 8 +++++---
>  2 files changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/Makefile.flags b/Makefile.flags
> index 207eace..dac790f 100644
> --- a/Makefile.flags
> +++ b/Makefile.flags
> @@ -106,7 +106,8 @@ ifeq ($(CONFIG_HANDLER_IN_LUA),y)
>  ifeq ($(CONFIG_EMBEDDED_LUA_HANDLER),y)
>  ifneq ($(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE),)	
>  LDFLAGS_swupdate += -Wl,--format=binary -Wl,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE) -Wl,--format=default
> -KBUILD_CPPFLAGS += -DEMBEDDED_LUA_SRC="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE))))_start"
> +KBUILD_CPPFLAGS += -DEMBEDDED_LUA_SRC_START="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE))))_start"
> +KBUILD_CPPFLAGS += -DEMBEDDED_LUA_SRC_END="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE))))_end"
>  endif
>  endif
>  endif
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index e96fbe6..3212ea8 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -24,7 +24,8 @@
>  #define LUA_TYPE_HANDLER 2
>  
>  #if defined(CONFIG_EMBEDDED_LUA_HANDLER)
> -extern const char EMBEDDED_LUA_SRC[];
> +extern const char EMBEDDED_LUA_SRC_START[];
> +extern const char EMBEDDED_LUA_SRC_END[];
>  #endif
>  
>  #define LUA_PUSH_IMG_STRING(img, attr, field)  do { \
> @@ -1021,12 +1022,13 @@ int lua_handlers_init(void)
>  		lua_pop(gL, 1); /* remove unused copy left on stack */
>  		/* try to load Lua handlers for the swupdate system */
>  #if defined(CONFIG_EMBEDDED_LUA_HANDLER)
> -		if ((ret = luaL_dostring(gL, EMBEDDED_LUA_SRC)) != 0) {
> +		if ((ret = (luaL_loadbuffer(gL, EMBEDDED_LUA_SRC_START, EMBEDDED_LUA_SRC_END-EMBEDDED_LUA_SRC_START, "LuaHandler") ||
> +					lua_pcall(gL, 0, LUA_MULTRET, 0))) != 0) {
>  			INFO("No compiled-in Lua handler(s) found.");
>  			TRACE("Lua exception:\n%s", lua_tostring(gL, -1));
>  			lua_pop(gL, 1);
>  		} else {
> -			INFO("Compiled-in Lua handler(s) found.");
> +			INFO("Compiled-in Lua handler(s) found and loaded.");
>  		}
>  #else
>  		if ((ret = luaL_dostring(gL, "require (\"swupdate_handlers\")")) != 0) {
> 

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/Makefile.flags b/Makefile.flags
index 207eace..dac790f 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -106,7 +106,8 @@  ifeq ($(CONFIG_HANDLER_IN_LUA),y)
 ifeq ($(CONFIG_EMBEDDED_LUA_HANDLER),y)
 ifneq ($(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE),)	
 LDFLAGS_swupdate += -Wl,--format=binary -Wl,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE) -Wl,--format=default
-KBUILD_CPPFLAGS += -DEMBEDDED_LUA_SRC="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE))))_start"
+KBUILD_CPPFLAGS += -DEMBEDDED_LUA_SRC_START="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE))))_start"
+KBUILD_CPPFLAGS += -DEMBEDDED_LUA_SRC_END="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_LUA_HANDLER_SOURCE))))_end"
 endif
 endif
 endif
diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index e96fbe6..3212ea8 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -24,7 +24,8 @@ 
 #define LUA_TYPE_HANDLER 2
 
 #if defined(CONFIG_EMBEDDED_LUA_HANDLER)
-extern const char EMBEDDED_LUA_SRC[];
+extern const char EMBEDDED_LUA_SRC_START[];
+extern const char EMBEDDED_LUA_SRC_END[];
 #endif
 
 #define LUA_PUSH_IMG_STRING(img, attr, field)  do { \
@@ -1021,12 +1022,13 @@  int lua_handlers_init(void)
 		lua_pop(gL, 1); /* remove unused copy left on stack */
 		/* try to load Lua handlers for the swupdate system */
 #if defined(CONFIG_EMBEDDED_LUA_HANDLER)
-		if ((ret = luaL_dostring(gL, EMBEDDED_LUA_SRC)) != 0) {
+		if ((ret = (luaL_loadbuffer(gL, EMBEDDED_LUA_SRC_START, EMBEDDED_LUA_SRC_END-EMBEDDED_LUA_SRC_START, "LuaHandler") ||
+					lua_pcall(gL, 0, LUA_MULTRET, 0))) != 0) {
 			INFO("No compiled-in Lua handler(s) found.");
 			TRACE("Lua exception:\n%s", lua_tostring(gL, -1));
 			lua_pop(gL, 1);
 		} else {
-			INFO("Compiled-in Lua handler(s) found.");
+			INFO("Compiled-in Lua handler(s) found and loaded.");
 		}
 #else
 		if ((ret = luaL_dostring(gL, "require (\"swupdate_handlers\")")) != 0) {