Commit Graph

2381 Commits

Author SHA1 Message Date
Oleh Matiusha 0decc79d0b kpatch-build: introduce ability to ignore distro-specific checks
Ignore distro-specific checks if kernel source directory is supplied as a
command line argument to remove the distro-specific tunings during
cross-compilation.

Signed-off-by: Ruslan Bilovol <rbilovol@cisco.com>
Signed-off-by: Oleh Matiusha <oleh.matiusha@globallogic.com>
2022-01-21 17:29:53 +02:00
Oleh Matiusha fd0457b9f3 kpatch-build: add cross-compilation support
Introduce the ability to pick a CROSS_COMPILE environment variable for
specifying toolchain prefix for all gcc and binutils. To facilitate its
use, do not set the DISTRO variable when USERSRCDIR is specified.

Signed-off-by: Ruslan Bilovol <rbilovol@cisco.com>
Signed-off-by: Oleh Matiusha <oleh.matiusha@globallogic.com>
2022-01-21 17:29:53 +02:00
Markus Boehme c4286ee2cb create-diff-object: add support for .retpoline_sites section
Commit 134ab5bd1883 ("objtool,x86: Replace alternatives with .retpoline_sites")
in the kernel starts keeping track of retpoline thunk call sites in a
dedicated section rather than via the alternatives mechanism.

The .retpoline_sites section needs to have its entries and relocations
for changed symbols included in the patch ELF when building for kernel
5.16+ with CONFIG_RETPOLINE=y.

Signed-off-by: Markus Boehme <markubo@amazon.com>
2022-01-17 21:54:58 +01:00
Joe Lawrence b65fce5e07
Merge pull request #1246 from Decave/malloc_failure_check
kpatch-build: Add missing malloc failure check
2022-01-17 11:44:02 -05:00
David Vernet 9ee9f7a3ec kpatch-build: Add missing allocation failure checks
In kpatch-build, there are a number of places where a dynamic allocation
is performed, but the allocation is not checked for a failure. The
common pattern in kpatch-build is to check whether the returned pointer
is NULL, and if so, invoke the ERROR() macro to print a message and
abort the program.

kpatch_create_mcount_sections(), CORRELATE_ELEMENT(), and
create_klp_arch_sections() all had dynamic allocations without failure
checks. This diff adjusts those callsites to properly check for a failed
allocation, and ERROR() accordingly.

Signed-off-by: David Vernet <void@manifault.com>
2022-01-14 07:32:54 -08:00
Joe Lawrence 71fe6c52ee
Merge pull request #1245 from sm00th/oot_fix
Fix oot-module builds
2021-12-17 14:51:46 -05:00
Joe Lawrence 774ede4e88
Merge pull request #1237 from joe-lawrence/CONFIG_PRINTK_INDEX
kpatch-build: refactor kernel config and special section parsing
2021-12-17 14:50:48 -05:00
Joe Lawrence bdc43561e9 kpatch-build: combine find_special_section_data() variants
find_special_section_data() and find_special_section_data_ppc64le() are
almost identical.  Refactor the code so that a single function serves
both (and future) architectures without all the code duplication.

The inline awk script algorithm remains the same, only flip the "skip_x"
logic to "check_x" logic for readability.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-12-17 14:47:59 -05:00
Joe Lawrence 34fcfd4d56 kpatch-build: source kernel .config instead of grepping
Rather than individually (re)grepping the kernel configuration for
CONFIG_ options, source the entire file to import all the settings in
one shot.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-12-17 14:47:54 -05:00
Artem Savkov 89a871fff9 Fix oot-module builds
KPATCH_BUILD is used in kpatch-module build to specify the kernel
directory and thus needs to be set to KERNEL_SRCDIR, not BUILDDIR.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-12-16 14:42:28 +01:00
Joe Lawrence b02330f56e
Merge pull request #1241 from joe-lawrence/already_done-static-local
create-diff-object: update for __already_done
2021-12-15 17:50:13 -05:00
Artem Savkov 16e447ad09
Merge pull request #1242 from sm00th/oot_refactor
OOT-related updates
2021-12-15 08:59:22 +01:00
Artem Savkov aa85922ec6 Do not unset KBUILD_EXTRA_SYMBOLS
kpatch-build currently initializes KBUILD_EXTRA_SYMBOLS to "", denying
users ability to set it themselves.

Fixes: #1238

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-12-13 18:52:19 +01:00
Artem Savkov 4a7dab17c8 Allow OOT modules to be built with non-distro kernels
Previously OOT-module builds used `--sourcedir` to specify oot-module
source directory which was a bit confusing and also denied users ability
to build kpatches agains oot modules built against non-distro kernels.

This patch adds `-p/--oot-module-src` option to specify source dir for
oot module while keeping `--sourcedir` for kernel source directory
specification.

I also tried to disambiguate `SRCDIR` in kpatch-build a bit. Now there
are 3 variables:
 - `KERNEL_SRCDIR` - contains path to kernel source directory
 - `OOT_MODULEL_SRCDIR` - contains path to out-of-tree module source directory
 - `BUILDDIR` - can be set to either of the above and is used for
   patch-related actions

Another attempt at this was done by @omatiusha in #1234

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-12-13 18:51:52 +01:00
Joe Lawrence 6c4c8c0a2d create-diff-object: update for __already_done
Upstream v5.14+ kernel change a358f40600b3 ("once: implement
DO_ONCE_LITE for non-fast-path "do once" functionality") consolidated a
bunch of do-once macros into a common macro:

  #define DO_ONCE_LITE_IF(condition, func, ...)				\
  	({								\
  		static bool __section(".data.once") __already_done;	\
  		...

which replaced static local variable __warned with __already_done.

Update any __warned static local checks to also look for the new
__already_done variable as well.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-12-08 11:05:31 -05:00
Artem Savkov a9b82fee61
Merge pull request #1239 from sm00th/tintup-ppc64le
test/integration/linux-5.10.11: fix patches to build on ppc64le
2021-12-01 09:15:25 +01:00
Artem Savkov 6fa8e4fd43 test/integration/linux-5.10.11: fix patches to build on ppc64le
module-call-external.patch and new-function.patch fail to build on
ppc64le with "Found an unsupported sibling call". Add
fno-optimize-sibling-calls attributes to functions in question.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-11-19 10:12:12 +01:00
Artem Savkov 4975ae8b97
Merge pull request #1235 from keiya-nobuta/test-devel
kpatch-test: Change DYNDEBUG_CONTROL setting to use sudo
2021-11-15 12:15:40 +01:00
Keiya Nobuta 7612b4da68 kpatch-test: Change DYNDEBUG_CONTROL setting to use sudo
Depending on environment, "Permission denied" error may occur
when /sys/kernel/debug/dynamic_debug/control setting.

Signed-off-by: Keiya Nobuta <nobuta.keiya@fujitsu.com>
2021-11-02 15:22:03 +09:00
Artem Savkov 61fc3f1077
Merge pull request #1232 from sm00th/9.5_update
Update version to v0.9.5
2021-10-13 15:28:06 +02:00
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