1. 14 Sep, 2017 1 commit
    • Joe Lawrence's avatar
      livepatch: introduce shadow variable API · 439e7271
      Joe Lawrence authored
      
      
      Add exported API for livepatch modules:
      
        klp_shadow_get()
        klp_shadow_alloc()
        klp_shadow_get_or_alloc()
        klp_shadow_free()
        klp_shadow_free_all()
      
      that implement "shadow" variables, which allow callers to associate new
      shadow fields to existing data structures.  This is intended to be used
      by livepatch modules seeking to emulate additions to data structure
      definitions.
      
      See Documentation/livepatch/shadow-vars.txt for a summary of the new
      shadow variable API, including a few common use cases.
      
      See samples/livepatch/livepatch-shadow-* for example modules that
      demonstrate shadow variables.
      
      [jkosina@suse.cz: fix __klp_shadow_get_or_alloc() comment as spotted by
       Josh]
      Signed-off-by: default avatarJoe Lawrence <joe.lawrence@redhat.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      439e7271
  2. 08 Mar, 2017 5 commits
    • Josh Poimboeuf's avatar
      livepatch: allow removal of a disabled patch · 3ec24776
      Josh Poimboeuf authored
      Currently we do not allow patch module to unload since there is no
      method to determine if a task is still running in the patched code.
      
      The consistency model gives us the way because when the unpatching
      finishes we know that all tasks were marked as safe to call an original
      function. Thus every new call to the function calls the original code
      and at the same time no task can be somewhere in the patched code,
      because it had to leave that code to be marked as safe.
      
      We can safely let the patch module go after that.
      
      Completion is used for synchronization between module removal and sysfs
      infrastructure in a similar way to commit 942e4431 ("module: Fix
      mod->mkobj.kobj potentially freed too early").
      
      Note that we still do not allow the removal for immediate model, that is
      no consistency model. The module refcount may increase in this case if
      somebody disables and enables the patch several times. This should not
      cause any harm.
      
      With this change a call to try_module_get() is moved to
      __klp_enable_patch from klp_register_patch to make module reference
      counting symmetric (module_put() is in a patch disable path) and to
      allow to take a new reference to a disabled module when being enabled.
      
      Finally, we need to be very careful about possible races between
      klp_unregister_patch(), kobject_put() functions and operations
      on the related sysfs files.
      
      kobject_put(&patch->kobj) must be called without klp_mutex. Otherwise,
      it might be blocked by enabled_store() that needs the mutex as well.
      In addition, enabled_store() must check if the patch was not
      unregisted in the meantime.
      
      There is no need to do the same for other kobject_put() callsites
      at the moment. Their sysfs operations neither take the lock nor
      they access any data that might be freed in the meantime.
      
      There was an attempt to use kobjects the right way and prevent these
      races by design. But it made the patch definition more complicated
      and opened another can of worms. See
      https://lkml.kernel.org/r/1464018848-4303-1-git-send-email-pmladek@suse.com
      
      
      
      [Thanks to Petr Mladek for improving the commit message.]
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      3ec24776
    • Josh Poimboeuf's avatar
      livepatch: change to a per-task consistency model · d83a7cb3
      Josh Poimboeuf authored
      Change livepatch to use a basic per-task consistency model.  This is the
      foundation which will eventually enable us to patch those ~10% of
      security patches which change function or data semantics.  This is the
      biggest remaining piece needed to make livepatch more generally useful.
      
      This code stems from the design proposal made by Vojtech [1] in November
      2014.  It's a hybrid of kGraft and kpatch: it uses kGraft's per-task
      consistency and syscall barrier switching combined with kpatch's stack
      trace switching.  There are also a number of fallback options which make
      it quite flexible.
      
      Patches are applied on a per-task basis, when the task is deemed safe to
      switch over.  When a patch is enabled, livepatch enters into a
      transition state where tasks are converging to the patched state.
      Usually this transition state can complete in a few seconds.  The same
      sequence occurs when a patch is disabled, except the tasks converge from
      the patched state to the unpatched state.
      
      An interrupt handler inherits the patched state of the task it
      interrupts.  The same is true for forked tasks: the child inherits the
      patched state of the parent.
      
      Livepatch uses several complementary approaches to determine when it's
      safe to patch tasks:
      
      1. The first and most effective approach is stack checking of sleeping
         tasks.  If no affected functions are on the stack of a given task,
         the task is patched.  In most cases this will patch most or all of
         the tasks on the first try.  Otherwise it'll keep trying
         periodically.  This option is only available if the architecture has
         reliable stacks (HAVE_RELIABLE_STACKTRACE).
      
      2. The second approach, if needed, is kernel exit switching.  A
         task is switched when it returns to user space from a system call, a
         user space IRQ, or a signal.  It's useful in the following cases:
      
         a) Patching I/O-bound user tasks which are sleeping on an affected
            function.  In this case you have to send SIGSTOP and SIGCONT to
            force it to exit the kernel and be patched.
         b) Patching CPU-bound user tasks.  If the task is highly CPU-bound
            then it will get patched the next time it gets interrupted by an
            IRQ.
         c) In the future it could be useful for applying patches for
            architectures which don't yet have HAVE_RELIABLE_STACKTRACE.  In
            this case you would have to signal most of the tasks on the
            system.  However this isn't supported yet because there's
            currently no way to patch kthreads without
            HAVE_RELIABLE_STACKTRACE.
      
      3. For idle "swapper" tasks, since they don't ever exit the kernel, they
         instead have a klp_update_patch_state() call in the idle loop which
         allows them to be patched before the CPU enters the idle state.
      
         (Note there's not yet such an approach for kthreads.)
      
      All the above approaches may be skipped by setting the 'immediate' flag
      in the 'klp_patch' struct, which will disable per-task consistency and
      patch all tasks immediately.  This can be useful if the patch doesn't
      change any function or data semantics.  Note that, even with this flag
      set, it's possible that some tasks may still be running with an old
      version of the function, until that function returns.
      
      There's also an 'immediate' flag in the 'klp_func' struct which allows
      you to specify that certain functions in the patch can be applied
      without per-task consistency.  This might be useful if you want to patch
      a common function like schedule(), and the function change doesn't need
      consistency but the rest of the patch does.
      
      For architectures which don't have HAVE_RELIABLE_STACKTRACE, the user
      must set patch->immediate which causes all tasks to be patched
      immediately.  This option should be used with care, only when the patch
      doesn't change any function or data semantics.
      
      In the future, architectures which don't have HAVE_RELIABLE_STACKTRACE
      may be allowed to use per-task consistency if we can come up with
      another way to patch kthreads.
      
      The /sys/kernel/livepatch/<patch>/transition file shows whether a patch
      is in transition.  Only a single patch (the topmost patch on the stack)
      can be in transition at a given time.  A patch can remain in transition
      indefinitely, if any of the tasks are stuck in the initial patch state.
      
      A transition can be reversed and effectively canceled by writing the
      opposite value to the /sys/kernel/livepatch/<patch>/enabled file while
      the transition is in progress.  Then all the tasks will attempt to
      converge back to the original patch state.
      
      [1] https://lkml.kernel.org/r/20141107140458.GA21774@suse.cz
      
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: Ingo Molnar <mingo@kernel.org>        # for the scheduler changes
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      d83a7cb3
    • Josh Poimboeuf's avatar
      livepatch: store function sizes · f5e547f4
      Josh Poimboeuf authored
      
      
      For the consistency model we'll need to know the sizes of the old and
      new functions to determine if they're on the stacks of any tasks.
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Reviewed-by: default avatarKamalesh Babulal <kamalesh@linux.vnet.ibm.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      f5e547f4
    • Josh Poimboeuf's avatar
      livepatch: separate enabled and patched states · 0dade9f3
      Josh Poimboeuf authored
      
      
      Once we have a consistency model, patches and their objects will be
      enabled and disabled at different times.  For example, when a patch is
      disabled, its loaded objects' funcs can remain registered with ftrace
      indefinitely until the unpatching operation is complete and they're no
      longer in use.
      
      It's less confusing if we give them different names: patches can be
      enabled or disabled; objects (and their funcs) can be patched or
      unpatched:
      
      - Enabled means that a patch is logically enabled (but not necessarily
        fully applied).
      
      - Patched means that an object's funcs are registered with ftrace and
        added to the klp_ops func stack.
      
      Also, since these states are binary, represent them with booleans
      instead of ints.
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Reviewed-by: default avatarKamalesh Babulal <kamalesh@linux.vnet.ibm.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      0dade9f3
    • Josh Poimboeuf's avatar
      livepatch: create temporary klp_update_patch_state() stub · 46c5a011
      Josh Poimboeuf authored
      
      
      Create temporary stubs for klp_update_patch_state() so we can add
      TIF_PATCH_PENDING to different architectures in separate patches without
      breaking build bisectability.
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      46c5a011
  3. 18 Aug, 2016 1 commit
  4. 29 Apr, 2016 1 commit
    • Miroslav Benes's avatar
      livepatch: make object/func-walking helpers more robust · f09d9086
      Miroslav Benes authored
      
      
      Current object-walking helper checks the presence of obj->funcs to
      determine the end of objs array in klp_object structure. This is
      somewhat fragile because one can easily forget about funcs definition
      during livepatch creation. In such a case the livepatch module is
      successfully loaded and all objects after the incorrect one are omitted.
      This is very confusing. Let's make the helper more robust and check also
      for the other external member, name. Thus the helper correctly stops on
      an empty item of the array. We need to have a check for obj->funcs in
      klp_init_object() to make it work.
      
      The same applies to a func-walking helper.
      
      As a benefit we'll check for new_func member definition during the
      livepatch initialization. There is no such check anywhere in the code
      now.
      
      [jkosina@suse.cz: fix shortlog]
      Signed-off-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: default avatarJessica Yu <jeyu@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      f09d9086
  5. 01 Apr, 2016 1 commit
    • Jessica Yu's avatar
      livepatch: reuse module loader code to write relocations · 425595a7
      Jessica Yu authored
      
      
      Reuse module loader code to write relocations, thereby eliminating the need
      for architecture specific relocation code in livepatch. Specifically, reuse
      the apply_relocate_add() function in the module loader to write relocations
      instead of duplicating functionality in livepatch's arch-dependent
      klp_write_module_reloc() function.
      
      In order to accomplish this, livepatch modules manage their own relocation
      sections (marked with the SHF_RELA_LIVEPATCH section flag) and
      livepatch-specific symbols (marked with SHN_LIVEPATCH symbol section
      index). To apply livepatch relocation sections, livepatch symbols
      referenced by relocs are resolved and then apply_relocate_add() is called
      to apply those relocations.
      
      In addition, remove x86 livepatch relocation code and the s390
      klp_write_module_reloc() function stub. They are no longer needed since
      relocation work has been offloaded to module loader.
      
      Lastly, mark the module as a livepatch module so that the module loader
      canappropriately identify and initialize it.
      Signed-off-by: default avatarJessica Yu <jeyu@redhat.com>
      Reviewed-by: default avatarMiroslav Benes <mbenes@suse.cz>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Acked-by: Heiko Carstens <heiko.carstens@de.ibm.com>   # for s390 changes
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      425595a7
  6. 17 Mar, 2016 1 commit
  7. 06 Mar, 2016 1 commit
  8. 03 Dec, 2015 2 commits
    • Chris J Arges's avatar
      livepatch: add sympos as disambiguator field to klp_reloc · 064c89df
      Chris J Arges authored
      
      
      In cases of duplicate symbols, sympos will be used to disambiguate instead
      of val. By default sympos will be 0, and patching will only succeed if
      the symbol is unique. Specifying a positive value will ensure that
      occurrence of the symbol in kallsyms for the patched object will be used
      for patching if it is valid. For external relocations sympos is not
      supported.
      
      Remove klp_verify_callback, klp_verify_args and klp_verify_vmlinux_symbol
      as they are no longer used.
      
      From the klp_reloc structure remove val, as it can be refactored as a
      local variable in klp_write_object_relocations.
      Signed-off-by: default avatarChris J Arges <chris.j.arges@canonical.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      064c89df
    • Chris J Arges's avatar
      livepatch: add old_sympos as disambiguator field to klp_func · b2b018ef
      Chris J Arges authored
      
      
      Currently, patching objects with duplicate symbol names fail because the
      creation of the sysfs function directory collides with the previous
      attempt. Appending old_addr to the function name is problematic as it
      reveals the address of the function being patch to a normal user. Using
      the symbol's occurrence in kallsyms to postfix the function name in the
      sysfs directory solves the issue of having consistent unique names and
      ensuring that the address is not exposed to a normal user.
      
      In addition, using the symbol position as the user's method to disambiguate
      symbols instead of addr allows for disambiguating symbols in modules as
      well for both function addresses and for relocs. This also simplifies much
      of the code. Special handling for kASLR is no longer needed and can be
      removed. The klp_find_verify_func_addr function can be replaced by
      klp_find_object_symbol, and klp_verify_vmlinux_symbol and its callback can
      be removed completely.
      
      In cases of duplicate symbols, old_sympos will be used to disambiguate
      instead of old_addr. By default old_sympos will be 0, and patching will
      only succeed if the symbol is unique. Specifying a positive value will
      ensure that occurrence of the symbol in kallsyms for the patched object
      will be used for patching if it is valid.
      
      In addition, make old_addr an internal structure field not to be specified
      by the user. Finally, remove klp_find_verify_func_addr as it can be
      replaced by klp_find_object_symbol directly.
      
      Support for symbol position disambiguation for relocations is added in the
      next patch in this series.
      Signed-off-by: default avatarChris J Arges <chris.j.arges@canonical.com>
      Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      b2b018ef
  9. 19 May, 2015 2 commits
  10. 18 Feb, 2015 1 commit
  11. 04 Feb, 2015 1 commit
  12. 20 Jan, 2015 1 commit
    • Josh Poimboeuf's avatar
      livepatch: support for repatching a function · 3c33f5b9
      Josh Poimboeuf authored
      
      
      Add support for patching a function multiple times.  If multiple patches
      affect a function, the function in the most recently enabled patch
      "wins".  This enables a cumulative patch upgrade path, where each patch
      is a superset of previous patches.
      
      This requires restructuring the data a little bit.  With the current
      design, where each klp_func struct has its own ftrace_ops, we'd have to
      unregister the old ops and then register the new ops, because
      FTRACE_OPS_FL_IPMODIFY prevents us from having two ops registered for
      the same function at the same time.  That would leave a regression
      window where the function isn't patched at all (not good for a patch
      upgrade path).
      
      This patch replaces the per-klp_func ftrace_ops with a global klp_ops
      list, with one ftrace_ops per original function.  A single ftrace_ops is
      shared between all klp_funcs which have the same old_addr.  This allows
      the switch between function versions to happen instantaneously by
      updating the klp_ops struct's func_stack list.  The winner is the
      klp_func at the top of the func_stack (front of the list).
      
      [ jkosina@suse.cz: turn WARN_ON() into WARN_ON_ONCE() in ftrace handler to
        avoid storm in pathological cases ]
      Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Reviewed-by: default avatarJiri Slaby <jslaby@suse.cz>
      Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
      3c33f5b9
  13. 22 Dec, 2014 1 commit