Commit Graph

810 Commits

Author SHA1 Message Date
Joe Lawrence
5331463768
Merge pull request #1102 from joe-lawrence/jp-ppc-mcount
create-diff-object: Add ppc64le mcount support
2020-05-18 09:20:40 -04:00
Josh Poimboeuf
b958ed601c create-diff-object: Add ppc64le mcount support
Add the __mcount_loc section on ppc64le.  It has pointers to all the
mcount calls.  This will enable the ftrace hook to be used for patched
functions.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com> (rebased)
2020-05-18 08:35:33 -04:00
Evgenii Shatokhin
9bb75659e2 kpatch-build: Detect R_X86_64_64 dynrelas with large addends
Or, to be exact, with addend values which cannot be represented by
a signed int variable.

This only applies to the old KPatch core.

Commit 15067fcd "kmod/core: apply dynrela addend for R_X86_64_64" fixed
calculation of the values for R_X86_64_64 dynrelas. This revealed
another issue, similar to https://github.com/dynup/kpatch/issues/1064.

Dynrelas are stored as 'struct kpatch_patch_dynrela' instances in the
patch module but both the patch module and kpatch.ko use
'struct kpatch_dynrela' to work with the dynrelas. 'addend' has type
'long' in kpatch_patch_dynrela but 'int' in kpatch_dynrela, so this
value can be truncated when read.

R_X86_64_64 dynrela can be created, for example, if a patch for vmlinux
refers to something like '(unsigned long)&idt_table+0x80000000' (a global
variable which is not exported, with some addend).
The addend == +0x80000000, however, effectively becomes 0xffffffff80000000
(== -0x80000000) due to this bug.

Unfortunately, 'struct kpatch_dynrela' is a part of the ABI between
kpatch.ko and patch modules. Plain changing 'int addend' into 'long addend'
there could be problematic. The patch module built using the new
'struct kpatch_dynrela' will either fail to load if kpatch.ko is using the old
'struct kpatch_dynrela' or cause crashes or data corruptions. Unloading
and reloading patch modules and kpatch.ko is not always an option
either.

Luckily, R_X86_64_64 dynrelas seem to be quite rare in the production
patch modules and R_X86_64_64 dynrelas with large addends are expected
to be even more rare.

So, instead of fixing the truncation of addends right away, I propose to
detect it, for now, when building a patch. If one never hits such conditions,
it is not worth it to fix the issue. If R_X86_64_64 dynrelas with large
addends do happen and cannot be avoided, we can try to figure out how to
fix this properly, without breaking too much.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2020-05-05 23:21:16 +03:00
Julien Thierry
c1caee1468 create-diff-object: Ignore kpatch_ignored functions/sections missing ftrace hook
Some theoretically unchanged functions can have undesired changes if the
compiler decides to perform inlining in a different way (e.g. because of
newly added references). In such a case, it can be useful to discard
changes to functions that don't actually need modification.

Sadly, this currently doesn't work for functions missing the ftrace hook
(e.g. notrace code) as presence of the hook is checked before
identifying elements to ignore.

Look for functions/sections to ignore earlier.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-04-23 08:22:50 +01:00
Josh Poimboeuf
1991ff0018 create-diff-object: add support for .klp.arch removal
Starting with Linux 5.8, vmlinux-specific KLP relas are applied early,
before all the special section initializations are done.

This means that jump labels can now be supported for cases where the
corresponding static keys live in the core kernel (vmlinux).

It also means that paravirt patching and alternatives can also now be
supported without the need for the .klp.arch sections.

This simplifies things greatly for newer kernels.  We just have to make
sure that module-specific KLP relas aren't created for special sections.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-14 12:44:04 -05:00
Josh Poimboeuf
3982b329bc kpatch-build: drop support for old non-.klp.arch kernels
There were a few kernels (4.7 and 4.8) which didn't have support for
.klp.arch sections, but for which we still tried to use
CONFIG_LIVEPATCH.  Those are inherently buggy, so just drop
CONFIG_LIVEPATCH support for them altogether.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-14 12:44:04 -05:00
Josh Poimboeuf
e80562a8cb kpatch-build: add kpatch.ko deprecation warning
kpatch.ko has been quietly deprecated for a while, because there are
some known issues, including special section initialization ordering
issues.  Starting with Linux 5.7, it will be completely broken because
kallsyms_lookup_name() will no longer be exported.

Add a warning to make its deprecation status more obvious.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-14 12:44:04 -05:00
Josh Poimboeuf
d97de8d554 kpatch-build: readability cleanups
Do some kpatch-build script cleanups to improve readability.  This
is only a cleanup and shouldn't affect any functionality.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-14 12:44:04 -05:00
Josh Poimboeuf
4e0db559c1 lookup: fix Module.symvers reading for newer kernels
With Linux commit 5190044c2965 ("modpost: move the namespace field in
Module.symvers last"), the format of Module.symvers has changed yet
again.

Use a completely different approach for figuring out the format.  If a
column has "vmlinux", assume that's the "Module" column.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-14 12:44:00 -05:00
Yannick Cote
1cc52bf19b
Merge pull request #1088 from euspectre/show-all-jump-labels
create-diff-object: show all jump labels before reporting failure
2020-04-14 09:09:48 -04:00
Evgenii Shatokhin
89e8574027 create-diff-object: show all jump labels before reporting failure
We have recently encountered a situation when a patched function
had more than one jump label (static branches with the same static key
used to turn on/off some debugging feature). As it is often the case
with jump labels, their locations were far from obvious in the source
code, hidden in the chains of inline functions.

create-diff-object, however, exits after it has reported one jump label.
This is inconvenient, because, after one updates the patch to avoid
that jump label, the next build of the binary patch reveals another
one and fails again, and so on. It can be very time-consuming.

Let us report all jump labels first.

Before this commit the messages looked like this:

  kpatch-build/create-diff-object: ERROR: dev.o:
  kpatch_regenerate_special_section: 2084:
  Found a jump label at ploop_req_state_process()+0x220, using key css_stacks_on.
  Jump labels aren't currently supported.  Use static_key_enabled() instead.

After:

  dev.o: Found a jump label at ploop_req_state_process+0x220, key: css_stacks_on.
  dev.o: Found a jump label at ploop_ioctl+0x2708, key: css_stacks_on.
  kpatch-build/create-diff-object: ERROR: dev.o:
  kpatch_regenerate_special_section: 2123:
  Found 2 jump label(s) in the patched code.
  Jump labels aren't currently supported. Use static_key_enabled() instead.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2020-04-10 19:26:06 +03:00
Josh Poimboeuf
0a3e6c5f42 create-diff-object: refactor dynrela conversion
The dynrela (aka .klp.rela) conversion logic is notoriously complex and
fragile.  Simplify it and improve the comments.

This is mainly a cosmetic change.  In theory it shouldn't change
functionality or break anything.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
d2089a4d72 create-diff-object: rename lookup 'result' -> 'symbol'
Improve readability by renaming the lookup "result" variables to
"symbol".

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
ae9f1c17b1 lookup: add duplicate symbol checks
Add checks for duplicate symbols, and refactor the logic slightly.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
3064cf3c60 lookup: add 'objname' to lookup table and lookup results
This will be needed for the upcoming dynrela refactoring.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
7e1f2b0e07 lookup: convert lookup functions to return bool
IMO, the code is easier to follow if these functions return bool.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
6cc03f9599 lookup: rename 'pos' to 'sympos'
To more accurately describe its purpose.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
cd121422d9 lookup: rename 'value' -> 'addr'
Rename 'value' to 'addr' to more accurately describe it.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
5fad6599a4 lookup: remove unused testing code
This testing code is no longer used.  Remove it.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
e4d37dad5d lookup: Skip .dynsym table in symtab_read()
On powerpc, "readelf -s" of vmlinux shows both .dynsym and .symtab.
.dynsym is just a subset of .symtab, so skip it to avoid duplicates.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
7e48138185 lookup: Simplify first loop in symtab_read()
symtab_read() is quite fragile because it relies on the fact that the
first and second loops have the exact same conditions.

Instead just change the first loop to count all the lines in the file,
to get an upper bound for allocation.  It's ok to over-allocate
slightly.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Josh Poimboeuf
74c9c99931 create-diff-object: reduce indentation in kpatch_create_patches_sections()
Reverse the if condition and use a 'continue' statement to reduce
indentation and improve readability.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Julien Thierry
b548ba153f kpatch-build: Look for local static variables in child functions
A symbol in the original object might get split in several sub-functions
in the patched object, which can themselves be bundled (and use a
separate rela section). References to local static variables from the
original function, might have been moved in one of the sub-functions
in the patched object.

Look for references to local static variables in the rela section
of child symbols in the patched object.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-03-30 14:14:17 +01:00
Julien Thierry
42128ff78c kpatch-build: Include .part. symbols as child function
Consider symbols containing .part. in their names as sub-function
of the symbols they are derived from (if such symbol still exists in the
object file).

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-03-30 14:14:17 +01:00
Julien Thierry
b502e5b1cc kpatch-build: Allow function to have multiple child functions
A symbol associated to a function can be split into multiple
sub-functions. Currently, kpatch only supports one child per function.

Extend this to support an arbitrary number of sub-function per function.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-03-30 14:14:17 +01:00
Julien Thierry
af1fe267c5 create-diff-object: Avoid unnecessary parent symbol inclusion
When a child symbol has changed, the parent symbol is only needed
in the output object if the child symbol is unpatchable on its own.
This is the case when the child symbol does not have its own profiling
call.

Only include unchanged parent symbols if their child has changed and
the child does not have a function profiling call.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-03-30 14:14:17 +01:00
Julien Thierry
fbfc8f9bec create-diff-object: Handle ppc64le toc with only constants
When a ppcle64 ".toc" section contains only constants, the compiler
might not (won't?) create a corresponding ".rela.toc" section.

In such cases, create-diff-object crashes, assuming ".rela.toc" exists
whenever .toc exists. Simply report that no rela are available when
looking up possible relocations in .toc.

Fixes #1078.
Signed-off-by: Julien Thierry <jthierry@redhat.com>
2020-02-28 03:50:44 -05:00
Josh Poimboeuf
687e2caabc create-diff-object: Don't strip callback section symbols
Internal CI is reporting a SIGSEGV in create-diff-object when it
processes macro-callbacks.patch, starting with 19baa5b7c7
("create-diff-object: process debug sections last").

The problem is that, after changing the order between callback and debug
section inclusion, kpatch_include_debug_sections() now tries to include
the callback section symbols.  But kpatch_include_callback_elements()
inadvertently un-includes the callback section symbols (e.g.,
".kpatch.callbacks.pre_patch") when it un-includes the callback struct
symbols (e.g., "kpatch_pre_patch_data").

So after kpatch_elf_teardown(kelf_patched), the callback section symbols
get freed even though there are DWARF .debug_info relocations which
reference them.  Then kpatch_check_relocations() goes off into the weeds
when it accesses one of the freed symbols.

Fix it by refining the callback un-include logic so that it *only*
strips the struct object symbols.

Fixes: 19baa5b7c7 ("create-diff-object: process debug sections last")
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-02-18 10:16:39 -06:00
Josh Poimboeuf
648be4c380
Merge pull request #1053 from julien-thierry/stricter-static-twin
kpatch-build: Make local static twining stricter
2020-02-17 15:15:52 -06:00
Josh Poimboeuf
1b7871507a
Merge pull request #1055 from jpoimboe/process-debug-sections-last
create-diff-object: process debug sections last
2020-02-17 14:50:39 -06:00
Kamalesh Babulal
37e950a653 create-kpatch-module: Fix -Wconversion warnings
Fix warnings reported by -Wconversion,-Wno-sign-conversion CFLAGS.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-02-11 19:25:10 +05:30
Kamalesh Babulal
ea9e708df0 create-klp-module: Fix -Wconversion warnings
Fix warnings reported by -Wconversion,-Wno-sign-conversion flags.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-02-11 19:25:04 +05:30
Kamalesh Babulal
f67c2ebce0 kpatch-elf: Fix -Wconversion warnings
Fix warnings reported by -Wconversion,-Wno-sign-conversion flags.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-02-11 19:24:57 +05:30
Kamalesh Babulal
5f68db22e5 create-diff-object: Fix -Wconversion warnings
Fix warnings reported by -Wconversion,-Wno-sign-conversion flags.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-02-10 09:07:21 +05:30
Kamalesh Babulal
a0c608de70 kpatch-build/Makefile: Skip build insn/plugin with -Wconversion
Skip building insn/* on x86 and gcc-plugin on Power with -Wconversion,
-Wno-sign-converion flags.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-01-23 12:59:38 +05:30
Kamalesh Babulal
4e4a8d99da kpatch-build: add -Wconversion/-Wno-sign-conversion CFLAGS
Add -Wconversion and -Wno-sign-conversion to CFLAGS.  The first flag
should catch any implicit conversions like the one seen with #1065 and
the second flag suppress the warnings between signed and unsigned
integers.

Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-01-23 12:59:31 +05:30
Evgenii Shatokhin
f5f5479614 create-diff-object: fix relocations used for ZERO_PAGE(0)
On x86_64, GCC generates the following instruction to compute
'empty_zero_page - __START_KERNEL_map' (__phys_addr_nodebug(), used in
the implementation of ZERO_PAGE()):

    48 ba 00 00 00 00 00 00 00 00   movabs $0x0,%rdx
          R_X86_64_64  empty_zero_page+0x80000000

__START_KERNEL_map is 0xffffffff80000000.

However, the relocation addend becomes wrong in the patch module:

    48 ba 00 00 00 00 00 00 00 00   movabs $0x0,%rdx
          R_X86_64_64  empty_zero_page-0x80000000

Note the sign of the addend.

As a result, ZERO_PAGE(0) returns a wrong value in any function touched
by the patch, which may lead to memory corruption and difficult-to-debug
kernel crashes.

The cause is that 'struct rela' uses 'int' for the addend, which is not
enough to store such values. r_addend from Elf64_Rela is int64_t
(Elf64_Sxword) for that.

Let us use 'long' instead of 'int' for the addend in 'struct rela'.

v2:
* Moved 'addend' field after 'offset' in struct rela to facilitate
  structure packing (suggested by Kamalesh Babulal).

Fixes https://github.com/dynup/kpatch/issues/1064.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2020-01-20 11:41:01 +03:00
Zhipeng Xie
61c55d9e52 kpatch-build: replace all '-' to '_' in KOBJFILE_NAME
When patching kernel module dm-persistent-data, I found
that the KOBJFILE_NAME is incorrectly replaced to
dm_persistent-data while the module name in kernel is
dm_persistent_data.

Signed-off-by: Zhipeng Xie <xiezhipeng1@huawei.com>
2019-12-30 17:48:14 +08:00
Julien Thierry
68530e4c2b kpatch-build: Make local static twining stricter
The current code to find the twin of a local static variable allows two
variables of the same name to be wrongly matched with the other's twin.

While there isn't a magic formula to avoid this, make stricter
requirements for twining static local from the original object with
a symbol from the patched object. This reduces the risk of erroneous
matches.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2019-11-22 09:53:16 +00:00
Josh Poimboeuf
19baa5b7c7 create-diff-object: process debug sections last
Process the debug sections only after all the other inclusion logic has
finished, since it makes decisions based on what else has already been
included.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2019-11-01 15:26:07 -05:00
Julien Thierry
852bad5e8d create-diff-object: Use new helpers for static local correlation
Simplify static local variable correlation and renaming code by using
the newly introduced helpers for section and symbol correlation.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2019-10-29 15:29:51 +00:00
Julien Thierry
e49e3a59c2 create-diff-object: Rename elements getting correlated
Change 935f199875 ('create-diff-object: simplify mangled function
correlation') simplified the way symbols are correlated and got rid of
symbol section renaming.

As a result a symbol/section can now have a CHANGED status, being
correlated to an element that doesn't have the exact same name. This
will cause lookups to the original object fail when creating the new
patch object.

So lets bring back the symbol/section renaming, but only once they
have actually been correlated.

Fixes: 935f199875 ('create-diff-object: simplify mangled function
correlation')
Signed-off-by: Julien Thierry <jthierry@redhat.com>
2019-10-29 15:27:51 +00:00
Julien Thierry
49a9adaf74 create-diff-object: Correlate objects related to a section at once
Elements from the original object and the patched object can be
correlated using their mangled names. In case an elements (section or
symbol) could be matched with more than one object through mangling,
make sure all elements related to a section are correlated with the
corresponding elements of the twin section.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2019-10-29 15:27:51 +00:00
Julien Thierry
770f529225 kpatch-elf: Ensure stale references are not used
When freeing a kpatch_elf, another object might have symbols and
sections twined with elements that are getting freed.

Clear the twin references, so if they are used after the object they
reference is freed, the program will crash.

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2019-10-29 10:48:02 +00:00
Josh Poimboeuf
e0bd024c18
Merge pull request #1052 from sm00th/symvers
Make symvers reading code more flexible.
2019-10-28 15:46:53 -05:00
Artem Savkov
3a1a73f08c Make symvers reading code more flexible.
Kernel commit cb9b55d21fe0 modpost: add support for symbol namespaces
adds a new namespace column to Module.symvers file which can be blank.
fscanf is no longer a viable solution to parse that. Switch to the way
scripts/mod/modpost.c handles this and try to support both versions with
and without namespace column.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2019-10-25 14:17:11 +02:00
Julien Thierry
a02842fb69 kpatch-elf: Free sections in elf teardown
Currently, only rela section get freed. This seems like a simple
scope mistake.

Free all sections regardless of their nature in kpatch_elf_teardown()

Signed-off-by: Julien Thierry <jthierry@redhat.com>
2019-10-15 16:02:11 +01:00
Josh Poimboeuf
935f199875 create-diff-object: simplify mangled function correlation
The RHEL powerpc kernel is compiled with -O3, which triggers some
"interesting" new optimizations.  One of them, which seems to be
relatively common, is the replacing of a function with two separate
"constprop" functions.

Previously we only ever saw a single constprop clone, so we just renamed
the patched version of the function to match the original version.  Now
that we can have two such clones, that no longer makes sense.

Instead of renaming functions, just improve the correlation logic such
that they can be correlated despite having slightly different symbol
names.  The first clone in the original object is correlated with the
first clone in the patched object; the second clone is correlated with
the second clone; and so on.

This assumes that the order of the symbols and sections doesn't change,
which seems to be a reasonable assumption based on past experience with
the compiler.  Otherwise it will just unnecessarily mark the cloned
constprop functions as changed, which is annoying but harmless, and
noticeable by a human anyway.

Fixes #935.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2019-10-03 09:38:29 -05:00
Josh Poimboeuf
683289206b
Merge pull request #1038 from wwheart/master
kpatch-build: fix two parent matches error
2019-09-27 14:43:25 -05:00
Josh Poimboeuf
c175d8697c
Merge pull request #1039 from julien-thierry/misc-cleanups
Misc cleanups
2019-09-27 14:29:53 -05:00