]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
procfs: get_stat_nth: handle braces in process name
authorThomas Weißschuh <thomas@t-8ch.de>
Thu, 22 Dec 2022 23:54:41 +0000 (23:54 +0000)
committerThomas Weißschuh <thomas@t-8ch.de>
Sat, 24 Dec 2022 17:32:06 +0000 (17:32 +0000)
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
lib/procfs.c
tests/expected/lib/procfs-one-process
tests/expected/lib/procfs-stat-nth
tests/ts/lib/procfs
tests/ts/lib/procfs-data/proc/3/cmdline [new file with mode: 0644]
tests/ts/lib/procfs-data/proc/3/comm [new file with mode: 0644]
tests/ts/lib/procfs-data/proc/3/stat [new file with mode: 0644]

index 1789505326d91eed375270a8656ff5a51c9292d3..20fa514a0cb359b27c36a5256dc19409048db76c 100644 (file)
@@ -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;
        }
 
index 62c89c72091372555f8cb2aab0268b6761706466..db953c07c8a8e2ea7858ac8430c698bcdc29629d 100644 (file)
@@ -8,3 +8,7 @@
 bar'
    COMM: 'foo
 bar'
+3
+   UID: [redacted]
+   CMDLINE: './foo )bar'
+   COMM: 'foo )bar'
index e9d2bab13965d4920d008ed663aaad03c29beaf0..7ed534a036c376b0dd37f2e705091480a62a7ed8 100644 (file)
@@ -2,3 +2,5 @@
 1: 4th 373752
 2: 1th 1583
 2: 4th 1165
+3: 1th 4102
+3: 4th 1165
index 4f272e80e6f03313c6de35a48eab22bee2d7ab63..998aa395bdce691f888cd221a883e12ff5a2efd5 100755 (executable)
@@ -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 (file)
index 0000000..4840b32
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 (file)
index 0000000..57bf5bb
--- /dev/null
@@ -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 (file)
index 0000000..4b5f575
--- /dev/null
@@ -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