openresty / openresty/lua-nginx-module

Make plugin binary patch safe

Open
#1,686 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

I was attempting to build a conda package for openresty at work. Conda lets you install pre-compiled binary packages to user selected prefixes. For binaries, it patches the installed binaries to replace the hard coded old prefixes with new ones padded with a bunch of \x0.

This didn't work for my case because of the use of lua_pushliteral() which allows you to send null characters as part of the string. So at the end (after bin-patching), the package.path and package.cpath become:
<hard-coded-default-patched-for-new-prefix>\x0\x0...\x0<whatever-path-was-before>
and then the old path gets ignored by lua.

The following patch fixes this problem, but I am not sure if it is the right fix.

From cef7b49268e320df9130146455d3f99b71fe74dd Mon Sep 17 00:00:00 2001
From: Nehal J Wani <nehaljw.kkd1@gmail.com>
Date: Sat, 4 Apr 2020 10:36:15 -0400
Subject: [PATCH] Use LUA_DEFAULT_PATH and LUA_DEFAULT_CPATH in a bin patch
 safe manner

Change-Id: I09ae467182dc1b171778e296da9beda293b9e28e
---
 ngx_lua-0.10.15/src/ngx_http_lua_util.c        | 4 ++--
 ngx_stream_lua-0.0.7/src/ngx_stream_lua_util.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/ngx_lua-0.10.15/src/ngx_http_lua_util.c b/ngx_lua-0.10.15/src/ngx_http_lua_util.c
index 88e9986..4e1e0c4 100644
--- a/ngx_lua-0.10.15/src/ngx_http_lua_util.c
+++ b/ngx_lua-0.10.15/src/ngx_http_lua_util.c
@@ -267,7 +267,7 @@ ngx_http_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
                        "lua prepending default package.path with %s",
                        LUA_DEFAULT_PATH);

-        lua_pushliteral(L, LUA_DEFAULT_PATH ";"); /* package default */
+        lua_pushstring(L, LUA_DEFAULT_PATH ";"); /* package default */
         lua_getfield(L, -2, "path"); /* package default old */
         old_path = lua_tolstring(L, -1, &old_path_len);
         lua_concat(L, 2); /* package new */
@@ -280,7 +280,7 @@ ngx_http_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
                        "lua prepending default package.cpath with %s",
                        LUA_DEFAULT_CPATH);

-        lua_pushliteral(L, LUA_DEFAULT_CPATH ";"); /* package default */
+        lua_pushstring(L, LUA_DEFAULT_CPATH ";"); /* package default */
         lua_getfield(L, -2, "cpath"); /* package default old */
         old_cpath = lua_tolstring(L, -1, &old_cpath_len);
         lua_concat(L, 2); /* package new */
diff --git a/ngx_stream_lua-0.0.7/src/ngx_stream_lua_util.c b/ngx_stream_lua-0.0.7/src/ngx_stream_lua_util.c
index 463938b..2d8deaa 100644
--- a/ngx_stream_lua-0.0.7/src/ngx_stream_lua_util.c
+++ b/ngx_stream_lua-0.0.7/src/ngx_stream_lua_util.c
@@ -249,7 +249,7 @@ ngx_stream_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
                        "lua prepending default package.path with %s",
                        LUA_DEFAULT_PATH);

-        lua_pushliteral(L, LUA_DEFAULT_PATH ";"); /* package default */
+        lua_pushstring(L, LUA_DEFAULT_PATH ";"); /* package default */
         lua_getfield(L, -2, "path"); /* package default old */
         old_path = lua_tolstring(L, -1, &old_path_len);
         lua_concat(L, 2); /* package new */
@@ -262,7 +262,7 @@ ngx_stream_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
                        "lua prepending default package.cpath with %s",
                        LUA_DEFAULT_CPATH);

-        lua_pushliteral(L, LUA_DEFAULT_CPATH ";"); /* package default */
+        lua_pushstring(L, LUA_DEFAULT_CPATH ";"); /* package default */
         lua_getfield(L, -2, "cpath"); /* package default old */
         old_cpath = lua_tolstring(L, -1, &old_cpath_len);
         lua_concat(L, 2); /* package new */
--
2.17.1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review ngx_lua-0.10.15/src/ngx_http_lua_util.c and ngx_stream_lua-0.0.7/src/ngx_stream_lua_util.c, focusing on the ngx_*_lua_new_state functions where LUA_DEFAULT_PATH and LUA_DEFAULT_CPATH are pushed. Reproduce the prefix-patching scenario described in the issue and verify that package.path and package.cpath retain the existing path after patching. Done means confirming the safe behavior in both HTTP and stream implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.