X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=ipc%2Futil.c;h=1aa0ebf71bac7e5c30559eb0f60a89bdc6688efb;hb=3ba5619f06300cd0944150901ed20de87483ad8c;hp=2a205875d277bcce202306124a332cbb1fa07c36;hpb=7ca7e564e049d8b350ec9d958ff25eaa24226352;p=linux-2.6 diff --git a/ipc/util.c b/ipc/util.c index 2a205875d2..1aa0ebf71b 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -136,7 +137,7 @@ __initcall(ipc_init); void ipc_init_ids(struct ipc_ids *ids) { - mutex_init(&ids->mutex); + init_rwsem(&ids->rw_mutex); ids->in_use = 0; ids->seq = 0; @@ -191,13 +192,13 @@ void __init ipc_init_proc_interface(const char *path, const char *header, * @ids: Identifier set * @key: The key to find * - * Requires ipc_ids.mutex locked. + * Requires ipc_ids.rw_mutex locked. * Returns the LOCKED pointer to the ipc structure if found or NULL * if not. - * If key is found ipc contains its ipc structure + * If key is found ipc points to the owning ipc structure */ -struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) +static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) { struct kern_ipc_perm *ipc; int next_id; @@ -225,7 +226,7 @@ struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) * ipc_get_maxid - get the last assigned id * @ids: IPC identifier set * - * Called with ipc_ids.mutex held. + * Called with ipc_ids.rw_mutex held. */ int ipc_get_maxid(struct ipc_ids *ids) @@ -258,31 +259,27 @@ int ipc_get_maxid(struct ipc_ids *ids) * @new: new IPC permission set * @size: limit for the number of used ids * - * Add an entry 'new' to the IPC 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 - * is returned. The list is returned in a locked state on success. - * On failure the list is not locked and -1 is returned. + * 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. * - * Called with ipc_ids.mutex held. + * Called with ipc_ids.rw_mutex held as a writer. */ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) { int id, err; - /* - * rcu_dereference()() is not needed here since - * ipc_ids.mutex is held - */ if (size > IPCMNI) size = IPCMNI; if (ids->in_use >= size) - return -1; + return -ENOSPC; err = idr_get_new(&ids->ipcs_idr, new, &id); if (err) - return -1; + return err; ids->in_use++; @@ -300,21 +297,142 @@ int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) return id; } +/** + * ipcget_new - create a new ipc object + * @ns: namespace + * @ids: IPC identifer set + * @ops: the actual creation routine to call + * @params: its parameters + * + * This routine is called by sys_msgget, sys_semget() and sys_shmget() + * when the key is IPC_PRIVATE. + */ +int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids, + struct ipc_ops *ops, struct ipc_params *params) +{ + int err; +retry: + err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL); + + if (!err) + return -ENOMEM; + + down_write(&ids->rw_mutex); + err = ops->getnew(ns, params); + up_write(&ids->rw_mutex); + + if (err == -EAGAIN) + goto retry; + + return err; +} + +/** + * ipc_check_perms - check security and permissions for an IPC + * @ipcp: ipc permission set + * @ops: the actual security routine to call + * @params: its parameters + * + * This routine is called by sys_msgget(), sys_semget() and sys_shmget() + * when the key is not IPC_PRIVATE and that key already exists in the + * ids IDR. + * + * On success, the IPC id is returned. + * + * It is called with ipc_ids.rw_mutex and ipcp->lock held. + */ +static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops, + struct ipc_params *params) +{ + int err; + + if (ipcperms(ipcp, params->flg)) + err = -EACCES; + else { + err = ops->associate(ipcp, params->flg); + if (!err) + err = ipcp->id; + } + + return err; +} + +/** + * ipcget_public - get an ipc object or create a new one + * @ns: namespace + * @ids: IPC identifer set + * @ops: the actual creation routine to call + * @params: its parameters + * + * This routine is called by sys_msgget, sys_semget() and sys_shmget() + * when the key is not IPC_PRIVATE. + * It adds a new entry if the key is not found and does some permission + * / security checkings if the key is found. + * + * On success, the ipc id is returned. + */ +int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids, + struct ipc_ops *ops, struct ipc_params *params) +{ + struct kern_ipc_perm *ipcp; + int flg = params->flg; + int err; +retry: + err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL); + + /* + * Take the lock as a writer since we are potentially going to add + * a new entry + read locks are not "upgradable" + */ + down_write(&ids->rw_mutex); + ipcp = ipc_findkey(ids, params->key); + if (ipcp == NULL) { + /* key not used */ + if (!(flg & IPC_CREAT)) + err = -ENOENT; + else if (!err) + err = -ENOMEM; + else + err = ops->getnew(ns, params); + } else { + /* ipc object has been locked by ipc_findkey() */ + + if (flg & IPC_CREAT && flg & IPC_EXCL) + err = -EEXIST; + else { + err = 0; + if (ops->more_checks) + err = ops->more_checks(ipcp, params); + if (!err) + /* + * ipc_check_perms returns the IPC id on + * success + */ + err = ipc_check_perms(ipcp, ops, params); + } + ipc_unlock(ipcp); + } + up_write(&ids->rw_mutex); + + if (err == -EAGAIN) + goto retry; + + return err; +} + + /** * ipc_rmid - remove an IPC identifier - * @ids: identifier set - * @id: ipc perm structure containing the identifier to remove + * @ids: IPC identifier set + * @ipcp: ipc perm structure containing the identifier to remove * - * The identifier must be valid, and in use. The kernel will panic if - * fed an invalid identifier. The entry is removed and internal - * variables recomputed. - * ipc_ids.mutex and the spinlock for this ID are held before this - * function is called, and remain locked on the exit. + * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held + * before this function is called, and remain locked on the exit. */ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) { - int lid = ipcp->id % SEQ_MULTIPLIER; + int lid = ipcid_to_idx(ipcp->id); idr_remove(&ids->ipcs_idr, lid); @@ -459,10 +577,12 @@ static void ipc_do_vfree(struct work_struct *work) */ static void ipc_schedule_free(struct rcu_head *head) { - struct ipc_rcu_grace *grace = - container_of(head, struct ipc_rcu_grace, rcu); - struct ipc_rcu_sched *sched = - container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]); + struct ipc_rcu_grace *grace; + struct ipc_rcu_sched *sched; + + grace = container_of(head, struct ipc_rcu_grace, rcu); + sched = container_of(&(grace->data[0]), struct ipc_rcu_sched, + data[0]); INIT_WORK(&sched->work, ipc_do_vfree); schedule_work(&sched->work); @@ -551,7 +671,7 @@ void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out) } /** - * ipc64_perm_to_ipc_perm - convert old ipc permissions to new + * ipc64_perm_to_ipc_perm - convert new ipc permissions to old * @in: new style IPC permissions * @out: old style IPC permissions * @@ -570,37 +690,37 @@ void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out) out->seq = in->seq; } -/* - * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get() - * is called with shm_ids.mutex locked. Since grow_ary() is also called with - * shm_ids.mutex down(for Shared Memory), there is no need to add read - * barriers here to gurantee the writes in grow_ary() are seen in order - * here (for Alpha). +/** + * ipc_lock - Lock an ipc structure without rw_mutex held + * @ids: IPC identifier set + * @id: ipc id to look for + * + * Look for an id in the ipc ids idr and lock the associated ipc object. + * + * The ipc object is locked on exit. * - * However ipc_get() itself does not necessary require ipc_ids.mutex down. So - * if in the future ipc_get() is used by other places without ipc_ids.mutex - * down, then ipc_get() needs read memery barriers as ipc_lock() does. + * This is the routine that should be called when the rw_mutex is not already + * held, i.e. idr tree not protected: it protects the idr tree in read mode + * during the idr_find(). */ -struct kern_ipc_perm *ipc_get(struct ipc_ids *ids, int id) -{ - struct kern_ipc_perm *out; - int lid = id % SEQ_MULTIPLIER; - out = idr_find(&ids->ipcs_idr, lid); - return out; -} struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id) { struct kern_ipc_perm *out; - int lid = id % SEQ_MULTIPLIER; + int lid = ipcid_to_idx(id); + + down_read(&ids->rw_mutex); rcu_read_lock(); out = idr_find(&ids->ipcs_idr, lid); if (out == NULL) { rcu_read_unlock(); - return NULL; + up_read(&ids->rw_mutex); + return ERR_PTR(-EINVAL); } + up_read(&ids->rw_mutex); + spin_lock(&out->lock); /* ipc_rmid() may have already freed the ID while ipc_lock @@ -609,34 +729,44 @@ struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id) if (out->deleted) { spin_unlock(&out->lock); rcu_read_unlock(); - return NULL; + return ERR_PTR(-EINVAL); } return out; } -void ipc_lock_by_ptr(struct kern_ipc_perm *perm) -{ - rcu_read_lock(); - spin_lock(&perm->lock); -} +/** + * ipc_lock_down - Lock an ipc structure with rw_sem held + * @ids: IPC identifier set + * @id: ipc id to look for + * + * Look for an id in the ipc ids idr and lock the associated ipc object. + * + * The ipc object is locked on exit. + * + * This is the routine that should be called when the rw_mutex is already + * held, i.e. idr tree protected. + */ -void ipc_unlock(struct kern_ipc_perm* perm) +struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id) { - spin_unlock(&perm->lock); - rcu_read_unlock(); -} + struct kern_ipc_perm *out; + int lid = ipcid_to_idx(id); -int ipc_buildid(struct ipc_ids* ids, int id, int seq) -{ - return SEQ_MULTIPLIER*seq + id; -} + rcu_read_lock(); + out = idr_find(&ids->ipcs_idr, lid); + if (out == NULL) { + rcu_read_unlock(); + return ERR_PTR(-EINVAL); + } -int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid) -{ - if(uid/SEQ_MULTIPLIER != ipcp->seq) - return 1; - return 0; + spin_lock(&out->lock); + + /* + * No need to verify that the structure is still valid since the + * rw_mutex is held. + */ + return out; } #ifdef __ARCH_WANT_IPC_PARSE_VERSION @@ -715,8 +845,8 @@ static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) } /* - * File positions: pos 0 -> header, pos n -> ipc id + 1. - * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START. + * File positions: pos 0 -> header, pos n -> ipc id = n - 1. + * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START. */ static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) { @@ -730,7 +860,7 @@ static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) * Take the lock - this will be released by the corresponding * call to stop(). */ - mutex_lock(&ids->mutex); + down_read(&ids->rw_mutex); /* pos < 0 is invalid */ if (*pos < 0) @@ -751,13 +881,13 @@ static void sysvipc_proc_stop(struct seq_file *s, void *it) struct ipc_proc_iface *iface = iter->iface; struct ipc_ids *ids; - /* If we had a locked segment, release it */ + /* If we had a locked structure, release it */ if (ipc && ipc != SEQ_START_TOKEN) ipc_unlock(ipc); ids = iter->ns->ids[iface->ids]; /* Release the lock we took in start() */ - mutex_unlock(&ids->mutex); + up_read(&ids->rw_mutex); } static int sysvipc_proc_show(struct seq_file *s, void *it)