printk: fix reading beyond buffer
John Ogness
maintainercover printk: fix reading beyond buffer
Hello,
Marek Szyprowski reported [0] a problem with a particular printk usage. This particular usage performs thousands of LOG_CONT calls. The printk.c implementation was only limiting the growing record by the maximum size available in the ringbuffer, thus creating a record that was several kilobytes in size. This in and of itself is not a problem.
However, the various readers used buffers that were about 1KB in size. The ringbuffer would only fill the reader's 1KB buffer, but the meta data stated that the message was actually much larger. The reader code was not checking this and assumed its buffer contained the full message.
I have solved this problem by adding the necessary check to the functions where the situation can occur and also adding an argument when extending records so that a maximum size is specified. This will prevent the records from growing beyond the size that we know our readers are using.
I did not add the check where it is certain that the reader's buffer is large enough to contain the largest possible message.
The 2nd patch in this series reduces the size of the initial setup buffer. I noticed it was too big while verifying all the sizes for this series.
John Ogness
[0] https://lkml.kernel.org/r/[email protected]
John Ogness (2):
printk: avoid and/or handle record truncation
printk: reduce setup_text_buf size to LOG_LINE_MAX
kernel/printk/printk.c | 9 +++++++-- kernel/printk/printk_ringbuffer.c | 12 ++++++++++-- kernel/printk/printk_ringbuffer.h | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-)
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH 1/2 printk: avoid and/or handle record truncation
If a reader provides a buffer that is smaller than the message text, the @text_len field of @info will have a value larger than the buffer size. If readers blindly read @text_len bytes of data without checking the size, they will read beyond their buffer.
Add this check to record_print_text() to properly recognize when such truncation needs to occur.
Add a maximum size argument to the ringbuffer function to extend records so that records can not be created that are larger than the buffer size of readers.
When extending records (LOG_CONT), do not extend records beyond LOG_LINE_MAX since that is the maximum size available in the buffers used by consoles and syslog.
--- kernel/printk/printk.c | 7 ++++++- kernel/printk/printk_ringbuffer.c | 12 ++++++++++-- kernel/printk/printk_ringbuffer.h | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-)
kernel/printk/printk.c +6 -1
@@ -1357,6 +1357,11 @@ static size_t record_print_text(struct printk_record *r, bool syslog,size_t len =0 ;char *next; +if (text_len > buf_size) { + text_len = buf_size; + truncated = true; + } + prefix_len =info_print_prefix (r->info, syslog, time, prefix);/*
@@ -1911,7 +1916,7 @@ static size_t log_output(int facility, int level, enum log_flags lflags,struct printk_record r;prb_rec_init_wr (&r, text_len); -if (prb_reserve_in_last (&e, prb, &r, caller_id)) { +if (prb_reserve_in_last (&e, prb, &r, caller_id,LOG_LINE_MAX )) {memcpy (&r.text_buf[r.info->text_len], text, text_len); r.info->text_len += text_len;if (lflags &LOG_NEWLINE ) {
kernel/printk/printk_ringbuffer.c +10 -2
@@ -202,7 +202,8 @@* // specify additional 5 bytes text space to extend * prb_rec_init_wr(&r, 5); * -* if (prb_reserve_in_last(&e, &test_rb, &r, printk_caller_id())) { +* // try to extend, but only if it does not exceed 32 bytes +* if (prb_reserve_in_last(&e, &test_rb, &r, printk_caller_id()), 32) { * snprintf(&r.text_buf[r.info->text_len], * r.text_buf_size - r.info->text_len, "hello"); *
@@ -1309,6 +1310,7 @@ static struct prb_desc *desc_reopen_last(struct prb_desc_ring *desc_ring,* @rb: The ringbuffer to re-reserve and extend data in. * @r: The record structure to allocate buffers for. * @caller_id: The caller ID of the caller (reserving writer). +* @max_size: Fail if the extended size would be greater than this. * * This is the public function available to writers to re-reserve and extend * data.
@@ -1343,7 +1345,7 @@ static struct prb_desc *desc_reopen_last(struct prb_desc_ring *desc_ring,* @r->info->text_len after concatenating. */ bool prb_reserve_in_last (struct prb_reserved_entry *e,struct printk_ringbuffer *rb, -struct printk_record *r,u32 caller_id) +struct printk_record *r,u32 caller_id,unsigned int max_size) {struct prb_desc_ring *desc_ring = &rb->desc_ring;struct printk_info *info;
@@ -1389,6 +1391,9 @@ bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbufferif (!data_check_size (&rb->text_data_ring, r->text_buf_size)) goto fail; +if (r->text_buf_size > max_size) + goto fail; + r->text_buf =data_alloc (rb, &rb->text_data_ring, r->text_buf_size, &d->text_blk_lpos, id); }else {
@@ -1410,6 +1415,9 @@ bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbufferif (!data_check_size (&rb->text_data_ring, r->text_buf_size)) goto fail; +if (r->text_buf_size > max_size) + goto fail; + r->text_buf =data_realloc (rb, &rb->text_data_ring, r->text_buf_size, &d->text_blk_lpos, id); }
kernel/printk/printk_ringbuffer.h +1 -1
@@ -303,7 +303,7 @@ static inline void prb_rec_init_wr(struct printk_record *r,bool prb_reserve (struct prb_reserved_entry *e,struct printk_ringbuffer *rb,struct printk_record *r);bool prb_reserve_in_last (struct prb_reserved_entry *e,struct printk_ringbuffer *rb, -struct printk_record *r,u32 caller_id); +struct printk_record *r,u32 caller_id,unsigned int max_size);void prb_commit (struct prb_reserved_entry *e);void prb_final_commit (struct prb_reserved_entry *e);
Signature · 1 line
2.20.1
Re: [PATCH next 1/2] printk: avoid and/or handle record truncation
Hi John,
Quoted 12 lines · John Ogness
If a reader provides a buffer that is smaller than the message text, the @text_len field of @info will have a value larger than the buffer size. If readers blindly read @text_len bytes of data without checking the size, they will read beyond their buffer.
Add this check to
record_print_text()to properly recognize when such truncation needs to occur.Add a maximum size argument to the ringbuffer function to extend records so that records can not be created that are larger than the buffer size of readers.
Quoted 7 lines
When extending records (
LOG_CONT), do not extend records beyondLOG_LINE_MAXsince that is the maximum size available in the buffers used by consoles and syslog.Fixes f5f022e53b87 ("printk: reimplement log_cont using record extension")Signed-off-by John Ogness <[email protected]>Reported-by Marek Szyprowski <[email protected]>
Thanks for fixing this issue!
Quoted 87 lines
--- kernel/printk/printk.c | 7 ++++++- kernel/printk/printk_ringbuffer.c | 12 ++++++++++-- kernel/printk/printk_ringbuffer.h | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-)kernel/printk/printk.c +6
-1@@ -1357,6 +1357,11 @@ static size_t record_print_text(struct printk_record *r, bool syslog,size_t len =0 ;char *next; +if (text_len > buf_size) { + text_len = buf_size; + truncated = true; + } + prefix_len =info_print_prefix (r->info, syslog, time, prefix);/* @@ -1911,7 +1916,7 @@ static size_t log_output(int facility, int level, enum log_flags lflags,struct printk_record r;prb_rec_init_wr (&r, text_len); -if (prb_reserve_in_last (&e, prb, &r, caller_id)) { +if (prb_reserve_in_last (&e, prb, &r, caller_id,LOG_LINE_MAX )) {memcpy (&r.text_buf[r.info->text_len], text, text_len); r.info->text_len += text_len;if (lflags &LOG_NEWLINE ) {kernel/printk/printk_ringbuffer.c +10
-2@@ -202,7 +202,8 @@* // specify additional 5 bytes text space to extend * prb_rec_init_wr(&r, 5); * -* if (prb_reserve_in_last(&e, &test_rb, &r, printk_caller_id())) { +* // try to extend, but only if it does not exceed 32 bytes +* if (prb_reserve_in_last(&e, &test_rb, &r, printk_caller_id()), 32) { * snprintf(&r.text_buf[r.info->text_len], * r.text_buf_size - r.info->text_len, "hello"); * @@ -1309,6 +1310,7 @@ static struct prb_desc *desc_reopen_last(struct prb_desc_ring *desc_ring,* @rb: The ringbuffer to re-reserve and extend data in. * @r: The record structure to allocate buffers for. * @caller_id: The caller ID of the caller (reserving writer). +* @max_size: Fail if the extended size would be greater than this. * * This is the public function available to writers to re-reserve and extend * data. @@ -1343,7 +1345,7 @@ static struct prb_desc *desc_reopen_last(struct prb_desc_ring *desc_ring,* @r->info->text_len after concatenating. */ bool prb_reserve_in_last (struct prb_reserved_entry *e,struct printk_ringbuffer *rb, -struct printk_record *r,u32 caller_id) +struct printk_record *r,u32 caller_id,unsigned int max_size) {struct prb_desc_ring *desc_ring = &rb->desc_ring;struct printk_info *info;@@ -1389,6 +1391,9 @@ bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbufferif (!data_check_size (&rb->text_data_ring, r->text_buf_size)) goto fail; +if (r->text_buf_size > max_size) + goto fail; + r->text_buf =data_alloc (rb, &rb->text_data_ring, r->text_buf_size, &d->text_blk_lpos, id); }else {@@ -1410,6 +1415,9 @@ bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbufferif (!data_check_size (&rb->text_data_ring, r->text_buf_size)) goto fail; +if (r->text_buf_size > max_size) + goto fail; + r->text_buf =data_realloc (rb, &rb->text_data_ring, r->text_buf_size, &d->text_blk_lpos, id); }kernel/printk/printk_ringbuffer.h +0
-0
kernel/printk/printk_ringbuffer.h static inline void prb_rec_init_wr(struct printk_record *r, · line 309
Quoted 8 lines
kernel/printk/printk_ringbuffer.h +1
-1@@ -303,7 +303,7 @@ static inline void prb_rec_init_wr(struct printk_record *r,bool prb_reserve (struct prb_reserved_entry *e,struct printk_ringbuffer *rb,struct printk_record *r);bool prb_reserve_in_last (struct prb_reserved_entry *e,struct printk_ringbuffer *rb, -struct printk_record *r,u32 caller_id); +struct printk_record *r,u32 caller_id,unsigned int max_size);void prb_commit (struct prb_reserved_entry *e);void prb_final_commit (struct prb_reserved_entry *e);
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Petr Mladek
maintainerRe: [PATCH next 1/2] printk: avoid and/or handle record truncation
In reply to John OgnessQuoted 4 lines · John Ogness
If a reader provides a buffer that is smaller than the message text, the @text_len field of @info will have a value larger than the buffer size. If readers blindly read @text_len bytes of data without checking the size, they will read beyond their buffer.
Great catch!
Quoted 26 lines
Add this check to
record_print_text()to properly recognize when such truncation needs to occur.Add a maximum size argument to the ringbuffer function to extend records so that records can not be created that are larger than the buffer size of readers.
When extending records (
LOG_CONT), do not extend records beyondLOG_LINE_MAXsince that is the maximum size available in the buffers used by consoles and syslog.Fixes f5f022e53b87 ("printk: reimplement log_cont using record extension")Signed-off-by John Ogness <[email protected]>Reported-by Marek Szyprowski <[email protected]>--- kernel/printk/printk.c | 7 ++++++- kernel/printk/printk_ringbuffer.c | 12 ++++++++++-- kernel/printk/printk_ringbuffer.h | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-)kernel/printk/printk.c +0
-0
kernel/printk/printk.c static size_t record_print_text(struct printk_record *r, bool syslog, · line 1362
Quoted 6 lines
kernel/printk/printk.c +3
-0@@ -1357,6 +1357,11 @@ static size_t record_print_text(struct printk_record *r, bool syslog,size_t len =0 ;char *next; +if (text_len > buf_size) { + text_len = buf_size; + truncated = true;
@truncate must not be set here. Otherwise, the prefix would not be added when there no '\n' in the entire string. It would call:
/* Drop truncated line(s). */ if (truncated) break;
before copying the prefix.
It is enough to remove the line. It will be set in the very first cycle anyway. We need to add one prefix at all. It would require to truncate even more bytes.
Otherwise, the patch looks good to me.
Best Regards,
Petr
Re: [PATCH next 1/2] printk: avoid and/or handle record truncation
In reply to John OgnessQuoted 9 lines · John Ogness
kernel/printk/printk.c +5
-0+if (text_len > buf_size) { + text_len = buf_size; + truncated = true; + } + prefix_len =info_print_prefix (r->info, syslog, time, prefix);/*
kernel/printk/printk.c static size_t log_output(int facility, int level, enum log_flags lflags, · line 1919
Quoted 5 lines
kernel/printk/printk.c +1
-1@@ -1911,7 +1916,7 @@ static size_t log_output(int facility, int level, enum log_flags lflags,struct printk_record r;prb_rec_init_wr (&r, text_len); -if (prb_reserve_in_last (&e, prb, &r, caller_id)) { +if (prb_reserve_in_last (&e, prb, &r, caller_id,LOG_LINE_MAX )) {
Are we going to pass anything other than LOG_LINE_MAX? If not then maybe we can drop that argument and compare the text_buf_size to LOG_LINE_MAX?
-ss
John Ogness
maintainerPATCH 2/2 printk: reduce setup_text_buf size to LOG_LINE_MAX
In reply to John Ogness@setup_text_buf only copies the original text messages (without any prefix or extended text). It only needs to be LOG_LINE_MAX in size.
--- kernel/printk/printk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
@@ -1103,7 +1103,7 @@ static unsigned int __init add_to_rb(struct printk_ringbuffer *rb,return prb_record_text_space (&e); } -static char setup_text_buf[CONSOLE_EXT_LOG_MAX ]__initdata ; +static char setup_text_buf[LOG_LINE_MAX ]__initdata ;void __init setup_log_buf (int early) {
Signature · 1 line
2.20.1
Re: [PATCH next 2/2] printk: reduce setup_text_buf size to LOG_LINE_MAX
Quoted 4 lines · John Ogness
@setup_text_buf only copies the original text messages (without any prefix or extended text). It only needs to be
LOG_LINE_MAXin size.Signed-off-by John Ogness <[email protected]>
Best Regards,
Petr
Quoted 8 lines · John Ogness
Hello,
Marek Szyprowski reported [0] a problem with a particular printk usage. This particular usage performs thousands of
LOG_CONTcalls. Theprintk.cimplementation was only limiting the growing record by the maximum size available in the ringbuffer, thus creating a record that was several kilobytes in size. This in and of itself is not a problem.
Perhaps another mechanism would be to change the code to add a backspace before the rotor and have the printk ringbuffer actually backspace on \h when position > 0
Something like:
---
init/do_mounts_rd.c +1 -1
@@ -257,7 +258,7 @@ int __init rd_load_image(char *from)kernel_write (out_file, buf,BLOCK_SIZE , &out_pos);#if !defined(CONFIG_S390 )if (!(i %16 )) { -pr_cont ("%c\b" , rotator[rotate &0x3 ]); +pr_cont ("\h%c\b" , rotator[rotate &0x3 ]); rotate++; }#endif