Skip to content

Commit 3e34104

Browse files
committed
tools subcmd: Tighten the filename size in check_if_command_finished
JIRA: https://issues.redhat.com/browse/RHEL-78198 upstream ======== commit 478272d Author: Ian Rogers <irogers@google.com> Date: Thu Jul 17 08:08:53 2025 -0700 description =========== FILENAME_MAX is often PATH_MAX (4kb), far more than needed for the /proc path. Make the buffer size sufficient for the maximum integer plus "/proc/" and "/status" with a '\0' terminator. Fixes: 5ce42b5 ("tools subcmd: Add non-waitpid check_if_command_finished()") Signed-off-by: Ian Rogers <irogers@google.com> Link: https://lore.kernel.org/r/20250717150855.1032526-1-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Michael Petlan <mpetlan@redhat.com>
1 parent 2ea5ec3 commit 3e34104

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tools/lib/subcmd/run-command.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <ctype.h>
66
#include <fcntl.h>
77
#include <string.h>
8+
#include <linux/compiler.h>
89
#include <linux/string.h>
910
#include <errno.h>
1011
#include <sys/wait.h>
@@ -216,18 +217,28 @@ static int wait_or_whine(struct child_process *cmd, bool block)
216217
return result;
217218
}
218219

220+
/*
221+
* Conservative estimate of number of characaters needed to hold an a decoded
222+
* integer, assume each 3 bits needs a character byte and plus a possible sign
223+
* character.
224+
*/
225+
#ifndef is_signed_type
226+
#define is_signed_type(type) (((type)(-1)) < (type)1)
227+
#endif
228+
#define MAX_STRLEN_TYPE(type) (sizeof(type) * 8 / 3 + (is_signed_type(type) ? 1 : 0))
229+
219230
int check_if_command_finished(struct child_process *cmd)
220231
{
221232
#ifdef __linux__
222-
char filename[FILENAME_MAX + 12];
233+
char filename[6 + MAX_STRLEN_TYPE(typeof(cmd->pid)) + 7 + 1];
223234
char status_line[256];
224235
FILE *status_file;
225236

226237
/*
227238
* Check by reading /proc/<pid>/status as calling waitpid causes
228239
* stdout/stderr to be closed and data lost.
229240
*/
230-
sprintf(filename, "/proc/%d/status", cmd->pid);
241+
sprintf(filename, "/proc/%u/status", cmd->pid);
231242
status_file = fopen(filename, "r");
232243
if (status_file == NULL) {
233244
/* Open failed assume finish_command was called. */

0 commit comments

Comments
 (0)