util/envlist: fix prefix-match in name lookup
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.
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
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.
--- util/envlist.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
util/envlist.c +15 -4
@@ -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
PATCH 2/2 tests/unit: add test-envlist covering setenv/unsetenv name matching
In reply to Denis V. Lunev via qemu developmentutil/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 '=').
--- 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
@@ -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
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.Signed-off-by Denis V. Lunev <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Markus Armbruster <[email protected]>Cc Paolo Bonzini <[email protected]>Denis V. Lunev (2):
util/envlist: fix prefix-match inenvlist_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
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.Signed-off-by Denis V. Lunev <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Markus Armbruster <[email protected]>Cc Paolo Bonzini <[email protected]>Denis V. Lunev (2):
util/envlist: fix prefix-match inenvlist_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
Stefan Hajnoczi
Reviewed-byQuoted 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.Signed-off-by Denis V. Lunev <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Markus Armbruster <[email protected]>Cc Paolo Bonzini <[email protected]>Denis V. Lunev (2):
util/envlist: fix prefix-match inenvlist_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:
Stefan Hajnoczi
AppliedQuoted 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.Signed-off-by Denis V. Lunev <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Markus Armbruster <[email protected]>Cc Paolo Bonzini <[email protected]>Denis V. Lunev (2):
util/envlist: fix prefix-match inenvlist_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.cping
Thanks, applied to staging:
https://gitlab.com/qemu-project/qemu/commits/staging
Stefan