Skip to contentSkip to conversation

fix sighand use after free

PATCH In review Rik van Riel 2 participants 5 messages Aug 13, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix sighand use after free

Subject: fix sighand use after free

__lock_task_sighand carefully takes the rcu_read_lock, gets tsk->sighand with rcu_dereference, and verifies that the task is still using the sighand_struct after taking the spinlock.

This works much better if the sighand struct is actually rcu freed.

---
 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
index 1380d8a..754a7c6 100644
@@ -1017,7 +1017,7 @@ void __cleanup_sighand(struct sighand_struct *sighand)
 {
 	if (atomic_dec_and_test(&sighand->count)) {
 		signalfd_cleanup(sighand);
-		kmem_cache_free(sighand_cachep, sighand);
+		rcu_free(sighand_cachep, sighand);
 	}
 }
 
#1 lore raw
Quoted 9 lines · Rik van Riel

Subject: fix sighand use after free

__lock_task_sighand carefully takes the rcu_read_lock, gets tsk->sighand with rcu_dereference, and verifies that the task is still using the sighand_struct after taking the spinlock.

This works much better if the sighand struct is actually rcu freed.

I promise, I'll send the doc patch soon ;)

kernel/fork.c void __cleanup_sighand(struct sighand_struct *sighand) · line 1020

Quoted 8 lines
kernel/fork.c +1 -1
@@ -1017,7 +1017,7 @@ void __cleanup_sighand(struct sighand_struct *sighand)
 {
 	if (atomic_dec_and_test(&sighand->count)) {
 		signalfd_cleanup(sighand);
-		kmem_cache_free(sighand_cachep, sighand);
+		rcu_free(sighand_cachep, sighand);

Please note that sighand_cachep is SLAB_DESTROY_BY_RCU.

Hmm. and what is rcu_free() ?

Oleg.

#2 lore raw
Quoted 13 lines · Oleg Nesterov
Quoted 9 lines · Rik van Riel

Subject: fix sighand use after free

__lock_task_sighand carefully takes the rcu_read_lock, gets tsk->sighand with rcu_dereference, and verifies that the task is still using the sighand_struct after taking the spinlock.

This works much better if the sighand struct is actually rcu freed.

I promise, I'll send the doc patch soon ;)

Quoted 10 lines

kernel/fork.c void __cleanup_sighand(struct sighand_struct *sighand) · line 1020

Quoted 8 lines
kernel/fork.c +1 -1
@@ -1017,7 +1017,7 @@ void __cleanup_sighand(struct sighand_struct *sighand)
 {
 	if (atomic_dec_and_test(&sighand->count)) {
 		signalfd_cleanup(sighand);
-		kmem_cache_free(sighand_cachep, sighand);
+		rcu_free(sighand_cachep, sighand);

Please note that sighand_cachep is SLAB_DESTROY_BY_RCU.

SLAB_DESTROY_BY_RCU means that the slab page is not given back to the system until after the RCU grace period has expired.

However, the objects inside the slab can still be reused immediately!

In the case of the sighand struct, we have this possible race:

thread A thread B thread C

gets task A->sighand
kmem_cache_free sighand
re-alloc sighand
spin_lock sighand
spin_lock_init sighand
spin_unlock sighand

Now task C has a sighand which can never be locked.

Quoted 1 line

Hmm. and what is rcu_free() ?

Ugh, that should have been kfree_rcu of course, with appropriate rcu space in the struct.

Now I wonder why my test compile succeeded...

#3 lore raw
Quoted 13 lines · Rik van Riel
Quoted 10 lines · Oleg Nesterov

kernel/fork.c void __cleanup_sighand(struct sighand_struct *sighand) · line 1020

Quoted 7 lines · Rik van Riel
kernel/fork.c +1 -1
@@ -1017,7 +1017,7 @@ void __cleanup_sighand(struct sighand_struct *sighand)
 {
 	if (atomic_dec_and_test(&sighand->count)) {
 		signalfd_cleanup(sighand);
-		kmem_cache_free(sighand_cachep, sighand);
+		rcu_free(sighand_cachep, sighand);

Please note that sighand_cachep is SLAB_DESTROY_BY_RCU.

Quoted 6 lines

SLAB_DESTROY_BY_RCU means that the slab page is not given back to the system until after the RCU grace period has expired.

However, the objects inside the slab can still be reused immediately!

Yes. This is fine. This memory won't be returned to system before rcu gp pass, and this memory is still "struct sighand_struct" with the properly initialized ->siglock (note the sighand_ctor()).

Quoted 9 lines

In the case of the sighand struct, we have this possible race:

thread A thread B thread C

gets task A->sighand
kmem_cache_free sighand
re-alloc sighand
spin_lock sighand
spin_lock_init sighand

                                            ^^^^^^^^^^^^^^^^^^^^^^

see below,

Quoted 3 lines

spin_unlock sighand

Now task C has a sighand which can never be locked.

No, please see above. And that is why lock_task_sighand() (which in turn needs the comment and cleanup, I already have a patch) re-checks task-sighand with ->siglock.

Quoted 4 lines
Quoted 1 line

Hmm. and what is rcu_free() ?

Ugh, that should have been kfree_rcu of course, with appropriate rcu space in the struct.

kfree_rcu() can't work in this case, __rcu_reclaim() does kfree() but we need kmem_cache_free().

Oleg.

#4 lore raw
Quoted 18 lines · Oleg Nesterov
Quoted 17 lines · Rik van Riel
Quoted 10 lines · Oleg Nesterov

kernel/fork.c void __cleanup_sighand(struct sighand_struct *sighand) · line 1020

Quoted 7 lines · Rik van Riel
kernel/fork.c +1 -1
@@ -1017,7 +1017,7 @@ void __cleanup_sighand(struct sighand_struct *sighand)
 {
 	if (atomic_dec_and_test(&sighand->count)) {
 		signalfd_cleanup(sighand);
-		kmem_cache_free(sighand_cachep, sighand);
+		rcu_free(sighand_cachep, sighand);

Please note that sighand_cachep is SLAB_DESTROY_BY_RCU.

SLAB_DESTROY_BY_RCU means that the slab page is not given back to the system until after the RCU grace period has expired.

Quoted 6 lines
Quoted 2 lines

However, the objects inside the slab can still be reused immediately!

Yes. This is fine. This memory won't be returned to system before rcu gp pass, and this memory is still "struct sighand_struct" with the properly initialized ->siglock (note the sighand_ctor()).

Fair enough, you are right.

The sighand_ctor does prevent any issues.

The slub code also prevents slubs with constructor functions from ever getting merged, so this is all taken care of.