Skip to contentSkip to conversation

kasan: Rename kasan_enabled to kasan_report_enabled

PATCH Superseded Aneesh Kumar K.V 2 participants 7 messages Sep 3, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 kasan: Rename kasan_enabled to kasan_report_enabled

The function only disable/enable reporting. In the later patch we will be adding a kasan early enable/disable. Rename kasan_enabled to properly reflect its function.

---
 mm/kasan/kasan.h  | 2 +-
 mm/kasan/report.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/kasan/kasan.h +1 -1
index c242adf6bc85..a6b46cc94907 100644
@@ -63,7 +63,7 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 		<< KASAN_SHADOW_SCALE_SHIFT);
 }
 
-static inline bool kasan_enabled(void)
+static inline bool kasan_report_enabled(void)
 {
 	return !current->kasan_depth;
 }
mm/kasan/report.c +1 -1
index e07c94fbd0ac..6c3f82b0240b 100644
@@ -220,7 +220,7 @@ void kasan_report(unsigned long addr, size_t size,
 {
 	struct kasan_access_info info;
 
-	if (likely(!kasan_enabled()))
+	if (likely(!kasan_report_enabled()))
 		return;
 
 	info.access_addr = (void *)addr;
Signature · 1 line
2.5.0
#1 lore raw

PATCH 2/4 kasan: MODULE_VADDR is not available on all archs

Use is_module_text_address instead

---
 mm/kasan/report.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/kasan/report.c +6 -3
index 6c3f82b0240b..01d2efec8ea4 100644
@@ -22,6 +22,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/kasan.h>
+#include <linux/module.h>
 
 #include <asm/sections.h>
 
@@ -85,9 +86,11 @@ static void print_error_description(struct kasan_access_info *info)
 
 static inline bool kernel_or_module_addr(const void *addr)
 {
-	return (addr >= (void *)_stext && addr < (void *)_end)
-		|| (addr >= (void *)MODULES_VADDR
-			&& addr < (void *)MODULES_END);
+	if (addr >= (void *)_stext && addr < (void *)_end)
+		return true;
+	if (is_module_text_address((unsigned long)addr))
+		return true;
+	return false;
 }
 
 static inline bool init_task_stack_addr(const void *addr)
Signature · 1 line
2.5.0
#3 lore raw

PATCH 3/4 kasan: Don't use kasan shadow pointer in generic functions

In reply to Aneesh Kumar K.V

We can't use generic functions like print_hex_dump to access kasan shadow region. This require us to setup another kasan shadow region for the address passed (kasan shadow address). Most architecture won't be able to do that. Hence make a copy of the shadow region row and pass that to generic functions.

---
 mm/kasan/report.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/kasan/report.c +8 -2
index 01d2efec8ea4..440bda3a3ecd 100644
@@ -164,14 +164,20 @@ static void print_shadow_for_address(const void *addr)
 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
 		char buffer[4 + (BITS_PER_LONG/8)*2];
+		char shadow_buf[SHADOW_BYTES_PER_ROW];
 
 		snprintf(buffer, sizeof(buffer),
 			(i == 0) ? ">%p: " : " %p: ", kaddr);
-
+		/*
+		 * We should not pass a shadow pointer to generic
+		 * function, because generic functions may try to
+		 * access kasan mapping for the passed address.
+		 */
 		kasan_disable_current();
+		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
 		print_hex_dump(KERN_ERR, buffer,
 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
-			shadow_row, SHADOW_BYTES_PER_ROW, 0);
+			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
 		kasan_enable_current();
 
 		if (row_is_guilty(shadow_row, shadow))
Signature · 1 line
2.5.0
#5 lore raw

PATCH 4/4 kasan: Prevent deadlock in kasan reporting

In reply to Aneesh Kumar K.V

We we end up calling kasan_report in real mode, our shadow mapping for even spinlock variable will show poisoned. This will result in us calling kasan_report_error with lock_report spin lock held. To prevent this disable kasan reporting when we are priting error w.r.t kasan.

---
 mm/kasan/report.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/report.c +10 -2
index 440bda3a3ecd..8c409b1664c8 100644
@@ -173,12 +173,10 @@ static void print_shadow_for_address(const void *addr)
 		 * function, because generic functions may try to
 		 * access kasan mapping for the passed address.
 		 */
-		kasan_disable_current();
 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
 		print_hex_dump(KERN_ERR, buffer,
 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
 			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
-		kasan_enable_current();
 
 		if (row_is_guilty(shadow_row, shadow))
 			pr_err("%*c\n",
@@ -195,6 +193,10 @@ void kasan_report_error(struct kasan_access_info *info)
 {
 	unsigned long flags;
 
+	/*
+	 * Make sure we don't end up in loop.
+	 */
+	kasan_disable_current();
 	spin_lock_irqsave(&report_lock, flags);
 	pr_err("================================="
 		"=================================\n");
@@ -204,12 +206,17 @@ void kasan_report_error(struct kasan_access_info *info)
 	pr_err("================================="
 		"=================================\n");
 	spin_unlock_irqrestore(&report_lock, flags);
+	kasan_enable_current();
 }
 
 void kasan_report_user_access(struct kasan_access_info *info)
 {
 	unsigned long flags;
 
+	/*
+	 * Make sure we don't end up in loop.
+	 */
+	kasan_disable_current();
 	spin_lock_irqsave(&report_lock, flags);
 	pr_err("================================="
 		"=================================\n");
@@ -222,6 +229,7 @@ void kasan_report_user_access(struct kasan_access_info *info)
 	pr_err("================================="
 		"=================================\n");
 	spin_unlock_irqrestore(&report_lock, flags);
+	kasan_enable_current();
 }
 
 void kasan_report(unsigned long addr, size_t size,
Signature · 1 line
2.5.0
maintainer
#2 lore raw

Re: [PATCH 2/4] kasan: MODULE_VADDR is not available on all archs

In reply to Aneesh Kumar K.V
Quoted 2 lines · Aneesh Kumar K.V

Use is_module_text_address instead

It should be is_module_address().

We use kernel_or_module_addr() to determine whether this address belongs to some global variable or not. And variables are in .data section, .text is only code.

Something like is_module_data_address() would be more precise here. But since we don't have it, we can just use is_module_address(). Definitely not is_module_text_address().

Quoted 33 lines
---
 mm/kasan/report.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/kasan/report.c +6 -3
index 6c3f82b0240b..01d2efec8ea4 100644
@@ -22,6 +22,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/kasan.h>
+#include <linux/module.h>
 
 #include <asm/sections.h>
 
@@ -85,9 +86,11 @@ static void print_error_description(struct kasan_access_info *info)
 
 static inline bool kernel_or_module_addr(const void *addr)
 {
-	return (addr >= (void *)_stext && addr < (void *)_end)
-		|| (addr >= (void *)MODULES_VADDR
-			&& addr < (void *)MODULES_END);
+	if (addr >= (void *)_stext && addr < (void *)_end)
+		return true;
+	if (is_module_text_address((unsigned long)addr))
+		return true;
+	return false;
 }
 
 static inline bool init_task_stack_addr(const void *addr)
maintainer Reviewed-by on v1 3/4
#4 lore raw

Re: [PATCH 3/4] kasan: Don't use kasan shadow pointer in generic functions

In reply to Aneesh Kumar K.V
Quoted 4 lines · Aneesh Kumar K.V

We can't use generic functions like print_hex_dump to access kasan shadow region. This require us to setup another kasan shadow region for the address passed (kasan shadow address). Most architecture won't be able to do that.

s/Most architecture/Some architectures

At least ARM/ARM64/x86 are able to do that.

Quoted 7 lines

Hence make a copy of the shadow region row and
pass that to generic functions.

---
 mm/kasan/report.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Anyway, for this patch:

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer Reviewed-by on v1 4/4
#6 lore raw

Re: [PATCH 4/4] kasan: Prevent deadlock in kasan reporting

In reply to Aneesh Kumar K.V
Quoted 1 line · Aneesh Kumar K.V

We we end up calling kasan_report in real mode, our shadow mapping

s/We we/We

Quoted 3 lines

for even spinlock variable will show poisoned. This will result in us calling kasan_report_error with lock_report spin lock held. To prevent this disable kasan reporting when we are priting

s/priting/printing

Quoted 7 lines

error w.r.t kasan.

---
 mm/kasan/report.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>