ipl_tools/cmd_lsshut.c: Replace /sys mount point with util_path_sysfs

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 <japo@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Polensky
2025-02-26 16:00:59 +01:00
committed by Jan Höppner
parent aea2ba8728
commit 4c301d47dd

View File

@@ -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)