Skip to content

Commit a5f75e7

Browse files
Pierre Peiffertorvalds
authored andcommitted
IPC: consolidate all xxxctl_down() functions
semctl_down(), msgctl_down() and shmctl_down() are used to handle the same set of commands for each kind of IPC. They all start to do the same job (they retrieve the ipc and do some permission checks) before handling the commands on their own. This patch proposes to consolidate this by moving these same pieces of code into one common function called ipcctl_pre_down(). It simplifies a little these xxxctl_down() functions and increases a little the maintainability. Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net> Acked-by: Serge Hallyn <serue@us.ibm.com> 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 8f4a380 commit a5f75e7

5 files changed

Lines changed: 66 additions & 119 deletions

File tree

‎ipc/msg.c‎

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,6 @@ void __init msg_init(void)
141141
IPC_MSG_IDS, sysvipc_msg_proc_show);
142142
}
143143

144-
/*
145-
* This routine is called in the paths where the rw_mutex is held to protect
146-
* access to the idr tree.
147-
*/
148-
static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
149-
int id)
150-
{
151-
struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
152-
153-
if (IS_ERR(ipcp))
154-
return (struct msg_queue *)ipcp;
155-
156-
return container_of(ipcp, struct msg_queue, q_perm);
157-
}
158-
159144
/*
160145
* msg_lock_(check_) routines are called in the paths where the rw_mutex
161146
* is not held.
@@ -437,35 +422,12 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
437422
return -EFAULT;
438423
}
439424

440-
down_write(&msg_ids(ns).rw_mutex);
441-
msq = msg_lock_check_down(ns, msqid);
442-
if (IS_ERR(msq)) {
443-
err = PTR_ERR(msq);
444-
goto out_up;
445-
}
446-
447-
ipcp = &msq->q_perm;
448-
449-
err = audit_ipc_obj(ipcp);
450-
if (err)
451-
goto out_unlock;
452-
453-
if (cmd == IPC_SET) {
454-
err = audit_ipc_set_perm(msqid64.msg_qbytes,
455-
msqid64.msg_perm.uid,
456-
msqid64.msg_perm.gid,
457-
msqid64.msg_perm.mode);
458-
if (err)
459-
goto out_unlock;
460-
}
425+
ipcp = ipcctl_pre_down(&msg_ids(ns), msqid, cmd,
426+
&msqid64.msg_perm, msqid64.msg_qbytes);
427+
if (IS_ERR(ipcp))
428+
return PTR_ERR(ipcp);
461429

462-
if (current->euid != ipcp->cuid &&
463-
current->euid != ipcp->uid &&
464-
!capable(CAP_SYS_ADMIN)) {
465-
/* We _could_ check for CAP_CHOWN above, but we don't */
466-
err = -EPERM;
467-
goto out_unlock;
468-
}
430+
msq = container_of(ipcp, struct msg_queue, q_perm);
469431

470432
err = security_msg_queue_msgctl(msq, cmd);
471433
if (err)

‎ipc/sem.c‎

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,6 @@ void __init sem_init (void)
140140
IPC_SEM_IDS, sysvipc_sem_proc_show);
141141
}
142142

143-
/*
144-
* This routine is called in the paths where the rw_mutex is held to protect
145-
* access to the idr tree.
146-
*/
147-
static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
148-
int id)
149-
{
150-
struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
151-
152-
if (IS_ERR(ipcp))
153-
return (struct sem_array *)ipcp;
154-
155-
return container_of(ipcp, struct sem_array, sem_perm);
156-
}
157-
158143
/*
159144
* sem_lock_(check_) routines are called in the paths where the rw_mutex
160145
* is not held.
@@ -878,31 +863,12 @@ static int semctl_down(struct ipc_namespace *ns, int semid,
878863
if (copy_semid_from_user(&semid64, arg.buf, version))
879864
return -EFAULT;
880865
}
881-
down_write(&sem_ids(ns).rw_mutex);
882-
sma = sem_lock_check_down(ns, semid);
883-
if (IS_ERR(sma)) {
884-
err = PTR_ERR(sma);
885-
goto out_up;
886-
}
887-
888-
ipcp = &sma->sem_perm;
889866

890-
err = audit_ipc_obj(ipcp);
891-
if (err)
892-
goto out_unlock;
867+
ipcp = ipcctl_pre_down(&sem_ids(ns), semid, cmd, &semid64.sem_perm, 0);
868+
if (IS_ERR(ipcp))
869+
return PTR_ERR(ipcp);
893870

894-
if (cmd == IPC_SET) {
895-
err = audit_ipc_set_perm(0, semid64.sem_perm.uid,
896-
semid64.sem_perm.gid,
897-
semid64.sem_perm.mode);
898-
if (err)
899-
goto out_unlock;
900-
}
901-
if (current->euid != ipcp->cuid &&
902-
current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
903-
err=-EPERM;
904-
goto out_unlock;
905-
}
871+
sma = container_of(ipcp, struct sem_array, sem_perm);
906872

907873
err = security_sem_semctl(sma, cmd);
908874
if (err)

‎ipc/shm.c‎

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,6 @@ static inline struct shmid_kernel *shm_lock_down(struct ipc_namespace *ns,
126126
return container_of(ipcp, struct shmid_kernel, shm_perm);
127127
}
128128

129-
static inline struct shmid_kernel *shm_lock_check_down(
130-
struct ipc_namespace *ns,
131-
int id)
132-
{
133-
struct kern_ipc_perm *ipcp = ipc_lock_check_down(&shm_ids(ns), id);
134-
135-
if (IS_ERR(ipcp))
136-
return (struct shmid_kernel *)ipcp;
137-
138-
return container_of(ipcp, struct shmid_kernel, shm_perm);
139-
}
140-
141129
/*
142130
* shm_lock_(check_) routines are called in the paths where the rw_mutex
143131
* is not held.
@@ -620,33 +608,11 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
620608
return -EFAULT;
621609
}
622610

623-
down_write(&shm_ids(ns).rw_mutex);
624-
shp = shm_lock_check_down(ns, shmid);
625-
if (IS_ERR(shp)) {
626-
err = PTR_ERR(shp);
627-
goto out_up;
628-
}
629-
630-
ipcp = &shp->shm_perm;
631-
632-
err = audit_ipc_obj(ipcp);
633-
if (err)
634-
goto out_unlock;
635-
636-
if (cmd == IPC_SET) {
637-
err = audit_ipc_set_perm(0, shmid64.shm_perm.uid,
638-
shmid64.shm_perm.gid,
639-
shmid64.shm_perm.mode);
640-
if (err)
641-
goto out_unlock;
642-
}
611+
ipcp = ipcctl_pre_down(&shm_ids(ns), shmid, cmd, &shmid64.shm_perm, 0);
612+
if (IS_ERR(ipcp))
613+
return PTR_ERR(ipcp);
643614

644-
if (current->euid != ipcp->uid &&
645-
current->euid != ipcp->cuid &&
646-
!capable(CAP_SYS_ADMIN)) {
647-
err = -EPERM;
648-
goto out_unlock;
649-
}
615+
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
650616

651617
err = security_shm_shmctl(shp, cmd);
652618
if (err)

‎ipc/util.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,57 @@ void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
824824
| (in->mode & S_IRWXUGO);
825825
}
826826

827+
/**
828+
* ipcctl_pre_down - retrieve an ipc and check permissions for some IPC_XXX cmd
829+
* @ids: the table of ids where to look for the ipc
830+
* @id: the id of the ipc to retrieve
831+
* @cmd: the cmd to check
832+
* @perm: the permission to set
833+
* @extra_perm: one extra permission parameter used by msq
834+
*
835+
* This function does some common audit and permissions check for some IPC_XXX
836+
* cmd and is called from semctl_down, shmctl_down and msgctl_down.
837+
* It must be called without any lock held and
838+
* - retrieves the ipc with the given id in the given table.
839+
* - performs some audit and permission check, depending on the given cmd
840+
* - returns the ipc with both ipc and rw_mutex locks held in case of success
841+
* or an err-code without any lock held otherwise.
842+
*/
843+
struct kern_ipc_perm *ipcctl_pre_down(struct ipc_ids *ids, int id, int cmd,
844+
struct ipc64_perm *perm, int extra_perm)
845+
{
846+
struct kern_ipc_perm *ipcp;
847+
int err;
848+
849+
down_write(&ids->rw_mutex);
850+
ipcp = ipc_lock_check_down(ids, id);
851+
if (IS_ERR(ipcp)) {
852+
err = PTR_ERR(ipcp);
853+
goto out_up;
854+
}
855+
856+
err = audit_ipc_obj(ipcp);
857+
if (err)
858+
goto out_unlock;
859+
860+
if (cmd == IPC_SET) {
861+
err = audit_ipc_set_perm(extra_perm, perm->uid,
862+
perm->gid, perm->mode);
863+
if (err)
864+
goto out_unlock;
865+
}
866+
if (current->euid == ipcp->cuid ||
867+
current->euid == ipcp->uid || capable(CAP_SYS_ADMIN))
868+
return ipcp;
869+
870+
err = -EPERM;
871+
out_unlock:
872+
ipc_unlock(ipcp);
873+
out_up:
874+
up_write(&ids->rw_mutex);
875+
return ERR_PTR(err);
876+
}
877+
827878
#ifdef __ARCH_WANT_IPC_PARSE_VERSION
828879

829880

‎ipc/util.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
113113
void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
114114
void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
115115
void ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
116+
struct kern_ipc_perm *ipcctl_pre_down(struct ipc_ids *ids, int id, int cmd,
117+
struct ipc64_perm *perm, int extra_perm);
116118

117119
#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
118120
/* On IA-64, we always use the "64-bit version" of the IPC structures. */

0 commit comments

Comments
 (0)