From ddda20c77cd8366df3e22e7ab6b16b3eda5ddb6c Mon Sep 17 00:00:00 2001 From: Will James <39811085+wjames111@users.noreply.github.com> Date: Wed, 7 Oct 2026 12:40:10 -0400 Subject: [PATCH] hal: check strstr() results before dereferencing them hal_identify() and hal_temperature_read() search text read from kernel files (/proc/cmdline for LX_MEM=, /sys/devices/soc0/machine for SSC, the msys TEMP_R node for "Temperature ") and use the result without checking it. When a node is missing, empty or worded differently, strstr() returns NULL and divinus segfaults during identification, or on GET /api/status in the temperature case. Only use each value when the read succeeded and the match was found, and make sure the SSC match is long enough for the three characters copied out of it. --- src/hal/support.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/hal/support.c b/src/hal/support.c index e261bca6..30cf1050 100644 --- a/src/hal/support.c +++ b/src/hal/support.c @@ -34,16 +34,21 @@ void hal_identify(void) { vid_thread = i6_video_thread; if (file = fopen("/proc/cmdline", "r")) { - fgets(line, 200, file); - char *remain, *capacity = strstr(line, "LX_MEM="); - memory = (short)(strtol(capacity + 7, &remain, 16) >> 20); + char *remain, *capacity = NULL; + if (fgets(line, 200, file)) + capacity = strstr(line, "LX_MEM="); + if (capacity) + memory = (short)(strtol(capacity + 7, &remain, 16) >> 20); fclose(file); } if (file = fopen("/sys/devices/soc0/machine", "r")) { - fgets(line, 200, file); - char *board = strstr(line, "SSC"); - strncpy(package, board + 4, 3); + char *board = NULL; + if (fgets(line, 200, file)) + board = strstr(line, "SSC"); + /* package takes board[4..6], so the match must be that long */ + if (board && strlen(board) >= 7) + strncpy(package, board + 4, 3); fclose(file); } @@ -338,9 +343,11 @@ float hal_temperature_read(void) { FILE* file; char line[20] = {0}; if (file = fopen("/sys/class/mstar/msys/TEMP_R", "r")) { - fgets(line, 20, file); - char *remain, *parsed = strstr(line, "Temperature "); - lastReadTemp = strtof(parsed + 12, &remain); + char *remain, *parsed = NULL; + if (fgets(line, 20, file)) + parsed = strstr(line, "Temperature "); + if (parsed) + lastReadTemp = strtof(parsed + 12, &remain); fclose(file); } break;