printk_ringbuffer: Fix get_data() size sanity check
John Ogness
maintainerPATCH 1/1 printk_ringbuffer: Fix get_data() size sanity check
Commit cc3bad11de6e ("printk_ringbuffer: Fix check of valid data size when blk_lpos overflows") added sanity checking to get_data() to avoid returning data of illegal sizes (too large or too small). It uses the helper function data_check_size() for the check. However, data_check_size() expects the size of the data, not the size of the data block. get_data() is providing the size of the data block. This means that if the data size (text_buf_size) is the maximum legal size:
sizeof(prb_data_block) + text_buf_size == DATA_SIZE(data_ring) / 2
data_check_size() will report failure because it adds sizeof(prb_data_block) to the provided size. The sanity check in get_data() is counting the data block header twice. The result is that the reader fails to read the legal record.
Since get_data() subtracts the data block header size before returning, move the sanity check to after the subtraction.
Luckily printk() is not vulnerable to this problem because truncate_msg() limits printk-messages to 1/4 of the ringbuffer. Indeed, by adjusting the printk_ringbuffer KUnit test, which does not use printk() and its truncate_msg() check, it is easy to see that the reader fails and the WARN_ON is triggered.
--- kernel/printk/printk_ringbuffer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/printk/printk_ringbuffer.c +4 -4
@@ -1302,10 +1302,6 @@ static const char *get_data(struct prb_data_ring *data_ring,return NULL ; } -/* Sanity check. Data-less blocks were handled earlier. */ -if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) -return NULL ; -/* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE (blk_lpos->begin !=ALIGN (blk_lpos->begin,sizeof (db->id))) ||WARN_ON_ONCE (blk_lpos->next !=ALIGN (blk_lpos->next,sizeof (db->id)))) {
@@ -1319,6 +1315,10 @@ static const char *get_data(struct prb_data_ring *data_ring,/* Subtract block ID space from size to reflect data size. */ *data_size -=sizeof (db->id); +/* Sanity check. Data-less blocks were handled earlier. */ +if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) +return NULL ; +return &db->data[0 ]; }
base-commit: 9095f233c0258e9a05e958c7d822eb38681e7a5a
Signature · 1 line
2.47.3
Petr Mladek
maintainerQuoted 9 lines · John Ogness
Commit
cc3bad11de6e("printk_ringbuffer: Fix check of valid data size whenblk_lposoverflows") added sanity checking toget_data()to avoid returning data of illegal sizes (too large or too small). It uses the helper functiondata_check_size()for the check. However,data_check_size()expects the size of the data, not the size of the data block.get_data()is providing the size of the data block. This means that if the data size (text_buf_size) is the maximum legal size:
Quoted 6 lines
sizeof(prb_data_block)+text_buf_size==DATA_SIZE(data_ring)/ 2
data_check_size()will report failure because it addssizeof(prb_data_block)to the provided size. The sanity check inget_data()is counting the data block header twice. The result is that the reader fails to read the legal record.
Great catch!
Quoted 8 lines
Since
get_data()subtracts the data block header size before returning, move the sanity check to after the subtraction.Luckily
printk()is not vulnerable to this problem becausetruncate_msg()limits printk-messages to 1/4 of the ringbuffer. Indeed, by adjusting theprintk_ringbufferKUnit test, which does not useprintk()and itstruncate_msg()check, it is easy to see that the reader fails and theWARN_ONis triggered.
Uff ;-)
Quoted 14 lines
kernel/printk/printk_ringbuffer.c +0
-4@@ -1302,10 +1302,6 @@ static const char *get_data(struct prb_data_ring *data_ring,return NULL ; } -/* Sanity check. Data-less blocks were handled earlier. */ -if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) -return NULL ; -/* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE (blk_lpos->begin !=ALIGN (blk_lpos->begin,sizeof (db->id))) ||WARN_ON_ONCE (blk_lpos->next !=ALIGN (blk_lpos->next,sizeof (db->id)))) {
kernel/printk/printk_ringbuffer.c static const char *get_data(struct prb_data_ring *data_ring, · line 1319
Quoted 5 lines
kernel/printk/printk_ringbuffer.c +2
-0@@ -1319,6 +1315,10 @@ static const char *get_data(struct prb_data_ring *data_ring,/* Subtract block ID space from size to reflect data size. */ *data_size -=sizeof (db->id); +/* Sanity check. Data-less blocks were handled earlier. */ +if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size))
The check of "!*data_size" is wrong after sizeof(db->id) subtractions.
The question is whether we still need it. As the comment says, the data-less block were handled earlier. And it seems that data_alloc() explicitly creates data-less blocks when the given size is 0. And prb_reserve_in_last() only allows to increase the size.
IMHO, we could remove it. Wrong values will be handled by the above check:
/* A valid data block will always have at least an ID. */ if (WARN_ON_ONCE(*data_size < sizeof(db->id))) return NULL;
A zero size datablock which is not handled using the explicit data-less block is not expected after all.
Best Regards,
Petr
Quoted 9 lines
kernel/printk/printk_ringbuffer.c +2
-0+return NULL ; +return &db->data[0 ]; }base-commit:
9095f233c0258e9a05e958c7d822eb38681e7a5aSignature · 1 line
2.47.3
John Ogness
maintainerQuoted 17 lines · Petr Mladek
Quoted 17 lines
kernel/printk/printk_ringbuffer.c +0
-4@@ -1302,10 +1302,6 @@ static const char *get_data(struct prb_data_ring *data_ring,return NULL ; } -/* Sanity check. Data-less blocks were handled earlier. */ -if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) -return NULL ; -/* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE (blk_lpos->begin !=ALIGN (blk_lpos->begin,sizeof (db->id))) ||WARN_ON_ONCE (blk_lpos->next !=ALIGN (blk_lpos->next,sizeof (db->id)))) {@@ -1319,6 +1315,10 @@ static const char *get_data(struct prb_data_ring *data_ring,/* Subtract block ID space from size to reflect data size. */ *data_size -=sizeof (db->id);
Quoted 4 lines
kernel/printk/printk_ringbuffer.c static const char *get_data(struct prb_data_ring *data_ring, · line 1319
Quoted 2 lines
kernel/printk/printk_ringbuffer.c +2
-0+/* Sanity check. Data-less blocks were handled earlier. */ +if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size))The check of "!*data_size" is wrong after
sizeof(db->id)subtractions.
It is not wrong. It is checking something else.
Quoted 5 lines
The question is whether we still need it. As the comment says, the data-less block were handled earlier. And it seems that
data_alloc()explicitly creates data-less blocks when the given size is 0. Andprb_reserve_in_last()only allows to increase the size.
Quoted 8 lines
IMHO, we could remove it. Wrong values will be handled by the above check:
/* A valid data block will always have at least an ID. */ if (WARN_ON_ONCE(*data_size < sizeof(db->id))) return NULL;A zero size datablock which is not handled using the explicit data-less block is not expected after all.
Well, the point of the WARN is to catch when things are broken. If for some reason a data block of textlen 0 exists, it is a bug. I think we should still catch it. I do not know if there are places in the code that assume if it is a data block, it must have a textlen > 0. Probably not, but still, it should not exist.
John
Petr Mladek
maintainerQuoted 18 lines · John Ogness
Quoted 17 lines · Petr Mladek
Quoted 17 lines
kernel/printk/printk_ringbuffer.c +0
-4@@ -1302,10 +1302,6 @@ static const char *get_data(struct prb_data_ring *data_ring,return NULL ; } -/* Sanity check. Data-less blocks were handled earlier. */ -if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) -return NULL ; -/* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE (blk_lpos->begin !=ALIGN (blk_lpos->begin,sizeof (db->id))) ||WARN_ON_ONCE (blk_lpos->next !=ALIGN (blk_lpos->next,sizeof (db->id)))) {@@ -1319,6 +1315,10 @@ static const char *get_data(struct prb_data_ring *data_ring,/* Subtract block ID space from size to reflect data size. */ *data_size -=sizeof (db->id);
Quoted 6 lines
Quoted 4 lines
kernel/printk/printk_ringbuffer.c static const char *get_data(struct prb_data_ring *data_ring, · line 1319
Quoted 2 lines
kernel/printk/printk_ringbuffer.c +2
-0+/* Sanity check. Data-less blocks were handled earlier. */ +if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size))The check of "!*data_size" is wrong after
sizeof(db->id)subtractions.It is not wrong. It is checking something else.
Good point!
Quoted 10 lines
Quoted 10 lines
The question is whether we still need it. As the comment says, the data-less block were handled earlier. And it seems that
data_alloc()explicitly creates data-less blocks when the given size is 0. Andprb_reserve_in_last()only allows to increase the size.IMHO, we could remove it. Wrong values will be handled by the above check:
/* A valid data block will always have at least an ID. */ if (WARN_ON_ONCE(*data_size < sizeof(db->id))) return NULL;
Ah, I have missed that this earlier check won't catch it because it is '<' and not '<='.
Quoted 8 lines
Quoted 2 lines
A zero size datablock which is not handled using the explicit data-less block is not expected after all.
Well, the point of the WARN is to catch when things are broken. If for some reason a data block of textlen 0 exists, it is a bug. I think we should still catch it. I do not know if there are places in the code that assume if it is a data block, it must have a textlen > 0. Probably not, but still, it should not exist.
IMHO, there is no function which would expect that a data_block must have a textlen > 0. It just should not exit. Because zero size data blocks are handled special way.
So, just to be sure that the new code works as expected. Note that the moved check:
/* Sanity check. Data-less blocks were handled earlier. */ if (WARN_ON_ONCE(!data_check_size(data_ring, *data_size) || !*data_size)) return NULL;
warns only when data_check_size() fails. It just quietly returns NULL when *data_size is zero.
If we really want to warn. Then it would make more sense to change "<" to "<=" in the previous check before the subtraction.
I do not have a strong opinion. But I tend to add the warning because the zero size is not expected at this stage. Well, we could do it in a followup patch and keep this on as is.
Best Regards,
Petr
John Ogness
maintainerQuoted 9 lines · Petr Mladek
So, just to be sure that the new code works as expected. Note that the moved check:
/* Sanity check. Data-less blocks were handled earlier. */ if (WARN_ON_ONCE(!data_check_size(data_ring, *data_size) || !*data_size)) return NULL;warns only when
data_check_size()fails. It just quietly returns NULL when*data_sizeis zero.
??? The above code warns for !*data_size as well.
Quoted 2 lines
If we really want to warn. Then it would make more sense to change "<" to "<=" in the previous check before the subtraction.
Yes, actually I would prefer that. Let me send a v2 where I relocate and reduce the data_check_size()-WARN and extend the data_size-WARN. So it is something like this:
kernel/printk/printk_ringbuffer.c +9 -6
@@ -1302,23 +1302,26 @@ static const char *get_data(struct prb_data_ring *data_ring,return NULL ; } -/* Sanity check. Data-less blocks were handled earlier. */ -if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) -return NULL ; -/* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE (blk_lpos->begin !=ALIGN (blk_lpos->begin,sizeof (db->id))) ||WARN_ON_ONCE (blk_lpos->next !=ALIGN (blk_lpos->next,sizeof (db->id)))) {return NULL ; } -/* A valid data block will always have at least an ID. */ -if (WARN_ON_ONCE (*data_size <sizeof (db->id))) +/* +* A regular data block will always have an ID and at least +* 1 byte of data. Data-less blocks were handled earlier. +*/ +if (WARN_ON_ONCE (*data_size <=sizeof (db->id)))return NULL ;/* Subtract block ID space from size to reflect data size. */ *data_size -=sizeof (db->id); +/* Sanity check the max size of the regular data block. */ +if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size))) +return NULL ; +return &db->data[0 ]; }
John
Petr Mladek
maintainerQuoted 4 lines · John Ogness
Quoted 3 lines · Petr Mladek
So, just to be sure that the new code works as expected. Note that the moved check:
Quoted 8 lines
Quoted 6 lines
/* Sanity check. Data-less blocks were handled earlier. */ if (WARN_ON_ONCE(!data_check_size(data_ring, *data_size) || !*data_size)) return NULL;warns only when
data_check_size()fails. It just quietly returns NULL when*data_sizeis zero.??? The above code warns for !*data_size as well.
Grr, I wrongly interpreted the brackets.
Quoted 33 lines
Quoted 2 lines
If we really want to warn. Then it would make more sense to change "<" to "<=" in the previous check before the subtraction.
Yes, actually I would prefer that. Let me send a v2 where I relocate and reduce the data_check_size()-WARN and extend the data_size-WARN. So it is something like this:
kernel/printk/printk_ringbuffer.c +5
-6@@ -1302,23 +1302,26 @@ static const char *get_data(struct prb_data_ring *data_ring,return NULL ; } -/* Sanity check. Data-less blocks were handled earlier. */ -if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size) || !*data_size)) -return NULL ; -/* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE (blk_lpos->begin !=ALIGN (blk_lpos->begin,sizeof (db->id))) ||WARN_ON_ONCE (blk_lpos->next !=ALIGN (blk_lpos->next,sizeof (db->id)))) {return NULL ; } -/* A valid data block will always have at least an ID. */ -if (WARN_ON_ONCE (*data_size <sizeof (db->id))) +/* +* A regular data block will always have an ID and at least +* 1 byte of data. Data-less blocks were handled earlier. +*/ +if (WARN_ON_ONCE (*data_size <=sizeof (db->id)))return NULL ;
kernel/printk/printk_ringbuffer.c static const char *get_data(struct prb_data_ring *data_ring, · line 1326
Quoted 10 lines
kernel/printk/printk_ringbuffer.c +4
-0/* Subtract block ID space from size to reflect data size. */ *data_size -=sizeof (db->id); +/* Sanity check the max size of the regular data block. */ +if (WARN_ON_ONCE (!data_check_size (data_ring, *data_size))) +return NULL ; +return &db->data[0 ]; }
I like this. Thanks for patience with me.
Best Regards,
Petr