Commit Graph

49 Commits

Author SHA1 Message Date
Josh Poimboeuf 0858e8b054 add include linux/module.h to kpatch.h
Needed for the module and kobject structs.
2014-04-28 13:24:04 -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
Jincheng Miao 6c2d6444b1 Adding preempt_mask.h to core.c
When compiling core.c, it may report error like:
"error: implicit declaration of function ‘in_nmi’"

Adding header file in_nmi defined could avoid this.

Signed-off-by: Jincheng Miao <jincheng.miao@gmail.com>
2014-04-25 12:08:04 +08:00
Josh Poimboeuf fe6ace4fc7 kmod/core: error handling review fixes
Some fixes from the code review for better readability.
2014-04-24 14:22:51 -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 48cc3a409e kmod/core: move a couple of functions
Move kpatch_get_*_func a little higher in the file where they will be
needed for the next commit.
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
Masami Hiramatsu 42e0779c0c kmod/core: Support live patching on NMI handlers
Support live patching on NMI handlers. This adds checks for
possible inconsistency of live patching on NMI handlers.
The inconsistency problem means that any concurrent execution
of old function and new function, which can lead unexpected results.

Current kpatch checks possible inconsistency problem with
stop_machine, which can cover only threads and normal interrupts.
However, beacuse NMI can not stop with it, stop_machine is not
enough for live patching on NMI handlers or sub-functions which are
invoked in the NMI context.

To check for possible inconsistency of live patching on those
functions, add an atomic flag to count patching target functions
invoked in NMI context while updating kpatch hash table. If the
flag is set by the target functions in NMI, we can not ensure
there is no concurrent execution on it.

This fixes the issue #65.

Changes from v5:
 - Fix to add a NULL check in kpatch_get_committed_func().

Changes from v4:
 - Change kpatch_operation to atomic_t.
 - Use smp_rmb/wmb barriers between kpatch_operation and kpatch_status.
 - Check in_nmi() first and if true, access kpatch_operation.

Changes from v3:
 - Fix kpatch_apply/remove_patch to return 0 if succeeded.

Changes from v2:
 - Clean up kpatch_get_committed_func as same style of kpatch_get_func.
 - Rename opr to op in kpatch_ftrace_handler.
 - Consolidate in_nmi() and kpatch_operation check into one condition.
 - Fix UNPATCH/PATCH mistype in kpatch_register.

Changes from v1:
 - Rename inconsistent_flag to kpatch_status.
 - Introduce new enums and helper functions for kpatch_status.
 - Use hash_del_rcu instead of hlist_del_rcu.
 - Rename get_committed_func to kpatch_get_committed_func.
 - Use ACCESS_ONCE for kpatch_operation to prevent compiler optimization.
 - Fix to remove (!func || func->updating) condition from NMI check.
 - Add more precise comments.
 - Fix setting order of kpatch_status and kpatch_operation.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
2014-04-23 10:58:45 +09:00
Masami Hiramatsu 79ca5dbfa7 kmod/core: Handle registering error and unroll it
Handle registering error to unroll the ftrace filter.
This also introduces get_kpatch_func() and
kpatch_remove_funcs_from_filter() for holding up
redundant loops.

Changes from v2:
 - Rebased on the latest kpatch.

Changes from v1:
 - Rename get_kpatch_func to kpatch_get_func.
 - Fix function definition style issue.
 - Do not jump to a label in "if" block.
 - Rollback the ftrace user counter if we hit an error.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
2014-04-23 10:58:45 +09:00
Josh Poimboeuf 991b25b067 kmod/core: require fentry
Only compile the core module if the compiler supports -mfentry.
2014-04-15 14:40:03 -05:00
Josh Poimboeuf e6cad4f0b1 kmod/core: use pr_err instead of printk 2014-04-15 14:21:19 -05:00
Josh Poimboeuf 56645d346d kmod/core: taint kernel with TAINT_USER
For now, taint with TAINT_USER when loading a patch module so that the
user can always detect when a kpatch module has been previously loaded.
Eventually we will want a dedicated TAINT_KPATCH flag in the kernel.
2014-04-15 13:34:01 -05:00
Josh Poimboeuf 3b489e7d29 support CONFIG_MODVERSIONS=y
When CONFIG_MODVERSIONS is enabled, loading of the patch module fails
with "no symbol version for kpatch_register".  When building the patch
module, we need to point it to the core module's Module.symvers file.

This also works when CONFIG_MODVERSIONS is disabled, since
Module.symvers is created regardless.
2014-03-27 16:36:43 -07:00
Josh Poimboeuf d4ab5fab2e always rebuild core module
If the kernel updates, make won't detect that the core module needs
rebuilding.  So always rebuild it.
2014-03-26 21:28:37 -07:00
Seth Jennings 5932f0d083 add uninstall target
Make it easy to clean up an install

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-03-20 11:29:25 -05:00
Josh Poimboeuf 2d7be5f48c kmod/core: add "notrace" to ftrace handler
The ftrace handler needs the notrace annotation so that ftrace won't
trace it and get into a recursive loop.
2014-03-19 19:16:12 -05:00
Josh Poimboeuf 29227a0fbd kmod/core: improve performance for cumulative patching
When multiple patch modules patch the same function, there's no need to
patch all the intermediate functions.  Just hook them all into the
original function and use the ftrace handler to find the newest one.

Also use a mutex in the register/unregister functions to protect changes
to kpatch_num_registered, kpatch_func_hash and calls to the ftrace
functions by other register/unregister invocations.
2014-03-19 10:05:07 -05:00
Josh Poimboeuf 37a756af58 kmod/core: protect kpatch_[un]register with mutex
Use a mutex in the register/unregister functions to protect changes to
kpatch_num_registered, kpatch_func_hash and calls to the ftrace
functions by other register/unregister invocations.
2014-03-19 10:05:07 -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 c164649d4e kmod/core: rename some variables
For the sake of consistency and readability, rename some variables.
Also change func->old_addr_end to func->old_size.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf 3fb9fd48ff kmod/core: move kpatch_remove_patch
Just move kpatch_remove_patch to a more logical location next to
kpatch_apply_patch.  No functional changes.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf 5f243c9db1 kmod/core: remove obsolete kpatch_rela struct 2014-03-14 20:23:12 -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 e7dde72ed9 kmod/core: remove unused old_func_name field
Not needed, we can always add it again later if needed (for sanity
checks, maybe)
2014-03-14 20:23:12 -05:00
Josh Poimboeuf 15e1b83cfe kmod/core: handle kmalloc errors and fix leak 2014-03-14 20:23:12 -05:00
Josh Poimboeuf aea0932655 kmod/core: remove sync_core comment
No need to call sync_core() since we aren't directly modifying any code.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf aa5346d13b kmod/core: remove comments about preemption
Preemption shouldn't cause a problem with determining activeness safety.
Even if a thread is preempted, it'll be on the backtrace.

We may need to disable preemption when reading the kpatch_funcs array,
but I'm removing that comment for now because the kpatch_funcs array
will soon be replaced by a much better data structure, and we'll deal
with proper synchronization then.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf 57f45c89e6 kmod/core: remove ftrace hacks
These hacks were from a previous implementation and are no longer
needed.
2014-03-14 20:23:12 -05:00
Josh Poimboeuf 99dd4b53fb kmod/core: update module comments 2014-03-14 20:23:12 -05:00
Josh Poimboeuf 344050d8d3 kmod/core: remove assembler code
Long ago, the kpatch_trampoline required being written in assembler, but
that's no longer needed now that it integrates nicely with ftrace.

Move it to a C function and rename it kpatch_ftrace_handler.
2014-03-14 20:23:12 -05:00
Seth Jennings 2419410a52 fix KPATCH_BUILD path
use the build symlink in /lib/modules for a more portable
path to the build env for the kernel.

Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-03-05 11:31:15 -06:00
Josh Poimboeuf 330a08dd0d add GPLv2 headers to source files 2014-03-04 21:34:19 -06:00
Josh Poimboeuf c3ad933132 install uname fix 2014-02-18 09:23:17 -06:00
Josh Poimboeuf 8085d0402b build and install core module with make
Build and install the kpatch core module with make and make install,
rather than building it every time with kpatch build.

The only downside to this approach is that the user has to make and make
install kpatch every time they get a new kernel.  But this is only
temporary, until the kpatch module is delivered in an RPM.
2014-02-18 07:44:33 -06:00
Josh Poimboeuf 62de820aab proper makefile support
- setup the makefiles to support "make" and "make install", which builds
  the kpatch-build tools and installs everything in /usr/local.
- update kpatch-build to support new paths
- add "kpatch build" wrapper around kpatch-build
2014-02-13 16:02:10 -06:00
Josh Poimboeuf 72b1ee7916 use consistent naming for core and patch modules 2014-02-13 11:00:12 -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
Seth Jennings 6ab1273021 major rewrite to per-section function/data model
Signed-off-by: Seth Jennings <sjenning@redhat.com>
2014-02-11 10:25:48 -06:00
Josh Poimboeuf 32c0fa5b44 Tell ftrace to return to new function
This changes the way the trampoline code works, thanks to a suggestion
by Steve Rostedt.  Before, the trampoline was mucking with the stack
pointer and other registers, and jumping to the new function directly.
With this change, all it does is set regs->ip to the address of the new
function and return back to ftrace.  When ftrace returns, it will return
to the beginning of the new function.
2013-01-24 09:50:43 -05:00
Josh Poimboeuf 12fb573fca add address end to fix activeness safety check 2013-01-19 13:44:13 -06:00
Josh Poimboeuf 0cf58eea37 fix bugs
- fix real issue with 0's in the middle of a merged section (wrong
  alignment)
- show patch util output in case it asks a question so it doesn't
  silently fail
- fix issue with relocation of local objects (because they become global
  objects)
2013-01-19 12:36:52 -06:00
Josh Poimboeuf 16647ccf89 multiple .o's combined have gaps in elf data section 2013-01-19 01:29:35 -06:00
Josh Poimboeuf 5fcbb0bdfa some fixes
- allow changes to .rela.initcall*.  they should be instead caught by
  the relocation comparison code.
- fix issue in compare symbols when the symbol section index has changed
  but the sections themselves are the same
- in compare_symbols, when a new STT_SECTION symbol is added, ignore it.
  it will be caught instead by the section comparison code
- fix issue in kpatch-gcc script that was causing gcc command lines
  containing quotes to fail
2013-01-19 01:13:42 -06:00
Josh Poimboeuf 04433125bc incremental patching and rmmod support 2013-01-18 12:31:14 -06:00
Josh Poimboeuf fc12020ae3 fix compiler warnings, add -o flag 2013-01-18 10:42:56 -06:00
Josh Poimboeuf e9e4563a80 one click support! 2013-01-17 18:10:01 -06:00
Josh Poimboeuf 4feb144e98 It's alive...
The end-to-end patching works.  From object analysis to generation to
runtime patching.  It's still missing the scripting piece that will only
take a patch and kernel source dir as input.
2013-01-16 11:28:59 -06:00