So users don't feel tempted to look at inside the strbuf. Just add a
function for it and proper tests.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
The main for strbuf_steal() to free() on error was to allow the caller
to pass the NULL up the stack with just a return call to
strbuf_steal().
However this is error-prone and surprising that the buffer is still
invalidated on error. Provide an strbuf cleanup attribute that can be
used for the same purpose and make sure that when the string is stolen,
it's set to NULL, so there's no dangling pointer around.
Since we run the testsuite with AddressSanitizer, a simple test can be
added to make sure the stolen string becomes valid when the attribute is
used.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
Do not override pointers to first list nodes if appending failed,
otherwise it's impossible to release already existing nodes of these
lists afterwards.
Remove the now unused function kmod_list_remove_n_latest as well.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/228
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Add a macro to reduce amount of explicit while-loops for removal of
nodes and release of their associated data in code base.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/228
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Currently, we repeatedly loop over the elf headers looking for five well
known sections. Just do it once in kmod_elf_new() and reuse the data as
needed.
Note that not all sections are guaranteed to be available, so check and
ELFDBG print the ones which are missing.
v2: quit looping when found, ELFDBG print missing sections
v3: match the first section name, use a loop
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Rename kmod_elf_get_strings() to kmod_elf_get_modinfo_strings() and fold
the section name within, instead of passing it as an argument.
This aligns better with the other kmod_elf function names and will allow
us to tweak the kmod_elf_get_section() handling with later commit.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Since the caller already copies the strings as needed, we can just
point to the elf data directly.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Since the caller already copies the strings as needed, we can just
point to the elf data directly.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Since the caller already copies the strings as needed, we can just
point to the elf data directly.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
As per the documentation (man 5 elf) the section must be null
terminated. Move the check further up and remove the no longer needed
code trying to workaround non-compliant instances.
Note: drop the erroneous +1 in the overflow (malloc size) calculation
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Since the caller already copies the strings as needed, we can just
point to the elf data directly.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/210
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
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. A much faster approach could be
done without strbuf and using our overflow check functions, but
readability should win here.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/236
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Specially crafted files could overflow internal counters, allowing out of
boundary writes. Make sure that neither counters nor resulting calculations
overflow.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/234
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Add the clang::suppress attribute to places where allocation is done and
that rely on the cleanup attribute. Clang analyzer doesn't handle those
(yet), so keep it from giving us false positive.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/233
Do not access memory out of bounds if the first character read by fgets
is NUL. Treat such a character as EOL instead. This is a purely
defensive measure since /proc/modules should normaly not contain such
characters.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/227
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The index_mm_{search,read,dump}* API does not mutate the index_mm
struct. So let's annotate it as constant.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The callers are interested if the file is within the list of builtins or
not. So change the return type and push the free() down the stack.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Create a simple helper and reuse it instead of duplicating code.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The compiler already creates an empty string as applicable, drop the
extra variable.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
As seen with previous commit, the way we pass an empty string to
index_{,mm_}dump() is very error prone.
Swap that with a bool, which makes things a lot more obvious.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
For non-alias indexes prefix is an empty string, where
strbuf_pushchars() returns the number of characters added to the strbuf.
Since those are zero, we end up completely skipping the dump process.
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Fixes: 889d02b1 ("libkmod: check strbuf return values")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Make sure that symbol names in ELF files are actually NUL terminated.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/222
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Whenever sizeof(mv->crc) is used, it's called crclen, not offcrc.
Move retrieval of field sizes into its own function.
Prepare this as a cleanup for easier auditing.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/222
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
This fixes a regression introduced while converting pointer to
offset arithmetics.
The for-loop itself starts at 1 already, so reflect this with the
manually performed offset + length calculation right at the start.
Closes: https://github.com/kmod-project/kmod/issues/214
Fixes: 25ab561b ("libkmod: Use ELF offsets more often")
Reported-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/215
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Return result of kmod_module_new directly in kmod_module_new_from_alias
so this function handles return values like the others.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/211
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
If kmod_module_parse_depline runs out of memory, it is possible
that not all dependency modules are unlinked.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/211
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The module_get_dependencies_noref function may modify content of
given kmod_module struct, so clarify this fact.
Actually we would have to adjust kmod API for total clarification,
but let's start with internally used functions first.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/211
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The return value of kmod_module_parse_depline and
module_get_dependencies_noref are never used, and the same is true for
n_dep in struct kmod_module.
Remove them and turn variable n in kmod_module_parse_depline into a
size_t to make sure that it never overflows.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/211
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
If functions exist which cover the exact explicitly written code, use
them instead.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/211
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
This is in sync with read_char, which uses getc_unlocked. This and
previous FILE based index adjustments improved FILE based index dump
performance by 8 %.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/190
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
We can use getdelim and reuse buffers by referencing the index_file
instead of just the FILE handle.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/190
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reduce the FILE overhead by reading as many bytes as possible at once.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/190
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Many values are never needed, so only parse them on demand. Also keep
pointers into memory-mapped area without copying data into dynamically
sized structs, which allows nodes to be kept on stack.
Improves performance of `modprobe -c` by around 3 %.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/201
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Remove arguments with fixed values. Also increment char pointers instead
of working with addresses of indices for easier readability.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/201
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Clarify that we never intend to modify the memory-mapped content.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/201
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The kmod_module_get_filtered_blacklist() was deprecated since kmod v6 in
favour of kmod_module_apply_filter().
Add the decoration so gtk-doc includes it in the generated html and also
add a designated index.
It seem that gtk-doc insists on having deprecation guards, so not it
prints an extra warning like:
warning: XXX is deprecated in the inline comments, but no deprecation
guards were found around the declaration. (See the --deprecated-guards
option for gtkdoc-scan.)
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/200
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
With the introduction of the "Since: XX" tags, gtk-doc has been
helpfully reminding us that the per-version index isn't referenced
anywhere.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/200
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
On 32 bit systems it's possible to overflow the final calculation of
required memory for symbols retrieved from __ksymtab_strings.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/198
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Add a blank between variable declaration and function calls.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/196
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Use correct format specifiers for size_t variables.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/196
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The contents of s and strings are identical at this point, but iterate
over the correct variable nonetheless.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/196
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
An empty section is very unlikely, so reorder code to account for it.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/196
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
The function's purpose can be merged into elf_get_section. Reduces
amount of duplicated code.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/196
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>