Commit Graph

39 Commits

Author SHA1 Message Date
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
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
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 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 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
Lucas De Marchi fea655dcb3 libkmod-elf: Fix check by class in get_modversions()
Commit 51c409b ("Cache the offset of crc") unintentinally changed the
comparison "if (elf->class & KMOD_ELF_32)" to
"if (elf->class == KMOD_ELF_32)".

This has been reported by Serge Voilokov <serge0x76@gmail.com>:

	On Raspberry PI elf->class equals KMOD_ELF_32|KMOD_ELF_LSB so
	valid condition should be (elf->class & KMOD_ELF_32) instead of
	(elf->class == KMOD_ELF_32).

This fixes "modprobe --dump-modversions" failing on 32b systems.
2014-03-07 01:24:39 -03:00
Lucas De Marchi 83b855a6ed Use "-internal" suffix instead of "-private" 2013-07-04 16:13:11 -03:00
Lucas De Marchi a20a37c351 Use static assertions for sizeof checks 2013-04-15 14:14:07 -03:00
Lucas De Marchi e6b0e49b4e Update copyright notices 2013-01-16 11:27:45 -02:00
Lucas De Marchi a4578669ca libkmod-module: mangle the section header, not the section
When we are told to remove the "__versions" section we were mangling
that section instead of tweaking the SHF_ALLOC flag in its header.
2012-11-21 20:23:02 -02:00
Lucas De Marchi d196b8d99f libkmod-module: Remove key+value vermagic from .modinfo section
When told to force load a module, we were removing only the value of
vermagic instead of the complete entry.

Philippe De Swert (philippe.deswert@jollamobile.com) sent a patch that
was additionally mangling also the last two chars of the key
("vermagic="). Instead of creating an invalid entry in .modinfo section
like this, this patch removes the complete entry, key + value, by
zeroing the entire string.

Much thanks to Philippe who found the issue and pointed to the fix.
2012-11-21 20:22:56 -02:00
Lucas De Marchi 9e2eadb1d7 Silence clang warnings with __unused__ attribute
I hate this kind of READV and WRITEV macros that Gustavo seems to love.
clang-analyzer hates them as well.

I'm not motivated enough to refactor this, but I want a clean clang
report, so just shut it up.
2012-05-23 20:31:18 -03:00
Lucas De Marchi 4321590770 Remove dead increment 2012-04-26 11:42:27 -03:00
Lucas De Marchi ea17e2b043 elf: privately export kmod_elf_get_section()
This function is used to find a certain section in ELF file. It will be
used to get the modname from an ELF file.
2012-01-26 16:05:05 -02:00
Lucas De Marchi a66a6a999f Update copyright 2012-01-09 00:41:07 -02:00
Gustavo Sverzut Barbieri 599a032467 elf: zero *array when count is zero. 2012-01-03 14:53:15 -02:00
Gustavo Sverzut Barbieri 674f8590e3 elf: implement kmod_module_get_dependency_symbols()
Uses kmod_elf_get_dependency_symbols() that looks into ".symtab" for
UNDEF symbols and matches the name from ".strtab" to "__versions" to
get crc.

Likely the public API should unify the symbol information getters and
list release, they are almost the same.
2011-12-24 01:44:31 -02:00
Gustavo Sverzut Barbieri 4b55befd37 elf: add bind type to kmod_modversion.
will be used to share this structure with get_dependency_symbols()
2011-12-24 01:44:31 -02:00
Gustavo Sverzut Barbieri 45e6db9c01 elf: add get_symbols()
Similar to module-init-tools load_symbols(), it will try .symtab and
.strtab for symbols starting with __crc_, if they are found their crc
is read from ELF's Elf_Sym::st_value.

If not found, then it will fallback to __ksymtab_strings.
2011-12-24 01:44:31 -02:00
Gustavo Sverzut Barbieri 9bbb72df05 elf: minimum size is enough. 2011-12-24 01:24:55 -02:00
Jan Engelhardt 87beacc99f libkmod: use C99 __func__ over GNU-specific __FUNCTION__ 2011-12-20 13:48:44 -02:00
Luis Felipe Strano Moraes a969376fb0 Removing warnings when compiling on i686 platform. 2011-12-20 13:15:32 -02:00
Lucas De Marchi 76b8031bce elf: fix regression with empty strings
Commit "b20dc17 Remove unneeded reference to last string" reverted the
fix in "47a0ef6 elf: do not output empty strings." and empty strings are
appearing again in kmod-modinfo.

With this commit we do a bit different and instead of keeping the
reference to last string we skip the '\0' inside the loop.
2011-12-20 12:04:21 -02:00
Lucas De Marchi 51c409b449 Cache the offset of crc
This way we don't need to check every time in loop. Indeed compiler
could already optimize out that checks, but this ways is cleaner and
shorter.
2011-12-20 11:41:19 -02:00
Lucas De Marchi 58b7191c94 Make section size constant since it's always the same 2011-12-20 11:35:17 -02:00
Lucas De Marchi f8fa525c7f Optimize kmod_elf_get_strings() by reducing calls to memcpy
Copy the entire string only once and then scan it, setting up the
pointers of the vector.
2011-12-20 03:34:28 -02:00
Lucas De Marchi b20dc17068 Remove unneeded reference to last string 2011-12-20 03:27:51 -02:00
Lucas De Marchi 052656f5b3 Put blank lines where it's due
See CODING-STYLE for reference
2011-12-20 03:27:51 -02:00
Lucas De Marchi 3267026e0b Terminate strv with NULL 2011-12-20 03:27:50 -02:00
Lucas De Marchi 4f0f0e75e2 Reduce ident by continuing early 2011-12-20 03:27:50 -02:00
Gustavo Sverzut Barbieri 1c58590549 elf: skip leading symbol dot in modversion.
Mimics module-init-tools's dump_modversions() that calls skip_dot()
to skip leading dots. This is required for PPC64.
2011-12-19 21:55:27 -02:00
Gustavo Sverzut Barbieri fc8e58ba4e elf: fix typo that resulted in invalid uint reads for big-endian platforms. 2011-12-19 21:55:22 -02:00
Gustavo Sverzut Barbieri 47a0ef601d elf: do not output empty strings. 2011-12-19 18:32:58 -02:00
Gustavo Sverzut Barbieri fc27344ed9 elf: fix minimum file size calculation.
there is only a single file header, multiple sections.
2011-12-19 15:06:49 -02:00
Gustavo Sverzut Barbieri 708624a4eb ELF: initial support for modinfo and strip of modversions and vermagic.
Needs testing, but should work.
2011-12-19 15:06:49 -02:00