]> git.ipfire.org Git - thirdparty/kmod.git/log
thirdparty/kmod.git
6 months agoshared/macro: Add macros for more attributes
Lucas De Marchi [Fri, 3 Jan 2025 20:32:00 +0000 (14:32 -0600)] 
shared/macro: Add macros for more attributes

Move attributes from test definition to a wrapper macro.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/274
6 months agotestsuite: Fix build when linking with lld
Lucas De Marchi [Fri, 3 Jan 2025 19:01:14 +0000 (13:01 -0600)] 
testsuite: Fix build when linking with lld

When building with clang and linking with lld there is an issue with
keeping the kmod_tests sections: On ELF targets, __attribute__((used))
prevents compiler discarding, but does not affect linker --gc-sections,
according to https://lld.llvm.org/ELF/start-stop-gc.

Make sure the _start/_stop symbols are not weak and add the "retain"
attribute.

Closes: https://github.com/kmod-project/kmod/issues/269
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/274
6 months agotools: specify buffer to be size PATH_MAX
Grayson Nocera [Mon, 25 Nov 2024 20:33:02 +0000 (15:33 -0500)] 
tools: specify buffer to be size PATH_MAX

Using a CodeQL query, I discovered that the destination of a `sscanf` call could overflow.
Thus, we bound the buffer size to be PATH_MAX, to ensure that it is
not larger than `modname` or `devname`.

Signed-off-by: Grayson Nocera <gnocera@purdue.edu>
Suggested-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/260
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
6 months agostrbuf: Extend strbuf_reserve_extra size check
Tobias Stoeckmann [Wed, 18 Dec 2024 09:37:14 +0000 (10:37 +0100)] 
strbuf: Extend strbuf_reserve_extra size check

Make sure that alignment to BUF_STEP won't overflow if excessively
huge amount of memory is requested to be reserved.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/268
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
6 months agolibkmod: Use correct data types for ELF arithmetic
Tobias Stoeckmann [Sun, 22 Dec 2024 18:02:44 +0000 (19:02 +0100)] 
libkmod: Use correct data types for ELF arithmetic

Make sure that no input files could overflow internal variables
used for calculations.

This prevents wrong output and out of boundary writes.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/272
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
7 months agolsmod: Simplify code
Tobias Stoeckmann [Sun, 22 Dec 2024 16:24:57 +0000 (17:24 +0100)] 
lsmod: Simplify code

Reduces cyclomatic complexity and binary size.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/271
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
7 months agoci: Add 2 more pipelines to the matrix
Lucas De Marchi [Thu, 5 Dec 2024 17:59:43 +0000 (11:59 -0600)] 
ci: Add 2 more pipelines to the matrix

Test disabling one of the compression libs while dlopen'ing the rest and
handling 1 linked and 2 dlopen'ed.

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/262
7 months agoci: Add configure tests for dlopen
Lucas De Marchi [Sat, 30 Nov 2024 20:48:56 +0000 (14:48 -0600)] 
ci: Add configure tests for dlopen

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/262
7 months agoci: Fix fail due to existent build dir
Lucas De Marchi [Sat, 30 Nov 2024 18:46:21 +0000 (12:46 -0600)] 
ci: Fix fail due to existent build dir

The second check is a "should fail" check, but it will fail because the
build dir already exists rather than for the true reason. Use a
different dir for the configure tests and move the `rm` inside the
function.

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/262
7 months agolibkmod: Add ELF notes to compression libraries
Lucas De Marchi [Wed, 4 Dec 2024 15:24:49 +0000 (09:24 -0600)] 
libkmod: Add ELF notes to compression libraries

Follow the new spec for ELF notes as detailed in
https://systemd.io/ELF_PACKAGE_METADATA/.

We can copy mostly verbatim the macros from systemd codebase.

Example output:

$ meson setup --native-file build-dev.ini -Dxz=disabled -Ddlopen=zlib build
...
    dlopen           : zlib

    features         : +ZSTD -XZ +ZLIB +OPENSSL

$ dlopen-notes.py build/libkmod.so.2
# build/libkmod.so.2
[
  {
    "feature": "xz",
    "description": "Support for uncompressing xz-compressed modules",
    "priority": "recommended",
    "soname": [
      "liblzma.so.5"
    ]
  }
]

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/262
7 months agomeson: Allow to set dlopen option for compression libraries
Lucas De Marchi [Wed, 4 Dec 2024 07:19:32 +0000 (01:19 -0600)] 
meson: Allow to set dlopen option for compression libraries

Add a dlopen option that allows toggling what libraries libkmod should
attempt to dlopen. If -Ddlopen=foo is passed, it means that library is
required to build, regardless of -Dfoo=*. However that library will
only be linked in if it's not set as dlopen.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
[ with disagreement on the need to toggle each one individually,
  it'd be better to be all-or-nothing dlopen'ed ]
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/262
7 months agomeson: Let openssl option follow same logic as compression
Lucas De Marchi [Wed, 4 Dec 2024 08:03:07 +0000 (02:03 -0600)] 
meson: Let openssl option follow same logic as compression

Keep the logic similar for all libraries. While at it, add a comment
divider for the config output.

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/262
7 months agomeson: Simplify compression handling
Lucas De Marchi [Wed, 4 Dec 2024 07:16:09 +0000 (01:16 -0600)] 
meson: Simplify compression handling

Move libs to a table, similarly to how other things are handled,
so this logic can be more easily expanded in future for all compression
types.

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/262
7 months agobuild: Always define ENABLE_* macros
Lucas De Marchi [Wed, 4 Dec 2024 07:04:09 +0000 (01:04 -0600)] 
build: Always define ENABLE_* macros

Define either to 0 or 1 so codebase is forced to used `#if ENABLE_*` or
similar. It's confusing to have some leaving undefined and others
defining as 0 or 1.

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/262
7 months agolibkmod/zstd: Allow to load libzstd.so on demand
Lucas De Marchi [Sat, 30 Nov 2024 17:00:22 +0000 (11:00 -0600)] 
libkmod/zstd: Allow to load libzstd.so on demand

Use dlfcn helpers to load libzstd once it's needed.

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/262
7 months agolibkmod/zstd: Do not re-use size_t for ret
Lucas De Marchi [Sat, 30 Nov 2024 16:53:49 +0000 (10:53 -0600)] 
libkmod/zstd: Do not re-use size_t for ret

size_t is unsigned, while the function returns a negative number.
Instead of using `ret` for the return from ZSTD_decompress(), re-use
dst_size that should be the same as we passed in for that call since
it already checks for ZSTD_CONTENTSIZE_UNKNOWN and ZSTD_CONTENTSIZE_ERROR.

Even if it's not the same, it's more correct to use that value to
assign to file->size to avoid accessing uninitialized memory.

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/262
7 months agolibkmod/zlib: Allow to load libz.so on demand
Lucas De Marchi [Sat, 30 Nov 2024 06:38:11 +0000 (00:38 -0600)] 
libkmod/zlib: Allow to load libz.so on demand

Use dlfcn helpers to load libz once it's needed.

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/262
7 months agolibkmod/zlib: s/err/ret/ for consistency
Lucas De Marchi [Sat, 30 Nov 2024 16:22:26 +0000 (10:22 -0600)] 
libkmod/zlib: s/err/ret/ for consistency

Use the libkmod-file-xz.c as reference, keeping similar variable names
so it's easy to share code.

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/262
7 months agolibkmod/xz: Allow to load liblzma.so on demand
Lucas De Marchi [Sat, 30 Nov 2024 06:38:11 +0000 (00:38 -0600)] 
libkmod/xz: Allow to load liblzma.so on demand

Use dlfcn helpers to load liblzma once it's needed.

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/262
7 months agoutil: Add dlfcn helpers
Lucas De Marchi [Sat, 30 Nov 2024 06:30:53 +0000 (00:30 -0600)] 
util: Add dlfcn helpers

Heavily based on systemd: add similar (but simplified) functions to
dlopen() a library and load specific symbols from it. This will be
useful to allow to load the compression libraries as needed. A few
differences from the systemd implementation:

1) It's allowed to link directly to the library and hence bypass the
   dlopen() + dlsym()
2) The only entrypoint is dlsym_many() which is already declared with
   the sentinel: it's expected callers will use an x-macro that doesn't
   allow forgetting the sentinel
3) No support yet for ELF info annotation yet

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/262
7 months agoAdd TAKE_PTR()
Lucas De Marchi [Sun, 1 Dec 2024 00:49:00 +0000 (18:49 -0600)] 
Add TAKE_PTR()

Similar to macro in systemd codebase: add a macro that documents we are
"leaking" a pointer that would otherwise be cleaned up by the cleanup
attribute.

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/264
7 months agotools: Unify help messages
Tobias Stoeckmann [Mon, 2 Dec 2024 18:35:04 +0000 (19:35 +0100)] 
tools: Unify help messages

Unify help messages shown when kmod is called without any arguments
to use the same grammar and capitalisation.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/265
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite/test-weakdep: remove custom handling
Emil Velikov [Thu, 21 Nov 2024 17:53:47 +0000 (17:53 +0000)] 
testsuite/test-weakdep: remove custom handling

Currently test-weakdep is the only test which explicitly sets the kernel
module lookup directory and the modprobe.d location. It does so by
explicitly hard-coding the full path for both, thus effectively
bypassing the path handling done in our `LD_PRELOAD` module `path.so`.

Just use `kmod_new(NULL, NULL);` which will ensure that everything is
handled relatively to `TC_ROOTFS` defined further down in the test.

Note: this technically reduces our test coverage, although kmod_new() is
not the goal of this test and should be handled separately.

Noticed while removing the TESTSUITE_ROOTFS instances from DEFINE_TEST()

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/258
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: hash_new(): always use a step size of at least 4
Martin Wilck [Thu, 21 Nov 2024 23:02:26 +0000 (00:02 +0100)] 
libkmod: hash_new(): always use a step size of at least 4

The current algorithm would choose a step size of 4 for
n_buckets == 31, but 1 for n_buckets == 32, which seems wrong.
Fix it.

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: strbuf_pushchars: handle pushing empty string gracefully
Martin Wilck [Fri, 15 Nov 2024 22:26:26 +0000 (23:26 +0100)] 
libkmod: strbuf_pushchars: handle pushing empty string gracefully

If buf->bytes, buf->used, and len are all 0, buf_grow() will do nothing,
and memcpy() willbe called with a NULL first argument. This will cause
an error because the function is annotated with __nonnull(1, 2).

Fixes: e62d8c7 ("strbuf: make strbuf_pushchars() a little less dumb")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: test_strbuf_pushmem: test pushing 0 bytes to an empty strbuf
Martin Wilck [Thu, 21 Nov 2024 22:56:10 +0000 (23:56 +0100)] 
testsuite: test_strbuf_pushmem: test pushing 0 bytes to an empty strbuf

This test fails, and will be fixed by the next commit.

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: hash_del: handle deleting a non-existing element gracefully
Martin Wilck [Fri, 15 Nov 2024 22:25:15 +0000 (23:25 +0100)] 
libkmod: hash_del: handle deleting a non-existing element gracefully

It can happen that bucket->entries is NULL, but bsearch is annotated
such that it's second argument must be non-NULL.

Fixes: 7db0865 ("Add simple hash implementation")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: test-hash: add a test for deleting a non-existing element
Martin Wilck [Thu, 21 Nov 2024 22:45:02 +0000 (23:45 +0100)] 
testsuite: test-hash: add a test for deleting a non-existing element

This test fails and will be fixed by the next commit.

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: hash_del: fix out-of-bounds memory access
Martin Wilck [Fri, 15 Nov 2024 22:24:30 +0000 (23:24 +0100)] 
libkmod: hash_del: fix out-of-bounds memory access

Assume bucket->used is 1, and element 0 is deleted. We shouldn't access any
memory above (entry + 1) in this case. Likewise, if bucked->used is 2, only
one element should be shifted, etc.

Fixes: 7db0865 ("Add simple hash implementation")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: test-hash: add a test for deleting a single element
Martin Wilck [Thu, 21 Nov 2024 21:52:34 +0000 (22:52 +0100)] 
testsuite: test-hash: add a test for deleting a single element

...using n_buckets = 32, which will cause a step size of 1.
This test fails, and will be fixed by the next commit.

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agoman: remove duplicate option in depmod.8.scd
Martin Wilck [Wed, 13 Nov 2024 23:53:14 +0000 (00:53 +0100)] 
man: remove duplicate option in depmod.8.scd

Fixes: c36ddb6 ("depmod: Add option to override MODULE_DIRECTORY")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: expand the deprecated/0xB007FA57 comment
Emil Velikov [Thu, 21 Nov 2024 09:15:36 +0000 (09:15 +0000)] 
libkmod: expand the deprecated/0xB007FA57 comment

Provide a summary how the magic and version fields have evolved. It
serves as better reference than the current "deprecated versions" note.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/256
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: Test that modprobe --show-depends doesn't load module
Lucas De Marchi [Thu, 21 Nov 2024 14:19:08 +0000 (08:19 -0600)] 
testsuite: Test that modprobe --show-depends doesn't load module

Remove /proc/modules and /sys/module/* from the show-depends rootfs
since they shouldn't be there and add a test to make sure no module is
loaded.

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/254
8 months agotestsuite: No need to check for not loaded
Lucas De Marchi [Tue, 19 Nov 2024 14:55:58 +0000 (08:55 -0600)] 
testsuite: No need to check for not loaded

Simplify tests that just dump the configuration: no need to check that
no module was loaded.

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/254
8 months agotestsuite: Check cmdline for multiple blacklist
Lucas De Marchi [Tue, 19 Nov 2024 14:53:57 +0000 (08:53 -0600)] 
testsuite: Check cmdline for multiple blacklist

Make sure this (undocumented) behavior doesn't change in future.
Multiple blacklist append to the config.

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/254
8 months agotestsuite: Remove duplicated directory
Lucas De Marchi [Tue, 19 Nov 2024 14:48:48 +0000 (08:48 -0600)] 
testsuite: Remove duplicated directory

module-param-kcmdline7 was incorrectly copied inside
module-param-kcmdline{7,8}. Drop the extra dir that is not used
anywhere.

Fixes: d3a1fe67b64c ("libkmod-config: re-quote option from kernel cmdline")
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/254
8 months agolibkmod: Improve index dump performance
Tobias Stoeckmann [Sun, 17 Nov 2024 10:39:59 +0000 (11:39 +0100)] 
libkmod: Improve index dump performance

Buffer data before calling write for output. This reduces the amount
of total write system calls and leads to faster execution times.

This is a simple approach to imitate FILE's I/O buffering, which we
cannot use because we want to properly handle interrupts.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/252
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: use uint8_t for the child prefix/index
Emil Velikov [Sat, 16 Nov 2024 15:20:42 +0000 (15:20 +0000)] 
libkmod: use uint8_t for the child prefix/index

Stop implicitly casting the child prefix/index to int. It can have high
bits set thus get promoted to wildly incorrect value and cause chaos
further on.

In addition, convert the existing `unsigned char` instances to uint8_t,
which better illustrates what we're after - a fixed sized 8 bit unsigned
integer.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/251
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agodepmod: use uint8_t for the child prefix/index
Emil Velikov [Sat, 16 Nov 2024 15:20:42 +0000 (15:20 +0000)] 
depmod: use uint8_t for the child prefix/index

Stop implicitly casting the child prefix/index to int. It can have high
bits set thus get promoted to wildly incorrect value and cause chaos
further on.

In addition, convert the existing `unsigned char` instances to uint8_t,
which better illustrates what we're after - a fixed sized 8 bit unsigned
integer.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/251
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod, tools: Fix format specifiers
Tobias Stoeckmann [Fri, 15 Nov 2024 17:46:58 +0000 (18:46 +0100)] 
libkmod, tools: Fix format specifiers

Use %zu for size_t, not %zd which would be for ssize_t.

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/248
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Prevent ouf of boundary access
Tobias Stoeckmann [Fri, 15 Nov 2024 17:45:28 +0000 (18:45 +0100)] 
libkmod: Prevent ouf of boundary access

Follow up of aad7c697, which fixes the same issue in another function.

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/248
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Propagate hash_add errors
Tobias Stoeckmann [Fri, 15 Nov 2024 17:43:12 +0000 (18:43 +0100)] 
libkmod: Propagate hash_add errors

If newly created module in kmod_module_new could not be added to hash,
return an error. Otherwise it is possible to create multiple independent
structs with the same name, which the hash set is supposed to prevent.

An error only occurs in out of memory conditions.

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/248
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: remove need_spawn = false support
Emil Velikov [Fri, 15 Nov 2024 15:54:43 +0000 (15:54 +0000)] 
testsuite: remove need_spawn = false support

Our test suite is a little unique in my experience in that the test can
be either a normal (fork) child or a (re)spawned one. All the other test
frameworks I have used opt for only one of the two.

I'm not entirely sure why we have both since the latter is sufficient for
all use-cases that we have. Perhaps the former was kept as
micro-optimisation?

Currently I am exploring a way to provide the results summary and the
need_spawn = false ones, are printed multiple times. At a glance I
couldn't quite find a way to fix it.

In addition I am also looking at removing/reducing the use of exit()
across the test suite. Where the two code-flows makes that process more
convoluted.

So let's remove one of the code-paths, simplify things and fix the
logging output. If needed we can re-introduce it later on.

NOTE: there's a lot going on here, because clang-format insist on
reformatting bunch of the DEFINE_TEST() instances :-\

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/246
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite/test-init: use distinct test names
Emil Velikov [Fri, 15 Nov 2024 15:44:26 +0000 (15:44 +0000)] 
testsuite/test-init: use distinct test names

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/246
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: run all tests, don't exit on first failure
Emil Velikov [Wed, 13 Nov 2024 12:58:00 +0000 (12:58 +0000)] 
testsuite: run all tests, don't exit on first failure

Instead of exiting on the first failing test execute all tests and
report overall failure as needed.

This gives us a better overview - which do we have single failure or
multiple instances need work - and more consistent results numbers.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/246
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: use exitcode 77 for skipped tests
Emil Velikov [Fri, 15 Nov 2024 15:38:23 +0000 (15:38 +0000)] 
testsuite: use exitcode 77 for skipped tests

Using 77 is a de-facto standard for skipped tests that both autotools
and meson support OOTB. Use that instead of EXIT_SUCCESS aka 0.

Semi-recently we removed the only skip = true tests, although with more
tests incoming we're need bound to have a few users.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/246
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite/modprobe: Simplify modprobe exec
Emil Velikov [Fri, 15 Nov 2024 16:54:47 +0000 (16:54 +0000)] 
testsuite/modprobe: Simplify modprobe exec

Too much copy and paste for simple exec tests. Just provide an
EXEC_MODPROBE macro to help.

Hats off to Lucas for the initial idea and porting the depmod test.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/246
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agotestsuite: Test depmod with relative -m
Lucas De Marchi [Thu, 14 Nov 2024 21:36:09 +0000 (15:36 -0600)] 
testsuite: Test depmod with relative -m

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/243
8 months agotestsuite: Simplify defines for depmod -m test
Lucas De Marchi [Tue, 19 Nov 2024 07:05:47 +0000 (01:05 -0600)] 
testsuite: Simplify defines for depmod -m test

Simplify the defines so it's easier to extend the test for another
directory.

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/243
8 months agotestsuite/depmod: Simplify depmod exec
Lucas De Marchi [Thu, 14 Nov 2024 20:33:02 +0000 (14:33 -0600)] 
testsuite/depmod: Simplify depmod exec

Too much copy and paste for simple exec tests. Just provide an
EXEC_DEPMOD macro to help.

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/243
8 months agotestsuite/depmod: Remove bogus rootfs from test
Lucas De Marchi [Thu, 14 Nov 2024 16:53:39 +0000 (10:53 -0600)] 
testsuite/depmod: Remove bogus rootfs from test

The testsuite rootfs shouldn't be part of the argument.

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/243
8 months agodepmod: Fix handling relative moduledir
Lucas De Marchi [Thu, 14 Nov 2024 16:33:14 +0000 (10:33 -0600)] 
depmod: Fix handling relative moduledir

Make sure moduledir is always relative to the basedir and document it as
such.

Note: scdoc 1.11.3 produces a strange result when trying to render the
man page for the 2 examples. Workaround that by ending with an explicit
newline, which produces another stranger behavior (but visually correct)
of indenting the next line.

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/243
8 months agoman: Fix reference to MODULEDIR
Lucas De Marchi [Thu, 14 Nov 2024 16:28:37 +0000 (10:28 -0600)] 
man: Fix reference to MODULEDIR

The expression used is <MODULEDIR>, so keep it consistent with the
option name.

Fixes: e9f2580eaf27 ("man: Reword depmod's paths")
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/243
8 months agodepmod: Remove multiple ternary operations
Lucas De Marchi [Thu, 14 Nov 2024 15:41:05 +0000 (09:41 -0600)] 
depmod: Remove multiple ternary operations

Just keep a separate default value around and use it in case no option
from command line was parsed.

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/243
8 months agoutil: Promote path_is_absolute() to header
Lucas De Marchi [Thu, 14 Nov 2024 15:48:30 +0000 (09:48 -0600)] 
util: Promote path_is_absolute() to header

This is a trivial function that can be used elsewhere. There's no point
in keeping the assert if we are going to crash in the very next
instruction. Rather add the relevant attribute and drop the assert.

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/243
8 months agodepmod: Fix error messages related to module_directory
Lucas De Marchi [Thu, 14 Nov 2024 05:55:10 +0000 (23:55 -0600)] 
depmod: Fix error messages related to module_directory

It's no longer a constant: print the right message.

Fixes: 4f8a6a85 ("tools: Check snprintf return value")
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/243
8 months agoci/codeql: use filter-sarif to filter meson-private
Enrico Joerns [Wed, 28 Feb 2024 23:33:30 +0000 (00:33 +0100)] 
ci/codeql: use filter-sarif to filter meson-private

There is a severe number of false-positive in code scanning caused by
inspecting meson-internal test files like
'build/meson-private/tmpzb46osmq/testfile.c'.

As a workaround, use the 'filter-sarif' action to filter out these
results before uploading the SARIF (Static Analysis Results Interchange
Format).

This PR was inspired by https://github.com/rauc/rauc/pull/1346 and the
example from https://github.com/advanced-security/filter-sarif.

Signed-off-by: Enrico Joerns <ejo@pengutronix.de>
[Emil: port from rauc, use checksums for actions]
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/249
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agodepmod: Properly check index keys
Tobias Stoeckmann [Fri, 15 Nov 2024 17:22:45 +0000 (18:22 +0100)] 
depmod: Properly check index keys

Synchronize character checks with libkmod-index.c. 8-bit ASCII chars
would turn negative (due to signed char), which index__checkstring does
not properly check for.

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/247
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agoci: add meson setup -D {distconf,module}dir=relative/ checks
Emil Velikov [Fri, 15 Nov 2024 15:27:26 +0000 (15:27 +0000)] 
ci: add meson setup -D {distconf,module}dir=relative/ checks

Based on the autotools build, meson does not support relative
directories for distconfig and moduledir. We fixed that recently, but
never added a check so we don't regress.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/245
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Fix file error handling regression
Tobias Stoeckmann [Wed, 13 Nov 2024 22:32:55 +0000 (23:32 +0100)] 
libkmod: Fix file error handling regression

The propagation of errors from loading function through errno must make
sure that errno is positive, otherwise errors are not correctly spotted.

Fixes: 81e5c797 ("libkmod: propagate {zstd,xz,zlib}_load errors")
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/242
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Fix typo in comment
Tobias Stoeckmann [Wed, 13 Nov 2024 18:43:48 +0000 (19:43 +0100)] 
libkmod: Fix typo in comment

The word practise only exists in British English and is a verb.
Switch to practice and adjust style.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/241
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Improve index comment
Tobias Stoeckmann [Wed, 13 Nov 2024 18:41:04 +0000 (19:41 +0100)] 
libkmod: Improve index comment

In version 1 of kmod, definitions of INDEX_MAGIC_OLD and INDEX_MAGIC
were in two adjacent lines, which made the sentence starting with
"Second one" much easier to read.

Since the "second one" is actually INDEX_MAGIC and even kmod version 1
did use it, let's not talk about "newer versioned binary files" anymore
but clearly state that 0xB007FA57 is deprecated.

Also add a missing full stop.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Max Kunzelmann <maxdev@posteo.de>
Link: https://github.com/kmod-project/kmod/pull/241
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Do not set errno in libkmod-index
Max Kunzelmann [Tue, 12 Nov 2024 21:49:41 +0000 (22:49 +0100)] 
libkmod: Do not set errno in libkmod-index

Don't set errno in read functions ad it's a libc interface which we
should not be overriding. None of the functions up the call stack (some
examples) check on it.

Signed-off-by: Max Kunzelmann <maxdev@posteo.de>
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/240
[ Reword commit message according to suggestion by Emil and remove
  inline ]
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agoRemove scratchbuf implementation
Lucas De Marchi [Tue, 12 Nov 2024 20:31:36 +0000 (14:31 -0600)] 
Remove scratchbuf implementation

All its unique features have been ported to strbuf and users converted.
Nuke it.

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
8 months agodepmod: Convert output_symbols_bin() to strbuf
Lucas De Marchi [Tue, 12 Nov 2024 20:26:56 +0000 (14:26 -0600)] 
depmod: Convert output_symbols_bin() to strbuf

Remove last use of scratchbuf.

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
8 months agodepmod: Convert depmod_modules_search_path() to strbuf
Lucas De Marchi [Tue, 12 Nov 2024 20:07:32 +0000 (14:07 -0600)] 
depmod: Convert depmod_modules_search_path() to strbuf

Replace the scratchbuf usage with corresponding API from strbuf.

depmod_modules_search_path() itself may further be simplified in the
future to share opening the dir with depmod_modules_search_dir(),
but that is left for later.

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
8 months agostrbuf: Add strbuf_shrink_to()
Lucas De Marchi [Thu, 14 Nov 2024 05:02:07 +0000 (23:02 -0600)] 
strbuf: Add strbuf_shrink_to()

Useful when working with paths on a loop and resetting to a base path
component on every loop iteration.

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
8 months agostrbuf: Add strbuf_used()
Lucas De Marchi [Thu, 14 Nov 2024 04:49:52 +0000 (22:49 -0600)] 
strbuf: Add strbuf_used()

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
8 months agostrbuf: Add strbuf_pushmem()
Lucas De Marchi [Tue, 12 Nov 2024 19:45:33 +0000 (13:45 -0600)] 
strbuf: Add strbuf_pushmem()

Wrapper for memcpy(). It's similar to strbuf_pushchars(), but push any
char, including the NUL byte, relying on the size passed as argument.

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
8 months agostrbuf: Do not append '\0' if not needed
Lucas De Marchi [Tue, 12 Nov 2024 19:43:20 +0000 (13:43 -0600)] 
strbuf: Do not append '\0' if not needed

Unconditionally appending '\0' is not a big problem, but that does
trigger the buffer to potentially be re-allocated. Avoid that by
checking the last char is not already NUL.

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
8 months agostrbuf: Add strbuf_reserve_extra()
Lucas De Marchi [Tue, 12 Nov 2024 18:07:39 +0000 (12:07 -0600)] 
strbuf: Add strbuf_reserve_extra()

To accomplish the same task as scratchbuf_alloc() does: make sure the
buffer has enough space for next operations. One difference is that
ensures **extra** space, not the total space. If needed in future,
a strbuf_reserve(), that resembles C++'s std::vector::reserve(), can be
added on top.

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
8 months agostrbuf: Allow to start with stack space
Lucas De Marchi [Tue, 12 Nov 2024 16:09:38 +0000 (10:09 -0600)] 
strbuf: Allow to start with stack space

This is the main functionality of the scratchbuf implementation: it
starts with a buffer on stack that covers most of the calls, but also
has a fallback to allocate the buffer if it grows beyond the initial
size.

Port that functionality from scratchbuf to strbuf, so the former can be
eventually removed.

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
8 months agostrbuf: Invalidate (only) when stolen
Lucas De Marchi [Tue, 12 Nov 2024 15:34:58 +0000 (09:34 -0600)] 
strbuf: Invalidate (only) when stolen

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
8 months agostrbuf: Document strbuf_popchar(s)
Lucas De Marchi [Tue, 12 Nov 2024 15:25:52 +0000 (09:25 -0600)] 
strbuf: Document strbuf_popchar(s)

Document the behavior for these functions and also clarify why the
testsuite can check the buffer for equality after calling strbuf_str().

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
8 months agostrbuf: Re-use buf_realloc()
Lucas De Marchi [Tue, 12 Nov 2024 15:07:05 +0000 (09:07 -0600)] 
strbuf: Re-use buf_realloc()

Let the realloc happen in only one place, so we can change its behavior
in future.

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
8 months agostrbuf: Extract realloc
Lucas De Marchi [Tue, 12 Nov 2024 14:36:54 +0000 (08:36 -0600)] 
strbuf: Extract realloc

Extract the realloc from the size calculation so it can be shared with
other upcoming callers.

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
8 months agolibkmod: Release memory on error paths
Tobias Stoeckmann [Tue, 5 Nov 2024 17:17:50 +0000 (18:17 +0100)] 
libkmod: Release memory on error paths

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>
8 months agodepmod: Release memory on error path
Tobias Stoeckmann [Wed, 6 Nov 2024 16:50:27 +0000 (17:50 +0100)] 
depmod: Release memory on error path

Check malloc return value and clean up resources if allocation fails.

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>
8 months agodepmod: Release memory on error paths
Tobias Stoeckmann [Tue, 5 Nov 2024 16:58:15 +0000 (17:58 +0100)] 
depmod: Release memory on error paths

If a list node could not be added, release already allocated data which
was supposed to end up in list.

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>
8 months agolibkmod: Introduce kmod_list_release
Tobias Stoeckmann [Tue, 5 Nov 2024 16:47:02 +0000 (17:47 +0100)] 
libkmod: Introduce kmod_list_release

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>
8 months agolibkmod: store common section off/size and use them
Emil Velikov [Wed, 23 Oct 2024 23:27:08 +0000 (00:27 +0100)] 
libkmod: store common section off/size and use them

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>
8 months agolibkmod: rename kmod_elf_get_strings()
Emil Velikov [Wed, 23 Oct 2024 23:37:31 +0000 (00:37 +0100)] 
libkmod: rename kmod_elf_get_strings()

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>
8 months agolibkmod: stop copying symbol names in kmod_elf_get_dependency_symbols()
Emil Velikov [Wed, 23 Oct 2024 16:19:53 +0000 (17:19 +0100)] 
libkmod: stop copying symbol names in kmod_elf_get_dependency_symbols()

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>
8 months agolibkmod: stop copying symbol names in kmod_elf_get_symbols()
Emil Velikov [Wed, 23 Oct 2024 16:19:53 +0000 (17:19 +0100)] 
libkmod: stop copying symbol names in kmod_elf_get_symbols()

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>
8 months agolibkmod: stop copying symbol names in kmod_elf_get_symbols_symtab()
Emil Velikov [Wed, 23 Oct 2024 16:19:53 +0000 (17:19 +0100)] 
libkmod: stop copying symbol names in kmod_elf_get_symbols_symtab()

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>
8 months agolibkmod: check for trailing \0 in __ksymtab_strings
Emil Velikov [Thu, 24 Oct 2024 22:18:28 +0000 (23:18 +0100)] 
libkmod: check for trailing \0 in __ksymtab_strings

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>
8 months agolibkmod: stop copying symbol names in kmod_elf_get_modversions()
Emil Velikov [Wed, 23 Oct 2024 16:19:53 +0000 (17:19 +0100)] 
libkmod: stop copying symbol names in kmod_elf_get_modversions()

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>
8 months agolibkmod: const annotate kmod_modversion::symbol
Emil Velikov [Wed, 23 Oct 2024 22:11:27 +0000 (23:11 +0100)] 
libkmod: const annotate kmod_modversion::symbol

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>
8 months agodepmod: Remove a malloc call
Emil Velikov [Thu, 17 Oct 2024 13:53:03 +0000 (15:53 +0200)] 
depmod: Remove a malloc call

The offsets of node children can be stored on stack.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/188
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agodepmod: Write index in pre-order
Tobias Stoeckmann [Wed, 16 Oct 2024 19:38:04 +0000 (21:38 +0200)] 
depmod: Write index in pre-order

Index files in pre-order have a significant performance improvement
for libkmod users.

On Arch Linux system, dumping configuration takes 296 read calls
in pre-order, compared to 4080 in post-order. Tests on a Raspberry
Pi 2 test system have shown an improvement by 9 %.

Even writing is faster now. This happens because we must know in
advance how many bytes index nodes will consume in the resulting file.
Although this code calculates it on the fly and caches the results,
saving lseek system calls has a significant positive effect which
compensates this extra overhead.

On Arch Linux system, writing index takes 6143 lseek calls now, compared
to previous 83701, leading to a performance gain of 13 %.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/188
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agomeson: undefine NDEBUG in the tests
Emil Velikov [Mon, 11 Nov 2024 13:08:20 +0000 (13:08 +0000)] 
meson: undefine NDEBUG in the tests

When using -D b_ndebug=true or inheriting CFLAGS="-DNDEBUG" from the
environment, the asserts will be no-op thus the checks in the
LD_PRELOADED libraries will be omitted.

Explicitly undefine the macro, so we don't accidentally shoot ourselves
in the foot.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/238
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agoci: bump the all-actions group across 1 directory with 3 updates
dependabot[bot] [Fri, 8 Nov 2024 17:55:23 +0000 (17:55 +0000)] 
ci: bump the all-actions group across 1 directory with 3 updates

Bumps the all-actions group with 3 updates in the / directory: [actions/checkout](https://github.com/actions/checkout), [yshui/git-clang-format-lint](https://github.com/yshui/git-clang-format-lint) and [github/codeql-action](https://github.com/github/codeql-action).

Updates `actions/checkout` from 4.1.7 to 4.2.2
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/692973e3d937129bcbf40652eb9f2f61becf3332...11bd71901bbe5b1630ceea73d27597364c9af683)

Updates `yshui/git-clang-format-lint` from 1.16 to 1.17
- [Release notes](https://github.com/yshui/git-clang-format-lint/releases)
- [Commits](https://github.com/yshui/git-clang-format-lint/compare/a65b466f5903524aef27552f63c3906c0f73f184...27f3890c6655216edadcc2759110b9c127c74786)

Updates `github/codeql-action` from 3.26.7 to 3.27.1
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/8214744c546c1e5c8f03dde8fab3a7353211988d...4f3212b61783c3c68e8309a0f18a699764811cda)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: all-actions
- dependency-name: yshui/git-clang-format-lint
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: all-actions
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: all-actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Link: https://github.com/kmod-project/kmod/pull/237
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
8 months agolibkmod: Fix overflow in kmod_module_hex_to_str
Tobias Stoeckmann [Fri, 8 Nov 2024 16:08:41 +0000 (17:08 +0100)] 
libkmod: Fix overflow in kmod_module_hex_to_str

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>
8 months agolibkmod: Avoid overflows while parsing files
Tobias Stoeckmann [Thu, 7 Nov 2024 20:24:35 +0000 (21:24 +0100)] 
libkmod: Avoid overflows while parsing files

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>
8 months agotree-wide: Sprinkle _clang_suppress_alloc_
Lucas De Marchi [Mon, 11 Nov 2024 15:04:44 +0000 (09:04 -0600)] 
tree-wide: Sprinkle _clang_suppress_alloc_

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
8 months agolibkmod: Simplify lookup_builtin_file()
Lucas De Marchi [Thu, 7 Nov 2024 20:26:39 +0000 (14:26 -0600)] 
libkmod: Simplify lookup_builtin_file()

Use cleanup attribute.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/233
8 months agoshared: Ignore clang-analyzer on cleanup attribute
Lucas De Marchi [Sat, 9 Nov 2024 22:53:22 +0000 (16:53 -0600)] 
shared: Ignore clang-analyzer on cleanup attribute

When using the cleanup attribute we know we are not leaking that
allocation. Most of the time the assignment is together with the
declaration, so we can simplify additional clang annotations by making
the cleanup attribute imply clang::suppress.

In cases declaration and assignment are not together, provide
_clang_suppress_alloc_ to annotate the code. That is only defined for
clang analyzer.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/233
8 months agobuild: Stop warning for attribute clang::suppress
Lucas De Marchi [Sat, 9 Nov 2024 22:23:02 +0000 (16:23 -0600)] 
build: Stop warning for attribute clang::suppress

That attribute allows us to instruct the Clang Static Analyzer to stop
giving some false positives. However when building the code (with gcc
and clang) they warn that the attribute is ignored. Just ignore as we
know what the attribute is for.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/233
8 months agoshared: Move cleanup attribute
Lucas De Marchi [Sat, 9 Nov 2024 22:20:18 +0000 (16:20 -0600)] 
shared: Move cleanup attribute

Move to macro.h where other attributes are located. The inline helper
function can also move along as we don't need to keep it separate.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/233
8 months agoman: add modprobe.conf.5 manpage link
Emil Velikov [Thu, 7 Nov 2024 15:44:30 +0000 (15:44 +0000)] 
man: add modprobe.conf.5 manpage link

Seemingly Fedora and friends have been carrying this one for years. Just
add it to kmod itself so that people using multiple distros have
consistent experience.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/231
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>