History

kernel: add panic_on_taint

Analogously to the introduction of panic_on_warn, this patch introduces a kernel option named panic_on_taint in order to provide a simple and generic way to stop execution and catch a coredump when the kernel gets tainted by any given flag. This is useful for debugging sessions as it avoids having to rebuild the kernel to explicitly add calls to panic() into the code sites that introduce the taint flags of interest. For instance, if one is interested in proceeding with a post-mortem analysis at the point a given code path is hitting a bad page (i.e. unaccount_page_cache_page(), or slab_bug()), a coredump can be collected by rebooting the kernel with 'panic_on_taint=0x20' amended to the command line. Another, perhaps less frequent, use for this option would be as a means for assuring a security policy case where only a subset of taints, or no single taint (in paranoid mode), is allowed for the running system. The optional switch 'nousertaint' is handy in this particular scenario, as it will avoid userspace induced crashes by writes to sysctl interface /proc/sys/kernel/tainted causing false positive hits for such policies. [akpm@linux-foundation.org: tweak kernel-parameters.txt wording]

Rafael Aquini committed 6 years ago

Commit db38d5c1 parent ceabef76 files · +75 −1Browse files

Showing the changes to kernel (2 of 6 files). Show all

Unified · Split

2 files
  1. kernel/panic.c +34 −0
  2. kernel/sysctl.c +10 −1
kernel/panic.c+34−0
@@ -44,6 +44,8 @@ static int pause_on_oops_flag; · 43 unmodified lines
4444 static DEFINE_SPINLOCK(pause_on_oops_lock);
4545 bool crash_kexec_post_notifiers;
4646 int panic_on_warn __read_mostly;
47+unsigned long panic_on_taint;
48+bool panic_on_taint_nousertaint = false;
4749
4850 int panic_timeout = CONFIG_PANIC_TIMEOUT;
4951 EXPORT_SYMBOL_GPL(panic_timeout);
@@ -434,6 +436,11 @@ void add_taint(unsigned flag, enum lockdep_ok lockdep_ok) · 384 unmodified lines
434436 pr_warn("Disabling lock debugging due to kernel taint\n");
435437
436438 set_bit(flag, &tainted_mask);
439+
440+ if (tainted_mask & panic_on_taint) {
441+ panic_on_taint = 0;
442+ panic("panic_on_taint set ...");
443+ }
437444 }
438445 EXPORT_SYMBOL(add_taint);
439446
@@ -686,3 +693,30 @@ static int __init oops_setup(char *s) · 246 unmodified lines
686693 return 0;
687694 }
688695 early_param("oops", oops_setup);
696+
697+static int __init panic_on_taint_setup(char *s)
698+{
699+ char *taint_str;
700+
701+ if (!s)
702+ return -EINVAL;
703+
704+ taint_str = strsep(&s, ",");
705+ if (kstrtoul(taint_str, 16, &panic_on_taint))
706+ return -EINVAL;
707+
708+ /* make sure panic_on_taint doesn't hold out-of-range TAINT flags */
709+ panic_on_taint &= TAINT_FLAGS_MAX;
710+
711+ if (!panic_on_taint)
712+ return -EINVAL;
713+
714+ if (s && !strcmp(s, "nousertaint"))
715+ panic_on_taint_nousertaint = true;
716+
717+ pr_info("panic_on_taint: bitmask=0x%lx nousertaint_mode=%sabled\n",
718+ panic_on_taint, panic_on_taint_nousertaint ? "en" : "dis");
719+
720+ return 0;
721+}
722+early_param("panic_on_taint", panic_on_taint_setup);
kernel/sysctl.c+10−1
@@ -866,11 +866,20 @@ static int proc_taint(struct ctl_table *table, int write, · 865 unmodified lines
866866 return err;
867867
868868 if (write) {
869+ int i;
870+
871+ /*
872+ * If we are relying on panic_on_taint not producing
873+ * false positives due to userspace input, bail out
874+ * before setting the requested taint flags.
875+ */
876+ if (panic_on_taint_nousertaint && (tmptaint & panic_on_taint))
877+ return -EINVAL;
878+
869879 /*
870880 * Poor man's atomic or. Not worth adding a primitive
871881 * to everyone's atomic.h for this
872882 */
873− int i;
874883 for (i = 0; i < BITS_PER_LONG && tmptaint >> i; i++) {
875884 if ((tmptaint >> i) & 1)
876885 add_taint(i, LOCKDEP_STILL_OK);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t