Commit Graph

19 Commits

Author SHA1 Message Date
David Vernet 9ee9f7a3ec kpatch-build: Add missing allocation failure checks
In kpatch-build, there are a number of places where a dynamic allocation
is performed, but the allocation is not checked for a failure. The
common pattern in kpatch-build is to check whether the returned pointer
is NULL, and if so, invoke the ERROR() macro to print a message and
abort the program.

kpatch_create_mcount_sections(), CORRELATE_ELEMENT(), and
create_klp_arch_sections() all had dynamic allocations without failure
checks. This diff adjusts those callsites to properly check for a failed
allocation, and ERROR() accordingly.

Signed-off-by: David Vernet <void@manifault.com>
2022-01-14 07:32:54 -08:00
Joe Lawrence ef420050e3 kpatch-elf: pass new ELF output file mode to kpatch_write_output_elf()
Currently all the callers of kpatch_write_output_elf() are creating
.o object files or .ko kernel modules.  Neither of these filetypes are
executable on their own, so enhance kpatch_write_output_elf() to accept
file creation mode and update its callers to pass 0664 to match
the expected permissions.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2020-09-25 09:30:13 -04:00
Josh Poimboeuf 6cc03f9599 lookup: rename 'pos' to 'sympos'
To more accurately describe its purpose.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2020-04-06 15:18:58 -05:00
Kamalesh Babulal ea9e708df0 create-klp-module: Fix -Wconversion warnings
Fix warnings reported by -Wconversion,-Wno-sign-conversion flags.

Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-02-11 19:25:04 +05:30
Artem Savkov 5cd9833b0d create-klp-module: check symtab in main()
Make sure symtab section was found before dereferencing it.

Found by covscan, see issue #984 for full log.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2019-08-05 12:10:47 +02:00
Artem Savkov bba18e3612 create-klp-module: remove unused variable in create_klp_arch_sections()
Only user of "entries" variable was sizeof and the value was never
actually used. Use struct name directly instead.

Found by covscan, see issue #984 for full log.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2019-08-05 12:10:47 +02: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
Joe Lawrence de10550fae create-klp-module: group .kpatch.symbols with like-scope
From Oracle's Linker and Libraries Guide [1]:

"The symbols in a symbol table are written in the following order ...
The global symbols immediately follow the local symbols in the symbol
table. The first global symbol is identified by the symbol table sh_info
value. Local and global symbols are always kept separate in this manner,
and cannot be mixed together."

[1] https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter6-79797/index.html

Fixes #854.
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2018-05-29 15:30:39 -04:00
Josh Poimboeuf 3d04265899 create-klp-module: Properly align merged .parainstructions sections
When a patch is composed of multiple .o files which have
.parainstructions sections, loading the patch causes a panic:

  general protection fault: 0000 [#1] SMP
  Modules linked in: livepatch_4_9_88_1_20180518_1(OK+) livepatch_4_9_88_1_20180510_1(OK) ...
  CPU: 1 PID: 17257 Comm: insmod Tainted: G           O  K 4.9.0-6-amd64 #1 Debian 4.9.88-1
  Hardware name: HP ProLiant MicroServer Gen8, BIOS J06 11/02/2015
  task: ffff9ff3411a4480 task.stack: ffffac8f8271c000
  RIP: 0010:[<ffffffff8ae2e1d0>]  [<ffffffff8ae2e1d0>] apply_paravirt+0xc0/0x140
  RSP: 0018:ffffac8f8271f9a0  EFLAGS: 00010216
  RAX: 00010749ffffffff RBX: ffffffffc0940658 RCX: 0000000000000085
  RDX: 00000000bfebfbff RSI: ffffac8f8271f9a2 RDI: 0000000000000246
  RBP: ffffac8f8271f9a2 R08: 0000000000000085 R09: ffffffff8ae5acb0
  R10: 0000000000000001 R11: ffff9ff3544b4400 R12: ffffffffc0940660
  R13: 0000000000000000 R14: ffff9ff3544b49c0 R15: ffff9ff3b43f0800
  FS:  00007f04c1cea700(0000) GS:ffff9ff3ca640000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: 0000560cfd63e460 CR3: 00000001455c6000 CR4: 0000000000160670
  Stack:
   401f0ff889486973 6172007172006b00 746c00650031312e 007265746e655f69
   74006e6f69007870 ffffac8f006e6f69 00ffac8f8271fa28 ffffffff8b13ae86
   ffffac8f8271fa68 ffffffffc09471ec ffffffff8b7da9eb 0000000affffffff
  Call Trace:
   [<ffffffff8b13ae86>] ? vsscanf+0x4c6/0x800
   [<ffffffff8b13b20e>] ? sscanf+0x4e/0x70
   [<ffffffff8ae52be5>] ? arch_klp_init_object_loaded+0x105/0x130
   [<ffffffff8b13b0be>] ? vsscanf+0x6fe/0x800
   [<ffffffff8b13b20e>] ? sscanf+0x4e/0x70
   [<ffffffff8aee29e8>] ? klp_init_object_loaded+0xf8/0x210
   [<ffffffff8aee2d85>] ? klp_register_patch+0x285/0x390
   [<ffffffffc09491fa>] ? patch_init+0x1fa/0x1000 [livepatch_4_9_88_1_20180518_1]
   [<ffffffffc0949000>] ? 0xffffffffc0949000
   [<ffffffff8ae0218e>] ? do_one_initcall+0x4e/0x180
   [<ffffffff8afc87dd>] ? __vunmap+0x6d/0xc0
   [<ffffffff8afc87dd>] ? __vunmap+0x6d/0xc0
   [<ffffffff8af7eaa1>] ? do_init_module+0x5b/0x1ed
   [<ffffffff8af025a6>] ? load_module+0x2596/0x2ab0
   [<ffffffff8aefed50>] ? __symbol_put+0x60/0x60
   [<ffffffff8af02d06>] ? SYSC_finit_module+0xc6/0xf0
   [<ffffffff8ae03b7d>] ? do_syscall_64+0x8d/0xf0
   [<ffffffff8b41244e>] ? entry_SYSCALL_64_after_swapgs+0x58/0xc6
  Code: 8d 7c 05 00 e8 62 f7 ff ff 0f b6 53 f9 48 8b 7b f0 48 89 ee e8 f2 f8 ff ff 49 39 dc 76 57 44 0f b6 43 09 41 80 f8 ff 75 84 0f 0b <48> 8b 10 48 8d 7d 08 48 83 e7 f8 48 89 55 00 89 ca 48 8b 74 10
  RIP  [<ffffffff8ae2e1d0>] apply_paravirt+0xc0/0x140
   RSP <ffffac8f8271f9a0>
  ---[ end trace 128c0fa6efe85d9e ]---

The panic is caused by a corrupt .klp.arch.vmlinux..parainstructions
section:

Relocation section [208] '.rela.klp.arch.vmlinux..parainstructions' for section [207] '.klp.arch.vmlinux..parainstructions' at offset 0x29dc78 contains 10 entries:
  Offset              Type            Value               Addend Name
  000000000000000000  X86_64_64       000000000000000000    +750 __get_user_pages
  0x0000000000000010  X86_64_64       000000000000000000    +823 __get_user_pages
  0x0000000000000020  X86_64_64       000000000000000000    +890 __get_user_pages
  0x0000000000000030  X86_64_64       000000000000000000    +941 __get_user_pages
  0x0000000000000040  X86_64_64       000000000000000000   +1631 __get_user_pages
  0x0000000000000050  X86_64_64       000000000000000000   +1671 __get_user_pages
  0x000000000000005c  X86_64_64       000000000000000000   +1245 handle_userfault
  0x000000000000006c  X86_64_64       000000000000000000   +1340 handle_userfault
  0x000000000000007c  X86_64_64       000000000000000000   +1417 handle_userfault
  0x000000000000008c  X86_64_64       000000000000000000   +1717 handle_userfault

The entries are supposed to be 16 bytes each, but notice they become
misaligned starting with the 'handle_userfault' entry.

This happens because the kernel linking process lies about the
.parainstructions section size, underreporting it by four bytes.  So
when two .parainstructions sections are merged together, it results in a
corrupted .klp.arch.vmlinux..parainstructions section.

Fix it by properly aligning the section before merging it with another
one.

Fixes #852.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
2018-05-18 16:11:41 -05: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
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
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
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 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 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
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