Skip to content

Commit 73f6e67

Browse files
committed
conf: fix one-byte OOB read/write underflow in chomp()
chomp() trims trailing newlines from a line read from a .service config file. For a string consisting of only a newline (str == n, exactly what fgets() produces for a blank line), the pointer p starts at str+0. The loop body zeroes str[0], decrements p to str-1, and then unconditionally dereferences *p to test the loop condition -- a one-byte read before the start of the buffer. If that stray byte also happens to be 0x0A the loop keeps walking backward, turning the OOB read into an OOB write as well. Confirmed with the project's own asan CI flags (-fsanitize=address,undefined -fno-sanitize-recover=undefined): a .service file with a blank line between two directives makes UBSan report 'load of address ... with insufficient space for an object of type char' at this exact line, reachable via the normal parse() -> read_line() -> chomp() path. Bound the pointer before dereferencing it, mirroring the tab indentation used by the rest of the function (the line was previously indented with spaces instead of a tab). Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com>
1 parent e190ef7 commit 73f6e67

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

src/conf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static char *chomp(char *str)
6464
}
6565

6666
p = str + strlen(str) - 1;
67-
while (*p == '\n')
67+
while (p >= str && *p == '\n')
6868
*p-- = 0;
6969

7070
return str;

0 commit comments

Comments
 (0)