Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/ipc/util.c |
| 3 | * Copyright (C) 1992 Krishna Balasubramanian |
| 4 | * |
| 5 | * Sep 1997 - Call suser() last after "normal" permission checks so we |
| 6 | * get BSD style process accounting right. |
| 7 | * Occurs in several places in the IPC code. |
| 8 | * Chris Evans, <chris@ferret.lmh.ox.ac.uk> |
| 9 | * Nov 1999 - ipc helper functions, unified SMP locking |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 10 | * Manfred Spraul <manfred@colorfullife.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary(). |
| 12 | * Mingming Cao <cmm@us.ibm.com> |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 13 | * Mar 2006 - support for audit of ipc object properties |
| 14 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 15 | * Jun 2006 - namespaces ssupport |
| 16 | * OpenVZ, SWsoft Inc. |
| 17 | * Pavel Emelianov <xemul@openvz.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/mm.h> |
| 21 | #include <linux/shm.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/msg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
| 25 | #include <linux/slab.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 26 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/highuid.h> |
| 28 | #include <linux/security.h> |
| 29 | #include <linux/rcupdate.h> |
| 30 | #include <linux/workqueue.h> |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 31 | #include <linux/seq_file.h> |
| 32 | #include <linux/proc_fs.h> |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 33 | #include <linux/audit.h> |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 34 | #include <linux/nsproxy.h> |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 35 | #include <linux/rwsem.h> |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 36 | #include <linux/ipc_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/unistd.h> |
| 39 | |
| 40 | #include "util.h" |
| 41 | |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 42 | struct ipc_proc_iface { |
| 43 | const char *path; |
| 44 | const char *header; |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 45 | int ids; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 46 | int (*show)(struct seq_file *, void *); |
| 47 | }; |
| 48 | |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 49 | struct ipc_namespace init_ipc_ns = { |
| 50 | .kref = { |
| 51 | .refcount = ATOMIC_INIT(2), |
| 52 | }, |
| 53 | }; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /** |
| 56 | * ipc_init - initialise IPC subsystem |
| 57 | * |
| 58 | * The various system5 IPC resources (semaphores, messages and shared |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 59 | * memory) are initialised |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | static int __init ipc_init(void) |
| 63 | { |
| 64 | sem_init(); |
| 65 | msg_init(); |
| 66 | shm_init(); |
| 67 | return 0; |
| 68 | } |
| 69 | __initcall(ipc_init); |
| 70 | |
| 71 | /** |
| 72 | * ipc_init_ids - initialise IPC identifiers |
| 73 | * @ids: Identifier set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 75 | * Set up the sequence range to use for the ipc identifier range (limited |
| 76 | * below IPCMNI) then initialise the ids idr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | */ |
| 78 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 79 | void ipc_init_ids(struct ipc_ids *ids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 81 | init_rwsem(&ids->rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | ids->in_use = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | ids->seq = 0; |
| 85 | { |
| 86 | int seq_limit = INT_MAX/SEQ_MULTIPLIER; |
| 87 | if(seq_limit > USHRT_MAX) |
| 88 | ids->seq_max = USHRT_MAX; |
| 89 | else |
| 90 | ids->seq_max = seq_limit; |
| 91 | } |
| 92 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 93 | idr_init(&ids->ipcs_idr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 96 | #ifdef CONFIG_PROC_FS |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 97 | static const struct file_operations sysvipc_proc_fops; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 98 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 99 | * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface. |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 100 | * @path: Path in procfs |
| 101 | * @header: Banner to be printed at the beginning of the file. |
| 102 | * @ids: ipc id table to iterate. |
| 103 | * @show: show routine. |
| 104 | */ |
| 105 | void __init ipc_init_proc_interface(const char *path, const char *header, |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 106 | int ids, int (*show)(struct seq_file *, void *)) |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 107 | { |
| 108 | struct proc_dir_entry *pde; |
| 109 | struct ipc_proc_iface *iface; |
| 110 | |
| 111 | iface = kmalloc(sizeof(*iface), GFP_KERNEL); |
| 112 | if (!iface) |
| 113 | return; |
| 114 | iface->path = path; |
| 115 | iface->header = header; |
| 116 | iface->ids = ids; |
| 117 | iface->show = show; |
| 118 | |
| 119 | pde = create_proc_entry(path, |
| 120 | S_IRUGO, /* world readable */ |
| 121 | NULL /* parent dir */); |
| 122 | if (pde) { |
| 123 | pde->data = iface; |
| 124 | pde->proc_fops = &sysvipc_proc_fops; |
| 125 | } else { |
| 126 | kfree(iface); |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /** |
| 132 | * ipc_findkey - find a key in an ipc identifier set |
| 133 | * @ids: Identifier set |
| 134 | * @key: The key to find |
| 135 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 136 | * Requires ipc_ids.rw_mutex locked. |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 137 | * Returns the LOCKED pointer to the ipc structure if found or NULL |
| 138 | * if not. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 139 | * If key is found ipc points to the owning ipc structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | */ |
| 141 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 142 | static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 144 | struct kern_ipc_perm *ipc; |
| 145 | int next_id; |
| 146 | int total; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 148 | for (total = 0, next_id = 0; total < ids->in_use; next_id++) { |
| 149 | ipc = idr_find(&ids->ipcs_idr, next_id); |
| 150 | |
| 151 | if (ipc == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | continue; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 153 | |
| 154 | if (ipc->key != key) { |
| 155 | total++; |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | ipc_lock_by_ptr(ipc); |
| 160 | return ipc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 162 | |
| 163 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 166 | /** |
| 167 | * ipc_get_maxid - get the last assigned id |
| 168 | * @ids: IPC identifier set |
| 169 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 170 | * Called with ipc_ids.rw_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 172 | |
| 173 | int ipc_get_maxid(struct ipc_ids *ids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 175 | struct kern_ipc_perm *ipc; |
| 176 | int max_id = -1; |
| 177 | int total, id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 179 | if (ids->in_use == 0) |
| 180 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 182 | if (ids->in_use == IPCMNI) |
| 183 | return IPCMNI - 1; |
| 184 | |
| 185 | /* Look for the last assigned id */ |
| 186 | total = 0; |
| 187 | for (id = 0; id < IPCMNI && total < ids->in_use; id++) { |
| 188 | ipc = idr_find(&ids->ipcs_idr, id); |
| 189 | if (ipc != NULL) { |
| 190 | max_id = id; |
| 191 | total++; |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 194 | return max_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ipc_addid - add an IPC identifier |
| 199 | * @ids: IPC identifier set |
| 200 | * @new: new IPC permission set |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 201 | * @size: limit for the number of used ids |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | * |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 203 | * Add an entry 'new' to the IPC ids idr. The permissions object is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * initialised and the first free entry is set up and the id assigned |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 205 | * is returned. The 'new' entry is returned in a locked state on success. |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 206 | * On failure the entry is not locked and a negative err-code is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 208 | * Called with ipc_ids.rw_mutex held as a writer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | */ |
| 210 | |
| 211 | int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) |
| 212 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 213 | int id, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 215 | if (size > IPCMNI) |
| 216 | size = IPCMNI; |
| 217 | |
| 218 | if (ids->in_use >= size) |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 219 | return -ENOSPC; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 220 | |
| 221 | err = idr_get_new(&ids->ipcs_idr, new, &id); |
| 222 | if (err) |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 223 | return err; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | ids->in_use++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | new->cuid = new->uid = current->euid; |
| 228 | new->gid = new->cgid = current->egid; |
| 229 | |
| 230 | new->seq = ids->seq++; |
| 231 | if(ids->seq > ids->seq_max) |
| 232 | ids->seq = 0; |
| 233 | |
Pierre Peiffer | 48dea40 | 2008-04-29 01:00:35 -0700 | [diff] [blame^] | 234 | new->id = ipc_buildid(id, new->seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | spin_lock_init(&new->lock); |
| 236 | new->deleted = 0; |
| 237 | rcu_read_lock(); |
| 238 | spin_lock(&new->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return id; |
| 240 | } |
| 241 | |
| 242 | /** |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 243 | * ipcget_new - create a new ipc object |
| 244 | * @ns: namespace |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 245 | * @ids: IPC identifer set |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 246 | * @ops: the actual creation routine to call |
| 247 | * @params: its parameters |
| 248 | * |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 249 | * This routine is called by sys_msgget, sys_semget() and sys_shmget() |
| 250 | * when the key is IPC_PRIVATE. |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 251 | */ |
Pavel Emelyanov | b2d75cd | 2008-02-08 04:18:54 -0800 | [diff] [blame] | 252 | static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids, |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 253 | struct ipc_ops *ops, struct ipc_params *params) |
| 254 | { |
| 255 | int err; |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 256 | retry: |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 257 | err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL); |
| 258 | |
| 259 | if (!err) |
| 260 | return -ENOMEM; |
| 261 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 262 | down_write(&ids->rw_mutex); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 263 | err = ops->getnew(ns, params); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 264 | up_write(&ids->rw_mutex); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 265 | |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 266 | if (err == -EAGAIN) |
| 267 | goto retry; |
| 268 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 269 | return err; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * ipc_check_perms - check security and permissions for an IPC |
| 274 | * @ipcp: ipc permission set |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 275 | * @ops: the actual security routine to call |
| 276 | * @params: its parameters |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 277 | * |
| 278 | * This routine is called by sys_msgget(), sys_semget() and sys_shmget() |
| 279 | * when the key is not IPC_PRIVATE and that key already exists in the |
| 280 | * ids IDR. |
| 281 | * |
| 282 | * On success, the IPC id is returned. |
| 283 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 284 | * It is called with ipc_ids.rw_mutex and ipcp->lock held. |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 285 | */ |
| 286 | static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops, |
| 287 | struct ipc_params *params) |
| 288 | { |
| 289 | int err; |
| 290 | |
| 291 | if (ipcperms(ipcp, params->flg)) |
| 292 | err = -EACCES; |
| 293 | else { |
| 294 | err = ops->associate(ipcp, params->flg); |
| 295 | if (!err) |
| 296 | err = ipcp->id; |
| 297 | } |
| 298 | |
| 299 | return err; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * ipcget_public - get an ipc object or create a new one |
| 304 | * @ns: namespace |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 305 | * @ids: IPC identifer set |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 306 | * @ops: the actual creation routine to call |
| 307 | * @params: its parameters |
| 308 | * |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 309 | * This routine is called by sys_msgget, sys_semget() and sys_shmget() |
| 310 | * when the key is not IPC_PRIVATE. |
| 311 | * It adds a new entry if the key is not found and does some permission |
| 312 | * / security checkings if the key is found. |
| 313 | * |
| 314 | * On success, the ipc id is returned. |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 315 | */ |
Pavel Emelyanov | b2d75cd | 2008-02-08 04:18:54 -0800 | [diff] [blame] | 316 | static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids, |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 317 | struct ipc_ops *ops, struct ipc_params *params) |
| 318 | { |
| 319 | struct kern_ipc_perm *ipcp; |
| 320 | int flg = params->flg; |
| 321 | int err; |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 322 | retry: |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 323 | err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL); |
| 324 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Take the lock as a writer since we are potentially going to add |
| 327 | * a new entry + read locks are not "upgradable" |
| 328 | */ |
| 329 | down_write(&ids->rw_mutex); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 330 | ipcp = ipc_findkey(ids, params->key); |
| 331 | if (ipcp == NULL) { |
| 332 | /* key not used */ |
| 333 | if (!(flg & IPC_CREAT)) |
| 334 | err = -ENOENT; |
| 335 | else if (!err) |
| 336 | err = -ENOMEM; |
| 337 | else |
| 338 | err = ops->getnew(ns, params); |
| 339 | } else { |
| 340 | /* ipc object has been locked by ipc_findkey() */ |
| 341 | |
| 342 | if (flg & IPC_CREAT && flg & IPC_EXCL) |
| 343 | err = -EEXIST; |
| 344 | else { |
| 345 | err = 0; |
| 346 | if (ops->more_checks) |
| 347 | err = ops->more_checks(ipcp, params); |
| 348 | if (!err) |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 349 | /* |
| 350 | * ipc_check_perms returns the IPC id on |
| 351 | * success |
| 352 | */ |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 353 | err = ipc_check_perms(ipcp, ops, params); |
| 354 | } |
| 355 | ipc_unlock(ipcp); |
| 356 | } |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 357 | up_write(&ids->rw_mutex); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 358 | |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 359 | if (err == -EAGAIN) |
| 360 | goto retry; |
| 361 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 362 | return err; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | * ipc_rmid - remove an IPC identifier |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 368 | * @ids: IPC identifier set |
| 369 | * @ipcp: ipc perm structure containing the identifier to remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 371 | * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held |
| 372 | * before this function is called, and remain locked on the exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | */ |
| 374 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 375 | void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Nadia Derbey | ce621f5 | 2007-10-18 23:40:52 -0700 | [diff] [blame] | 377 | int lid = ipcid_to_idx(ipcp->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 379 | idr_remove(&ids->ipcs_idr, lid); |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | ids->in_use--; |
| 382 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 383 | ipcp->deleted = 1; |
| 384 | |
| 385 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** |
| 389 | * ipc_alloc - allocate ipc space |
| 390 | * @size: size desired |
| 391 | * |
| 392 | * Allocate memory from the appropriate pools and return a pointer to it. |
| 393 | * NULL is returned if the allocation fails |
| 394 | */ |
| 395 | |
| 396 | void* ipc_alloc(int size) |
| 397 | { |
| 398 | void* out; |
| 399 | if(size > PAGE_SIZE) |
| 400 | out = vmalloc(size); |
| 401 | else |
| 402 | out = kmalloc(size, GFP_KERNEL); |
| 403 | return out; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * ipc_free - free ipc space |
| 408 | * @ptr: pointer returned by ipc_alloc |
| 409 | * @size: size of block |
| 410 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 411 | * Free a block created with ipc_alloc(). The caller must know the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | * used in the allocation call. |
| 413 | */ |
| 414 | |
| 415 | void ipc_free(void* ptr, int size) |
| 416 | { |
| 417 | if(size > PAGE_SIZE) |
| 418 | vfree(ptr); |
| 419 | else |
| 420 | kfree(ptr); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * rcu allocations: |
| 425 | * There are three headers that are prepended to the actual allocation: |
| 426 | * - during use: ipc_rcu_hdr. |
| 427 | * - during the rcu grace period: ipc_rcu_grace. |
| 428 | * - [only if vmalloc]: ipc_rcu_sched. |
| 429 | * Their lifetime doesn't overlap, thus the headers share the same memory. |
| 430 | * Unlike a normal union, they are right-aligned, thus some container_of |
| 431 | * forward/backward casting is necessary: |
| 432 | */ |
| 433 | struct ipc_rcu_hdr |
| 434 | { |
| 435 | int refcount; |
| 436 | int is_vmalloc; |
| 437 | void *data[0]; |
| 438 | }; |
| 439 | |
| 440 | |
| 441 | struct ipc_rcu_grace |
| 442 | { |
| 443 | struct rcu_head rcu; |
| 444 | /* "void *" makes sure alignment of following data is sane. */ |
| 445 | void *data[0]; |
| 446 | }; |
| 447 | |
| 448 | struct ipc_rcu_sched |
| 449 | { |
| 450 | struct work_struct work; |
| 451 | /* "void *" makes sure alignment of following data is sane. */ |
| 452 | void *data[0]; |
| 453 | }; |
| 454 | |
| 455 | #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \ |
| 456 | sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr)) |
| 457 | #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \ |
| 458 | sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC) |
| 459 | |
| 460 | static inline int rcu_use_vmalloc(int size) |
| 461 | { |
| 462 | /* Too big for a single page? */ |
| 463 | if (HDRLEN_KMALLOC + size > PAGE_SIZE) |
| 464 | return 1; |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * ipc_rcu_alloc - allocate ipc and rcu space |
| 470 | * @size: size desired |
| 471 | * |
| 472 | * Allocate memory for the rcu header structure + the object. |
| 473 | * Returns the pointer to the object. |
| 474 | * NULL is returned if the allocation fails. |
| 475 | */ |
| 476 | |
| 477 | void* ipc_rcu_alloc(int size) |
| 478 | { |
| 479 | void* out; |
| 480 | /* |
| 481 | * We prepend the allocation with the rcu struct, and |
| 482 | * workqueue if necessary (for vmalloc). |
| 483 | */ |
| 484 | if (rcu_use_vmalloc(size)) { |
| 485 | out = vmalloc(HDRLEN_VMALLOC + size); |
| 486 | if (out) { |
| 487 | out += HDRLEN_VMALLOC; |
| 488 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1; |
| 489 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; |
| 490 | } |
| 491 | } else { |
| 492 | out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL); |
| 493 | if (out) { |
| 494 | out += HDRLEN_KMALLOC; |
| 495 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0; |
| 496 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return out; |
| 501 | } |
| 502 | |
| 503 | void ipc_rcu_getref(void *ptr) |
| 504 | { |
| 505 | container_of(ptr, struct ipc_rcu_hdr, data)->refcount++; |
| 506 | } |
| 507 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 508 | static void ipc_do_vfree(struct work_struct *work) |
| 509 | { |
| 510 | vfree(container_of(work, struct ipc_rcu_sched, work)); |
| 511 | } |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | /** |
Randy Dunlap | 1e5d533 | 2005-11-07 01:01:06 -0800 | [diff] [blame] | 514 | * ipc_schedule_free - free ipc + rcu space |
| 515 | * @head: RCU callback structure for queued work |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | * |
| 517 | * Since RCU callback function is called in bh, |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 518 | * we need to defer the vfree to schedule_work(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | */ |
| 520 | static void ipc_schedule_free(struct rcu_head *head) |
| 521 | { |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 522 | struct ipc_rcu_grace *grace; |
| 523 | struct ipc_rcu_sched *sched; |
| 524 | |
| 525 | grace = container_of(head, struct ipc_rcu_grace, rcu); |
| 526 | sched = container_of(&(grace->data[0]), struct ipc_rcu_sched, |
| 527 | data[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 529 | INIT_WORK(&sched->work, ipc_do_vfree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | schedule_work(&sched->work); |
| 531 | } |
| 532 | |
| 533 | /** |
Randy Dunlap | 1e5d533 | 2005-11-07 01:01:06 -0800 | [diff] [blame] | 534 | * ipc_immediate_free - free ipc + rcu space |
| 535 | * @head: RCU callback structure that contains pointer to be freed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 537 | * Free from the RCU callback context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | */ |
| 539 | static void ipc_immediate_free(struct rcu_head *head) |
| 540 | { |
| 541 | struct ipc_rcu_grace *free = |
| 542 | container_of(head, struct ipc_rcu_grace, rcu); |
| 543 | kfree(free); |
| 544 | } |
| 545 | |
| 546 | void ipc_rcu_putref(void *ptr) |
| 547 | { |
| 548 | if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0) |
| 549 | return; |
| 550 | |
| 551 | if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) { |
| 552 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, |
| 553 | ipc_schedule_free); |
| 554 | } else { |
| 555 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, |
| 556 | ipc_immediate_free); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * ipcperms - check IPC permissions |
| 562 | * @ipcp: IPC permission set |
| 563 | * @flag: desired permission set. |
| 564 | * |
| 565 | * Check user, group, other permissions for access |
| 566 | * to ipc resources. return 0 if allowed |
| 567 | */ |
| 568 | |
| 569 | int ipcperms (struct kern_ipc_perm *ipcp, short flag) |
| 570 | { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */ |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 571 | int requested_mode, granted_mode, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 573 | if (unlikely((err = audit_ipc_obj(ipcp)))) |
| 574 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | requested_mode = (flag >> 6) | (flag >> 3) | flag; |
| 576 | granted_mode = ipcp->mode; |
| 577 | if (current->euid == ipcp->cuid || current->euid == ipcp->uid) |
| 578 | granted_mode >>= 6; |
| 579 | else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid)) |
| 580 | granted_mode >>= 3; |
| 581 | /* is there some bit set in requested_mode but not in granted_mode? */ |
| 582 | if ((requested_mode & ~granted_mode & 0007) && |
| 583 | !capable(CAP_IPC_OWNER)) |
| 584 | return -1; |
| 585 | |
| 586 | return security_ipc_permission(ipcp, flag); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Functions to convert between the kern_ipc_perm structure and the |
| 591 | * old/new ipc_perm structures |
| 592 | */ |
| 593 | |
| 594 | /** |
| 595 | * kernel_to_ipc64_perm - convert kernel ipc permissions to user |
| 596 | * @in: kernel permissions |
| 597 | * @out: new style IPC permissions |
| 598 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 599 | * Turn the kernel object @in into a set of permissions descriptions |
| 600 | * for returning to userspace (@out). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | */ |
| 602 | |
| 603 | |
| 604 | void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out) |
| 605 | { |
| 606 | out->key = in->key; |
| 607 | out->uid = in->uid; |
| 608 | out->gid = in->gid; |
| 609 | out->cuid = in->cuid; |
| 610 | out->cgid = in->cgid; |
| 611 | out->mode = in->mode; |
| 612 | out->seq = in->seq; |
| 613 | } |
| 614 | |
| 615 | /** |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 616 | * ipc64_perm_to_ipc_perm - convert new ipc permissions to old |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | * @in: new style IPC permissions |
| 618 | * @out: old style IPC permissions |
| 619 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 620 | * Turn the new style permissions object @in into a compatibility |
| 621 | * object and store it into the @out pointer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | */ |
| 623 | |
| 624 | void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out) |
| 625 | { |
| 626 | out->key = in->key; |
| 627 | SET_UID(out->uid, in->uid); |
| 628 | SET_GID(out->gid, in->gid); |
| 629 | SET_UID(out->cuid, in->cuid); |
| 630 | SET_GID(out->cgid, in->cgid); |
| 631 | out->mode = in->mode; |
| 632 | out->seq = in->seq; |
| 633 | } |
| 634 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 635 | /** |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 636 | * ipc_lock - Lock an ipc structure without rw_mutex held |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 637 | * @ids: IPC identifier set |
| 638 | * @id: ipc id to look for |
| 639 | * |
| 640 | * Look for an id in the ipc ids idr and lock the associated ipc object. |
| 641 | * |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 642 | * The ipc object is locked on exit. |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 643 | * |
| 644 | * This is the routine that should be called when the rw_mutex is not already |
| 645 | * held, i.e. idr tree not protected: it protects the idr tree in read mode |
| 646 | * during the idr_find(). |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 647 | */ |
| 648 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 649 | struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 651 | struct kern_ipc_perm *out; |
Nadia Derbey | ce621f5 | 2007-10-18 23:40:52 -0700 | [diff] [blame] | 652 | int lid = ipcid_to_idx(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 654 | down_read(&ids->rw_mutex); |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | rcu_read_lock(); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 657 | out = idr_find(&ids->ipcs_idr, lid); |
| 658 | if (out == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | rcu_read_unlock(); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 660 | up_read(&ids->rw_mutex); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 661 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 663 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 664 | up_read(&ids->rw_mutex); |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | spin_lock(&out->lock); |
| 667 | |
| 668 | /* ipc_rmid() may have already freed the ID while ipc_lock |
| 669 | * was spinning: here verify that the structure is still valid |
| 670 | */ |
| 671 | if (out->deleted) { |
| 672 | spin_unlock(&out->lock); |
| 673 | rcu_read_unlock(); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 674 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return out; |
| 678 | } |
| 679 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 680 | /** |
| 681 | * ipc_lock_down - Lock an ipc structure with rw_sem held |
| 682 | * @ids: IPC identifier set |
| 683 | * @id: ipc id to look for |
| 684 | * |
| 685 | * Look for an id in the ipc ids idr and lock the associated ipc object. |
| 686 | * |
| 687 | * The ipc object is locked on exit. |
| 688 | * |
| 689 | * This is the routine that should be called when the rw_mutex is already |
| 690 | * held, i.e. idr tree protected. |
| 691 | */ |
| 692 | |
| 693 | struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id) |
| 694 | { |
| 695 | struct kern_ipc_perm *out; |
| 696 | int lid = ipcid_to_idx(id); |
| 697 | |
| 698 | rcu_read_lock(); |
| 699 | out = idr_find(&ids->ipcs_idr, lid); |
| 700 | if (out == NULL) { |
| 701 | rcu_read_unlock(); |
| 702 | return ERR_PTR(-EINVAL); |
| 703 | } |
| 704 | |
| 705 | spin_lock(&out->lock); |
| 706 | |
| 707 | /* |
| 708 | * No need to verify that the structure is still valid since the |
| 709 | * rw_mutex is held. |
| 710 | */ |
| 711 | return out; |
| 712 | } |
| 713 | |
Pavel Emelyanov | b2d75cd | 2008-02-08 04:18:54 -0800 | [diff] [blame] | 714 | struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id) |
| 715 | { |
| 716 | struct kern_ipc_perm *out; |
| 717 | |
| 718 | out = ipc_lock_down(ids, id); |
| 719 | if (IS_ERR(out)) |
| 720 | return out; |
| 721 | |
| 722 | if (ipc_checkid(out, id)) { |
| 723 | ipc_unlock(out); |
| 724 | return ERR_PTR(-EIDRM); |
| 725 | } |
| 726 | |
| 727 | return out; |
| 728 | } |
| 729 | |
| 730 | struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id) |
| 731 | { |
| 732 | struct kern_ipc_perm *out; |
| 733 | |
| 734 | out = ipc_lock(ids, id); |
| 735 | if (IS_ERR(out)) |
| 736 | return out; |
| 737 | |
| 738 | if (ipc_checkid(out, id)) { |
| 739 | ipc_unlock(out); |
| 740 | return ERR_PTR(-EIDRM); |
| 741 | } |
| 742 | |
| 743 | return out; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * ipcget - Common sys_*get() code |
| 748 | * @ns : namsepace |
| 749 | * @ids : IPC identifier set |
| 750 | * @ops : operations to be called on ipc object creation, permission checks |
| 751 | * and further checks |
| 752 | * @params : the parameters needed by the previous operations. |
| 753 | * |
| 754 | * Common routine called by sys_msgget(), sys_semget() and sys_shmget(). |
| 755 | */ |
| 756 | int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, |
| 757 | struct ipc_ops *ops, struct ipc_params *params) |
| 758 | { |
| 759 | if (params->key == IPC_PRIVATE) |
| 760 | return ipcget_new(ns, ids, ops, params); |
| 761 | else |
| 762 | return ipcget_public(ns, ids, ops, params); |
| 763 | } |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | #ifdef __ARCH_WANT_IPC_PARSE_VERSION |
| 766 | |
| 767 | |
| 768 | /** |
| 769 | * ipc_parse_version - IPC call version |
| 770 | * @cmd: pointer to command |
| 771 | * |
| 772 | * Return IPC_64 for new style IPC and IPC_OLD for old style IPC. |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 773 | * The @cmd value is turned from an encoding command and version into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | * just the command code. |
| 775 | */ |
| 776 | |
| 777 | int ipc_parse_version (int *cmd) |
| 778 | { |
| 779 | if (*cmd & IPC_64) { |
| 780 | *cmd ^= IPC_64; |
| 781 | return IPC_64; |
| 782 | } else { |
| 783 | return IPC_OLD; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | #endif /* __ARCH_WANT_IPC_PARSE_VERSION */ |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 788 | |
| 789 | #ifdef CONFIG_PROC_FS |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 790 | struct ipc_proc_iter { |
| 791 | struct ipc_namespace *ns; |
| 792 | struct ipc_proc_iface *iface; |
| 793 | }; |
| 794 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 795 | /* |
| 796 | * This routine locks the ipc structure found at least at position pos. |
| 797 | */ |
Adrian Bunk | b524b9a | 2008-02-06 01:36:28 -0800 | [diff] [blame] | 798 | static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos, |
| 799 | loff_t *new_pos) |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 800 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 801 | struct kern_ipc_perm *ipc; |
| 802 | int total, id; |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 803 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 804 | total = 0; |
| 805 | for (id = 0; id < pos && total < ids->in_use; id++) { |
| 806 | ipc = idr_find(&ids->ipcs_idr, id); |
| 807 | if (ipc != NULL) |
| 808 | total++; |
| 809 | } |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 810 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 811 | if (total >= ids->in_use) |
| 812 | return NULL; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 813 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 814 | for ( ; pos < IPCMNI; pos++) { |
| 815 | ipc = idr_find(&ids->ipcs_idr, pos); |
| 816 | if (ipc != NULL) { |
| 817 | *new_pos = pos + 1; |
| 818 | ipc_lock_by_ptr(ipc); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 819 | return ipc; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /* Out of range - return NULL to terminate iteration */ |
| 824 | return NULL; |
| 825 | } |
| 826 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 827 | static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) |
| 828 | { |
| 829 | struct ipc_proc_iter *iter = s->private; |
| 830 | struct ipc_proc_iface *iface = iter->iface; |
| 831 | struct kern_ipc_perm *ipc = it; |
| 832 | |
| 833 | /* If we had an ipc id locked before, unlock it */ |
| 834 | if (ipc && ipc != SEQ_START_TOKEN) |
| 835 | ipc_unlock(ipc); |
| 836 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 837 | return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 840 | /* |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 841 | * File positions: pos 0 -> header, pos n -> ipc id = n - 1. |
| 842 | * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START. |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 843 | */ |
| 844 | static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) |
| 845 | { |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 846 | struct ipc_proc_iter *iter = s->private; |
| 847 | struct ipc_proc_iface *iface = iter->iface; |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 848 | struct ipc_ids *ids; |
| 849 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 850 | ids = &iter->ns->ids[iface->ids]; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | * Take the lock - this will be released by the corresponding |
| 854 | * call to stop(). |
| 855 | */ |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 856 | down_read(&ids->rw_mutex); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 857 | |
| 858 | /* pos < 0 is invalid */ |
| 859 | if (*pos < 0) |
| 860 | return NULL; |
| 861 | |
| 862 | /* pos == 0 means header */ |
| 863 | if (*pos == 0) |
| 864 | return SEQ_START_TOKEN; |
| 865 | |
| 866 | /* Find the (pos-1)th ipc */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 867 | return sysvipc_find_ipc(ids, *pos - 1, pos); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static void sysvipc_proc_stop(struct seq_file *s, void *it) |
| 871 | { |
| 872 | struct kern_ipc_perm *ipc = it; |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 873 | struct ipc_proc_iter *iter = s->private; |
| 874 | struct ipc_proc_iface *iface = iter->iface; |
Kirill Korotaev | 73ea413 | 2006-10-02 02:18:20 -0700 | [diff] [blame] | 875 | struct ipc_ids *ids; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 876 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 877 | /* If we had a locked structure, release it */ |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 878 | if (ipc && ipc != SEQ_START_TOKEN) |
| 879 | ipc_unlock(ipc); |
| 880 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 881 | ids = &iter->ns->ids[iface->ids]; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 882 | /* Release the lock we took in start() */ |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 883 | up_read(&ids->rw_mutex); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static int sysvipc_proc_show(struct seq_file *s, void *it) |
| 887 | { |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 888 | struct ipc_proc_iter *iter = s->private; |
| 889 | struct ipc_proc_iface *iface = iter->iface; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 890 | |
| 891 | if (it == SEQ_START_TOKEN) |
| 892 | return seq_puts(s, iface->header); |
| 893 | |
| 894 | return iface->show(s, it); |
| 895 | } |
| 896 | |
| 897 | static struct seq_operations sysvipc_proc_seqops = { |
| 898 | .start = sysvipc_proc_start, |
| 899 | .stop = sysvipc_proc_stop, |
| 900 | .next = sysvipc_proc_next, |
| 901 | .show = sysvipc_proc_show, |
| 902 | }; |
| 903 | |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 904 | static int sysvipc_proc_open(struct inode *inode, struct file *file) |
| 905 | { |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 906 | int ret; |
| 907 | struct seq_file *seq; |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 908 | struct ipc_proc_iter *iter; |
| 909 | |
| 910 | ret = -ENOMEM; |
| 911 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 912 | if (!iter) |
| 913 | goto out; |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 914 | |
| 915 | ret = seq_open(file, &sysvipc_proc_seqops); |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 916 | if (ret) |
| 917 | goto out_kfree; |
| 918 | |
| 919 | seq = file->private_data; |
| 920 | seq->private = iter; |
| 921 | |
| 922 | iter->iface = PDE(inode)->data; |
| 923 | iter->ns = get_ipc_ns(current->nsproxy->ipc_ns); |
| 924 | out: |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 925 | return ret; |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 926 | out_kfree: |
| 927 | kfree(iter); |
| 928 | goto out; |
| 929 | } |
| 930 | |
| 931 | static int sysvipc_proc_release(struct inode *inode, struct file *file) |
| 932 | { |
| 933 | struct seq_file *seq = file->private_data; |
| 934 | struct ipc_proc_iter *iter = seq->private; |
| 935 | put_ipc_ns(iter->ns); |
| 936 | return seq_release_private(inode, file); |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 939 | static const struct file_operations sysvipc_proc_fops = { |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 940 | .open = sysvipc_proc_open, |
| 941 | .read = seq_read, |
| 942 | .llseek = seq_lseek, |
Eric W. Biederman | bc1fc6d | 2007-02-12 00:52:10 -0800 | [diff] [blame] | 943 | .release = sysvipc_proc_release, |
Mike Waychison | ae78177 | 2005-09-06 15:17:09 -0700 | [diff] [blame] | 944 | }; |
| 945 | #endif /* CONFIG_PROC_FS */ |