bug: Provide toggle for BUG on data corruption
Subsystems (3)
v3 cover bug: Provide toggle for BUG on data corruption
This adds CONFIG_BUG_ON_DATA_CORRUPTION to trigger BUG()s when the kernel encounters unexpected data structure integrity as currently detected with CONFIG_DEBUG_LIST.
Specifically list operations have been a target for widening flaws to gain "write anywhere" primitives for attackers, so this also consolidates the debug checking to avoid code and check duplication (e.g. RCU list debug was missing a check that got added to regular list debug). It also stops manipulations when corruption is detected, since worsening the corruption makes no sense. (Really, everyone should build with CONFIG_DEBUG_LIST since the checks are so inexpensive.)
This is mostly a refactoring of similar code from PaX and Grsecurity, along with MSM kernel changes by Syed Rameez Mustafa.
Along with the patches is a new lkdtm test to validate that setting CONFIG_DEBUG_LIST actually does what is desired.
Thanks,
-Kees
v3:
- fix MSM attribution, sboyd
- use pr_err, joe
v2:
- consolidate printk/WARN/BUG/return logic into a CONFIG-specific macro
- drop non-list BUGs, labbott
PATCH v3 1/5 list: Split list_add() debug checking into separate function
Right now, __list_add() code is repeated either in list.h or in list_debug.c, but only the debug checks are the different part. This extracts the checking into a separate function and consolidates __list_add(). Additionally this __list_add_debug() will stop list manipulations if a corruption is detected, instead of allowing for further corruption that may lead to even worse conditions.
This is slight refactoring of the same hardening done in PaX and Grsecurity.
--- include/linux/list.h | 22 ++++++++++++++++------ lib/list_debug.c | 48 +++++++++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 31 deletions(-)
include/linux/list.h +16 -6
@@ -28,27 +28,37 @@ static inline void INIT_LIST_HEAD(struct list_head *list) list->prev = list; } +#ifdef CONFIG_DEBUG_LIST +extern bool __list_add_valid (struct list_head *new, +struct list_head *prev, +struct list_head *next); +#else +static inline bool __list_add_valid (struct list_head *new, +struct list_head *prev, +struct list_head *next) +{ +return true; +} +#endif +/* * Insert a new entry between two known consecutive entries. * * This is only for internal list manipulation where we know * the prev/next entries already! */ -#ifndef CONFIG_DEBUG_LIST static inline void __list_add (struct list_head *new,struct list_head *prev,struct list_head *next) { +if (!__list_add_valid (new, prev, next)) +return ; + next->prev = new; new->next = next; new->prev = prev;WRITE_ONCE (prev->next, new); } -#else -extern void __list_add (struct list_head *new, -struct list_head *prev, -struct list_head *next); -#endif /** * list_add - add a new entry
lib/list_debug.c +23 -25
64 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 2/5 rculist: Consolidate DEBUG_LIST for list_add_rcu()
In reply to Kees CookConsolidates the debug checking for list_add_rcu() into the new single debug function. Notably, this fixes the sanity check that was added in
commit 17a801f4bfeb ("list_debug: WARN for adding something already in
the list"). Before, it wasn't being checked for RCU lists.
--- include/linux/rculist.h | 8 +++----- lib/list_debug.c | 19 ------------------- 2 files changed, 3 insertions(+), 24 deletions(-)
include/linux/rculist.h +3 -5
@@ -45,19 +45,17 @@ static inline void INIT_LIST_HEAD_RCU(struct list_head *list)* This is only for internal list manipulation where we know * the prev/next entries already! */ -#ifndef CONFIG_DEBUG_LIST static inline void __list_add_rcu (struct list_head *new,struct list_head *prev,struct list_head *next) { +if (!__list_add_valid (new, prev, next)) +return ; + new->next = next; new->prev = prev;rcu_assign_pointer (list_next_rcu (prev), new); next->prev = new; } -#else -void __list_add_rcu (struct list_head *new, -struct list_head *prev,struct list_head *next); -#endif /** * list_add_rcu - add a new entry to rcu-protected list
lib/list_debug.c +0 -19
@@ -77,22 +77,3 @@ void list_del(struct list_head *entry) entry->prev =LIST_POISON2 ; }EXPORT_SYMBOL (list_del); - -/* -* RCU variants. -*/ -void __list_add_rcu (struct list_head *new, -struct list_head *prev,struct list_head *next) -{ -WARN (next->prev != prev, -"list_add_rcu corruption. next->prev should be prev (%p), but was %p. (next=%p).\n" , - prev, next->prev, next); -WARN (prev->next != next, -"list_add_rcu corruption. prev->next should be next (%p), but was %p. (prev=%p).\n" , - next, prev->next, prev); - new->next = next; - new->prev = prev; -rcu_assign_pointer (list_next_rcu (prev), new); - next->prev = new; -} -EXPORT_SYMBOL (__list_add_rcu);
Signature · 1 line
2.7.4
PATCH v3 3/5 list: Split list_del() debug checking into separate function
In reply to Kees CookSimilar to the list_add() debug consolidation, this consolidates the debug checking performed during CONFIG_DEBUG_LIST, and stops list updates when corruption is found.
Refactored from same hardening in PaX and Grsecurity.
--- include/linux/list.h | 15 +++++++++------ lib/list_debug.c | 53 +++++++++++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 36 deletions(-)
include/linux/list.h +9 -6
@@ -32,6 +32,7 @@ static inline void INIT_LIST_HEAD(struct list_head *list)extern bool __list_add_valid (struct list_head *new,struct list_head *prev,struct list_head *next); +extern bool __list_del_entry_valid (struct list_head *entry);#else static inline bool __list_add_valid(struct list_head *new, struct list_head *prev,
@@ -39,6 +40,10 @@ static inline bool __list_add_valid(struct list_head *new, {return true; } +static inline bool __list_del_entry_valid (struct list_head *entry) +{ +return true; +}#endif /*
@@ -106,22 +111,20 @@ static inline void __list_del(struct list_head * prev, struct list_head * next)* Note: list_empty() on entry does not return true after this, the entry is * in an undefined state. */ -#ifndef CONFIG_DEBUG_LIST static inline void __list_del_entry (struct list_head *entry) { +if (!__list_del_entry_valid (entry)) +return ; +__list_del (entry->prev, entry->next); }static inline void list_del (struct list_head *entry) { -__list_del (entry->prev, entry->next); +__list_del_entry (entry); entry->next =LIST_POISON1 ; entry->prev =LIST_POISON2 ; } -#else -extern void __list_del_entry (struct list_head *entry); -extern void list_del (struct list_head *entry); -#endif /** * list_replace - replace old entry by new one
lib/list_debug.c +23 -30
64 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 4/5 bug: Provide toggle for BUG on data corruption
In reply to Kees CookThe kernel checks for cases of data structure corruption under some CONFIGs (e.g. CONFIG_DEBUG_LIST). When corruption is detected, some systems may want to BUG() immediately instead of letting the system run with known corruption. Usually these kinds of manipulation primitives can be used by security flaws to gain arbitrary memory write control. This provides a new config CONFIG_BUG_ON_DATA_CORRUPTION and a corresponding macro CHECK_DATA_CORRUPTION for handling these situations. Notably, even if not BUGing, the kernel should not continue processing the corrupted structure.
This is inspired by similar hardening by Syed Rameez Mustafa in MSM kernels, and in PaX and Grsecurity, which is likely in response to earlier removal of the BUG calls in commit 924d9addb9b1 ("list debugging: use WARN() instead of BUG()").
--- include/linux/bug.h | 17 ++++++++++++++++ lib/Kconfig.debug | 10 ++++++++++ lib/list_debug.c | 57 +++++++++++++++++++++-------------------------------- 3 files changed, 49 insertions(+), 35 deletions(-)
include/linux/bug.h +17 -0
@@ -118,4 +118,21 @@ static inline enum bug_trap_type report_bug(unsigned long bug_addr, }#endif /* CONFIG_GENERIC_BUG */ + +/* +* Since detected data corruption should stop operation on the affected +* structures, this returns false if the corruption condition is found. +*/ +#define CHECK_DATA_CORRUPTION (condition, fmt, ...) \ + do { \ + if (unlikely(condition)) { \ + if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \ + pr_err(fmt, ##__VA_ARGS__); \ + BUG(); \ + } else \ + WARN(1, fmt, ##__VA_ARGS__); \ + return false; \ + } \ + } while (0) +#endif /* _LINUX_BUG_H */
lib/Kconfig.debug +10 -0
@@ -1987,6 +1987,16 @@ config TEST_STATIC_KEYS If unsure, say N. +config BUG_ON_DATA_CORRUPTION + bool "Trigger a BUG when data corruption is detected" + select CONFIG_DEBUG_LIST + help + Select this option if the kernel should BUG when it encounters + data corruption in kernel memory structures when they get checked + for validity. + + If unsure, say N. + source "samples/Kconfig" source "lib/Kconfig.kgdb"
lib/list_debug.c +22 -35
69 lines not on this page · raw message
Signature · 1 line
2.7.4
Arnd Bergmann
maintainerQuoted 7 lines · CEST Kees Cook
include/linux/bug.h +7
-0+ +/* +* Since detected data corruption should stop operation on the affected +* structures, this returns false if the corruption condition is found. +*/ +#define CHECK_DATA_CORRUPTION (condition, fmt, ...) \ + do { \
include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 137
Quoted 10 lines
include/linux/bug.h +10
-0+if (unlikely (condition)) { \ +if (IS_ENABLED (CONFIG_BUG_ON_DATA_CORRUPTION )) { \ + pr_err(fmt, ##__VA_ARGS__); \ +BUG (); \ + }else \ + WARN(1, fmt, ##__VA_ARGS__); \ +return false; \ + } \ + }while (0 ) +
I think the "return false" inside of the macro makes it easy to misread what is actually going on.
How about making it a macro that returns the condition argument?
#define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
({ \
bool _condition = unlikely(condition); \
if (_condition) { \
...
} \
_condition; \
})Arnd
Quoted 24 lines · Arnd Bergmann
Quoted 7 lines · CEST Kees Cook
include/linux/bug.h +7
-0+ +/* +* Since detected data corruption should stop operation on the affected +* structures, this returns false if the corruption condition is found. +*/ +#define CHECK_DATA_CORRUPTION (condition, fmt, ...) \ + do { \include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 137
Quoted 10 lines
include/linux/bug.h +10
-0+if (unlikely (condition)) { \ +if (IS_ENABLED (CONFIG_BUG_ON_DATA_CORRUPTION )) { \ + pr_err(fmt, ##__VA_ARGS__); \ +BUG (); \ + }else \ + WARN(1, fmt, ##__VA_ARGS__); \ +return false; \ + } \ + }while (0 ) +I think the "return false" inside of the macro makes it easy to misread what is actually going on.
How about making it a macro that returns the condition argument?
Quoted 8 lines
#define
CHECK_DATA_CORRUPTION(condition, fmt, ...)\({ \ bool _condition = unlikely(condition); \ if (_condition) { \ ... } \ _condition; \ })
That does look better, now that you mention it. Kees, any objections?
Thanx, Paul
Quoted 34 lines · Paul E. McKenney
Quoted 24 lines · Arnd Bergmann
Quoted 7 lines · CEST Kees Cook
include/linux/bug.h +7
-0+ +/* +* Since detected data corruption should stop operation on the affected +* structures, this returns false if the corruption condition is found. +*/ +#define CHECK_DATA_CORRUPTION (condition, fmt, ...) \ + do { \include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 137
Quoted 10 lines
include/linux/bug.h +10
-0+if (unlikely (condition)) { \ +if (IS_ENABLED (CONFIG_BUG_ON_DATA_CORRUPTION )) { \ + pr_err(fmt, ##__VA_ARGS__); \ +BUG (); \ + }else \ + WARN(1, fmt, ##__VA_ARGS__); \ +return false; \ + } \ + }while (0 ) +I think the "return false" inside of the macro makes it easy to misread what is actually going on.
How about making it a macro that returns the condition argument?
Quoted 8 lines
#define
CHECK_DATA_CORRUPTION(condition, fmt, ...)\({ \ bool _condition = unlikely(condition); \ if (_condition) { \ ... } \ _condition; \ })
Quoted 1 line
That does look better, now that you mention it. Kees, any objections?
That's fine with me; it'll require changing the callers of the macros to test their results, but that should be clean change.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
PATCH v3 5/5 lkdtm: Add tests for struct list corruption
In reply to Kees CookWhen building under CONFIG_DEBUG_LIST, list addition and removal will be sanity-checked. This validates that the check is working as expected by setting up classic corruption attacks against list manipulations, available with the new lkdtm tests CORRUPT_LIST_ADD and CORRUPT_LIST_DEL.
--- drivers/misc/lkdtm.h | 2 ++ drivers/misc/lkdtm_bugs.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/misc/lkdtm_core.c | 2 ++ 3 files changed, 72 insertions(+)
drivers/misc/lkdtm.h +2 -0
@@ -21,6 +21,8 @@ void lkdtm_SPINLOCKUP(void);void lkdtm_HUNG_TASK (void );void lkdtm_ATOMIC_UNDERFLOW (void );void lkdtm_ATOMIC_OVERFLOW (void ); +void lkdtm_CORRUPT_LIST_ADD (void ); +void lkdtm_CORRUPT_LIST_DEL (void );/* lkdtm_heap.c */ void lkdtm_OVERWRITE_ALLOCATION (void );
drivers/misc/lkdtm_bugs.c +68 -0
79 lines not on this page · raw message
drivers/misc/lkdtm_core.c +2 -0
@@ -197,6 +197,8 @@ struct crashtype crashtypes[] = {CRASHTYPE (EXCEPTION ),CRASHTYPE (LOOP ),CRASHTYPE (OVERFLOW ), +CRASHTYPE (CORRUPT_LIST_ADD ), +CRASHTYPE (CORRUPT_LIST_DEL ),CRASHTYPE (CORRUPT_STACK ),CRASHTYPE (UNALIGNED_LOAD_STORE_WRITE ), CRASHTYPE(OVERWRITE_ALLOCATION),
Signature · 1 line
2.7.4
Quoted 12 lines · On Wed, 17 Aug 2016 14:42:07 -0700 Kees Cook <[email protected]> wrote:
This adds
CONFIG_BUG_ON_DATA_CORRUPTIONto trigger BUG()s when the kernel encounters unexpected data structure integrity as currently detected withCONFIG_DEBUG_LIST.Specifically list operations have been a target for widening flaws to gain "write anywhere" primitives for attackers, so this also consolidates the debug checking to avoid code and check duplication (e.g. RCU list debug was missing a check that got added to regular list debug). It also stops manipulations when corruption is detected, since worsening the corruption makes no sense. (Really, everyone should build with
CONFIG_DEBUG_LISTsince the checks are so inexpensive.)
Quoted 6 lines
This is mostly a refactoring of similar code from PaX and Grsecurity, along with MSM kernel changes by Syed Rameez Mustafa.
Along with the patches is a new lkdtm test to validate that setting
CONFIG_DEBUG_LISTactually does what is desired.
The series looks fine by me.
-- Steve
Quoted 11 lines
Thanks,
-Kees
v3:
- fix MSM attribution, sboyd
- usepr_err, joev2:
- consolidate printk/WARN/BUG/return logic into a CONFIG-specific macro
- drop non-list BUGs, labbott
Quoted 18 lines · Steven Rostedt
Quoted 15 lines · On Wed, 17 Aug 2016 14:42:07 -0700 Kees Cook <[email protected]> wrote:
This adds
CONFIG_BUG_ON_DATA_CORRUPTIONto trigger BUG()s when the kernel encounters unexpected data structure integrity as currently detected withCONFIG_DEBUG_LIST.Specifically list operations have been a target for widening flaws to gain "write anywhere" primitives for attackers, so this also consolidates the debug checking to avoid code and check duplication (e.g. RCU list debug was missing a check that got added to regular list debug). It also stops manipulations when corruption is detected, since worsening the corruption makes no sense. (Really, everyone should build with
CONFIG_DEBUG_LISTsince the checks are so inexpensive.)This is mostly a refactoring of similar code from PaX and Grsecurity, along with MSM kernel changes by Syed Rameez Mustafa.
Quoted 7 lines
Quoted 3 lines
Along with the patches is a new lkdtm test to validate that setting
CONFIG_DEBUG_LISTactually does what is desired.The series looks fine by me.
Acked-by Steven Rostedt <[email protected]>
Queued for further review and testing with Steven's Acked-by, thank you, Kees!
Thanx, Paul
Quoted 14 lines
-- Steve
Quoted 11 lines
Thanks,
-Kees
v3:
- fix MSM attribution, sboyd
- usepr_err, joev2:
- consolidate printk/WARN/BUG/return logic into a CONFIG-specific macro
- drop non-list BUGs, labbott
Quoted 18 lines · Kees Cook
This adds
CONFIG_BUG_ON_DATA_CORRUPTIONto trigger BUG()s when the kernel
encounters unexpected data structure integrity as currently detected withCONFIG_DEBUG_LIST.Specifically list operations have been a target for widening flaws to gain
"write anywhere" primitives for attackers, so this also consolidates the
debug checking to avoid code and check duplication (e.g. RCU list debug
was missing a check that got added to regular list debug). It also stops
manipulations when corruption is detected, since worsening the corruption
makes no sense. (Really, everyone should build withCONFIG_DEBUG_LISTsince the checks are so inexpensive.)
Quoted 6 lines
This is mostly a refactoring of similar code from PaX and Grsecurity, along with MSM kernel changes by Syed Rameez Mustafa.
Along with the patches is a new lkdtm test to validate that setting
CONFIG_DEBUG_LISTactually does what is desired.
Series looks good to me, too.
Quoted 19 lines · Rik van Riel
Quoted 18 lines · Kees Cook
This adds
CONFIG_BUG_ON_DATA_CORRUPTIONto trigger BUG()s when the kernel
encounters unexpected data structure integrity as currently detected withCONFIG_DEBUG_LIST.Specifically list operations have been a target for widening flaws to gain
"write anywhere" primitives for attackers, so this also consolidates the
debug checking to avoid code and check duplication (e.g. RCU list debug
was missing a check that got added to regular list debug). It also stops
manipulations when corruption is detected, since worsening the corruption
makes no sense. (Really, everyone should build withCONFIG_DEBUG_LISTsince the checks are so inexpensive.)
Quoted 7 lines
Quoted 5 lines
This is mostly a refactoring of similar code from PaX and Grsecurity, along with MSM kernel changes by Syed Rameez Mustafa.
Along with the patches is a new lkdtm test to validate that setting
CONFIG_DEBUG_LISTactually does what is desired.Series looks good to me, too.
Reviewed-by? Acked-by? Ephemeral accolades? ;-)
Thanx, Paul
Rik van Riel
Acked-byQuoted 30 lines · Paul E. McKenney
Quoted 29 lines · Rik van Riel
Quoted 28 lines · Kees Cook
This adds
CONFIG_BUG_ON_DATA_CORRUPTIONto trigger BUG()s when the
kernel
encounters unexpected data structure integrity as currently detected
withCONFIG_DEBUG_LIST.Specifically list operations have been a target for widening flaws to
gain
"write anywhere" primitives for attackers, so this also consolidates
the
debug checking to avoid code and check duplication (e.g. RCU list debug
was missing a check that got added to regular list debug). It also
stops
manipulations when corruption is detected, since worsening the corruption
makes no sense. (Really, everyone should build withCONFIG_DEBUG_LIST
since the checks are so inexpensive.)This is mostly a refactoring of similar code from PaX and Grsecurity,
along with MSM kernel changes by Syed Rameez Mustafa.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Along with the patches is a new lkdtm test to validate that setting
CONFIG_DEBUG_LISTactually does what is desired.Series looks good to me, too.
Reviewed-by? Acked-by? Ephemeral accolades? ;-)
works, but I saw you already committed the series to your tree, and was not sure you would add more reviews :)
Signature · 1 line
All Rights Reversed.
Quoted 36 lines · Rik van Riel
Quoted 35 lines · Paul E. McKenney
Quoted 34 lines · Rik van Riel
Quoted 23 lines · Kees Cook
This adds
CONFIG_BUG_ON_DATA_CORRUPTIONto trigger BUG()s when the
kernel
encounters unexpected data structure integrity as currently detected
withCONFIG_DEBUG_LIST.
=20
Specifically list operations have been a target for widening flaws to
gain
"write anywhere" primitives for attackers, so this also consolidates
the
debug checking to avoid code and check duplication (e.g. RCU list debug
was missing a check that got added to regular list debug). It also
stops
manipulations when corruption is detected, since worsening the corruption
makes no sense. (Really, everyone should build withCONFIG_DEBUG_LIST
since the checks are so inexpensive.)Quoted 8 lines
=20
This is mostly a refactoring of similar code from PaX and Grsecurity,
along with MSM kernel changes by Syed Rameez Mustafa. =20
Along with the patches is a new lkdtm test to validate that settingCONFIG_DEBUG_LISTactually does what is desired.=20
Series looks good to me, too.
Quoted 8 lines
Quoted 2 lines
=20
Reviewed-by?=C2=A0=C2=A0Acked-by?=C2=A0=C2=A0Ephemeral accolades?=C2=A0==C2=A0;-)
Acked-by Rik van Riel <[email protected]>works, but I saw you already committed the series to your tree, and was not sure you would add more reviews :)
Well, there is at least one more rebase for topic branches. ;-)
Thanx, Paul