Commit Graph

633 Commits

Author SHA1 Message Date
Lucas De Marchi
d4f659e12a Drop the one line short description on sources
Some are outdated, misleading or just repeat the same thing over and
over. Remove them as they are not needed.

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://lore.kernel.org/r/20240723185921.1005569-3-lucas.de.marchi@gmail.com
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-07-26 13:41:56 -05:00
Lucas De Marchi
b5a2cd070d Use SPDX header for license
Drop the lengthy license from each file and just use SPDX like most
projects nowadays. This doesn't have any change to license, just how
they are recorded in each file.

This follows the kernel approach: header files use '/*' for comments
while .c files use '//'. For .m4, use "#".

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://lore.kernel.org/r/20240723185921.1005569-2-lucas.de.marchi@gmail.com
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-07-26 13:41:56 -05:00
Lucas De Marchi
d84631afc2 libkmod: Move zstd-related functions to separate file
Move zstd-related function to a separate file so it's easier to isolate
the dependency on each decompression library.

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/58
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-07-26 13:41:47 -05:00
Lucas De Marchi
24d78fed15 libkmod: Move zlib-related functions to separate file
Move zlib-related function to a separate file so it's easier to isolate
the dependency on each decompression library.

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/58
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-07-26 13:41:24 -05:00
Lucas De Marchi
929ca4c92a libkmod: Move xz-related functions to separate file
Move xz-related function to a separate file so it's easier to isolate
the dependency on each decompression library.

Declare struct kmod_file in a shared libkmod-internal-file.h that will
be included only by sources implementing kmod_file decompression
routines.

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/58
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-07-26 13:41:13 -05:00
Emil Velikov
7d72b22385 libkmod: move new weak API to separate section
Add the new weak API, to a separate section in the version script. These
will be exposed with the upcoming v33 release and not with v5 as the
symbol tag implies.

Cc: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Fixes: 05828b4 ("libkmod: add weak dependecies")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://lore.kernel.org/r/20240719-abi-fixes-v1-3-1e6d99a2846b@gmail.com
2024-07-20 09:11:01 -05:00
Emil Velikov
a5b7ac3ee1 libkmod: correctly tag kmod_module_new_from_name_lookup
The symbol was introduced with v30, yet was erroneously added in the v5
section. Move it to the correct place.

In theory this might cause an issue - severity depends on how the
runtime linker is setup. From a harmless warning (on stderr/stdout) to
failure to load the library.

In practise this shouldn't be a problem, since there are seemingly no
external users of the API.

Fixes: 9becaae ("libkmod: Add lookup from module name")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://lore.kernel.org/r/20240719-abi-fixes-v1-2-1e6d99a2846b@gmail.com
2024-07-20 09:11:01 -05:00
Emil Velikov
89596b7518 libkmod: document KMOD_INDEX_MODULES_BUILTIN_ALIAS in kmod_dump_index()
The extra enum was introduced a few years ago, although the (only)
function using it did not have it's documentation updated.

The commit itself has caused an ABI break with kmod v27, since the
KMOD_INDEX_MODULES_BUILTIN value has changed.

A search through Google, Github and Arch packages have shown one user of
kmod_dump_index() - the only API that uses the enum.

In that case and all others, no projects referencing the changed enum
were found. Although since recent and/or supported distros use kmod v27
or later we've decided to only update the documentation.

Cc: Alexey Gladkov <gladkov.alexey@gmail.com>
Fixes: b866b21 ("Lookup aliases in the modules.builtin.modinfo")
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://lore.kernel.org/r/20240719-abi-fixes-v1-1-1e6d99a2846b@gmail.com
2024-07-20 09:11:01 -05:00
Emil Velikov
29cc8c2e70 kmod: remove .alias config files for modprobe.d
The use of .alias (alongside .conf) was added for compatibility with the
original module-init-tools project and has been living in kmod ever
since.

In practise, all the linux distributions that I can see are using .conf
files alone, as instructed by modprobe.d(5) and the only instance of an
.alias file is the modules.alias as shipped in the kernel.

The latter is already handled by other parts of the kmod project, so
let's enforce what our documentation says.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://lore.kernel.org/r/20240717-rm-alias-v1-1-58874caf343a@gmail.com
2024-07-20 09:10:28 -05:00
q66
8da7c1e088 libkmod: improve realloc behavior for zstd outbuffer
The allocator in glibc has a particular quirk that successive
reallocs on the same pointer are cheap, at the cost of excess
memory fragmentation. Other allocators generally do not do this,
so excessive reallocs become relatively expensive.

Reducing the number of reallocations by using a more agressive
strategy for buffer size increase makes performance better on
those setups, e.g. musl libc, or generally any other allocator;
on my Chimera Linux setup with Scudo allocator (LLVM) it doubles
to triples the performance of running e.g. depmod.

Signed-off-by: q66 <q66@chimera-linux.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-07-12 07:58:13 -05:00
Jose Ignacio Tornos Martinez
05828b4a6e libkmod: add weak dependecies
It has been seen that for some network mac drivers (i.e. lan78xx) the
related module for the phy is loaded dynamically depending on the current
hardware. In this case, the associated phy is read using mdio bus and then
the associated phy module is loaded during runtime (kernel function
phy_request_driver_module). However, no software dependency is defined, so
the user tools will no be able to get this dependency. For example, if
dracut is used and the hardware is present, lan78xx will be included but no
phy module will be added, and in the next restart the device will not work
from boot because no related phy will be found during initramfs stage.

In order to solve this, we could define a normal 'pre' software dependency
in lan78xx module with all the possible phy modules (there may be some),
but proceeding in that way, all the possible phy modules would be loaded
while only one is necessary.

The idea is to create a new type of dependency, that we are going to call
'weak' to be used only by the user tools that need to detect this situation.
In that way, for example, dracut could check the 'weak' dependency of the
modules involved in order to install these dependencies in initramfs too.
That is, for the commented lan78xx module, defining the 'weak' dependency
with the possible phy modules list, only the necessary phy would be loaded
on demand keeping the same behavior, but all the possible phy modules would
be available from initramfs.

A new function 'kmod_module_get_weakdeps' in libkmod will be added for
this to avoid breaking the API and maintain backward compatibility. This
general procedure could be useful for other similar cases (not only for
dynamic phy loading).

Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
Link: https://lore.kernel.org/r/20240327141116.97587-1-jtornosm@redhat.com
2024-05-08 23:37:38 -05:00
Emil Velikov
5a8b16b718 libkmod: keep KMOD_FILE_COMPRESSION_NONE/load_reg in comp_types
It's cleaner to handle all compression types and load functions in the
same style.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 15:35:37 -05:00
Emil Velikov
045fd571c4 libkmod: move load_reg() further up
We're about to reference it in comp_types with next commit.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 15:34:28 -05:00
Emil Velikov
61bf8e74b9 libkmod: tidy-up kmod_file_open()
This commit cleans up the indentation and the error path of the
function. It bears no functional changes.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
[ Move assert to avoid warning with -Wdeclaration-after-statement ]
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 15:33:55 -05:00
Emil Velikov
737744301a libkmod: swap alloca usage for a few assert_cc
Since all the compression magic is always available now, we don't need
to loop at runtime nor use alloca - latter of which comes with a handful
of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
0c12738807 libkmod: always detect the module compression
Currently, when built w/o given compression we'll incorrectly report a
"compression_none".

As we reach do_finit_module(), we'll naively assume that the kernel can
handle the compressed module, yet omit the MODULE_INIT_COMPRESSED_FILE
flag.

As result the kernel will barf at us, do_finit_module will fail with non
-ENOSYS and we won't end in the do_init_module codepath (which will also
fail).

In other words: with this change, you can build kmod without zstd, xz
and zlib support and the kernel will load the modules, assuming it
supports the format \o/

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
8cc475f7da libkmod: move kmod_file_load_contents as applicable
When dealing with an elf, we don't know or care about loading the file.
The kmod_elf subsystem/API will deal with the required parts itself.

Which in this case, already calls kmod_file_load_contents() as
applicable.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
81e5c797d0 libkmod: propagate {zstd,xz,zlib}_load errors
Propagate any errors during decompression further up the call stack.
Without this we could easily pass NULL as mem to init_module(2).

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
ad15892394 libkmod: nuke struct file_ops
With the previous commits, we removed the need for a distinct unload
callback.

So nuke the struct all together and only use/keep the load one around.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
90b271fbd2 libkmod: clear file->memory if map fails
On mmap failure file->memory is set to -1, which we'll happily pass down
to munmap later on.

More importantly, since we do a NULL check in kmod_file_load_contents()
we will exit the function without (re)attempting the load again.

Since we ignore the return code for the load function(s), one can end up
calling kmod_elf_get_memory() and feed that -1 into init_module.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
03da2db135 libkmod: remove kmod_file::{zstd,xz}_used flags
These are used to protect a free(file->memory), within their respective
unload functions. Where the sole caller of the unload function already
does a NULL check prior.

Even so, free(NULL) is guaranteed to be safe by the standard.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
09256b9a4f libkmod: keep gzFile gzf local to load_zlib()
There is no need to keep the root gzFile context open for the whole
duration. Once we've copied the decompressed module to file->memory we
can close the handle.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Emil Velikov
d6cd6c74d2 libkmod: use a dup()'d fd for zlib
The gzdopen() API used, takes ownership of the fd. To make that more
explicit we clear it (-1) as applicable.

Yet again, kmod has explicit API to return the fd to the user - which
currently is used solely when uncompressed, so we're safe.

Regardless - simply duplicate the fd locally and use that with zlib.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-04-30 12:33:52 -05:00
Lucas De Marchi
594f102cf5 Drop python bindings
Python bindings are not well maintained. Currently it's just broken when
trying to build with cython 3.0.8:

	make --no-print-directory all-recursive
	Making all in .
	  CYTHON  libkmod/python/kmod/kmod.c

	Error compiling Cython file:
	------------------------------------------------------------
	...
	# details.
	#
	# You should have received a copy of the GNU Lesser General Public License
	# along with python-kmod.  If not, see <http://www.gnu.org/licenses/>.

	cimport _libkmod_h
		^

Nothing really touched those bindings for 10 years already.
I postponed the removal since they were at least building, but that just
changed. So let's drop it and allow any interested people to give it
a better life outside of libkmod.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-02-02 12:54:04 -06:00
Michal Suchanek
9d1fb317c8 libkmod, depmod, modprobe: Make directory for kernel modules configurable
Now that modprobe.d is searched under ${prefix}/lib, allow a complete
transition to files only under ${prefix} by adding a ${module_directory}
configuration. This specifies the directory where to search for kernel
modules and should match the location where the kernel/distro installs
them.

With this distributions that do not want to ship files in /lib can also
move kernel modules to /usr while others can keep them in /lib.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
Link: https://lore.kernel.org/r/a3765f4e8ae3ce29c0847a0132d4a8d51ad040a9.1699618135.git.msuchanek@suse.de
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-12-06 09:53:05 -06:00
Dimitri John Ledkov
510c8b7f74 libkmod: remove pkcs7 obj_to_hash_algo()
Switch to using OBJ_obj2txt() to calculate and print the pkcs7
signature hash name. This eliminates the need to duplicate libcrypto
NID to name mapping, detect SM3 openssl compile-time support, and
enables using any hashes that openssl and kernel know about. For
example SHA3 are being added for v6.7 and with this patch are
automatically supported.

Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
Link: https://lore.kernel.org/r/20231029010319.157390-1-dimitri.ledkov@canonical.com
2023-11-07 14:05:44 -06:00
Michal Suchanek
8463809f8a libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib.
There is an ongoing effort to limit use of files outside of /usr (or
${prefix} on general). Currently all modprobe.d paths are hardcoded to
outside of $prefix. Teach kmod to load modprobe.d from ${prefix}/lib.

On some distributions /usr/lib and /lib are the same directory because
of a compatibility symlink, and it is possible to craft configuration
files with sideeffects that would behave differently when loaded twice.
However, the override semantic ensures that one 'overrides' the other,
and only one configuration file of the same name is loaded from any of
the search directories.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
Link: https://lore.kernel.org/r/a290343ce32e2a3c25b134e4f27c13b26e06c9e0.1689681454.git.msuchanek@suse.de
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-10-17 12:27:59 -05:00
Lucas De Marchi
09c9f8c5df libkmod: Use kernel decompression when available
With the recent changes to bypass loading the file it's possible to
reduce the work in userspace and delegating it to the kernel. Without
any compression to illustrate:

Before:
	read(3, "\177ELF\2\1", 6)               = 6
	lseek(3, 0, SEEK_SET)                   = 0
	newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=238592, ...}, AT_EMPTY_PATH) = 0
	mmap(NULL, 238592, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd85cbd1000
	finit_module(3, "", 0)                  = 0
	munmap(0x7fd85cbd1000, 238592)          = 0
	close(3)                                = 0

After:
	read(3, "\177ELF\2\1", 6)               = 6
	lseek(3, 0, SEEK_SET)                   = 0
	finit_module(3, "", 0)                  = 0
	close(3)                                = 0

When using kernel compression now it's also possible to direct libkmod
to take the finit_module() path, avoiding the decompression in userspace
and just delegating it to the kernel.

Before:
	read(3, "(\265/\375\244\0", 6)          = 6
	lseek(3, 0, SEEK_SET)                   = 0
	read(3, "(\265/\375\244", 5)            = 5
	mmap(NULL, 135168, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f3fa431e000
	read(3, "\0\244\3\0\\y\6", 7)           = 7
	mmap(NULL, 372736, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f3fa414f000
	brk(0x55944c6a1000)                     = 0x55944c6a1000
	read(3, "\356|\6G\27U\20 \312\260s\211\335\333\263\326\330\336\273O\211\356\306K\360Z\341\374U6\342\221"..., 53038) = 53038
	mremap(0x7f3fa431e000, 135168, 266240, MREMAP_MAYMOVE) = 0x7f3fa410e000
	read(3, ",;\3\nqf\311\362\325\211\7\341\375A\355\221\371L\\\5\7\375 \32\246<(\258=K\304"..., 20851) = 20851
	mremap(0x7f3fa410e000, 266240, 397312, MREMAP_MAYMOVE) = 0x7f3fa40ad000
	read(3, ")\36\250\213", 4)              = 4
	read(3, "", 4)                          = 0
	munmap(0x7f3fa414f000, 372736)          = 0
	init_module(0x7f3fa40ad010, 238592, "") = 0
	munmap(0x7f3fa40ad000, 397312)          = 0
	close(3)                                = 0

After:
	read(3, "(\265/\375\244P", 6)           = 6
	lseek(3, 0, SEEK_SET)                   = 0
	finit_module(3, "", 0x4 /* MODULE_INIT_??? */) = 0
	close(3)                                = 0

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-06-20 14:39:25 -07:00
Lucas De Marchi
30077bf171 libkmod: Keep track of in-kernel compression support
When creating the context, read /sys/kernel/compression to check what's
the compression type supported by the kernel. This will later be used
when loading modules to check if the decompression step has to happen in
userspace or if it can be delegated to the kernel.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-06-09 10:45:55 -07:00
Lucas De Marchi
e539827635 libkmod: Keep track of compression type
Do not only set the type as direct, but also keep track of the
compression being used. This will allow using the in-kernel compression
in future.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-06-09 10:45:55 -07:00
Lucas De Marchi
e1f0e169de libkmod: Extract finit_module vs init_module paths
Extract 2 functions to handle finit_module vs init_modules differences,
with a fallback from the former to the latter.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-06-09 10:45:55 -07:00
Lucas De Marchi
7a86f12920 libkmod: Do not inititialize file->memory on open
Add a separate function to load the file contents when it's needed.
When it's not needed on the path of loading modules via finit_module(),
there is no need to mmap the file. This will help support loading
modules with the in-kernel compression support.

This is done differently than the lazy initialization for
kmod_file_get_elf() because on the contents case there is also the
file->size to be updated. It would be a weird API to return the pointer
and have the size changed as a side-effect.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-06-09 10:45:51 -07:00
Dmitry Antipov
badacf76e4 libkmod: fix possible out-of-bounds memory access
An attempt to pass too long module name to, say, rmmod, may
cause an out-of-bounds memory access (as repoted by UBSan):

$ rmmod $(for i in $(seq 0 4200); do echo -ne x; done)
libkmod/libkmod-module.c:1828:8: runtime error: index 4107 out of bounds for type 'char [4096]'

This is because 'snprintf(path, sizeof(path), ...)' may return the
value which exceeds 'sizeof(path)' (which happens when an output
gets truncated). To play it safe, such a suspicious output is
better to be rejected explicitly.

Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Link: https://lore.kernel.org/r/20230519074638.402045-1-dmantipov@yandex.ru
2023-05-30 12:56:54 -07:00
Dmitry Antipov
5c004af29d libkmod, depmod: prefer -ENODATA over -ENOENT if no section found
When the module is definitely present but CONFIG_MODVERSIONS is
disabled, the following error message may be somewhat confusing:

modprobe --dump-modversions /path/to/module.ko.xz
modprobe: FATAL: could not get modversions of /path/to/module.ko.xz: No such file or directory

Choosing among the convenient errno values, I would suggest to use
ENODATA when the module lacks a particular ELF section (and vermagic
as well). So now it is expected to be:

modprobe: FATAL: could not get modversions of /path/to/module.ko.xz: No data available

Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Link: https://lore.kernel.org/r/20230519093630.474185-1-dmantipov@yandex.ru
2023-05-30 12:54:12 -07:00
Emil Velikov
df9d07a149 libkmod: annotate kmod_builtin_iter API as static
It's no longer used outside the compilation unit, as of last commit.

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-02-21 16:35:37 -08:00
Emil Velikov
0237665bef libkmod: remove unused kmod_module_get_builtin
The last and only user was removed with commit 0246e06 ("depmod: Stop
opening modules.modinfo once per module")

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-02-21 16:35:14 -08:00
Emil Velikov
b9605c63b8 libkmod: error out on unknown hash algorithm
Currently if we see unknown algorithm, we'll do an OOB read in
pkey_hash_algo. This can happen for example if OPENSSL_NO_SM3 is set and
the kernel module uses a SM3 hash.

Cc: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-02-09 12:08:14 -08:00
Emil Velikov
efc2e4b467 treewide: add some static const notations
Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2023-02-09 07:15:56 -08:00
Mikhail Novosyolov
d5950b0b5e libkmod: do not crash on unknown signature algorithm
Example kernel module:
https://file-store.rosalinux.ru/download/7281f97e0c04c0f818ad3f936706f4a407e8dc7e
(/lib/modules/5.15.67-generic-1rosa2021.1-x86_64/kernel/drivers/usb/host/xhci-pci.ko.zst)
It is signed with Streebog 512.

libkmod v30 crashed in libkmod-module.c:2413 in this code:

n = kmod_module_info_append(list,
	"sig_hashalgo", strlen("sig_hashalgo"),
	sig_info.hash_algo, strlen(sig_info.hash_algo));

because strlen() got null.
2022-10-03 06:59:41 -07:00
HuaxinLu
f609cb5185 libkmod: Support SM3 hash algorithm
SM3 has been supported in kernel and cryptographic libraries like openssl.
This patch adds support for the SM3 algorithm of kmod.

Signed-off-by: HuaxinLu <luhuaxin1@huawei.com>
2022-06-30 07:44:32 -07:00
Lucas De Marchi
3a92fc6309 libkmod: Allow to ignore log message on module removal
Caller may want to handle retries, in which case the log message is not
appropriate.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2022-06-26 23:23:46 -07:00
Lucas De Marchi
205827426d docs: Add missing functions to documentation
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
2022-03-03 00:57:10 -08:00
Lucas De Marchi
571a84c924 libkmod: Fix use of sizeof instead of ARRAY_SIZE
Link: https://github.com/kmod-project/kmod/issues/12
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
2022-02-23 10:21:06 -08:00
Lucas De Marchi
9becaaea25 libkmod: Add lookup from module name
Slightly different than kmod_module_new_from_lookup(): it doesn't
consider aliases, only module names. This is useful for cases we want to
force a tool to handle something as the module name, without trying to
interpret it as an alias.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
2022-02-20 20:58:11 -08:00
Lucas De Marchi
a859220483 libkmod: Update docs about indexes order
New indexes were created without updating the documentation about the
order in kmod_module_new_from_lookup(). Add them to the documentation.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
2022-02-20 20:58:11 -08:00
Lucas De Marchi
8297271019 libkmod: Add helper function to iterate lookup options
The CHECK_ERR_AND_FINISH macro with conditional code flow changes has
been a source of bugs. Get rid of it replacing with a helper function
to iterate an array of lookup functions. This helper may also be useful
in future to create different lookup APIs in libkmod.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
2022-02-20 20:58:11 -08:00
Lucas De Marchi
ec8818bcfb libkmod-module: Fix return code for kmod_module_new_from_lookup()
When kmod_module_new_from_lookup() resolves to an alias, `err` will be
set to a positive value from the lookup function. Do not return a
positive value to follow the behavior when it matches a module name
and the documentation.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
2022-02-20 20:58:11 -08:00
Lucas De Marchi
a965641625 libkmod: Prefer builtin index over builtin.alias
The modules.builtin.alias.bin is way larger than the
modules.builtin.bin.  On a normal "distro kernel":

	21k modules.builtin.alias.bin
	11k modules.builtin.bin

From the kernel we get both modules.builtin and modules.builtin.modinfo.
depmod generates modules.builtin.bin and modules.builtin.alias.bin
from them respectively. modules.bultin is not going away: it's not
deprecated by the new index added. So, let's just stop duplicating the
information inside modules.builtin.alias.bin and just use the other
index.
2022-02-11 22:06:33 -08:00
Michal Suchanek
4e391ac92d libkmod: Set builtin to no when module is created from path.
A recent bug report showed that modinfo doesn't give the signature
information for certain modules, and it turned out to happen only on
the modules that are built-in on the running kernel; then modinfo
skips the signature check, as if the target module file never exists.
The behavior is, however, inconsistent when modinfo is performed for
external modules (no matter which kernel version is) and the module
file path is explicitly given by a command-line argument, which
guarantees the presence of the module file itself.

Fixes: e7e2cb61fa ("modinfo: Show information about built-in modules")
Link: https://lore.kernel.org/linux-modules/CAKi4VAJVvY3=JdSZm-GD1hJqyCPYaYz-jBJ_REeY5BakVb6_ww@mail.gmail.com/
BugLink: https://bugzilla.opensuse.org/show_bug.cgi?id=1189537
Suggested-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Signed-off-by: Michal Suchanek <msuchanek@suse.de>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
2022-01-20 00:46:13 -08:00
Luis Chamberlain
43bdf97ce1 libkmod: add a library notice log level print
When you use pass the -v argument to modprobe we bump
the log level from the default modprobe log level of
LOG_WARNING (4) to LOG_NOTICE (5), however the library
only has avaiable to print:

 #define DBG(ctx, arg...) kmod_log_cond(ctx, LOG_DEBUG, ## arg)
 #define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ## arg)
 #define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ## arg)

LOG_INFO (6) however is too high of a level for it to be
effective at printing anything when modprobe -v is passed.
And so the only way in which modprobe -v can trigger the
library to print a verbose message is to use ERR() but that
always prints something and we don't want that in some
situations.

We need to add a new log level macro which uses LOG_NOTICE (5)
for a "normal but significant condition" which users and developers
can use to look underneath the hood to confirm if a situation is
happening.

Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
2021-09-23 00:59:38 -07:00