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 10 * Manfred Spraul <manfred@colorfullife.com> 11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary(). 12 * Mingming Cao <cmm@us.ibm.com> 13 */ 14 15 #include <linux/config.h> 16 #include <linux/mm.h> 17 #include <linux/shm.h> 18 #include <linux/init.h> 19 #include <linux/msg.h> 20 #include <linux/smp_lock.h> 21 #include <linux/vmalloc.h> 22 #include <linux/slab.h> 23 #include <linux/capability.h> 24 #include <linux/highuid.h> 25 #include <linux/security.h> 26 #include <linux/rcupdate.h> 27 #include <linux/workqueue.h> 28 #include <linux/seq_file.h> 29 #include <linux/proc_fs.h> 30 31 #include <asm/unistd.h> 32 33 #include "util.h" 34 35 struct ipc_proc_iface { 36 const char *path; 37 const char *header; 38 struct ipc_ids *ids; 39 int (*show)(struct seq_file *, void *); 40 }; 41 42 /** 43 * ipc_init - initialise IPC subsystem 44 * 45 * The various system5 IPC resources (semaphores, messages and shared 46 * memory are initialised 47 */ 48 49 static int __init ipc_init(void) 50 { 51 sem_init(); 52 msg_init(); 53 shm_init(); 54 return 0; 55 } 56 __initcall(ipc_init); 57 58 /** 59 * ipc_init_ids - initialise IPC identifiers 60 * @ids: Identifier set 61 * @size: Number of identifiers 62 * 63 * Given a size for the ipc identifier range (limited below IPCMNI) 64 * set up the sequence range to use then allocate and initialise the 65 * array itself. 66 */ 67 68 void __init ipc_init_ids(struct ipc_ids* ids, int size) 69 { 70 int i; 71 72 mutex_init(&ids->mutex); 73 74 if(size > IPCMNI) 75 size = IPCMNI; 76 ids->in_use = 0; 77 ids->max_id = -1; 78 ids->seq = 0; 79 { 80 int seq_limit = INT_MAX/SEQ_MULTIPLIER; 81 if(seq_limit > USHRT_MAX) 82 ids->seq_max = USHRT_MAX; 83 else 84 ids->seq_max = seq_limit; 85 } 86 87 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size + 88 sizeof(struct ipc_id_ary)); 89 90 if(ids->entries == NULL) { 91 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n"); 92 size = 0; 93 ids->entries = &ids->nullentry; 94 } 95 ids->entries->size = size; 96 for(i=0;i<size;i++) 97 ids->entries->p[i] = NULL; 98 } 99 100 #ifdef CONFIG_PROC_FS 101 static struct file_operations sysvipc_proc_fops; 102 /** 103 * ipc_init_proc_interface - Create a proc interface for sysipc types 104 * using a seq_file interface. 105 * @path: Path in procfs 106 * @header: Banner to be printed at the beginning of the file. 107 * @ids: ipc id table to iterate. 108 * @show: show routine. 109 */ 110 void __init ipc_init_proc_interface(const char *path, const char *header, 111 struct ipc_ids *ids, 112 int (*show)(struct seq_file *, void *)) 113 { 114 struct proc_dir_entry *pde; 115 struct ipc_proc_iface *iface; 116 117 iface = kmalloc(sizeof(*iface), GFP_KERNEL); 118 if (!iface) 119 return; 120 iface->path = path; 121 iface->header = header; 122 iface->ids = ids; 123 iface->show = show; 124 125 pde = create_proc_entry(path, 126 S_IRUGO, /* world readable */ 127 NULL /* parent dir */); 128 if (pde) { 129 pde->data = iface; 130 pde->proc_fops = &sysvipc_proc_fops; 131 } else { 132 kfree(iface); 133 } 134 } 135 #endif 136 137 /** 138 * ipc_findkey - find a key in an ipc identifier set 139 * @ids: Identifier set 140 * @key: The key to find 141 * 142 * Requires ipc_ids.mutex locked. 143 * Returns the identifier if found or -1 if not. 144 */ 145 146 int ipc_findkey(struct ipc_ids* ids, key_t key) 147 { 148 int id; 149 struct kern_ipc_perm* p; 150 int max_id = ids->max_id; 151 152 /* 153 * rcu_dereference() is not needed here 154 * since ipc_ids.mutex is held 155 */ 156 for (id = 0; id <= max_id; id++) { 157 p = ids->entries->p[id]; 158 if(p==NULL) 159 continue; 160 if (key == p->key) 161 return id; 162 } 163 return -1; 164 } 165 166 /* 167 * Requires ipc_ids.mutex locked 168 */ 169 static int grow_ary(struct ipc_ids* ids, int newsize) 170 { 171 struct ipc_id_ary* new; 172 struct ipc_id_ary* old; 173 int i; 174 int size = ids->entries->size; 175 176 if(newsize > IPCMNI) 177 newsize = IPCMNI; 178 if(newsize <= size) 179 return newsize; 180 181 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize + 182 sizeof(struct ipc_id_ary)); 183 if(new == NULL) 184 return size; 185 new->size = newsize; 186 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size + 187 sizeof(struct ipc_id_ary)); 188 for(i=size;i<newsize;i++) { 189 new->p[i] = NULL; 190 } 191 old = ids->entries; 192 193 /* 194 * Use rcu_assign_pointer() to make sure the memcpyed contents 195 * of the new array are visible before the new array becomes visible. 196 */ 197 rcu_assign_pointer(ids->entries, new); 198 199 ipc_rcu_putref(old); 200 return newsize; 201 } 202 203 /** 204 * ipc_addid - add an IPC identifier 205 * @ids: IPC identifier set 206 * @new: new IPC permission set 207 * @size: new size limit for the id array 208 * 209 * Add an entry 'new' to the IPC arrays. The permissions object is 210 * initialised and the first free entry is set up and the id assigned 211 * is returned. The list is returned in a locked state on success. 212 * On failure the list is not locked and -1 is returned. 213 * 214 * Called with ipc_ids.mutex held. 215 */ 216 217 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) 218 { 219 int id; 220 221 size = grow_ary(ids,size); 222 223 /* 224 * rcu_dereference()() is not needed here since 225 * ipc_ids.mutex is held 226 */ 227 for (id = 0; id < size; id++) { 228 if(ids->entries->p[id] == NULL) 229 goto found; 230 } 231 return -1; 232 found: 233 ids->in_use++; 234 if (id > ids->max_id) 235 ids->max_id = id; 236 237 new->cuid = new->uid = current->euid; 238 new->gid = new->cgid = current->egid; 239 240 new->seq = ids->seq++; 241 if(ids->seq > ids->seq_max) 242 ids->seq = 0; 243 244 spin_lock_init(&new->lock); 245 new->deleted = 0; 246 rcu_read_lock(); 247 spin_lock(&new->lock); 248 ids->entries->p[id] = new; 249 return id; 250 } 251 252 /** 253 * ipc_rmid - remove an IPC identifier 254 * @ids: identifier set 255 * @id: Identifier to remove 256 * 257 * The identifier must be valid, and in use. The kernel will panic if 258 * fed an invalid identifier. The entry is removed and internal 259 * variables recomputed. The object associated with the identifier 260 * is returned. 261 * ipc_ids.mutex and the spinlock for this ID is hold before this function 262 * is called, and remain locked on the exit. 263 */ 264 265 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id) 266 { 267 struct kern_ipc_perm* p; 268 int lid = id % SEQ_MULTIPLIER; 269 BUG_ON(lid >= ids->entries->size); 270 271 /* 272 * do not need a rcu_dereference()() here to force ordering 273 * on Alpha, since the ipc_ids.mutex is held. 274 */ 275 p = ids->entries->p[lid]; 276 ids->entries->p[lid] = NULL; 277 BUG_ON(p==NULL); 278 ids->in_use--; 279 280 if (lid == ids->max_id) { 281 do { 282 lid--; 283 if(lid == -1) 284 break; 285 } while (ids->entries->p[lid] == NULL); 286 ids->max_id = lid; 287 } 288 p->deleted = 1; 289 return p; 290 } 291 292 /** 293 * ipc_alloc - allocate ipc space 294 * @size: size desired 295 * 296 * Allocate memory from the appropriate pools and return a pointer to it. 297 * NULL is returned if the allocation fails 298 */ 299 300 void* ipc_alloc(int size) 301 { 302 void* out; 303 if(size > PAGE_SIZE) 304 out = vmalloc(size); 305 else 306 out = kmalloc(size, GFP_KERNEL); 307 return out; 308 } 309 310 /** 311 * ipc_free - free ipc space 312 * @ptr: pointer returned by ipc_alloc 313 * @size: size of block 314 * 315 * Free a block created with ipc_alloc. The caller must know the size 316 * used in the allocation call. 317 */ 318 319 void ipc_free(void* ptr, int size) 320 { 321 if(size > PAGE_SIZE) 322 vfree(ptr); 323 else 324 kfree(ptr); 325 } 326 327 /* 328 * rcu allocations: 329 * There are three headers that are prepended to the actual allocation: 330 * - during use: ipc_rcu_hdr. 331 * - during the rcu grace period: ipc_rcu_grace. 332 * - [only if vmalloc]: ipc_rcu_sched. 333 * Their lifetime doesn't overlap, thus the headers share the same memory. 334 * Unlike a normal union, they are right-aligned, thus some container_of 335 * forward/backward casting is necessary: 336 */ 337 struct ipc_rcu_hdr 338 { 339 int refcount; 340 int is_vmalloc; 341 void *data[0]; 342 }; 343 344 345 struct ipc_rcu_grace 346 { 347 struct rcu_head rcu; 348 /* "void *" makes sure alignment of following data is sane. */ 349 void *data[0]; 350 }; 351 352 struct ipc_rcu_sched 353 { 354 struct work_struct work; 355 /* "void *" makes sure alignment of following data is sane. */ 356 void *data[0]; 357 }; 358 359 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \ 360 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr)) 361 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \ 362 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC) 363 364 static inline int rcu_use_vmalloc(int size) 365 { 366 /* Too big for a single page? */ 367 if (HDRLEN_KMALLOC + size > PAGE_SIZE) 368 return 1; 369 return 0; 370 } 371 372 /** 373 * ipc_rcu_alloc - allocate ipc and rcu space 374 * @size: size desired 375 * 376 * Allocate memory for the rcu header structure + the object. 377 * Returns the pointer to the object. 378 * NULL is returned if the allocation fails. 379 */ 380 381 void* ipc_rcu_alloc(int size) 382 { 383 void* out; 384 /* 385 * We prepend the allocation with the rcu struct, and 386 * workqueue if necessary (for vmalloc). 387 */ 388 if (rcu_use_vmalloc(size)) { 389 out = vmalloc(HDRLEN_VMALLOC + size); 390 if (out) { 391 out += HDRLEN_VMALLOC; 392 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1; 393 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; 394 } 395 } else { 396 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL); 397 if (out) { 398 out += HDRLEN_KMALLOC; 399 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0; 400 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; 401 } 402 } 403 404 return out; 405 } 406 407 void ipc_rcu_getref(void *ptr) 408 { 409 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++; 410 } 411 412 /** 413 * ipc_schedule_free - free ipc + rcu space 414 * @head: RCU callback structure for queued work 415 * 416 * Since RCU callback function is called in bh, 417 * we need to defer the vfree to schedule_work 418 */ 419 static void ipc_schedule_free(struct rcu_head *head) 420 { 421 struct ipc_rcu_grace *grace = 422 container_of(head, struct ipc_rcu_grace, rcu); 423 struct ipc_rcu_sched *sched = 424 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]); 425 426 INIT_WORK(&sched->work, vfree, sched); 427 schedule_work(&sched->work); 428 } 429 430 /** 431 * ipc_immediate_free - free ipc + rcu space 432 * @head: RCU callback structure that contains pointer to be freed 433 * 434 * Free from the RCU callback context 435 */ 436 static void ipc_immediate_free(struct rcu_head *head) 437 { 438 struct ipc_rcu_grace *free = 439 container_of(head, struct ipc_rcu_grace, rcu); 440 kfree(free); 441 } 442 443 void ipc_rcu_putref(void *ptr) 444 { 445 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0) 446 return; 447 448 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) { 449 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, 450 ipc_schedule_free); 451 } else { 452 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, 453 ipc_immediate_free); 454 } 455 } 456 457 /** 458 * ipcperms - check IPC permissions 459 * @ipcp: IPC permission set 460 * @flag: desired permission set. 461 * 462 * Check user, group, other permissions for access 463 * to ipc resources. return 0 if allowed 464 */ 465 466 int ipcperms (struct kern_ipc_perm *ipcp, short flag) 467 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */ 468 int requested_mode, granted_mode; 469 470 requested_mode = (flag >> 6) | (flag >> 3) | flag; 471 granted_mode = ipcp->mode; 472 if (current->euid == ipcp->cuid || current->euid == ipcp->uid) 473 granted_mode >>= 6; 474 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid)) 475 granted_mode >>= 3; 476 /* is there some bit set in requested_mode but not in granted_mode? */ 477 if ((requested_mode & ~granted_mode & 0007) && 478 !capable(CAP_IPC_OWNER)) 479 return -1; 480 481 return security_ipc_permission(ipcp, flag); 482 } 483 484 /* 485 * Functions to convert between the kern_ipc_perm structure and the 486 * old/new ipc_perm structures 487 */ 488 489 /** 490 * kernel_to_ipc64_perm - convert kernel ipc permissions to user 491 * @in: kernel permissions 492 * @out: new style IPC permissions 493 * 494 * Turn the kernel object 'in' into a set of permissions descriptions 495 * for returning to userspace (out). 496 */ 497 498 499 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out) 500 { 501 out->key = in->key; 502 out->uid = in->uid; 503 out->gid = in->gid; 504 out->cuid = in->cuid; 505 out->cgid = in->cgid; 506 out->mode = in->mode; 507 out->seq = in->seq; 508 } 509 510 /** 511 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new 512 * @in: new style IPC permissions 513 * @out: old style IPC permissions 514 * 515 * Turn the new style permissions object in into a compatibility 516 * object and store it into the 'out' pointer. 517 */ 518 519 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out) 520 { 521 out->key = in->key; 522 SET_UID(out->uid, in->uid); 523 SET_GID(out->gid, in->gid); 524 SET_UID(out->cuid, in->cuid); 525 SET_GID(out->cgid, in->cgid); 526 out->mode = in->mode; 527 out->seq = in->seq; 528 } 529 530 /* 531 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get() 532 * is called with shm_ids.mutex locked. Since grow_ary() is also called with 533 * shm_ids.mutex down(for Shared Memory), there is no need to add read 534 * barriers here to gurantee the writes in grow_ary() are seen in order 535 * here (for Alpha). 536 * 537 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So 538 * if in the future ipc_get() is used by other places without ipc_ids.mutex 539 * down, then ipc_get() needs read memery barriers as ipc_lock() does. 540 */ 541 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id) 542 { 543 struct kern_ipc_perm* out; 544 int lid = id % SEQ_MULTIPLIER; 545 if(lid >= ids->entries->size) 546 return NULL; 547 out = ids->entries->p[lid]; 548 return out; 549 } 550 551 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id) 552 { 553 struct kern_ipc_perm* out; 554 int lid = id % SEQ_MULTIPLIER; 555 struct ipc_id_ary* entries; 556 557 rcu_read_lock(); 558 entries = rcu_dereference(ids->entries); 559 if(lid >= entries->size) { 560 rcu_read_unlock(); 561 return NULL; 562 } 563 out = entries->p[lid]; 564 if(out == NULL) { 565 rcu_read_unlock(); 566 return NULL; 567 } 568 spin_lock(&out->lock); 569 570 /* ipc_rmid() may have already freed the ID while ipc_lock 571 * was spinning: here verify that the structure is still valid 572 */ 573 if (out->deleted) { 574 spin_unlock(&out->lock); 575 rcu_read_unlock(); 576 return NULL; 577 } 578 return out; 579 } 580 581 void ipc_lock_by_ptr(struct kern_ipc_perm *perm) 582 { 583 rcu_read_lock(); 584 spin_lock(&perm->lock); 585 } 586 587 void ipc_unlock(struct kern_ipc_perm* perm) 588 { 589 spin_unlock(&perm->lock); 590 rcu_read_unlock(); 591 } 592 593 int ipc_buildid(struct ipc_ids* ids, int id, int seq) 594 { 595 return SEQ_MULTIPLIER*seq + id; 596 } 597 598 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid) 599 { 600 if(uid/SEQ_MULTIPLIER != ipcp->seq) 601 return 1; 602 return 0; 603 } 604 605 #ifdef __ARCH_WANT_IPC_PARSE_VERSION 606 607 608 /** 609 * ipc_parse_version - IPC call version 610 * @cmd: pointer to command 611 * 612 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC. 613 * The cmd value is turned from an encoding command and version into 614 * just the command code. 615 */ 616 617 int ipc_parse_version (int *cmd) 618 { 619 if (*cmd & IPC_64) { 620 *cmd ^= IPC_64; 621 return IPC_64; 622 } else { 623 return IPC_OLD; 624 } 625 } 626 627 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */ 628 629 #ifdef CONFIG_PROC_FS 630 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) 631 { 632 struct ipc_proc_iface *iface = s->private; 633 struct kern_ipc_perm *ipc = it; 634 loff_t p; 635 636 /* If we had an ipc id locked before, unlock it */ 637 if (ipc && ipc != SEQ_START_TOKEN) 638 ipc_unlock(ipc); 639 640 /* 641 * p = *pos - 1 (because id 0 starts at position 1) 642 * + 1 (because we increment the position by one) 643 */ 644 for (p = *pos; p <= iface->ids->max_id; p++) { 645 if ((ipc = ipc_lock(iface->ids, p)) != NULL) { 646 *pos = p + 1; 647 return ipc; 648 } 649 } 650 651 /* Out of range - return NULL to terminate iteration */ 652 return NULL; 653 } 654 655 /* 656 * File positions: pos 0 -> header, pos n -> ipc id + 1. 657 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START. 658 */ 659 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) 660 { 661 struct ipc_proc_iface *iface = s->private; 662 struct kern_ipc_perm *ipc; 663 loff_t p; 664 665 /* 666 * Take the lock - this will be released by the corresponding 667 * call to stop(). 668 */ 669 mutex_lock(&iface->ids->mutex); 670 671 /* pos < 0 is invalid */ 672 if (*pos < 0) 673 return NULL; 674 675 /* pos == 0 means header */ 676 if (*pos == 0) 677 return SEQ_START_TOKEN; 678 679 /* Find the (pos-1)th ipc */ 680 for (p = *pos - 1; p <= iface->ids->max_id; p++) { 681 if ((ipc = ipc_lock(iface->ids, p)) != NULL) { 682 *pos = p + 1; 683 return ipc; 684 } 685 } 686 return NULL; 687 } 688 689 static void sysvipc_proc_stop(struct seq_file *s, void *it) 690 { 691 struct kern_ipc_perm *ipc = it; 692 struct ipc_proc_iface *iface = s->private; 693 694 /* If we had a locked segment, release it */ 695 if (ipc && ipc != SEQ_START_TOKEN) 696 ipc_unlock(ipc); 697 698 /* Release the lock we took in start() */ 699 mutex_unlock(&iface->ids->mutex); 700 } 701 702 static int sysvipc_proc_show(struct seq_file *s, void *it) 703 { 704 struct ipc_proc_iface *iface = s->private; 705 706 if (it == SEQ_START_TOKEN) 707 return seq_puts(s, iface->header); 708 709 return iface->show(s, it); 710 } 711 712 static struct seq_operations sysvipc_proc_seqops = { 713 .start = sysvipc_proc_start, 714 .stop = sysvipc_proc_stop, 715 .next = sysvipc_proc_next, 716 .show = sysvipc_proc_show, 717 }; 718 719 static int sysvipc_proc_open(struct inode *inode, struct file *file) { 720 int ret; 721 struct seq_file *seq; 722 723 ret = seq_open(file, &sysvipc_proc_seqops); 724 if (!ret) { 725 seq = file->private_data; 726 seq->private = PDE(inode)->data; 727 } 728 return ret; 729 } 730 731 static struct file_operations sysvipc_proc_fops = { 732 .open = sysvipc_proc_open, 733 .read = seq_read, 734 .llseek = seq_lseek, 735 .release = seq_release, 736 }; 737 #endif /* CONFIG_PROC_FS */ 738