Skip to content

Commit 39cfffd

Browse files
manfred-colorfutorvalds
authored andcommitted
ipc/util.c: use ipc_rcu_putref() for failues in ipc_addid()
ipc_addid() is impossible to use: - for certain failures, the caller must not use ipc_rcu_putref(), because the reference counter is not yet initialized. - for other failures, the caller must use ipc_rcu_putref(), because parallel operations could be ongoing already. The patch cleans that up, by initializing the refcount early, and by modifying all callers. The issues is related to the finding of syzbot+2827ef6b3385deb07eaf@syzkaller.appspotmail.com: syzbot found an issue with reading kern_ipc_perm.seq, here both read and write to already released memory could happen. Link: http://lkml.kernel.org/r/20180712185241.4017-4-manfred@colorfullife.com Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Kees Cook <keescook@chromium.org> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Davidlohr Bueso <dbueso@suse.de> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Michal Hocko <mhocko@suse.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e2652ae commit 39cfffd

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

ipc/msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
163163
/* ipc_addid() locks msq upon success. */
164164
retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
165165
if (retval < 0) {
166-
call_rcu(&msq->q_perm.rcu, msg_rcu_free);
166+
ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
167167
return retval;
168168
}
169169

ipc/sem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
557557
/* ipc_addid() locks sma upon success. */
558558
retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
559559
if (retval < 0) {
560-
call_rcu(&sma->sem_perm.rcu, sem_rcu_free);
560+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
561561
return retval;
562562
}
563563
ns->used_sems += nsems;

ipc/shm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
684684
if (is_file_hugepages(file) && shp->mlock_user)
685685
user_shm_unlock(size, shp->mlock_user);
686686
fput(file);
687+
ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
688+
return error;
687689
no_file:
688690
call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
689691
return error;

ipc/util.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
251251
* Add an entry 'new' to the ipc ids idr. The permissions object is
252252
* initialised and the first free entry is set up and the id assigned
253253
* is returned. The 'new' entry is returned in a locked state on success.
254+
*
254255
* On failure the entry is not locked and a negative err-code is returned.
256+
* The caller must use ipc_rcu_putref() to free the identifier.
255257
*
256258
* Called with writer ipc_ids.rwsem held.
257259
*/
@@ -261,6 +263,9 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
261263
kgid_t egid;
262264
int idx, err;
263265

266+
/* 1) Initialize the refcount so that ipc_rcu_putref works */
267+
refcount_set(&new->refcount, 1);
268+
264269
if (limit > IPCMNI)
265270
limit = IPCMNI;
266271

@@ -269,16 +274,16 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
269274

270275
idr_preload(GFP_KERNEL);
271276

272-
refcount_set(&new->refcount, 1);
273277
spin_lock_init(&new->lock);
274-
new->deleted = false;
275278
rcu_read_lock();
276279
spin_lock(&new->lock);
277280

278281
current_euid_egid(&euid, &egid);
279282
new->cuid = new->uid = euid;
280283
new->gid = new->cgid = egid;
281284

285+
new->deleted = false;
286+
282287
idx = ipc_idr_alloc(ids, new);
283288
idr_preload_end();
284289

@@ -291,6 +296,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
291296
}
292297
}
293298
if (idx < 0) {
299+
new->deleted = true;
294300
spin_unlock(&new->lock);
295301
rcu_read_unlock();
296302
return idx;

0 commit comments

Comments
 (0)