Commit Graph

2194 Commits

Author SHA1 Message Date
Josh Poimboeuf
86d5208b46 create-diff-object: error on symbol conversion failure
If a section reference can't be converted to a symbol reference, error
out to try to prevent unexpected behavior later on.

There are a few sections for which a symbol is optional: .rodata and
string literal sections.  Don't warn about those.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-17 19:27:25 -07:00
Josh Poimboeuf
325bccd89d create-diff-object: skip conversion for sections which never have symbols
These sections don't have symbols.  Don't even try to replace references
to them with symbols.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:02:07 -07:00
Josh Poimboeuf
8508abd3b1 create-diff-object: allow converstion of empty symbols
Empty (zero-length) symbols are possible, allow
kpatch_replace_sections_syms() to work on them.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:02:07 -07:00
Josh Poimboeuf
8d5a628bde create-diff-object: add extra check for symbol conversion edge case
This issue was only seen in in a text section.  Explicitly check for
that.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:02:07 -07:00
Josh Poimboeuf
01427d50a1 create-diff-object: move addend math to a new function
Split out the addend offset math into a separate function so it can be
used elsewhere.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:02:04 -07:00
Josh Poimboeuf
bec6488af6 create-diff-object: add rela_insn() error check
Error out if the insn can't be found.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:26 -07:00
Josh Poimboeuf
6b1895a6b7 create-diff-object: convert rela_insn() to take normal 'sec'
rela_insn() only cares about the base section.  Convert it to take a
non-rela section as its argument instead of a relasec.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:26 -07:00
Josh Poimboeuf
bf212f7750 create-diff-object: error on unsupported rela in symbol conversion
Error out if an unsupported rela is encountered.  This is more robust
than just ignoring it.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:26 -07:00
Josh Poimboeuf
3f8e1062cc create-diff-object: support R_X86_64_NONE in kpatch_replace_sections_syms()
Add support for R_X86_64_NONE.  With an upstream kernel, it's quite
rare, only used for a few jump labels.  With older kernels it was used
for fentry hooks.  Either way, it should be treated like a PC-relative
relocation.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:26 -07:00
Josh Poimboeuf
79f45d1b0a create-diff-object: fix kpatch_replace_sections_syms() for non-text
It doesn't make sense to disassemble a data section.  That just happened
to work by accident.  PC-relative offsets only need adjusting when
associated with an instruction.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:26 -07:00
Josh Poimboeuf
61e46094b5 create-diff-object: convert function return types to 'bool'
Several functions have a boolean semantic, but don't actually return
bool, which is confusing.  Fix that.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:26 -07:00
Josh Poimboeuf
c24d135f40 create-diff-object: rename "sec" -> "relasec" for rela sections
Several functions expect to take a ".rela" section as an argument.  Make
such cases more clear by renaming "sec" -> "relasec".

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 17:00:25 -07:00
Josh Poimboeuf
3b63456817 kpatch-elf: convert functions to static
These functions are only called locally, convert them to static.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 16:59:54 -07:00
Josh Poimboeuf
0c5a1e7753 kpatch-build: make xtrace output less verbose
With '--debug', most of the xtrace output shows the reading of the
.config and Module.symvers files, which isn't very useful and floods the
rest of the xtrace output.  Temporarily disable xtrace before reading
the files.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-05-11 13:55:56 -07:00
Joe Lawrence
f6e0142b3c
Merge pull request #1265 from joe-lawrence/integration-tests-rhel-9.0
test/integration/rhel-9.0: add kernel-5.14.0-70.13.1.el9_0 tests
2022-05-10 18:32:00 -04:00
Joe Lawrence
4774d070d2 test/integration/rhel-9.0: remove obsolete tests
Clean out any integration tests that no longer exercise their original
use cases.

Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2022-05-06 14:42:11 -04:00
Joe Lawrence
9bf6aba6b1
Merge pull request #1266 from joe-lawrence/integration-tests-rhel-8.6
test/integration/rhel-8.6: add kernel-4.18.0-372.9.1.el8 tests
2022-05-06 14:34:11 -04:00
Josh Poimboeuf
43e91f6b9f
Merge pull request #1268 from yhcote/fix-doc
doc: fix internal link to supported arches
2022-05-05 13:16:13 -05:00
Yannick Cote
7b4e422cba
doc: fix internal link to supported arches
Signed-off-by: Yannick Cote <ycote@redhat.com>
2022-05-04 12:10:41 -04:00
Joe Lawrence
dd11b08659
Merge pull request #1264 from liu-song-6/fix-01
kpatch-build: remove duplicated use_klp_arch
2022-05-02 10:11:08 -04:00
Joe Lawrence
dc1d49a142 test/integration/rhel-9.0: add kernel-5.14.0-70.13.1.el9_0 tests
* gcc-constprop.patch
In v4.20, 33e26418193f ("y2038: make do_gettimeofday() and get_seconds()
inline"), do_gettimeofdat() no longer exists as a stand alone function
in kernel/time/timekeeping.c.

* gcc-static-local-var-4.patch
Unlike on rhel-8.4, _always_inline put_aio_ring_file() is causing too
many inlines and results in modified, but not ftrace-able,
__do_sys_io_submit() and __do_sys_io_setup().  Remove the annotation
from this function.

* module.patch
In v4.20, 9ceddd9da134 ("knfsd: Allow lockless lookups of the exports"),
the nfs_exports_op seq_operations converted to RCU protected lookups.
Calling yield() from a kpatched e_show() results in a kernel warning,
"Voluntary context switch within RCU read-side critical section!"
Substitute with single_task_running(), which does not context switch or
have any other side effects.

In v5.10, ec6347bb4339 ("x86, powerpc: Rename memcpy_mcsafe() to
copy_mc_to_{user, kernel}()") did away with the mcsafe_key.  Use
another exported static key like context_tracking_enabled.

In v5.13, a0e2bf7cb700 ("x86/paravirt: Switch time pvops functions to
use static_call()"), paravirt_sched_clock() was converted from a
paravirt call to a non-exported static call.  Update the x86 code to
instead call __flush_tlb_local() (which is still a paravirt call).

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2022-05-01 11:34:28 -04:00
Joe Lawrence
734a9d9ef5 test/integration/rhel-8.6: add kernel-4.18.0-372.9.1.el8 tests
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2022-04-29 16:14:30 -04:00
Song Liu
3392049665 kpatch-build: remove duplicated use_klp_arch
The two versions are not the same (4.18.0-240.el8 vs. 4.18.0-284.el8).
But I am not quite sure which one is accurate. Remove the first one as the
second one is being used before this change.

Signed-off-by: Song Liu <song@kernel.org>
2022-04-28 11:43:50 -07:00
Josh Poimboeuf
b0444d2599
Merge pull request #1173 from jpoimboe/syscall-macros
Add syscall macros
2022-04-26 11:44:02 -05:00
Josh Poimboeuf
1528c8d298
Merge pull request #1261 from jpoimboe/readme
readme: bring README.md into the 21st century
2022-04-26 11:43:43 -05:00
Josh Poimboeuf
edcac8ac78
Merge pull request #1262 from jpoimboe/unit
test/unit: bump
2022-04-22 11:36:17 -05:00
Josh Poimboeuf
f835cff795 test/unit: bump
Oops, this somehow ended up with a reference to nonexistent commit.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-22 09:33:49 -07:00
Josh Poimboeuf
e6c1c082ce readme: bring README.md into the 21st century
Update a lot of the old cruft and move the boring installation stuff to
a separate file.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-22 09:28:55 -07:00
Josh Poimboeuf
286e85fddf patch-author-guide: Add TOC
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-22 09:10:16 -07:00
Josh Poimboeuf
fe5df04d30 patch-author-guide: Add section about patching syscalls
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-22 08:40:00 -07:00
Josh Poimboeuf
b21b35f7d4 patch-author-guide: Add latest jump label status
Mention that jump labels in patched functions are mostly supported for
Linux 5.8+.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-22 08:38:14 -07:00
Josh Poimboeuf
373993ec1c test/integration: Add tests for patching a syscall
Add ".kpatch" to the end of "uname -s".

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-20 08:35:12 -07:00
Josh Poimboeuf
e9c0b67862 macros: add syscall patching macros
Attempting to patch a syscall results in an error due to a missing
fentry hook in the inner __do_sys##name() function.  The fentry hook is
missing because of the 'inline' annotation, which invokes 'notrace'.

Add some kpatch-specific syscall definition macros which can be used for
patching a syscall.

These macros are copied almost verbatim from the kernel, the main
difference being a 'kpatch' prefix added to the __do_sys##name()
function name.  This causes kpatch-build to treat it as a new function
(due to its new name), and its caller __se_sys##name() function is
inlined by its own caller __x64_sys##name() function, which has an
fentry hook.

To patch a syscall, just use replace the use of the SYSCALL_DEFINE1 (or
similar) macro with the "KPATCH_" prefixed version.

Fixes: #1171

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-19 19:11:44 -07:00
Joe Lawrence
e4c0bb9999
Merge pull request #1260 from joe-lawrence/release-v0.9.6
Bump to version v0.9.6
2022-04-12 15:15:17 -04:00
Joe Lawrence
eea6582fb2 Bump to version v0.9.6
Updates of interest:

v0.9.6:
- Tue Apr 12 Joe Lawrence <joe.lawrence@redhat.com> - 0.9.6
- Allow OOT modules to be built with non-distro kernels
- Add cross-arch unit testing support
- Support ELF extended symbol section indexes
- Allow setting kernel version if --sourcedir and --vmlinux are used
- Cleanup and enhance __LINE__ macro detection for all arches
- Fix segfault on .LCx string literal symbols
- Include __dyndbg section when referenced by jump table
- Honor user provided KBUILD_EXTRA_SYMBOLS
- Support .retpoline_sites section
- Add native compiler selection via CROSS_COMPILE

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2022-04-12 11:13:23 -04:00
Josh Poimboeuf
ef8b270e4d
Merge pull request #1258 from jpoimboe/seg-fault
Fix seg fault caused by missing .LCx symbol
2022-04-07 09:31:52 -05:00
Josh Poimboeuf
8439de4616
Merge pull request #1254 from jpoimboe/dynamic-debug-fixes
Multiple `__LINE__` macro detection fixes and improvements
2022-04-07 09:31:20 -05:00
Josh Poimboeuf
c2e73c2cbc create-diff-object: ensure debug sections don't use dynrelas
Debug sections are intended to refer to the patch module only.  And in
fact, any debug section references to non-included symbols are stripped
in kpatch_include_debug_sections().  So there's no need for
need_dynrela() to even think about it.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-06 13:26:09 -07:00
Josh Poimboeuf
305ff8a5d8 create-diff-object: include .LCx string literal symbols
A seg fault was reported:

  Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7f18c8e in __strcmp_avx2 () from /lib64/libc.so.6
  Missing separate debuginfos, use: dnf debuginfo-install elfutils-libelf-0.186-1.fc34.x86_64 zlib-1.2.11-26.fc34.x86_64
  (gdb) bt
  #0  0x00007ffff7f18c8e in __strcmp_avx2 () from /lib64/libc.so.6
  #1  0x000000000040a0f7 in kpatch_is_core_module_symbol (name=0x0) at create-diff-object.c:3060
  #2  0x000000000040a267 in need_dynrela (kelf=0x4669a0, table=0x92af30, sec=0x6d6b20, rela=0x8c7fd0) at create-diff-object.c:3117
  #3  0x000000000040a4cc in kpatch_create_intermediate_sections (kelf=0x4669a0, table=0x92af30, objname=0x7fffffffcfc6 "vmlinux", pmod_name=0x7fffffffd020 "livepatch_a") at create-diff-object.c:3281
  #4  0x000000000040c7c5 in main (argc=8, argv=0x7fffffffca48) at create-diff-object.c:3931

It happened because 'rela->sym->name' was NULL, and
kpatch_is_core_module_symbol() tried to dereference it.

Here's the corresponding relocation:

  Relocation section [455] '.rela.debug_loclists' for section [454] '.debug_loclists' at offset 0xd0478 contains 2432 entries:
    Offset              Type            Value               Addend Name
    ...
    0x000000000000aad6  X86_64_64       000000000000000000     +32 .LC55
    ...

That '.LC55' symbol lives in the following section:

  [104] .rodata.btf_show_end_aggr_type.str1.8 PROGBITS     0000000000000000 00003ef0 00000021  1 AMS    0   0  8

The problem is that the symbol wasn't included in the output file
(though its corresponding section was).  So it got zeroed by
kpatch_elf_teardown(), which was designed to trigger seg faults to help
find bugs like this.

The string literal sections which hold the '.LCx' string symbols are
already being included.  Include their symbols as well.

Fixes #1257.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-06 13:26:09 -07:00
Josh Poimboeuf
5f87a3cbd0
Merge pull request #1255 from euspectre/archversion-override
kpatch-build: Allow setting kernel version if --sourcedir and --vmlinux are used
2022-04-06 09:47:27 -05:00
Josh Poimboeuf
e9b941a612
Merge pull request #1256 from rosslagerwall/name-len
kpatch-build: Increase name length limit to 55 chars
2022-04-06 09:47:10 -05:00
Josh Poimboeuf
641daaa489 test/unit: bump to master
Pull in unit tests for dyndbg & __LINE__ fixes.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-06 07:34:21 -07:00
Josh Poimboeuf
b700732f5d create-diff-object: add "_printk" to the __LINE__ detection
The __LINE__ detection code looks for "printk", which on newer kernels
has been renamed to "_printk".  Fix the check for newer kernels.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-05 12:14:22 -07:00
Josh Poimboeuf
79fd60b28d create-diff-object: check arg2 and arg3 on all arches for line detection
For x86, we already check arg2 and arg3 for `__LINE__` immediate load
detection.  For parity, do the same thing for the other two arches.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-05 12:14:09 -07:00
Ross Lagerwall
f7b27f2fec kpatch-build: Increase name length limit to 55 chars
Previously, the name length was limited to 48 chars. This was then
prepended with "kpatch-" and a trailing NUL terminator to get to the 56
char limit for kernel module names. After some code rearrangement, the
48 char restriction was applied to the name after being prefixed with
kpatch-/livepatch-, limiting the length more than necessary. Increase
the name length limit back to 55 chars to restore the original limit.

Fixes: c0105ea467 ("kpatch-build: set default module prefix accordingly")
Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-04-05 10:35:12 +01:00
Josh Poimboeuf
e6b1664d0e create-diff-object.c: add s390 support for __LINE__ detection
Technically we don't support s390 yet, but it's coming soon and there's
no harm in merging this one early.  In fact this came in handy for
testing my endian fixes with #1203.

Note it doesn't actually do anything since 'kelf->arch' can't actually
get set to 'S390' yet.  But it should work nicely with #1203 as it
evolves.

This is based on the patch from C. Erastus Toe in #1243, though there
may still be a few outstanding issues to look at in that PR, based on
some of the code review comments.

Originally-by: C. Erastus Toe <ctoe@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-04 21:13:49 -07:00
Josh Poimboeuf
a6920b9381 create-diff-object: fix endian issues in insn_is_load_immediate()
With s390 support coming, we'll soon have to worry about endianness
issues when doing cross-compiles and cross-arch unit tests.  Make
insn_is_load_immediate() endian-agnostic.

Suggested-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-04 21:13:49 -07:00
Josh Poimboeuf
38839017d2 gitignore: ignore .swo files
Sometimes I like to live on the edge and edit the same file in multiple
vims :-)

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-04 21:13:49 -07:00
Josh Poimboeuf
1152e58244 create-diff-object: support __LINE__ detection for btrfs macros
These btrfs macros use the `__LINE__` macro.  Add proper support for
them in kpatch_line_macro_change_only().

   5:   ba 34 09 00 00          mov    $0x934,%edx
   a:   48 c7 c6 00 00 00 00    mov    $0x0,%rsi
                        d: R_X86_64_32S .rodata.__func__.5
  11:   4c 89 ef                mov    %r13,%rdi
  14:   e8 00 00 00 00          callq  19 <push_node_left.cold+0x19>
                        15: R_X86_64_PLT32      __btrfs_abort_transaction-0x4

Fixes #1253.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2022-04-04 21:12:58 -07:00
Evgenii Shatokhin
fea0192104 kpatch-build: Allow setting kernel version if --sourcedir and --vmlinux are used
Using -a/--archversion to explicitly set target kernel version was not
allowed if the kernel source directory and/or path to vmlinux with debug
info were set. This seems too strict, however.

vmlinux with debug info is used during the build to get symbol data, the
size of special structures, GCC version used to build the kernel, and the
target kernel version. As it turned out, some kernels, e.g. the ones from
OpenSUSE and derivatives, contain all that data except the Linux version
string in the file with debug info for vmlinux. Instead, that string is
present in vmlinux itself but that file does not contain debug info.

A simple workaround is to allow specifying the target kernel version
explicitly using -a/--archversion even if --sourcedir and/or --vmlinux are
set. If vmlinux with debug info does contain the Linux version string and
the version is different, kpatch-build will report an error.

Signed-off-by: Evgenii Shatokhin <evgenii.shatokhin@openvz.org>
2022-04-04 13:16:24 +03:00