Commit Graph

1408 Commits

Author SHA1 Message Date
loretob
c9614c4298 Adding support to upstart systems.
Currently kpatch rely on systemd to load all kmods on startup.
This patch aims to enable kpatch to be used on upstart systems.

Limitations:
With systemd, it would be possible to unload all modules by issuing:
        systemctl stop kpatch
It was not possible to make a reasonable upstart's equivalent of it, so
to unload the modules it will be necessary to call kpatch explicitly:
        kpatch unload --all

I believe this it an non-issue, as it is still possible to unload
the modules by calling kpatch explicitly.

The file /etc/init/kpatch.conf will be installed unconditionally, and
removed on uninstall.

On my tests I have verified that all newly added files by this commit
are also deleted on uninstall.

It was also verified that applied patches are loaded again on startup.

rpmlint does not complain about anything new.

Signed-off-by: Bruno Loreto <loretob@amazon.com>
2018-04-06 16:12:02 +02:00
Joe Lawrence
9e7db03d3f
Merge pull request #810 from joe-lawrence/sparse
sparse: quiet latest trivial complaints
2018-03-24 15:50:08 -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
4d5febd4a8 sparse: quiet latest trivial complaints
Fixes sparse warnings:

  kmod/core/core.c:142:20: warning: symbol 'trace' was not declared. Should it be static?

  livepatch-patch-hook.c:73:18: warning: symbol 'lpatch' was not declared. Should it be static?

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-03-23 16:20:45 -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
7e1b495f52
Merge pull request #762 from joe-lawrence/fedora-27-rebase
testing fedora 27: rebase integration test patches
2018-03-22 14:43:48 -04:00
Joe Lawrence
1d942ebb41 testing: rebase patches for Fedora 27 4.13.9-300.fc27.x86_64
Rename the old Fedora 25 integration patch directory and rebase to
Fedora 27 sources.

Fixes #761.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-03-22 14:36:34 -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
4031927f13
Merge pull request #805 from jpoimboe/gitignore
gitignore: ignore cache.mk from newer kernel builds
2018-03-22 10:44:27 -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
fefa67554b
Merge pull request #806 from cardoe/drop-rpmdevtools
drop unused rpmdevtools dependency
2018-03-21 22:34:03 -05: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
d4e4258172 gitignore: ignore cache.mk from newer kernel builds
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-03-21 13:22:26 -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
Joe Lawrence
d2fba54b42
Merge pull request #802 from sm00th/builtin_rename
Prepare for built-in.o -> built-in.a rename
2018-03-20 13:50:29 -04: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
Joe Lawrence
bb088de342
Merge pull request #797 from jpoimboe/PPC64_LOCAL_ENTRY_OFFSET-fix
create-diff-object: Fix PPC64_LOCAL_ENTRY_OFFSET usage
2018-03-19 16:15:31 -04:00
Kamalesh Babulal
d651cd994c livepatch-patch-hook: Add upper bound kernel version for immediate flag
Effective Kernel v4.16, the immediate flag is removed by upstream
kernel commit d0807da78e11 ("livepatch: Remove immediate feature").
Add an upper bound kernel version check for inclusion of the
immediate flag.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-03-17 15:00:55 +05:30
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
Joe Lawrence
3adfc41217
Merge pull request #794 from sm00th/dirtytree-fix
Fix resulting module vermagic when source dir is a git tree
2018-03-14 09:58:24 -04: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 f0518ef58afc ("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: f0518ef58afc ("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
Joe Lawrence
07c7200cc9
Merge pull request #790 from euspectre/retry-disable
If kpatch fails to disable a patch, retry a few times
2018-02-28 10:30:50 -05:00
Evgenii Shatokhin
23f4e7554d If kpatch fails to disable a patch, retry a few times
This is similar to how loading of the patches works now. Needed mostly
for the "old" kpatch, i.e. for the kernels that do not support livepatch.

If the patched functions are currently used, loading of the patch fails
with "Device or resource busy" error. kpatch script then retries the
operation several times.

In some cases, it could be convenient to do the same thing when
unloading or simply disabling the patches. One of the use cases is when
it is needed to replace a loaded cumulative patch with its previous
version, esp. if the patches have patch/unpatch hooks. It is often more
reliable to disable the loaded patches first and then load the new
patch. Disable operation may fail due to activeness safety check - so
let us retry it a few times.

v2:
As suggested in PR #790, disable_patch() no longer returns a value but
rather calls die() at the point of error.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-02-28 14:44:00 +03:00
Joe Lawrence
2d0fd42c64
Merge pull request #788 from euspectre/no-hint-in-error-msgs
kpatch-build: 'hint' is not needed in kpatch_create_*_sections()
2018-02-22 10:03:32 -05:00
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
Joe Lawrence
6d0d8d0b04
Merge pull request #786 from euspectre/makefile-krel-fix
kmod/core: fix definition of KERNELRELEASE
2018-02-20 15:08:01 -05:00
Joe Lawrence
17ea5939e1
Merge pull request #787 from euspectre/help-no-initrd
Do not mention initrd in the help messages and in the man page
2018-02-20 15:06:41 -05:00
Evgenii Shatokhin
cc09c5efd3 Do not mention initrd in the help messages and in the man page
Kpatch no longer uses initrd to make sure the patch modules are loaded
at boot. The users could either install the provided systemd service
for that or come up with some other solution.

The messages mentioning initrd could confuse the users.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-02-20 17:27:25 +03:00
Evgenii Shatokhin
03c41d05e3 kmod/core: fix definition of KERNELRELEASE
A cosmetic fix.

If KPATCH_BUILD ending with 'build/' is passed to 'make', KERNELRELEASE
will become 'build' and the error message will look like:

"<...> doesn't exist. Try installing the kernel-devel-build RPM or
linux-headers-build DEB."

Let us fix that.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-02-19 13:13:03 +03:00
Joe Lawrence
84f2405dd3
Merge pull request #784 from euspectre/kpatch-retry-cosmetic
kpatch: a couple small enhancements in retry logic
2018-02-08 11:49:46 -05:00
Evgenii Shatokhin
58cc8fc626 kpatch: make it easier to tune the number of attempts to load the patch
... and the interval between the retries.

If activeness safety check fails and the patch fails to load, kpatch waits
for 2 seconds and then retries loading of that patch.

It may be needed to change the number of retries and the interval
between them in some cases, e.g. during stress testing of patches, etc.

Make it a bit easier by keeping these values in the variables close to
the beginning of the script.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-02-08 15:08:25 +03:00
Evgenii Shatokhin
2ac771fbe2 kpatch: set LC_ALL=C explicitly when loading the patch module
kpatch checks the messages output by insmod to decide if loading failed
with -EBUSY (i.e. activeness safety check failed). It looks for
"Device or resource busy" message, but one cannot guarantee it is not
output in some other language.

Let us use LC_ALL=C to be sure.

Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-02-08 15:08:19 +03:00
Joe Lawrence
4ef38429ab
Merge pull request #782 from bmcculley/patch-1
dnf to yum
2018-01-11 11:19:11 -05:00
bmcculley
c3a1d5c762
fixing RHEL and oracle typos as well. 2018-01-10 20:08:06 -05:00
bmcculley
00cdbee63c
dnf to yum
switch dnf to yum command under centos 7 section.
2018-01-10 20:04:14 -05:00
Josh Poimboeuf
866b6a26ea
Merge pull request #777 from aliceinwire/version_update
version update
2017-12-29 07:55:34 -06:00