Skip to content

Commit 861c2e3

Browse files
jognessgregkh
authored andcommitted
printk: fix buffer overflow potential for print_text()
commit f0e386e upstream. 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: stable@vger.kernel.org # 5.10+ Signed-off-by: John Ogness <john.ogness@linutronix.de> Reviewed-by: Petr Mladek <pmladek@suse.com> Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20210114170412.4819-1-john.ogness@linutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cb14bbb commit 861c2e3

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

‎kernel/printk/printk.c‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,11 +1338,16 @@ static size_t info_print_prefix(const struct printk_info *info, bool syslog,
13381338
* done:
13391339
*
13401340
* - Add prefix for each line.
1341+
* - Drop truncated lines that no longer fit into the buffer.
13411342
* - Add the trailing newline that has been removed in vprintk_store().
1342-
* - Drop truncated lines that do not longer fit into the buffer.
1343+
* - Add a string terminator.
1344+
*
1345+
* Since the produced string is always terminated, the maximum possible
1346+
* return value is @r->text_buf_size - 1;
13431347
*
13441348
* Return: The length of the updated/prepared text, including the added
1345-
* prefixes and the newline. The dropped line(s) are not counted.
1349+
* prefixes and the newline. The terminator is not counted. The dropped
1350+
* line(s) are not counted.
13461351
*/
13471352
static size_t record_print_text(struct printk_record *r, bool syslog,
13481353
bool time)
@@ -1385,26 +1390,31 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
13851390

13861391
/*
13871392
* Truncate the text if there is not enough space to add the
1388-
* prefix and a trailing newline.
1393+
* prefix and a trailing newline and a terminator.
13891394
*/
1390-
if (len + prefix_len + text_len + 1 > buf_size) {
1395+
if (len + prefix_len + text_len + 1 + 1 > buf_size) {
13911396
/* Drop even the current line if no space. */
1392-
if (len + prefix_len + line_len + 1 > buf_size)
1397+
if (len + prefix_len + line_len + 1 + 1 > buf_size)
13931398
break;
13941399

1395-
text_len = buf_size - len - prefix_len - 1;
1400+
text_len = buf_size - len - prefix_len - 1 - 1;
13961401
truncated = true;
13971402
}
13981403

13991404
memmove(text + prefix_len, text, text_len);
14001405
memcpy(text, prefix, prefix_len);
14011406

1407+
/*
1408+
* Increment the prepared length to include the text and
1409+
* prefix that were just moved+copied. Also increment for the
1410+
* newline at the end of this line. If this is the last line,
1411+
* there is no newline, but it will be added immediately below.
1412+
*/
14021413
len += prefix_len + line_len + 1;
1403-
14041414
if (text_len == line_len) {
14051415
/*
1406-
* Add the trailing newline removed in
1407-
* vprintk_store().
1416+
* This is the last line. Add the trailing newline
1417+
* removed in vprintk_store().
14081418
*/
14091419
text[prefix_len + line_len] = '\n';
14101420
break;
@@ -1429,6 +1439,14 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
14291439
text_len -= line_len + 1;
14301440
}
14311441

1442+
/*
1443+
* If a buffer was provided, it will be terminated. Space for the
1444+
* string terminator is guaranteed to be available. The terminator is
1445+
* not counted in the return value.
1446+
*/
1447+
if (buf_size > 0)
1448+
text[len] = 0;
1449+
14321450
return len;
14331451
}
14341452

0 commit comments

Comments
 (0)