Commit Graph

589 Commits

Author SHA1 Message Date
Kamalesh Babulal 481d8c4bc2 create-diff-object: Support for __warned placed in .data.once
With kernel commit b1fca27d384 ("kernel debug: support resetting
WARN*_ONCE") the *_ONCE warnings are placed .data.once section.
Including .data.once section is valid, so add an check in
kpatch_verify_patchability() while checking for included invalid
sections.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-03-17 15:00:50 +05:30
Kamalesh Babulal cac608a05a create-diff-object: Remove REL24 rela check in kpatch_replace_sections_syms
Symbols with R_PPC64_REL24 relocation type are functions and it's
currently assumed that all functions are replaced with their respective
section symbols.

There are function whose reference are not straight forward section
symbol but section + offset. These function replacement should be
handled more like bundled sections. Remove the check, which imposes
the inital assumption.

Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-03-17 15:00:46 +05:30
Kamalesh Babulal c61cb6776a create-diff-object: Add support for .toc constants
.toc section entries are mostly place holder for relocation entries,
specified in .rela.toc section. Sometimes, .toc section may have
constants as entries. These constants are not reference to any symbols,
but plain instructions mostly due to some arthimetics in the functions
referring them.

They are referred by the functions like normal .toc entries, these
entries can not be resolved to any symbols. This patch creates a list
of constants if available for .toc sections and compares them in
rela_equal() to ensure their is no mismatch in the generated constants
for original and patched .o files.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-03-17 15:00:41 +05:30
Evgenii Shatokhin f8669fa8e8 kpatch-build: 'hint' is not needed in kpatch_create_*_sections()
Found in the scope of https://github.com/dynup/kpatch/pull/755 but not
related to the main problem discussed there.

kpatch_create_patches_sections() and kpatch_create_intermediate_sections()
used 'hint' in error messages.

However, the string 'hint' refers to is owned by 'kelf_base' and is
freed before kpatch_create_*_sections() are called. As a result, if
these functions try to output errors and print 'hint',
create-diff-object will crash.

As suggested in the mentioned PR, 'hint' is actually no longer needed at
that stage, so I have removed it from kpatch_create_*_sections().
2018-02-22 15:06:20 +03:00
Josh Poimboeuf 258ac3f39a
Merge pull request #757 from jpoimboe/TODO-ppc-fix
Some ppc64le cleanups and fixes
2017-12-20 23:02:48 -06:00
Juerg Haefliger 757bc71d2b kpatch-build: Add additional debug modes
By specifying -d, --debug multiple times, the following additional
debug modes can be enabled:
  -d -d:       Writes everything that is written to the logfile also to
               stdout.
  -d -d -d:    Same as '-d -d' plus sets 'xtrace' in kpatch-build.
  -d -d -d -d: Same as '-d -d -d' plus sets 'xtrace' in kpatch-gcc.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-17 15:04:11 +01:00
Juerg Haefliger b2c00b1215 kpatch-build: Add a flexible logger function
Add a logger funcition that can be used to log to both stdout and the
logfile or only to the logfile. This is needed for subsequent patches
where we introduce an alternate debug mode.

Since we're piping to a logger now, we need to set 'pipefail' otherwise
the return status of such a pipeline is always 0 (the exit status of the
logger) and we won't catch any errors.

From the bash manpage:
  The return status of a pipeline is the exit status of the last command,
  unless the pipefail option is enabled

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-15 09:42:41 +01:00
Josh Poimboeuf 63a94b8323 gcc-plugin: create a ppc64le GCC plugin which inserts nops after local calls
This is in response to an upstream discussion for the following patch:

  https://lkml.kernel.org/r/1508217523-18885-1-git-send-email-kamalesh@linux.vnet.ibm.com

This should hopefully make it a lot easier for the ppc64le kernel module
code to support klp relocations.

The gcc-common.h and gcc-generate-rtl-pass.h header files are copied
from the upstream Linux source tree.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-10 09:17:00 -06:00
Juerg Haefliger b5f77d0608 kpatch-build: Stop reading vmlinux after the first match
When searching for 'Linux version ...' in vmlinux, stop after the first
match so that we don't keep reading a potentially huge file.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-10 08:51:41 +01:00
Juerg Haefliger 7770c18f93 kpatch-build: Fix the logfile checks for 'undefined'
The current checks never fail, because the first grep in the pipeline
doesn't write anything to stdout.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-10 08:51:41 +01:00
Juerg Haefliger 1950c193ec kpatch-build: Add -a, --archversion commandline option
This can be used for building a kpatch module for a non-running
kernel. Note that the correct kernel and debug packages still need
to be installed.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-10 08:50:22 +01:00
Josh Poimboeuf 57321ad7e6 create-diff-object: don't convert relocation destinations to symbols
When creating .kpatch.relocations, there's no reason to convert the
relocation destinations to symbols.  In fact, it's actively harmful
because it makes it harder for create-klp-module to deal with the GCC 6+
8-byte localentry gap.

This also fixes a regression which was introduced in 5888f316e6, which
broke ppc64le relocations.

Fixes #754.

Fixes: 5888f316e6 ("create-klp-module: support unbundled symbols")
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-09 19:16:14 -06:00
Josh Poimboeuf c6c153431f create-klp-module: get rid of redundant kpatch_relocation.offset field
kpatch_relocation's 'dest' addend and 'offset' fields are redundant.  In
fact, the 'offset' field isn't always accurate because it doesn't have a
relocation, so its value doesn't adjust when multiple .o files are
combined.  Just use the 'dest' addend instead.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-09 19:16:14 -06:00
Josh Poimboeuf f0518ef58a create-diff-object: fix ppc64le kpatch_replace_sections_syms() bundling assumption
kpatch_replace_sections_syms() assumes that all bundled symbols start at
section offset zero.  With ppc64le and GCC 6+, that assumption is no
longer accurate.  When replacing a rela symbol section with its
corresponding symbol, adjust the addend as necessary.

Also, with this fix in place, the workaround in
create_klp_relasecs_and_syms() can be removed.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-09 19:16:14 -06:00
Josh Poimboeuf bc2d5aa815 create-diff-object: allow correlated symbols to have different st_values
On ppc64le, adding a printk to total_mapping_size() caused it to change
from non-localentry to localentry, presumably because it was no longer a
leaf function.  With GCC 6, a localentry function is offset by 8 in the
section, so different st_values are ok.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-09 19:16:14 -06:00
Josh Poimboeuf e598d9628f create-diff-object: simplify is_gcc6_localentry_bundled_sym()
The STT_FUNC and SHN_UNDEF checks aren't needed because they're already
implied by the localentry check.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-09 19:16:14 -06:00
Josh Poimboeuf 1eac193bfc create-diff-object: is_localentry_sym -> is_gcc6_localentry_bundled_sym
is_localentry_sym() isn't quite the right name, because it also checks
for the 8-byte gap introduced by GCC 6, and also checks that the
function is otherwise at the beginning of the section.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-11-09 19:16:14 -06:00
Juerg Haefliger 862327db3e kpatch-build: Fix indentation
Replace stray spaces with tabs, except in the usage output where tabs
don't make much sense.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-09 15:03:19 +01:00
Juerg Haefliger 15cdcc8b5f kpatch-build: Add -n, --name to the usage
Fixes: 8dc25d79d1 ('kpatch-build: let user specify kpatch module name')
Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-09 15:03:19 +01:00
Juerg Haefliger c33adf0ff2 kpatch-build: Remove unnecessary 'make prepare'
This was introduced in commit 5352d8b01a ('build objects in separate
directory to fix caching') but is no longer necessary.

Fixes: 2e99d6b7a4 ('kpatch-build: build the kernel in ~/.kpatch/src again')
Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-09 15:03:06 +01:00
Juerg Haefliger b6ba60f740 kpatch-build: Fix clean rule (remove insn/*.o)
Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2017-11-07 13:18:42 +01:00
Joe Lawrence 64ba06606f Merge pull request #750 from jpoimboe/rhel75
Fix livepatch version checks for RHEL
2017-10-18 14:11:13 -04:00
Josh Poimboeuf 2e85ec5672 kpatch-build: add support for CONFIG_LIVEPATCH on RHEL
Fix the version checks for when we enable CONFIG_LIVEPATCH on RHEL.  It
will be based on the latest upstream code.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-10-18 06:33:27 -05:00
Joe Lawrence ae074693e0 Merge pull request #749 from jpoimboe/special-struct-padding
create-diff-object: handle missing padding at end of special section
2017-10-17 20:55:07 -04:00
Josh Poimboeuf c2dc3836e8 create-diff-object: handle missing padding at end of special section
The paravirt_patch_site struct has 12 bytes of data and 4 bytes of
padding, for a total of 16 bytes.  However, when laying out the structs
in the .parainstructions section, the vmlinux script only aligns before
each struct's data, not after.  So the last entry doesn't have the
4-byte padding, which breaks kpatch_regenerate_special_section()'s
assumption of a 16-byte struct, resulting in a memcpy past the end of
the section.

Fixes #747.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-10-17 11:09:01 -05:00
Simon Ruderich 70aa26a0c0 kpatch-build: always use spaces around pipes 2017-10-13 11:59:59 +02:00
Simon Ruderich 8173d35a15 kpatch-build: use grep -F to match plain strings
The pattern contains dots which are treated as any character but in this
case should only match a dot.
2017-10-13 11:57:07 +02:00
Simon Ruderich c2a7f2449a kpatch-build: ignore false positives detected by shellcheck 2017-10-13 11:47:56 +02:00
Simon Ruderich 223c65015c kpatch-build: further shell code cleanup
Replace find * with find ./* to prevent treating files with dashes as
options. The leading ./ is later used in comparisons and thus must be
removed before that.

Found by shellcheck.
2017-10-13 11:45:06 +02:00
Simon Ruderich 0b8a53163d kpatch-build: make gcc version check distribution independent
gcc --version varies too much for sane comparisons with vmlinux's
.comment section. Therefore compile a test file and compare its .comment
section.

Also fix gcc 4.8 check which used a lexicographically comparison which
will break for gcc versions >= 10. Instead check for the required
compiler options.

Closes #565.
2017-10-12 17:17:56 +02:00
Simon Ruderich fa6a6dd821 kpatch-build: further shell code cleanup
- Replace grep | wc -l with grep -c.
- Use find -print0 and xargs -0 to handle non-alphanumeric filenames
  (shouldn't be an issue for us but it's good practice).
- Replace expr with $(( )).

Found by shellcheck.
2017-10-11 09:36:05 +02:00
Simon Ruderich f796dc6014 kpatch-build: cleanup quoting of $UBUNTU_ABI
The double quotes are confusing as they don't quote $UBUNTU_ABI and thus
have no real effect. As $UBUNTU_ABI is a number simply remove them and
put $UBUNTU_ABI into the surrounding quotes.

Found by shellcheck.
2017-10-10 12:19:29 +02:00
Simon Ruderich 60e5125cb0 kpatch, kpatch-build: further shell code cleanup
- Replace echo $(cmd) with just cmd.
- Replace $@ inside quotes with $*.
- Always die if cd fails.
- Ensure rm -rf "$TEMPDIR"/* never expands to rm -rf /*.

Found by shellcheck.
2017-10-07 10:12:40 +02:00
Simon Ruderich 08fa04bb0d kpatch, kpatch-build, kpatch-gcc: quote all variables
Without proper quoting kpatch fails if the argument contains spaces, the
other scripts might be affected as well.

Not all new quotes are strictly necessary but they were added for
consistency with the existing code and to prevent copy & paste errors in
the future.

There's one conversion which is not straight-forward:

-       grepname=$grepname\\\.o
+       grepname="$grepname\.o"

There are different quoting rules with and without the double quotes.
2017-10-07 10:12:40 +02:00
Joe Lawrence edfdaedd48 kpatch-build: create-klp-module: set default arguments.no_klp_arch
Valgrind complains about an uninitialized variable in
create-klp-module.c:

 ==4412== Conditional jump or move depends on uninitialised value(s)
 ==4412==    at 0x402846: main (create-klp-module.c:497)

This warning refers to main()'s struct arguments stack variable,
precisely its .no_klp_arch member.  Initialize the entire structure to
zero to avoid complaint.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2017-10-06 10:51:52 -04:00
Joe Lawrence 321bbf9417 kpatch-build: clear Elf_Data d_buf buffer on allocation
Valgrind complains about uninitialized bytes passed to pwrite64(buf)
from kpatch_write_output_elf()'s call to elf_update():

  ==32378== Syscall param pwrite64(buf) points to uninitialised byte(s)
  ==32378==    at 0x5141A03: __pwrite_nocancel (in /usr/lib64/libc-2.23.so)
  ==32378==    by 0x4E46846: ??? (in /usr/lib64/libelf-0.168.so)
  ==32378==    by 0x4E42B88: elf_update (in /usr/lib64/libelf-0.168.so)
  ==32378==    by 0x40C57A: kpatch_write_output_elf (kpatch-elf.c:895)
  ==32378==    by 0x40926F: main (create-diff-object.c:2851)
  ==32378==  Address 0x28d52300 is 0 bytes inside a block of size 56 alloc'd
  ==32378==    at 0x4C2BBAD: malloc (vg_replace_malloc.c:299)
  ==32378==    by 0x40B86A: create_section_pair (kpatch-elf.c:707)
  ==32378==    by 0x406CAE: kpatch_create_patches_sections (create-diff-object.c:2109)
  ==32378==    by 0x4090C5: main (create-diff-object.c:2815)

These are fields which we don't need to populate (like a
funcs[index].new_addr value that will be filled by relocation).  The
easiest way to appease valgrind and not clutter the code is to just
zero-out this entire buffer on allocation.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2017-10-03 16:42:28 -04:00
Joe Lawrence 9b2e37baf7 kpatch-build: ignore .discard sections
Fixes #711.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2017-10-03 15:28:52 -04:00
Joe Lawrence 10b37a16a4 Merge pull request #737 from jpoimboe/bundling
kpatch-build: do symbol/section bundling in create-diff-object
2017-09-21 10:32:19 -04:00
Josh Poimboeuf b72027c44e kpatch-build: do symbol/section bundling in create-diff-object
kpatch-elf.c is used by binaries other than create-diff-object, but
create-diff-object is the only one that cares about "bundling".  Move
the bundling to create-diff-object.

Fixes #700.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-09-20 23:57:31 -05:00
Josh Poimboeuf 5888f316e6 create-klp-module: support unbundled symbols
The create_klp_relasecs_and_syms() function assumes that all dest
symbols are bundled, i.e. each symbol is located at offset 0 in its own
section.

However that may not always be the case.  Unbundled symbols can occur,
for example, when combining two .o files which have the same bundled
symbol.  They will be combined into the same section and will no longer
be considered "bundled".

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-09-20 23:57:31 -05:00
Josh Poimboeuf 6c8e90e640 create-kpatch-module: support unbundled symbols
The create_dynamic_rela_sections() function assumes that all dest
symbols are bundled, i.e. each symbol is located at offset 0 in its own
section.

However that may not always be the case.  Unbundled symbols can occur,
for example, when combining two .o files which have the same bundled
symbol.  They will be combined into the same section and will no longer
be considered "bundled".

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2017-09-20 23:57:24 -05:00
Joe Lawrence c0105ea467 kpatch-build: set default module prefix accordingly
Use kpatch-<modname>.ko or livepatch-<modname>.ko depending on the type
of module we're building.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2017-09-20 16:30:00 -04:00
Joe Lawrence 139cfb38d4 kpatch-build: allow list of patches on command line
It can be convenient to build a patchset into a single kpatch module, so
teach kpatch-build to accept a list of .patch files on the commandline.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2017-09-20 16:30:00 -04:00
Joe Lawrence 8dc25d79d1 kpatch-build: let user specify kpatch module name
Add commandline option to specify the kpatch module name, else derive it
from the .patch filename.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2017-09-20 16:30:00 -04:00
chen xiaoguang ec98604f80 kpatch: fix kpatch-build matching changed object bug
When there is a ".." in the source object path, kpatch-gcc can't handle
it correctly.  kpatch-gcc is called for objects which were recompiled
and writes the changed objects to "changed_objs". But if the path of the
input obj is something like:

  arch/x86/kvm/../../../virt/kvm/.tmp_kvm_main.o

then it will fall into the "*.*.o" branch of the kpatch-gcc case
statement and kpatch-build will report "ERROR: no changed objects
found."

Use Joe's suggestion to revert d526805619 ("kpatch-gcc: update
ignorelist to avoid foo/.lib_exports.o files") and instead add a
"*/.lib_exports.o" pattern.

Fixes #735.

[ cleaned up changelog - jpoimboe@redhat.com ]

Cc: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: chen xiaoguang <xiaoggchen@tencent.com>
2017-09-13 21:08:54 -05:00
Josh Poimboeuf dea77c42dc create-diff-object: treat "CSWTCH.*" as a special static local
Normally, kpatch doesn't complain if you remove or rename a function.
This is a feature, because sometimes you have to rename a function in
order to patch it, if for example it doesn't have an fentry call.  In
the object code, it's treated as a new function.  You could get the same
result by copying/pasting the original function and giving the copy a
new name.  But renaming it makes it much easier to review the patch.

In RHEL 7.4, I tried to rename l2cap_config_rsp() to
l2cap_config_rsp_kpatch(), but it failed with:

  ERROR: l2cap_core.o: reference to static local variable CSWTCH.347 in l2cap_config_rsp was removed

This particular error is an easy fix, because the CSWTCH.* symbols are
read-only and are created by GCC.  So they shouldn't be correlated
anyway.

In the future, we will need a more general fix to allow the removal of
functions which use *any* static local variables.  Either automatically,
or by adding a manual annotation.  This can be handled when we rewrite
the static local variable handling in #545.
2017-09-11 14:55:35 -05:00
Göktürk Yüksek fc54e0e6c3
kpatch-build: allow the number of make jobs to be specified using CPUS
Instead of always using the maximum number of CPUs available, allow
user to tune the number of make jobs using the command line argument
('-j', '--jobs').
2017-09-06 19:35:37 -04:00
Joe Lawrence 1ad31af49b Merge pull request #730 from jpoimboe/rename-mangled-string-sections
create-diff-object: rename mangled string sections
2017-08-24 15:14:20 -04:00
Josh Poimboeuf b1095f485e create-diff-object: handle .LCx symbol renames
If an .LCx symbol gets renamed or changes sections, or if its section
gets renamed, kpatch-build will get confused.

They aren't *real* symbols, just string constants.  So no need to
correlate and compare them.

Fixes #714.
Fixes #727.
2017-08-22 10:30:09 -05:00
Simon Ruderich 8ab170d402 kpatch-build: use implicit make rules
This removes duplicate code which is already handled by make internally
and also respects CPPFLAGS.

LDFLAGS are general linker flags, LDLIBS should be used for the
libraries itself. Therefore switch to LDLIBS which is put after the
object files in the command line (which is not true for LDFLAGS).
2017-08-22 03:21:00 +02:00