Closed stoeckmann closed 2 weeks ago
Attention: Patch coverage is 47.05882%
with 9 lines
in your changes missing coverage. Please review.
Files with missing lines | Patch % | Lines |
---|---|---|
libkmod/libkmod-module.c | 47.05% | 6 Missing and 3 partials :warning: |
Files with missing lines | Coverage Δ | |
---|---|---|
libkmod/libkmod-module.c | 53.66% <47.05%> (ø) |
To simplify the alternative, we could also change the function signature to take a uint32_t
, calculate with uint64_t
and just check afterwards if the result is larger than SIZE_MAX
. This would reduce quite some function calls, speed up the code, but also keep a manual memcpy
.
Mentioned it here if either it's decided to go with this alternative instead or, alternatively, that we might perform these adjustments as "performance improvements" later on.
Applied, thanks
If an overly long signature is found in a module file, it is possible to trigger an out of boundary write in
kmod_module_hex_to_str
due to integer and subsequent heap buffer overflow.This approach replaces malloc + sprintf with a simple hex-lookup and a strbuf approach, being slightly faster in real life scenarios while adding around 100 bytes to library size. Even though it calls
realloc
due tostrbuf_steal
,sprintf
for such simple format specifiers has still a larger overhead. A much faster approach could be done without strbuf and using our overflow check functions, but readability should win here.Proof of Concept:
Create a module with a long signature (use any uncompressed module with .modinfo as foundation)
Run modinfo
If you do not run out of memory, you will see a segmentation fault.
This saves a few instructions and cycles in
depmod
, but the best thing about this diff is, that it removes the lastsprintf
call found in libkmod, replacing it with our safer and easier to handle strbuf implementation.The previously mentioned alternative would be:
With huge signatures, it's 10 times faster, but when calling depmod, the difference is, of course, much smaller. It reduces the size of library by a few bytes, but in total I think it's not worth it to have a piece of code which is harder to read in this scenario. Still wanted to offer at least a tested alternative.