Commit Graph

725 Commits

Author SHA1 Message Date
Artem Savkov
5f610fdda2 ppc64le: add WARN detection
ppc64le lacked kpatch_line_macro_change_only() implementation. Add one
based on x86_64 version and available unit tests.

Fixes: #843

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-05-04 16:32:48 +02:00
Joe Lawrence
70959ce6ec
Merge pull request #838 from jpoimboe/test-cleanups
Test cleanups
2018-04-23 17:31:35 -04:00
Josh Poimboeuf
81fb34ed57
Merge pull request #835 from jpoimboe/function-ptr-new
create-diff-object: skip dynrelas for function pointers to new functions
2018-04-20 06:38:55 -05:00
Josh Poimboeuf
0e1b95edea create-diff-object: update the __LINE__ macro checks
Update the `__LINE__` macro detection heuristics based on recent kernel
changes:

- __warn_printk(), used in the WARN macros, which put the line number in
  %edx

- __might_fault(), used in the usercopy macros

- lockdep_rcu_suspicious(), used in the RCU_LOCKDEP_WARN macro

- printk(), used in the ASSERT_RTNL macro

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-04-19 17:19:08 -05:00
Josh Poimboeuf
87939db272 create-diff-object: skip dynrelas for function pointers to new functions
With #755, we started using dynrelas for function pointers.  However,
this behavior only makes sense for function pointers to existing
functions.  For function pointers to *new* functions, just use a normal
rela.

The 'function-ptr-new' unit test is from the following patch:

  https://github.com/dynup/kpatch/files/1927198/new-static-callback.patch.txt

Fixes #834.

Fixes: 495e619750 ("kpatch-build, x86: do not use the patched functions as callbacks directly")
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-04-19 07:56:12 -05:00
Artem Savkov
a4241797fb ppc64le: fix gcc-plugin installation
Because $PLUGIN variable is a part of $TARGETS we are trying to install
gcc-plugins twice which is treated as an error by install command:

/usr/bin/install create-diff-object create-klp-module create-kpatch-module gcc-plugins/ppc64le-plugin.so kpatch-gcc gcc-plugins/ppc64le-plugin.so /usr/local/libexec/kpatch
/usr/bin/install: will not overwrite just-created '/usr/local/libexec/kpatch/ppc64le-plugin.so' with 'gcc-plugins/ppc64le-plugin.so'

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-04-19 10:16:37 +02:00
Artem Savkov
a39eace756 kpatch-build: add -Wsign-compare to CFLAGS
Add -Wsign-compare to kpatch-build CFLAGS to make sure something like #826
never happens again.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-04-13 16:27:23 +02:00
Artem Savkov
1638c64fe5 Fix multiple sign-compare warnings
- convert section/symbol indexes and rela->offset to unsigned int as I
    couldn't find any way for them to become negative.
  - cast a number of rela->addend comparisons to int (assuming an 64bit
    system this should be enough)
  - a number of simple for-loop counter conversions to the type it
    compares against

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-04-13 16:26:06 +02:00
Joe Lawrence
c5e4b51982
Merge pull request #827 from vincentbernat/fix/ubuntu-kernel-retry
kpatch-build: fix Ubuntu kernel detection on successive retries
2018-04-12 14:11:38 -04:00
Vincent Bernat
e2e7d2cd21 kpatch-build: fix Ubuntu kernel detection on successive retries
If a patch failed a first time, kpatch-build is using the previous
cache directory on subsequent builds. The UBUNTU_KERNEL=1 variable is
not set in this case. Therefore, utsrelease.h is not updated correctly
and the appropriate structures are not used. Just check if distro is
Ubuntu and we didn't request our own specific directory.

Signed-off-by: Vincent Bernat <vincent@bernat.im>
2018-04-12 18:29:18 +02:00
Artem Savkov
4591c0701c Fix false-positives in kpatch_check_relocations
Because of signedness difference kpatch_check_relocations() would trigger
an error on any negative addend.
Fix by casting Elf_Data->d_size from size_t to int.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-04-12 17:46:12 +02:00
Artem Savkov
1d9f75a155 create-diff-object: check for out-of-range relocations
Check that none of the relocations are out-of-range of their
corresponding sections before writing the output elf.

Fixes: #618

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-04-11 10:40:34 +02:00
Kamalesh Babulal
ed14d8d332 create-diff-object: Refactor rela_equal() using toc_rela()
Heavy lifting of reading .toc rela entries for rela symbols
referring to .toc + offset, can be simplified using toc_rela() in
rela_equal() and remove the #ifdery guarding PowerPC code.

This patch also trims the commentary related to PowerPC.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-04-06 21:12:51 +05:30
Josh Poimboeuf
ff8b2510af create-diff-object: fix ppc64le static local variable correlation
On ppc64le, the static local variable correlation doesn't take into
account the .toc rela indirection for data references, meaning that it's
basically broken in many cases.

Fix it by making the code .toc-aware.

Fixes #793.

Reported-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-04-04 16:30:07 -05:00
Joe Lawrence
55650e16af
Merge pull request #780 from joe-lawrence/livepatch-hooks
kmod: add support for in-kernel livepatch hooks
2018-04-02 14:49:07 -04:00
Joe Lawrence
e4131d4651
Merge pull request #809 from joe-lawrence/make-check
scripts: small ShellCheck cleanups
2018-03-24 15:49:48 -04:00
Joe Lawrence
812081c329
Merge pull request #789 from kamalesh-babulal/ppc64le_callback
[RFC] PC64le - do not use the patched functions as callbacks directly
2018-03-24 15:49:22 -04:00
Joe Lawrence
926e4e0c7d kmod: add support for in-kernel livepatch hooks
Upstream 4.15 kernels provide support for pre and post (un)patch
callbacks, inspired by the kpatch load hooks.  Add support for them
in the livepatch-patch-hook.

At the same time, convert the kpatch hooks to use the same API.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-03-23 10:32:14 -04:00
Joe Lawrence
64173f4f1d scripts: small ShellCheck cleanups
Cleanup some of the new bash script code to appease 'make check':

  - kpatch: Use integer comparison for $MAX_LOAD_ATTEMPTS
  - kpatch-build: Quote ${TEMPDIR} variable

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-03-23 10:27:35 -04:00
Joe Lawrence
4a0b3541be
Merge pull request #796 from jpoimboe/plugin-fix
gcc-plugin: run the plugin at an earlier pass
2018-03-22 14:32:10 -04:00
Joe Lawrence
5ec00a0bfd
Merge pull request #801 from sm00th/extsym
Undefined reference failure logic rework
2018-03-22 14:20:02 -04:00
Joe Lawrence
0f17a019be
Merge pull request #808 from jpoimboe/include-logic-rewrite
create-diff-object: refactor symbol/section inclusion logic
2018-03-22 13:06:09 -04:00
Joe Lawrence
2aa75f268f
Merge pull request #804 from jpoimboe/lookup-fix
lookup: fix discarded symbol handling for all kernel versions
2018-03-22 10:43:50 -04:00
Josh Poimboeuf
f1d71ac846 create-diff-object: refactor symbol/section inclusion logic
kpatch_include_symbol() is confusing.  Refactor it:

- Remove the "inclusion tree" debug messages.  I never use them, and
  they just help make the code more confusing and the debug output more
  cluttered.

- Split it up into two functions: kpatch_include_symbol() and
  kpatch_include_section(), so that kpatch_include_section() can be used
  elsewhere.

- Call kpatch_include_section() from kpatch_include_standard_elements().
  This covertly fixes #702, by also including the .rela.rodata section.

- Add a bunch of comments to clarify some of the trickier points.

Fixes #702.
Fixes #807.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-03-21 21:36:33 -05:00
Doug Goldstein
ff5c554a40
drop unused rpmdevtools dependency
It appears that since 0dec5136ee nothing
has been used from the rpmdevtools package by kpatch-build.

Signed-off-by: Doug Goldstein <cardoe@cardoe.com>
2018-03-21 15:15:08 -05:00
Josh Poimboeuf
b811940173 lookup: fix discarded symbol handling for all kernel versions
Fix discarded symbol handling for all kernel versions.

Fixes #765.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-03-21 13:19:31 -05:00
Kamalesh Babulal
19b0aba672 PPC64le - do not use the patched functions as callbacks directly
It was observed by Evgenii Shatokhin in PR#755, that when the RCU
callback was called on the patched function, from unloaded livepatch
module triggered a kernel crash.

This patch implements the approach on PowerPC outlined in PR#755.
With -mcmodel=large, like any other data, function pointers are also
loaded relative to the current TOC base and are populated as
relocation entries in .toc section. Every function passing a function
pointer as the argument need to load the function address through
.toc section + offset. Convert such .toc + offset relocation into
a dynamic rela, which resolves to original function address, during
module load.

Also move the comment related to nested function check, into
may_need_dynrela().

Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
Cc: Joe Lawrence <jdl1291@gmail.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-03-21 09:05:25 +05:30
Evgenii Shatokhin
495e619750 kpatch-build, x86: do not use the patched functions as callbacks directly
A kernel crash happened in __do_softirq() in very rare cases when the
binary patch created from mainline commit be82485fbcbb
("netlink: fix an use-after-free issue for nlk groups") was unloaded.

Investigation has shown that the kernel tried to execute an RCU
callback, deferred_put_nlk_sk(), defined in the patch module after the
module had been unloaded.

The callback was set by the patched variant of netlink_release() and
the address of the patched deferred_put_nlk_sk() was used, rather than
the address of the original function.

Similar problems occur with workqueue functions as well.

As suggested in
https://github.com/dynup/kpatch/pull/755#issuecomment-344135224,
create-diff-object was modified so that the addresses of the original
functions were used in such situations, at least for x86 systems. A
similar fix for PowerPC was added as well.

Changes in v4:

* '#ifdef __x86_64__' was removed. It is not actually needed right now
because the constants for relocation types are different on different
architectures.

Changes in v3:

* Minor refactoring and a comment explaining what this all is about.
Quite lengthy, but the dynrela-related code is really far from obvious.

Changes in v2:

* Handle the nested functions the same way as before, because they are
unlikely to be used as asynchronous callbacks. Example: cmp() in
bch_cache_show() from drivers/md/bcache/sysfs.c in the kernel 4.4.

As the nested functions are local to the functions they are defined in,
the compiler names them in a similar way to static locals: <name>.<number>.
Currently, we filter out all functions with '.' in their names. If there
are any asynchronous callbacks in the kernel that have a dot in their
names too, they could be handled in the future patches. It is unclear
though, if the callbacks with such names can appear in the kernel.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-03-21 09:01:22 +05:30
Artem Savkov
2ac206b3ba Undefined reference failure logic rework
Don't die outright when encountering an 'undefined reference' error,
instead write those down and check if needed symbols are provided by the
resulting module or core kpatch module.

Fixes #783.

v2:
  - make undefined_references and new_symbols unique before comparing
  - remove leftover $KMOD_PATH from new_symbols readelf

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-03-20 20:42:55 +01:00
Artem Savkov
cedcd2314b Prepare for built-in.o -> built-in.a rename
for-next branch of kbuild repo contains a "kbuild: rename built-in.o to
built-in.a" which renames all built-in.o instances. Filter on both .o
and .a in kpatch-gcc/kpatch-build to be prepared for this change.

Fixes #800.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-03-20 10:46:50 +01:00
Joe Lawrence
613794e85a
Merge pull request #773 from kamalesh-babulal/ppc64le_fixes
More ppc64le fixes
2018-03-19 16:53:06 -04:00
Joe Lawrence
794db9c318
Merge pull request #791 from kamalesh-babulal/ppc64le_addend
ppc64le - Fix wrong addend value for switch labels
2018-03-19 16:27:05 -04:00
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
Josh Poimboeuf
2991e6259a create-diff-object: Fix PPC64_LOCAL_ENTRY_OFFSET usage
GCC 7.2.1 complains about the usage of the PPC64_LOCAL_ENTRY_OFFSET
macro:

  create-diff-object.c: In function ‘is_gcc6_localentry_bundled_sym’:
  create-diff-object.c:119:83: error: ‘<<’ in boolean context, did you mean ‘<’ ? [-Werror=int-in-bool-context]
            (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
  create-diff-object.c:140:10: note: in expansion of macro ‘PPC64_LOCAL_ENTRY_OFFSET’
    return (PPC64_LOCAL_ENTRY_OFFSET(sym->sym.st_other) &&
          ^~~~~~~~~~~~~~~~~~~~~~~~

Fix it by explicitly treating the macro as an integer instead of a bool.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-03-16 17:20:27 -05:00
Josh Poimboeuf
0019e83622 gcc-plugin: run the plugin at an earlier pass
On ppc64le, building drivers/media/i2c/cx25840/cx25840.ko with GCC 4.85,
the plugin triggers the following errors:

  {standard input}: Assembler messages:
  {standard input}:1078: Error: operand out of range (0x0000000000008fd4 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:1288: Error: operand out of range (0x000000000000874c is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:3844: Error: operand out of range (0x00000000000080e8 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:4028: Error: operand out of range (0x00000000000082b4 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:4031: Error: operand out of range (0x00000000000080b4 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:6661: Error: operand out of range (0x000000000000841c is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:6664: Error: operand out of range (0x0000000000008214 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:6845: Error: operand out of range (0x00000000000089dc is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:6848: Error: operand out of range (0x00000000000087d4 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:7587: Error: operand out of range (0x0000000000008930 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:7590: Error: operand out of range (0x0000000000008728 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  {standard input}:14592: Error: operand out of range (0xffffffffffff79b8 is not between 0xffffffffffff8000 and 0x0000000000007ffc)
  make[1]: *** [drivers/media/i2c/cx25840/cx25840-core.o] Error 1

The problem is that the plugin nops were inserted too late.  GCC laid
out the code with knowledge about the distance between branches, but
then the plugin came along after that and increased the branch distances
by inserting the nops.

Fix it by inserting the nops as early as possible.  The 'vregs' pass is
where the insn codes get initialized, so run the plugin immediately
afterwards.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-03-16 17:16:17 -05:00
Artem Savkov
5782a977a8 Fix resulting module vermagic when source dir is a git tree
Sometimes git doesn't see that the patches have been reverted, if that
happens during ./scripts/setlocalversion call the resulting patch module
is built with a wrong vermagic because the tree is still considered
dirty.

Fix by moving git update-index call into remove_patches function so that
it is called every time the patches are reverted, not only on cleanup.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-03-13 16:23:03 +01:00
Kamalesh Babulal
7b4efd1509 create-diff-object: ppc64le - Fix wrong addend value for switch labels
Commit f0518ef58a ("create-diff-object: fix ppc64le
kpatch_replace_sections_syms() bundling assumption"), subtracted
sym.st_value for all bundled symbols on ppc64le and GCC6+ irrespective
of function or switch label.

As a side effect the addend for switch labels were incorrect and call
to local function executed TOC setup code. Fallback to the original
behaviour of subtracting sym.st_value only for the dynrela.

Example of wrong switch label:
------------------------------
0000000000000008 <register_netdevice>:
...
 31c:   05 00 85 2b     cmplwi  cr7,r5,5
 320:   7c 02 9d 41     bgt     cr7,59c <register_netdevice+0x594>
 324:   00 00 e2 3c     addis   r7,r2,0
                        324: R_PPC64_TOC16_HA   .toc+0x580
 328:   64 17 a5 78     rldicr  r5,r5,2,61
 32c:   00 00 47 e9     ld      r10,0(r7)
                        32c: R_PPC64_TOC16_LO_DS        .toc+0x580
 330:   aa 2a 2a 7d     lwax    r9,r10,r5
 334:   14 52 29 7d     add     r9,r9,r10
 338:   a6 03 29 7d     mtctr   r9
 33c:   20 04 80 4e     bctr
 340:   44 02 00 00     .long 0x244
 344:   f0 01 00 00     .long 0x1f0
 348:   20 02 00 00     .long 0x220
 34c:   38 02 00 00     .long 0x238
 350:   2c 02 00 00     .long 0x22c
 354:   50 02 00 00     .long 0x250
 358:   00 00 00 60     nop

Before Patch (.toc section):
Offset Info             Type           Symbol's Value  Symbol's Name + Addend
...
0x580  0000008200000026 R_PPC64_ADDR64 0000000000000008 register_netdevice + 338

After Patch (.toc section):
Offset Info             Type           Symbol's Value  Symbol's Name + Addend
...
0x580  0000008200000026 R_PPC64_ADDR64 0000000000000008 register_netdevice + 340

Example of localentry addend removed:
------------------------------------
Before Patch (.toc section):
Offset Info             Type           Symbol's Value  Symbol's Name + Addend
...
0x48   0000002c00000026 R_PPC64_ADDR64 0000000000000008 cmdline_proc_show + 0

After Patch (.toc section):
Offset Info             Type           Symbol's Value   Symbol's Name + Addend
...
0x48   0000002c00000026 R_PPC64_ADDR64 0000000000000008 cmdline_proc_show + 8

Fixes: f0518ef58a ("create-diff-object: fix ppc64le kpatch_replace_sections_syms() bundling assumption")
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-03-06 20:40:16 +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
Simon Ruderich
3578e9a6d8 kpatch-build: check DEBUG_KERNEL to guard against missing debug information
Without this option kpatch-build dies with "ERROR: can't find special
struct alt_instr size.".

DEBUG_KERNEL was most likely a typo.
2017-08-17 09:43:09 +02:00
Josh Poimboeuf
339938c0a9 kpatch-build: clarify ppc64le comments
Clarify some of the comment wording in the new ppc64le code.
2017-07-27 15:23:26 -05:00
Kamalesh Babulal
e3ccff0cab kpatch-build: Support gcc-6 function prologue
With gcc-6 the function prologue is changeg by
moving the toc base resolution func - 0x8 bytes:

        .globl my_func
        .type my_func, @function
        .quad .TOC.-my_func
my_func:
        .reloc ., R_PPC64_ENTRY ; optional
        ld r2,-8(r12)
        add r2,r2,r12
        .localentry my_func, .-my_func

Add support for function prologue, along with gcc-5.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-27 12:56:00 +05:30
Kamalesh Babulal
77f8fd09f1 kpatch-build: ppc64le - Add special section support
Add support for ppc64le specific special sections:

- __ftr_fixup
- __mmu_ftr_fixup
- __fw_ftr_fixup
- __lwsync_fixup

This patch also add #ifdef guards for architecture specific
special sections.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-26 14:41:08 +05:30
Kamalesh Babulal
c14e6e9118 kpatch-build: Add PPC64le livepatch support
This patch adds support for livepatch hook based module
creation for PPC64le. It introduces PPC64le architecture
bits:
- Add relocation type of R_PPC64_ADDR64 while parsing powerpc ELF.
- Introduce .toc sections mainpulation.
- Skip kpatch specific details for livepatch hook.

Also remove the definition of rela_insn() for powerpc. The only
call site is been guarded by #ifdef x86.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-26 14:40:37 +05:30
Kamalesh Babulal
c9e4230d88 kpatch-build: Rename fentry instances to func_profiling
symbol->has_fentry_call is x86 specfic. Rename it to more
generic name, representing the general idea of calling
profiling function at function entry.

This patch converts all instance of symbol->has_fentry_call
to symbol->has_func_profiling and also renames functions:
kpatch_check_fentry_calls() -> kpatch_check_func_profiling_calls()
kpatch_find_fentry_calls() -> kpatch_find_func_profiling_calls()

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-26 14:40:33 +05:30
Kamalesh Babulal
afafc239e5 kpatch-build: Create find_special_section_data helper
Move special section data checks to helper function
find_special_section_data(). Special section data will differ
between architectures and all architecture specific and common
checks can be handled better within a helper function.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-26 14:40:31 +05:30
Kamalesh Babulal
665c2222ce kpatch-build: PPC64le - gcc profiling support check
gcc -mprofile-kernel support is required on ppc64le for livepatch
to work. Check should be performed on the gcc, instead of relying
on the verion number.

This check is already performed during the kernel build by:
<linux-sources>/arch/poweprc/tools/gcc-check-mprofile-kernel.sh
Bail out, during the kernel build. Incase the gcc lacks the support
for -mprofile-kernel, instead of duplicating the check in kpatch-buid.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-26 14:40:28 +05:30
Kamalesh Babulal
dde4e95fa4 kpatch-build: Build with insn support only on x86_64
kpatch-build/insn provides x86 instruction analysis, disable
the analyzer support when build on powerpc.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2017-07-26 14:40:25 +05:30
Josh Poimboeuf
9a123ecc02 kpatch-build: fix errors messages for missing files/dirs
If you give kpatch-build a bad argument for the '-s' option, it shows
the following error:

  $ kpatch-build/kpatch-build -s foo
  ERROR: source dir  not found.

The supplied 'foo' argument isn't printed as intended.

Also fix some other options which have a similar issue.
2017-07-17 22:59:37 -05:00
Joe Lawrence
4960f09fe2 Merge pull request #719 from aliceinwire/small_fix
fix style
2017-07-13 15:42:13 -04:00
Alice Ferrazzi
9d6e1ef535 fix style 2017-07-13 19:01:14 +00:00
Alice Ferrazzi
d1d94a5e5a added output argument for choose where to save the livepatch file 2017-07-13 18:15:02 +00:00
Josh Poimboeuf
c8c474ca0e lookup: allow local symbols to be in any order
With #650, we found that using -ffunction-sections and -fdata-sections
sometimes causes GCC to output the local symbols in a different order in
the symbol table.  So don't assume they're in the same order, and
instead search all the locals.

This requires two passes: once going through the lookup table symbols
and once going through the .o symbols.  This is needed to make sure
there aren't any extra symbols in one of the files.

I also reorganized the code a bit to simplify it.
2017-07-06 12:44:50 -05:00
Josh Poimboeuf
c6763e218f lookup: silence maybe-uninitialized warnings for -O2
When compiling with -O2, it fails with:

  gcc -MMD -MP -O2 -I../kmod/patch -Iinsn -Wall -g -Werror -c -o lookup.o lookup.c
  lookup.c: In function ‘lookup_open’:
  lookup.c:132:21: error: ‘file_sym’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
     table->local_syms = file_sym;
     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~
  lookup.c:83:30: note: ‘file_sym’ was declared here
    struct object_symbol *sym, *file_sym;
                                ^~~~~~~~
  lookup.c:129:27: error: ‘child_sym’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
    if (in_file && !child_sym->name) {
                    ~~~~~~~~~^~~~~~
  lookup.c:85:27: note: ‘child_sym’ was declared here
    struct sym_compare_type *child_sym;
                             ^~~~~~~~~
  cc1: all warnings being treated as errors
  Makefile:17: recipe for target 'lookup.o' failed
  make[1]: *** [lookup.o] Error 1
  make[1]: Leaving directory '/home/jpoimboe/git/kpatch/kpatch-build'
  Makefile:14: recipe for target 'build-kpatch-build' failed
  make: *** [build-kpatch-build] Error 2

As far as I can tell, these are false positive warnings.  When in_file
is 1, file_sym and child_sym are properly initialized.  But silence the
warnings anyway so Gentoo users can build with -O2.

Fixes: #675
2017-06-05 11:11:55 -05:00
Jessica Yu
5a04c3395a Merge pull request #705 from vincentbernat/fix/ubuntu-get-source
kpatch-build: retrieve source package instead of linux-source
2017-05-18 20:23:21 -07:00
Vincent Bernat
998c794c7a create-diff-object: only check if kernel object starts with "vmlinux"
On Debian/Ubuntu, the `vmlinux` from `-dbg` package has a version number
appended to it. For example:
`/usr/lib/debug/boot/vmlinux-3.13.0-117-generic`. Make it work
nonetheless.
2017-05-18 08:18:14 +02:00
Vincent Bernat
0f717abf0c kpatch-build: retrieve source package instead of linux-source
On Ubuntu Trusty, HWE kernels don't come with a linux-source
package. Use dget to retrieve the source package instead. This is not
the case anymore with Xenial as the linux-source package is also
provided for the HWE kernels. For Debian, backports always come with the
linux-source package.
2017-05-17 23:24:32 +02:00
Josh Poimboeuf
2b39f7d8d8 elf: add .eh_frame* sections to debug section list
SUSE-based kernels have a DWARF unwinder, so they build with the gcc
'-fasynchronous-unwind-tables' flag, which adds .eh_frame and
.eh_frame_hdr sections.  Treat those sections like the other debug
sections.

Fixes: #703
2017-05-11 14:02:08 -05:00
Josh Poimboeuf
012ccece85 create-diff-object: don't create dynrelas for exported vmlinux symbols used by modules
Joe saw the following errors when loading Linux commit 128394eff343
("sg_write()/bsg_write() is not fit to be called under KERNEL_DS"):

  Skipped dynrela for copy_user_generic_unrolled (0xffffffffa0475942 <- 0xffffffff813211e0): the instruction has been changed already.
  Skipped dynrela for copy_user_generic_unrolled (0xffffffffa0475a57 <- 0xffffffff813211e0): the instruction has been changed already.

That is known issue #580, but it can be avoided by leaving
'copy_user_generic_unrolled' as a normal relocation instead of
converting it to a dynrela, because it's an exported symbol.

Also remove the manual check for '__fentry__' because it's covered by
the exported symbol check.

Also remove a duplicate comment about unexported global object symbols
being in another .o in the patch object.

Fixes #695.
2017-04-20 16:56:43 -05:00
Jessica Yu
f2b205b534 kpatch-build: fix small typo 2017-03-21 15:25:38 -07:00
Jessica Yu
759774eaa4 Merge pull request #687 from joe-lawrence/lib_exports
kpatch-gcc: update ignorelist to avoid foo/.lib_exports.o files
2017-03-10 10:00:32 -08:00
Joe Lawrence
d526805619 kpatch-gcc: update ignorelist to avoid foo/.lib_exports.o files
Upstream kernel commit 7f2084fa55e6 ("[kbuild] handle exports in lib-y
objects reliably") (v4.9+) added temporary dummy .lib_exports.o objects
to the kernel build.  As these ephemeral files don't contain any code,
update the kpatch-gcc glob pattern to ignore them.

(glob pattern suggested by flaming-toast)

Fixes #686.
2017-03-10 10:26:06 -05:00
Jessica Yu
2553653789 Merge pull request #682 from rosslagerwall/strip-ignored-symbols
create-diff-object: Strip kpatch_ignore_func_* symbols
2017-03-08 21:12:11 -08:00
Josh Poimboeuf
41b2364f66 Merge pull request #685 from jpoimboe/build-in-tree
kpatch-build: build the kernel in ~/.kpatch/src again
2017-03-08 07:41:03 -06:00
Josh Poimboeuf
1308787995 Merge pull request #684 from jpoimboe/find_local_syms-fix
lookup: skip discarded symbols in local symbol comparison
2017-03-07 12:38:15 -06:00
Ross Lagerwall
579ce0dfc6 create-diff-object: Strip *kpatch_ignore_*_* symbols
Strip kpatch_ignore_func_* and __UNIQUE_ID_kpatch_ignore_section_*
symbols to prevent the inclusion of .kpatch.ignore.functions and
.kpatch.ignore.sections. Mark the symbols as SAME, otherwise they are
considered NEW and are recursively included. This includes the
corresponding ignore sections and rela sections and may also create new,
unnecessary dynrelas.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2017-03-07 15:01:06 +00:00
Josh Poimboeuf
23952b5ce0 lookup: minor lookup_open() cleanup
A couple of minor cleanups:

- move the `if (locals)` check to find_local_syms()

- remove the explicit initialization of `local_syms`, the entire struct
  was already previously cleared to zero.
2017-03-03 17:06:16 -06:00
Josh Poimboeuf
e1a2e4e6ad kpatch-build: make clean_cache() wipe out ~/.kpatch completely
When wiping out the ~/.kpatch cache before replacing it with a new
kernel source, there's no need to keep anything around.  Just wipe it
all out and start over.

Also, when building with the -s option, it doesn't need to touch
~/.kpatch/version or ~/.kpatch/src, so it can just skip the cleaning.
That keeps the previous cache around for the next incantation of
kpatch-build without '-s'.
2017-03-03 11:29:21 -06:00
Josh Poimboeuf
2e99d6b7a4 kpatch-build: build the kernel in ~/.kpatch/src again
Once upon a time, kpatch-build did the kernel build in three passes.
The extra pass was done without '-ffunction-sections -fdata-sections',
so it could produce the original vmlinux file.

At that time, there was no ~/.kpatch/obj directory.  The kernel was
built directly in ~/.kpatch/src.  Because the same directory was used
for both the original kernel build and the '-ffunction-sections
-fdata-sections' build, the entire tree had to be rebuilt twice for
every kpatch-build incantation, making it very slow.

That situation was improved with the following commit:

  5352d8b01a ("build objects in separate directory to fix caching")

That built the regular and special binaries in ~/.kpatch/obj and
~/.kpatch/obj2, respectively.

Since then we've simplified things so that it only does two build
passes: original and patched, both with '-ffunction-sections
-fdata-sections', and ~/.kpatch/obj2 was removed.  However,
~/.kpatch/obj still remained.  That's because we never had a reason to
change it, until now.

Recent commit aa2907df29 ("support dup file+symbol")
triggers a new warning:

  create-diff-object: ERROR: dynamic_debug.o: find_local_syms: 124: find_local_syms for dynamic_debug.c: found_none

This was actually a preexisting issue which that commit helped uncover.
The root issue is that dynamic_debug.c has some creative uses of the
`__FILE__` macro.  When building the kernel objects outside the source
tree, the macro results in a absolute path like:

  /home/jpoimboe/.kpatch/src/lib/dynamic_debug.c

But when building inside the source tree it's a relative path:

  lib/dynamic_debug.c

The Fedora kernel is built in-tree, and I would imagine most other
distros are also built that way.  So the way kpatch builds can result in
a slightly different 'original' object than the distro version, thanks
to the __FILE__ macro.

In this case, the order of the symbol table changed slightly between
vmlinux and the 'orig' object.  Presumably, the difference in string
lengths was enough to convince the compiler to shuffle things around a
bit.

So considering that bug, and the possibility of other mismatches, go
back to building the kernel in the source tree.
2017-03-03 11:29:21 -06:00
Josh Poimboeuf
4779b9c0d8 kpatch-build: make vim's syntax highlighting happy
For some reason, the backticks on this line confuse my editor's syntax
highlighter!  Make vim happy by using the other form of command
substition.

Also convert the function definition syntax to comply with the
kpatch-build coding guidelines ;-)
2017-03-03 11:29:21 -06:00
Josh Poimboeuf
85def82275 lookup: skip discarded symbols in local symbol comparison
A few symbols are discarded in the kernel linking phase, which means
they won't be in the lookup table.  Skip their comparison.

This fixes a bunch of warnings seen when building a patch which triggers
a tree-wide rebuild:

  create-diff-object: ERROR: aes_glue.o: find_local_syms: 112: find_local_syms for aes_glue.c: found_none
  create-diff-object: ERROR: aesni-intel_glue.o: find_local_syms: 112: find_local_syms for aesni-intel_glue.c: found_none
  create-diff-object: ERROR: init.o: find_local_syms: 112: find_local_syms for init.c: found_none
  create-diff-object: ERROR: iosf_mbi.o: find_local_syms: 112: find_local_syms for iosf_mbi.c: found_none
  create-diff-object: ERROR: setup.o: find_local_syms: 112: find_local_syms for setup.c: found_none
  ...

After this patch, there's still one warning remaining:

  create-diff-object: ERROR: dynamic_debug.o: find_local_syms: 133: find_local_syms for dynamic_debug.c: found_none

That one has a completely different cause, which I'll fix in another
pull request (coming soon).

Fixes: #676
2017-03-03 09:10:15 -06:00
Ross Lagerwall
09ef2457cd create-diff-object: Mark correlated static local variables the SAME initially
Normal correlated symbols are marked the SAME initially but static local
variables are correlated in a separate function. Also mark these the
SAME.

This fixes an issue where patching a function which called printk_once
(which uses a static local variable) would fail to build because the
static local variable was considered new and thus introduced a new data
member into .data..read_mostly which is not allowed to change.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2017-02-28 17:44:28 +00:00
Josh Poimboeuf
fd911ab66c Merge pull request #669 from joe-lawrence/debug_atomic_sleep
detect and ignore __might_sleep line number changes
2017-02-23 16:44:01 -06:00
Josh Poimboeuf
51f1f51f87 lookup: rename find_local_syms() variables
Rename a couple of the variables in find_local_syms() to better reflect
their purpose.  The passed in 'locals' are from the childobj (e.g.
foo.o) rather than the parent (e.g. vmlinux).
2017-02-15 15:18:02 -06:00
Joe Lawrence
99f7792fdc detect and ignore might_sleep line number changes
When CONFIG_DEBUG_ATOMIC_SLEEP is enabled, might_sleep calls will add
the line number to the instruction stream.  Detect and ignore any such
changes.

Fixes: #657.
2017-02-14 16:34:48 -05:00
Joe Lawrence
337965ad71 kpatch-build: consider CONFIG_PARAVIRT optional
CONFIG_PARAVIRT is not required for building kpatch patch modules.  The
sizeof paravirt_patch_site struct was only needed to create
.parainstructions sections as part of create-diff-object.  As long as
the original objects were built without such sections then
this kernel option (and struct handling) can be considered optional.
2017-02-09 10:14:34 -05:00
Joe Lawrence
3b2c372d81 kpatch-build: CONFIG_DEBUG_INFO_SPLIT is unsupported
The CONFIG_DEBUG_INFO_SPLIT kernel .config option places debug
information into separate .dwo files.  As no known distribution is
currently shipping .dwo in their debuginfo packages, leave it as
unsupported for now.
2017-02-09 10:14:34 -05:00
Joe Lawrence
7800064bde kpatch-build: handle out of order special section data
When building a kernel with CONFIG_DEBUG_INFO_REDUCED, it was observed
that subsequent readelf -wi output may not always display structure size
information first.  The affects the kpatch-build awk script that needs
to consider readelf output like the following:

 <1><26393>: Abbrev Number: 12 (DW_TAG_structure_type)
    <26394>   DW_AT_name        : (indirect string, offset: 0x914f): alt_instr
    <26398>   DW_AT_declaration : 12
 ...

 <1><169d1b>: Abbrev Number: 13 (DW_TAG_structure_type)
    <169d1c>   DW_AT_name        : (indirect string, offset: 0x914f): alt_instr
    <169d20>   DW_AT_byte_size   : 13

Therefore the awk state machine should reset if it doesn't encounter
"DW_AT_byte_size" after given structure name match.

Fixes: #668.
2017-02-09 10:14:34 -05:00
Joe Lawrence
bcd1ee3324 kpatch-build: support older gawk regex
Support for gawk '\s' (whitespace) GNU Regexp Operator was added
somewhere between gawk 3 and 4 (RHEL6 and RHEL7).  Use the [[:space:]]
bracket expression to support older releases of gawk.
2017-02-09 10:14:34 -05:00
Zhou Chengming
aa2907df29 support dup file+symbol
We use kelf_base->symbols to find a unique matching FILE+locals combination
when we call lookup_open(). If we can't find one matching or we find more
than one matching, we error out.

If we find a unique one, we setup table->local_syms in lookup_open(),
so later lookup_local_symbol() could do its lookup based on table->local_syms.

Fixes #604.

Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
2017-02-04 09:37:18 +08:00
Jessica Yu
8e1aef2893 Merge pull request #659 from joe-lawrence/4.9-unwinder
RFC - 4.9 unwinder
2017-01-30 09:57:04 -08:00
Josh Poimboeuf
ab5e1290bb lookup: use log.h version of ERROR macro
Use the log.h version of the ERROR macro so the childobj gets printed.
2017-01-25 10:03:23 -06:00
Joe Lawrence
56708b43d3 kpatch-build: skip initramfs_data.o
ERROR: initramfs_data.o: 2 unsupported section change(s)
initramfs_data.o: changed section .init.ramfs not selected for inclusion
initramfs_data.o: changed section .init.ramfs.info not selected for inclusion
/usr/local/libexec/kpatch/create-diff-object: unreconcilable difference
cmdline.o: changed function: cmdline_proc_show
ERROR: 1 error(s) encountered. Check /root/.kpatch/build.log for more details.
2017-01-24 13:32:29 -05:00
Josh Poimboeuf
44b03dec65 Merge pull request #607 from flaming-toast/4.7-changes
4.7 changes (WIP)
2017-01-24 09:11:46 -06:00
Jessica Yu
b0f5ded2b0 kpatch-intermediate.h: fix comments 2017-01-23 22:42:25 -08:00
Jessica Yu
43f8683cd7 kpatch-build: rename kpatch-tmp.ko to tmp.ko
Rename kpatch-tmp.ko to tmp.ko, since create-klp-module will run into
issues if we happen to have a patch named tmp.patch
2017-01-23 22:39:05 -08:00
Josh Poimboeuf
ac899455ab create-diff-object: convert global symbols to local
As reported in PR #658, when patching an exported function which is used
by the patch module init code, the init code will incorrectly link to
the patched version of the function and will call the patched function
before its relocations have been applied.

For example, when patching __kmalloc(), I see:

  # insmod kpatch-kmalloc.ko
  kpatch_kmalloc: module is marked as livepatch module, but livepatch support is disabled
  kernel tried to execute NX-protected page - exploit attempt? (uid: 0)
  BUG: unable to handle kernel paging request at ffffc90001d03c58
  IP: 0xffffc90001d03c58
  PGD 7c8b4067
  PUD 7c8b5067
  PMD 755f7067
  PTE 800000007ad03163
  Oops: 0011 [#1] PREEMPT SMP
  Modules linked in: kpatch_kmalloc(OE+) kpatch(OE) ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack cfg80211 rfkill ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw snd_hda_codec_generic ppdev crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel snd_hda_intel virtio_balloon snd_hda_codec snd_hwdep serio_raw virtio_console snd_hda_core parport_pc parport snd_pcm virtio_net snd_timer snd i2c_piix4 soundcore virtio_blk qxl drm_kms_helper ttm drm virtio_pci virtio_ring virtio ata_generic pata_acpi
  CPU: 1 PID: 1042 Comm: insmod Tainted: G           OE   4.10.0-rc5 #1
  Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.1-1.fc24 04/01/2014
  task: ffff88007560d480 task.stack: ffffc90001d00000
  RIP: 0010:0xffffc90001d03c58
  RSP: 0018:ffffc90001d03c50 EFLAGS: 00010286
  RAX: 0000000000000004 RBX: 0000000000000008 RCX: 0000000000000000
  RDX: 0000000000000000 RSI: 00000000014080c0 RDI: 0000000000000008
  RBP: ffff88007ad00058 R08: 0000000000000001 R09: 0000000000000001
  R10: 0000000000000000 R11: 0000000000000001 R12: 00000000fffffff4
  R13: ffffffffa0394ef0 R14: 0000000000000000 R15: ffffc90001d03ea8
  FS:  00007f9689d74700(0000) GS:ffff88007d000000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: ffffc90001d03c58 CR3: 000000007af1d000 CR4: 00000000001406e0
  Call Trace:
   ? kzalloc.constprop.6+0xe/0x10 [kpatch_kmalloc]
   ? patch_init+0xa6/0x1000 [kpatch_kmalloc]
   ? 0xffffffffa0399000
   ? do_one_initcall+0x52/0x1b0
   ? do_init_module+0x27/0x1fa
   ? rcu_read_lock_sched_held+0x4a/0x80
   ? kmem_cache_alloc_trace+0x28a/0x2f0
   ? do_init_module+0x5f/0x1fa
   ? load_module+0x2446/0x2b90
   ? __symbol_put+0x90/0x90
   ? vfs_read+0x137/0x180
   ? SYSC_finit_module+0xdf/0x110
   ? SyS_finit_module+0xe/0x10
   ? entry_SYSCALL_64_fastpath+0x1f/0xc2
  Code: c9 ff ff 10 00 00 00 00 00 00 00 86 02 01 00 00 00 00 00 50 3c d0 01 00 c9 ff ff 18 00 00 00 00 00 00 00 b4 33 39 a0 ff ff ff ff <a0> 3c d0 01 00 c9 ff ff a6 90 39 a0 ff ff ff ff 00 00 00 00 00
  RIP: 0xffffc90001d03c58 RSP: ffffc90001d03c50
  CR2: ffffc90001d03c58
2017-01-23 15:50:13 -06:00
Jessica Yu
b1cdc83d57 kpatch-build: build dynrelas or klp relas depending on kernel version
Introduce a second phase in the kpatch-build process that creates kpatch
modules or livepatch modules that use the new klp rela sections depending on
the kernel version being worked on. This change uses the two new programs to
either create a patch module that uses dynrelas (create-kpatch-module) or a
patch module that uses klp rela and arch sections + klp symbols marked with the
correct Elf flags (create-klp-module).

For klp patch modules, the --unique flag for ld is needed to prevent
.parainstructions and .altinstructions sections from different objects
from being merged, as arch_klp_init_object_loaded() applies these sections
per-object.
2017-01-23 12:43:43 -08:00
Jessica Yu
0cc693ba36 create-kpatch-module: add new program that creates kpatch modules
Add new program create-kpatch-module, that, given an intermediate object
outputted by create-diff-object, outputs an object (.o) that contains the
.kpatch.dynrelas section required by kpatch.
2017-01-23 12:43:34 -08:00
Jessica Yu
09d2af5cb4 create-klp-module: add new program that creates patch modules that have klp elements
Add a new program, create-klp-module, that, given a built module (.ko),
will create a patch module with klp rela sections, klp arch sections, and
klp symbols.
2017-01-23 12:43:31 -08:00
Jessica Yu
900d28fe75 kpatch-elf: add find_rela_by_offset()
Add find_rela_by_offset(), which, given a relocation section and offset,
will return the rela struct with the matching offset.
2017-01-23 12:43:27 -08:00
Jessica Yu
42902d0fcc create-diff-object: create intermediate .kpatch.arch section
In addition to .kpatch.relocations and .kpatch.symbols, have
create-diff-object create an .kpatch.arch section. This section can be used
to create .klp.arch. sections that are required for klp modules built for
versions >= 4.9. Each entry in the .kpatch.arch section represents an
arch-specific section (.altinstructions or .parainstructions) and contains
a pointer to the arch-specific section itself (see kpatch_arch struct
member 'sec') and a pointer to the objname string (see kpatch_arch struct
member 'objname'). This is enough information to be able to build
.klp.arch. sections in a later phase of kpatch-build.
2017-01-23 12:43:24 -08:00
Jessica Yu
87643703a7 create-diff-object: create .kpatch.relocations and .kpatch.symbols sections
Instead of creating dynrela sections, have create-diff-object create
intermediate sections .kpatch.relocations and .kpatch.symbols which can
then be used to build (depending on kernel version) either dynrela sections
or klp rela/klp arch sections + klp symbols in a later phase of kpatch-build.
2017-01-23 12:43:17 -08:00
Jessica Yu
58de46cb9e lookup: parse Module.symvers in lookup_open()
Have lookup_open() also parse Module.symvers and add the resulting symbols
and their objnames to the lookup table. This code was essentially
cherry-picked from Josh Poimboeuf's lookup code found here:

  8cdca59c88

That patch was modified to fix a bug in obj_read() (calling elf_end()
without strdup'ing the symbol name strings, which was causing null
dereferences) and to fix up the module name after reading it from
Module.symvers (replacing '-' with '_' and stripping the path prefixes).

Also, add lookup_exported_symbol_objname(), which looks up the objname of
an exported symbol by making use of the objname information obtained from
Module.symvers.
2017-01-23 12:43:12 -08:00
Jessica Yu
b6a15f3dd6 create-diff-object: rename 'name' variable to 'objname'
'name' isn't very descriptive, rename it to 'objname' to avoid confusion
2017-01-23 12:43:09 -08:00
Jessica Yu
52e2ad66ca kpatch-elf: add kpatch_remove_and_free_section()
Add kpatch_remove_and_free_section(), which, given a section name,
removes and frees all matching sections from the passed in kpatch_elf.
2017-01-23 12:43:05 -08:00
Jessica Yu
dac26b8cb2 kpatch-elf: for rela sections, find base section by index rather than name
If there exist multiple sections with the same name (which can happen when
using the --unique option with ld, which will be used to keep multiple
(per-object) .parainstructions and .altinstructions sections separate),
find_section_by_name() will only return the first section name match, which
leads to incorrect base section assignments for rela sections. Fix this by
using the sh_info field of the rela section to find its base section
instead, which contains the index of the section to which the relocation
applies.
2017-01-23 12:43:00 -08:00
Jessica Yu
a3108de96a kpatch-elf: fix null dereference when sym->sec is NULL
Make sure sym->sec is not NULL before checking for its rela section
(sym->sec->rela). This fixes a case where an object may have STT_FUNC
symbols whose the sections (sym->sec) were not selected for inclusion (or
are located in another object) and hence these symbols do not have sym->sec
set. This corner case only recently popped up after reusing kpatch_elf_open()
on objects that have been outputted by create-diff-object (and these
objects only contain the necessary sections needed for the patch module).

This will also automatically exclude livepatch symbols from the check,
because they do not have sections associated with them (i.e., sym->sec is
NULL). We do not have to check for fentry calls for klp (SHN_LIVEPATCH)
symbols, because [1] they do not have sections associated with them, [2]
they are not the target functions to be patched, and [3] they are
technically just placeholder symbols for symbol resolution in livepatch.
2017-01-23 12:42:56 -08:00
Jessica Yu
91909e9273 kpatch-elf: ensure SHN_LIVEPATCH syms don't get set to SHN_UNDEF when reindexing elements 2017-01-23 12:42:52 -08:00
Jessica Yu
2c3c44fec2 kpatch-elf: add kpatch_reindex_elements() and kpatch_rebuild_rela_section_data()
Move functions kpatch_reindex_elements() and kpatch_rebuild_rela_section_data()
from create-diff-object.c to kpatch-elf.c. These functions will be used
to rebuild kpatch elf data in create-klp-module and create-kpatch-module,
i.e. during the second "phase" of kpatch-build.
2017-01-23 12:42:47 -08:00
Jessica Yu
6e43062409 kpatch-elf: add livepatch related Elf constants 2017-01-23 12:42:42 -08:00
Jessica Yu
3d6ea904e8 log: include error.h
Usage of error() requires error.h
2017-01-23 12:42:35 -08:00
Josh Poimboeuf
bc76e64b8b Merge pull request #649 from kamalesh-babulal/ubuntu_bug_fix
kpatch-build: Add UTS_UBUNTU_RELEASE_ABI tag for non-stock kernel
2017-01-16 08:58:57 -06:00