From: Nikola Forró Date: Thu, 16 Jan 2020 17:43:50 +0000 (+0100) Subject: api.c: Fix potential buffer overflow X-Git-Tag: v0.42.1~1^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76b745519e77f83d207a2e0ddc332e812797543b;p=thirdparty%2Flibcgroup.git api.c: Fix potential buffer overflow It is assumed that arguments read from /proc//cmdline don't exceed buf_pname buffer size, which is FILENAME_MAX - 1 characters, but that's not always the case. Add check to prevent buffer overflow and discard the excessive part of an argument. Signed-off-by: Nikola Forró Reviewed-by: Tom Hromatka --- diff --git a/src/api.c b/src/api.c index 92730e6a..959a8140 100644 --- a/src/api.c +++ b/src/api.c @@ -4601,13 +4601,17 @@ static int cg_get_procname_from_proc_cmdline(pid_t pid, while (c != EOF) { c = fgetc(f); - if ((c != EOF) && (c != '\0')) { + if ((c != EOF) && (c != '\0') && (len < FILENAME_MAX - 1)) { buf_pname[len] = c; len++; continue; } buf_pname[len] = '\0'; + if (len == FILENAME_MAX - 1) + while ((c != EOF) && (c != '\0')) + c = fgetc(f); + /* * The taken process name from /proc//status is * shortened to 15 characters if it is over. So the