Skip to content

Commit ce85722

Browse files
Al Virotorvalds
authored andcommitted
ipc: fix GETALL/IPC_RM race for sysv semaphores
We can step on WARN_ON_ONCE() in sem_getref() if a semaphore is removed just as we are about to call sem_getref() from semctl_main(); results are not pretty. We should fail with -EIDRM, same as if IPC_RM happened while we'd been doing allocation there. This also expands sem_getref() at its only callsite (and fixed there), while sem_getref_and_unlock() is simply killed off - it has no callers at all. Signed-off-by: Al Viro <[email protected]> Acked-by: Davidlohr Bueso <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 20a2078 commit ce85722

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

ipc/sem.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -328,28 +328,12 @@ static inline void sem_lock_and_putref(struct sem_array *sma)
328328
ipc_rcu_putref(sma);
329329
}
330330

331-
static inline void sem_getref_and_unlock(struct sem_array *sma)
332-
{
333-
WARN_ON_ONCE(!ipc_rcu_getref(sma));
334-
sem_unlock(sma, -1);
335-
}
336-
337331
static inline void sem_putref(struct sem_array *sma)
338332
{
339333
sem_lock_and_putref(sma);
340334
sem_unlock(sma, -1);
341335
}
342336

343-
/*
344-
* Call inside the rcu read section.
345-
*/
346-
static inline void sem_getref(struct sem_array *sma)
347-
{
348-
sem_lock(sma, NULL, -1);
349-
WARN_ON_ONCE(!ipc_rcu_getref(sma));
350-
sem_unlock(sma, -1);
351-
}
352-
353337
static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
354338
{
355339
ipc_rmid(&sem_ids(ns), &s->sem_perm);
@@ -1116,9 +1100,14 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
11161100
ushort __user *array = p;
11171101
int i;
11181102

1103+
sem_lock(sma, NULL, -1);
11191104
if(nsems > SEMMSL_FAST) {
1120-
sem_getref(sma);
1121-
1105+
if (!ipc_rcu_getref(sma)) {
1106+
sem_unlock(sma, -1);
1107+
err = -EIDRM;
1108+
goto out_free;
1109+
}
1110+
sem_unlock(sma, -1);
11221111
sem_io = ipc_alloc(sizeof(ushort)*nsems);
11231112
if(sem_io == NULL) {
11241113
sem_putref(sma);
@@ -1131,9 +1120,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
11311120
err = -EIDRM;
11321121
goto out_free;
11331122
}
1134-
} else
1135-
sem_lock(sma, NULL, -1);
1136-
1123+
}
11371124
for (i = 0; i < sma->sem_nsems; i++)
11381125
sem_io[i] = sma->sem_base[i].semval;
11391126
sem_unlock(sma, -1);

0 commit comments

Comments
 (0)