Commit Graph

599 Commits

Author SHA1 Message Date
Lucas De Marchi 8df21177fb libkmod-config: replace 0/1 with bool 2017-02-16 08:57:01 -08: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
James Minor f27a2b1274 libkmod: Fix handling of quotes in kernel command line
If a module parameter on the command line contains quotes, any
spaces inside those quotes should be included as part of the
parameter.

Signed-off-by: James Minor <james.minor@ni.com>
2017-01-23 10:47:13 -08:00
Lucas De Marchi 53d3a99ccc libkmod: fix use of strcpy
We were not checking if there was sufficient space in the buffer.
2016-08-08 11:42:52 -03:00
Lucas De Marchi a0f6726e66 libkmod: fix integration with gtk-doc
It was failing to generate doc with recent version of gtk-doc

[kmod]$ ./bootstrap
libkmod/docs/gtk-doc.make:33: error: EXTRA_DIST must be set with '=' before using '+='
libkmod/docs/Makefile.am:29:   'libkmod/docs/gtk-doc.make' included from here
autoreconf: automake failed with exit status: 1

Just add an empty EXTRA_DIST so it works.
2016-06-28 12:15:38 -03: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 75f45d9bfa libkmod-signature: handle 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 f7f2851024 libkmod: export new kmod_get_dirname() function
Make sure it gets exported and add documentation.
2015-09-30 15:28:24 -03:00
Laura Abbott b87d01d6ef modprobe: Update error message when path is missing
Currently, modprobe fails with no output by default if the
search paths it tries are missing:

$ modprobe -S notakernel dm-crypt
$
$ modprobe -S notakernel lkjjweiojo
$

This is fairly cryptic and not at all obvious there is a problem
unless the error code is checked or verbose flags are used.
Update the error message to indicate a problem and print out the
directory that failed.
2015-09-30 15:16:59 -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
Lucas De Marchi ebdac0005b Check return of fseek while reading index 2015-02-28 16:11:53 -03:00
Lucas De Marchi d98f2d32d1 Avoid sign-extension error on multiplication
Do not promote "idx * elf->header.section.entry_size" to int. Explicitly
cast the result to uint64_t so it's zero-extended.
2015-02-28 16:07:55 -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 885e90b6f9 libkmod: better explain a signed module layout 2015-02-18 16:49:01 -02:00
Lucas De Marchi dcbe1846e8 libkmod: consider empty signature key as invalid
A segmentation fault occurs if a module has an empty key attached to
its signature. This is mostly likely due to a corrupted module.

The crash happens because kmod_module_get_info() assumes that
kmod_module_signature_info() returns a signature of at least 1 byte.

The fix is based on a patch from Tobias Stoeckmann
<tobias@stoeckmann.org>, but rather than changing kmod_module_get_info()
to fix the crash, this changes kmod_module_signature_info() to
consider the signature as invalid.
2015-02-18 16:15:45 -02:00
Tobias Stoeckmann 40ef6e69bb Fix out of bounds signature access with 32 bit off_t
If kmod has been configured with --disable-largefile on a 32 bit
system, off_t will be 32 bit. In that case, the parsed sig_len can
bypass a validation check (it's _unsigned_ 32 bit).

Due to the unlikeliness of people using --disable-largefile, this is
a mere validation fix. With an explicit signed 64 bit cast, there is
no binary change for 99.9% of Linux systems out there. ;)
2015-02-18 15:49:10 -02:00
Tobias Stoeckmann 249dc5909b libkmod: properly validate file size
In function kmod_elf_new, the file size has to be properly validated against
section offset. Currently, the file size is considered valid based on
ELF header size + section header size * section count. That is not sufficient.

In fact, ELF specifies a section header offset, which doesn't have to be the
size of the ELF header. The supplied test cases even cover this.

The correct test is: section offset + section header size * section count

This patch also verifies that this value won't overflow.  I don't know a way
to crash a tool due to this bug, because later on the offset check would
prevent out-of-bounds access. An overflow would just mean to access a wrong
part in elf->memory. Yet it's a validation error.

Please note: The file size does not have to be validated against the size
of the ELF header again, elf_identify did this already.
2015-02-17 14:10:31 -02:00
Lucas De Marchi 6684102cd3 libkmod: use helper function to check for overflow 2015-02-10 10:46:26 -02:00
Tobias Stoeckmann 67466f266d Prevent offset + size overflow.
it is possible to overflow uint64_t by summing variables offset and
size up in elf_get_section_info. Thee values are extracted from module
file and are possibly maliciously tampered with.

If offset is in valid range and size very large, the result will
overflow and the size check passes. Later on, this will most likely
lead to a segmentation fault due to accessing uninitialized memory.

Attached please find a proof of concept module, which will trigger
a segmentation fault on modinfo. Tested on amd64:

tobias:~$ modinfo poc.ko
filename:       /home/tobias/poc.ko
Segmentation fault

There are more errors of this type in the ELF handling code that will be
fixed in other patches.
2015-02-10 09:34:44 -02: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 b35bf2a11c Fix include in python bindings
Prefix the include of libkmod in python bindings.
2015-01-02 13:11:39 -02:00
Lucas De Marchi 0a67d4f360 libkmod-elf: Remove unused variables
libkmod/libkmod-elf.c:52:34: warning: unused variable 'native_endianess'
      [-Wunused-const-variable]
static const enum kmod_elf_class native_endianess = KMOD_ELF_LSB;
2015-01-02 12:47:00 -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 bb72153d34 libkmod-index: move comment to include the includes 2014-11-15 11:18:54 -02:00
Lucas De Marchi a5852e3b36 libkmod-index: remove invalid comment 2014-11-15 11:18:54 -02:00
Lucas De Marchi c4cbdf8e17 libkmod-index: keep index and comments in .c
Just like other source files, keep the index and comments in the source
file rather than the header.

This also removes INDEX_PRIORITY_MIN that was never being used.
2014-10-29 11:58:12 -02:00
Lucas De Marchi 15a7ae30b3 shared: rename prefixes of strbuf functions
Use strbuf_ prefix instead of buf_.
2014-10-11 13:25:51 -03:00
Lucas De Marchi b4d1f44af1 Move strbuf implementation to shared/
Just move the strbuf-like implementation to shared/. No renames were
made yet to avoid cluttering the diff. It will come in a separate patch.
2014-10-11 13:09:42 -03:00
Lucas De Marchi eb6f9112cf libkmod: remove unused inline functions
libkmod/libkmod-list.c:39:33: warning: unused function 'list_node_next' [-Wunused-function]
static inline struct list_node *list_node_next(const struct list_node *node)
                                ^
libkmod/libkmod-list.c:47:33: warning: unused function 'list_node_prev' [-Wunused-function]
static inline struct list_node *list_node_prev(const struct list_node *node)
                                ^

It doesn't really matter in the end result since the compiler won't
generate any code for it. But let's keep it clean. It wasn't needed
until now, so probably it won't be anymore.
2014-10-09 11:11:29 -03:00
Lucas De Marchi 52c9c99056 Log error on failed underscores(), moving it to shared/
Move underscores() to shared/. It's the same as alias_normalize(), but
it rather operates in place, with the same string being passed.

The difference now that it's in shared/ is that it's a non-logging
function.

This makes us a little bit more verbose: we don't accept partially
correct module and aliases names in kcmdline and in configuration files.
We log an error instead.
2014-10-09 11:00:21 -03:00
Lucas De Marchi f4e8c16291 Move remaining functions from libkmod-util to shared 2014-10-09 01:26:39 -03:00
Lucas De Marchi 2b0104fe3c Move alias_normalize() to shared 2014-10-09 01:26:39 -03:00
Lucas De Marchi b95506ff61 Do not export array of kmod extensions
The only user outside of libkmod-util is depmod, which really only needs
to get the string for the extension of uncompressed modules. It doesn't
need to access the array itself.
2014-10-09 01:26:39 -03:00
Lucas De Marchi aafd38359a Rename getline_wrapped() to freadline_wrapped() 2014-10-03 03:25:06 -03:00
Lucas De Marchi b18979b770 Prefer inttypes.h over stdint.h 2014-10-03 02:03:55 -03: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 0db718edcf Move hash implementation to shared directory 2014-10-03 00:40:11 -03:00
Lucas De Marchi 74d1df6682 Move array implementation to shared directory 2014-10-03 00:33:25 -03:00
Lucas De Marchi 96573a0220 Move generic util functions to shared directory 2014-10-03 00:33:25 -03:00
Lucas De Marchi 8b7189bc25 Move missing.h to shared directory 2014-10-03 00:31:10 -03:00
Lucas De Marchi 576dd4393d Move macro.h to shared directory
It's not really related to libkmod, so move it to a directory in which
we keep common stuff.
2014-10-02 22:03:19 -03:00
Randy MacLeod 9b34db1ae6 Add back-up implementation of be32toh()
Older systems may not have the be32toh function defined. Check for this
and fall back to checking the endianness and calling bswap_32 directly
if needed.  This works on both old and new systems.

[Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>:
address comments raised by Lucas De Marchi [1], update commit message]
[1] http://www.spinics.net/lists/linux-modules/msg01129.html
2014-09-29 17:13:26 -03:00
Holger Obermaier 1a4aa7e2cb libkmod-index.c: Fix error message 2014-09-04 16:29:47 -03:00
Cristian Rodríguez 74c26943f1 Add missing O_CLOEXEC in kmod_module_get_size() 2014-06-19 18:01:37 -03:00
Lukas Anzinger 86e19e9acd Fix use-after-free in hash implementation.
If a value is added to the hash under a key that already exists the new value
replaces the old value for that key. Since key can be a pointer to data that
is part of value and freed by hash->free_value(), the key must be also
replaced and not only the value. Otherwise key potentially points to freed data.
2014-05-18 16:04:50 -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
Leandro Pereira e84d912bd7 Free realnames if kmod_lookup_alias_from_alias_bin() fails 2014-05-14 20:19:52 -03:00
Leandro Pereira b6d985c61a Ensure read_long() reads the correct number of bytes from the index 2014-05-14 20:18:00 -03:00
Leandro Pereira d36c886aed Bail out of index_mm_open() if fstat() fails 2014-05-14 20:17:30 -03:00
Lucas De Marchi f5cdd574a5 Make sure there's NUL byte at the end of strndupa
Since strcpy() doesn't ensure we have a NUL byte in the resulting
string, use alloca() + memcpy(). Also make sure we don't evaluate "s"
twice.
2014-04-07 12:30:04 -03:00
Lucas De Marchi 04c0956e20 Add strndupa to missing.h 2014-04-07 11:00:24 -03:00
Tom Gundersen 8240333b25 config: also parse softdeps from modules
This information can be found in /lib/modules/`uname -r`/modules.softdep, and
has only recently been exported by the kernel.

Also remove the advice about copying modules.softdep to /lib/modules as it is
not clear how to do this correctly with several kernels installed with
potentially conflicting soft dependencies.
2014-04-01 08:13:54 -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
Lucas De Marchi 3a33a7a566 build-sys: add hooks to build python bindings
Add --enable-python configure switch so we build the python bindings. We
also pass version.py through SED_PROCESS macro, so the version is kept
in sync with kmod.

Acked-by: Andy Grover <agrover@redhat.com>
2014-03-26 22:30:56 -03:00
Lucas De Marchi 5ad36e5222 python: Remove unused files from import
Acked-by: Andy Grover <agrover@redhat.com>
2014-03-26 22:30:49 -03:00
Guy Rozendorn c03dfbd5a7 python: Issue #15: resovled by using setuptools_cython 2014-03-25 00:34:16 -03:00
Guy Rozendorn 60ce23e707 python: Issue #15: Cython needs to be in setup_requires
We need Cython BEFORE installing kmod (done with setup_requires),
and AFTER installation - during runtime (done with install_requires)
2014-03-25 00:34:16 -03:00
Guy Rozendorn 1f9c5a04f7 python: Issue #15 Cython is now a dependency 2014-03-25 00:34:16 -03:00
Andy Grover 57a8efdf92 python: update version to 0.9.1 2014-03-25 00:34:16 -03:00
Guy Rozendorn 17b69ada6f python: Issue #13 skipping building extensions on non-Linux platforms 2014-03-25 00:34:16 -03:00
Andy Grover e4a57f6828 python: Make kmod.modprobe() raise an error if no modules found
Add 'quiet' option to override.

Add docstring.
2014-03-25 00:34:16 -03:00
Andy Grover 891e0756e7 python: Add self as maintainer to setup.py 2014-03-25 00:34:16 -03:00
Andy Grover 8e73a86d6e python: Correct building for python 2.6
Reported-by: Oz Nahum Tiram <nahumoz@gmail.com>
2014-03-25 00:34:16 -03:00
Oz f0d6f3701a python: add docstring to insert and remove methods 2014-03-25 00:34:16 -03:00
Oz 240c0f4694 python: add docstring to rmmod 2014-03-25 00:34:16 -03:00
Andy Grover b32900f4e8 python: update version to 0.9 2014-03-25 00:34:16 -03:00
Andy Grover 9ebfb9c198 python: Update README
Last update changed API slightly:
* loaded_modules renamed to modules
* returns a module object instead of (name, size)
2014-03-25 00:34:16 -03:00
W. Trevor King 26105c168f python: MANIFEST.in: add AUTHORS to distibuted source files. 2014-03-25 00:34:16 -03:00
W. Trevor King c8b5c51ed6 python: Ran update-copyright.py. 2014-03-25 00:34:16 -03:00
W. Trevor King 64f9a6a630 python: .update-copyright.conf: update to pipe separators.
This brings the config file up to speed with the following
update-copyright commit:

  commit 3c68a1a48419d8b2bbc2ce0e7f1700b996ec30e9
  Author: W. Trevor King <wking@tremily.us>
  Date:   Fri Oct 19 21:52:48 2012 -0400

    project: for consistency, also separate ignored paths with pipes.
2014-03-25 00:34:16 -03:00
W. Trevor King 8e8e24bcbd python: .update-copyright.conf: add MANIFEST.in to ignored files. 2014-03-25 00:34:16 -03:00
W. Trevor King ac24814360 python: .update-copyright.conf: add Red Hat alias for Andy Grover.
He seems to be assigning copyright of files he writes to Red Hat.
Avoiding .mailmap so the git log will still attribute changes to him.
I'm not sure which name should go in the AUTHORS file.
2014-03-25 00:34:16 -03:00
W. Trevor King 5a39a9d988 python: .update-copyright.conf: add README.rst to ignored files. 2014-03-25 00:34:16 -03:00
W. Trevor King 3c65e333f7 python: .mailmap: consolidate Andy Grover email addresses. 2014-03-25 00:34:15 -03:00
W. Trevor King 6745a4a7da python: .update-copyright.conf: add copyright configuration.
Use my external update-copyright package to maintain copyright blurbs.

http://pypi.python.org/pypi/update-copyright/
2014-03-25 00:34:15 -03:00
W. Trevor King 0a2fab2b32 python: module: fix versions -> info typo in Module._info_get() error message. 2014-03-25 00:34:15 -03:00
W. Trevor King d9c0c63eb7 python: Initialize Kmod.mod_dir to None in __cinit__(). 2014-03-25 00:34:15 -03:00
W. Trevor King caee9c10c5 python: Use an OrderedDict for Module.info (preserving modinfo ordering). 2014-03-25 00:34:15 -03:00
W. Trevor King 81c1a4e840 python: Add Module.info attribute. 2014-03-25 00:34:15 -03:00
W. Trevor King e0ff45a048 python: Use Cython's libc.errno for EEXIST. 2014-03-25 00:34:15 -03:00
W. Trevor King 0dd3047efb python: Add Module.versions attribute. 2014-03-25 00:34:15 -03:00
W. Trevor King 76d34f40e1 python: README.rst: add .rst symlink for GitHub. 2014-03-25 00:34:15 -03:00
W. Trevor King 161576d420 python: README: mention Cython dependency. 2014-03-25 00:34:15 -03:00
W. Trevor King 028fc127b6 python: README: cleanup reStructuredText syntax. 2014-03-25 00:34:15 -03:00
W. Trevor King 075f4ea0e2 python: Add missing copyright blurbs. 2014-03-25 00:34:15 -03:00
W. Trevor King c737198936 python: Add docstrings to kmod.kmod and kmod.kmod.Kmod. 2014-03-25 00:34:15 -03:00
W. Trevor King 3cc8a66078 python: MANIFEST.in: add missing source to the sdist tarball. 2014-03-25 00:34:15 -03:00
W. Trevor King 113b46bc1e python: Add additional out Module attributes (path, refcnt, ...). 2014-03-25 00:34:15 -03:00
W. Trevor King f7c62154ec python: Convert to Cython.
With Cython we get easier memory handling and Python 3 compatibility.
2014-03-25 00:34:15 -03:00
Andy Grover cd4df5accd python: update FSF address in COPYING and COPYING.LESSER
Please FSF, never move again.
2014-03-25 00:34:15 -03:00
Andy Grover 6cdc22b36b python: whitespace 2014-03-25 00:34:15 -03:00
Andy Grover 705187f17b python: add proper error handling to loaded_modules()
We need to check the result of basically all Py* calls and cleanup
properly if they fail.
2014-03-25 00:34:14 -03:00
Andy Grover 31aa6834ba python: Improve README's description of kmod
Based on text from kmod's README.
2014-03-25 00:34:14 -03:00
Andy Grover c73ffc51e5 python: Make setup.py version match version in .c 2014-03-25 00:34:14 -03:00
Andy Grover 3aa27ba988 python: Add README 2014-03-25 00:34:14 -03:00
Andy Grover 8a4d09336e python: More cleanup and renaming of stuff. Add docstrings. 2014-03-25 00:34:14 -03:00