Commit 81ced12a authored by Linus Torvalds's avatar Linus Torvalds Committed by Willy Tarreau

Initialize msg/shm IPC objects before doing ipc_addid()

commit b9a53227 upstream.

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 e8577d1f:
"ipc/sem.c: fully initialize sem_array before making it visible") but we
clearly forgot about msg and shm.
Reported-by: default avatarDmitry Vyukov <dvyukov@google.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Davidlohr Bueso <dbueso@suse.de>
Cc: stable@vger.kernel.org
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
[bwh: Backported to 2.6.32:
 - Adjust context
 - The error path being moved looks a little different]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
Signed-off-by: default avatarWilly Tarreau <w@1wt.eu>
parent 424d5990
...@@ -199,6 +199,15 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -199,6 +199,15 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
return retval; return retval;
} }
msq->q_stime = msq->q_rtime = 0;
msq->q_ctime = get_seconds();
msq->q_cbytes = msq->q_qnum = 0;
msq->q_qbytes = ns->msg_ctlmnb;
msq->q_lspid = msq->q_lrpid = 0;
INIT_LIST_HEAD(&msq->q_messages);
INIT_LIST_HEAD(&msq->q_receivers);
INIT_LIST_HEAD(&msq->q_senders);
/* /*
* ipc_addid() locks msq * ipc_addid() locks msq
*/ */
...@@ -209,15 +218,6 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -209,15 +218,6 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
return id; return id;
} }
msq->q_stime = msq->q_rtime = 0;
msq->q_ctime = get_seconds();
msq->q_cbytes = msq->q_qnum = 0;
msq->q_qbytes = ns->msg_ctlmnb;
msq->q_lspid = msq->q_lrpid = 0;
INIT_LIST_HEAD(&msq->q_messages);
INIT_LIST_HEAD(&msq->q_receivers);
INIT_LIST_HEAD(&msq->q_senders);
msg_unlock(msq); msg_unlock(msq);
return msq->q_perm.id; return msq->q_perm.id;
......
...@@ -386,12 +386,6 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -386,12 +386,6 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
if (IS_ERR(file)) if (IS_ERR(file))
goto no_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_cprid = task_tgid_vnr(current);
shp->shm_lprid = 0; shp->shm_lprid = 0;
shp->shm_atim = shp->shm_dtim = 0; shp->shm_atim = shp->shm_dtim = 0;
...@@ -399,6 +393,13 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params) ...@@ -399,6 +393,13 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
shp->shm_segsz = size; shp->shm_segsz = size;
shp->shm_nattch = 0; shp->shm_nattch = 0;
shp->shm_file = file; shp->shm_file = file;
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. * shmid gets reported as "inode#" in /proc/pid/maps.
* proc-ps tools use this. Changing this will break them. * proc-ps tools use this. Changing this will break them.
......
...@@ -264,6 +264,10 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) ...@@ -264,6 +264,10 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
rcu_read_lock(); rcu_read_lock();
spin_lock(&new->lock); spin_lock(&new->lock);
current_euid_egid(&euid, &egid);
new->cuid = new->uid = euid;
new->gid = new->cgid = egid;
err = idr_get_new(&ids->ipcs_idr, new, &id); err = idr_get_new(&ids->ipcs_idr, new, &id);
if (err) { if (err) {
spin_unlock(&new->lock); spin_unlock(&new->lock);
...@@ -273,10 +277,6 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) ...@@ -273,10 +277,6 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
ids->in_use++; ids->in_use++;
current_euid_egid(&euid, &egid);
new->cuid = new->uid = euid;
new->gid = new->cgid = egid;
new->seq = ids->seq++; new->seq = ids->seq++;
if(ids->seq > ids->seq_max) if(ids->seq > ids->seq_max)
ids->seq = 0; ids->seq = 0;
......
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