Skip to content

Commit

Permalink
Initialize msg/shm IPC objects before doing ipc_addid()
Browse files Browse the repository at this point in the history
(cherry pick from commit b9a532277938798b53178d5a66af6e2915cb27cf)

As reported by Dmitry Vyukov, we really shouldn't do ipc_addid() before
having initialized the IPC object state.  Yes, we initialize the IPC
object in a locked state, but with all the lockless RCU lookup work,
that IPC object lock no longer means that the state cannot be seen.

We already did this for the IPC semaphore code (see commit e8577d1f0329:
"ipc/sem.c: fully initialize sem_array before making it visible") but we
clearly forgot about msg and shm.

Reported-by: Dmitry Vyukov <[email protected]>
Cc: Manfred Spraul <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: [email protected]
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Mark Salyzyn <[email protected]>
Bug: 24551430
Change-Id: Ic7b980a6f2a79eb3c04fc43b6531c396645af1db
  • Loading branch information
torvalds authored and jrior001 committed Sep 6, 2016
1 parent a4d11ef commit a845bb1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
17 changes: 8 additions & 9 deletions ipc/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,6 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
return retval;
}

/* ipc_addid() locks msq upon success. */
id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
if (id < 0) {
ipc_rcu_putref(msq, msg_rcu_free);
return id;
}

msq->q_stime = msq->q_rtime = 0;
msq->q_ctime = get_seconds();
msq->q_cbytes = msq->q_qnum = 0;
Expand All @@ -218,8 +211,14 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
INIT_LIST_HEAD(&msq->q_receivers);
INIT_LIST_HEAD(&msq->q_senders);

ipc_unlock_object(&msq->q_perm);
rcu_read_unlock();
/* ipc_addid() locks msq upon success. */
id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
if (id < 0) {
ipc_rcu_putref(msq, msg_rcu_free);
return id;
}

msg_unlock(msq);

return msq->q_perm.id;
}
Expand Down
12 changes: 6 additions & 6 deletions ipc/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,6 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
if (IS_ERR(file))
goto no_file;

id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
if (id < 0) {
error = id;
goto no_id;
}

shp->shm_cprid = task_tgid_vnr(current);
shp->shm_lprid = 0;
shp->shm_atim = shp->shm_dtim = 0;
Expand All @@ -569,6 +563,12 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
shp->shm_file = file;
shp->shm_creator = current;

id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
if (id < 0) {
error = id;
goto no_id;
}

/*
* shmid gets reported as "inode#" in /proc/pid/maps.
* proc-ps tools use this. Changing this will break them.
Expand Down
8 changes: 4 additions & 4 deletions ipc/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
rcu_read_lock();
spin_lock(&new->lock);

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

id = idr_alloc(&ids->ipcs_idr, new,
(next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
GFP_NOWAIT);
Expand All @@ -304,10 +308,6 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)

ids->in_use++;

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

if (next_id < 0) {
new->seq = ids->seq++;
if (ids->seq > ids->seq_max)
Expand Down

0 comments on commit a845bb1

Please sign in to comment.