Skip to contentSkip to conversation

rcu: kasan: record and print kvfree_call_rcu call stack

PATCH Superseded [email protected] 4 participants 10 messages Nov 18, 2020 – Nov 20, 2020 RCU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rcu: kasan: record and print kvfree_call_rcu call stack

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/tree.c +1 -1
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 		success = true;
 		goto unlock_return;
 	}
-
+	kasan_record_aux_stack(ptr);
 	success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
 	if (!success) {
 		run_page_cache_worker(krcp);
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 20 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/tree.c +1 -1
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 		success = true;
 		goto unlock_return;
 	}
-
+	kasan_record_aux_stack(ptr);
 	success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
 	if (!success) {
 		run_page_cache_worker(krcp);
Signature · 1 line
2.17.1
Acked-by
#2 lore raw
Quoted 20 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3512

Quoted 13 lines
kernel/rcu/tree.c +1 -1
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
              success = true;
              goto unlock_return;
      }
-
+     kasan_record_aux_stack(ptr);
      success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
      if (!success) {
              run_page_cache_worker(krcp);

kvfree_call_rcu is intended to free objects, right? If so this is:

maintainer
#3 lore raw
Quoted 21 lines · Dmitry Vyukov
Quoted 20 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
kernel/rcu/tree.c +0 -0
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Quoted 8 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3512

Quoted 8 lines
kernel/rcu/tree.c +1 -1
              success = true;
              goto unlock_return;
      }
-
+     kasan_record_aux_stack(ptr);
      success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
      if (!success) {
              run_page_cache_worker(krcp);

kvfree_call_rcu is intended to free objects, right? If so this is:

True, but mightn't there still be RCU readers referencing this object for some time, as in up to the point that the RCU grace period ends? If so, won't adding this cause KASAN to incorrectly complain about those readers?

Or am I missing something here?

Thanx, Paul

Quoted 1 line
#4 lore raw
Quoted 39 lines · Paul E. McKenney
Quoted 21 lines · Dmitry Vyukov
Quoted 20 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
kernel/rcu/tree.c +0 -0
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Quoted 8 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3512

Quoted 8 lines
kernel/rcu/tree.c +1 -1
              success = true;
              goto unlock_return;
      }
-
+     kasan_record_aux_stack(ptr);
      success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
      if (!success) {
              run_page_cache_worker(krcp);

kvfree_call_rcu is intended to free objects, right? If so this is:

Quoted 5 lines

True, but mightn't there still be RCU readers referencing this object for some time, as in up to the point that the RCU grace period ends? If so, won't adding this cause KASAN to incorrectly complain about those readers?

Or am I missing something here?

kvfree_call_rcu does not check anything, not poison the object for future accesses (it is also called in call_rcu which does not necessarily free the object).
It just notes the current stack to provide in reports later. The problem is that the free stack is pointless for objects freed by rcu. In such cases we want call_rcu/kvfree_call_rcu stack in use-after-free reports.

maintainer Acked-by Dmitry Vyukov Reviewed-by Uladzislau Rezki (Sony)
#5 lore raw
Quoted 46 lines · Dmitry Vyukov
Quoted 39 lines · Paul E. McKenney
Quoted 21 lines · Dmitry Vyukov
Quoted 20 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
kernel/rcu/tree.c +0 -0
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Quoted 8 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3512

Quoted 8 lines
kernel/rcu/tree.c +1 -1
              success = true;
              goto unlock_return;
      }
-
+     kasan_record_aux_stack(ptr);
      success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
      if (!success) {
              run_page_cache_worker(krcp);

kvfree_call_rcu is intended to free objects, right? If so this is:

Quoted 5 lines

True, but mightn't there still be RCU readers referencing this object for some time, as in up to the point that the RCU grace period ends? If so, won't adding this cause KASAN to incorrectly complain about those readers?

Or am I missing something here?

Quoted 7 lines

kvfree_call_rcu does not check anything, not poison the object for future accesses (it is also called in call_rcu which does not necessarily free the object).
It just notes the current stack to provide in reports later. The problem is that the free stack is pointless for objects freed by rcu. In such cases we want call_rcu/kvfree_call_rcu stack in use-after-free reports.

OK, sounds good, thank you!

I will take this patch with your ack and Uladzislau's reviewed-by. I had to forward-port this to -rcu brach "dev", and along the way I updated the commit log to make Dmitry's point above, so please let me know if I messed anything up.

Thanx, Paul

------------------------------------------------------------------------

commit 3ce23b2df528877623ffc9c9cc2b6885eb3ae9db

Author: Zqiang <[email protected]>
Date:   Fri Nov 20 06:53:11 2020 -0800

rcu: Record kvfree_call_rcu() call stack for KASAN

This commit adds a call to kasan_record_aux_stack() in kvfree_call_rcu() in order to record the call stack of the code that caused the object to be freed. Please note that this function does not update the allocated/freed state, which is important because RCU readers might still be referencing this object.

kernel/rcu/tree.c +1 -0
index 1d956f9..4aa7745 100644
@@ -3514,6 +3514,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 		return;
 	}
 
+	kasan_record_aux_stack(ptr);
 	success = add_ptr_to_bulk_krc_lock(&krcp, &flags, ptr, !head);
 	if (!success) {
 		run_page_cache_worker(krcp);
maintainer
#6 lore raw
In reply to Paul E. McKenney
Quoted 38 lines · Paul E. McKenney
Quoted 21 lines · Dmitry Vyukov
Quoted 20 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
kernel/rcu/tree.c +0 -0
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
Quoted 8 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3512

Quoted 8 lines
kernel/rcu/tree.c +1 -1
              success = true;
              goto unlock_return;
      }
-
+     kasan_record_aux_stack(ptr);
      success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
      if (!success) {
              run_page_cache_worker(krcp);

kvfree_call_rcu is intended to free objects, right? If so this is:

Quoted 5 lines

True, but mightn't there still be RCU readers referencing this object for some time, as in up to the point that the RCU grace period ends? If so, won't adding this cause KASAN to incorrectly complain about those readers?

Or am I missing something here?

For example, is kasan_record_aux_stack() -only- recording the stack and not at all updating the allocated/freed state?

Thanx, Paul

Quoted 1 line
Quoted 1 line
maintainer
#7 lore raw
In reply to Paul E. McKenney
Quoted 19 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3509

Quoted 10 lines
kernel/rcu/tree.c +1 -1
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 		success = true;
 		goto unlock_return;
 	}
-
+	kasan_record_aux_stack(ptr);

Is that save to invoke it on vmalloced ptr.?

--
Vlad Rezki

#8 lore raw
Quoted 21 lines · Uladzislau Rezki
Quoted 19 lines · Paul E. McKenney
Quoted 6 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines
Quoted 10 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3509

Quoted 10 lines
kernel/rcu/tree.c +1 -1
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
            success = true;
            goto unlock_return;
    }
-
+   kasan_record_aux_stack(ptr);

Is that save to invoke it on vmalloced ptr.?

Yes, kasan_record_aux_stack should figure it out itself. We call kasan_record_aux_stack on call_rcu as well, and rcu structs can be anywhere.
See:
https://elixir.bootlin.com/linux/v5.10-rc4/source/mm/kasan/generic.c#L335

maintainer Reviewed-by
#9 lore raw
Quoted 34 lines · Uladzislau Rezki
Quoted 21 lines · Paul E. McKenney
Quoted 19 lines · On Wed, Nov 18, 2020 at 11:53:09AM +0800, [email protected] wrote:
Quoted 6 lines

From: Zqiang <[email protected]>

Add kasan_record_aux_stack function for kvfree_call_rcu function to record call stacks.

Thank you, but this does not apply on the "dev" branch of the -rcu tree. See file:///home/git/kernel.org/rcutodo.html for more info.

Adding others on CC who might have feedback on the general approach.

Thanx, Paul

Quoted 4 lines
---
 kernel/rcu/tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines
Quoted 10 lines

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3509

Quoted 10 lines
kernel/rcu/tree.c +1 -1
index da3414522285..a252b2f0208d 100644
@@ -3506,7 +3506,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
            success = true;
            goto unlock_return;
    }
-
+   kasan_record_aux_stack(ptr);

Is that save to invoke it on vmalloced ptr.?

Quoted 6 lines

Yes, kasan_record_aux_stack should figure it out itself. We call kasan_record_aux_stack on call_rcu as well, and rcu structs can be anywhere.
See:
https://elixir.bootlin.com/linux/v5.10-rc4/source/mm/kasan/generic.c#L335

Ah, i see the check. Just a minor comment, please also add a clear comment about intention of doing such record.

Thanks.

--
Vlad Rezki