Commit 39cfffd7 authored by Manfred Spraul's avatar Manfred Spraul Committed by Linus Torvalds

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.comSigned-off-by: default avatarManfred 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: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent e2652ae6
...@@ -163,7 +163,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -163,7 +163,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
/* ipc_addid() locks msq upon success. */ /* ipc_addid() locks msq upon success. */
retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni); retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
if (retval < 0) { if (retval < 0) {
call_rcu(&msq->q_perm.rcu, msg_rcu_free); ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
return retval; return retval;
} }
......
...@@ -557,7 +557,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -557,7 +557,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
/* ipc_addid() locks sma upon success. */ /* ipc_addid() locks sma upon success. */
retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni); retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
if (retval < 0) { if (retval < 0) {
call_rcu(&sma->sem_perm.rcu, sem_rcu_free); ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
return retval; return retval;
} }
ns->used_sems += nsems; ns->used_sems += nsems;
......
...@@ -684,6 +684,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -684,6 +684,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
if (is_file_hugepages(file) && shp->mlock_user) if (is_file_hugepages(file) && shp->mlock_user)
user_shm_unlock(size, shp->mlock_user); user_shm_unlock(size, shp->mlock_user);
fput(file); fput(file);
ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
return error;
no_file: no_file:
call_rcu(&shp->shm_perm.rcu, shm_rcu_free); call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
return error; return error;
......
...@@ -251,7 +251,9 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) ...@@ -251,7 +251,9 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
* Add an entry 'new' to the ipc ids idr. The permissions object is * Add an entry 'new' to the ipc ids idr. The permissions object is
* initialised and the first free entry is set up and the id assigned * initialised and the first free entry is set up and the id assigned
* is returned. The 'new' entry is returned in a locked state on success. * is returned. The 'new' entry is returned in a locked state on success.
*
* On failure the entry is not locked and a negative err-code is returned. * On failure the entry is not locked and a negative err-code is returned.
* The caller must use ipc_rcu_putref() to free the identifier.
* *
* Called with writer ipc_ids.rwsem held. * Called with writer ipc_ids.rwsem held.
*/ */
...@@ -261,6 +263,9 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) ...@@ -261,6 +263,9 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
kgid_t egid; kgid_t egid;
int idx, err; int idx, err;
/* 1) Initialize the refcount so that ipc_rcu_putref works */
refcount_set(&new->refcount, 1);
if (limit > IPCMNI) if (limit > IPCMNI)
limit = IPCMNI; limit = IPCMNI;
...@@ -269,9 +274,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) ...@@ -269,9 +274,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
idr_preload(GFP_KERNEL); idr_preload(GFP_KERNEL);
refcount_set(&new->refcount, 1);
spin_lock_init(&new->lock); spin_lock_init(&new->lock);
new->deleted = false;
rcu_read_lock(); rcu_read_lock();
spin_lock(&new->lock); spin_lock(&new->lock);
...@@ -279,6 +282,8 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) ...@@ -279,6 +282,8 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
new->cuid = new->uid = euid; new->cuid = new->uid = euid;
new->gid = new->cgid = egid; new->gid = new->cgid = egid;
new->deleted = false;
idx = ipc_idr_alloc(ids, new); idx = ipc_idr_alloc(ids, new);
idr_preload_end(); idr_preload_end();
...@@ -291,6 +296,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) ...@@ -291,6 +296,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
} }
} }
if (idx < 0) { if (idx < 0) {
new->deleted = true;
spin_unlock(&new->lock); spin_unlock(&new->lock);
rcu_read_unlock(); rcu_read_unlock();
return idx; return idx;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment