Commit Graph

1263 Commits

Author SHA1 Message Date
Jessica Yu
87643703a7 create-diff-object: create .kpatch.relocations and .kpatch.symbols sections
Instead of creating dynrela sections, have create-diff-object create
intermediate sections .kpatch.relocations and .kpatch.symbols which can
then be used to build (depending on kernel version) either dynrela sections
or klp rela/klp arch sections + klp symbols in a later phase of kpatch-build.
2017-01-23 12:43:17 -08:00
Jessica Yu
58de46cb9e lookup: parse Module.symvers in lookup_open()
Have lookup_open() also parse Module.symvers and add the resulting symbols
and their objnames to the lookup table. This code was essentially
cherry-picked from Josh Poimboeuf's lookup code found here:

  8cdca59c88

That patch was modified to fix a bug in obj_read() (calling elf_end()
without strdup'ing the symbol name strings, which was causing null
dereferences) and to fix up the module name after reading it from
Module.symvers (replacing '-' with '_' and stripping the path prefixes).

Also, add lookup_exported_symbol_objname(), which looks up the objname of
an exported symbol by making use of the objname information obtained from
Module.symvers.
2017-01-23 12:43:12 -08:00
Jessica Yu
b6a15f3dd6 create-diff-object: rename 'name' variable to 'objname'
'name' isn't very descriptive, rename it to 'objname' to avoid confusion
2017-01-23 12:43:09 -08:00
Jessica Yu
52e2ad66ca kpatch-elf: add kpatch_remove_and_free_section()
Add kpatch_remove_and_free_section(), which, given a section name,
removes and frees all matching sections from the passed in kpatch_elf.
2017-01-23 12:43:05 -08:00
Jessica Yu
dac26b8cb2 kpatch-elf: for rela sections, find base section by index rather than name
If there exist multiple sections with the same name (which can happen when
using the --unique option with ld, which will be used to keep multiple
(per-object) .parainstructions and .altinstructions sections separate),
find_section_by_name() will only return the first section name match, which
leads to incorrect base section assignments for rela sections. Fix this by
using the sh_info field of the rela section to find its base section
instead, which contains the index of the section to which the relocation
applies.
2017-01-23 12:43:00 -08:00
Jessica Yu
a3108de96a kpatch-elf: fix null dereference when sym->sec is NULL
Make sure sym->sec is not NULL before checking for its rela section
(sym->sec->rela). This fixes a case where an object may have STT_FUNC
symbols whose the sections (sym->sec) were not selected for inclusion (or
are located in another object) and hence these symbols do not have sym->sec
set. This corner case only recently popped up after reusing kpatch_elf_open()
on objects that have been outputted by create-diff-object (and these
objects only contain the necessary sections needed for the patch module).

This will also automatically exclude livepatch symbols from the check,
because they do not have sections associated with them (i.e., sym->sec is
NULL). We do not have to check for fentry calls for klp (SHN_LIVEPATCH)
symbols, because [1] they do not have sections associated with them, [2]
they are not the target functions to be patched, and [3] they are
technically just placeholder symbols for symbol resolution in livepatch.
2017-01-23 12:42:56 -08:00
Jessica Yu
91909e9273 kpatch-elf: ensure SHN_LIVEPATCH syms don't get set to SHN_UNDEF when reindexing elements 2017-01-23 12:42:52 -08:00
Jessica Yu
2c3c44fec2 kpatch-elf: add kpatch_reindex_elements() and kpatch_rebuild_rela_section_data()
Move functions kpatch_reindex_elements() and kpatch_rebuild_rela_section_data()
from create-diff-object.c to kpatch-elf.c. These functions will be used
to rebuild kpatch elf data in create-klp-module and create-kpatch-module,
i.e. during the second "phase" of kpatch-build.
2017-01-23 12:42:47 -08:00
Jessica Yu
6e43062409 kpatch-elf: add livepatch related Elf constants 2017-01-23 12:42:42 -08:00
Jessica Yu
3d6ea904e8 log: include error.h
Usage of error() requires error.h
2017-01-23 12:42:35 -08:00
Josh Poimboeuf
1dcf13f82b Merge pull request #653 from joe-lawrence/kpatch-test-fixes
Small kpatch-test fixups
2017-01-18 22:57:51 -06:00
Josh Poimboeuf
bc76e64b8b Merge pull request #649 from kamalesh-babulal/ubuntu_bug_fix
kpatch-build: Add UTS_UBUNTU_RELEASE_ABI tag for non-stock kernel
2017-01-16 08:58:57 -06:00
Joe Lawrence
97be7f57fb testing: add full path for /sbin executables
"/sbin" might not be in the user's path, so prefix callouts to such
utilities with their full path.
2017-01-13 13:54:35 -05:00
Joe Lawrence
f7b59ca809 testing: skip combined test for a single patch
If kpatch-test processes a single .patch file, it will skip creating a
COMBINED.patch.

Do the same on the testing side (instead of complaining that it "can't
find kpatch-COMBINED.ko, skipping.")
2017-01-13 13:54:35 -05:00
Joe Lawrence
1096da7155 testing: handle empty glob cases
If kpatch-test looks for *.patch or *.test globs in a directory that
contains no such files, it will try to handle a single file named
"*.patch" or "*.test".

Set the global nullglob option to avoid this problem.  At the same time,
stop and whine to the user if no .patch files were found.
2017-01-13 13:54:04 -05:00
Kamalesh Babulal
825cddcc45 kpatch-build: Add UTS_UBUNTU_RELEASE_ABI tag for non-stock kernel
commit eb55adc52d ("use livepatch 4.5 features in Ubuntu Xenial
kernel") will trigger following build failure, while building stock
kernel on Ubuntu:
make[2]: Entering directory '/root/.kpatch/obj'
  CC [M]  /root/.kpatch/tmp/patch/patch-hook.o
In file included from
/root/.kpatch/tmp/patch/livepatch-patch-hook.c:28:0,
                 from /root/.kpatch/tmp/patch/patch-hook.c:21:
/root/.kpatch/tmp/patch/livepatch-patch-hook.c: In functionpatch_ini:
/root/linux-4.8.15/include/generated/utsrelease.h:2:32: error: too many
decimal points in number
 #define UTS_UBUNTU_RELEASE_ABI 4.8.15
                                ^
/root/.kpatch/tmp/patch/livepatch-patch-hook.c:252:7: note: in expansion
of macro UTS_UBUNTU_RELEASE_ABI
       UTS_UBUNTU_RELEASE_ABI >= 7 ) \
       ^
Stock kernel version string might differ from the ubuntu kernel
versioning format. This patch sets UBUNTU_KERNEL flag, when kpatch
module is being build for ubuntu distro kernel and check for this
flag before echoing UTS_UBUNTU_RELEASE_ABI tag.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Cc: Chris J Arges <christopherarges@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
2017-01-13 10:18:45 +05:30
Jessica Yu
11396a5943 Merge pull request #646 from joe-lawrence/sparse_smatch_2
Sparse smatch round 2
2017-01-12 17:27:00 -08:00
Jessica Yu
3f0278147f Merge pull request #654 from joe-lawrence/taint
kmod: let kernel apply TAINT_LIVEPATCH
2017-01-12 13:16:25 -08:00
Joe Lawrence
e7937196b7 kmod: let kernel apply TAINT_LIVEPATCH
Upstream commit 2992ef29ae01 ("livepatch/module: make TAINT_LIVEPATCH
module-specific") v4.9+ modified the kernel to add the TAINT_LIVEPATCH
flag on module load.  To support this feature, add the "livepatch"
module info in the {k,live}patch modules and drop the add_taint() in the
core module.
2017-01-12 16:05:53 -05:00
Joe Lawrence
7a855df172 sparse: use "NULL" for NULL pointers
Fixes sparse complaints:

  create-diff-object.c:2302:24: warning: Using plain integer as NULL pointer
  create-diff-object.c:2303:11: warning: Using plain integer as NULL pointer
  create-diff-object.c:2334:59: warning: Using plain integer as NULL pointer
  create-diff-object.c:2347:43: warning: Using plain integer as NULL pointer
2017-01-12 15:38:25 -05:00
Joe Lawrence
0193dd51bd sparse: make local symbols static
Fixes many sparse warnings like:

  warning: symbol 'foo' was not declared. Should it be static?
2017-01-12 15:38:21 -05:00
Josh Poimboeuf
23bc1bc93d Merge pull request #624 from joe-lawrence/integration_tests
Update integration tests
2016-12-21 09:39:03 -06:00
Jessica Yu
823158d15d Merge pull request #647 from joe-lawrence/livepatch_compat
kpatch: better support for livepatch modules
2016-12-20 14:38:26 -08:00
Joe Lawrence
b55dfb7dee kpatch: better support for livepatch modules
Livepatch modules can be supported with minimal changes to the kpatch
script.  Adjust for appropriate sysfs paths, core-patching code (in
kernel for livepatch, kpatch.ko for kpatch), and checksum verification
(only verify the checksum if it exists).

Fixes #479.
2016-12-20 16:34:41 -05:00
Jessica Yu
5046cc9243 Merge pull request #644 from jpoimboe/no-seg-fault
create-diff-object: print error message instead of seg faulting
2016-12-20 11:55:27 -08:00
Jessica Yu
f6918090ff Merge pull request #645 from jpoimboe/kpatch-build-debug
kpatch-build: reduce the debug firehose
2016-12-20 10:42:06 -08:00
Josh Poimboeuf
c27b3206cf Merge pull request #643 from jpoimboe/gcc-6-rodata-str
create-diff-object: support gcc 6 per-function string tables
2016-12-20 10:30:40 -06:00
Josh Poimboeuf
d3d8a603e2 kpatch-build: reduce the debug firehose
When passing '-d' to kpatch-build, it prints out some useful information
and keeps the related files around in ~/.kpatch/tmp.  However, it also
passes '-d' to create-diff-object, which spits out way too much
information, drowning out all the other useful messages printed by
kpatch-build.

In my experience, the create-diff-object debug info is overkill for
debugging most issues, so disable it.  The flag can still be used when
running create-diff-object manually.
2016-12-19 17:36:06 -06:00
Josh Poimboeuf
79fd538f51 create-diff-object: print error message instead of seg faulting
Related to issue #629, print a useful error message at the time of the
original error condition instead of seg faulting later.
2016-12-19 17:29:57 -06:00
Josh Poimboeuf
bfc24c745d create-diff-object: support gcc 6 per-function string tables
With older versions of gcc, string tables were stored in a few
.rodata.str1.* sections:

  # eu-readelf -S patched/fs/proc/meminfo.o |grep rodata
  [ 6] .rodata.str1.1       PROGBITS     0000000000000000 0000005c 00000008  1 AMS    0   0  1
  [11] .rodata.str1.8       PROGBITS     0000000000000000 000000a0 00000364  1 AMS    0   0  8
  [16] .rodata.meminfo_proc_fops PROGBITS     0000000000000000 000009c0 000000d8  0 A      0   0 32
  [17] .rela.rodata.meminfo_proc_fops RELA         0000000000000000 00017a58 00000060 24 I     35  16  8

With gcc 6, there can now be per-function string tables:

  # eu-readelf -S patched/kernel/fork.o |grep rodata
  [10] .rodata.trace_raw_output_task_newtask.str1.8 PROGBITS     0000000000000000 00000208 00000032  1 AMS    0   0  8
  [13] .rodata.trace_raw_output_task_rename.str1.8 PROGBITS     0000000000000000 000002b0 00000030  1 AMS    0   0  8
  [24] .rodata.mm_init.str1.1 PROGBITS     0000000000000000 0000094b 0000000e  1 AMS    0   0  1
  [29] .rodata.sighand_ctor.str1.1 PROGBITS     0000000000000000 00000ba1 00000017  1 AMS    0   0  1
  [32] .rodata.str1.1       PROGBITS     0000000000000000 00000bec 000001d9  1 AMS    0   0  1
  [33] .rodata.__mmdrop.str1.8 PROGBITS     0000000000000000 00000dc8 000000c4  1 AMS    0   0  8
  [82] .rodata.copy_process.part.30.str1.8 PROGBITS     0000000000000000 000019e0 00000098  1 AMS    0   0  8
  [83] .rodata.copy_process.part.30.str1.1 PROGBITS     0000000000000000 00001a78 0000003f  1 AMS    0   0  1
  [92] .rodata._do_fork.str1.1 PROGBITS     0000000000000000 0000372e 00000009  1 AMS    0   0  1
  [173] .init.rodata         PROGBITS     0000000000000000 00004a18 00000011  0 A      0   0  1
  [202] .rodata.str__task__trace_system_name PROGBITS     0000000000000000 00004df0 00000005  0 A      0   0  1

Fixes: #628
2016-12-19 12:01:30 -06:00
Jessica Yu
5bbae490d4 Merge pull request #642 from iredko/enable-kpatch-for-debug-kernels
kpatch-build: make patches for debug kernels too
2016-12-19 09:56:17 -08:00
Igor Redko
8f237b1e7d kpatch-build: make patches for debug kernels too
GCC with KASAN instrumentation creates section ".rodata" with some static strings (i.e. some of them go to ".rodata.str1.1" for release build).
This change makes possible to build patch and check if it fixes issue found with KASAN, such as CVE-2016-9555.
2016-12-19 14:27:43 +03:00
Jessica Yu
86e21e9b9e Merge pull request #641 from joe-lawrence/sparse_smatch
Fix static code analyzer complaints
2016-12-16 16:50:34 -08:00
Jessica Yu
dd8d03ba9b Merge pull request #639 from jpoimboe/activeness-safety-retry
kpatch: retry kpatch load on failure
2016-12-16 16:35:34 -08:00
Joe Lawrence
6ee3c0f8fa testing: add a PATCHES_LIST to kpatch-test
Instead of building *.patch, allow the user to specify patch files on
the command line.  For example:

  kpatch-test --quick centos-7/cmdline-string.patch centos-7/data-new.patch

Update the top-level Makefile as well, so it can be similarly invoked:

  PATCHES="centos-7/cmdline-string.patch centos-7/data-new.patch" make quick

If no patches are specified on the kpatch-test command line, then fall
back to the previous behavior of *.patch.

NOTE: If patches *are* specified, then only the .test files
corresponding to those patches will be executed.  Provided patch paths
will also override any --directory value.
2016-12-16 16:18:19 -05:00
Joe Lawrence
5db4e232ee testing: use common Makefile
Move the integration tests Makefile up a directory level so that it can
be shared.  Update the kpatch-test script to accept a patch-directory
argument and the multiple.test cases to handle this new arrangement.
2016-12-16 16:16:30 -05:00
Joe Lawrence
b6692c14d7 testing: add and rebase integration tests for Ubuntu 16
Kernel version: 4.4.0-53-generic
2016-12-16 16:06:49 -05:00
Joe Lawrence
11ce8102c2 testing: add and rebase integration tests for CentOS 7
Kernel version: 3.10.0-327.36.3.el7.x86_64
2016-12-16 16:06:28 -05:00
Joe Lawrence
d7bcd38787 testing: rebase integration tests for Fedora 25
Kernel version 4.8.6-300.fc25.x86_64
2016-12-16 16:06:15 -05:00
Joe Lawrence
1592c5a711 testing: move integration test patches directory
Prepare for updating the integration tests and move into a standard path
(this should be easier for future automation).

The patch directory name(s) should be in the form ${ID}-${VERSION_ID}/
as defined by the target's /etc/os-release.
2016-12-16 16:05:59 -05:00
Joe Lawrence
33ac7db028 testing: add rebase_patches tool 2016-12-16 16:05:06 -05:00
Joe Lawrence
501a63ad6d smatch,sparse: trivial code cleanups
Fixes smatch warning:
  kmod/core/core.c:64:1: warning: symbol 'kpmod_list' was not declared.  Should it be static?

Fixes sparse warnings:
  kmod/core/core.c:680 kpatch_write_relocations() warn: inconsistent indenting
  kmod/core/core.c:750 kpatch_write_relocations() warn: inconsistent indenting
2016-12-16 14:26:45 -05:00
Joe Lawrence
5a6ddaad8d smatch: fix kpatch_shadow_alloc cleanup
The error paths in kpatch_shadow_alloc do not free an allocated
kpatch_shadow structure (and it's not added to the kpatch_shadow_hash).
Handle the kfree in the various error return paths.

Fixes the smatch warning:

  kmod/core/shadow.c:97 kpatch_shadow_alloc() warn: possible memory leak of 'shadow'
2016-12-16 14:26:11 -05:00
Jessica Yu
e9fc979712 Merge pull request #637 from arges/636
kpatch and patch module builds fail on Ubuntu 16.04 #636
2016-12-16 10:55:45 -08:00
Josh Poimboeuf
62f8e6daa2 kpatch: retry kpatch load on failure
If a kpatch load fails due to an activeness safety check, try again, up
to 5 times.

Unfortunately, insmod doesn't return the error code reported by the
kernel.  So we have to parse its error output instead.
2016-12-16 11:05:28 -06:00
Josh Poimboeuf
e4f6b5d4c4 kpatch: convert load_module() to use local variables
The kpatch script's functions don't use local variables, which makes it
bug prone and hard to understand.  It would be nice to convert the whole
file, but I'll just start with converting load_module() for now.

This also removes the unused 2nd argument to insmod, which is leftover
from commit 8e8de4718d ("kpatch: deprecate the replace command").
2016-12-16 11:05:28 -06:00
Chris J Arges
eb55adc52d use livepatch 4.5 features in Ubuntu Xenial kernel
Some features were backported into the 4.4 kernel which change the fields
of the livepatch structures. Ensure we can work with either v4.5 or greater,
or Ubuntu 4.4.0-7 or greater.
2016-12-16 07:05:53 -06:00
Chris J Arges
254e8bbe8d kpatch-build: Add UTS_UBUNTU_RELEASE_ABI symbol for utsrelease.h
The UTS_UBUNTU_RELEASE_ABI symbol is in utsrelease.h as installed by
linux-headers-`uname -r`. However when building a module with kpatch-build
utsrelease.h gets regenerated and doesn't include the ABI variable. This
patch just adds the additional define based on the input ARCHVERSION.
2016-12-16 07:05:15 -06:00
Jessica Yu
afc7f12bb7 Merge pull request #638 from jpoimboe/activeness-safety-fixes
kmod/core: fix activeness safety checks for kernels >= 4.6
2016-12-15 12:59:38 -08:00
Jessica Yu
ecaf939291 Merge pull request #640 from jpoimboe/dup-file-symbol
create-diff-object: better error message for dup file+symbol
2016-12-14 21:30:41 -08:00