Commit Graph

2111 Commits

Author SHA1 Message Date
Artem Savkov
5698d868e6 Update version to v0.9.5
Updating version in preparation for rhel9 support.

Changes since v0.9.4:
- openEuler support
- kpatch-build: Do not check KLP_REPLACE for kpatch.ko-based patches
- create-diff-object: fix use after free in kpatch-check-relocations()
- kpatch-build: Handle error in create-klp-module
- create-diff-object: support ppc64le relative jump labels
- kmod/patch: clean only rebuildable objs
- kpatch-build: save environment varibles to file

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-10-13 13:53:47 +02:00
Artem Savkov
912c2cbd17
Merge pull request #1130 from joe-lawrence/hook-hacking-helpers
Hook hacking helpers
2021-10-13 13:45:29 +02:00
Joe Lawrence
f5175a402b kpatch-build: save environment varibles to file
The kpatch-build script sets and exports a bunch of environment varibles
like KCFLAGS as well as data structure sizes in <STRUCT>_SIZE.

Dump the current environment to $CACHEDIR/tmp/kpatch-build.env so these
values can be more easily reused later:

	% source <PATH>/kpatch-build.env && <command>

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-10-13 09:01:46 +02:00
Joe Lawrence
03893f669e
Merge pull request #1222 from joe-lawrence/ppc64le-relative-jump-labels
create-diff-object: support ppc64le relative jump labels
2021-10-05 17:26:54 -04:00
Joe Lawrence
6673c693d6 kmod/patch: clean only rebuildable objs
Relax the clean target to leave the kpatch-build generated output.o file.
This is helpful after invoking kpatch-build in debug mode and rebuilding
$CACHEDIR/tmp/patch in a debugging session.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-10-01 15:33:00 +02:00
Joe Lawrence
607ee53e1e livepatch-patch-hook: add comments
Add some commentary to the livepatch-patch-hook file to explain what
it's doing.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-10-01 15:29:13 +02:00
Joe Lawrence
4f51ee7fa3 create-diff-object: support ppc64le relative jump labels
RHEL-9 integration tests revealed that the kernel now makes use of
R_PPC64_REL64 relocations in the jump table, but need_dynrela() contains
code to specifically skip any R_PPC64_REL64 type when determining if a
relocation should be turned into dynrela.

Kamalesh Babulal explains:

  I tried digging a little deeper and the upstream Kernel commit
  b0b3b2c78ec (powerpc: Switch to relative jump labels) in v5.13,
  introduced the change of generating relocation entries of type
  R_PPC64_REL64, instead of absolute relocation type R_PPC64_ADDR64:

  Relocation section '.rela__jump_table' at offset 0x1a87d8 contains 303 entries:
      Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
  ...
  00000000000003c8  000007910000002c R_PPC64_REL64          0000000000000000 __tracepoint_netif_receive_skb + 8
  ...

Relax the existing check in need_dynrela() for .rela__jump_table
R_PPC64_REL64 relocations in case we need dynrelas for them.

Fixes: #1212
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-09-28 12:49:23 -04:00
Joe Lawrence
cc5200fbf8
Merge pull request #1226 from sumanthkorikkar/handle-error-kpatch-build
kpatch-build: Handle error in create-klp-module
2021-09-28 08:15:12 -04:00
Sumanth Korikkar
cb23c601f6 kpatch-build: Handle error in create-klp-module
create-klp-module could error out and in this case ensure that
kpatch-build does not proceed further.

Signed-off-by: Sumanth Korikkar <sumanthk@linux.ibm.com>
2021-09-26 22:57:41 +02:00
Joe Lawrence
f77491d570
Merge pull request #1211 from joe-lawrence/kpatch-check-relocations-use-after-free
create-diff-object: fix use after free in kpatch-check-relocations()
2021-09-15 10:31:11 -04:00
Joe Lawrence
ef0ce9715a create-diff-object: fix use after free in kpatch-check-relocations()
Building data-read-mostly.patch on rhel-9.0-beta for ppc64le leads to a
segmentation fault:

    Program received signal SIGSEGV, Segmentation fault.
    kpatch_check_relocations (kelf=0x10040490) at create-diff-object.c:2571
    2571                                    sdata = rela->sym->sec->data;
    (gdb) bt
    (gdb) p rela->sym->sec->data
    Cannot access memory at address 0x160000007e

Valgrind narrows the problem down to invalid reads through rela->sym in
kpatch-check-relocations().

The culprits are kpatch_create_intermediate_sections(), which marks
symbols referenced by rela sections that are now dynrelas to be
stripped, and kpatch_strip_unneeded_syms(), which removes and frees
them.

The problem with the symbol stripping is that multiple relas may
reference the same ELF symbol.  If any remaining relocation references a
shared symbol, we must keep it.

Replace the symbol->strip boolean with an enumeration:

  SYMBOL_DEFAULT - initial value, symbol usage unknown
  SYMBOL_USED    - symbol is definitely used by a rela
  SYMBOL_STRIP   - symbol was only referenced by dynrela(s)

Allow transitions from SYMBOL_DEFAULT to SYMBOL_* and SYMBOL_STRIP to
SYMBOL_USED, but _not_ SYMBOL_USED to SYMBOL_*.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-09-14 22:54:09 -04:00
Artem Savkov
6ba8551546
Merge pull request #1220 from euspectre/old-core-replace-nofail
kpatch-build: Do not check KLP_REPLACE for kpatch.ko-based patches
2021-09-13 11:18:44 +02:00
Joe Lawrence
25ea21d217
Merge pull request #1219 from gouhao/master
add openEuler build support
2021-09-10 20:36:26 -04:00
Evgenii Shatokhin
70b8fe8f95 kpatch-build: Do not check KLP_REPLACE for kpatch.ko-based patches
After commit 17dcebf077 ("kpatch-build: enable klp with replace option by default"),
building the old-style (kpatch.ko-based) patches fails with the following
error:
"kpatch core module (kpatch.ko) does not support replace, please add -R|--non-replace"

kpatch.ko actually supports atomic replacement of patches but KLP_REPLACE
has nothing to do with that. Let us not break such builds and remove that check.

Fixes: 17dcebf077 ("kpatch-build: enable klp with replace option by default")
Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2021-09-10 19:21:11 +03:00
gouhao
eaaced1912 add openEuler build support 2021-09-08 09:37:08 +08:00
Yannick Cote
6de60e79b7
Merge pull request #1215 from yhcote/master
Bump to v0.9.4
2021-08-26 15:31:07 -04:00
Yannick Cote
bff258754c
Bump to v0.9.4
Interesting changes since v0.9.3:

- Support for multiple source files
- Makefile tweaks for handling non-replace kpatch building
- Support CONFIG_PRINTK_INDEX
- kpatch-build: set EXTRAVERSION and not localversion for RH kernels
- Make sure section symbols exist
- create-diff-object: Check that the section has a secsym
- kpatch: rmmod module of the same name before loading a module
- kpatch-build: enable option -R|--replace to build replace klp
- kpatch: use /sys/kernel/kpatch/ to check whether core module is loaded
- kpatch: Sync signal subcmd usage output with manpage
- fixes for the out-of-range relocation check

Signed-off-by: Yannick Cote <ycote@redhat.com>
2021-08-25 22:24:21 -04:00
Artem Savkov
8f7e7c21b5
Merge pull request #1204 from sm00th/files
Support for multiple source files
2021-08-24 09:05:27 +02:00
Joe Lawrence
1ca8e8fc1f
Merge pull request #1205 from joe-lawrence/atomic-replace-fixes
Makefile tweaks for handling non-replace kpatch building
2021-08-17 12:45:44 -04:00
Artem Savkov
941a96742d Include unit-tests for multifile case
Update unit-test submodule pointer to include a multi-file unit-test.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:38:30 +02:00
Artem Savkov
99542e864e create-diff-object: rename arguments in most correlate/compare functions
While theoretically most of these functions can work both ways right now
all calls follow the same pattern: first argument is orig element and
second is patched element. Rename the arguments so that these functions
are used in the same fashion going forward. This allows us to cut some
corners such as removing the elseif statement in kpatch_correlate_symbol().

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
22e16619e0 create-diff-object: base->orig renames
Rename "base" to "orig" when referencing object files and their contents
to be consistent with temporary directory structure.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
e2b50d7b66 create-diff-object: make locals_match() and maybe_discarded_sym() return bool
Change the return type of locals_match() and maybe_discarded_sym() to
better reflect their usage.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
720768767d Switch to per-file lookup table pointers.
So far create-diff-object worked only with objectfiles built from a
single source file. To support object-files built from multiple sources
such as linked vmlinux.o, we call locals_match() for each of STT_FILE
symbols and store the pointer to the beginning of appropriate symbol
block in lookup table in each symbol.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
db442d1405 Make lookup_symbol() accept struct symbol as an argument
At the moment lookup_symbol() takes symbol name as an argument which
might not be enough in some cases (e.g. for objectfiles built from
multiple source files). Make it accept full symbol structure.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
a132000868 test/integration/rhel-8.0: disable unneeded test
gcc-static-local-var-4.patch is disabled on this distribution, disable
the test as well as it will always fail during 'slow' integration test
runs.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-08-17 09:37:44 +02:00
Artem Savkov
edf6613870
Merge pull request #1207 from jpoimboe/config-printk-index
Support CONFIG_PRINTK_INDEX
2021-08-17 09:36:43 +02:00
Josh Poimboeuf
56471ffc7c kpatch-build: Support CONFIG_PRINTK_INDEX, part 2
For each printk() call site, CONFIG_PRINTK_INDEX makes a static local
struct named `_entry`, and then adds a pointer to it in the
`.printk_index` section.

When regenerating the `.printk_index` section for the patch module, we
only need to include those entries which are referenced by included
functions.  Luckily this is a common pattern already used by several
other "special" sections.  Add `.printk_index` to the special section
handling logic.

Fixes: #1206

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-08-11 08:47:04 -07:00
Josh Poimboeuf
6cf50a6fca create-diff-object: Support CONFIG_PRINTK_INDEX, part 1
CONFIG_PRINTK_INDEX creates a static local struct variable named
`_entry` for every call site to printk().  The initializer for that
struct assigns the `__LINE__` macro to one of its fields.

Similarly to the WARN macro's usage [1] of `__LINE__`, it causes
problems because it results in the line number getting directly embedded
in the struct.  If a line is added or removed higher up in the source
file, the `_entry` struct changes accordingly due to a change in the
printk() call site line number.

`_entry` is similar to other "special" static locals, in that we don't
need to correlate the patched version with the original version.  We can
instead just ignore any changes to it.

Any substantial (non-line-number) change to the `_entry` struct would be
a second-order (dependent) effect of a first-order code change, which
would be detected using other means.  In that case the patched version
of `_entry` will be included, due to being referenced by the changed
function.

Fixes: #1206

[1] See kpatch_line_macro_change_only()

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-08-11 08:46:32 -07:00
Josh Poimboeuf
ea0470baa7 create-diff-object: change kpatch_line_macro_change_only() return type to bool
Make kpatch_line_macro_change_only()'s usage more clear by changing its
return type to bool.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-08-10 13:56:58 -07:00
Joe Lawrence
a19c4ed296 testing: pass KPATCH_BUILD_OPTS to kpatch-test
Update the test/integration/Makefile to pass a KPATCH_BUILD_OPTS
variable to kpatch-test.  This allows the user better control over the
kpatch build process, for example, building non-atomic replace .ko files
on kernels that do support atomic-replace:

  % make integration KPATCH_BUILD_OPTS="--non-replace"

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-08-09 17:58:02 -04:00
Joe Lawrence
a02668a0f7 kmod: remove redundant -mcmodel=large on ppc64le
In PR #1205, Kamalesh reports:

  ... I see that the -mcmodel=large flag is being passed twice with
  KBUILD_CFLAGS_MODULE set:

  gcc -Wp,-MMD,/root/.kpatch/tmp/patch/.livepatch-meminfo.mod.o.d ............ -mcmodel=medium .... -I/root/kpatch/kmod/patch -mcmodel=large -fplugin=/root/kpatch/kpatch-build/gcc-plugins/ppc64le-plugin.so ... -DMODULE -mno-save-toc-indirect -mcmodel=large -mcmodel=large -DKBUILD_BASENAME='"livepatch_meminfo.mod"' -DKBUILD_MODNAME='"livepatch_meminfo"' -D__KBUILD_MODNAME=kmod_livepatch_meminfo -c -o /root/.kpatch/tmp/patch/livepatch-meminfo.mod.o /root/.kpatch/tmp/patch/livepatch-meminfo.mod.c.

  I loaded the module built without the KBUILD_CFLAGS_MODULE +=
  -mcmodel=large flag and seems to okay. I guess, we can remove the arch
  specific flag from the Makefile.

Suggested-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-08-09 17:58:02 -04:00
Joe Lawrence
624e5e3b82 kmod: pass CFLAGS_MODULE to module build
The kmod/patch/Makefile defines KBUILD_CFLAGS_MODULE, but it seems that
kbuild doesn't honor it as environment variable.  This is noticed when
attempting to use the kpatch-build --non-replace option: the flag is
added to KBUILD_CFLAGS_MODULE, yet the kernel module build ignores it.

At the same time, the kernel docs suggest passing CFLAGS_MODULE [1], not
KBUILD_CFLAGS_MODULE, from the commandline.  Setup KPATCH_MAKE to pass
these options through that variable.

[1] https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt

Fixes: c14e6e9118 ("kpatch-build: Add PPC64le livepatch support")
Fixes: 17dcebf077 ("kpatch-build: enable klp with replace option by default")
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-08-09 17:58:02 -04:00
Joe Lawrence
58c816ebd4
Merge pull request #1199 from joe-lawrence/kpatch-build-extraversion
kpatch-build: set EXTRAVERSION and not localversion for RH kernels
2021-07-20 11:22:44 -04:00
Artem Savkov
948fb639c6
Merge pull request #1200 from sm00th/secsym
Make sure section symbols exist
2021-07-19 09:00:17 +02:00
Artem Savkov
ae574f6975 Bump unittest objs submodule
Update unittest objects submodule to point to a version that contains
objectfiles to test #1193.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-07-13 17:42:36 +02:00
Artem Savkov
5622e3cc3d Make sure section symbols exist
Binutils recently became much more aggressive about removing unused
section symbols. Since we can not rely on those being available anymore
add additional checks before using them.

Fixes: #1193

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-07-13 17:40:58 +02:00
Artem Savkov
59aabd154e
Merge pull request #1186 from gwelymernans/secsym-pr
create-diff-object: Check that the section has a secsym
2021-07-13 17:40:01 +02:00
Joe Lawrence
a26b2af2a3 kpatch-build: set EXTRAVERSION and not localversion for RH kernels
There are some Red Hat kernel NVR combinations like
"kernel-5.13.0-0.rc4.33.el9.x86_64" that don't work well with our srpm
localversion strategy and end up botching the utsrelease.h file... which
allows for kpatch builds, but the module loader rightly rejects the
vermagic mismatch.

An ordinary rpmbuild sets up the kernel Makefile with:

  # make sure EXTRAVERSION says what we want it to say
  # Trim the release if this is a CI build, since KERNELVERSION is limited to 64 characters
  ShortRel=$(perl -e "print \"%{release}\" =~ s/\.pr\.[0-9A-Fa-f]{32}//r")
  perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -${ShortRel}.%{_target_cpu}${Variant:++${Variant}}/" Makefile

The simplest fix is just adding the version string to the kernel
Makefile EXTRAVERSION as rpmbuild would do (minus the perl voodoo).

Fixes: #1196
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-07-02 16:34:27 -04:00
Artem Savkov
cecf544e8e
Merge pull request #1190 from liu-song-6/kpatch-rmmod-before-insmod
kpatch: rmmod module of the same name before loading a module
2021-06-21 11:03:20 +02:00
Artem Savkov
0331438d7e
Merge pull request #1183 from liu-song-6/replace
kpatch-build: enable option -R|--replace to build replace klp
2021-06-21 11:02:49 +02:00
Song Liu
df1e98f2c7 kpatch: rmmod module of the same name before loading a module
With klp->replace, when a patch is replaced, it is no longer an active
patch, but the module is still loaded with a refcount of 0. If the user
tries to load the patch again, insmod will fail with EEXIT. To avoid
such errors, run a proactive rmmod before loading the module. This is a
no-op if the module is not loaded or is actually in use.

Also, update module_ref_count() to only succeed with refcnt > 1.

Signed-off-by: Song Liu <song@kernel.org>
2021-06-18 12:09:16 -07:00
Joe Lawrence
10abc1bbc3
Merge pull request #1192 from xiejingf/upstream_master
kpatch: use /sys/kernel/kpatch/ to check whether core module is loaded
2021-06-17 16:58:28 -04:00
tim.xjf
39f695bbb2 kpatch: use /sys/kernel/kpatch/ to check whether core module is loaded
Fixes #1187

checking if kpatch_register or klp_enable_patch exists in /proc/kallsyms
might not be reliable when module loading or unloading occurs at the same time.
The kernel implementation about /proc/kallsyms is not guranteed to be consistent.

Signed-off-by: xiejingfeng <xiejingfeng@linux.alibaba.com>
2021-06-12 00:43:45 +08:00
Joe Lawrence
6d90e31172
Merge pull request #1189 from theyoyojo/fixes_1188
kpatch: Sync signal subcmd usage output with manpage
2021-06-08 11:29:08 -04:00
Joel Savitz
17539e71e7 kpatch: Sync signal subcmd usage output with manpage
Fixes: 1188

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
2021-06-07 17:40:30 -04:00
Bill Wendling
ba3defa060 create-diff-object: Check that the section has a secsym
A STT_SECTION symbol is not needed if if it is not used as a relocation
target. Therefore, a section, in this case a debug section, may not have
a secsym associated with it.

Signed-off-by: Bill Wendling <morbo@google.com>
2021-06-03 01:50:16 -07:00
Artem Savkov
cbbdae2a3d
Merge pull request #1185 from jpoimboe/irc-libera
README.md: Migrate #kpatch IRC channel to libera.chat
2021-05-27 09:21:56 +02:00
Josh Poimboeuf
87ca62d8de README.md: Migrate #kpatch IRC channel to libera.chat
Move the #kpatch channel to libera.chat due to the freenode implosion.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-05-26 15:40:26 -05:00
Song Liu
4b68be868f kpatch-test: disable replace flag when the kernel doesn't support it
For redhat kernel < 4.18.0-193.el8 or non-redhat kernel version < 5.1,
add -R to $KPATCHBUILD_OPTS.

Signed-off-by: Song Liu <song@kernel.org>
2021-05-25 15:22:27 -07:00