Back to home page

LXR

 
 

    


0001 /*
0002  * linux/ipc/util.c
0003  * Copyright (C) 1992 Krishna Balasubramanian
0004  *
0005  * Sep 1997 - Call suser() last after "normal" permission checks so we
0006  *            get BSD style process accounting right.
0007  *            Occurs in several places in the IPC code.
0008  *            Chris Evans, <chris@ferret.lmh.ox.ac.uk>
0009  * Nov 1999 - ipc helper functions, unified SMP locking
0010  *        Manfred Spraul <manfred@colorfullife.com>
0011  * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
0012  *            Mingming Cao <cmm@us.ibm.com>
0013  * Mar 2006 - support for audit of ipc object properties
0014  *            Dustin Kirkland <dustin.kirkland@us.ibm.com>
0015  * Jun 2006 - namespaces ssupport
0016  *            OpenVZ, SWsoft Inc.
0017  *            Pavel Emelianov <xemul@openvz.org>
0018  *
0019  * General sysv ipc locking scheme:
0020  *  rcu_read_lock()
0021  *          obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
0022  *      tree.
0023  *      - perform initial checks (capabilities, auditing and permission,
0024  *        etc).
0025  *      - perform read-only operations, such as STAT, INFO commands.
0026  *        acquire the ipc lock (kern_ipc_perm.lock) through
0027  *        ipc_lock_object()
0028  *      - perform data updates, such as SET, RMID commands and
0029  *        mechanism-specific operations (semop/semtimedop,
0030  *        msgsnd/msgrcv, shmat/shmdt).
0031  *      drop the ipc lock, through ipc_unlock_object().
0032  *  rcu_read_unlock()
0033  *
0034  *  The ids->rwsem must be taken when:
0035  *  - creating, removing and iterating the existing entries in ipc
0036  *    identifier sets.
0037  *  - iterating through files under /proc/sysvipc/
0038  *
0039  *  Note that sems have a special fast path that avoids kern_ipc_perm.lock -
0040  *  see sem_lock().
0041  */
0042 
0043 #include <linux/mm.h>
0044 #include <linux/shm.h>
0045 #include <linux/init.h>
0046 #include <linux/msg.h>
0047 #include <linux/vmalloc.h>
0048 #include <linux/slab.h>
0049 #include <linux/notifier.h>
0050 #include <linux/capability.h>
0051 #include <linux/highuid.h>
0052 #include <linux/security.h>
0053 #include <linux/rcupdate.h>
0054 #include <linux/workqueue.h>
0055 #include <linux/seq_file.h>
0056 #include <linux/proc_fs.h>
0057 #include <linux/audit.h>
0058 #include <linux/nsproxy.h>
0059 #include <linux/rwsem.h>
0060 #include <linux/memory.h>
0061 #include <linux/ipc_namespace.h>
0062 
0063 #include <asm/unistd.h>
0064 
0065 #include "util.h"
0066 
0067 struct ipc_proc_iface {
0068     const char *path;
0069     const char *header;
0070     int ids;
0071     int (*show)(struct seq_file *, void *);
0072 };
0073 
0074 /**
0075  * ipc_init - initialise ipc subsystem
0076  *
0077  * The various sysv ipc resources (semaphores, messages and shared
0078  * memory) are initialised.
0079  *
0080  * A callback routine is registered into the memory hotplug notifier
0081  * chain: since msgmni scales to lowmem this callback routine will be
0082  * called upon successful memory add / remove to recompute msmgni.
0083  */
0084 static int __init ipc_init(void)
0085 {
0086     sem_init();
0087     msg_init();
0088     shm_init();
0089     return 0;
0090 }
0091 device_initcall(ipc_init);
0092 
0093 /**
0094  * ipc_init_ids - initialise ipc identifiers
0095  * @ids: ipc identifier set
0096  *
0097  * Set up the sequence range to use for the ipc identifier range (limited
0098  * below IPCMNI) then initialise the ids idr.
0099  */
0100 void ipc_init_ids(struct ipc_ids *ids)
0101 {
0102     ids->in_use = 0;
0103     ids->seq = 0;
0104     ids->next_id = -1;
0105     init_rwsem(&ids->rwsem);
0106     idr_init(&ids->ipcs_idr);
0107 }
0108 
0109 #ifdef CONFIG_PROC_FS
0110 static const struct file_operations sysvipc_proc_fops;
0111 /**
0112  * ipc_init_proc_interface -  create a proc interface for sysipc types using a seq_file interface.
0113  * @path: Path in procfs
0114  * @header: Banner to be printed at the beginning of the file.
0115  * @ids: ipc id table to iterate.
0116  * @show: show routine.
0117  */
0118 void __init ipc_init_proc_interface(const char *path, const char *header,
0119         int ids, int (*show)(struct seq_file *, void *))
0120 {
0121     struct proc_dir_entry *pde;
0122     struct ipc_proc_iface *iface;
0123 
0124     iface = kmalloc(sizeof(*iface), GFP_KERNEL);
0125     if (!iface)
0126         return;
0127     iface->path = path;
0128     iface->header   = header;
0129     iface->ids  = ids;
0130     iface->show = show;
0131 
0132     pde = proc_create_data(path,
0133                    S_IRUGO,        /* world readable */
0134                    NULL,           /* parent dir */
0135                    &sysvipc_proc_fops,
0136                    iface);
0137     if (!pde)
0138         kfree(iface);
0139 }
0140 #endif
0141 
0142 /**
0143  * ipc_findkey  - find a key in an ipc identifier set
0144  * @ids: ipc identifier set
0145  * @key: key to find
0146  *
0147  * Returns the locked pointer to the ipc structure if found or NULL
0148  * otherwise. If key is found ipc points to the owning ipc structure
0149  *
0150  * Called with ipc_ids.rwsem held.
0151  */
0152 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
0153 {
0154     struct kern_ipc_perm *ipc;
0155     int next_id;
0156     int total;
0157 
0158     for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
0159         ipc = idr_find(&ids->ipcs_idr, next_id);
0160 
0161         if (ipc == NULL)
0162             continue;
0163 
0164         if (ipc->key != key) {
0165             total++;
0166             continue;
0167         }
0168 
0169         rcu_read_lock();
0170         ipc_lock_object(ipc);
0171         return ipc;
0172     }
0173 
0174     return NULL;
0175 }
0176 
0177 /**
0178  * ipc_get_maxid - get the last assigned id
0179  * @ids: ipc identifier set
0180  *
0181  * Called with ipc_ids.rwsem held.
0182  */
0183 int ipc_get_maxid(struct ipc_ids *ids)
0184 {
0185     struct kern_ipc_perm *ipc;
0186     int max_id = -1;
0187     int total, id;
0188 
0189     if (ids->in_use == 0)
0190         return -1;
0191 
0192     if (ids->in_use == IPCMNI)
0193         return IPCMNI - 1;
0194 
0195     /* Look for the last assigned id */
0196     total = 0;
0197     for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
0198         ipc = idr_find(&ids->ipcs_idr, id);
0199         if (ipc != NULL) {
0200             max_id = id;
0201             total++;
0202         }
0203     }
0204     return max_id;
0205 }
0206 
0207 /**
0208  * ipc_addid - add an ipc identifier
0209  * @ids: ipc identifier set
0210  * @new: new ipc permission set
0211  * @size: limit for the number of used ids
0212  *
0213  * Add an entry 'new' to the ipc ids idr. The permissions object is
0214  * initialised and the first free entry is set up and the id assigned
0215  * is returned. The 'new' entry is returned in a locked state on success.
0216  * On failure the entry is not locked and a negative err-code is returned.
0217  *
0218  * Called with writer ipc_ids.rwsem held.
0219  */
0220 int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
0221 {
0222     kuid_t euid;
0223     kgid_t egid;
0224     int id;
0225     int next_id = ids->next_id;
0226 
0227     if (size > IPCMNI)
0228         size = IPCMNI;
0229 
0230     if (ids->in_use >= size)
0231         return -ENOSPC;
0232 
0233     idr_preload(GFP_KERNEL);
0234 
0235     spin_lock_init(&new->lock);
0236     new->deleted = false;
0237     rcu_read_lock();
0238     spin_lock(&new->lock);
0239 
0240     current_euid_egid(&euid, &egid);
0241     new->cuid = new->uid = euid;
0242     new->gid = new->cgid = egid;
0243 
0244     id = idr_alloc(&ids->ipcs_idr, new,
0245                (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
0246                GFP_NOWAIT);
0247     idr_preload_end();
0248     if (id < 0) {
0249         spin_unlock(&new->lock);
0250         rcu_read_unlock();
0251         return id;
0252     }
0253 
0254     ids->in_use++;
0255 
0256     if (next_id < 0) {
0257         new->seq = ids->seq++;
0258         if (ids->seq > IPCID_SEQ_MAX)
0259             ids->seq = 0;
0260     } else {
0261         new->seq = ipcid_to_seqx(next_id);
0262         ids->next_id = -1;
0263     }
0264 
0265     new->id = ipc_buildid(id, new->seq);
0266     return id;
0267 }
0268 
0269 /**
0270  * ipcget_new - create a new ipc object
0271  * @ns: ipc namespace
0272  * @ids: ipc identifier set
0273  * @ops: the actual creation routine to call
0274  * @params: its parameters
0275  *
0276  * This routine is called by sys_msgget, sys_semget() and sys_shmget()
0277  * when the key is IPC_PRIVATE.
0278  */
0279 static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
0280         const struct ipc_ops *ops, struct ipc_params *params)
0281 {
0282     int err;
0283 
0284     down_write(&ids->rwsem);
0285     err = ops->getnew(ns, params);
0286     up_write(&ids->rwsem);
0287     return err;
0288 }
0289 
0290 /**
0291  * ipc_check_perms - check security and permissions for an ipc object
0292  * @ns: ipc namespace
0293  * @ipcp: ipc permission set
0294  * @ops: the actual security routine to call
0295  * @params: its parameters
0296  *
0297  * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
0298  * when the key is not IPC_PRIVATE and that key already exists in the
0299  * ds IDR.
0300  *
0301  * On success, the ipc id is returned.
0302  *
0303  * It is called with ipc_ids.rwsem and ipcp->lock held.
0304  */
0305 static int ipc_check_perms(struct ipc_namespace *ns,
0306                struct kern_ipc_perm *ipcp,
0307                const struct ipc_ops *ops,
0308                struct ipc_params *params)
0309 {
0310     int err;
0311 
0312     if (ipcperms(ns, ipcp, params->flg))
0313         err = -EACCES;
0314     else {
0315         err = ops->associate(ipcp, params->flg);
0316         if (!err)
0317             err = ipcp->id;
0318     }
0319 
0320     return err;
0321 }
0322 
0323 /**
0324  * ipcget_public - get an ipc object or create a new one
0325  * @ns: ipc namespace
0326  * @ids: ipc identifier set
0327  * @ops: the actual creation routine to call
0328  * @params: its parameters
0329  *
0330  * This routine is called by sys_msgget, sys_semget() and sys_shmget()
0331  * when the key is not IPC_PRIVATE.
0332  * It adds a new entry if the key is not found and does some permission
0333  * / security checkings if the key is found.
0334  *
0335  * On success, the ipc id is returned.
0336  */
0337 static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
0338         const struct ipc_ops *ops, struct ipc_params *params)
0339 {
0340     struct kern_ipc_perm *ipcp;
0341     int flg = params->flg;
0342     int err;
0343 
0344     /*
0345      * Take the lock as a writer since we are potentially going to add
0346      * a new entry + read locks are not "upgradable"
0347      */
0348     down_write(&ids->rwsem);
0349     ipcp = ipc_findkey(ids, params->key);
0350     if (ipcp == NULL) {
0351         /* key not used */
0352         if (!(flg & IPC_CREAT))
0353             err = -ENOENT;
0354         else
0355             err = ops->getnew(ns, params);
0356     } else {
0357         /* ipc object has been locked by ipc_findkey() */
0358 
0359         if (flg & IPC_CREAT && flg & IPC_EXCL)
0360             err = -EEXIST;
0361         else {
0362             err = 0;
0363             if (ops->more_checks)
0364                 err = ops->more_checks(ipcp, params);
0365             if (!err)
0366                 /*
0367                  * ipc_check_perms returns the IPC id on
0368                  * success
0369                  */
0370                 err = ipc_check_perms(ns, ipcp, ops, params);
0371         }
0372         ipc_unlock(ipcp);
0373     }
0374     up_write(&ids->rwsem);
0375 
0376     return err;
0377 }
0378 
0379 
0380 /**
0381  * ipc_rmid - remove an ipc identifier
0382  * @ids: ipc identifier set
0383  * @ipcp: ipc perm structure containing the identifier to remove
0384  *
0385  * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
0386  * before this function is called, and remain locked on the exit.
0387  */
0388 void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
0389 {
0390     int lid = ipcid_to_idx(ipcp->id);
0391 
0392     idr_remove(&ids->ipcs_idr, lid);
0393     ids->in_use--;
0394     ipcp->deleted = true;
0395 }
0396 
0397 /**
0398  * ipc_alloc -  allocate ipc space
0399  * @size: size desired
0400  *
0401  * Allocate memory from the appropriate pools and return a pointer to it.
0402  * NULL is returned if the allocation fails
0403  */
0404 void *ipc_alloc(int size)
0405 {
0406     void *out;
0407     if (size > PAGE_SIZE)
0408         out = vmalloc(size);
0409     else
0410         out = kmalloc(size, GFP_KERNEL);
0411     return out;
0412 }
0413 
0414 /**
0415  * ipc_free - free ipc space
0416  * @ptr: pointer returned by ipc_alloc
0417  *
0418  * Free a block created with ipc_alloc().
0419  */
0420 void ipc_free(void *ptr)
0421 {
0422     kvfree(ptr);
0423 }
0424 
0425 /**
0426  * ipc_rcu_alloc - allocate ipc and rcu space
0427  * @size: size desired
0428  *
0429  * Allocate memory for the rcu header structure +  the object.
0430  * Returns the pointer to the object or NULL upon failure.
0431  */
0432 void *ipc_rcu_alloc(int size)
0433 {
0434     /*
0435      * We prepend the allocation with the rcu struct
0436      */
0437     struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size);
0438     if (unlikely(!out))
0439         return NULL;
0440     atomic_set(&out->refcount, 1);
0441     return out + 1;
0442 }
0443 
0444 int ipc_rcu_getref(void *ptr)
0445 {
0446     struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
0447 
0448     return atomic_inc_not_zero(&p->refcount);
0449 }
0450 
0451 void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head))
0452 {
0453     struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
0454 
0455     if (!atomic_dec_and_test(&p->refcount))
0456         return;
0457 
0458     call_rcu(&p->rcu, func);
0459 }
0460 
0461 void ipc_rcu_free(struct rcu_head *head)
0462 {
0463     struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
0464 
0465     kvfree(p);
0466 }
0467 
0468 /**
0469  * ipcperms - check ipc permissions
0470  * @ns: ipc namespace
0471  * @ipcp: ipc permission set
0472  * @flag: desired permission set
0473  *
0474  * Check user, group, other permissions for access
0475  * to ipc resources. return 0 if allowed
0476  *
0477  * @flag will most probably be 0 or S_...UGO from <linux/stat.h>
0478  */
0479 int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
0480 {
0481     kuid_t euid = current_euid();
0482     int requested_mode, granted_mode;
0483 
0484     audit_ipc_obj(ipcp);
0485     requested_mode = (flag >> 6) | (flag >> 3) | flag;
0486     granted_mode = ipcp->mode;
0487     if (uid_eq(euid, ipcp->cuid) ||
0488         uid_eq(euid, ipcp->uid))
0489         granted_mode >>= 6;
0490     else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
0491         granted_mode >>= 3;
0492     /* is there some bit set in requested_mode but not in granted_mode? */
0493     if ((requested_mode & ~granted_mode & 0007) &&
0494         !ns_capable(ns->user_ns, CAP_IPC_OWNER))
0495         return -1;
0496 
0497     return security_ipc_permission(ipcp, flag);
0498 }
0499 
0500 /*
0501  * Functions to convert between the kern_ipc_perm structure and the
0502  * old/new ipc_perm structures
0503  */
0504 
0505 /**
0506  * kernel_to_ipc64_perm - convert kernel ipc permissions to user
0507  * @in: kernel permissions
0508  * @out: new style ipc permissions
0509  *
0510  * Turn the kernel object @in into a set of permissions descriptions
0511  * for returning to userspace (@out).
0512  */
0513 void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
0514 {
0515     out->key    = in->key;
0516     out->uid    = from_kuid_munged(current_user_ns(), in->uid);
0517     out->gid    = from_kgid_munged(current_user_ns(), in->gid);
0518     out->cuid   = from_kuid_munged(current_user_ns(), in->cuid);
0519     out->cgid   = from_kgid_munged(current_user_ns(), in->cgid);
0520     out->mode   = in->mode;
0521     out->seq    = in->seq;
0522 }
0523 
0524 /**
0525  * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
0526  * @in: new style ipc permissions
0527  * @out: old style ipc permissions
0528  *
0529  * Turn the new style permissions object @in into a compatibility
0530  * object and store it into the @out pointer.
0531  */
0532 void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
0533 {
0534     out->key    = in->key;
0535     SET_UID(out->uid, in->uid);
0536     SET_GID(out->gid, in->gid);
0537     SET_UID(out->cuid, in->cuid);
0538     SET_GID(out->cgid, in->cgid);
0539     out->mode   = in->mode;
0540     out->seq    = in->seq;
0541 }
0542 
0543 /**
0544  * ipc_obtain_object
0545  * @ids: ipc identifier set
0546  * @id: ipc id to look for
0547  *
0548  * Look for an id in the ipc ids idr and return associated ipc object.
0549  *
0550  * Call inside the RCU critical section.
0551  * The ipc object is *not* locked on exit.
0552  */
0553 struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
0554 {
0555     struct kern_ipc_perm *out;
0556     int lid = ipcid_to_idx(id);
0557 
0558     out = idr_find(&ids->ipcs_idr, lid);
0559     if (!out)
0560         return ERR_PTR(-EINVAL);
0561 
0562     return out;
0563 }
0564 
0565 /**
0566  * ipc_lock - lock an ipc structure without rwsem held
0567  * @ids: ipc identifier set
0568  * @id: ipc id to look for
0569  *
0570  * Look for an id in the ipc ids idr and lock the associated ipc object.
0571  *
0572  * The ipc object is locked on successful exit.
0573  */
0574 struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
0575 {
0576     struct kern_ipc_perm *out;
0577 
0578     rcu_read_lock();
0579     out = ipc_obtain_object_idr(ids, id);
0580     if (IS_ERR(out))
0581         goto err;
0582 
0583     spin_lock(&out->lock);
0584 
0585     /*
0586      * ipc_rmid() may have already freed the ID while ipc_lock()
0587      * was spinning: here verify that the structure is still valid.
0588      * Upon races with RMID, return -EIDRM, thus indicating that
0589      * the ID points to a removed identifier.
0590      */
0591     if (ipc_valid_object(out))
0592         return out;
0593 
0594     spin_unlock(&out->lock);
0595     out = ERR_PTR(-EIDRM);
0596 err:
0597     rcu_read_unlock();
0598     return out;
0599 }
0600 
0601 /**
0602  * ipc_obtain_object_check
0603  * @ids: ipc identifier set
0604  * @id: ipc id to look for
0605  *
0606  * Similar to ipc_obtain_object_idr() but also checks
0607  * the ipc object reference counter.
0608  *
0609  * Call inside the RCU critical section.
0610  * The ipc object is *not* locked on exit.
0611  */
0612 struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
0613 {
0614     struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
0615 
0616     if (IS_ERR(out))
0617         goto out;
0618 
0619     if (ipc_checkid(out, id))
0620         return ERR_PTR(-EINVAL);
0621 out:
0622     return out;
0623 }
0624 
0625 /**
0626  * ipcget - Common sys_*get() code
0627  * @ns: namespace
0628  * @ids: ipc identifier set
0629  * @ops: operations to be called on ipc object creation, permission checks
0630  *       and further checks
0631  * @params: the parameters needed by the previous operations.
0632  *
0633  * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
0634  */
0635 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
0636             const struct ipc_ops *ops, struct ipc_params *params)
0637 {
0638     if (params->key == IPC_PRIVATE)
0639         return ipcget_new(ns, ids, ops, params);
0640     else
0641         return ipcget_public(ns, ids, ops, params);
0642 }
0643 
0644 /**
0645  * ipc_update_perm - update the permissions of an ipc object
0646  * @in:  the permission given as input.
0647  * @out: the permission of the ipc to set.
0648  */
0649 int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
0650 {
0651     kuid_t uid = make_kuid(current_user_ns(), in->uid);
0652     kgid_t gid = make_kgid(current_user_ns(), in->gid);
0653     if (!uid_valid(uid) || !gid_valid(gid))
0654         return -EINVAL;
0655 
0656     out->uid = uid;
0657     out->gid = gid;
0658     out->mode = (out->mode & ~S_IRWXUGO)
0659         | (in->mode & S_IRWXUGO);
0660 
0661     return 0;
0662 }
0663 
0664 /**
0665  * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
0666  * @ns:  ipc namespace
0667  * @ids:  the table of ids where to look for the ipc
0668  * @id:   the id of the ipc to retrieve
0669  * @cmd:  the cmd to check
0670  * @perm: the permission to set
0671  * @extra_perm: one extra permission parameter used by msq
0672  *
0673  * This function does some common audit and permissions check for some IPC_XXX
0674  * cmd and is called from semctl_down, shmctl_down and msgctl_down.
0675  * It must be called without any lock held and
0676  *  - retrieves the ipc with the given id in the given table.
0677  *  - performs some audit and permission check, depending on the given cmd
0678  *  - returns a pointer to the ipc object or otherwise, the corresponding error.
0679  *
0680  * Call holding the both the rwsem and the rcu read lock.
0681  */
0682 struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
0683                     struct ipc_ids *ids, int id, int cmd,
0684                     struct ipc64_perm *perm, int extra_perm)
0685 {
0686     kuid_t euid;
0687     int err = -EPERM;
0688     struct kern_ipc_perm *ipcp;
0689 
0690     ipcp = ipc_obtain_object_check(ids, id);
0691     if (IS_ERR(ipcp)) {
0692         err = PTR_ERR(ipcp);
0693         goto err;
0694     }
0695 
0696     audit_ipc_obj(ipcp);
0697     if (cmd == IPC_SET)
0698         audit_ipc_set_perm(extra_perm, perm->uid,
0699                    perm->gid, perm->mode);
0700 
0701     euid = current_euid();
0702     if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid)  ||
0703         ns_capable(ns->user_ns, CAP_SYS_ADMIN))
0704         return ipcp; /* successful lookup */
0705 err:
0706     return ERR_PTR(err);
0707 }
0708 
0709 #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
0710 
0711 
0712 /**
0713  * ipc_parse_version - ipc call version
0714  * @cmd: pointer to command
0715  *
0716  * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
0717  * The @cmd value is turned from an encoding command and version into
0718  * just the command code.
0719  */
0720 int ipc_parse_version(int *cmd)
0721 {
0722     if (*cmd & IPC_64) {
0723         *cmd ^= IPC_64;
0724         return IPC_64;
0725     } else {
0726         return IPC_OLD;
0727     }
0728 }
0729 
0730 #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
0731 
0732 #ifdef CONFIG_PROC_FS
0733 struct ipc_proc_iter {
0734     struct ipc_namespace *ns;
0735     struct ipc_proc_iface *iface;
0736 };
0737 
0738 /*
0739  * This routine locks the ipc structure found at least at position pos.
0740  */
0741 static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
0742                           loff_t *new_pos)
0743 {
0744     struct kern_ipc_perm *ipc;
0745     int total, id;
0746 
0747     total = 0;
0748     for (id = 0; id < pos && total < ids->in_use; id++) {
0749         ipc = idr_find(&ids->ipcs_idr, id);
0750         if (ipc != NULL)
0751             total++;
0752     }
0753 
0754     if (total >= ids->in_use)
0755         return NULL;
0756 
0757     for (; pos < IPCMNI; pos++) {
0758         ipc = idr_find(&ids->ipcs_idr, pos);
0759         if (ipc != NULL) {
0760             *new_pos = pos + 1;
0761             rcu_read_lock();
0762             ipc_lock_object(ipc);
0763             return ipc;
0764         }
0765     }
0766 
0767     /* Out of range - return NULL to terminate iteration */
0768     return NULL;
0769 }
0770 
0771 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
0772 {
0773     struct ipc_proc_iter *iter = s->private;
0774     struct ipc_proc_iface *iface = iter->iface;
0775     struct kern_ipc_perm *ipc = it;
0776 
0777     /* If we had an ipc id locked before, unlock it */
0778     if (ipc && ipc != SEQ_START_TOKEN)
0779         ipc_unlock(ipc);
0780 
0781     return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
0782 }
0783 
0784 /*
0785  * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
0786  * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
0787  */
0788 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
0789 {
0790     struct ipc_proc_iter *iter = s->private;
0791     struct ipc_proc_iface *iface = iter->iface;
0792     struct ipc_ids *ids;
0793 
0794     ids = &iter->ns->ids[iface->ids];
0795 
0796     /*
0797      * Take the lock - this will be released by the corresponding
0798      * call to stop().
0799      */
0800     down_read(&ids->rwsem);
0801 
0802     /* pos < 0 is invalid */
0803     if (*pos < 0)
0804         return NULL;
0805 
0806     /* pos == 0 means header */
0807     if (*pos == 0)
0808         return SEQ_START_TOKEN;
0809 
0810     /* Find the (pos-1)th ipc */
0811     return sysvipc_find_ipc(ids, *pos - 1, pos);
0812 }
0813 
0814 static void sysvipc_proc_stop(struct seq_file *s, void *it)
0815 {
0816     struct kern_ipc_perm *ipc = it;
0817     struct ipc_proc_iter *iter = s->private;
0818     struct ipc_proc_iface *iface = iter->iface;
0819     struct ipc_ids *ids;
0820 
0821     /* If we had a locked structure, release it */
0822     if (ipc && ipc != SEQ_START_TOKEN)
0823         ipc_unlock(ipc);
0824 
0825     ids = &iter->ns->ids[iface->ids];
0826     /* Release the lock we took in start() */
0827     up_read(&ids->rwsem);
0828 }
0829 
0830 static int sysvipc_proc_show(struct seq_file *s, void *it)
0831 {
0832     struct ipc_proc_iter *iter = s->private;
0833     struct ipc_proc_iface *iface = iter->iface;
0834 
0835     if (it == SEQ_START_TOKEN) {
0836         seq_puts(s, iface->header);
0837         return 0;
0838     }
0839 
0840     return iface->show(s, it);
0841 }
0842 
0843 static const struct seq_operations sysvipc_proc_seqops = {
0844     .start = sysvipc_proc_start,
0845     .stop  = sysvipc_proc_stop,
0846     .next  = sysvipc_proc_next,
0847     .show  = sysvipc_proc_show,
0848 };
0849 
0850 static int sysvipc_proc_open(struct inode *inode, struct file *file)
0851 {
0852     struct ipc_proc_iter *iter;
0853 
0854     iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
0855     if (!iter)
0856         return -ENOMEM;
0857 
0858     iter->iface = PDE_DATA(inode);
0859     iter->ns    = get_ipc_ns(current->nsproxy->ipc_ns);
0860 
0861     return 0;
0862 }
0863 
0864 static int sysvipc_proc_release(struct inode *inode, struct file *file)
0865 {
0866     struct seq_file *seq = file->private_data;
0867     struct ipc_proc_iter *iter = seq->private;
0868     put_ipc_ns(iter->ns);
0869     return seq_release_private(inode, file);
0870 }
0871 
0872 static const struct file_operations sysvipc_proc_fops = {
0873     .open    = sysvipc_proc_open,
0874     .read    = seq_read,
0875     .llseek  = seq_lseek,
0876     .release = sysvipc_proc_release,
0877 };
0878 #endif /* CONFIG_PROC_FS */