Commit Graph

1792 Commits

Author SHA1 Message Date
chenzefeng
23c232d3c1 kpatch-elf: fix Segmentation fault when d_type not set properly
kpatch-elf::create_section_pair would create new rela section, and the
relasec->data->d_type is not set, which is a random value, and it will
use in kpatch-elf::kpatch_write_output_elf
	data->d_type = sec->data->d_type;
which would cause Segmentation fault in kpatch_write_output_elf::elf_update.

Program received signal SIGSEGV, Segmentation fault.
(gdb) bt
0  0x00007ffff7bcd8d2 in __elf64_updatefile at elf64_updatefile.c
1  0x00007ffff7bc9bed in write_file at elf_update.c
2  0x00007ffff7bc9f16 in elf_update at elf_update.c
3  0x000000000040ca3d in kpatch_write_output_elf at kpatch-elf.c
4  0x0000000000409a92 in main at create-diff-object.c

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-06-03 11:29:59 +08:00
Joe Lawrence
0cdc2a3757
Merge pull request #965 from joe-lawrence/altinstr_replacement
kpatch-build: ensure that .altinstr_replacement section is included
2019-05-31 13:51:27 -04:00
Joe Lawrence
ceac47c0e0
Merge pull request #969 from wwheart/master
test: use readelf instead of eu-readelf
2019-05-31 13:50:23 -04:00
chenzefeng
d91b416b00 test: use readelf instead of eu-readelf
readelf is more standard, using readelf insteaded

test/difftree.sh: the symbol name may be with "FILE", which may be
get a incorrect count, here add "awk".

test/unit/Makefile.include: use "readelf -s --wide" instead of
"eu-readelf -s".

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-30 17:45:44 +08:00
Joe Lawrence
5d8b069e12
Merge pull request #968 from wwheart/master
kpatch-build: use readelf instead of eu-readelf
2019-05-29 10:08:54 -04:00
chenzefeng
767d9669bd kpatch-build: use readelf instead of eu-readelf
readelf is more standard, using readelf insteaded we should solve there
issues:

First, using "readelf -s", the symbol name would truncated by 25 chars,
to solve this issue, add option "--wide".

Second, the size may be mixed of decimal and hex, we get the size by "%s",
and use strtoul(size, NULL, 0) to convert the size.

Third, the symbol type is SHN_UNDE, the Ndx display "UND", so changed to
compare with "UND".

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-28 20:36:45 +08:00
Joe Lawrence
13b0014671 kpatch-build: ensure that .altinstr_replacement section is included
.altinstr_replacement section may have relocation symbols which need to
be included, therefore we should call kpatch_include_symbol() to ensure
that its section is included as well.

The special section processing should also occur before
kpatch_print_changes() to provide accurate logging info.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-05-17 10:31:04 -04:00
Josh Poimboeuf
7aa371aa37
Merge pull request #962 from sm00th/fixup-section-fix
Update fixup-section patches
2019-05-15 09:41:02 -05:00
Joe Lawrence
92b9c7d73b
Merge pull request #961 from kamalesh-babulal/shell_check
kpatch, kpatch-build: Use -n instead of ! -z
2019-05-15 10:23:38 -04:00
Joe Lawrence
d40ecd6835
Merge pull request #960 from wwheart/master
kpatch-elf: fix the unexpected elf class
2019-05-15 10:22:41 -04:00
Joe Lawrence
66dfd9ab5b
Merge pull request #958 from kirawrath/master
Making kpatch-build compatible with custom gcc names
2019-05-15 10:21:38 -04:00
Artem Savkov
5dfe17cb02 Update fixup-section patches
These patches previously would try to dereference a userspace pointer
directly which can cause issue on some systems. Since the aim of these
patches is to check fixup section changes we can don't need to do any
meaningful changes to the function, so change the patches to just add
asm("nop") instead.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2019-05-15 10:28:45 +02:00
chenzefeng
3bfc85732d kpatch-elf: fix the unexpected elf classes
kpatch-elf::kpatch_write_output_elf will call the gelf_getclass()
to acquire the output elf's class. But the input parameter kelf->elf
is NULL, the gelf_getclass(kelf->elf) will return ELFCLASSNONE, not
the value we expect ELFCLASS32 or ELFCLASS64.

the gelf_getclass function code:
int
gelf_getclass (Elf *elf)
{
  return elf == NULL || elf->kind != ELF_K_ELF ? ELFCLASSNONE : elf->class;
}

the gelf_newehdr fuction code:
void *
gelf_newehdr (Elf *elf, int class)
{
  return (class == ELFCLASS32
          ? (void *) INTUSE(elf32_newehdr) (elf)
          : (void *) INTUSE(elf64_newehdr) (elf));
}

Luckily, when we create a patch for x86_64 or powerpc64, if we pass the
ELFCLASSNONE for the function gelf_newehdr, it will return elf64_newehdr,
so don't cause the fault. But it's better to use the gelf_getclass(elf)
instead of gelf_getclass(kelf->elf).

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-15 14:10:47 +08:00
Kamalesh Babulal
13e03de0d4 kpatch, kpatch-build: Use -n instead of ! -z
make check using shellcheck version 0.6.0 suggests following
improvements:
In kpatch/kpatch line 160:
        if [[ ! -z "$checksum" ]] && [[ -e "$SYSFS/${modname}/checksum"]] ; then
              ^-- SC2236: Use -n instead of ! -z.

In kpatch-build/kpatch-build line 953:
[[ ! -z "$UNDEFINED" ]] && die "Undefined symbols: $UNDEFINED"
   ^-- SC2236: Use -n instead of ! -z.

'-n' and '! -z' are used interchangeably across the scripts, let's use
'-n' consistently to check a non-empty string instead of using negation.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2019-05-12 21:26:45 +05:30
Joe Lawrence
bfe1c74d4f
Merge pull request #957 from wwheart/master
create-diff-object: remove unneeded condition
2019-05-10 17:06:54 -04:00
Joe Lawrence
d304740269
Merge pull request #959 from kirawrath/systemctl_check
Testing systemctl existence before executing it
2019-05-10 17:04:30 -04:00
Bruno Loreto
d33e1149d1 Testing systemctl existence before executing it
Since commit c9614c4298 kpatch has support for upstart systems,
which means we should test the existence of `systemctl` before executing
it.

The command `command` is POSIX compliant, and should be widely
available.

Signed-of-by: Bruno Loreto <loretob@amazon.com>
2019-05-10 15:02:56 +02:00
chenzefeng
b6e19c7795 create-diff-object: fix the condition for the sections changed
The create-diff-object.c create intermediate ".kpatch.relocations"
sections instead of ".kpatch.dynrelas" sections, and add a new
section ".rela.kpatch.symbols", so we should update the conditions
in function kpatch_create_intermediate_sections for these changed.

Fixes: 87643703a7 ("create-diff-object: create .kpatch.relocations and .kpatch.symbols sections")

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-10 08:58:36 +08:00
Bruno Loreto
4c40c3ff4b Making kpatch-build compatible with custom gcc names
After changing the gcc name in a linux tree to gcc72, kpatch-build failed to
produce hotpatches with the error message "ERROR: no changed objects found."

This is due to a wrapper script called kpatch-gcc, called while kpatch-build
builds the kernel, which checks if the compiler name matches exactly gcc,
failing the check when comparing to gcc72, and thus not producing the expected
file changed_objs containing the list of changed objects.

This commit fixes this issue by loosening the check on the gcc name.

Signed-off-by: Bruno Loreto <loretob@amazon.com>
Reviewed-by: Bjoern Doebel <doebel@amazon.com>
Reviewed-by: Amit Shah <aams@amazon.com>
Reviewed-by: Pawel Wieczorkiewicz <wipawel@amazon.com>
2019-05-09 19:15:11 +02:00
Joe Lawrence
19c7564242
Merge pull request #956 from sm00th/simple_api
Fix livepatch-enabled kernel detection in kpatch script
2019-05-07 10:06:36 -04:00
Josh Poimboeuf
8ac758159c
Merge pull request #955 from wwheart/master
kpatch-build/kpatch-build: fix error of invalid ancestor
2019-05-07 08:38:06 -05:00
Artem Savkov
54c3d6d8fe Fix livepatch-enabled kernel detection in kpatch script
We can no longer use klp_register_patch symbol to determine if the
kernel is livepatch-enabled. Use klp_enable_patch instead.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2019-05-07 14:18:59 +02:00
chenzefeng
eb4f5833e0 kpatch-build: find_parent_obj should search subdirs
The kpatch-build :: find_parent_obj() function's "deep find" may
failed to find objects if they are not located in current directory:

	ERROR: invalid ancestor xxx/xxx.o for xxx/xxx.o.

This is reproducable when building an out-of-tree module of the
following structure:

	wwheart@linux41:~/helloworld 0 > tree -a
	.
	├── buffer_overflow1.ko
	├── .buffer_overflow1.ko.cmd
	├── buffer_overflow1.mod.c
	├── buffer_overflow1.mod.o
	├── .buffer_overflow1.mod.o.cmd
	├── buffer_overflow1.o
	├── .buffer_overflow1.o.cmd
	├── hello.c
	├── hello.o
	├── .hello.o.cmd
	├── Makefile
	├── modules.order
	├── Module.symvers
	├── test.patch
	├── .tmp_versions
	│   └── buffer_overflow1.mod
	└── xxx
	    ├── xxx.c
	    ├── xxx.h
	    ├── xxx.o
	    └── .xxx.o.cmd

	wwheart@linux41:~/helloworld 0 > cat test.patch
	diff --git a/xxx/xxx.c b/xxx/xxx.c
	index aab3c67..d81ad00 100644
	--- a/xxx/xxx.c
	+++ b/xxx/xxx.c
	@@ -1,6 +1,7 @@
	#include <linux/kernel.h>
	void czf_test(void)
	{
	+       printk("livepatch test\n");
		printk("xxx\n");
	}

	wwheart@linux41:~/helloworld 0 > cat Makefile
	obj-m += buffer_overflow1.o
	buffer_overflow1-y += hello.o xxx/xxx.o

Modify the deep find to traverse sub-directories in order to search
the entire tree instead of only the current directory.

Fixes: 8c2792af6c ("kpatch-build: deep find performance improvement")

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-07 15:41:18 +08:00
Joe Lawrence
9f1a0b85a8
Merge pull request #936 from joe-lawrence/kernel-v5.0
livepatch-patch-hook: skip klp_(un)register_patch() for v5.0+ kernels
2019-05-01 15:55:53 -04:00
Joe Lawrence
9ae97c5d29 livepatch-patch-hook: skip klp_(un)register_patch() for v5.1+ kernels
In v5.1, upstream kernel commit 958ef1e39d24 ("livepatch: Simplify API
by removing registration step") removed klp_(un)register_patch().  We
only need to call klp_enable_patch() now.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-05-01 09:35:32 -04:00
Joe Lawrence
4c76b51981
Merge pull request #954 from wwheart/master
kmod: fix memleak in the function patch_exit
2019-05-01 09:32:25 -04:00
chenzefeng
8a319e0133 livepatch-patch-hook: fix memleak in the function patch_exit
reason: after the function klp_unregister_patch, the lpatch must
	be freed, otherwise, it would cause memory leak.

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-04-30 20:48:16 +08:00
Joe Lawrence
d79eb87926
Merge pull request #952 from wwheart/master
fix memleak in the create-klp-module.c
2019-04-29 12:07:48 -04:00
chenzefeng
7513db3c63 fix memleak in the create-klp-module.c
reason: The strdup() function returns a pointer to a new string
	which is a duplicate of the string s.  Memory for the
	new string is obtained with malloc, and can be freed
	with free.

	here, fix memleak by removing the strdup.

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-04-26 11:37:19 +08:00
Josh Poimboeuf
bc7f34b8c5
Merge pull request #953 from kamalesh-babulal/lookup_memleak
lookup: Fix memleak in symtab_read()
2019-04-23 12:57:14 -05:00
Kamalesh Babulal
08a353bdcc lookup: Fix memleak in symtab_read()
Fix memory leak in symtab_read(), by removing the duplicate strdup()
of obj_syms.name.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2019-04-23 12:03:17 +05:30
Joe Lawrence
0c5ed3d9a6
Merge pull request #951 from wwheart/master
create-diff-objject: fix memleak of the struct lookup_table
2019-04-18 13:26:45 -04:00
chenzefeng
8e3ffbc8f6 create-diff-objject: fix memleak of the struct lookup_table
reason: Firstly, in the function lookup_open use the malloc to
	allocate some memory, but call the function lookup_close
	to free the memory.
	Secondly, table->obj_sym->name, table->exp_sym->name and
	table->exp_sym->objname used the strdup, so them should
	free also.
	Thirdly, adjust the order of make_nodname, if not, it
	will cause an exception when free(exp_sym->objname) in
	lookup_close.

Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-04-18 10:15:29 +08:00
Joe Lawrence
f4ed9ff769
Merge pull request #950 from joe-lawrence/tag-v0.6.3
Patch release v0.6.3
2019-04-12 13:32:18 -04:00
Joe Lawrence
dfa5765d2c Patch release v0.6.3
Create a minor release that includes fixes for:

- Lots of integration test work
- Better support for building out-of-tree modules
- Updated manpage options, drop deprecated distro specific mentions
- README.md updates for shadow variables, out-of-tree modules
- Fix core module compilation with CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
- kpatch-build detects and abort on unsupported options
  GCC_PLUGIN_LATENT_ENTROPY, GCC_PLUGIN_RANDSTRUCT
- Fix patch linking with 4.20+
- Other minor shellcheck and kpatch-build fixups

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-04-12 11:18:36 -04:00
Josh Poimboeuf
33a2fd51b1
Merge pull request #949 from wwheart/master
kpatch-build: fix memleak in function kpatch_write_output_elf
2019-04-12 09:45:23 -05:00
chenzefeng
206db25c27 kpatch-build: fix memleak in function kpatch_write_output_elf
Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-04-12 17:09:10 +08:00
Joe Lawrence
05b18e6d0a
Merge pull request #942 from joe-lawrence/oot-fixes
Out of tree module fixes
2019-03-25 15:57:13 -04:00
Joe Lawrence
fff628ca07
Merge pull request #943 from joe-lawrence/unit-test-non-stripped
test/unit: relax stripped object requirement
2019-02-22 10:58:27 -05:00
Joe Lawrence
7dff9b5f6a unit-tests: include oot-issue-928 objects
Add object files that verify kpatch issue #928.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-02-22 10:50:55 -05:00
Joe Lawrence
ccd0615407 test/unit: relax stripped object requirement
Some unit tests may need debug symbols to reproduce problems (see
issue #928 for example), so skip the unit-test Makefile.include
check_stripped call for objects that include "NOSTRIP" in their
filename.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-02-22 10:49:54 -05:00
Joe Lawrence
37672ef681 manpages: update kpatch-build with --oot-module option
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-02-21 09:47:47 -05:00
Josh Poimboeuf
2763560367
Merge pull request #944 from jpoimboe/revert-jump-label
Revert "create-diff-object: add jump label support"
2019-02-20 08:05:59 -06:00
Josh Poimboeuf
09ee03f3df Revert "create-diff-object: add jump label support"
This reverts commit 87c64519fc.

The jump label support doesn't work with upstream livepatch.  Joe
Lawrence found the following ordering issue:

load_module

  apply_relocations

    /* Livepatch relocation sections are applied by livepatch */
    if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
            continue;

  post_relocation
    module_finalize
      jump_label_apply_nops        << crash

  ...

  do_init_module
    do_one_initcall(mod->init)
      __init patch_init [kpatch-patch]
        klp_register_patch
          klp_init_patch
            klp_init_object
              klp_init_object_loaded
                klp_write_object_relocations

So jump_label_apply_nops() is called *before*
klp_write_object_relocations() has had a chance to write the klp
relocations (.klp.rela.kvm_intel.__jump_table, for example).

We need to resolve this upstream first.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2019-02-19 16:10:54 -06:00
Joe Lawrence
b4e6085b6d
Merge pull request #928 from haoren3696/master
kpatch-build: include secsym in kpatch_mark_ignored_sections
2019-02-19 13:24:42 -05:00
Joe Lawrence
fd9806b152 kpatch-gcc: use relative path when filtering objects to ignore
When building out-of-tree modules, gcc may be passed full source
pathnames (like /home/user/testmod/testmod.c).  Adjust the filepath
filtering in kpatch-gcc to match against files relative to the
KPATCH_GCC_SRCDIR / kpatch-build SRCDIR prefix.

Fixes: #941
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-02-15 10:10:24 -05:00
Joe Lawrence
9863f5fa9b README.md: fix up out-of-tree module command line switches
For consistency, use the long format command line options in the
description and sample invocation.  Also, use "--oot-module" and not
"--out-of-tree" as per kpatch-build sources.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2019-02-15 10:09:46 -05:00
Joe Lawrence
a16c003d88
Merge pull request #937 from jpoimboe/jump-tables
Add jump label support
2019-02-11 13:47:36 -05:00
Josh Poimboeuf
87c64519fc create-diff-object: add jump label support
Add support for jump labels, also known as static jumps, static keys,
static branches, and jump tables.  Luckily,
kpatch_process_special_sections() is already generic enough to make this
an easy fix.

Fixes: #931

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2019-02-08 13:45:38 -06:00
Joe Lawrence
5a0c0ad944
Merge pull request #938 from sm00th/unitenv
test/unit: add support for .env files
2019-02-08 10:45:21 -05:00