From: Thomas Weißschuh Date: Thu, 22 Dec 2022 23:54:41 +0000 (+0000) Subject: procfs: get_stat_nth: handle braces in process name X-Git-Tag: v2.39-rc1~356^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4848c9c0ea92950eaf83b2a8ef82cc66626695d1;p=thirdparty%2Futil-linux.git procfs: get_stat_nth: handle braces in process name In procfs_process_get_stat_nth() we skip to the end of the process name by looking for the ')' after it. However if the process name itself contains a ')' then find that instead of the correct one. By searching the ')' from the end of the file we can make sure to always find the correct one. Link: https://www.openwall.com/lists/oss-security/2022/12/21/6 --- diff --git a/lib/procfs.c b/lib/procfs.c index 1789505326..20fa514a0c 100644 --- a/lib/procfs.c +++ b/lib/procfs.c @@ -191,7 +191,7 @@ int procfs_process_get_stat_nth(struct path_cxt *pc, int n, uintmax_t *re) return ul_strtou64(tok, re, 10); /* skip rest of the process name */ - if (i == 2 && (p = strchr(key, ')'))) + if (i == 2 && (p = strrchr(key, ')'))) key = p + 2; } diff --git a/tests/expected/lib/procfs-one-process b/tests/expected/lib/procfs-one-process index 62c89c7209..db953c07c8 100644 --- a/tests/expected/lib/procfs-one-process +++ b/tests/expected/lib/procfs-one-process @@ -8,3 +8,7 @@ bar' COMM: 'foo bar' +3 + UID: [redacted] + CMDLINE: './foo )bar' + COMM: 'foo )bar' diff --git a/tests/expected/lib/procfs-stat-nth b/tests/expected/lib/procfs-stat-nth index e9d2bab139..7ed534a036 100644 --- a/tests/expected/lib/procfs-stat-nth +++ b/tests/expected/lib/procfs-stat-nth @@ -2,3 +2,5 @@ 1: 4th 373752 2: 1th 1583 2: 4th 1165 +3: 1th 4102 +3: 4th 1165 diff --git a/tests/ts/lib/procfs b/tests/ts/lib/procfs index 4f272e80e6..998aa395bd 100755 --- a/tests/ts/lib/procfs +++ b/tests/ts/lib/procfs @@ -34,6 +34,7 @@ ts_init_subtest "one-process" test_cmd --one 1 test_cmd --one 2 +test_cmd --one 3 ts_finalize_subtest @@ -48,6 +49,9 @@ test_cmd --stat-nth 1 4 test_cmd --stat-nth 2 1 test_cmd --stat-nth 2 4 +test_cmd --stat-nth 3 1 +test_cmd --stat-nth 3 4 + ts_finalize_subtest ts_finalize diff --git a/tests/ts/lib/procfs-data/proc/3/cmdline b/tests/ts/lib/procfs-data/proc/3/cmdline new file mode 100644 index 0000000000..4840b32a57 Binary files /dev/null and b/tests/ts/lib/procfs-data/proc/3/cmdline differ diff --git a/tests/ts/lib/procfs-data/proc/3/comm b/tests/ts/lib/procfs-data/proc/3/comm new file mode 100644 index 0000000000..57bf5bb066 --- /dev/null +++ b/tests/ts/lib/procfs-data/proc/3/comm @@ -0,0 +1 @@ +foo )bar diff --git a/tests/ts/lib/procfs-data/proc/3/stat b/tests/ts/lib/procfs-data/proc/3/stat new file mode 100644 index 0000000000..4b5f57592e --- /dev/null +++ b/tests/ts/lib/procfs-data/proc/3/stat @@ -0,0 +1 @@ +4102 (foo )bar) S 1165 4102 1165 34818 4102 4194304 80 0 0 0 0 0 0 0 20 0 1 0 61909 2543616 215 18446744073709551615 94697631760384 94697631760801 140728832669632 0 0 0 0 0 0 1 0 0 17 0 0 0 0 0 0 94697631772112 94697631772712 94697662554112 140728832670811 140728832670822 140728832670822 140728832671725 0