Commit Graph

38 Commits

Author SHA1 Message Date
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
Jessica Yu 0bb5c106ef kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.

Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.

Before:
/sys/kernel/kpatch
└── patches
    └── <patch_module>
        ├── checksum
        ├── enabled
        └── functions
            ├── <function>    # from <object1>
            │    ├── new_addr
            │    └── old_addr
            ├── <function>    # from <object2>
            │    ├── new_addr
            │    └── old_addr
            └─── <function>   # from <object3>
                 ├── new_addr
                 └── old_addr

After:
/sys/kernel/kpatch
└── <patch_module>
    ├── <object1>
    │   └── <function,sympos>
    │       ├── new_addr
    │       └── old_addr
    ├── <object2>
    │   └── <function,sympos>
    │       ├── new_addr
    │       └── old_addr
    ├── checksum
    ├── enabled
    └── <object3>
        └── <function,sympos>
            ├── new_addr
            └── old_addr
2017-02-27 20:07:16 -08:00
Joe Lawrence 13fd6f2563 kmod: fix kpatch patch module load if CONFIG_LIVEPATCH=n
Previous commit "kmod: let kernel apply TAINT_LIVEPATCH" modified the
kpatch patch module to set the "livepatch" module info.  This breaks
module loading for kernel config CONFIG_LIVEPATCH=n

  kpatch_kmalloc: module is marked as livepatch module, but livepatch support is disabled

kpatch modules can still use TAINT_LIVEPATCH as a per-module taint flag,
but only if it is set after the module loads.

Fixes: 660.
2017-01-24 15:38:51 -05:00
Joe Lawrence e7937196b7 kmod: let kernel apply TAINT_LIVEPATCH
Upstream commit 2992ef29ae01 ("livepatch/module: make TAINT_LIVEPATCH
module-specific") v4.9+ modified the kernel to add the TAINT_LIVEPATCH
flag on module load.  To support this feature, add the "livepatch"
module info in the {k,live}patch modules and drop the add_taint() in the
core module.
2017-01-12 16:05:53 -05:00
Joe Lawrence a6133bba08 Add CONFIG_RANDOMIZE_BASE KASLR support
Backport the symbol lookup and checking code from upstream livepatch
code that relies on a symbol position enumeration rather than a fixed
memory address.

Fixes #617.
2016-11-29 13:55:34 -05:00
Jessica Yu cd4c0ce02d kpatch-patch-hook: change old_addr and new_addr perms to readable by root only 2014-11-07 21:30:32 -08:00
Josh Poimboeuf f5de932b8d allow patched modules to call external functions
When patching a kernel module, if we can't find a needed dynrela symbol,
we currently assume it's exported.  However, it's also possible that
it's provided by another .o in the patch module.  Add support for that.

Fixes #445.
2014-10-06 23:16:13 -05:00
Josh Poimboeuf a224d2470c patch module function/variable renaming
Make the kpatch-patch-hook.c function and variable names consistent by
prefixing them all with 'patch_'.  This makes it easier to distinguish
the patch hook sections from the patched sections when looking at the
ELF section data.
2014-09-10 21:14:23 -05:00
Jessica Yu 0c9a54645c re-enable patch modules with checksum matching
In order to safely re-enable patch modules, add a special
.kpatch.checksum section containing an md5sum of a patch module's
contents. The contents of this section are exported to sysfs via
patch_init and double checked when kpatch load finds that a module of
the same name is already loaded.
2014-09-09 07:52:16 -04:00
Jessica Yu 6a69f5f91a consolidate variables func->old_offset and func->old_addr to just old_addr
To reduce redundancy, remove/change the old_offset fields in the
kpatch_func and kpatch_patch_func structs to just old_addr. Since
old_offset is being used as a placeholder for old_addr, might as well
consolidate it to just one variable.
2014-08-15 23:42:26 -07:00
Jessica Yu 8464c25d95 kpatch-patch-hook: fix incorrect old_offsets for loadable modules
Fix incorrect old_offsets for loadable modules during sysfs
initialization in patch_init.

sysfs will be initialized on patch module init regardless of whether
or not the module is loaded. func_old_addr_show() will read from func->old_addr,
which is initially set to 0; it'll be eventually filled in by the core module.
2014-08-15 23:42:22 -07:00
Seth Jennings 2e93c5e1e3 support forced patching
Some functions in the kernel are always on the stack of some thread
in the system.  Attempts to patch these function will currently always
fail the activeness safety check.

However, through human inspection, it can be determined that, for a
particular function, consistency is maintained even if the old and new
versions of the function run concurrently.

This commit introduces a KPATCH_FORCE_UNSAFE() macro to define patched
functions that such be exempted from the activeness safety check.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-07-02 14:06:33 -05:00
Seth Jennings 4835e3edc3 add user-defined load/unload hook support
This commit enables the ability to create user-defined hooks as part of
the normal code patch that can do preparatory work for the application
of the patch.  This work could include, but is not limited to, changing
data structure semantics.

The user may define a new function as part of the patch and mark it as a
load-time or unload-time hook with the kpatch_load_hook() and
kpatch_unload_hook() macros.  These macros are in an include file that
gets copied into the source tree at include/linux/kpatch-hooks.h at
patch build time. The signature for both hooks is "int kpatch_unload_hook(void)".

For now, the return code is ignored.  The hooks may not fail.  They also
run in stop_machine() context and may not sleep.  These hooks, more or
less, must follow all the rules of interrupt context code.
2014-06-30 13:37:26 -05:00
Seth Jennings e8d2ab565b kmod: patch: cleanup error paths
Right now, if there is a failure in patch_make_dynrelas_list(),
patch_free_objects() is called twice; once in the error section of
patch_make_dynrelas_list() and again in the err_objects section of
patch_init().

This fixes this and cleans up the error handling a bit.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-06-24 12:50:26 -05:00
Josh Poimboeuf 6b45c5dbd0 remove unused kpatch_dynrela.objname field 2014-06-18 11:17:55 -05:00
Josh Poimboeuf 34cc258a31 fix undefined symbols for future loaded modules
When patching module A, if one of the new function's relas reference a
symbol in module B, we currently just leave it as a normal rela.  But if
module B hasn't been loaded yet, the patch module will fail to load due
to the rela's reference to an undefined symbol.

The fix is to convert these relas to dynrelas, which can be resolved
later in the module notifier when A is loaded.

Also added support for the R_X86_64_NONE relocation type, needed for
dynrelas which reference __fentry__.
2014-06-18 11:17:11 -05:00
Josh Poimboeuf 827c91bae6 fix review comments
- get rid of unneeded vmlinux variable
- create patch_make_funcs_list() and patch_make_dynrelas_list()
2014-06-17 12:17:58 -05:00
Josh Poimboeuf 84c34ff584 implement per-object patching/relocations
The recent module patching code has exposed some problems with our data
structures. We currently patch the funcs and dynrelas individually,
which is kind of scary now that different objects can be patched at
different times.  Instead it's cleaner and safer to group them by
patched object.

This patch implements per-object patching and relocations by refactoring
the interfaces:

- Completely separate the create-diff-object <-> patch module interface
  from the patch module <-> core module interface. create-diff-object
  will include "kpatch-patch.h" but not "kpatch.h". Thus,
  create-diff-object has no knowledge about the core module's
  interfaces, and the core module has no knowledge about the patch
  module's special sections.

- Newly added kpatch-patch.h defines the format of the patch module
  special sections. It's used by create-diff-object to create the
  special sections and used by the patch module to read them.

- kpatch.h still defines the core module interfaces. Each kpatch_module
  has a list of kpatch_objects for each module object to be patched.
  Each kpatch_object has a list of kpatch_funcs and a list of
  kpatch_dynrelas. The patch module creates these lists when populating
  kpatch_module.

This way of structuring the data allows us to patch funcs and dynrelas
on a per patched object basis, which will allow us to catch more error
scenarios and make the code easier to manage going forward.  It also
allows the use of much more common code between kpatch_register() and
kpatch_module_notify().
2014-06-17 10:03:08 -05:00
Seth Jennings aa8c5da5ba change struct kpatch_patch s/old_addr/old_offset
Make old addresses relative to the start address of the relocatable
kernel or module.

This commit has no functional effect; it just prepares the code for
future acceptance of the module patching support.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-06-02 15:57:12 -05:00
Jincheng Miao 827a143caf kmod/patch: export patched functions information via sysfs
This feature is implemented as:
```
[root@localhost kpatch]# insmod ./kpatch-meminfo.ko
[root@localhost kpatch]# ls /sys/kernel/kpatch/patches/kpatch_meminfo/functions/meminfo_proc_show/
new_addr  old_addr
[root@localhost kpatch]# cat /sys/kernel/kpatch/patches/kpatch_meminfo/functions/meminfo_proc_show/new_addr
0xffffffffa05211e0
[root@localhost kpatch]# cat /sys/kernel/kpatch/patches/kpatch_meminfo/functions/meminfo_proc_show/old_addr
0xffffffff8125d0e0
```

The patch module init function will allocate and init kpatch_func_obj with
customized kobj_type func_ktype. The attribute new_addr and old_addr of
kpatch_func_obj is attached to this func_ktype, so that these files could
be created by kobject_add automatically.

Signed-off-by: Jincheng Miao <jincheng.miao@gmail.com>
2014-05-27 21:56:56 +08:00
Seth Jennings 21fc274448 dynrelas support, obsoleting link-vmlinux-syms
This adds dynamic linking support for the patch modules.  It is the
first step toward supporting patching module code and relocatable
kernels.

Rela entries that reference non-included local and non-exported global
symbols are converted to "dynrelas".  These dynrelas are relocations
that are done by the core module, not the kernel module linker.  This
allows the core module to apply offsets to the base addresses found
in the base vmlinux or module.

Signed-off-by: Seth Jennings <sjenning@redhat.com>

Conflicts:
	kpatch-build/kpatch-build
2014-05-20 12:44:31 -05:00
Seth Jennings 797b7af234 cleanup: remove unneeded patches variable
Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-05-20 12:44:30 -05:00
Josh Poimboeuf 5e25365244 Revert #186 (add dynamic symbol linking support)
We merged PR #186 a little too hastily.  It seg faults with the new
parainstructions-section.patch in the integration test suite.  Reverting
it for now until we get it figured out.

This reverts commit e1177e3a03.
This reverts commit 880e271841.
This reverts commit 2de5f6cbfb.
This reverts commit 38b7ac74ad.
This reverts commit 108cd9f95e.
2014-05-15 17:34:16 -05:00
Seth Jennings 880e271841 dynrelas support, obsoleting link-vmlinux-syms
This adds dynamic linking support for the patch modules.  It is the
first step toward supporting patching module code and relocatable
kernels.

Rela entries that reference non-included local and non-exported global
symbols are converted to "dynrelas".  These dynrelas are relocations
that are done by the core module, not the kernel module linker.  This
allows the core module to apply offsets to the base addresses found
in the base vmlinux or module.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-05-15 13:29:15 -05:00
Seth Jennings 108cd9f95e cleanup: remove unneeded patches variable
Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-05-14 14:04:54 -05:00
Josh Poimboeuf 5ea376a47a Merge pull request #181 from spartacus06/interface-refactor
refactor core <-> patch interface
2014-05-12 15:43:26 -05:00
Seth Jennings e5f17c6b9a fix incompatible pointer type warning
Fixes the following warning:

kpatch-patch-hook.c:71:2: warning: initialization from incompatible pointer type [enabled by default]
  __ATTR(enabled, 0644, patch_enabled_show, patch_enabled_store);
  ^

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-05-12 15:20:09 -05:00
Seth Jennings 681a6e80b9 refactor core <-> patch interface
Make kpatch_funs truly internal by:
Defining it in core.c
Adding a struct kpatch_internal, declared in kpatch.h and defined in
 core.c, that contains per patch module internal data.
Adding an "internal" field to struct kpatch_modules.
Allocating internal and funcs data in core.c, not in the patch module,
 since the patch module has no knowledge of kpatch_func anymore.
Adding a "patch" field to kpatch_func that points directly to the
 kpatch_patch provided by the module (rather than a field-by-field copy)

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-05-12 14:58:29 -05:00
Josh Poimboeuf 65810a47d0 kpatch replace
Allow the user to atomically replace all existing modules with a new
"kpatch replace" command.  This provides a safe way to do atomic
upgrades for cumulative patch module updates.
2014-05-02 15:35:00 -05:00
Josh Poimboeuf b7a2862f90 safe kpatch unload
Currently the patch module calls kpatch_unregister in the patch module
exit path.  If the activeness safety check fails in kpatch_unregister,
it's too late for the patch module to stop exiting, so all it can do is
panic.

Prevent this scenario by requiring the user to disable the patch module
via sysfs before allowing the module to be unloaded.  The sysfs write
will fail if the activeness safety check fails.  An rmmod will fail if
the patch is still enabled.

Also add support for this new unloading model in "kpatch unload".
2014-04-25 23:05:26 -05:00
Josh Poimboeuf f3f39c0587 fix activeness safety check when unpatching
When unpatching, the activeness safety logic should check for the new
function on the stack, not the old one.

Fixes #64.
2014-04-23 14:37:35 -05:00
Josh Poimboeuf 2984b53d21 kmod: add new kpatch_module struct
Put funcs, num_funcs, and mod in their own struct called kpatch_module.
This allows us to keep patch module specific variables in one place (and
we'll have more of these variables soon).
2014-04-23 14:36:15 -05:00
Josh Poimboeuf ff28767295 kmod: error handling cleanup
Cleanup the error handling a little bit and make the flow a little
clearer.
2014-04-23 14:36:15 -05:00
Josh Poimboeuf ff44a0af26 core/kmod: use kmalloc instead of kzalloc
There's no need to zero out the kpatch funcs array.  The addr fields are
initialized by the patch module, the mod field is intialized by the core
module, and the node struct doesn't need to be initialized because its
fields are overwritten by hash_add.
2014-03-18 16:57:51 -05:00
Josh Poimboeuf bfad3b1880 kmod/core: update API and use hash table
My apologies for the size of this commit.  I combined these two features
(updating API and using a hash table) into a single commit because their
implementations are tightly coupled and I didn't want to have to add
support for the old kpatch_funcs array with the new API just for the
sake of splitting up the commit :-)

- Update the core module API to get a more clear separation between core
  module and patch module.  This is cleaner and will help our case for
  getting the core module merged upstream into the kernel.
- Convert the old kpatch_funcs array into a hash table.  This is so much
  nicer performance-wise and everything-else-wise than that ugly old
  array.
- Do the incremental patching in stop machine.  This ensures that the
  funcs hash is up to date and we don't miss anything.
- Disable preemption in the ftrace handler when accessing the func hash.
  That way we don't get conflicts with the stop_machine handler updating
  the hash.
2014-03-18 13:34:15 -05:00
Josh Poimboeuf b81e2d607b kmod/core: print module load/unload messages
Print the loading/unloading messages after they have successfully
completed.  Using the KERN_NOTICE log level which corresponds to a
"normal but significant condition."
2014-03-14 20:23:12 -05:00
Josh Poimboeuf 330a08dd0d add GPLv2 headers to source files 2014-03-04 21:34:19 -06:00
Josh Poimboeuf 4f27b9ae31 functional reorganization
Organize the files functionally:
- kmod/core: core kmod source
- kmod/patch: patch kmod source
- kpatch: kpatch script
- kpatch-build: kpatch build script and supporting tools
- contrib: distro-related files
2014-02-13 11:00:06 -06:00