]> git.ipfire.org Git - thirdparty/kmod.git/log
thirdparty/kmod.git
9 months agoshared: introduce umul{32,64}_overflow() helpers
Emil Velikov [Mon, 30 Sep 2024 20:35:18 +0000 (21:35 +0100)] 
shared: introduce umul{32,64}_overflow() helpers

We'll use them to implement the size_t variant.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/169
[ Fixup commit message for renamed functions ]
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoshared: introduce uadd32_overflow() helper
Emil Velikov [Mon, 30 Sep 2024 20:06:14 +0000 (21:06 +0100)] 
shared: introduce uadd32_overflow() helper

We'll use it in the upcoming size_t variant.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/169
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoshared: s/addu64_overflow/uadd64_overflow/g
Emil Velikov [Mon, 30 Sep 2024 21:36:29 +0000 (22:36 +0100)] 
shared: s/addu64_overflow/uadd64_overflow/g

Rename the helper closer to the actual built-in.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/169
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoshared: tweak addu64_overflow() #if/else chain
Emil Velikov [Mon, 30 Sep 2024 19:52:18 +0000 (20:52 +0100)] 
shared: tweak addu64_overflow() #if/else chain

Group the checks as applicable - require the long variant when
sizeof(long) == 8, or the long long one as sizeof(long long) == 8.

Ultimately, fold fallback in the #else path, since it's dead code atm
and seemingly confuses tools such as Coverity.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/169
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agolibkmod: call fnmatch() only as needed
Emil Velikov [Tue, 8 Oct 2024 17:04:34 +0000 (18:04 +0100)] 
libkmod: call fnmatch() only as needed

Currently we'll call the function even when the patterns is an empty
string.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/174
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agolibkmod: use strbuf_pushchars() where possible
Emil Velikov [Tue, 8 Oct 2024 17:04:34 +0000 (18:04 +0100)] 
libkmod: use strbuf_pushchars() where possible

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/174
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotestsuite/test-strbuf: use const char * for strbuf_str()
Emil Velikov [Tue, 8 Oct 2024 17:04:34 +0000 (18:04 +0100)] 
testsuite/test-strbuf: use const char * for strbuf_str()

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/174
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoci/actions: LC_ALL=C sort the packages
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
ci/actions: LC_ALL=C sort the packages

I'm about to add some more and the current (almost sorted) state breaks
my brain :-x

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoci: directly use archlinux:multilib-devel container
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
ci: directly use archlinux:multilib-devel container

There was some recent snafu which meant the container wasn't available.
With that resolved, we can drop our hack and use the correct container
directly.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agolibkmod: silence autological compare warning
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
libkmod: silence autological compare warning

Clang will detect that the enum cannot be zero, thus triggering a
warning. Since this is an external (public API) function, the end-user
can provide any input so we want to keep the check.

Note: include the pragmas in a if defined(__clang__) guard, otherwise
we'll trigger -Wunknown_pragma which will become an error in CI and
developer builds.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotools/depmod: cast variable in DBG() statement
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
tools/depmod: cast variable in DBG() statement

Older versions of clang (14.0.0 as seen in Ubuntu 22.04) will flag a
warning that we're using unsigned int, with a unsigned char modifier.

Newer versions like 16.0.6 (Debian unstable), 17.0.6 (Alpine), etc do
not flag this as an issue.

In practise the number fits in the latter range, so just add a cast to
silence the warning.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotools/lsmod: initialize use_syslog
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
tools/lsmod: initialize use_syslog

Initialize the variable, otherwise we'll get (random) stack value if the
user hasn't provided --syslog. Reported by scan-build.

Fixes: 6313d40d ("tools/lsmod: add basic opts like rmmod")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotools/rmmod: initialize use_syslog
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
tools/rmmod: initialize use_syslog

Initialize the variable, otherwise we'll get (random) stack value if the
user hasn't provided --syslog. Reported by scan-build.

Fixes: 24fe68dc ("tools/rmmod: make opt variables non-global")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotools/insmod: initialize use_syslog
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
tools/insmod: initialize use_syslog

Initialize the variable, otherwise we'll get (random) stack value if the
user hasn't provided --syslog. Reported by scan-build.

Fixes: ca8f04e8 ("tools/insmod: add syslog and verbose options")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoshared: add missing stddef.h include
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
shared: add missing stddef.h include

Required for size_t, reported by clang-tidy.

Fixes: 38943b20 ("shared: use size_t for strbuf")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotools: add missing stdio.h include
Emil Velikov [Thu, 3 Oct 2024 16:02:45 +0000 (17:02 +0100)] 
tools: add missing stdio.h include

Required for puts(), reported by clang-tidy.

Fixes: b644b8d7 ("tools: add kmod_version() helper")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoman: list short/long opt on same line
Emil Velikov [Thu, 3 Oct 2024 15:46:00 +0000 (16:46 +0100)] 
man: list short/long opt on same line

With earlier commit, I attempted to keep them on separate lines for
somewhat dubious reasons. In practise that didn't work and they stayed
on the same line, while also dropping the separating comma.

Looking back, it would be better to fix the vim manpager mode issues
rather than mask it. Mea culpa

Effectively this reverts commit e7626810fd0f34db2c205b86ff6ad6cf42794fa7

Reference: https://lore.kernel.org/linux-modules/ZvknyLKvQeBo16n9@meinfjell.helgefjelltest.de
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/164
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoman: add BUGS section with basic information
Emil Velikov [Mon, 30 Sep 2024 13:31:37 +0000 (14:31 +0100)] 
man: add BUGS section with basic information

Link the issue tracker and recommend providing reproducer/expectations
in the bug report.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/164
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agotools/insmod: args -> module options
Emil Velikov [Mon, 30 Sep 2024 13:23:13 +0000 (14:23 +0100)] 
tools/insmod: args -> module options

Align the help message with the manual page, swapping the ambiguous
"args" with module options.

Reference: https://lore.kernel.org/linux-modules/ZvknyLKvQeBo16n9@meinfjell.helgefjelltest.de
Reported-by: Helge Kreutzmann <debian@helgefjell.de>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/164
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agoman: few manpage-l10n inspired suggestions
Emil Velikov [Mon, 30 Sep 2024 13:18:46 +0000 (14:18 +0100)] 
man: few manpage-l10n inspired suggestions

Address a couple of stylistic (missing comma, manpage #, italic) issues,
reported by manpage-l10n project.

Reference: https://lore.kernel.org/linux-modules/ZvknyLKvQeBo16n9@meinfjell.helgefjelltest.de
Reported-by: Helge Kreutzmann <debian@helgefjell.de>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/164
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agolibkmod: unref in kmod_module_new_from_path() error path
Emil Velikov [Thu, 3 Oct 2024 15:27:37 +0000 (16:27 +0100)] 
libkmod: unref in  kmod_module_new_from_path() error path

Preserve the original behaviour, where the module refcount stays the
same, if the function fails.

Fixes: e19338da ("libkmod: Avoid redundant kmod_pool_get_module call")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/171
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
9 months agolibkmod: Check child range in memory mapped index
Tobias Stoeckmann [Mon, 30 Sep 2024 17:20:33 +0000 (19:20 +0200)] 
libkmod: Check child range in memory mapped index

If value of "first" is negative, then a broken index can trigger a stack
based buffer overflow, because child_count could become larger than
INDEX_CHILDMAX.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/167
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Avoid redundant kmod_pool_get_module call
Tobias Stoeckmann [Mon, 30 Sep 2024 18:27:49 +0000 (20:27 +0200)] 
libkmod: Avoid redundant kmod_pool_get_module call

Before kmod_module_new_from_path creates a module with kmod_module_new,
it checks with kmod_pool_get_module if a module with given name already
exists.

This check can be removed, because kmod_module_new does the same. If the
module already existed, the same checks are performed as in current code
for the "if (m != NULL)" case. If it did not exist yet, m->path is NULL
and the first if-block is entered.

And since kmod_module_new takes care of incrementing the reference
counter, the explicit call of kmod_module_ref can be removed from
kmod_module_new_from_path as well.

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/168
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Clarify the meaning of char array "alias"
Tobias Stoeckmann [Mon, 30 Sep 2024 16:54:54 +0000 (18:54 +0200)] 
depmod: Clarify the meaning of char array "alias"

It is meant as a backing buffer for scratch buffer. Do not use it
directly when constructing strings.

Co-authored-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/166
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Actually use scratch buffer memory
Tobias Stoeckmann [Mon, 30 Sep 2024 16:27:13 +0000 (18:27 +0200)] 
depmod: Actually use scratch buffer memory

Accessing the backing "alias" char buffer directly works only as long as
all symbols are smaller than 1024, because otherwise the scratch buffer
allocates memory and the addresses do not match anymore.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/166
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Fix typos
Tobias Stoeckmann [Tue, 24 Sep 2024 20:39:21 +0000 (22:39 +0200)] 
libkmod: Fix typos

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/152
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: check strbuf return values
Tobias Stoeckmann [Tue, 24 Sep 2024 18:56:40 +0000 (20:56 +0200)] 
libkmod: check strbuf return values

The strbuf functions may fail, so handle errors. Make especially sure
that push and pop operations stay in sync.

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/152
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: store index value lengths as size_t
Tobias Stoeckmann [Tue, 24 Sep 2024 18:55:20 +0000 (20:55 +0200)] 
libkmod: store index value lengths as size_t

The unsigned int data type might be too small on 64 bit systems and
when encountering huge and illegal index files.

Since lengths are retrieved from strbuf structs, this also keeps
the data type lengths in sync.

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/152
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Check amount of memories
Tobias Stoeckmann [Mon, 30 Sep 2024 15:05:23 +0000 (17:05 +0200)] 
depmod: Check amount of memories

The code does not support 65535 or more modules. Since this value is
probably never reached, add a proper check after array building and
do not rely solely on an assert later in code path.

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/158
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Support modules.order with duplicate lines
Tobias Stoeckmann [Sun, 29 Sep 2024 19:29:56 +0000 (21:29 +0200)] 
depmod: Support modules.order with duplicate lines

If the same line exists multiple times in modules.order, consider only
the first, since this is the earliest position requested.

This also makes sure that index iterator never turns positive or would
ever trigger a signed integer 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/158
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Read modules.order only once
Tobias Stoeckmann [Fri, 27 Sep 2024 20:17:36 +0000 (22:17 +0200)] 
depmod: Read modules.order only once

The current code iterates through modules.order twice. First, it
figures out how many lines exist. Then it iterates again to make
sure that the first line has the lowest sort index, then ascending.

Negative values make sure that the previously assigned positive
values will be larger, i.e. modules in modules.order take precedence,
then modules found in file system which were not listed in
modules.order.

This can be simplified by setting the initial sort index value to
the lowest possible value needed, i.e. -depmod->modules.count.

With this value, it is possible to iterate only once.

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/158
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Check memdup return value
Tobias Stoeckmann [Fri, 27 Sep 2024 20:17:11 +0000 (22:17 +0200)] 
depmod: Check memdup return value

If memdup fails, handle out of memory condition.

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/158
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Use memdup
Tobias Stoeckmann [Fri, 27 Sep 2024 20:15:59 +0000 (22:15 +0200)] 
depmod: Use memdup

No need to clear newly allocated memory if source is copied into
destination directly.

Simplify code by using memdup from shared.

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/158
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Fix typo in comment
Tobias Stoeckmann [Fri, 27 Sep 2024 19:32:43 +0000 (21:32 +0200)] 
depmod: Fix typo in comment

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/158
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: enable sanitizers in the meson build
Emil Velikov [Sat, 28 Sep 2024 13:12:26 +0000 (14:12 +0100)] 
ci: enable sanitizers in the meson build

... to prevent a range of issues creeping in.

Currently ASAN reports an ordering issue that we need to explicitly
disable. Thus the sanitizers are only enabled in CI and not for all
developer builds (aka build-dev.ini).

Seemingly musl lacks the sanitizers, so we don't enable it on Alpine.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/160
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshared: Fix EOF handling in freadline_wrapped
Tobias Stoeckmann [Fri, 27 Sep 2024 19:52:27 +0000 (21:52 +0200)] 
shared: Fix EOF handling in freadline_wrapped

If a file ends with a backslash without a newline, freadline_wrapped
adds EOF to the actual string.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/159
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: Fix test-weakdep with autoconf defaults
Tobias Stoeckmann [Fri, 27 Sep 2024 19:26:40 +0000 (21:26 +0200)] 
testsuite: Fix test-weakdep with autoconf defaults

If ./configure is run without any further options, then /usr/etc
is used as SYSCONFDIR, which breaks test-weakdep, because the
/etc path is hardcoded in it.

Use SYSCONFDIR to reflect the actual rootfs setup.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/157
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: Adjust README
Tobias Stoeckmann [Fri, 27 Sep 2024 19:25:20 +0000 (21:25 +0200)] 
testsuite: Adjust README

The populate-modules.sh does no longer exist. Also mention that the
script setup-rootfs.sh is in use for test suite setups.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/157
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agomodinfo: Avoid unneeded system call
Tobias Stoeckmann [Tue, 24 Sep 2024 20:37:26 +0000 (22:37 +0200)] 
modinfo: Avoid unneeded system call

If a file name does not end with ".ko", there is no need to call fstat.
Since common use cases are like "modinfo modname", the string check
should come first to save a tiny amount of system time.

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/154
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoman: Fix typo
Tobias Stoeckmann [Thu, 26 Sep 2024 22:36:22 +0000 (00:36 +0200)] 
man: Fix typo

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/156
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Plug memory leak on error path
Tobias Stoeckmann [Tue, 24 Sep 2024 19:30:13 +0000 (21:30 +0200)] 
libkmod: Plug memory leak on error path

Fix memory leak on error path introduced with read_u32 error checks.

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/153
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: add more SPDX copyright/license identifiers
Emil Velikov [Mon, 23 Sep 2024 20:51:05 +0000 (21:51 +0100)] 
ci: add more SPDX copyright/license identifiers

This concludes the files, I'm planning to reuse in usbutils so I'll stop
before it gets too annoying :-D

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/151
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Avoid OOB with huge ELF files
Tobias Stoeckmann [Mon, 23 Sep 2024 19:22:00 +0000 (21:22 +0200)] 
libkmod: Avoid OOB with huge ELF files

On 32 bit systems it is possible to trigger an out of boundary write
with excessively huge ELF files.

The calculation of required memory for char pointer vector and strings
might overflow, leading to an allocation which is too small. Subsequent
memcpy leads to an out of boundary write.

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/149
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Fix typo
Tobias Stoeckmann [Mon, 23 Sep 2024 18:32:53 +0000 (20:32 +0200)] 
libkmod: Fix typo

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/148
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Fix ELFDBG usage
Tobias Stoeckmann [Mon, 23 Sep 2024 17:57:15 +0000 (19:57 +0200)] 
libkmod: Fix ELFDBG usage

The first parameter has to be a kmod_elf.

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/148
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: add clang-format action
Emil Velikov [Sun, 8 Sep 2024 10:38:13 +0000 (11:38 +0100)] 
ci: add clang-format action

To keep the style consistent.

Closes: https://github.com/kmod-project/kmod/issues/88
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/118
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools: mass convert with clang-format
Emil Velikov [Wed, 11 Sep 2024 16:30:40 +0000 (17:30 +0100)] 
tools: mass convert with clang-format

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/118
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: mass convert with clang-format
Emil Velikov [Wed, 11 Sep 2024 16:30:32 +0000 (17:30 +0100)] 
testsuite: mass convert with clang-format

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/118
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshared: mass convert with clang-format
Emil Velikov [Wed, 11 Sep 2024 16:30:21 +0000 (17:30 +0100)] 
shared: mass convert with clang-format

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/118
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: mass convert with clang-format
Emil Velikov [Wed, 11 Sep 2024 16:30:13 +0000 (17:30 +0100)] 
libkmod: mass convert with clang-format

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/118
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: do monthly dependabot runs
Emil Velikov [Mon, 23 Sep 2024 20:40:53 +0000 (21:40 +0100)] 
ci: do monthly dependabot runs

The CodeQL action is getting 1-2 releases every week with only a handful
of changes in each.

This is getting a bit too much, so let's change to monthly checks.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/150
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Avoid misaligned memory accesses
Tobias Stoeckmann [Wed, 18 Sep 2024 19:47:48 +0000 (21:47 +0200)] 
libkmod: Avoid misaligned memory accesses

ASAN incorrectly reports this as unaligned access if compiler
optimizes code. Since ASAN support is valued higher than this
kind of optimization, just copy these few bytes into a stack
variable and avoid false positives in tests.

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/147
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: use bufferless zstd decompression
Emil Velikov [Thu, 19 Sep 2024 12:14:58 +0000 (13:14 +0100)] 
libkmod: use bufferless zstd decompression

Unlike the other two decompressions, zstd supports streaming bufferless
mode. Meaning we don't need to read and realloc in a loop.

Some strace numbers:

        $ strace -e read ./before/depmod -o /tmp/throaway | wc -l
        35265
        $ strace -e fstat ./before/depmod -o /tmp/throaway | wc -l
        1110

        $ strace -e read ./after/depmod -o /tmp/throaway | wc -l
        5677
        $ strace -e fstat ./after/depmod -o /tmp/throaway | wc -l
        6642

.. and valgrind total heap usage statistics:
        before:
        1,039,426 allocs, 1,039,426 frees, 3,679,232,922 bytes allocated

        after:
        1,020,643 allocs, 1,020,643 frees, 1,157,922,357 bytes allocated

The actual runtime is within the error margin.

v2:
 - use ZSTD_decompress(), which allocates ZSTD_DCtx internally

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/142
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools: Check snprintf return value
Tobias Stoeckmann [Wed, 14 Aug 2024 15:50:34 +0000 (17:50 +0200)] 
tools: Check snprintf return value

If an excessively large root directory is supplied, subsequent
operations might process unexpected parts in file system.

Inform user that supplied directory paths are too long and stop
program execution.

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/72
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: refactor builtin module handling
Tobias Stoeckmann [Tue, 17 Sep 2024 17:59:39 +0000 (19:59 +0200)] 
libkmod: refactor builtin module handling

Remove arbitrary limits due to file sizes (INTPR_MAX check). Reduce
amount of system calls by up to 90 % utilizing stream functions.

Also make sure that no TOCTOU could ever happen by not iterating
through the file twice: First to figure out amount of strings, then
parsing them. If the file changes in between, this can lead to
memory corruption.

Even though more memory allocations might occur due to strbuf usage,
performance generally increased by heavy reduction of system 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/136
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: add builtin test for modinfo
Tobias Stoeckmann [Tue, 17 Sep 2024 17:58:53 +0000 (19:58 +0200)] 
testsuite: add builtin test for modinfo

The modinfo command can show information about builtin modules. Make
sure that it functions correctly.

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/136
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: sort modnames only, if available
Emil Velikov [Thu, 19 Sep 2024 14:50:21 +0000 (15:50 +0100)] 
testsuite: sort modnames only, if available

In some tests we expect zero modules to be loaded. In those cases, skip
the sorting - qsort is annotated as non-null(1,2) so we shouldn't
provide null as expected/loaded modules.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/144
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: add SPDX copyright/license identifiers
Emil Velikov [Sun, 22 Sep 2024 11:52:42 +0000 (12:52 +0100)] 
ci: add SPDX copyright/license identifiers

To make it clear and explicit on the topic. Inspired by me reusing some
of those for usbutils and reuse-tool [1] flagging the lack of proper
attribution.

[1] https://github.com/fsfe/reuse-tool

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/145
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshell-completion/*/insmod: add bash/fish/zsh completion
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
shell-completion/*/insmod: add bash/fish/zsh completion

A few inline todos and some odd fish behaviour as mentioned inline.
Otherwise things just work :-)

v2:
 - use e(x)clusive answers for fish, tweak force string

v3:
 - wire the completions to the autotools build

v4:
 - use SPDX style copyright statements

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshell-completion/*/rmmod: add bash/fish/zsh completion
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
shell-completion/*/rmmod: add bash/fish/zsh completion

v2:
 - use e(x)clusive answers for fish, tweak force string

v3:
 - wire the completions to the autotools build

v4:
 - use SPDX style copyright statements

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshell-completion/*/lsmod: add bash/fish/zsh completion
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
shell-completion/*/lsmod: add bash/fish/zsh completion

Note that completions are explicitly aimed to be simple, depending on as
little as possible shell specific helpers.

The goal is that people unfamiliar with these can extend them with zero
ramp-up. Doing things efficiently or "properly" is somewhat secondary.

v2:
 - wire the completions to the autotools build

v3:
 - use SPDX style copyright statements

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: remove __secure_getenv handling
Emil Velikov [Wed, 18 Sep 2024 16:32:31 +0000 (17:32 +0100)] 
libkmod: remove __secure_getenv handling

The build-time checking for __secure_getenv was dropped earlier, yet I
forgot to remove the actual users :facepalm:

Fixes: 9dc54a3 ("build: stop checking for __secure_getenv")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/139
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite/list: resolve memory leaks
Emil Velikov [Thu, 19 Sep 2024 14:30:26 +0000 (15:30 +0100)] 
testsuite/list: resolve memory leaks

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/143
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/insmod: add --force longopt and document it
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/insmod: add --force longopt and document it

As of the initial import to git, the option was silently ignored.
Shortly afterwards, functionality was reinstated lacking a longopt and
any documentation.

As per insmod(8) for most use-cases you'd want to use modprobe(8).
Although since the option is available and we have an equivalent in
rmmod(8) having it documented and consistent triumphs.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/insmod: add syslog and verbose options
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/insmod: add syslog and verbose options

Add the extra options for consistency with the rest of kmod.

Document all options in the man page.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/insmod: remove unused -p -s shortopts
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/insmod: remove unused -p -s shortopts

These have been around since the import to git, doing nothing. There are
no users that I can see, the manual page does not list them and the
shell completions (fish and bash lack any for insmod, zsh has one) don't
suggest it either.

Remove them.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/rmmod: return EXIT_FAILURE if kmod_module_new_* fails
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/rmmod: return EXIT_FAILURE if kmod_module_new_* fails

In the unlikely case where kmod_module_new_from_{path,name} fails,
rmmod will return success. Change that to EXIT_FAILURE.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/rmmod: consistently use ERR logging facility
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/rmmod: consistently use ERR logging facility

Currently we'll print "Module foo is in use by:" to syslog, while the
modules themselves will end up in strerr.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/rmmod: make opt variables non-global
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/rmmod: make opt variables non-global

They're used locally within one function so declare them there.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoman/rmmod: reorder, add --help
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
man/rmmod: reorder, add --help

Stay consistent with the output of rmmod --help and update the
respective options.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/rmmod: tweak --force help message
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/rmmod: tweak --force help message

Drop the kernel configuration details - the CONFIG toggle name is more
useful (since you can see it in /proc/config.*) and generally the part
is better suited for the manual page.

Move DANGEROUS at the front, so it's obvious from the start

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/rmmod: remove unused -w shortopt
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/rmmod: remove unused -w shortopt

The (long)option and handling was removed some 10 years ago, yet the
shortopt remained... oops

Fixes: a4bd144 ("Remove "rmmod -w" documentation and getopt entry")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools/lsmod: add basic opts like rmmod
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools/lsmod: add basic opts like rmmod

This allows for more consistent experience across tools, allows to see
the version, observe/debug if "Used by" reports -1, etc.

Also don't forget to update the man page :-)

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotools: add kmod_version() helper
Emil Velikov [Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)] 
tools: add kmod_version() helper

Add a trivial helper that prints the version + features combo. I will be
adding another instance of those, so I'm aiming to keep the boilerplate
code to a minimum.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: Fix init_module
Tobias Stoeckmann [Wed, 18 Sep 2024 19:53:23 +0000 (21:53 +0200)] 
testsuite: Fix init_module

Apply same logic as in delete_module.c, i.e. pass a reference to a
pointer instead of the pointer value, otherwise we cannot update
the linked list and added entries are lost.

Spotted with ASAN while running testsuite.

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/141
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Fix OOB write with illegal index files
Tobias Stoeckmann [Wed, 11 Sep 2024 15:51:53 +0000 (17:51 +0200)] 
libkmod: Fix OOB write with illegal index files

If an index file with INDEX_NODE_CHILDS flag contains illegal first
and last markers for children, it is possible to trigger an out of
boundary write.

Make sure that first value is not larger than last value while reading
index files.

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/126
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: annotate fatal_oom() as noreturn
Emil Velikov [Tue, 17 Sep 2024 09:50:45 +0000 (10:50 +0100)] 
depmod: annotate fatal_oom() as noreturn

... in case the compiler cannot deduce it from the exit(3) within.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/135
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshared: remove no longer used NOFAIL() macro
Emil Velikov [Tue, 17 Sep 2024 09:49:38 +0000 (10:49 +0100)] 
shared: remove no longer used NOFAIL() macro

Closes: https://github.com/kmod-project/kmod/issues/60
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/135
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Replace NOFAIL macro
Tobias Stoeckmann [Fri, 13 Sep 2024 17:29:56 +0000 (19:29 +0200)] 
depmod: Replace NOFAIL macro

The NOFAIL macro was not implemented. Add a function which
gracefully exits depmod instead of letting it crash.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/130
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Check strdup return value
Tobias Stoeckmann [Tue, 10 Sep 2024 18:06:11 +0000 (20:06 +0200)] 
depmod: Check strdup return value

If strdup fails, return error.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/130
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agodepmod: Handle malloc failure in index_create
Tobias Stoeckmann [Tue, 10 Sep 2024 16:18:48 +0000 (18:18 +0200)] 
depmod: Handle malloc failure in index_create

Callers already check error return value, so actually return one in case
of failure.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/130
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Improve st_size checks on 32 bit systems
Tobias Stoeckmann [Tue, 3 Sep 2024 18:28:26 +0000 (20:28 +0200)] 
libkmod: Improve st_size checks on 32 bit systems

Since off_t can (and most likely will) be 64 bit on 32 bit systems,
check its actual value before casting it to 32 bit size_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/96
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: rename read_long to read_u32
Tobias Stoeckmann [Wed, 11 Sep 2024 15:07:05 +0000 (17:07 +0200)] 
libkmod: rename read_long to read_u32

Same goes for mm version of the 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/123
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: Remove NOFAIL usage
Tobias Stoeckmann [Tue, 10 Sep 2024 16:53:40 +0000 (18:53 +0200)] 
libkmod: Remove NOFAIL usage

Forward errors to callers instead of relying on unimplemented macro.

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/123
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: handle read_char errors
Tobias Stoeckmann [Tue, 10 Sep 2024 16:13:41 +0000 (18:13 +0200)] 
libkmod: handle read_char errors

If read_char fails, stop operations and return an error 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/123
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoshared: Remove fatal macro
Tobias Stoeckmann [Tue, 10 Sep 2024 16:11:41 +0000 (18:11 +0200)] 
shared: Remove fatal macro

The fatal macro was never implemented. Its only user, libkmod-index,
should propagate error condition through errno as good as possible.

Other logging mechanisms are not available without adjusting API
because context is missing.

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/123
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: treat read_long failures
Tobias Stoeckmann [Tue, 10 Sep 2024 16:04:14 +0000 (18:04 +0200)] 
libkmod: treat read_long failures

If read_long fails, forward error up to caller. Also perform proper
fread return value check.

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/123
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agolibkmod: allow buf_freadchars to report errors
Tobias Stoeckmann [Tue, 10 Sep 2024 16:01:35 +0000 (18:01 +0200)] 
libkmod: allow buf_freadchars to report errors

The return value is currently not needed. In prepration for subsequent
commits, use the return value to report if errors occurred or not.

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/123
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agobuild: stop checking for __secure_getenv
Emil Velikov [Sun, 15 Sep 2024 00:40:37 +0000 (01:40 +0100)] 
build: stop checking for __secure_getenv

Currently we check for both __secure_getenv and secure_getenv. The
former was introduced with glibc 2.2.5 as internal(?) API, while the
has been part of glibc 2.17, circa 2012.

Drop the former check. It may have been required over 10 years ago, but
not any more.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agobuild: check for __xstat declarations
Emil Velikov [Sun, 15 Sep 2024 00:40:14 +0000 (01:40 +0100)] 
build: check for __xstat declarations

Currently we check the function is resolved at link time. Although what
we really care is if the headers are silently transposing any of our stat
calls to __xstat{,64}. If so, we'd want to wrap the latter functions.

As the now-removed comment says, glibc 2.33 was the first release to no
longer have static inline wrappers that do the transposition. See the
glibc commit 8ed005daf0ab ("Remove stat wrapper functions, move them to
exported symbols") for more details.

So change the checking to check_decl/has_header_symbol and keep them for
distributions with older glibc.

NOTE: to summarise/contrast this wrt the previous open64, etc
 - here: we had inline wrappers and declarations (as below)
 - others: no inline wrappers and optionally declarations

glibc 2.32
extern ... __xstat(); extern inline stat(...) { return __xstat(...); }

glibc 2.33
extern stat(...);

Note we group the 64 variant as well, since the codepath has been
identical (wrt core logic) to the normal __xstat().

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agomeson: use has_header_symbol for checking declarations
Emil Velikov [Sun, 15 Sep 2024 18:04:19 +0000 (19:04 +0100)] 
meson: use has_header_symbol for checking declarations

Currently we use has_function, which will construct local declaration
and see if the program links successfully. What we're really interested
is if the referenced header has a declaration of the respective symbol.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agobuild: check properly for __stat64_time64
Emil Velikov [Sun, 15 Sep 2024 10:03:00 +0000 (11:03 +0100)] 
build: check properly for __stat64_time64

Having learned from prior LFS64 experience the glibc developers have
implemented stat in (albeit varying but) neater way:

 - declaration with asm linkage to __stat64_time64
 - or, `#define stat __stat64_time64`
 - or, `#define stat stat64; #define stat64 __stat64_time64`

In all cases __stat64_time64 lacks an explicit declaration, unlike
open64, stat64, fopen64 mentioned earlier.

Since we lack declaration, we have no other option but to check if a
program with reference to __stat64_time64 can link, so we use the
check/has function.

For more details glibc commit aa03f722f3 ("linux: Add {f}stat{at} y2038
support") added internal.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite/path: s/__stat64_t64/void/
Emil Velikov [Sun, 15 Sep 2024 10:03:00 +0000 (11:03 +0100)] 
testsuite/path: s/__stat64_t64/void/

The exact struct varies across the build-options, but in practise is
never __stat64_t64 - this is the internal name used within glibc.

When the fstat declaration with __fstat64_time64 asm linkage is used, we
have "struct stat". Whenever the `#define stat __stat64_time64` kicks
in, both function and struct get redefined/renamed.

Since we don't care about it (apart from the pointer part) just use void.

For more details glibc commit aa03f722f3 ("linux: Add {f}stat{at} y2038
support") added internal.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agobuild: check for open64, stat64 and fopen64
Emil Velikov [Sun, 15 Sep 2024 09:11:49 +0000 (10:11 +0100)] 
build: check for open64, stat64 and fopen64

... and use behind the respective ifdef HAVE_FOO guards instead of the
HAVE_DECL___GLIBC__ currently.

Since day one, glibc has declarations for the functions, which was
forwarding the normal functions to them, via asm linkage, et al.
Aka `extern int open(....) asm_linkage("open64").

In addition, a lot of libraries have grown to depend on the declarations
being available and functions being statically exposed via libc.so.

Whereas musl pre 1.2.4 (circa 2023) have exposed the symbols
statically, without a declaration for well over a decade. Newer musl,
no longer expose the symbol in their runtime but have retained the
define trick, stating it will be removed in the future.

Looking at the bionic front things are somewhat similar. Newer bionic
(circa 2019) have a declaration and an inline wrapper open64 function
forwarding to open. Throughout 2019, open64 did forward to misc other
internal functions thought.

Older pre 2019 bionic had a declaration alongside plug exposing the
symbol statically in their C runtime.... since 2014. Not sure what they
did prior to 2014, it's a target out of scope for us.

Considering the above, the most robust approach is to do a check/has
function checking.

With that, we no longer need the __GLIBC__ guard for the respective
functions.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: remove access() sub-test
Emil Velikov [Sat, 14 Sep 2024 14:08:59 +0000 (15:08 +0100)] 
testsuite: remove access() sub-test

Our test does access + stat, where stat is sufficient. In addition, the
kmod codebase never used access (afaict), so we're safe to remove the
access() part.

Alongside it we can drop the LD_PRELOAD wrapper.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agotestsuite: remove *lstat* wrappers
Emil Velikov [Sat, 14 Sep 2024 13:57:44 +0000 (14:57 +0100)] 
testsuite: remove *lstat* wrappers

The majority of these were initially introduced with commit 123e827
("testsuite preload: Factorize into macros, add more stat and open
variants").

The commit itself was meant to refactor existing wrappers and add new
_xstat (aka stat) variants. The lstat variants were never used in kmod
directly nor indirectly via header macros.

The rest were added to mimic the original, without much testing it
seems.

They are all dead code - remove them.

In theory one could have a macro/helper that calls `lstat` for `stat`
itself, although that isn't a practical solution for a few reasons.

The functionality across the two varies, where if the path provided is
a symlink `stat` follows it, while `lstat` gets the details for the link
itself. To fix this, the wrapper would need second syscall to resolve
the symlink, which will cause notable performance regression wrt using
using the `stat`/`stat64` syscall alone.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/131
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: add codeql code-scanning action
Emil Velikov [Sat, 14 Sep 2024 15:12:33 +0000 (16:12 +0100)] 
ci: add codeql code-scanning action

Effectively a trimmed down copy of main.yml, running only on
ubuntu-22.04 for now.

The action flags a handful of interesting issues and allows us to
prevent more from creeping in.

The action does not need need docs/manpages so we disable those.

v2:
 - script lives in .github/

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/132
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoscripts/print-kdir: error out on multiple kernels
Emil Velikov [Sat, 14 Sep 2024 15:48:19 +0000 (16:48 +0100)] 
scripts/print-kdir: error out on multiple kernels

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/132
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: flesh out print-kdir.sh script, bash ftw
Emil Velikov [Sat, 14 Sep 2024 15:46:27 +0000 (16:46 +0100)] 
ci: flesh out print-kdir.sh script, bash ftw

We'll be adding another action, where this script is handy. Flesh it out
to scripts and convert to bash.

v2:
 - move script to .github/

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/132
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
10 months agoci: don't abort all pipeline if one is failing
Emil Velikov [Sat, 14 Sep 2024 15:55:52 +0000 (16:55 +0100)] 
ci: don't abort all pipeline if one is failing

Our jobs are pretty quick sub 2m for meson builds and sub 1m, when if
going without tests.

Wait for all jobs so we get all the (potential) issues at once.

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