From 4c301d47ddf5e915a42fced25e006362b6710997 Mon Sep 17 00:00:00 2001 From: Jan Polensky Date: Wed, 26 Feb 2025 16:00:59 +0100 Subject: [PATCH] ipl_tools/cmd_lsshut.c: Replace /sys mount point with util_path_sysfs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The default sysfs path is always set to '/sys' unless the SYSFS_ROOT environment variable is defined. To address security concerns, secure_getenv() is used within util_path_sysfs() to protect against malicious values in SYSFS_ROOT. Additionally, constructing the sysfs path dynamically in an allocated buffer, rather than using a fixed-size buffer, helps prevent potential buffer overflows. These modifications also significantly improve testability by allowing sysfs read operations to be redirected to an alternative file path, which enables testing without affecting the active system state. Signed-off-by: Jan Polensky Reviewed-by: Jan Höppner Signed-off-by: Jan Höppner --- ipl_tools/cmd_lsshut.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ipl_tools/cmd_lsshut.c b/ipl_tools/cmd_lsshut.c index 164567e4..06e7419a 100644 --- a/ipl_tools/cmd_lsshut.c +++ b/ipl_tools/cmd_lsshut.c @@ -9,6 +9,7 @@ * it under the terms of the MIT license. See LICENSE for details. */ +#include "lib/util_path.h" #include "ipl_tools.h" static const char *const usage_lsshut = @@ -80,12 +81,17 @@ static void print_kdump(void) { struct stat sb; char tmp[1024]; + char *path; - if (stat("/sys/kernel/kexec_crash_loaded", &sb) != 0) + path = util_path_sysfs("kernel/kexec_crash_loaded"); + if (stat(path, &sb) != 0) { + free(path); return; - read_str(tmp, "/sys/kernel/kexec_crash_loaded", sizeof(tmp)); + } + read_str(tmp, path, sizeof(tmp)); if (strncmp(tmp, "1", 1) == 0) printf("kdump,"); + free(path); } static void shutdown_trigger_print(struct shutdown_trigger *trigger)