Commit Graph

536 Commits

Author SHA1 Message Date
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