Skip to content

Commit f0e386e

Browse files
jognesspmladek
authored andcommitted
printk: fix buffer overflow potential for print_text()
Before the commit 896fbe2 ("printk: use the lockless ringbuffer"), msg_print_text() would only write up to size-1 bytes into the provided buffer. Some callers expect this behavior and append a terminator to returned string. In particular: arch/powerpc/xmon/xmon.c:dump_log_buf() arch/um/kernel/kmsg_dump.c:kmsg_dumper_stdout() msg_print_text() has been replaced by record_print_text(), which currently fills the full size of the buffer. This causes a buffer overflow for the above callers. Change record_print_text() so that it will only use size-1 bytes for text data. Also, for paranoia sakes, add a terminator after the text data. And finally, document this behavior so that it is clear that only size-1 bytes are used and a terminator is added. Fixes: 896fbe2 ("printk: use the lockless ringbuffer") Cc: [email protected] # 5.10+ Signed-off-by: John Ogness <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Acked-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 89ccf18 commit f0e386e

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

kernel/printk/printk.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,11 +1292,16 @@ static size_t info_print_prefix(const struct printk_info *info, bool syslog,
12921292
* done:
12931293
*
12941294
* - Add prefix for each line.
1295+
* - Drop truncated lines that no longer fit into the buffer.
12951296
* - Add the trailing newline that has been removed in vprintk_store().
1296-
* - Drop truncated lines that do not longer fit into the buffer.
1297+
* - Add a string terminator.
1298+
*
1299+
* Since the produced string is always terminated, the maximum possible
1300+
* return value is @r->text_buf_size - 1;
12971301
*
12981302
* Return: The length of the updated/prepared text, including the added
1299-
* prefixes and the newline. The dropped line(s) are not counted.
1303+
* prefixes and the newline. The terminator is not counted. The dropped
1304+
* line(s) are not counted.
13001305
*/
13011306
static size_t record_print_text(struct printk_record *r, bool syslog,
13021307
bool time)
@@ -1339,26 +1344,31 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
13391344

13401345
/*
13411346
* Truncate the text if there is not enough space to add the
1342-
* prefix and a trailing newline.
1347+
* prefix and a trailing newline and a terminator.
13431348
*/
1344-
if (len + prefix_len + text_len + 1 > buf_size) {
1349+
if (len + prefix_len + text_len + 1 + 1 > buf_size) {
13451350
/* Drop even the current line if no space. */
1346-
if (len + prefix_len + line_len + 1 > buf_size)
1351+
if (len + prefix_len + line_len + 1 + 1 > buf_size)
13471352
break;
13481353

1349-
text_len = buf_size - len - prefix_len - 1;
1354+
text_len = buf_size - len - prefix_len - 1 - 1;
13501355
truncated = true;
13511356
}
13521357

13531358
memmove(text + prefix_len, text, text_len);
13541359
memcpy(text, prefix, prefix_len);
13551360

1361+
/*
1362+
* Increment the prepared length to include the text and
1363+
* prefix that were just moved+copied. Also increment for the
1364+
* newline at the end of this line. If this is the last line,
1365+
* there is no newline, but it will be added immediately below.
1366+
*/
13561367
len += prefix_len + line_len + 1;
1357-
13581368
if (text_len == line_len) {
13591369
/*
1360-
* Add the trailing newline removed in
1361-
* vprintk_store().
1370+
* This is the last line. Add the trailing newline
1371+
* removed in vprintk_store().
13621372
*/
13631373
text[prefix_len + line_len] = '\n';
13641374
break;
@@ -1383,6 +1393,14 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
13831393
text_len -= line_len + 1;
13841394
}
13851395

1396+
/*
1397+
* If a buffer was provided, it will be terminated. Space for the
1398+
* string terminator is guaranteed to be available. The terminator is
1399+
* not counted in the return value.
1400+
*/
1401+
if (buf_size > 0)
1402+
text[len] = 0;
1403+
13861404
return len;
13871405
}
13881406

0 commit comments

Comments
 (0)