Commit Graph

486 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
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
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
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
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
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
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
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
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
Josh Poimboeuf
20871391d9 create-diff-object: better error message for dup file+symbol
Give a slightly better error message for the dup file+symbol issue.
It's still cryptic but it's good enough to at least give us kpatch
developers a better idea about what went wrong.  This would have helped
diagnose issue #633 much more quickly.
2016-12-14 15:05:55 -06:00
Jessica Yu
f9cfd80718 Merge pull request #630 from jpoimboe/LDFLAGS_vmlinux
kpatch-build: fix 'undefined reference to kpatch_shadow_*' errors
2016-12-11 23:37:21 -08:00
Jessica Yu
a963ee76ee Merge pull request #631 from jpoimboe/treewide-rebuild-fixes
kpatch-build: fix tree-wide rebuild
2016-12-09 16:17:15 -08:00
Josh Poimboeuf
1fc364ee75 kpatch-build: fix tree-wide rebuild on RHEL 7
On RHEL 7 based kernels, copy_user_64.o misuses the .fixup section by
placing a normal function in it.  That confuses create-diff-object.

Work around it by just skipping the file altogether, which is fine to do
because it's an assembly file which should never change anyway.

Fixes #625.
2016-12-06 21:54:05 -06:00
Josh Poimboeuf
5f5fd136bb kpatch-build: fix tree-wide rebuild
For newer kernels, some new objects have been added to the 'head-y'
build target.  These objects aren't directly traceable to vmlinux so
they have to be added manually.

Fixes #626.
2016-12-05 21:13:15 -06:00
Josh Poimboeuf
1330b4a3e5 kpatch-build: fix 'undefined reference to kpatch_shadow_*' errors
When building the patched version of the kernel, vmlinux has to be
linked with the '--warn-unresolved-symbols' linker flag.  Otherwise the
link will fail if the patch uses kpatch-specific symbols like
kpatch_shadow_alloc() and friends.

As of upstream Linux commit b36fad65d61f ("kbuild: Initialize exported
variables"), LDFLAGS_vmlinux= no longer works from the command line,
resulting in '--warn-unresolved-symbols' no longer getting set.

Instead we can use kpatch-gcc to pass the flag to the linker.

Fixes #627.
2016-12-05 20:22:11 -06:00
Jessica Yu
dc3ef4d6da Merge pull request #616 from rosslagerwall/extable-addend
create-diff-object: Update fixup offsets in .rela__ex_table
2016-11-24 20:04:46 -08:00
Ross Lagerwall
13d61012f9 create-diff-object: Update fixup offsets in .rela__ex_table
When pruning entries from the fixup table, update the offsets in
.rela__ex_table otherwise the relas might point to the wrong fixup entry
or even out of the .fixup section.

Fixes #615.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2016-11-24 09:15:48 +00:00
Jessica Yu
de63a856c0 kpatch-build: refresh git index during cleanup if $SRCDIR was a git repository
If $SRCDIR was a git repo, we leave the repo with a dirty index even after
reversing the patch during cleanup. This gets picked up by
scripts/setlocalversion and consequently subsequent kpatch-builds using the
same $SRCDIR end up with a '+' sign appended to the version string. Fix
this by properly refreshing the index during cleanup.
2016-11-16 02:03:25 -08:00
Josh Poimboeuf
d200d40569 kpatch-build: remove ~/.kpatch/src/.git dir on Fedora
Source RPMs for recent Fedora kernels have a '.git' subdirectory, which
causes '+' to be appended to the module version magic, causing the
module to fail to load:

  kpatch_readdir: version magic '4.8.6-201.fc24.x86_64+ SMP mod_unload ' should be '4.8.6-201.fc24.x86_64 SMP mod_unload '
2016-11-10 16:50:11 -06:00
Joe Lawrence
86ba41ee06 Add kpatch-build --skip-cleanup option
Add a switch to kpatch-build that provides an opt-out to the cleanup
portion of the script.  This can be handy when debugging $TEMPDIR or
$RPMTOPDIR contents, as well as inspecting the patched source code
itself.
2016-09-29 16:30:38 -04:00
Martin Carroll
145d1289dc kpatch-build: do not assume that TEMPDIR is unexported on entry to script
The user's environment might have TEMPDIR exported.  If so, then kpatch-build
dies with a bogus "invalid ancestor" error. If you turn those bogus errors into
warnings, then the script goes on to incorrectly put into the generated .ko file
every single function that was compiled in the *original* kernel build, thereby
producing an immense .ko file with more than 64k sections that the linux kernel
cannot load.  This fix makes sure that TEMPDIR is unexported on the build of the
original kernel.  Actually, this fix uses a separate KPATCH_GCC_TEMPDIR variable,
so that if the kernel build is interrupted, the cleanup function in the kpatch-kbuild
script will still have TEMPDIR set correctly.

Signed-off-by: Martin Carroll <martin.carroll@alcatel-lucent.com>
2016-07-25 11:02:37 -04:00
Josh Poimboeuf
6b03bc8ec0 create-diff-object: fix WARN*_ONCE detection on newer kernels
This fixes the detection of WARN_ON_ONCE, WARN_ONCE, and WARN_TAINT_ONCE
on Linux 4.6 and newer.

The signature for those macros changed with upstream Linux commit
dfbf2897d004 ("bug: set warn variable before calling WARN()").

Fixes #602.
2016-07-13 16:53:02 -05:00
Jessica Yu
a343edcff0 kpatch-elf: make is_bundleable() a static function
Since is_bundleable() is only called once by kpatch_create_symbol_list(),
and no other kpatch-build tool will need to call this function, we can
simply make it static and local to kpatch-elf.c
2016-07-12 14:45:18 -07:00
Jessica Yu
adcd4581cc kpatch-elf: introduce a common kpatch-elf and logging interface
Introduce a common kpatch elf api by moving all functions and struct
declarations related to manipulating kpatch_elf objects from
create-diff-object to kpatch-elf.{h,c}. Move logging macros to a separate
file log.h, and have kpatch-elf.h include it. These changes will generalize
the kpatch-elf and logging api and make it available to other kpatch-build
tools.
2016-07-12 14:45:16 -07:00
Jessica Yu
cfe9aff51e create-diff-object: include .altinstr_replacement if .altinstructions is included
Including the .altinstr_replacement section by itself and without
.altinstructions doesn't make sense, as it only serves as a memory area to
hold replacement instructions to be copied over when alternatives are
applied. Don't include .altinstr_replacement unconditionally and only
include it when .altinstructions is also marked as included.
2016-07-01 12:33:28 -07:00
Quey-Liang Kao
f4686ee7df kpatch-build: Add "CONFIG_DEBUG_KERNEL" kernel config checking
While the officially supported distributions all have
CONFIG_DEBUG_KERNEL enabled, this is not true for some other
distributions.
This option is necessary when kpatch-build retrieves the
SPECIAL_VARS using readelf command.

Signed-off-by: Quey-Liang Kao <s101062801@m101.nthu.edu.tw>
2016-05-03 04:49:30 +08:00
Evgenii Shatokhin
6a76da9236 kpatch-build: fix building of livepatch-based patches
kpatch-build currently requires Module.symvers for the Kpatch core
module unconditionally and fails if it is not found. This does not allow
using kpatch-build to prepare livepatch-based patches.

This patch fixes the problem.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2016-03-22 16:14:07 +03:00
Jessica Yu
f80c2cf47e Merge pull request #582 from libin2015/process-the-patch-name
kpatch-build: process the patch name correctly
2016-03-01 15:30:36 -08:00
Li Bin
441ab87643 kpatch-build: process the patch name correctly
Process the patch name correctly that only concern the fuffix with
.patch or .diff. Otherwise if the patch name is not end with .patch
or .diff but has it as substring, the fuffix will be removed
unreasonably.

Signed-off-by: Li Bin <huawei.libin@huawei.com>
2016-02-27 11:24:27 +08:00
Chris J Arges
b64ab2b5e4 livepatch-patch-hook: add support for livepatch sympos
Support patching objects that have duplicated function names. This feature was
introduced upstream in Linux v4.5.

This patch appends the symbol position to the symbol structure when
lookup_local_symbol is called. This pos variable is then used when creating the
funcs and dynrelas sections. Finally, incorporate sympos into the livepatch
patch hook only if the kernel version is greater than v4.5. In other cases the
older format is used.

Fixes: #493

Signed-off-by: Chris J Arges <chris.j.arges@canonical.com>
2016-02-16 10:31:44 -06:00
Chris J Arges
053622b902 kpatch-build: allow external LDFLAGS
When building binaries such as create-diff-object it would be useful
to be able to pass LDFLAGS when running make from the command line.
2016-01-11 14:26:50 -06:00
Seth Jennings
b60d3acddb Merge pull request #560 from euspectre/get-kernel-version
Get kernel version from vmlinux if the kernel source tree is used
2015-11-18 15:28:42 -06:00
Seth Jennings
b781c0a843 Merge pull request #564 from jpoimboe/more-static-fixes
create-diff-object: static local uncorrelation/correlation fixes
2015-11-18 15:15:27 -06:00
Josh Poimboeuf
02d3c193ed create-diff-object: static local uncorrelation/correlation fixes
The uncorrelation logic is incomplete.  For bundled symbols, in addition
to uncorrelating the sections, it should also uncorrelate the section
symbols and any rela sections.

Similarly the correlation logic needs to correlate section symbols.  (It
already correlates rela sections.)
2015-11-18 14:56:02 -06:00
Josh Poimboeuf
707435ec62 Revert "kpatch-build: fix gcc_version_check"
This reverts commit 9fedd0d283.
2015-11-18 14:44:45 -06:00
Josh Poimboeuf
7b48c4ce12 Revert "kpatch-build: fix gcc_version_check: both "GNU" and "GCC" are possible"
This reverts commit 5737028667.
2015-11-18 14:44:26 -06:00