Skip to contentSkip to conversation

bug: Switch data corruption check to __must_check

PATCH Applied Kees Cook 3 participants 6 messages Feb 6, 2017 – Feb 7, 2017 Library code

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 bug: Switch data corruption check to __must_check

The CHECK_DATA_CORRUPTION() macro was designed to have callers do something meaningful/protective on failure. However, using "return false" in the macro too strictly limits the design patterns of callers. Instead, let callers handle the logic test directly, but make sure that the result IS checked by forcing __must_check (which appears to not be able to be used directly on macro expressions).

---
 include/linux/bug.h | 12 +++++++-----
 lib/list_debug.c    | 45 ++++++++++++++++++++++++---------------------
 2 files changed, 31 insertions(+), 26 deletions(-)
include/linux/bug.h +7 -5
index baff2e8fc8a8..5828489309bb 100644
@@ -124,18 +124,20 @@ static inline enum bug_trap_type report_bug(unsigned long bug_addr,
 
 /*
  * Since detected data corruption should stop operation on the affected
- * structures, this returns false if the corruption condition is found.
+ * structures. Return value must be checked and sanely acted on by caller.
  */
+static inline __must_check bool check_data_corruption(bool v) { return v; }
 #define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
-	do {								 \
-		if (unlikely(condition)) {				 \
+	check_data_corruption(({					 \
+		bool corruption = unlikely(condition);			 \
+		if (corruption) {					 \
 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
 				pr_err(fmt, ##__VA_ARGS__);		 \
 				BUG();					 \
 			} else						 \
 				WARN(1, fmt, ##__VA_ARGS__);		 \
-			return false;					 \
 		}							 \
-	} while (0)
+		corruption;						 \
+	}))
 
 #endif	/* _LINUX_BUG_H */
lib/list_debug.c +24 -21

57 lines not on this page · raw message

Signature · 6 lines
2.7.4


-- 
Kees Cook
Pixel Security
#1 lore raw
Quoted 9 lines · Kees Cook

The CHECK_DATA_CORRUPTION() macro was designed to have callers do something meaningful/protective on failure. However, using "return false" in the macro too strictly limits the design patterns of callers. Instead, let callers handle the logic test directly, but make sure that the result IS checked by forcing __must_check (which appears to not be able to be used directly on macro expressions).

The patch looks ok, but I have no memory of suggesting this. Was this an older conversation we had that I already forgot, or did you confuse me with someone else?

Arnd

author
#2 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Kees Cook

The CHECK_DATA_CORRUPTION() macro was designed to have callers do something meaningful/protective on failure. However, using "return false" in the macro too strictly limits the design patterns of callers. Instead, let callers handle the logic test directly, but make sure that the result IS checked by forcing __must_check (which appears to not be able to be used directly on macro expressions).

Quoted 6 lines
Quoted 2 lines

The patch looks ok, but I have no memory of suggesting this. Was this an older conversation we had that I already forgot, or did you confuse me with someone else?

Old conversation that we had. :)

https://patchwork.kernel.org/patch/9286627/

It just took me *cough* 6 months ... :P

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#3 lore raw
In reply to Kees Cook
Quoted 20 lines · Kees Cook

The CHECK_DATA_CORRUPTION() macro was designed to have callers do something meaningful/protective on failure. However, using "return false" in the macro too strictly limits the design patterns of callers. Instead, let callers handle the logic test directly, but make sure that the result IS checked by forcing __must_check (which appears to not be able to be used directly on macro expressions).

---
 include/linux/bug.h | 12 +++++++-----
 lib/list_debug.c    | 45 ++++++++++++++++++++++++---------------------
 2 files changed, 31 insertions(+), 26 deletions(-)
include/linux/bug.h +0 -0
index baff2e8fc8a8..5828489309bb 100644
@@ -124,18 +124,20 @@ static inline enum bug_trap_type report_bug(unsigned long bug_addr,
 

include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 131

Quoted 10 lines
include/linux/bug.h +3 -3
 /*
  * Since detected data corruption should stop operation on the affected
- * structures, this returns false if the corruption condition is found.
+ * structures. Return value must be checked and sanely acted on by caller.
  */
+static inline __must_check bool check_data_corruption(bool v) { return v; }
 #define CHECK_DATA_CORRUPTION(condition, fmt, ...)			 \
-	do {								 \
-		if (unlikely(condition)) {				 \
+	check_data_corruption(({					 \

The definition of check_data_corruption() is in some other patch? I don't see it in current mainline. I am not seeing what it might be doing.

include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 132

Quoted 1 line
include/linux/bug.h +1 -0
+		bool corruption = unlikely(condition);			 \

So corruption = unlikely(condition)? Sounds a bit optimistic to me! ;-)

Quoted 30 lines
include/linux/bug.h +3 -2
+		if (corruption) {					 \
 			if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
 				pr_err(fmt, ##__VA_ARGS__);		 \
 				BUG();					 \
 			} else						 \
 				WARN(1, fmt, ##__VA_ARGS__);		 \
-			return false;					 \
 		}							 \
-	} while (0)
+		corruption;						 \
+	}))
 
 #endif	/* _LINUX_BUG_H */
lib/list_debug.c +0 -9
index 7f7bfa55eb6d..a34db8d27667 100644
@@ -20,15 +20,16 @@
 bool __list_add_valid(struct list_head *new, struct list_head *prev,
 		      struct list_head *next)
 {
-	CHECK_DATA_CORRUPTION(next->prev != prev,
-		"list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
-		prev, next->prev, next);
-	CHECK_DATA_CORRUPTION(prev->next != next,
-		"list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
-		next, prev->next, prev);
-	CHECK_DATA_CORRUPTION(new == prev || new == next,
-		"list_add double add: new=%p, prev=%p, next=%p.\n",
-		new, prev, next);

lib/list_debug.c line 32

Quoted 10 lines
lib/list_debug.c +10 -0
+	if (CHECK_DATA_CORRUPTION(next->prev != prev,
+			"list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
+			prev, next->prev, next) ||
+	    CHECK_DATA_CORRUPTION(prev->next != next,
+			"list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
+			next, prev->next, prev) ||
+	    CHECK_DATA_CORRUPTION(new == prev || new == next,
+			"list_add double add: new=%p, prev=%p, next=%p.\n",
+			new, prev, next))
+		return false;

That -is- one ornate "if" condition, isn't it?

Still it is nice to avoid the magic return from out of the middle of the C-preprocessor macro.

Thanx, Paul

Quoted 43 lines
lib/list_debug.c +14 -12
 	return true;
 }
@@ -41,18 +42,20 @@ bool __list_del_entry_valid(struct list_head *entry)
 	prev = entry->prev;
 	next = entry->next;
 
-	CHECK_DATA_CORRUPTION(next == LIST_POISON1,
-		"list_del corruption, %p->next is LIST_POISON1 (%p)\n",
-		entry, LIST_POISON1);
-	CHECK_DATA_CORRUPTION(prev == LIST_POISON2,
-		"list_del corruption, %p->prev is LIST_POISON2 (%p)\n",
-		entry, LIST_POISON2);
-	CHECK_DATA_CORRUPTION(prev->next != entry,
-		"list_del corruption. prev->next should be %p, but was %p\n",
-		entry, prev->next);
-	CHECK_DATA_CORRUPTION(next->prev != entry,
-		"list_del corruption. next->prev should be %p, but was %p\n",
-		entry, next->prev);
+	if (CHECK_DATA_CORRUPTION(next == LIST_POISON1,
+			"list_del corruption, %p->next is LIST_POISON1 (%p)\n",
+			entry, LIST_POISON1) ||
+	    CHECK_DATA_CORRUPTION(prev == LIST_POISON2,
+			"list_del corruption, %p->prev is LIST_POISON2 (%p)\n",
+			entry, LIST_POISON2) ||
+	    CHECK_DATA_CORRUPTION(prev->next != entry,
+			"list_del corruption. prev->next should be %p, but was %p\n",
+			entry, prev->next) ||
+	    CHECK_DATA_CORRUPTION(next->prev != entry,
+			"list_del corruption. next->prev should be %p, but was %p\n",
+			entry, next->prev))
+		return false;
+
 	return true;
 
 }
Signature · 6 lines
2.7.4


-- 
Kees Cook
Pixel Security
author
#4 lore raw
Quoted 21 lines · Paul E. McKenney
Quoted 20 lines · Kees Cook

The CHECK_DATA_CORRUPTION() macro was designed to have callers do something meaningful/protective on failure. However, using "return false" in the macro too strictly limits the design patterns of callers. Instead, let callers handle the logic test directly, but make sure that the result IS checked by forcing __must_check (which appears to not be able to be used directly on macro expressions).

---
 include/linux/bug.h | 12 +++++++-----
 lib/list_debug.c    | 45 ++++++++++++++++++++++++---------------------
 2 files changed, 31 insertions(+), 26 deletions(-)
include/linux/bug.h +0 -0
index baff2e8fc8a8..5828489309bb 100644
@@ -124,18 +124,20 @@ static inline enum bug_trap_type report_bug(unsigned long bug_addr,
 
Quoted 13 lines

include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 131

Quoted 10 lines
include/linux/bug.h +3 -3
 /*
  * Since detected data corruption should stop operation on the affected
- * structures, this returns false if the corruption condition is found.
+ * structures. Return value must be checked and sanely acted on by caller.
  */
+static inline __must_check bool check_data_corruption(bool v) { return v; }
 #define CHECK_DATA_CORRUPTION(condition, fmt, ...)                    \
-     do {                                                             \
-             if (unlikely(condition)) {                               \
+     check_data_corruption(({                                         \

The definition of check_data_corruption() is in some other patch? I don't see it in current mainline. I am not seeing what it might be doing.

It's immediately before the #define line above. It's nothing more than an inline argument pass-through, but since it's a _function_ I can attach __must_check to it, which I can't do for a conditional expression macro. And I gave it the meaningful name so when someone fails to check CHECK_DATA_CORRUPTION, they'll get a gcc warning about "check_data_corruption" which will lead them here.

Quoted 3 lines

include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 132

Quoted 1 line
include/linux/bug.h +1 -0
+             bool corruption = unlikely(condition);                   \

So corruption = unlikely(condition)? Sounds a bit optimistic to me! ;-)

It's true though! :) Nearly all calls to CHECK_DATA_CORRUPTION() should end up with a false condition.

Quoted 35 lines
Quoted 35 lines
include/linux/bug.h +3 -2
+             if (corruption) {                                        \
                      if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
                              pr_err(fmt, ##__VA_ARGS__);              \
                              BUG();                                   \
                      } else                                           \
                              WARN(1, fmt, ##__VA_ARGS__);             \
-                     return false;                                    \
              }                                                        \
-     } while (0)
+             corruption;                                              \
+     }))
 
 #endif       /* _LINUX_BUG_H */
lib/list_debug.c +4 -9
index 7f7bfa55eb6d..a34db8d27667 100644
@@ -20,15 +20,16 @@
 bool __list_add_valid(struct list_head *new, struct list_head *prev,
                    struct list_head *next)
 {
-     CHECK_DATA_CORRUPTION(next->prev != prev,
-             "list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
-             prev, next->prev, next);
-     CHECK_DATA_CORRUPTION(prev->next != next,
-             "list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
-             next, prev->next, prev);
-     CHECK_DATA_CORRUPTION(new == prev || new == next,
-             "list_add double add: new=%p, prev=%p, next=%p.\n",
-             new, prev, next);
+     if (CHECK_DATA_CORRUPTION(next->prev != prev,
+                     "list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
+                     prev, next->prev, next) ||
+         CHECK_DATA_CORRUPTION(prev->next != next,
Quoted 8 lines

lib/list_debug.c line 32

Quoted 6 lines
lib/list_debug.c +6 -0
+                     "list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
+                     next, prev->next, prev) ||
+         CHECK_DATA_CORRUPTION(new == prev || new == next,
+                     "list_add double add: new=%p, prev=%p, next=%p.\n",
+                     new, prev, next))
+             return false;

That -is- one ornate "if" condition, isn't it?

It is, yes. :)

Quoted 2 lines

Still it is nice to avoid the magic return from out of the middle of the C-preprocessor macro.

Agreed. I had fun with indenting to make it passably readable. :P

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw
Quoted 37 lines · Kees Cook
Quoted 21 lines · Paul E. McKenney
Quoted 20 lines · Kees Cook

The CHECK_DATA_CORRUPTION() macro was designed to have callers do something meaningful/protective on failure. However, using "return false" in the macro too strictly limits the design patterns of callers. Instead, let callers handle the logic test directly, but make sure that the result IS checked by forcing __must_check (which appears to not be able to be used directly on macro expressions).

---
 include/linux/bug.h | 12 +++++++-----
 lib/list_debug.c    | 45 ++++++++++++++++++++++++---------------------
 2 files changed, 31 insertions(+), 26 deletions(-)
include/linux/bug.h +0 -0
index baff2e8fc8a8..5828489309bb 100644
@@ -124,18 +124,20 @@ static inline enum bug_trap_type report_bug(unsigned long bug_addr,
 
Quoted 13 lines

include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 131

Quoted 10 lines
include/linux/bug.h +3 -3
 /*
  * Since detected data corruption should stop operation on the affected
- * structures, this returns false if the corruption condition is found.
+ * structures. Return value must be checked and sanely acted on by caller.
  */
+static inline __must_check bool check_data_corruption(bool v) { return v; }
 #define CHECK_DATA_CORRUPTION(condition, fmt, ...)                    \
-     do {                                                             \
-             if (unlikely(condition)) {                               \
+     check_data_corruption(({                                         \

The definition of check_data_corruption() is in some other patch? I don't see it in current mainline. I am not seeing what it might be doing.

Quoted 6 lines

It's immediately before the #define line above. It's nothing more than an inline argument pass-through, but since it's a _function_ I can attach __must_check to it, which I can't do for a conditional expression macro. And I gave it the meaningful name so when someone fails to check CHECK_DATA_CORRUPTION, they'll get a gcc warning about "check_data_corruption" which will lead them here.

Ah, I see it now. Color me blind!

Thanx, Paul

Quoted 64 lines
Quoted 3 lines

include/linux/bug.h static inline enum bug_trap_type report_bug(unsigned long bug_addr, · line 132

Quoted 1 line
include/linux/bug.h +1 -0
+             bool corruption = unlikely(condition);                   \

So corruption = unlikely(condition)? Sounds a bit optimistic to me! ;-)

It's true though! :) Nearly all calls to CHECK_DATA_CORRUPTION() should end up with a false condition.

Quoted 35 lines
Quoted 35 lines
include/linux/bug.h +3 -2
+             if (corruption) {                                        \
                      if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
                              pr_err(fmt, ##__VA_ARGS__);              \
                              BUG();                                   \
                      } else                                           \
                              WARN(1, fmt, ##__VA_ARGS__);             \
-                     return false;                                    \
              }                                                        \
-     } while (0)
+             corruption;                                              \
+     }))
 
 #endif       /* _LINUX_BUG_H */
lib/list_debug.c +4 -9
index 7f7bfa55eb6d..a34db8d27667 100644
@@ -20,15 +20,16 @@
 bool __list_add_valid(struct list_head *new, struct list_head *prev,
                    struct list_head *next)
 {
-     CHECK_DATA_CORRUPTION(next->prev != prev,
-             "list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
-             prev, next->prev, next);
-     CHECK_DATA_CORRUPTION(prev->next != next,
-             "list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
-             next, prev->next, prev);
-     CHECK_DATA_CORRUPTION(new == prev || new == next,
-             "list_add double add: new=%p, prev=%p, next=%p.\n",
-             new, prev, next);
+     if (CHECK_DATA_CORRUPTION(next->prev != prev,
+                     "list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
+                     prev, next->prev, next) ||
+         CHECK_DATA_CORRUPTION(prev->next != next,
Quoted 8 lines

lib/list_debug.c line 32

Quoted 6 lines
lib/list_debug.c +6 -0
+                     "list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
+                     next, prev->next, prev) ||
+         CHECK_DATA_CORRUPTION(new == prev || new == next,
+                     "list_add double add: new=%p, prev=%p, next=%p.\n",
+                     new, prev, next))
+             return false;

That -is- one ornate "if" condition, isn't it?

It is, yes. :)

Quoted 2 lines

Still it is nice to avoid the magic return from out of the middle of the C-preprocessor macro.

Agreed. I had fun with indenting to make it passably readable. :P

-Kees

Signature · 2 lines
Kees Cook
Pixel Security