Commit Graph

183 Commits

Author SHA1 Message Date
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 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
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
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
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
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
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 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
Bartosz Golaszewski b1982674ae module: fix a memory leak
When a module is removed and re-inserted without unrefing, the
kmod_file is unconditionally re-opened. This results in a memory
and file descriptor leak.

Fix it by checking if the file is already open in
kmod_module_insert_module().

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2017-02-16 08:50:01 -08:00
Lucas De Marchi e78fe15f0a libkmod-module: modinfo: print signature id
This way it's possible to give at least the signature type for PKCS#7.
2016-06-27 08:17:55 -03:00
Lucas De Marchi dcdb17715e libkmod-module: do not crash modinfo on 0 key id len 2016-06-27 08:17:55 -03:00
Michal Marek 2206d7f763 libkmod: Handle long lines in /proc/modules
kmod_module_new_from_loaded() calls fgets with a 4k buffer. When a
module such as usbcore is used by too many modules, the rest of the line
is considered a beginning of another lines and we eventually get errors
like these from lsmod:

libkmod: kmod_module_get_holders: could not open '/sys/module/100,/holders': No such file or directory

together with bogus entries in the output. In kmod_module_get_size, the
problem does not affect functionality, but the line numbers in error
messages will be wrong.

Signed-off-by: Michal Marek <mmarek@suse.com>
2016-06-21 18:00:20 -03:00
Peter Wu 1930899aae kmod_module_get_refcnt: fix documentation 2016-05-21 14:27:53 -03:00
Lucas De Marchi 114ec87c85 libkmod-module: fix return code in error path
ENOSYS is the wrong errno to return when we don't find a module in
kmod_module_insert_module(). Why is it there in the first place?  This
goes back to kmod v1 when we couldn't load modules by names, but we
should give a path instead.

708624a ("ELF: initial support for modinfo and strip of modversions and
vermagic.") changed that so we do a lazy-search by the module path in
this function. Later  f304afe ("Change error message to reflect
reality") fixed the log message but the return coded remained the same.
2015-06-13 18:29:47 -03:00
Harish Jenny K N fd44a98ae2 Fix race while loading modules
usecase: two sd cards are being mounted in parallel at same time on
dual core. example modules which are getting loaded is nls_cp437.
While one module is being loaded , it starts creating sysfs files.
meanwhile on other core, modprobe might return saying the module
is KMOD_MODULE_BUILTIN, which might result in not mounting sd card.

Experiments done to prove the issue in kmod.
Added sleep in kernel module.c at the place of creation of sysfs files.
Then tried `modprobe nls_cp437` from two different shells.
While the first was still waiting for its completion ,
the second one returned saying the module is built-in.

[ Lucas:

  The problem is that the creation of /sys/module/<name> and
  /sys/module/<name>/initstate are not atomic. There's a small window in
  which the directory exists but the initstate file was still not
  created.

  Built-in modules can be handled by searching the modules.builtin file.
  We actually lose some "modules" that create entries in /sys/modules
  (e.g. vt) and are not in modules.builtin file: only those that can be
  compiled as module are present in this file.

  We enforce mod->builtin to always be up-to-date when
  kmod_module_get_initstate() is called. This way if the directory
  exists but the initstate doesn't, we can be sure this is because the
  module is in the "coming" state, i.e. kernel didn't create the file
  yet, but since builtin modules were already handled by checking our
  index the only reason for that to happen is that we hit the race
  condition.

  I also added some tweaks to the patch, so we don't repeat the code for builtin
  lookup.  ]
2015-02-28 14:15:22 -03:00
Lucas De Marchi 01f9bc6dee Fix uninitialized warning
Initialize variable to NULL before calling kmod_module_new_from_lookup().

libkmod/libkmod-module.c: In function 'kmod_module_new_from_lookup.part.4.constprop':
libkmod/libkmod-module.c:192:8: warning: 'depmod' may be used uninitialized in this function [-Wmaybe-uninitialized]
   list = kmod_list_prepend(list, depmod);
        ^
libkmod/libkmod-module.c:173:23: note: 'depmod' was declared here
   struct kmod_module *depmod;
2015-01-25 23:54:05 -02:00
Lucas De Marchi dea2dfee9b Remove FSF mailing address
It has changed in the past, and these days, anyone can get a copy of the
LGPL via the web rather than by post.

Like 657a122 (Remove FSF mailing address) in libabc by Josh Tripplet,
but let the FSF website in which the license can be found.
2014-12-25 23:41:34 -02:00
Lucas De Marchi c2e4286bb9 Reorder and reorganize header files
Let the includes in the following order:

< system headers >
< libkmod >
< tool >
< local headers >
2014-10-03 01:43:15 -03:00
Lucas De Marchi 96573a0220 Move generic util functions to shared directory 2014-10-03 00:33:25 -03:00
Cristian Rodríguez 74c26943f1 Add missing O_CLOEXEC in kmod_module_get_size() 2014-06-19 18:01:37 -03:00
Leandro Pereira 30bfd48aef Close /sys/module/$NAME directory if opening /proc/module fails. 2014-05-14 20:24:19 -03:00
Leandro Pereira c1bc88c98e Free abspath if kmod_module_new_from_path() fails. 2014-05-14 20:21:20 -03:00
Michal Marek 450bd1b429 libkmod: Ignore errors from softdeps
Before we had softdeps, the usual idiom was

install foo /sbin/modprobe bar; /sbin/modprobe --ignore-install foo

ignoring errors from the first modprobe invocation. This also matches
the behavior of module-init-tools' implementation of softdep.
2014-04-01 07:40:37 -03:00
Michal Marek c2f4d85a9a libkmod-module: Simplify kmod_module_insert_module()
Store the file and elf pointer in the kmod_module structure and have it
freed together with the module.
2014-03-05 12:22:37 -03:00
Lucas De Marchi 366e2ed791 Remove duplicate includes
Found by
https://raw.github.com/karelzak/util-linux/master/tools/checkincludes.pl
2014-01-21 14:03:02 -02:00
Lucas De Marchi 9f02561d84 module: use _cleanup_free and remove useless call to free() 2013-11-18 11:56:57 -02:00
Lucas De Marchi 7ab8804448 libkmod: always pass O_NONBLOCK to kernel
Not passsing O_NONBLOCK to delete_module() is deprecated since kmod 11
and is being removed from the kernel. Force this flag in libkmod.
2013-09-20 01:37:24 -05:00
Lucas De Marchi 7e0385c47a Fix usage of readdir_r()
With readdir_r() we should be providing enough space to store the dir
name. This could be accomplished by define an union like systemd does:

	union dirent_storage {
		struct dirent de;
		uint8_t storage[offsetof(struct dirent, d_name) +
				((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))];
	};

However in all places that we use readdir_r() we have no concerns about
reentrance nor we have problems with threads. Thus use the simpler
readdir() instead.

We also remove the error logging here (that could be added back by
checking errno), but it was not adding much value so it's gone.
2013-08-29 01:33:51 -03:00
Lucas De Marchi 83b855a6ed Use "-internal" suffix instead of "-private" 2013-07-04 16:13:11 -03:00
Chengwei Yang d7152f6282 Add document for exported enums
There are several exported enums by libkmod without document, this patch
mainly added documentation for below enums like the way kmod_resources
be documented in.
* kmod_index
* kmod_remove
* kmod_insert
* kmod_probe
* kmod_filter
* kmod_module_initstate

This is not the best way to document these exported enums, however, it's
the simple way due to gtkdoc limits. It doesn't support export plain
enum like below: see https://bugzilla.gnome.org/show_bug.cgi?id=657444
---------8<-------head.h--------------8<-----------
...
enum foo {
    ...
};
...
---------8<-------end of head.h-------8<-----------
---------8<-------source.c------------8<-----------
...
/**
 * document for foo here
 */
...
typedef enum foo foo;
...
---------8<-------end of source.c-----8<----------
2013-05-04 18:57:43 -03:00
Chengwei Yang 491c490204 Several minor fixes for documentation 2013-05-04 10:42:56 -03:00
Lucas De Marchi b9a7da3910 libkmod-module: Don't pass NULL ctx to kmod_log 2013-04-23 21:26:08 -03:00
Lucas De Marchi 818af4f6fa libkmod-module: Don't pass NULL pointer to memcpy
When passing n=0, don't pass a NULL pointer, but instead pass anything
else (like the pointer to the start of the string).
2013-04-23 21:26:08 -03:00
Lucas De Marchi 55112d19f7 libkmod: Move finit_module() definition to missing.h
Check for finit_module() and don't use our own static inline function if
there's such function in libc (or another lib).

In testsuite we need to unconditionally define HAVE_FINIT_MODULE because
we want to override this function, and never use the static inline one
in missing.h
2013-04-09 04:38:27 -03:00
Lucas De Marchi 1407064e73 libkmod: Add missing definitions
Depending on kernel header and simply not passing the flags in
finit_module() if this header is not found is not good.

Add a missing.h header in which stuff like this should be added.
2013-04-09 04:05:09 -03:00
Kees Cook 144d1826f1 libkmod: add finit_module logic
When a module is being loaded directly from disk (no compression, etc),
pass the file descriptor to the new finit_module() syscall. If the
finit_module syscall is exported by the kernel syscall headers, use it.
Additionally, if the kernel's module.h file is available, map kmod flags
to finit_module flags.
2013-02-19 19:19:51 -03:00