oss-sec mailing list archives
[patch] proc.5: tell how to parse /proc/*/stat correctly
From: Alexey Dobriyan <adobriyan () gmail com>
Date: Thu, 22 Dec 2022 19:42:53 +0300
/proc/*/stat can't be parsed with split() or split(" ") or split(' ') or sscanf("%d (%s) ...") or equivalents because "comm" can contain whitespace and parenthesis and is not escaped by the kernel. BTW escaping would not help with naive split() anyway. Mention strrchr(')') so people can at least stop adding new bugs. Signed-off-by: Alexey Dobriyan <adobriyan () gmail com> --- man5/proc.5 | 5 +++++ 1 file changed, 5 insertions(+) --- a/man5/proc.5 +++ b/man5/proc.5 @@ -2092,6 +2092,11 @@ Strings longer than .B TASK_COMM_LEN (16) characters (including the terminating null byte) are silently truncated. This is visible whether or not the executable is swapped out. + +Note that \fIcomm\fP can contain space and closing parenthesis characters. +Parsing /proc/${pid}/stat with split() or equivalent, or scanf(3) isn't +reliable. The correct way is to locate closing parenthesis with strrchr(')') +from the end of the buffer and parse integers from there. .TP (3) \fIstate\fP \ %c One of the following characters, indicating process state:
Current thread:
- [patch] proc.5: tell how to parse /proc/*/stat correctly Alexey Dobriyan (Dec 22)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Dominique Martinet (Dec 22)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Solar Designer (Dec 22)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Dominique Martinet (Dec 22)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Jan Engelhardt (Dec 22)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Lyndon Nerenberg (VE7TFX/VE6BBM) (Dec 28)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Shawn Webb (Dec 28)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Shawn Webb (Dec 28)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Demi Marie Obenour (Dec 28)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Jan Engelhardt (Dec 28)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Shawn Webb (Dec 28)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Solar Designer (Dec 22)
- Re: [patch] proc.5: tell how to parse /proc/*/stat correctly Dominique Martinet (Dec 22)