Skip to contentSkip to conversation

fork: don't copy inconsistent signal handler state to child

PATCH In review Jann Horn 2 participants 2 messages Jul 2, 2018

Subsystems (3)
lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 fork: don't copy inconsistent signal handler state to child

Before this change, if a multithreaded process forks while one of its threads is changing a signal handler using sigaction(), the memcpy() in copy_sighand() can race with the struct assignment in do_sigaction(). It isn't clear whether this can cause corruption of the userspace signal handler pointer, but it definitely can cause inconsistency between different fields of struct sigaction.

Take the appropriate spinlock to avoid this.

I have tested that this patch prevents inconsistency between sa_sigaction and sa_flags, which is possible before this patch.

---
I haven't added a CC stable line to this patch because I feel that it doesn't quite meet the bar; if anyone disagrees, please say so.

 kernel/fork.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/fork.c +2 -0
index 9440d61b925c..4c86e8daae53 100644
@@ -1394,7 +1394,9 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
 		return -ENOMEM;
 
 	atomic_set(&sig->count, 1);
+	spin_lock_irq(&current->sighand->siglock);
 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
+	spin_unlock_irq(&current->sighand->siglock);
 	return 0;
 }
 
Signature · 1 line
2.18.0.399.gad0ab374a1-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jann Horn

Before this change, if a multithreaded process forks while one of its threads is changing a signal handler using sigaction(), the memcpy() in copy_sighand() can race with the struct assignment in do_sigaction(). It isn't clear whether this can cause corruption of the userspace signal handler pointer, but it definitely can cause inconsistency between different fields of struct sigaction.

Take the appropriate spinlock to avoid this.

I have tested that this patch prevents inconsistency between sa_sigaction and sa_flags, which is possible before this patch.

Looks sensible to me.

Quoted 1 line
Quoted 25 lines

---
I haven't added a CC stable line to this patch because I feel that it doesn't quite meet the bar; if anyone disagrees, please say so.

 kernel/fork.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/fork.c +2 -0
index 9440d61b925c..4c86e8daae53 100644
@@ -1394,7 +1394,9 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
 		return -ENOMEM;
 
 	atomic_set(&sig->count, 1);
+	spin_lock_irq(&current->sighand->siglock);
 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
+	spin_unlock_irq(&current->sighand->siglock);
 	return 0;
 }
 
Signature · 1 line
2.18.0.399.gad0ab374a1-goog
Signature · 2 lines
Michal Hocko
SUSE Labs