Skip to contentSkip to conversation

util/envlist: fix prefix-match in name lookup

PATCH Applied Denis V. Lunev via qemu development 3 participants 7 messages May 20 – Jun 3 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover util/envlist: fix prefix-match in name lookup

A bug report against our downstream tree turned out to have its root cause in plain mainstream code: envlist_unsetenv() does a prefix-match lookup that drops the wrong entry when one stored name happens to be a prefix of another. The downstream symptom is specific to our setup and isn't interesting here -- the underlying lookup mistake is the part worth fixing, and it is reachable from a normal qemu-user invocation through the -U command-line option, so the fix belongs upstream.

Patch 1 fixes the lookup: each entry now stores its name length at insertion time, and a tiny helper compares with explicit length equality plus memcmp. envlist_setenv()'s self-search was accidentally safe (it included the '=' byte in its strncmp window and that '=' served as a boundary) but is converted to the same helper so the name boundary becomes a structural property of the entry rather than a property of its byte layout. Without that, the two sites can easily drift apart again.

Patch 2 backfils test coverage for util/envlist -- there was none -- in tests/unit/test-envlist. I verified that the regression case (envlist_unsetenv("FOO") vs. a stored "FOOBAR=...") fails against the pre-fix code and passes after the fix.

Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
util/envlist: fix prefix-match in envlist_unsetenv() name lookup tests/unit: add test-envlist covering setenv/unsetenv name matching

 tests/unit/meson.build    |   1 +
 tests/unit/test-envlist.c | 196 ++++++++++++++++++++++++++++++++++++++
 util/envlist.c            |  19 +++-
 3 files changed, 212 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit/test-envlist.c
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/2 util/envlist: fix prefix-match in envlist_unsetenv() name lookup

envlist_unsetenv() looked up the entry to remove with strncmp(entry->ev_var, env, strlen(env)). The comparison length is the requested name's length, so any stored entry whose name *starts* with that name compares equal. envlist_setenv() inserts at the head of the list, so the first hit wins: with FOO=... stored first and FOOBAR=... stored afterward, envlist_unsetenv("FOO") iterates from the head, matches FOOBAR=... on the prefix, and drops it instead of FOO=...

linux-user and bsd-user reach this code via the -U command-line switch, so the bug is reachable from a normal qemu-user invocation.

envlist_setenv() used the same strncmp pattern but with envname_len = (eq_sign - env + 1), so the '=' byte sat inside the compared window and acted as an implicit boundary. setenv was therefore not buggy -- but the safety lived in the byte layout of ev_var rather than in the entry, so a future edit could easily drift the two sites apart again.

Store the name length on each entry at insertion time and compare with explicit length equality plus memcmp via a small helper. Use the helper at both lookup sites so the boundary becomes a structural property of the entry: envlist_unsetenv() stops prefix-matching, and envlist_setenv()'s self-search no longer depends on the '=' byte serving as a sentinel.

Fixes 04a6dfebb6b5 ("linux-user: Add generic env variable handling")
Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 util/envlist.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
util/envlist.c +15 -4
index 15fdbb109d..196c92c190 100644
@@ -3,7 +3,8 @@
 #include "qemu/envlist.h"
 
 struct envlist_entry {
-    const char *ev_var;            /* actual env value */
+    const char *ev_var;            /* actual env value: "NAME=VALUE" */
+    size_t ev_name_len;            /* length of NAME (offset of '=') */
     QLIST_ENTRY(envlist_entry) ev_link;
 };
 
@@ -12,6 +13,13 @@ struct envlist {
     size_t el_count;                        /* number of entries */
 };
 
+static inline bool envlist_name_eq(const struct envlist_entry *entry,
+                                   const char *name, size_t name_len)
+{
+    return entry->ev_name_len == name_len &&
+           memcmp(entry->ev_var, name, name_len) == 0;
+}
+
 /*
  * Allocates new envlist and returns pointer to it.
  */
@@ -67,7 +75,7 @@ envlist_setenv(envlist_t *envlist, const char *env)
     /* find out first equals sign in given env */
     if ((eq_sign = strchr(env, '=')) == NULL)
         return (EINVAL);
-    envname_len = eq_sign - env + 1;
+    envname_len = eq_sign - env;
 
     /*
      * If there already exists variable with given name
@@ -76,8 +84,9 @@ envlist_setenv(envlist_t *envlist, const char *env)
      */
     for (entry = envlist->el_entries.lh_first; entry != NULL;
         entry = entry->ev_link.le_next) {
-        if (strncmp(entry->ev_var, env, envname_len) == 0)
+        if (envlist_name_eq(entry, env, envname_len)) {
             break;
+        }
     }
 
     if (entry != NULL) {
@@ -90,6 +99,7 @@ envlist_setenv(envlist_t *envlist, const char *env)
 
     entry = g_malloc(sizeof(*entry));
     entry->ev_var = g_strdup(env);
+    entry->ev_name_len = envname_len;
     QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
 
     return (0);
@@ -119,8 +129,9 @@ envlist_unsetenv(envlist_t *envlist, const char *env)
     envname_len = strlen(env);
     for (entry = envlist->el_entries.lh_first; entry != NULL;
         entry = entry->ev_link.le_next) {
-        if (strncmp(entry->ev_var, env, envname_len) == 0)
+        if (envlist_name_eq(entry, env, envname_len)) {
             break;
+        }
     }
     if (entry != NULL) {
         QLIST_REMOVE(entry, ev_link);
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/2 tests/unit: add test-envlist covering setenv/unsetenv name matching

In reply to Denis V. Lunev via qemu development

util/envlist had no test coverage. Add tests/unit/test-envlist exercising the public envlist API and pinning down the prefix-match hazard fixed in the previous commit:

- envlist_unsetenv("FOO") must not remove an entry named "FOOBAR";
- envlist_setenv("FOO=...") must not replace an existing "FOOBAR=..." entry placed earlier in the list (envlist_setenv() inserts at the head, so the first prefix match wins under the old strncmp rule).

Also cover the rest of the contract: head-insertion order observed through envlist_to_environ(), replacement of an existing variable, the count argument of envlist_to_environ(), and the documented EINVAL paths (NULL inputs, setenv without '=', unsetenv with '=').

Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 tests/unit/meson.build    |   1 +
 tests/unit/test-envlist.c | 196 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 197 insertions(+)
 create mode 100644 tests/unit/test-envlist.c
tests/unit/meson.build +1 -0
index de64f9501f..01cc540a45 100644
@@ -49,6 +49,7 @@ tests = {
   'test-qapi-util': [],
   'test-interval-tree': [],
   'test-fifo': [],
+  'test-envlist': [],
 }
 
 if have_system or have_tools
tests/unit/test-envlist.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.51.0
#3 lore raw
In reply to Denis V. Lunev via qemu development
Quoted 31 lines · Denis V. Lunev

A bug report against our downstream tree turned out to have its root cause in plain mainstream code: envlist_unsetenv() does a prefix-match lookup that drops the wrong entry when one stored name happens to be a prefix of another. The downstream symptom is specific to our setup and isn't interesting here -- the underlying lookup mistake is the part worth fixing, and it is reachable from a normal qemu-user invocation through the -U command-line option, so the fix belongs upstream.

Patch 1 fixes the lookup: each entry now stores its name length at insertion time, and a tiny helper compares with explicit length equality plus memcmp. envlist_setenv()'s self-search was accidentally safe (it included the '=' byte in its strncmp window and that '=' served as a boundary) but is converted to the same helper so the name boundary becomes a structural property of the entry rather than a property of its byte layout. Without that, the two sites can easily drift apart again.

Patch 2 backfils test coverage for util/envlist -- there was none -- in tests/unit/test-envlist. I verified that the regression case (envlist_unsetenv("FOO") vs. a stored "FOOBAR=...") fails against the pre-fix code and passes after the fix.

Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
util/envlist: fix prefix-match in envlist_unsetenv() name lookup tests/unit: add test-envlist covering setenv/unsetenv name matching

Quoted 6 lines
 tests/unit/meson.build    |   1 +
 tests/unit/test-envlist.c | 196 ++++++++++++++++++++++++++++++++++++++
 util/envlist.c            |  19 +++-
 3 files changed, 212 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit/test-envlist.c

ping

#4 lore raw
Quoted 32 lines · Denis V. Lunev
Quoted 31 lines · Denis V. Lunev

A bug report against our downstream tree turned out to have its root cause in plain mainstream code: envlist_unsetenv() does a prefix-match lookup that drops the wrong entry when one stored name happens to be a prefix of another. The downstream symptom is specific to our setup and isn't interesting here -- the underlying lookup mistake is the part worth fixing, and it is reachable from a normal qemu-user invocation through the -U command-line option, so the fix belongs upstream.

Patch 1 fixes the lookup: each entry now stores its name length at insertion time, and a tiny helper compares with explicit length equality plus memcmp. envlist_setenv()'s self-search was accidentally safe (it included the '=' byte in its strncmp window and that '=' served as a boundary) but is converted to the same helper so the name boundary becomes a structural property of the entry rather than a property of its byte layout. Without that, the two sites can easily drift apart again.

Patch 2 backfils test coverage for util/envlist -- there was none -- in tests/unit/test-envlist. I verified that the regression case (envlist_unsetenv("FOO") vs. a stored "FOOBAR=...") fails against the pre-fix code and passes after the fix.

Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
util/envlist: fix prefix-match in envlist_unsetenv() name lookup tests/unit: add test-envlist covering setenv/unsetenv name matching

Quoted 5 lines
Quoted 5 lines
 tests/unit/meson.build    |   1 +
 tests/unit/test-envlist.c | 196 ++++++++++++++++++++++++++++++++++++++
 util/envlist.c            |  19 +++-
 3 files changed, 212 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit/test-envlist.c

CCing Laurent, Helge, and Warner in case they are interested in this -user fix.

Stefan

Reviewed-by
#5 lore raw
In reply to Denis V. Lunev
Quoted 32 lines · Denis V. Lunev
Quoted 31 lines · Denis V. Lunev

A bug report against our downstream tree turned out to have its root cause in plain mainstream code: envlist_unsetenv() does a prefix-match lookup that drops the wrong entry when one stored name happens to be a prefix of another. The downstream symptom is specific to our setup and isn't interesting here -- the underlying lookup mistake is the part worth fixing, and it is reachable from a normal qemu-user invocation through the -U command-line option, so the fix belongs upstream.

Patch 1 fixes the lookup: each entry now stores its name length at insertion time, and a tiny helper compares with explicit length equality plus memcmp. envlist_setenv()'s self-search was accidentally safe (it included the '=' byte in its strncmp window and that '=' served as a boundary) but is converted to the same helper so the name boundary becomes a structural property of the entry rather than a property of its byte layout. Without that, the two sites can easily drift apart again.

Patch 2 backfils test coverage for util/envlist -- there was none -- in tests/unit/test-envlist. I verified that the regression case (envlist_unsetenv("FOO") vs. a stored "FOOBAR=...") fails against the pre-fix code and passes after the fix.

Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
util/envlist: fix prefix-match in envlist_unsetenv() name lookup tests/unit: add test-envlist covering setenv/unsetenv name matching

Quoted 6 lines
Quoted 6 lines
 tests/unit/meson.build    |   1 +
 tests/unit/test-envlist.c | 196 ++++++++++++++++++++++++++++++++++++++
 util/envlist.c            |  19 +++-
 3 files changed, 212 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit/test-envlist.c

I will way until Wednesday to merge this so that the -user maintainers have a chance to take a look:

#6 lore raw
In reply to Denis V. Lunev
Quoted 32 lines · Denis V. Lunev
Quoted 31 lines · Denis V. Lunev

A bug report against our downstream tree turned out to have its root cause in plain mainstream code: envlist_unsetenv() does a prefix-match lookup that drops the wrong entry when one stored name happens to be a prefix of another. The downstream symptom is specific to our setup and isn't interesting here -- the underlying lookup mistake is the part worth fixing, and it is reachable from a normal qemu-user invocation through the -U command-line option, so the fix belongs upstream.

Patch 1 fixes the lookup: each entry now stores its name length at insertion time, and a tiny helper compares with explicit length equality plus memcmp. envlist_setenv()'s self-search was accidentally safe (it included the '=' byte in its strncmp window and that '=' served as a boundary) but is converted to the same helper so the name boundary becomes a structural property of the entry rather than a property of its byte layout. Without that, the two sites can easily drift apart again.

Patch 2 backfils test coverage for util/envlist -- there was none -- in tests/unit/test-envlist. I verified that the regression case (envlist_unsetenv("FOO") vs. a stored "FOOBAR=...") fails against the pre-fix code and passes after the fix.

Cc Stefan Hajnoczi <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
util/envlist: fix prefix-match in envlist_unsetenv() name lookup tests/unit: add test-envlist covering setenv/unsetenv name matching

Quoted 8 lines
Quoted 6 lines
 tests/unit/meson.build    |   1 +
 tests/unit/test-envlist.c | 196 ++++++++++++++++++++++++++++++++++++++
 util/envlist.c            |  19 +++-
 3 files changed, 212 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit/test-envlist.c

ping

Thanks, applied to staging:
https://gitlab.com/qemu-project/qemu/commits/staging

Stefan