| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 | 
|  | 2 | /* | 
|  | 3 | * linux/ipc/util.c | 
|  | 4 | * Copyright (C) 1992 Krishna Balasubramanian | 
|  | 5 | * | 
|  | 6 | * Sep 1997 - Call suser() last after "normal" permission checks so we | 
|  | 7 | *            get BSD style process accounting right. | 
|  | 8 | *            Occurs in several places in the IPC code. | 
|  | 9 | *            Chris Evans, <chris@ferret.lmh.ox.ac.uk> | 
|  | 10 | * Nov 1999 - ipc helper functions, unified SMP locking | 
|  | 11 | *	      Manfred Spraul <manfred@colorfullife.com> | 
|  | 12 | * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary(). | 
|  | 13 | *            Mingming Cao <cmm@us.ibm.com> | 
|  | 14 | * Mar 2006 - support for audit of ipc object properties | 
|  | 15 | *            Dustin Kirkland <dustin.kirkland@us.ibm.com> | 
|  | 16 | * Jun 2006 - namespaces ssupport | 
|  | 17 | *            OpenVZ, SWsoft Inc. | 
|  | 18 | *            Pavel Emelianov <xemul@openvz.org> | 
|  | 19 | * | 
|  | 20 | * General sysv ipc locking scheme: | 
|  | 21 | *	rcu_read_lock() | 
|  | 22 | *          obtain the ipc object (kern_ipc_perm) by looking up the id in an idr | 
|  | 23 | *	    tree. | 
|  | 24 | *	    - perform initial checks (capabilities, auditing and permission, | 
|  | 25 | *	      etc). | 
|  | 26 | *	    - perform read-only operations, such as INFO command, that | 
|  | 27 | *	      do not demand atomicity | 
|  | 28 | *	      acquire the ipc lock (kern_ipc_perm.lock) through | 
|  | 29 | *	      ipc_lock_object() | 
|  | 30 | *		- perform read-only operations that demand atomicity, | 
|  | 31 | *		  such as STAT command. | 
|  | 32 | *		- perform data updates, such as SET, RMID commands and | 
|  | 33 | *		  mechanism-specific operations (semop/semtimedop, | 
|  | 34 | *		  msgsnd/msgrcv, shmat/shmdt). | 
|  | 35 | *	    drop the ipc lock, through ipc_unlock_object(). | 
|  | 36 | *	rcu_read_unlock() | 
|  | 37 | * | 
|  | 38 | *  The ids->rwsem must be taken when: | 
|  | 39 | *	- creating, removing and iterating the existing entries in ipc | 
|  | 40 | *	  identifier sets. | 
|  | 41 | *	- iterating through files under /proc/sysvipc/ | 
|  | 42 | * | 
|  | 43 | *  Note that sems have a special fast path that avoids kern_ipc_perm.lock - | 
|  | 44 | *  see sem_lock(). | 
|  | 45 | */ | 
|  | 46 |  | 
|  | 47 | #include <linux/mm.h> | 
|  | 48 | #include <linux/shm.h> | 
|  | 49 | #include <linux/init.h> | 
|  | 50 | #include <linux/msg.h> | 
|  | 51 | #include <linux/vmalloc.h> | 
|  | 52 | #include <linux/slab.h> | 
|  | 53 | #include <linux/notifier.h> | 
|  | 54 | #include <linux/capability.h> | 
|  | 55 | #include <linux/highuid.h> | 
|  | 56 | #include <linux/security.h> | 
|  | 57 | #include <linux/rcupdate.h> | 
|  | 58 | #include <linux/workqueue.h> | 
|  | 59 | #include <linux/seq_file.h> | 
|  | 60 | #include <linux/proc_fs.h> | 
|  | 61 | #include <linux/audit.h> | 
|  | 62 | #include <linux/nsproxy.h> | 
|  | 63 | #include <linux/rwsem.h> | 
|  | 64 | #include <linux/memory.h> | 
|  | 65 | #include <linux/ipc_namespace.h> | 
|  | 66 | #include <linux/rhashtable.h> | 
|  | 67 |  | 
|  | 68 | #include <asm/unistd.h> | 
|  | 69 |  | 
|  | 70 | #include "util.h" | 
|  | 71 |  | 
|  | 72 | struct ipc_proc_iface { | 
|  | 73 | const char *path; | 
|  | 74 | const char *header; | 
|  | 75 | int ids; | 
|  | 76 | int (*show)(struct seq_file *, void *); | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | /** | 
|  | 80 | * ipc_init - initialise ipc subsystem | 
|  | 81 | * | 
|  | 82 | * The various sysv ipc resources (semaphores, messages and shared | 
|  | 83 | * memory) are initialised. | 
|  | 84 | * | 
|  | 85 | * A callback routine is registered into the memory hotplug notifier | 
|  | 86 | * chain: since msgmni scales to lowmem this callback routine will be | 
|  | 87 | * called upon successful memory add / remove to recompute msmgni. | 
|  | 88 | */ | 
|  | 89 | static int __init ipc_init(void) | 
|  | 90 | { | 
|  | 91 | proc_mkdir("sysvipc", NULL); | 
|  | 92 | sem_init(); | 
|  | 93 | msg_init(); | 
|  | 94 | shm_init(); | 
|  | 95 |  | 
|  | 96 | return 0; | 
|  | 97 | } | 
|  | 98 | device_initcall(ipc_init); | 
|  | 99 |  | 
|  | 100 | static const struct rhashtable_params ipc_kht_params = { | 
|  | 101 | .head_offset		= offsetof(struct kern_ipc_perm, khtnode), | 
|  | 102 | .key_offset		= offsetof(struct kern_ipc_perm, key), | 
|  | 103 | .key_len		= FIELD_SIZEOF(struct kern_ipc_perm, key), | 
|  | 104 | .locks_mul		= 1, | 
|  | 105 | .automatic_shrinking	= true, | 
|  | 106 | }; | 
|  | 107 |  | 
|  | 108 | /** | 
|  | 109 | * ipc_init_ids	- initialise ipc identifiers | 
|  | 110 | * @ids: ipc identifier set | 
|  | 111 | * | 
|  | 112 | * Set up the sequence range to use for the ipc identifier range (limited | 
|  | 113 | * below IPCMNI) then initialise the keys hashtable and ids idr. | 
|  | 114 | */ | 
|  | 115 | void ipc_init_ids(struct ipc_ids *ids) | 
|  | 116 | { | 
|  | 117 | ids->in_use = 0; | 
|  | 118 | ids->seq = 0; | 
|  | 119 | init_rwsem(&ids->rwsem); | 
|  | 120 | rhashtable_init(&ids->key_ht, &ipc_kht_params); | 
|  | 121 | idr_init(&ids->ipcs_idr); | 
|  | 122 | ids->max_idx = -1; | 
|  | 123 | #ifdef CONFIG_CHECKPOINT_RESTORE | 
|  | 124 | ids->next_id = -1; | 
|  | 125 | #endif | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | #ifdef CONFIG_PROC_FS | 
|  | 129 | static const struct file_operations sysvipc_proc_fops; | 
|  | 130 | /** | 
|  | 131 | * ipc_init_proc_interface -  create a proc interface for sysipc types using a seq_file interface. | 
|  | 132 | * @path: Path in procfs | 
|  | 133 | * @header: Banner to be printed at the beginning of the file. | 
|  | 134 | * @ids: ipc id table to iterate. | 
|  | 135 | * @show: show routine. | 
|  | 136 | */ | 
|  | 137 | void __init ipc_init_proc_interface(const char *path, const char *header, | 
|  | 138 | int ids, int (*show)(struct seq_file *, void *)) | 
|  | 139 | { | 
|  | 140 | struct proc_dir_entry *pde; | 
|  | 141 | struct ipc_proc_iface *iface; | 
|  | 142 |  | 
|  | 143 | if (IS_ENABLED(CONFIG_PROC_STRIPPED)) | 
|  | 144 | return; | 
|  | 145 |  | 
|  | 146 | iface = kmalloc(sizeof(*iface), GFP_KERNEL); | 
|  | 147 | if (!iface) | 
|  | 148 | return; | 
|  | 149 | iface->path	= path; | 
|  | 150 | iface->header	= header; | 
|  | 151 | iface->ids	= ids; | 
|  | 152 | iface->show	= show; | 
|  | 153 |  | 
|  | 154 | pde = proc_create_data(path, | 
|  | 155 | S_IRUGO,        /* world readable */ | 
|  | 156 | NULL,           /* parent dir */ | 
|  | 157 | &sysvipc_proc_fops, | 
|  | 158 | iface); | 
|  | 159 | if (!pde) | 
|  | 160 | kfree(iface); | 
|  | 161 | } | 
|  | 162 | #endif | 
|  | 163 |  | 
|  | 164 | /** | 
|  | 165 | * ipc_findkey	- find a key in an ipc identifier set | 
|  | 166 | * @ids: ipc identifier set | 
|  | 167 | * @key: key to find | 
|  | 168 | * | 
|  | 169 | * Returns the locked pointer to the ipc structure if found or NULL | 
|  | 170 | * otherwise. If key is found ipc points to the owning ipc structure | 
|  | 171 | * | 
|  | 172 | * Called with writer ipc_ids.rwsem held. | 
|  | 173 | */ | 
|  | 174 | static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) | 
|  | 175 | { | 
|  | 176 | struct kern_ipc_perm *ipcp; | 
|  | 177 |  | 
|  | 178 | ipcp = rhashtable_lookup_fast(&ids->key_ht, &key, | 
|  | 179 | ipc_kht_params); | 
|  | 180 | if (!ipcp) | 
|  | 181 | return NULL; | 
|  | 182 |  | 
|  | 183 | rcu_read_lock(); | 
|  | 184 | ipc_lock_object(ipcp); | 
|  | 185 | return ipcp; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | /* | 
|  | 189 | * Insert new IPC object into idr tree, and set sequence number and id | 
|  | 190 | * in the correct order. | 
|  | 191 | * Especially: | 
|  | 192 | * - the sequence number must be set before inserting the object into the idr, | 
|  | 193 | *   because the sequence number is accessed without a lock. | 
|  | 194 | * - the id can/must be set after inserting the object into the idr. | 
|  | 195 | *   All accesses must be done after getting kern_ipc_perm.lock. | 
|  | 196 | * | 
|  | 197 | * The caller must own kern_ipc_perm.lock.of the new object. | 
|  | 198 | * On error, the function returns a (negative) error code. | 
|  | 199 | */ | 
|  | 200 | static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) | 
|  | 201 | { | 
|  | 202 | int idx, next_id = -1; | 
|  | 203 |  | 
|  | 204 | #ifdef CONFIG_CHECKPOINT_RESTORE | 
|  | 205 | next_id = ids->next_id; | 
|  | 206 | ids->next_id = -1; | 
|  | 207 | #endif | 
|  | 208 |  | 
|  | 209 | /* | 
|  | 210 | * As soon as a new object is inserted into the idr, | 
|  | 211 | * ipc_obtain_object_idr() or ipc_obtain_object_check() can find it, | 
|  | 212 | * and the lockless preparations for ipc operations can start. | 
|  | 213 | * This means especially: permission checks, audit calls, allocation | 
|  | 214 | * of undo structures, ... | 
|  | 215 | * | 
|  | 216 | * Thus the object must be fully initialized, and if something fails, | 
|  | 217 | * then the full tear-down sequence must be followed. | 
|  | 218 | * (i.e.: set new->deleted, reduce refcount, call_rcu()) | 
|  | 219 | */ | 
|  | 220 |  | 
|  | 221 | if (next_id < 0) { /* !CHECKPOINT_RESTORE or next_id is unset */ | 
|  | 222 | new->seq = ids->seq++; | 
|  | 223 | if (ids->seq > IPCID_SEQ_MAX) | 
|  | 224 | ids->seq = 0; | 
|  | 225 | idx = idr_alloc(&ids->ipcs_idr, new, 0, 0, GFP_NOWAIT); | 
|  | 226 | } else { | 
|  | 227 | new->seq = ipcid_to_seqx(next_id); | 
|  | 228 | idx = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id), | 
|  | 229 | 0, GFP_NOWAIT); | 
|  | 230 | } | 
|  | 231 | if (idx >= 0) | 
|  | 232 | new->id = SEQ_MULTIPLIER * new->seq + idx; | 
|  | 233 | return idx; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | /** | 
|  | 237 | * ipc_addid - add an ipc identifier | 
|  | 238 | * @ids: ipc identifier set | 
|  | 239 | * @new: new ipc permission set | 
|  | 240 | * @limit: limit for the number of used ids | 
|  | 241 | * | 
|  | 242 | * Add an entry 'new' to the ipc ids idr. The permissions object is | 
|  | 243 | * initialised and the first free entry is set up and the index assigned | 
|  | 244 | * is returned. The 'new' entry is returned in a locked state on success. | 
|  | 245 | * | 
|  | 246 | * On failure the entry is not locked and a negative err-code is returned. | 
|  | 247 | * The caller must use ipc_rcu_putref() to free the identifier. | 
|  | 248 | * | 
|  | 249 | * Called with writer ipc_ids.rwsem held. | 
|  | 250 | */ | 
|  | 251 | int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) | 
|  | 252 | { | 
|  | 253 | kuid_t euid; | 
|  | 254 | kgid_t egid; | 
|  | 255 | int idx, err; | 
|  | 256 |  | 
|  | 257 | /* 1) Initialize the refcount so that ipc_rcu_putref works */ | 
|  | 258 | refcount_set(&new->refcount, 1); | 
|  | 259 |  | 
|  | 260 | if (limit > IPCMNI) | 
|  | 261 | limit = IPCMNI; | 
|  | 262 |  | 
|  | 263 | if (ids->in_use >= limit) | 
|  | 264 | return -ENOSPC; | 
|  | 265 |  | 
|  | 266 | idr_preload(GFP_KERNEL); | 
|  | 267 |  | 
|  | 268 | spin_lock_init(&new->lock); | 
|  | 269 | rcu_read_lock(); | 
|  | 270 | spin_lock(&new->lock); | 
|  | 271 |  | 
|  | 272 | current_euid_egid(&euid, &egid); | 
|  | 273 | new->cuid = new->uid = euid; | 
|  | 274 | new->gid = new->cgid = egid; | 
|  | 275 |  | 
|  | 276 | new->deleted = false; | 
|  | 277 |  | 
|  | 278 | idx = ipc_idr_alloc(ids, new); | 
|  | 279 | idr_preload_end(); | 
|  | 280 |  | 
|  | 281 | if (idx >= 0 && new->key != IPC_PRIVATE) { | 
|  | 282 | err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode, | 
|  | 283 | ipc_kht_params); | 
|  | 284 | if (err < 0) { | 
|  | 285 | idr_remove(&ids->ipcs_idr, idx); | 
|  | 286 | idx = err; | 
|  | 287 | } | 
|  | 288 | } | 
|  | 289 | if (idx < 0) { | 
|  | 290 | new->deleted = true; | 
|  | 291 | spin_unlock(&new->lock); | 
|  | 292 | rcu_read_unlock(); | 
|  | 293 | return idx; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | ids->in_use++; | 
|  | 297 | if (idx > ids->max_idx) | 
|  | 298 | ids->max_idx = idx; | 
|  | 299 | return idx; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | /** | 
|  | 303 | * ipcget_new -	create a new ipc object | 
|  | 304 | * @ns: ipc namespace | 
|  | 305 | * @ids: ipc identifier set | 
|  | 306 | * @ops: the actual creation routine to call | 
|  | 307 | * @params: its parameters | 
|  | 308 | * | 
|  | 309 | * This routine is called by sys_msgget, sys_semget() and sys_shmget() | 
|  | 310 | * when the key is IPC_PRIVATE. | 
|  | 311 | */ | 
|  | 312 | static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids, | 
|  | 313 | const struct ipc_ops *ops, struct ipc_params *params) | 
|  | 314 | { | 
|  | 315 | int err; | 
|  | 316 |  | 
|  | 317 | down_write(&ids->rwsem); | 
|  | 318 | err = ops->getnew(ns, params); | 
|  | 319 | up_write(&ids->rwsem); | 
|  | 320 | return err; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | /** | 
|  | 324 | * ipc_check_perms - check security and permissions for an ipc object | 
|  | 325 | * @ns: ipc namespace | 
|  | 326 | * @ipcp: ipc permission set | 
|  | 327 | * @ops: the actual security routine to call | 
|  | 328 | * @params: its parameters | 
|  | 329 | * | 
|  | 330 | * This routine is called by sys_msgget(), sys_semget() and sys_shmget() | 
|  | 331 | * when the key is not IPC_PRIVATE and that key already exists in the | 
|  | 332 | * ds IDR. | 
|  | 333 | * | 
|  | 334 | * On success, the ipc id is returned. | 
|  | 335 | * | 
|  | 336 | * It is called with ipc_ids.rwsem and ipcp->lock held. | 
|  | 337 | */ | 
|  | 338 | static int ipc_check_perms(struct ipc_namespace *ns, | 
|  | 339 | struct kern_ipc_perm *ipcp, | 
|  | 340 | const struct ipc_ops *ops, | 
|  | 341 | struct ipc_params *params) | 
|  | 342 | { | 
|  | 343 | int err; | 
|  | 344 |  | 
|  | 345 | if (ipcperms(ns, ipcp, params->flg)) | 
|  | 346 | err = -EACCES; | 
|  | 347 | else { | 
|  | 348 | err = ops->associate(ipcp, params->flg); | 
|  | 349 | if (!err) | 
|  | 350 | err = ipcp->id; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | return err; | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | /** | 
|  | 357 | * ipcget_public - get an ipc object or create a new one | 
|  | 358 | * @ns: ipc namespace | 
|  | 359 | * @ids: ipc identifier set | 
|  | 360 | * @ops: the actual creation routine to call | 
|  | 361 | * @params: its parameters | 
|  | 362 | * | 
|  | 363 | * This routine is called by sys_msgget, sys_semget() and sys_shmget() | 
|  | 364 | * when the key is not IPC_PRIVATE. | 
|  | 365 | * It adds a new entry if the key is not found and does some permission | 
|  | 366 | * / security checkings if the key is found. | 
|  | 367 | * | 
|  | 368 | * On success, the ipc id is returned. | 
|  | 369 | */ | 
|  | 370 | static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids, | 
|  | 371 | const struct ipc_ops *ops, struct ipc_params *params) | 
|  | 372 | { | 
|  | 373 | struct kern_ipc_perm *ipcp; | 
|  | 374 | int flg = params->flg; | 
|  | 375 | int err; | 
|  | 376 |  | 
|  | 377 | /* | 
|  | 378 | * Take the lock as a writer since we are potentially going to add | 
|  | 379 | * a new entry + read locks are not "upgradable" | 
|  | 380 | */ | 
|  | 381 | down_write(&ids->rwsem); | 
|  | 382 | ipcp = ipc_findkey(ids, params->key); | 
|  | 383 | if (ipcp == NULL) { | 
|  | 384 | /* key not used */ | 
|  | 385 | if (!(flg & IPC_CREAT)) | 
|  | 386 | err = -ENOENT; | 
|  | 387 | else | 
|  | 388 | err = ops->getnew(ns, params); | 
|  | 389 | } else { | 
|  | 390 | /* ipc object has been locked by ipc_findkey() */ | 
|  | 391 |  | 
|  | 392 | if (flg & IPC_CREAT && flg & IPC_EXCL) | 
|  | 393 | err = -EEXIST; | 
|  | 394 | else { | 
|  | 395 | err = 0; | 
|  | 396 | if (ops->more_checks) | 
|  | 397 | err = ops->more_checks(ipcp, params); | 
|  | 398 | if (!err) | 
|  | 399 | /* | 
|  | 400 | * ipc_check_perms returns the IPC id on | 
|  | 401 | * success | 
|  | 402 | */ | 
|  | 403 | err = ipc_check_perms(ns, ipcp, ops, params); | 
|  | 404 | } | 
|  | 405 | ipc_unlock(ipcp); | 
|  | 406 | } | 
|  | 407 | up_write(&ids->rwsem); | 
|  | 408 |  | 
|  | 409 | return err; | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | /** | 
|  | 413 | * ipc_kht_remove - remove an ipc from the key hashtable | 
|  | 414 | * @ids: ipc identifier set | 
|  | 415 | * @ipcp: ipc perm structure containing the key to remove | 
|  | 416 | * | 
|  | 417 | * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held | 
|  | 418 | * before this function is called, and remain locked on the exit. | 
|  | 419 | */ | 
|  | 420 | static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) | 
|  | 421 | { | 
|  | 422 | if (ipcp->key != IPC_PRIVATE) | 
|  | 423 | rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode, | 
|  | 424 | ipc_kht_params); | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | /** | 
|  | 428 | * ipc_rmid - remove an ipc identifier | 
|  | 429 | * @ids: ipc identifier set | 
|  | 430 | * @ipcp: ipc perm structure containing the identifier to remove | 
|  | 431 | * | 
|  | 432 | * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held | 
|  | 433 | * before this function is called, and remain locked on the exit. | 
|  | 434 | */ | 
|  | 435 | void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) | 
|  | 436 | { | 
|  | 437 | int idx = ipcid_to_idx(ipcp->id); | 
|  | 438 |  | 
|  | 439 | idr_remove(&ids->ipcs_idr, idx); | 
|  | 440 | ipc_kht_remove(ids, ipcp); | 
|  | 441 | ids->in_use--; | 
|  | 442 | ipcp->deleted = true; | 
|  | 443 |  | 
|  | 444 | if (unlikely(idx == ids->max_idx)) { | 
|  | 445 | do { | 
|  | 446 | idx--; | 
|  | 447 | if (idx == -1) | 
|  | 448 | break; | 
|  | 449 | } while (!idr_find(&ids->ipcs_idr, idx)); | 
|  | 450 | ids->max_idx = idx; | 
|  | 451 | } | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | /** | 
|  | 455 | * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE | 
|  | 456 | * @ids: ipc identifier set | 
|  | 457 | * @ipcp: ipc perm structure containing the key to modify | 
|  | 458 | * | 
|  | 459 | * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held | 
|  | 460 | * before this function is called, and remain locked on the exit. | 
|  | 461 | */ | 
|  | 462 | void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) | 
|  | 463 | { | 
|  | 464 | ipc_kht_remove(ids, ipcp); | 
|  | 465 | ipcp->key = IPC_PRIVATE; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | bool ipc_rcu_getref(struct kern_ipc_perm *ptr) | 
|  | 469 | { | 
|  | 470 | return refcount_inc_not_zero(&ptr->refcount); | 
|  | 471 | } | 
|  | 472 |  | 
|  | 473 | void ipc_rcu_putref(struct kern_ipc_perm *ptr, | 
|  | 474 | void (*func)(struct rcu_head *head)) | 
|  | 475 | { | 
|  | 476 | if (!refcount_dec_and_test(&ptr->refcount)) | 
|  | 477 | return; | 
|  | 478 |  | 
|  | 479 | call_rcu(&ptr->rcu, func); | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | /** | 
|  | 483 | * ipcperms - check ipc permissions | 
|  | 484 | * @ns: ipc namespace | 
|  | 485 | * @ipcp: ipc permission set | 
|  | 486 | * @flag: desired permission set | 
|  | 487 | * | 
|  | 488 | * Check user, group, other permissions for access | 
|  | 489 | * to ipc resources. return 0 if allowed | 
|  | 490 | * | 
|  | 491 | * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h> | 
|  | 492 | */ | 
|  | 493 | int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag) | 
|  | 494 | { | 
|  | 495 | kuid_t euid = current_euid(); | 
|  | 496 | int requested_mode, granted_mode; | 
|  | 497 |  | 
|  | 498 | audit_ipc_obj(ipcp); | 
|  | 499 | requested_mode = (flag >> 6) | (flag >> 3) | flag; | 
|  | 500 | granted_mode = ipcp->mode; | 
|  | 501 | if (uid_eq(euid, ipcp->cuid) || | 
|  | 502 | uid_eq(euid, ipcp->uid)) | 
|  | 503 | granted_mode >>= 6; | 
|  | 504 | else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid)) | 
|  | 505 | granted_mode >>= 3; | 
|  | 506 | /* is there some bit set in requested_mode but not in granted_mode? */ | 
|  | 507 | if ((requested_mode & ~granted_mode & 0007) && | 
|  | 508 | !ns_capable(ns->user_ns, CAP_IPC_OWNER)) | 
|  | 509 | return -1; | 
|  | 510 |  | 
|  | 511 | return security_ipc_permission(ipcp, flag); | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | /* | 
|  | 515 | * Functions to convert between the kern_ipc_perm structure and the | 
|  | 516 | * old/new ipc_perm structures | 
|  | 517 | */ | 
|  | 518 |  | 
|  | 519 | /** | 
|  | 520 | * kernel_to_ipc64_perm	- convert kernel ipc permissions to user | 
|  | 521 | * @in: kernel permissions | 
|  | 522 | * @out: new style ipc permissions | 
|  | 523 | * | 
|  | 524 | * Turn the kernel object @in into a set of permissions descriptions | 
|  | 525 | * for returning to userspace (@out). | 
|  | 526 | */ | 
|  | 527 | void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out) | 
|  | 528 | { | 
|  | 529 | out->key	= in->key; | 
|  | 530 | out->uid	= from_kuid_munged(current_user_ns(), in->uid); | 
|  | 531 | out->gid	= from_kgid_munged(current_user_ns(), in->gid); | 
|  | 532 | out->cuid	= from_kuid_munged(current_user_ns(), in->cuid); | 
|  | 533 | out->cgid	= from_kgid_munged(current_user_ns(), in->cgid); | 
|  | 534 | out->mode	= in->mode; | 
|  | 535 | out->seq	= in->seq; | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | /** | 
|  | 539 | * ipc64_perm_to_ipc_perm - convert new ipc permissions to old | 
|  | 540 | * @in: new style ipc permissions | 
|  | 541 | * @out: old style ipc permissions | 
|  | 542 | * | 
|  | 543 | * Turn the new style permissions object @in into a compatibility | 
|  | 544 | * object and store it into the @out pointer. | 
|  | 545 | */ | 
|  | 546 | void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out) | 
|  | 547 | { | 
|  | 548 | out->key	= in->key; | 
|  | 549 | SET_UID(out->uid, in->uid); | 
|  | 550 | SET_GID(out->gid, in->gid); | 
|  | 551 | SET_UID(out->cuid, in->cuid); | 
|  | 552 | SET_GID(out->cgid, in->cgid); | 
|  | 553 | out->mode	= in->mode; | 
|  | 554 | out->seq	= in->seq; | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | /** | 
|  | 558 | * ipc_obtain_object_idr | 
|  | 559 | * @ids: ipc identifier set | 
|  | 560 | * @id: ipc id to look for | 
|  | 561 | * | 
|  | 562 | * Look for an id in the ipc ids idr and return associated ipc object. | 
|  | 563 | * | 
|  | 564 | * Call inside the RCU critical section. | 
|  | 565 | * The ipc object is *not* locked on exit. | 
|  | 566 | */ | 
|  | 567 | struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id) | 
|  | 568 | { | 
|  | 569 | struct kern_ipc_perm *out; | 
|  | 570 | int idx = ipcid_to_idx(id); | 
|  | 571 |  | 
|  | 572 | out = idr_find(&ids->ipcs_idr, idx); | 
|  | 573 | if (!out) | 
|  | 574 | return ERR_PTR(-EINVAL); | 
|  | 575 |  | 
|  | 576 | return out; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | /** | 
|  | 580 | * ipc_obtain_object_check | 
|  | 581 | * @ids: ipc identifier set | 
|  | 582 | * @id: ipc id to look for | 
|  | 583 | * | 
|  | 584 | * Similar to ipc_obtain_object_idr() but also checks the ipc object | 
|  | 585 | * sequence number. | 
|  | 586 | * | 
|  | 587 | * Call inside the RCU critical section. | 
|  | 588 | * The ipc object is *not* locked on exit. | 
|  | 589 | */ | 
|  | 590 | struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id) | 
|  | 591 | { | 
|  | 592 | struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id); | 
|  | 593 |  | 
|  | 594 | if (IS_ERR(out)) | 
|  | 595 | goto out; | 
|  | 596 |  | 
|  | 597 | if (ipc_checkid(out, id)) | 
|  | 598 | return ERR_PTR(-EINVAL); | 
|  | 599 | out: | 
|  | 600 | return out; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | /** | 
|  | 604 | * ipcget - Common sys_*get() code | 
|  | 605 | * @ns: namespace | 
|  | 606 | * @ids: ipc identifier set | 
|  | 607 | * @ops: operations to be called on ipc object creation, permission checks | 
|  | 608 | *       and further checks | 
|  | 609 | * @params: the parameters needed by the previous operations. | 
|  | 610 | * | 
|  | 611 | * Common routine called by sys_msgget(), sys_semget() and sys_shmget(). | 
|  | 612 | */ | 
|  | 613 | int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, | 
|  | 614 | const struct ipc_ops *ops, struct ipc_params *params) | 
|  | 615 | { | 
|  | 616 | if (params->key == IPC_PRIVATE) | 
|  | 617 | return ipcget_new(ns, ids, ops, params); | 
|  | 618 | else | 
|  | 619 | return ipcget_public(ns, ids, ops, params); | 
|  | 620 | } | 
|  | 621 |  | 
|  | 622 | /** | 
|  | 623 | * ipc_update_perm - update the permissions of an ipc object | 
|  | 624 | * @in:  the permission given as input. | 
|  | 625 | * @out: the permission of the ipc to set. | 
|  | 626 | */ | 
|  | 627 | int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out) | 
|  | 628 | { | 
|  | 629 | kuid_t uid = make_kuid(current_user_ns(), in->uid); | 
|  | 630 | kgid_t gid = make_kgid(current_user_ns(), in->gid); | 
|  | 631 | if (!uid_valid(uid) || !gid_valid(gid)) | 
|  | 632 | return -EINVAL; | 
|  | 633 |  | 
|  | 634 | out->uid = uid; | 
|  | 635 | out->gid = gid; | 
|  | 636 | out->mode = (out->mode & ~S_IRWXUGO) | 
|  | 637 | | (in->mode & S_IRWXUGO); | 
|  | 638 |  | 
|  | 639 | return 0; | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | /** | 
|  | 643 | * ipcctl_obtain_check - retrieve an ipc object and check permissions | 
|  | 644 | * @ns:  ipc namespace | 
|  | 645 | * @ids:  the table of ids where to look for the ipc | 
|  | 646 | * @id:   the id of the ipc to retrieve | 
|  | 647 | * @cmd:  the cmd to check | 
|  | 648 | * @perm: the permission to set | 
|  | 649 | * @extra_perm: one extra permission parameter used by msq | 
|  | 650 | * | 
|  | 651 | * This function does some common audit and permissions check for some IPC_XXX | 
|  | 652 | * cmd and is called from semctl_down, shmctl_down and msgctl_down. | 
|  | 653 | * | 
|  | 654 | * It: | 
|  | 655 | *   - retrieves the ipc object with the given id in the given table. | 
|  | 656 | *   - performs some audit and permission check, depending on the given cmd | 
|  | 657 | *   - returns a pointer to the ipc object or otherwise, the corresponding | 
|  | 658 | *     error. | 
|  | 659 | * | 
|  | 660 | * Call holding the both the rwsem and the rcu read lock. | 
|  | 661 | */ | 
|  | 662 | struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns, | 
|  | 663 | struct ipc_ids *ids, int id, int cmd, | 
|  | 664 | struct ipc64_perm *perm, int extra_perm) | 
|  | 665 | { | 
|  | 666 | kuid_t euid; | 
|  | 667 | int err = -EPERM; | 
|  | 668 | struct kern_ipc_perm *ipcp; | 
|  | 669 |  | 
|  | 670 | ipcp = ipc_obtain_object_check(ids, id); | 
|  | 671 | if (IS_ERR(ipcp)) { | 
|  | 672 | err = PTR_ERR(ipcp); | 
|  | 673 | goto err; | 
|  | 674 | } | 
|  | 675 |  | 
|  | 676 | audit_ipc_obj(ipcp); | 
|  | 677 | if (cmd == IPC_SET) | 
|  | 678 | audit_ipc_set_perm(extra_perm, perm->uid, | 
|  | 679 | perm->gid, perm->mode); | 
|  | 680 |  | 
|  | 681 | euid = current_euid(); | 
|  | 682 | if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid)  || | 
|  | 683 | ns_capable(ns->user_ns, CAP_SYS_ADMIN)) | 
|  | 684 | return ipcp; /* successful lookup */ | 
|  | 685 | err: | 
|  | 686 | return ERR_PTR(err); | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION | 
|  | 690 |  | 
|  | 691 |  | 
|  | 692 | /** | 
|  | 693 | * ipc_parse_version - ipc call version | 
|  | 694 | * @cmd: pointer to command | 
|  | 695 | * | 
|  | 696 | * Return IPC_64 for new style IPC and IPC_OLD for old style IPC. | 
|  | 697 | * The @cmd value is turned from an encoding command and version into | 
|  | 698 | * just the command code. | 
|  | 699 | */ | 
|  | 700 | int ipc_parse_version(int *cmd) | 
|  | 701 | { | 
|  | 702 | if (*cmd & IPC_64) { | 
|  | 703 | *cmd ^= IPC_64; | 
|  | 704 | return IPC_64; | 
|  | 705 | } else { | 
|  | 706 | return IPC_OLD; | 
|  | 707 | } | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */ | 
|  | 711 |  | 
|  | 712 | #ifdef CONFIG_PROC_FS | 
|  | 713 | struct ipc_proc_iter { | 
|  | 714 | struct ipc_namespace *ns; | 
|  | 715 | struct pid_namespace *pid_ns; | 
|  | 716 | struct ipc_proc_iface *iface; | 
|  | 717 | }; | 
|  | 718 |  | 
|  | 719 | struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s) | 
|  | 720 | { | 
|  | 721 | struct ipc_proc_iter *iter = s->private; | 
|  | 722 | return iter->pid_ns; | 
|  | 723 | } | 
|  | 724 |  | 
|  | 725 | /* | 
|  | 726 | * This routine locks the ipc structure found at least at position pos. | 
|  | 727 | */ | 
|  | 728 | static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos, | 
|  | 729 | loff_t *new_pos) | 
|  | 730 | { | 
|  | 731 | struct kern_ipc_perm *ipc; | 
|  | 732 | int total, id; | 
|  | 733 |  | 
|  | 734 | total = 0; | 
|  | 735 | for (id = 0; id < pos && total < ids->in_use; id++) { | 
|  | 736 | ipc = idr_find(&ids->ipcs_idr, id); | 
|  | 737 | if (ipc != NULL) | 
|  | 738 | total++; | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | if (total >= ids->in_use) | 
|  | 742 | return NULL; | 
|  | 743 |  | 
|  | 744 | for (; pos < IPCMNI; pos++) { | 
|  | 745 | ipc = idr_find(&ids->ipcs_idr, pos); | 
|  | 746 | if (ipc != NULL) { | 
|  | 747 | *new_pos = pos + 1; | 
|  | 748 | rcu_read_lock(); | 
|  | 749 | ipc_lock_object(ipc); | 
|  | 750 | return ipc; | 
|  | 751 | } | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | /* Out of range - return NULL to terminate iteration */ | 
|  | 755 | return NULL; | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) | 
|  | 759 | { | 
|  | 760 | struct ipc_proc_iter *iter = s->private; | 
|  | 761 | struct ipc_proc_iface *iface = iter->iface; | 
|  | 762 | struct kern_ipc_perm *ipc = it; | 
|  | 763 |  | 
|  | 764 | /* If we had an ipc id locked before, unlock it */ | 
|  | 765 | if (ipc && ipc != SEQ_START_TOKEN) | 
|  | 766 | ipc_unlock(ipc); | 
|  | 767 |  | 
|  | 768 | return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos); | 
|  | 769 | } | 
|  | 770 |  | 
|  | 771 | /* | 
|  | 772 | * File positions: pos 0 -> header, pos n -> ipc id = n - 1. | 
|  | 773 | * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START. | 
|  | 774 | */ | 
|  | 775 | static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) | 
|  | 776 | { | 
|  | 777 | struct ipc_proc_iter *iter = s->private; | 
|  | 778 | struct ipc_proc_iface *iface = iter->iface; | 
|  | 779 | struct ipc_ids *ids; | 
|  | 780 |  | 
|  | 781 | ids = &iter->ns->ids[iface->ids]; | 
|  | 782 |  | 
|  | 783 | /* | 
|  | 784 | * Take the lock - this will be released by the corresponding | 
|  | 785 | * call to stop(). | 
|  | 786 | */ | 
|  | 787 | down_read(&ids->rwsem); | 
|  | 788 |  | 
|  | 789 | /* pos < 0 is invalid */ | 
|  | 790 | if (*pos < 0) | 
|  | 791 | return NULL; | 
|  | 792 |  | 
|  | 793 | /* pos == 0 means header */ | 
|  | 794 | if (*pos == 0) | 
|  | 795 | return SEQ_START_TOKEN; | 
|  | 796 |  | 
|  | 797 | /* Find the (pos-1)th ipc */ | 
|  | 798 | return sysvipc_find_ipc(ids, *pos - 1, pos); | 
|  | 799 | } | 
|  | 800 |  | 
|  | 801 | static void sysvipc_proc_stop(struct seq_file *s, void *it) | 
|  | 802 | { | 
|  | 803 | struct kern_ipc_perm *ipc = it; | 
|  | 804 | struct ipc_proc_iter *iter = s->private; | 
|  | 805 | struct ipc_proc_iface *iface = iter->iface; | 
|  | 806 | struct ipc_ids *ids; | 
|  | 807 |  | 
|  | 808 | /* If we had a locked structure, release it */ | 
|  | 809 | if (ipc && ipc != SEQ_START_TOKEN) | 
|  | 810 | ipc_unlock(ipc); | 
|  | 811 |  | 
|  | 812 | ids = &iter->ns->ids[iface->ids]; | 
|  | 813 | /* Release the lock we took in start() */ | 
|  | 814 | up_read(&ids->rwsem); | 
|  | 815 | } | 
|  | 816 |  | 
|  | 817 | static int sysvipc_proc_show(struct seq_file *s, void *it) | 
|  | 818 | { | 
|  | 819 | struct ipc_proc_iter *iter = s->private; | 
|  | 820 | struct ipc_proc_iface *iface = iter->iface; | 
|  | 821 |  | 
|  | 822 | if (it == SEQ_START_TOKEN) { | 
|  | 823 | seq_puts(s, iface->header); | 
|  | 824 | return 0; | 
|  | 825 | } | 
|  | 826 |  | 
|  | 827 | return iface->show(s, it); | 
|  | 828 | } | 
|  | 829 |  | 
|  | 830 | static const struct seq_operations sysvipc_proc_seqops = { | 
|  | 831 | .start = sysvipc_proc_start, | 
|  | 832 | .stop  = sysvipc_proc_stop, | 
|  | 833 | .next  = sysvipc_proc_next, | 
|  | 834 | .show  = sysvipc_proc_show, | 
|  | 835 | }; | 
|  | 836 |  | 
|  | 837 | static int sysvipc_proc_open(struct inode *inode, struct file *file) | 
|  | 838 | { | 
|  | 839 | struct ipc_proc_iter *iter; | 
|  | 840 |  | 
|  | 841 | iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter)); | 
|  | 842 | if (!iter) | 
|  | 843 | return -ENOMEM; | 
|  | 844 |  | 
|  | 845 | iter->iface = PDE_DATA(inode); | 
|  | 846 | iter->ns    = get_ipc_ns(current->nsproxy->ipc_ns); | 
|  | 847 | iter->pid_ns = get_pid_ns(task_active_pid_ns(current)); | 
|  | 848 |  | 
|  | 849 | return 0; | 
|  | 850 | } | 
|  | 851 |  | 
|  | 852 | static int sysvipc_proc_release(struct inode *inode, struct file *file) | 
|  | 853 | { | 
|  | 854 | struct seq_file *seq = file->private_data; | 
|  | 855 | struct ipc_proc_iter *iter = seq->private; | 
|  | 856 | put_ipc_ns(iter->ns); | 
|  | 857 | put_pid_ns(iter->pid_ns); | 
|  | 858 | return seq_release_private(inode, file); | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | static const struct file_operations sysvipc_proc_fops = { | 
|  | 862 | .open    = sysvipc_proc_open, | 
|  | 863 | .read    = seq_read, | 
|  | 864 | .llseek  = seq_lseek, | 
|  | 865 | .release = sysvipc_proc_release, | 
|  | 866 | }; | 
|  | 867 | #endif /* CONFIG_PROC_FS */ |