Commit Graph

875 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
Song Liu
17dcebf077 kpatch-build: enable klp with replace option by default
Since 5.1 kernel, klp_patch supports a "replace" option, which does atomic
replace of cumulative patches. Enable building such patch by default. If
replace behavior is not desired, the user can use -R|--non-replace option
to disable it.

Signed-off-by: Song Liu <song@kernel.org>
2021-05-25 15:18:36 -07:00
Joe Lawrence
97b69bee75
Merge pull request #1172 from jpoimboe/out-of-range-rela-fix
A couple of fixes for the out-of-range relocation check
2021-05-05 12:35:26 -04:00
Joe Lawrence
c819391c26
Merge pull request #1174 from joe-lawrence/rhel-8.4-support
Additional rhel 8.4 support
2021-04-20 14:19:57 -04:00
Joe Lawrence
d9f49e850a kpatch-build: drop klp.arch support in RHEL-8.4
RHEL-8.4 backported the "livepatch,module: Remove .klp.arch and
module_disable_ro()" patchset, so update kpatch-build accordingly.

Link: https://lore.kernel.org/live-patching/nycvar.YFH.7.76.2005080016330.25812@cbobk.fhfr.pm/T/#t
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-04-20 11:17:56 -04:00
Josh Poimboeuf
81f9ca4833 create-diff-object: Fix out-of-range relocation check
Improve the relocation check for the case where the referenced symbol
isn't at the beginning of the section.

Note that the check still isn't perfect, as many relocations have a
negative addend.  But it's still a lot better than nothing.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-04-13 14:04:54 -05:00
Josh Poimboeuf
fa5a95cafd create-diff-object: Fix out-of-range relocation error message
Showing sec+addend isn't valid, show sym+addend instead.

Before:

  create-diff-object: ERROR: sys.o: kpatch_check_relocations: 2550: out-of-range relocation .rodata.__kpatch_do_sys_uname.str1.1+139 in .rela.text.__kpatch_do_sys_uname

After:

  create-diff-object: ERROR: sys.o: kpatch_check_relocations: 2550: out-of-range relocation .LC7+139 in .rela.text.__kpatch_do_sys_uname

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-04-13 13:58:59 -05:00
Joe Lawrence
453fb1b97a
Merge pull request #1167 from jpoimboe/static-call
kpatch-build: add support for static calls
2021-03-24 13:47:59 -04:00
Josh Poimboeuf
7be75549ba kpatch-build: add support for static calls
Add the new .static_call_sites special section.

Fixes #1165.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-03-24 10:37:44 -05:00
Joe Lawrence
6d466fa76f kpatch-build: drop klp.arch support in RHEL-8.4
RHEL-8.4 backported the "livepatch,module: Remove .klp.arch and
module_disable_ro()" patchset [1], so update kpatch-build accordingly.

[1] https://lore.kernel.org/live-patching/nycvar.YFH.7.76.2005080016330.25812@cbobk.fhfr.pm/T/#t

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-03-18 21:58:57 -04:00
Joe Lawrence
f9d2ec1d77 kpatch-build: fix kernel_is_rhel() for rhel-8 z-stream kernels
During review of PR #1163, Josh reported:

  When trying this out, I think I found that kernel_is_rhel() is broken
  for z-stream kernels. It expects a "." after the el8, like ".el8.",
  but [rhel-8] z-stream kernels have the minor version appended to the
  major version like ".el8_3".

Tweak the regex pattern in kernel_is_rhel() to account for such z-stream
kernel versions.  At the same time, add .el9 to the regex in preparation
of rhel-9.

Reported-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-03-18 21:58:01 -04:00
Artem Savkov
6321c9ab5e
Merge pull request #1162 from sm00th/cold-noid
support for .cold functions with no id suffix
2021-03-09 10:26:33 +01:00
Artem Savkov
1b5a17f934 create-build-diff: support for .cold functions with no id suffix
create-build-diff expects .cold functions to be suffixed by an id, which
is not always the case. Drop the trailing '.' when searching for cold
functions.

Fixes: #1160

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-03-04 15:55:58 +01:00
Artem Savkov
fc4a56f6b2 kpatch-build: check existance of Makefile.modfinal
It turns out there is a stretch of time in kernel's history when
CONFIG_DEBUG_INFO_BTF was already added, but Makefile.modfinal wasn't
split off yet. To address those we need to either check the file's
existance or, as @liu-song-6 suggested initially, check config for
CONFIG_DEBUG_INFO_BTF_MODULES=y.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-03-03 09:13:30 +01:00
Song Liu
4cc2f06279 kpatch-build: workaround pahole and CONFIG_DEBUG_INFO_BTF_MODULES
With CONFIG_DEBUG_INFO_BTF_MODULES, the kernel build process adds BTF to
each in-tree modules. However, this process is broken with kpatch, with
error message like:

  Failed to parse base BTF 'vmlinux': -4001

Unblock build with CONFIG_DEBUG_INFO_BTF_MODULES with similar workaround
as the one for CONFIG_DEBUG_INFO_BTF.

Signed-off-by: Song Liu <songliubraving@fb.com>
2021-03-02 12:42:39 -08:00
Artem Savkov
6c83b642a4 create-diff-object: make digit tail optional in kpatch_mengled_strcmp
Make kpatch_mangled_strcmp treat two strings as the same in case when
one has a digit tail and the other one doesn't.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-02-19 12:41:12 +01:00
Artem Savkov
b6e1d071ff create-diff-object: consider '.L' symbols not static local
Make kpatch_is_normal_static_local() treat all symbols prefixed by '.L'
as not static locals.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-02-19 12:41:12 +01:00
Artem Savkov
fddc6242c6 create-diff-object: support both gcc and clang style of static variables
gcc-generated static variables always have a numbered suffix, while
clang-generated static variables are always prepended with a function
name. Change is_special_static() so that it detects both cases.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-02-19 12:41:09 +01:00
Artem Savkov
38c2d7315d create-diff-object: clang __UNIQUE_ID symbol support
clang does not always use __UNIQUE_ID as prefix and can generate symbols
with names like this:
	trace_nfsd_exp_get_by_name.__UNIQUE_ID___addressable___SCK__tp_func_nfsd_exp_get_by_name645

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-02-17 12:59:54 +01:00
Artem Savkov
488f30c31b create-diff-object: .L.str* symbols support
Clang adds .L.str* symbols to .rodata.str sections which are used for
__FILE__ references. These are discarded during linking so add them to
maybe_discarded_sym().

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-02-17 12:59:54 +01:00
Artem Savkov
41de9e763a kpatch-build: clang support
Add support for clang-built kernels. This is completely automatic, we
check if the kernel was built with clang by looking for
CONFIG_CC_IS_CLANG in config.

This has almost no effect on non-clang built kernels with one exception:
we now do compliler checks _after_ we download kernel sources which is a
waste of resources in case when compilers don't match.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2021-02-17 12:59:51 +01:00
Joe Lawrence
2af31916ce
Merge pull request #1155 from joe-lawrence/CONFIG_DEBUG_INFO_BTF
kpatch-build: workaround pahole and CONFIG_DEBUG_INFO_BTF
2021-02-03 21:37:19 -05:00
Joe Lawrence
f951c1a92e kpatch-build: apply 'cp -f ... || die' pattern
Be robust and use "cp -f".  Finish with "|| die" to be dead serious
about it.

Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2021-02-02 17:07:24 -05:00
Joe Lawrence
e25450f5a8 kpatch-build: workaround pahole and CONFIG_DEBUG_INFO_BTF
kpatch-build requires gcc flags -f[function|data]-sections when building
original and patched targets.  These flags result in an ELF binary with
many sections, potentially requiring special extended ELF header
processing to parse correctly.

CONFIG_DEBUG_INFO_BTF invokes pahole as part of the kernel build and
unfortunately pahole cannot iterate through more than 65535 section
headers.  As result, the pahole program segfaults and fails the build
like so:

  ...
    BTF     .btf.vmlinux.bin.o
  scripts/link-vmlinux.sh: line 127: 718345 Segmentation fault      (core dumped) LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
  objcopy: --change-section-vma .BTF=0x0000000000000000 never used
  objcopy: --change-section-lma .BTF=0x0000000000000000 never used
  objcopy: error: the input file '.btf.vmlinux.bin' is empty
  Failed to generate BTF for vmlinux
  Try to disable CONFIG_DEBUG_INFO_BTF
  make: *** [Makefile:1050: vmlinux] Error 1

Workaround this limitation by disabling CONFIG_DEBUG_INFO_BTF code in
scripts/vmlinux-link.sh during kpatch-build.  This leaves
CONFIG_DEBUG_INFO_BTF contingent kernel code in place, but skips the
problematic pahole .BTF typeinfo generation step (for which kpatch
doesn't care about anyway).

Link: https://lore.kernel.org/dwarves/20210119231718.GA3173@redhat.com/T/
Link: https://lore.kernel.org/dwarves/20210121202203.9346-1-jolsa@kernel.org/T/
Link: https://lore.kernel.org/dwarves/20210122163920.59177-1-jolsa@kernel.org/T/
Link: https://lore.kernel.org/dwarves/b1469725-d462-9a6d-3329-f77c9eb6b43f@redhat.com/T/

Fixes: #1153
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
2021-02-02 17:06:53 -05:00
Kamalesh Babulal
387914b683 kpatch-gcc: powerpc - skip vdso files
Starting v5.11-rc, kpatch-build fails on powerpc with the error:

ERROR: invalid ancestor arch/powerpc/kernel/vdso64/vdso64.so.dbg for arch/powerpc/kernel/vdso64/vgettimeofday.o

the upstream commit ab037dd87a2f(powerpc/vdso: Switch VDSO to generic
implementation) introduced this breakage, lets skip vdso files. They are
not compatible with kpatch.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2021-01-20 12:32:03 +05:30
WANG Chao
aea2cb96d6 create-diff-object: fix __dyndbg section inclusion
__verbose has renamed to __dyndbg since Linux 5.9, commit e5ebffe18e5a
("dyndbg: rename __verbose section to __dyndbg")

Signed-off-by: WANG Chao <chao.wang@ucloud.cn>
2020-10-27 15:25:36 +08:00
Joe Lawrence
ef420050e3 kpatch-elf: pass new ELF output file mode to kpatch_write_output_elf()
Currently all the callers of kpatch_write_output_elf() are creating
.o object files or .ko kernel modules.  Neither of these filetypes are
executable on their own, so enhance kpatch_write_output_elf() to accept
file creation mode and update its callers to pass 0664 to match
the expected permissions.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2020-09-25 09:30:13 -04:00
Joe Lawrence
25f12681fa create-diff-object: cleanup maybe-uninitialized compiler complaints
User disaster123 reports the following build errors:

  create-diff-object.c: In function 'kpatch_process_special_sections':
  create-diff-object.c:2215:41: error: 'key' may be used uninitialized in this function [-Werror=maybe-uninitialized]
         code->sym->name, code->addend, key->sym->name);
                                           ^~
  create-diff-object.c:2138:22: note: 'key' was declared here
    struct rela *code, *key, *rela;
                        ^~~
  In file included from kpatch-elf.h:26,
                   from create-diff-object.c:53:
  log.h:20:3: error: 'code' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     printf(format, ##__VA_ARGS__); \
     ^~~~~~
  create-diff-object.c:2138:15: note: 'code' was declared here
    struct rela *code, *key, *rela;
                 ^~~~
  cc1: all warnings being treated as errors

These are reproducible when building with 9.3.1 and 8.3.1 when building
with optimization level > 2 ( CFLAGS=-O2 make ).  Fix them by
initializing the reported variables to NULL and verifying that they are
infact non-NULL after processing the __jump_table.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2020-09-09 14:28:44 -04:00
Julien Thierry
08056febfd kpatch-build: Check Final module symbols for loadability
If the final module has a reference in its symbol table to a kernel
symbol and the symbol version differs from the kernel symbol version,
the module will be unloadable.

Have kpatch-build emit a clear error and die in such a case instead
of providing an unusable module.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-08-17 12:38:32 +01:00
Julien Thierry
e9600c5513 kpatch-build: Check exported symbol version changes
The CRCs of exported symbols Module.symvers can differ between the
original build and the patched build.

In such a case, it is probably wise to rework the patch to avoid such
modifications.

Warn when a symbol changes version in the exported symbol list.

Fixes issue #1084

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-08-17 12:38:32 +01:00