ocp-smart-extended-log: fix sizeof mismatch in malloc for get_c0_log_page()#3555
Conversation
|
according pahole which is in hex #define C0_SMART_CLOUD_ATTR_LEN 0x200 and later in the code we do nvme_init_get_log(&cmd, NVME_NSID_ALL,
(enum nvme_cmd_get_log_lid)OCP_LID_SMART,
NVME_CSI_NVM, data, C0_SMART_CLOUD_ATTR_LEN);As far I understand your argument is that the compiler might insert padding? If this would happen the data struct would be useless. |
|
I see, I didn't realize the struct is exactly 512 bytes. That said, I'd still like to propose a small cleanup: the sizeof(__u8) * multiplier is redundant noise since sizeof(__u8) is always 1. Would you accept replacing it with just malloc(C0_SMART_CLOUD_ATTR_LEN) and memset(data, 0, C0_SMART_CLOUD_ATTR_LEN)? |
95b8a89 to
4bd965b
Compare
|
I am more in the camp of using With this in mind, what about making data = malloc(sizeof(*data));
[...]
nvme_init_get_log(&cmd, NVME_NSID_ALL,
(enum nvme_cmd_get_log_lid)OCP_LID_SMART,
NVME_CSI_NVM, data, sizeof(*data)); |
|
Sure, that sounds good. I'll implement that! |
…page() The get_c0_log_page() function allocates a buffer @DaTa using malloc with sizeof of an unrelated scalar type as the size operand instead of sizeof of the struct @DaTa points to. Use sizeof(*data) for the malloc, memset, and nvme_init_get_log calls so the size is always derived directly from the pointer type. This also makes the local constant redundant, so remove it. Signed-off-by: Sarah Ahmed <sarah.ahmed@ibm.com>
4bd965b to
2044c60
Compare
|
Excellent work! Thanks! |
The get_c0_log_page() function allocates a buffer @DaTa to hold the C0 SMART extended log page, but passes sizeof of an unrelated scalar type as the malloc operand instead of sizeof of the struct @DaTa points to.
If the compiler inserts alignment padding into the struct, the allocation will be smaller than the struct size, causing a heap buffer overflow when the struct fields are accessed beyond the allocated region.
Fix by using sizeof(*data) to ensure the correct amount of memory is allocated and zeroed for the struct.