Commit Graph

599 Commits

Author SHA1 Message Date
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
Yauheni Kaliuta 64541d6aad libkmod-module: check "new_from_name" return value in get_builtin
kmod_module_new_from_name() may fail and return error value. It is
handled properly across the code, but in this particular place the
check is missing.

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2021-06-09 10:21:15 -07:00
Lucas De Marchi 729f0f6853 Fix return value doc for kmod_module_get_info()
We don't return 0 on success, we return the number of elements we added
to the list.
2021-05-11 09:47:09 -07:00
Seung-Woo Kim 675410c3b3 libkmod: fix possible double free with wrong modules.builtin.modinfo
Fix double free for *modinfo with non '\0' terminated wrong
modules.builtin.modinfo, which is because EOF is minus value.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
2021-04-17 13:41:42 -07:00
Seung-Woo Kim 1cab02ecf6 libkmod: fix an overflow with wrong modules.builtin.modinfo
Fix a possbile overflow with exact PATH_MAX length modname
in wrong modules.builtin.modinfo.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
2021-04-17 13:41:42 -07:00
Seung-Woo Kim 39dd171623 libkmod-config: fix a memory leak when kmod_list_append() fails
From kmod_config_new(), when kmod_list_append() fails,
fix not list-appended kmod_config_path leak.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
2021-04-09 08:12:01 -07:00
Lucas De Marchi 8742be0aa5 libkmod-config: more chars allowed as space
Recently in a discussion I noticed that kernel allows more chars to be
considered as space in the kernel command line. Switch to the equivalent
of isspace() instead of considering only ' '.
2021-03-10 08:33:01 -08:00
Lucas De Marchi d3a1fe67b6 libkmod-config: re-quote option from kernel cmdline
It was reported that grub mangles the kernel cmdline. It turns

	acpi_cpufreq.dyndbg="file drivers/cpufreq/acpi-cpufreq.c +mpf"

	into

	"acpi_cpufreq.dyndbg=file drivers/cpufreq/acpi-cpufreq.c +mpf"

However, even though we could blame grub for doing that, the kernel
happily accepts and re-quotes it when the module is built-in.
So, it's better if kmod also understands it this way and does the same.

Here we basically add additional code to un-mangle it, moving the quote
in way that is acceptable to pass through init_module(). Note that the
interface [f]init_module() gives us mandates the quote to be part of the
value: the module name is not passed and the options are separated by
space.

Reported-by: Jiri Slaby <jirislaby@kernel.org>
Tested-by: Jessica Yu <jeyu@kernel.org>
Link: https://bugzilla.suse.com/show_bug.cgi?id=1181111#c10
2021-02-15 11:53:43 -08:00
Lucas De Marchi 01ed9af61e libkmod-config: revamp kcmdline parsing into a state machine
The handling of spaces and quotes is becoming hard to maintain. Convert
the parser into a state machine so we can check all the states. This
should make it easier to fix a corner case we have right now:
The kernel also accepts a quote before the module name instead of the
value. But this additional is left for later. This is purely an
algorithm change with no behavior change.

Tested-by: Jessica Yu <jeyu@kernel.org>
2021-02-15 11:53:38 -08:00
Lucas De Marchi 9319b0f4cb Support /usr/local for configuration files
Add /usr/local to the search path for configuration files. These are
intended for local installs, provided /usr/local is given as prefix.
2021-01-18 18:26:36 -08:00
Lucas De Marchi 67e43bbe04 libkmod: Fix documentation on config precedence order
/etc is has higher priority than /run.
2021-01-18 18:26:36 -08:00
Yauheni Kaliuta 95ed3e7536 libkmod: kmod_log_null: qualify ctx argument as const
kmod_log_null() does not change ctx (does nothing).

Fix warnings

In file included from libkmod/libkmod-index.c:33:
libkmod/libkmod-index.c: In function ‘index_mm_open’:
libkmod/libkmod-index.c:757:6: warning: passing argument 1 of ‘kmod_log_null’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  757 |  DBG(ctx, "file=%s\n", filename);

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2020-12-01 00:44:17 -08:00
Yauheni Kaliuta 47807c4cfa libkmod: kmod_builtin_get_modinfo: free modinfo on error
The function allocates array but on building it if get_string()
fails it returns the error leaving the array allocated. The caller
does not care about it in error case either.

Free it to fix memory leak.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2020-12-01 00:44:17 -08:00
Torge Matthies 3821e1971e add Zstandard compression support
I changed the style of the hackargs variable in autogen.sh to multiline
because said line was becoming a bit long with the new --with-zstd arg
added.

A previous version of this patch has been running on my two Arch Linux
installations (with an accompanying mkinitcpio patch) for several months
over many kernel updates without any issues.
Any additional testing and/or patch review would of course be appreciated.

Signed-off-by: Torge Matthies <openglfreak@googlemail.com>
2020-09-10 21:55:01 -07:00
Lucas De Marchi d8d1d54051 libkmod: allow modules.alias.builtin to be optional 2020-03-23 12:37:51 -07:00
Lucas De Marchi 3bd7187ff5 libkmod: fix return error when opening index
When calling kmod_load_resources() we could end up getting a bogus
return value -ENOMEM due to several other reasons, like the index not
existing. Change index_mm_open() to propagate the failure reason so we
can take actions on it or return to the caller.
2020-03-23 12:37:40 -07:00
Lucas De Marchi 89443220e3 libkmod: simplify lookup when builtin.modinfo.bin file is missing
When we try to lookup a module and builtin.modinfo.bin is missing, we
would do the right thing because the caller was replacing the return
code with 0 (and the list was not modified).

Make it simpler by allowing the caller to check and differentiate the
errors between module not found and index not found.
2020-03-23 12:37:26 -07:00
Lucas De Marchi 0262531556 libkmod: ignore kcmdline option if we fail to parse modname
The error message is saying we are ignoring the option on the kernel
command line, so just do it.
2020-03-13 09:23:58 -07:00
Lucas De Marchi f975f6bfcc libkmod: reset was_space on second pass
The softdep config parser uses a 2-pass approach to use a single
allocation for all the softdep struct. However "was_space" variable
isn't reset between them. This can lead to a buffer overflow.

Reported-by: Jorge Lucangeli Obes <jorgelo@google.com>
Link: https://lore.kernel.org/linux-modules/CAKYuF5QhGCPCazHQjN-=kFc5kHs7Ok8WqmmGLo31CiOEN8TYdA@mail.gmail.com
2020-01-20 16:15:38 -03:00
Topi Miettinen 81dbf2bee6 libkmod-module: convert return value from system() to errno
Don't use exit status of a command directly as errno code, callers
will be confused.

Signed-off-by: Topi Miettinen <toiwoton@gmail.com>
2019-12-29 16:13:35 -08:00
Alexey Gladkov e7e2cb61fa modinfo: Show information about built-in modules
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
2019-12-18 16:57:14 -08:00
Alexey Gladkov b866b2165a Lookup aliases in the modules.builtin.modinfo
New modules.builtin.modinfo duplicates modules.builtin in the built-in
module name search. If it exists, then we can use this file, but if not,
then we need to fallback to the old file.

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
2019-12-18 16:56:58 -08:00
Alexey Gladkov 73eed2aba3 libkmod: Add function to get list of built-in modules
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
2019-12-18 16:56:36 -08:00
Alexey Gladkov 60084cf1cb libkmod: Add parser for modules.builtin.modinfo
The kernel since version v5.2-rc1 exports information about built-in
modules in the modules.builtin.modinfo. Information is stored in
the same format as in the separate modules (null-terminated string
array). The module name is a prefix for each line.

$ tr '\0' '\n' < modules.builtin.modinfo
ext4.softdep=pre: crc32c
ext4.license=GPL
ext4.description=Fourth Extended Filesystem
ext4.author=Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others
ext4.alias=fs-ext4
ext4.alias=ext3
ext4.alias=fs-ext3
ext4.alias=ext2
ext4.alias=fs-ext2
md_mod.alias=block-major-9-*
md_mod.alias=md
md_mod.description=MD RAID framework
md_mod.license=GPL
md_mod.parmtype=create_on_open:bool
md_mod.parmtype=start_dirty_degraded:int
...

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
2019-12-18 16:56:10 -08:00
Stefan Strogin 628677e066 libkmod-signature: use PKCS#7 instead of CMS
Linux uses either PKCS #7 or CMS for signing modules (see
scripts/sign-file.c). CMS is not supported by LibreSSL or older OpenSSL,
so PKCS #7 is used on systems with these libcrypto providers.

CMS and PKCS #7 formats are very similar. CMS is newer but is as much as
possible backward compatible with PKCS #7 [1]. PKCS #7 is supported in
the latest OpenSSL as well as CMS. The fields used for signing kernel
modules are supported both in PKCS #7 and CMS.

For now modinfo uses CMS with no alternative requiring OpenSSL 1.1.0 or
newer.

Use PKCS #7 for parsing module signature information, so that modinfo
could be used both with OpenSSL and LibreSSL.

[1] https://tools.ietf.org/html/rfc5652#section-1.1

Changes v1->v2:
- Don't use ifdefs for keeping redundant CMS code, just use PKCS #7 both
with OpenSSL and LibreSSL.

Signed-off-by: Stefan Strogin <steils@gentoo.org>
2019-05-28 15:22:18 -07:00
Yauheni Kaliuta 391b4714b4 libkmod-signature: implement pkcs7 parsing with openssl
The patch adds data fetching from the PKCS#7 certificate using
openssl library (which is used by scripts/sign-file.c in the linux
kernel to sign modules).

In general the certificate can contain many signatures, but since
kmod (modinfo) supports only one signature at the moment, only first
one is taken.

With the current sign-file.c certificate doesn't contain signer
key's fingerprint, so "serial number" is used for the key id.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2019-02-04 13:51:27 -08:00
Yauheni Kaliuta a11057201e signature: do not report wrong data for pkc#7 signature
when PKC#7 signing method is used the old structure doesn't contain
any useful data, but the data are encoded in the certificate.

The info getting/showing code is not aware of that at the moment and
since 0 is a valid constant, shows, for example, wrong "md4" for the
hash algo.

The patch splits the 2 mothods of gethering the info and reports
"unknown" for the algo.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2018-11-16 00:59:44 -08:00
Jan Engelhardt 867da6fcfb kmod: build: cure compiler warnings showing up externally
When building a C source file with gcc-7 -Wshift-overflow=2, this warning
springs up:

libkmod.h: warning: result of "1 << 31" requires 33 bits to
represent, but "int" only has 32 bits [-Wshift-overflow=]

Change the two _KMOD_* identifiers to fit into 32 bits.
2018-06-18 09:46:56 -07:00
Luca Bruno c8f0623ad1 libkmod-module: check for NULL before accessing pointers
This introduces a few missing NULL-checks in public functions, and
align their docstrings with real behavior by getting rid of copy-paste
mistakes.

Signed-off-by: Luca Bruno <luca.bruno@coreos.com>
2018-04-05 14:54:43 -07:00
Yauheni Kaliuta 1e48901166 libkmod-elf: resolve CRC if module is built with MODULE_REL_CRCS
Normally exported symbol's crc is stored as absolute (SHN_ABS)
value of special named symbol __crc_<symbol name>.

When the kernel and modules are built with the config option
CONFIG_MODULE_REL_CRCS, all the CRCs are put in a special section
and the __crc_<symbol name> symbols values are offsets in the
section. See patch description of the commit:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=56067812d5b0e737ac2063e94a50f76b810d6ca3

Add kmod support of this configuration.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-08-04 08:20:24 -07:00
Yauheni Kaliuta e5b6a658ea libkmod: modinfo: implement signature output
Signature was ignored from the modinfo. Implement its parsing
from the module data and add its output to the modinfo utility.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-04-11 09:04:28 -07:00
Yauheni Kaliuta abcd0bf8ec libkmod: modinfo: implement line splitting in hex_to_str
The key output is usually short, but for signature it is more
readable to output it in several lines.

Implement line splitting. Set line limit hardcoded to 20 hex
numbers (not characters).

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-04-11 09:04:28 -07:00
Yauheni Kaliuta 96b88aea2a libkmod: modinfo: use own function for sig_key hex output
Refactor the code a bit to make it easier to extend for signature
output.

kmod_module_get_info() creats a hex string for the sig_key data
inplace. Separate it into own kmod_module_hex_to_string function
and handle the branch in the new kmod_module_info_append_hex,
keeping the same signature as the non-hex version.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-04-11 09:04:28 -07:00
Yauheni Kaliuta 30fb14f3c8 libkmod: modinfo: fix sig_id output
For some reason the key for sig_id was set to "signature". The
length was calculated against the proper string, as the result in
the output it was truncated to "signat".

Pass the proper key to the kmod_module_info_append() call.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2017-04-11 09:04:28 -07:00
Lucas De Marchi 31dd40a6b8 libkmod-config: fix parsing quoted kernel cmdline on params
We can only accept quoted values, not module names or parameter names.
2017-02-16 09:18:35 -08:00