Commit Graph

707 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
Josh Poimboeuf
09ee03f3df Revert "create-diff-object: add jump label support"
This reverts commit 87c64519fce02424e00725ddaeff8b619d40d160.

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
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
Josh Poimboeuf
d8a44076f8 create-diff-object: cleanup special section array
Clean up the special section array a bit, to make it a little more
readable.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2019-01-29 13:04:09 -06:00
Balbir singh
3998784d71 Fix NULL pointer deref in main due to base_locals
For fun I tried to create a livepatch of upstream patch
ad211f3e94b314a910d4af03178a0b52a7d1ee0a for my kernel. This
caused kpatch-build to fail with a NULL pointer derefence because
base_locals was NULL (returned via kpatch_elf_locals(), which
can return a NULL pointer). This patch fixes the SIGSEGV
via a NULL check. The end result is a live patch is created
and loaded.

Signed-off-by: Balbir singh <bsingharora@gmail.com>
2019-01-17 19:53:46 +11:00
Zhipeng Xie
517e26a6cb kpatch-build: include secsym in kpatch_mark_ignored_sections
kpatch_mark_ignored_sections include .rodata.str1.1 section but does
not include its section symbol, causing its section symbol can not be
included any more in kpatch_include_standard_elements. After the
section symbol is freed in kpatch_elf_teardown, we got a segmentation
fault in kpatch_create_intermediate_sections.

Signed-off-by: Zhipeng Xie <xiezhipeng1@huawei.com>
2018-11-23 10:50:21 +08:00
Joe Lawrence
f06f65666a
Merge pull request #925 from rudis/master
kpatch-build: abort on unsupported options GCC_PLUGIN_LATENT_ENTROPY,…
2018-11-14 11:15:16 -05:00
Paul Dagnelie
51a8fad34f Add support for building out-of-tree modules 2018-11-09 08:22:39 -08:00
Simon Ruderich
2441cdd7ba kpatch-build: abort on unsupported options GCC_PLUGIN_LATENT_ENTROPY, GCC_PLUGIN_RANDSTRUCT
Both generate randomly modified object files on each build. This breaks
comparing original and patched object file. See also #924.

Signed-off-by: Simon Ruderich <simon@ruderich.org>
2018-10-27 08:23:24 +02:00
Simon Ruderich
3cd2e1efd0 kpatch-build/kpatch-build: use command -v instead of which
Prevents the following shellcheck warning:

    In kpatch-build/kpatch-build line 583:
    which yumdownloader &>/dev/null || die "yumdownloader (yum-utils or dnf-utils) not installed"
    ^-- SC2230: which is non-standard. Use builtin 'command -v' instead.

Signed-off-by: Simon Ruderich <simon@ruderich.org>
2018-10-21 08:45:57 +02:00
Joe Lawrence
019c1fb3eb
Merge pull request #919 from jpoimboe/__FUNCTION__-special-static
create-diff-object: add __FUNCTION__ variables to the special static …
2018-10-18 09:46:03 -04:00
Joe Lawrence
3ac8b2e038
Merge pull request #917 from sm00th/addressable
Add "__addressable_" to maybe_discarded_sym().
2018-10-18 09:43:39 -04:00
Josh Poimboeuf
72103a178c create-diff-object: add __FUNCTION__ variables to the special static list
As discovered in #918, the `__FUNCTION__` static local variable is
similar to the `__func__` variable, in that it refers to the current
function name.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-10-17 21:08:30 -05:00
Artem Savkov
f959edecdb Move lookup_open() call to a later stage
Sometimes due to config-dependency issues or other reasons whole
object-files would get optimized out from final vmlinux/module, in cases
like this create-diff-object would fail during symbol lookup table
creation in lookup_open(). Because lookup_open() call is situated before
we established that objectfile has changed this triggers not only on
real problems, but also during mass-rebulds caused by changes to
header-files. While it usually indicates a real issue with config this
should not prevent kpatch from building.

Move lookup_open() call so that it is called only for changed
object-files.

Fixes #910

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-10-17 10:23:40 +02:00
Artem Savkov
c46191028e strdup symbol names from kelf_base
strdup symbol names in kpatch_elf_locals and when noting down hint
instead of just copying pointers so that they are still usable after
we teardown/free kelf_base.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-10-17 10:21:44 +02:00
Artem Savkov
722d27f6bd Add "__addressable_" to maybe_discarded_sym().
Starting with 1b1eeca7e4c1 "init: allow initcall tables to be emitted using
relative references" [1] __init functions are generating an "__addressable_"
symbol in a ".discarded.addressable" section so it does not show up in final
vmlinux triggering find_local_syms failures. Add "_addressable_" to the list
in maybe_discarded_sym().

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1b1eeca7e4c19fa76d409d4c7b338dba21f2df45

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-10-16 17:26:38 +02:00
Artem Savkov
f5679c9780 symtab_read: support entries with blank names
symtab_read() would previously skip entries with blank names resulting
in some of important entries being skipped. For instance vmlinux file
has an STT_FILE entry at the end with a blank name that contains global
offset table. Because it was skipped all of the global entries from this
table were considered a part of previous processed file resulting in
create-diff-object failing in find_local_syms().

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-09-20 16:06:29 +02:00
Artem Savkov
f7cfe25e8a symtab_read(): fix SECTION detection in symtab_read
symtab_read has been checking a wrong field for "SECTION". Switch the
field from "bind" to "type".

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-09-12 16:31:58 +02:00
Evgenii Shatokhin
0215587ad1 kpatch-build: ppc64le - fix a typo in find_special_section_data_ppc64le
Nothing critical, but find_special_section_data_ppc64le() could run
longer than needed: the exit condition was not met after all the values
had been found.

Fixes: 77f8fd09 "kpatch-build: ppc64le - Add special section support"
Signed-off-by: Evgenii Shatokhin <eshatokhin@virtuozzo.com>
2018-09-11 13:21:14 +03:00
Joe Lawrence
3a45a6ebb3
Merge pull request #900 from kamalesh-babulal/data_rel
create-diff-object.c: Extend is_bundleable()
2018-08-07 09:28:48 -04:00
Kamalesh Babulal
5b690b28bf create-diff-object.c: Extend is_bundleable()
GCC puts the constant variable requiring relocation into .data.rel. or
.data.rel.ro depending upon the bind type of the symbol. Extend
is_bundledable() to check these .data sections too.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-08-06 14:09:43 +05:30
Joe Lawrence
bc268c60e1 kpatch-build: update for RHEL / CentOS 8
- Future releases of RHEL / CentOS will provide the yumdownloader
  program with the 'dnf-utils' package (not 'yum-utils').  Instead of
  looking to see that the package is installed, just look for the
  program itself.

- RHEL / CentOS 8 kernel release names (as returned by 'uname -r') may
  not match the SRPM buildroot release subdirectory name.  Relax the
  wildcard when moving this directory to $SRCDIR.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-07-31 16:26:59 -04:00
Artem Savkov
19c4b52105 create-diff-object: -mcount-record support
4.18 adds -mcount-record to KBUILD_FLAGS when supported by the compiler.
This results in most of kpatch_create_mcount_sections()'s work being
already done, so we can at least skip the last part of it that updates
the first instruction in patched functions.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-26 16:51:24 +02:00
Lennert Buytenhek
932e0377bd kpatch-build: Ubuntu signed/unsigned kernel image package fix
In Ubuntu 18.04 LTS (but not in 16.04 LTS), the "linux" source package
no longer builds the "linux-image-*" binary kernel image packages
directly, but instead, it produces the "linux-image-unsigned-*" binary
packages, and the "linux-signed" source package then produces the
(signed) "linux-image-*" binary packages from the unsigned binaries.

This means that querying the target kernel's linux-image-* package for
its source package will yield a source package that is just a wrapper,
and does not actually contain the kernel source code.

Deal with this by removing the "-signed" substring from the kernel
source package name if it is present.  This makes kpatch-build work
on Ubuntu 18.04.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
2018-07-25 14:59:56 +03:00
Joe Lawrence
b974770197
Merge pull request #881 from sm00th/gcc8-opts
gcc8 fixes
2018-07-18 08:56:54 -04:00
Josh Poimboeuf
0655ca50b8 kpatch-build: fix clean_cache
Commit d86c1113cc ("kpatch-build: less aggressive clean_cache()")
broke clean_cache().  Instead of expanding the wildcard, it tries to
delete a file named '*'.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-07-17 15:43:43 -05:00
Artem Savkov
61839832ed create-diff-object: propagate ignore.functions to children
Add child symbols to .kpatch.ignore.functions in case their parents are
added to the list.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-13 09:39:26 +02:00
Artem Savkov
2ac1387701 create-diff-object: add .text.hot to the list of bundleable functions
According to gcc8's man pages gcc can put functions into .text.unlikely
or .text.hot subfunctions during optimization. Add ".text.hot" to the
list of bundleable functions in is_bundleable().

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-13 09:39:26 +02:00
Artem Savkov
35cc6ff016 create-diff-object: allow changing subsections
gcc8 can place functions to .text.unlikely and .text.hot subsections
during optimizations. Allow symbols to change subsections instead of
failing.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-13 09:39:21 +02:00
Artem Savkov
246c6e2ae7 create-diff-object: propagate child symbol changes
Propagate child symbol changes to it's parent.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-13 09:38:15 +02:00
Artem Savkov
73a278c2b6 create-diff-object: child symbol skips
Skip profiling calls checks for child symbols and don't include them in
output or .kpatch.funcs.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-11 09:57:54 +02:00
Artem Savkov
bd2589530c create-diff-object: add symbol relations
Add a function that would detect parent/child symbol relations. So far
it only supports .cold.* symbols as children.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2018-07-11 09:57:31 +02:00
Kamalesh Babulal
09fdb0772d create-diff-object: Relax sh_addralign check for .text sections
.text section addralign value might change between original and
patched .o files, for a loop() such as:

for (i = 0; i < sections_per_block; i++) {
	remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
	base += MIN_MEMORY_BLOCK_SIZE;
}

On ppc64le, which translate to:

  f0:   78 1b 7b 7c     mr      r27,r3
  f4:   6c 00 9d 40     ble     cr7,160 <pseries_remove_memblock+0x158>
  f8:   ff ff 9c 3b     addi    r28,r28,-1
  fc:   38 00 a1 fb     std     r29,56(r1)
 100:   00 01 bf 3f     addis   r29,r31,256
 104:   08 c2 9c 7b     rldic   r28,r28,24,8
 108:   14 ea 9c 7f     add     r28,r28,r29
 10c:   14 00 00 48     b       120 <pseries_remove_memblock+0x118>
 110:   00 00 00 60     nop
 114:   00 00 00 60     nop
 118:   00 00 00 60     nop
 11c:   00 00 00 60     nop
 120:   78 fb e4 7f     mr      r4,r31

.LVL174:
        rldic 28,28,24,8         #, tmp198, tmp196,
        add 28,28,29     # _45, tmp198, base
        .p2align 5,,31

Patch removing such loop, changes the section alignment boundary. Given
that alignment changes to .text section are not fatal, relax the check
for text sections.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2018-07-04 08:41:00 +05:30
Joe Lawrence
4d1ee7f80a kpatch-build: fix RHEL-ALT kernel version detection
kpatch-build detects RHEL-ALT kernel support by looking for a ".el7a."
substring in the kernel release string.  Look for that substring in the
unchanged $ARCHVERSION instead of $KVER, which may not have the
trailing '.' character that our regex expects.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-06-20 10:37:05 -04:00
Joe Lawrence
bc28b576ed kpatch-build: drop architecture spec from source rpm request
Source RPMs don't have an architecture associated with them, so to avoid
confusion, drop that part of the kernel release string when calling
yumdownloader.

Fixes #887.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-06-20 10:15:49 -04:00
Joe Lawrence
7dc6e5f215
Merge pull request #886 from sm00th/safe_cleancache
kpatch-build: less aggressive clean_cache()
2018-06-19 09:30:07 -04:00
Joe Lawrence
d3ed66a103
Merge pull request #885 from rudis/shellcheck
kpatch-build: fix shellcheck warnings
2018-06-19 09:29:47 -04:00
Joe Lawrence
d0ced9760d
Merge pull request #884 from rudis/master
create-diff-object: prevent "'toc_data1' may be used uninitialized" warning
2018-06-19 09:29:24 -04:00