Skip to content

Commit ab7709b

Browse files
ebiedermgregkh
authored andcommitted
exec: Transform exec_update_mutex into a rw_semaphore
[ Upstream commit f7cfd87 ] Recently syzbot reported[0] that there is a deadlock amongst the users of exec_update_mutex. The problematic lock ordering found by lockdep was: perf_event_open (exec_update_mutex -> ovl_i_mutex) chown (ovl_i_mutex -> sb_writes) sendfile (sb_writes -> p->lock) by reading from a proc file and writing to overlayfs proc_pid_syscall (p->lock -> exec_update_mutex) While looking at possible solutions it occured to me that all of the users and possible users involved only wanted to state of the given process to remain the same. They are all readers. The only writer is exec. There is no reason for readers to block on each other. So fix this deadlock by transforming exec_update_mutex into a rw_semaphore named exec_update_lock that only exec takes for writing. Cc: Jann Horn <jannh@google.com> Cc: Vasiliy Kulikov <segoon@openwall.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Bernd Edlinger <bernd.edlinger@hotmail.de> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Christopher Yeoh <cyeoh@au1.ibm.com> Cc: Cyrill Gorcunov <gorcunov@gmail.com> Cc: Sargun Dhillon <sargun@sargun.me> Cc: Christian Brauner <christian.brauner@ubuntu.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Fixes: eea9673 ("exec: Add exec_update_mutex to replace cred_guard_mutex") [0] https://lkml.kernel.org/r/00000000000063640c05ade8e3de@google.com Reported-by: syzbot+db9cdf3dd1f64252c6ef@syzkaller.appspotmail.com Link: https://lkml.kernel.org/r/87ft4mbqen.fsf@x220.int.ebiederm.org Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 933b7cc commit ab7709b

8 files changed

Lines changed: 44 additions & 43 deletions

File tree

‎fs/exec.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,8 @@ EXPORT_SYMBOL(read_code);
965965

966966
/*
967967
* Maps the mm_struct mm into the current task struct.
968-
* On success, this function returns with the mutex
969-
* exec_update_mutex locked.
968+
* On success, this function returns with exec_update_lock
969+
* held for writing.
970970
*/
971971
static int exec_mmap(struct mm_struct *mm)
972972
{
@@ -981,7 +981,7 @@ static int exec_mmap(struct mm_struct *mm)
981981
if (old_mm)
982982
sync_mm_rss(old_mm);
983983

984-
ret = mutex_lock_killable(&tsk->signal->exec_update_mutex);
984+
ret = down_write_killable(&tsk->signal->exec_update_lock);
985985
if (ret)
986986
return ret;
987987

@@ -995,7 +995,7 @@ static int exec_mmap(struct mm_struct *mm)
995995
mmap_read_lock(old_mm);
996996
if (unlikely(old_mm->core_state)) {
997997
mmap_read_unlock(old_mm);
998-
mutex_unlock(&tsk->signal->exec_update_mutex);
998+
up_write(&tsk->signal->exec_update_lock);
999999
return -EINTR;
10001000
}
10011001
}
@@ -1382,7 +1382,7 @@ int begin_new_exec(struct linux_binprm * bprm)
13821382
return 0;
13831383

13841384
out_unlock:
1385-
mutex_unlock(&me->signal->exec_update_mutex);
1385+
up_write(&me->signal->exec_update_lock);
13861386
out:
13871387
return retval;
13881388
}
@@ -1423,7 +1423,7 @@ void setup_new_exec(struct linux_binprm * bprm)
14231423
* some architectures like powerpc
14241424
*/
14251425
me->mm->task_size = TASK_SIZE;
1426-
mutex_unlock(&me->signal->exec_update_mutex);
1426+
up_write(&me->signal->exec_update_lock);
14271427
mutex_unlock(&me->signal->cred_guard_mutex);
14281428
}
14291429
EXPORT_SYMBOL(setup_new_exec);

‎fs/proc/base.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,19 @@ static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
405405

406406
static int lock_trace(struct task_struct *task)
407407
{
408-
int err = mutex_lock_killable(&task->signal->exec_update_mutex);
408+
int err = down_read_killable(&task->signal->exec_update_lock);
409409
if (err)
410410
return err;
411411
if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
412-
mutex_unlock(&task->signal->exec_update_mutex);
412+
up_read(&task->signal->exec_update_lock);
413413
return -EPERM;
414414
}
415415
return 0;
416416
}
417417

418418
static void unlock_trace(struct task_struct *task)
419419
{
420-
mutex_unlock(&task->signal->exec_update_mutex);
420+
up_read(&task->signal->exec_update_lock);
421421
}
422422

423423
#ifdef CONFIG_STACKTRACE
@@ -2930,7 +2930,7 @@ static int do_io_accounting(struct task_struct *task, struct seq_file *m, int wh
29302930
unsigned long flags;
29312931
int result;
29322932

2933-
result = mutex_lock_killable(&task->signal->exec_update_mutex);
2933+
result = down_read_killable(&task->signal->exec_update_lock);
29342934
if (result)
29352935
return result;
29362936

@@ -2966,7 +2966,7 @@ static int do_io_accounting(struct task_struct *task, struct seq_file *m, int wh
29662966
result = 0;
29672967

29682968
out_unlock:
2969-
mutex_unlock(&task->signal->exec_update_mutex);
2969+
up_read(&task->signal->exec_update_lock);
29702970
return result;
29712971
}
29722972

‎include/linux/sched/signal.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,13 @@ struct signal_struct {
228228
* credential calculations
229229
* (notably. ptrace)
230230
* Deprecated do not use in new code.
231-
* Use exec_update_mutex instead.
232-
*/
233-
struct mutex exec_update_mutex; /* Held while task_struct is being
234-
* updated during exec, and may have
235-
* inconsistent permissions.
231+
* Use exec_update_lock instead.
236232
*/
233+
struct rw_semaphore exec_update_lock; /* Held while task_struct is
234+
* being updated during exec,
235+
* and may have inconsistent
236+
* permissions.
237+
*/
237238
} __randomize_layout;
238239

239240
/*

‎init/init_task.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static struct signal_struct init_signals = {
2626
.multiprocess = HLIST_HEAD_INIT,
2727
.rlim = INIT_RLIMITS,
2828
.cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex),
29-
.exec_update_mutex = __MUTEX_INITIALIZER(init_signals.exec_update_mutex),
29+
.exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock),
3030
#ifdef CONFIG_POSIX_TIMERS
3131
.posix_timers = LIST_HEAD_INIT(init_signals.posix_timers),
3232
.cputimer = {

‎kernel/events/core.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ static void put_ctx(struct perf_event_context *ctx)
13251325
* function.
13261326
*
13271327
* Lock order:
1328-
* exec_update_mutex
1328+
* exec_update_lock
13291329
* task_struct::perf_event_mutex
13301330
* perf_event_context::mutex
13311331
* perf_event::child_mutex;
@@ -11847,14 +11847,14 @@ SYSCALL_DEFINE5(perf_event_open,
1184711847
}
1184811848

1184911849
if (task) {
11850-
err = mutex_lock_interruptible(&task->signal->exec_update_mutex);
11850+
err = down_read_interruptible(&task->signal->exec_update_lock);
1185111851
if (err)
1185211852
goto err_file;
1185311853

1185411854
/*
1185511855
* Preserve ptrace permission check for backwards compatibility.
1185611856
*
11857-
* We must hold exec_update_mutex across this and any potential
11857+
* We must hold exec_update_lock across this and any potential
1185811858
* perf_install_in_context() call for this new event to
1185911859
* serialize against exec() altering our credentials (and the
1186011860
* perf_event_exit_task() that could imply).
@@ -12017,7 +12017,7 @@ SYSCALL_DEFINE5(perf_event_open,
1201712017
mutex_unlock(&ctx->mutex);
1201812018

1201912019
if (task) {
12020-
mutex_unlock(&task->signal->exec_update_mutex);
12020+
up_read(&task->signal->exec_update_lock);
1202112021
put_task_struct(task);
1202212022
}
1202312023

@@ -12041,7 +12041,7 @@ SYSCALL_DEFINE5(perf_event_open,
1204112041
mutex_unlock(&ctx->mutex);
1204212042
err_cred:
1204312043
if (task)
12044-
mutex_unlock(&task->signal->exec_update_mutex);
12044+
up_read(&task->signal->exec_update_lock);
1204512045
err_file:
1204612046
fput(event_file);
1204712047
err_context:
@@ -12358,7 +12358,7 @@ static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
1235812358
/*
1235912359
* When a child task exits, feed back event values to parent events.
1236012360
*
12361-
* Can be called with exec_update_mutex held when called from
12361+
* Can be called with exec_update_lock held when called from
1236212362
* setup_new_exec().
1236312363
*/
1236412364
void perf_event_exit_task(struct task_struct *child)

‎kernel/fork.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
12221222
struct mm_struct *mm;
12231223
int err;
12241224

1225-
err = mutex_lock_killable(&task->signal->exec_update_mutex);
1225+
err = down_read_killable(&task->signal->exec_update_lock);
12261226
if (err)
12271227
return ERR_PTR(err);
12281228

@@ -1232,7 +1232,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
12321232
mmput(mm);
12331233
mm = ERR_PTR(-EACCES);
12341234
}
1235-
mutex_unlock(&task->signal->exec_update_mutex);
1235+
up_read(&task->signal->exec_update_lock);
12361236

12371237
return mm;
12381238
}
@@ -1592,7 +1592,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
15921592
sig->oom_score_adj_min = current->signal->oom_score_adj_min;
15931593

15941594
mutex_init(&sig->cred_guard_mutex);
1595-
mutex_init(&sig->exec_update_mutex);
1595+
init_rwsem(&sig->exec_update_lock);
15961596

15971597
return 0;
15981598
}

‎kernel/kcmp.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,25 @@ get_file_raw_ptr(struct task_struct *task, unsigned int idx)
7575
return file;
7676
}
7777

78-
static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
78+
static void kcmp_unlock(struct rw_semaphore *l1, struct rw_semaphore *l2)
7979
{
80-
if (likely(m2 != m1))
81-
mutex_unlock(m2);
82-
mutex_unlock(m1);
80+
if (likely(l2 != l1))
81+
up_read(l2);
82+
up_read(l1);
8383
}
8484

85-
static int kcmp_lock(struct mutex *m1, struct mutex *m2)
85+
static int kcmp_lock(struct rw_semaphore *l1, struct rw_semaphore *l2)
8686
{
8787
int err;
8888

89-
if (m2 > m1)
90-
swap(m1, m2);
89+
if (l2 > l1)
90+
swap(l1, l2);
9191

92-
err = mutex_lock_killable(m1);
93-
if (!err && likely(m1 != m2)) {
94-
err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
92+
err = down_read_killable(l1);
93+
if (!err && likely(l1 != l2)) {
94+
err = down_read_killable_nested(l2, SINGLE_DEPTH_NESTING);
9595
if (err)
96-
mutex_unlock(m1);
96+
up_read(l1);
9797
}
9898

9999
return err;
@@ -173,8 +173,8 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
173173
/*
174174
* One should have enough rights to inspect task details.
175175
*/
176-
ret = kcmp_lock(&task1->signal->exec_update_mutex,
177-
&task2->signal->exec_update_mutex);
176+
ret = kcmp_lock(&task1->signal->exec_update_lock,
177+
&task2->signal->exec_update_lock);
178178
if (ret)
179179
goto err;
180180
if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
@@ -229,8 +229,8 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
229229
}
230230

231231
err_unlock:
232-
kcmp_unlock(&task1->signal->exec_update_mutex,
233-
&task2->signal->exec_update_mutex);
232+
kcmp_unlock(&task1->signal->exec_update_lock,
233+
&task2->signal->exec_update_lock);
234234
err:
235235
put_task_struct(task1);
236236
put_task_struct(task2);

‎kernel/pid.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd)
628628
struct file *file;
629629
int ret;
630630

631-
ret = mutex_lock_killable(&task->signal->exec_update_mutex);
631+
ret = down_read_killable(&task->signal->exec_update_lock);
632632
if (ret)
633633
return ERR_PTR(ret);
634634

@@ -637,7 +637,7 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd)
637637
else
638638
file = ERR_PTR(-EPERM);
639639

640-
mutex_unlock(&task->signal->exec_update_mutex);
640+
up_read(&task->signal->exec_update_lock);
641641

642642
return file ?: ERR_PTR(-EBADF);
643643
}

0 commit comments

Comments
 (0)