Skip to content

Commit 283bb7f

Browse files
Pierre PeifferLinus Torvalds
authored andcommitted
IPC: fix error case when idr-cache is empty in ipcget()
With the use of idr to store the ipc, the case where the idr cache is empty, when idr_get_new is called (this may happen even if we call idr_pre_get() before), is not well handled: it lets semget()/shmget()/msgget() return ENOSPC when this cache is empty, what 1. does not reflect the facts and 2. does not conform to the man(s). This patch fixes this by retrying the whole process of allocation in this case. Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net> Cc: Nadia Derbey <Nadia.Derbey@bull.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3ac88a4 commit 283bb7f

4 files changed

Lines changed: 18 additions & 11 deletions

File tree

‎ipc/msg.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
203203
* ipc_addid() locks msq
204204
*/
205205
id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
206-
if (id == -1) {
206+
if (id < 0) {
207207
security_msg_queue_free(msq);
208208
ipc_rcu_putref(msq);
209-
return -ENOSPC;
209+
return id;
210210
}
211211

212212
msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);

‎ipc/sem.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
283283
}
284284

285285
id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
286-
if(id == -1) {
286+
if (id < 0) {
287287
security_sem_free(sma);
288288
ipc_rcu_putref(sma);
289-
return -ENOSPC;
289+
return id;
290290
}
291291
ns->used_sems += nsems;
292292

‎ipc/shm.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,11 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
433433
if (IS_ERR(file))
434434
goto no_file;
435435

436-
error = -ENOSPC;
437436
id = shm_addid(ns, shp);
438-
if(id == -1)
437+
if (id < 0) {
438+
error = id;
439439
goto no_id;
440+
}
440441

441442
shp->shm_cprid = task_tgid_vnr(current);
442443
shp->shm_lprid = 0;

‎ipc/util.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ int ipc_get_maxid(struct ipc_ids *ids)
262262
* Add an entry 'new' to the IPC ids idr. The permissions object is
263263
* initialised and the first free entry is set up and the id assigned
264264
* is returned. The 'new' entry is returned in a locked state on success.
265-
* On failure the entry is not locked and -1 is returned.
265+
* On failure the entry is not locked and a negative err-code is returned.
266266
*
267267
* Called with ipc_ids.rw_mutex held as a writer.
268268
*/
@@ -275,11 +275,11 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
275275
size = IPCMNI;
276276

277277
if (ids->in_use >= size)
278-
return -1;
278+
return -ENOSPC;
279279

280280
err = idr_get_new(&ids->ipcs_idr, new, &id);
281281
if (err)
282-
return -1;
282+
return err;
283283

284284
ids->in_use++;
285285

@@ -311,7 +311,7 @@ int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
311311
struct ipc_ops *ops, struct ipc_params *params)
312312
{
313313
int err;
314-
314+
retry:
315315
err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
316316

317317
if (!err)
@@ -321,6 +321,9 @@ int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
321321
err = ops->getnew(ns, params);
322322
up_write(&ids->rw_mutex);
323323

324+
if (err == -EAGAIN)
325+
goto retry;
326+
324327
return err;
325328
}
326329

@@ -374,7 +377,7 @@ int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
374377
struct kern_ipc_perm *ipcp;
375378
int flg = params->flg;
376379
int err;
377-
380+
retry:
378381
err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
379382

380383
/*
@@ -411,6 +414,9 @@ int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
411414
}
412415
up_write(&ids->rw_mutex);
413416

417+
if (err == -EAGAIN)
418+
goto retry;
419+
414420
return err;
415421
}
416422

0 commit comments

Comments
 (0)