Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/ipc/shm.c |
| 3 | * Copyright (C) 1992, 1993 Krishna Balasubramanian |
| 4 | * Many improvements/fixes by Bruno Haible. |
| 5 | * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994. |
| 6 | * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli. |
| 7 | * |
| 8 | * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com> |
| 9 | * BIGMEM support, Andrea Arcangeli <andrea@suse.de> |
| 10 | * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr> |
| 11 | * HIGHMEM support, Ingo Molnar <mingo@redhat.com> |
| 12 | * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com> |
| 13 | * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com> |
| 14 | * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com> |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/config.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/hugetlb.h> |
| 22 | #include <linux/shm.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/file.h> |
| 25 | #include <linux/mman.h> |
| 26 | #include <linux/proc_fs.h> |
| 27 | #include <linux/shmem_fs.h> |
| 28 | #include <linux/security.h> |
| 29 | #include <linux/syscalls.h> |
| 30 | #include <linux/audit.h> |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 31 | #include <linux/ptrace.h> |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/uaccess.h> |
| 34 | |
| 35 | #include "util.h" |
| 36 | |
| 37 | #define shm_flags shm_perm.mode |
| 38 | |
| 39 | static struct file_operations shm_file_operations; |
| 40 | static struct vm_operations_struct shm_vm_ops; |
| 41 | |
| 42 | static struct ipc_ids shm_ids; |
| 43 | |
| 44 | #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id)) |
| 45 | #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm) |
| 46 | #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id)) |
| 47 | #define shm_buildid(id, seq) \ |
| 48 | ipc_buildid(&shm_ids, id, seq) |
| 49 | |
| 50 | static int newseg (key_t key, int shmflg, size_t size); |
| 51 | static void shm_open (struct vm_area_struct *shmd); |
| 52 | static void shm_close (struct vm_area_struct *shmd); |
| 53 | #ifdef CONFIG_PROC_FS |
| 54 | static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data); |
| 55 | #endif |
| 56 | |
| 57 | size_t shm_ctlmax = SHMMAX; |
| 58 | size_t shm_ctlall = SHMALL; |
| 59 | int shm_ctlmni = SHMMNI; |
| 60 | |
| 61 | static int shm_tot; /* total number of shared memory pages */ |
| 62 | |
| 63 | void __init shm_init (void) |
| 64 | { |
| 65 | ipc_init_ids(&shm_ids, 1); |
| 66 | #ifdef CONFIG_PROC_FS |
| 67 | create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | static inline int shm_checkid(struct shmid_kernel *s, int id) |
| 72 | { |
| 73 | if (ipc_checkid(&shm_ids,&s->shm_perm,id)) |
| 74 | return -EIDRM; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static inline struct shmid_kernel *shm_rmid(int id) |
| 79 | { |
| 80 | return (struct shmid_kernel *)ipc_rmid(&shm_ids,id); |
| 81 | } |
| 82 | |
| 83 | static inline int shm_addid(struct shmid_kernel *shp) |
| 84 | { |
| 85 | return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | static inline void shm_inc (int id) { |
| 91 | struct shmid_kernel *shp; |
| 92 | |
| 93 | if(!(shp = shm_lock(id))) |
| 94 | BUG(); |
| 95 | shp->shm_atim = get_seconds(); |
| 96 | shp->shm_lprid = current->tgid; |
| 97 | shp->shm_nattch++; |
| 98 | shm_unlock(shp); |
| 99 | } |
| 100 | |
| 101 | /* This is called by fork, once for every shm attach. */ |
| 102 | static void shm_open (struct vm_area_struct *shmd) |
| 103 | { |
| 104 | shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * shm_destroy - free the struct shmid_kernel |
| 109 | * |
| 110 | * @shp: struct to free |
| 111 | * |
| 112 | * It has to be called with shp and shm_ids.sem locked, |
| 113 | * but returns with shp unlocked and freed. |
| 114 | */ |
| 115 | static void shm_destroy (struct shmid_kernel *shp) |
| 116 | { |
| 117 | shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 118 | shm_rmid (shp->id); |
| 119 | shm_unlock(shp); |
| 120 | if (!is_file_hugepages(shp->shm_file)) |
| 121 | shmem_lock(shp->shm_file, 0, shp->mlock_user); |
| 122 | else |
| 123 | user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size, |
| 124 | shp->mlock_user); |
| 125 | fput (shp->shm_file); |
| 126 | security_shm_free(shp); |
| 127 | ipc_rcu_putref(shp); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * remove the attach descriptor shmd. |
| 132 | * free memory for segment if it is marked destroyed. |
| 133 | * The descriptor has already been removed from the current->mm->mmap list |
| 134 | * and will later be kfree()d. |
| 135 | */ |
| 136 | static void shm_close (struct vm_area_struct *shmd) |
| 137 | { |
| 138 | struct file * file = shmd->vm_file; |
| 139 | int id = file->f_dentry->d_inode->i_ino; |
| 140 | struct shmid_kernel *shp; |
| 141 | |
| 142 | down (&shm_ids.sem); |
| 143 | /* remove from the list of attaches of the shm segment */ |
| 144 | if(!(shp = shm_lock(id))) |
| 145 | BUG(); |
| 146 | shp->shm_lprid = current->tgid; |
| 147 | shp->shm_dtim = get_seconds(); |
| 148 | shp->shm_nattch--; |
| 149 | if(shp->shm_nattch == 0 && |
| 150 | shp->shm_flags & SHM_DEST) |
| 151 | shm_destroy (shp); |
| 152 | else |
| 153 | shm_unlock(shp); |
| 154 | up (&shm_ids.sem); |
| 155 | } |
| 156 | |
| 157 | static int shm_mmap(struct file * file, struct vm_area_struct * vma) |
| 158 | { |
| 159 | file_accessed(file); |
| 160 | vma->vm_ops = &shm_vm_ops; |
| 161 | shm_inc(file->f_dentry->d_inode->i_ino); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static struct file_operations shm_file_operations = { |
| 166 | .mmap = shm_mmap |
| 167 | }; |
| 168 | |
| 169 | static struct vm_operations_struct shm_vm_ops = { |
| 170 | .open = shm_open, /* callback for a new vm-area open */ |
| 171 | .close = shm_close, /* callback for when the vm-area is released */ |
| 172 | .nopage = shmem_nopage, |
Andrew Morton | 6ade43f | 2005-08-01 21:11:45 -0700 | [diff] [blame^] | 173 | #if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .set_policy = shmem_set_policy, |
| 175 | .get_policy = shmem_get_policy, |
| 176 | #endif |
| 177 | }; |
| 178 | |
| 179 | static int newseg (key_t key, int shmflg, size_t size) |
| 180 | { |
| 181 | int error; |
| 182 | struct shmid_kernel *shp; |
| 183 | int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT; |
| 184 | struct file * file; |
| 185 | char name[13]; |
| 186 | int id; |
| 187 | |
| 188 | if (size < SHMMIN || size > shm_ctlmax) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | if (shm_tot + numpages >= shm_ctlall) |
| 192 | return -ENOSPC; |
| 193 | |
| 194 | shp = ipc_rcu_alloc(sizeof(*shp)); |
| 195 | if (!shp) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | shp->shm_perm.key = key; |
| 199 | shp->shm_flags = (shmflg & S_IRWXUGO); |
| 200 | shp->mlock_user = NULL; |
| 201 | |
| 202 | shp->shm_perm.security = NULL; |
| 203 | error = security_shm_alloc(shp); |
| 204 | if (error) { |
| 205 | ipc_rcu_putref(shp); |
| 206 | return error; |
| 207 | } |
| 208 | |
| 209 | if (shmflg & SHM_HUGETLB) { |
| 210 | /* hugetlb_zero_setup takes care of mlock user accounting */ |
| 211 | file = hugetlb_zero_setup(size); |
| 212 | shp->mlock_user = current->user; |
| 213 | } else { |
| 214 | sprintf (name, "SYSV%08x", key); |
| 215 | file = shmem_file_setup(name, size, VM_ACCOUNT); |
| 216 | } |
| 217 | error = PTR_ERR(file); |
| 218 | if (IS_ERR(file)) |
| 219 | goto no_file; |
| 220 | |
| 221 | error = -ENOSPC; |
| 222 | id = shm_addid(shp); |
| 223 | if(id == -1) |
| 224 | goto no_id; |
| 225 | |
| 226 | shp->shm_cprid = current->tgid; |
| 227 | shp->shm_lprid = 0; |
| 228 | shp->shm_atim = shp->shm_dtim = 0; |
| 229 | shp->shm_ctim = get_seconds(); |
| 230 | shp->shm_segsz = size; |
| 231 | shp->shm_nattch = 0; |
| 232 | shp->id = shm_buildid(id,shp->shm_perm.seq); |
| 233 | shp->shm_file = file; |
| 234 | file->f_dentry->d_inode->i_ino = shp->id; |
| 235 | if (shmflg & SHM_HUGETLB) |
| 236 | set_file_hugepages(file); |
| 237 | else |
| 238 | file->f_op = &shm_file_operations; |
| 239 | shm_tot += numpages; |
| 240 | shm_unlock(shp); |
| 241 | return shp->id; |
| 242 | |
| 243 | no_id: |
| 244 | fput(file); |
| 245 | no_file: |
| 246 | security_shm_free(shp); |
| 247 | ipc_rcu_putref(shp); |
| 248 | return error; |
| 249 | } |
| 250 | |
| 251 | asmlinkage long sys_shmget (key_t key, size_t size, int shmflg) |
| 252 | { |
| 253 | struct shmid_kernel *shp; |
| 254 | int err, id = 0; |
| 255 | |
| 256 | down(&shm_ids.sem); |
| 257 | if (key == IPC_PRIVATE) { |
| 258 | err = newseg(key, shmflg, size); |
| 259 | } else if ((id = ipc_findkey(&shm_ids, key)) == -1) { |
| 260 | if (!(shmflg & IPC_CREAT)) |
| 261 | err = -ENOENT; |
| 262 | else |
| 263 | err = newseg(key, shmflg, size); |
| 264 | } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) { |
| 265 | err = -EEXIST; |
| 266 | } else { |
| 267 | shp = shm_lock(id); |
| 268 | if(shp==NULL) |
| 269 | BUG(); |
| 270 | if (shp->shm_segsz < size) |
| 271 | err = -EINVAL; |
| 272 | else if (ipcperms(&shp->shm_perm, shmflg)) |
| 273 | err = -EACCES; |
| 274 | else { |
| 275 | int shmid = shm_buildid(id, shp->shm_perm.seq); |
| 276 | err = security_shm_associate(shp, shmflg); |
| 277 | if (!err) |
| 278 | err = shmid; |
| 279 | } |
| 280 | shm_unlock(shp); |
| 281 | } |
| 282 | up(&shm_ids.sem); |
| 283 | |
| 284 | return err; |
| 285 | } |
| 286 | |
| 287 | static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version) |
| 288 | { |
| 289 | switch(version) { |
| 290 | case IPC_64: |
| 291 | return copy_to_user(buf, in, sizeof(*in)); |
| 292 | case IPC_OLD: |
| 293 | { |
| 294 | struct shmid_ds out; |
| 295 | |
| 296 | ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm); |
| 297 | out.shm_segsz = in->shm_segsz; |
| 298 | out.shm_atime = in->shm_atime; |
| 299 | out.shm_dtime = in->shm_dtime; |
| 300 | out.shm_ctime = in->shm_ctime; |
| 301 | out.shm_cpid = in->shm_cpid; |
| 302 | out.shm_lpid = in->shm_lpid; |
| 303 | out.shm_nattch = in->shm_nattch; |
| 304 | |
| 305 | return copy_to_user(buf, &out, sizeof(out)); |
| 306 | } |
| 307 | default: |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | struct shm_setbuf { |
| 313 | uid_t uid; |
| 314 | gid_t gid; |
| 315 | mode_t mode; |
| 316 | }; |
| 317 | |
| 318 | static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version) |
| 319 | { |
| 320 | switch(version) { |
| 321 | case IPC_64: |
| 322 | { |
| 323 | struct shmid64_ds tbuf; |
| 324 | |
| 325 | if (copy_from_user(&tbuf, buf, sizeof(tbuf))) |
| 326 | return -EFAULT; |
| 327 | |
| 328 | out->uid = tbuf.shm_perm.uid; |
| 329 | out->gid = tbuf.shm_perm.gid; |
| 330 | out->mode = tbuf.shm_flags; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | case IPC_OLD: |
| 335 | { |
| 336 | struct shmid_ds tbuf_old; |
| 337 | |
| 338 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
| 339 | return -EFAULT; |
| 340 | |
| 341 | out->uid = tbuf_old.shm_perm.uid; |
| 342 | out->gid = tbuf_old.shm_perm.gid; |
| 343 | out->mode = tbuf_old.shm_flags; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | default: |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version) |
| 353 | { |
| 354 | switch(version) { |
| 355 | case IPC_64: |
| 356 | return copy_to_user(buf, in, sizeof(*in)); |
| 357 | case IPC_OLD: |
| 358 | { |
| 359 | struct shminfo out; |
| 360 | |
| 361 | if(in->shmmax > INT_MAX) |
| 362 | out.shmmax = INT_MAX; |
| 363 | else |
| 364 | out.shmmax = (int)in->shmmax; |
| 365 | |
| 366 | out.shmmin = in->shmmin; |
| 367 | out.shmmni = in->shmmni; |
| 368 | out.shmseg = in->shmseg; |
| 369 | out.shmall = in->shmall; |
| 370 | |
| 371 | return copy_to_user(buf, &out, sizeof(out)); |
| 372 | } |
| 373 | default: |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static void shm_get_stat(unsigned long *rss, unsigned long *swp) |
| 379 | { |
| 380 | int i; |
| 381 | |
| 382 | *rss = 0; |
| 383 | *swp = 0; |
| 384 | |
| 385 | for (i = 0; i <= shm_ids.max_id; i++) { |
| 386 | struct shmid_kernel *shp; |
| 387 | struct inode *inode; |
| 388 | |
| 389 | shp = shm_get(i); |
| 390 | if(!shp) |
| 391 | continue; |
| 392 | |
| 393 | inode = shp->shm_file->f_dentry->d_inode; |
| 394 | |
| 395 | if (is_file_hugepages(shp->shm_file)) { |
| 396 | struct address_space *mapping = inode->i_mapping; |
| 397 | *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages; |
| 398 | } else { |
| 399 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 400 | spin_lock(&info->lock); |
| 401 | *rss += inode->i_mapping->nrpages; |
| 402 | *swp += info->swapped; |
| 403 | spin_unlock(&info->lock); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf) |
| 409 | { |
| 410 | struct shm_setbuf setbuf; |
| 411 | struct shmid_kernel *shp; |
| 412 | int err, version; |
| 413 | |
| 414 | if (cmd < 0 || shmid < 0) { |
| 415 | err = -EINVAL; |
| 416 | goto out; |
| 417 | } |
| 418 | |
| 419 | version = ipc_parse_version(&cmd); |
| 420 | |
| 421 | switch (cmd) { /* replace with proc interface ? */ |
| 422 | case IPC_INFO: |
| 423 | { |
| 424 | struct shminfo64 shminfo; |
| 425 | |
| 426 | err = security_shm_shmctl(NULL, cmd); |
| 427 | if (err) |
| 428 | return err; |
| 429 | |
| 430 | memset(&shminfo,0,sizeof(shminfo)); |
| 431 | shminfo.shmmni = shminfo.shmseg = shm_ctlmni; |
| 432 | shminfo.shmmax = shm_ctlmax; |
| 433 | shminfo.shmall = shm_ctlall; |
| 434 | |
| 435 | shminfo.shmmin = SHMMIN; |
| 436 | if(copy_shminfo_to_user (buf, &shminfo, version)) |
| 437 | return -EFAULT; |
| 438 | /* reading a integer is always atomic */ |
| 439 | err= shm_ids.max_id; |
| 440 | if(err<0) |
| 441 | err = 0; |
| 442 | goto out; |
| 443 | } |
| 444 | case SHM_INFO: |
| 445 | { |
| 446 | struct shm_info shm_info; |
| 447 | |
| 448 | err = security_shm_shmctl(NULL, cmd); |
| 449 | if (err) |
| 450 | return err; |
| 451 | |
| 452 | memset(&shm_info,0,sizeof(shm_info)); |
| 453 | down(&shm_ids.sem); |
| 454 | shm_info.used_ids = shm_ids.in_use; |
| 455 | shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp); |
| 456 | shm_info.shm_tot = shm_tot; |
| 457 | shm_info.swap_attempts = 0; |
| 458 | shm_info.swap_successes = 0; |
| 459 | err = shm_ids.max_id; |
| 460 | up(&shm_ids.sem); |
| 461 | if(copy_to_user (buf, &shm_info, sizeof(shm_info))) { |
| 462 | err = -EFAULT; |
| 463 | goto out; |
| 464 | } |
| 465 | |
| 466 | err = err < 0 ? 0 : err; |
| 467 | goto out; |
| 468 | } |
| 469 | case SHM_STAT: |
| 470 | case IPC_STAT: |
| 471 | { |
| 472 | struct shmid64_ds tbuf; |
| 473 | int result; |
| 474 | memset(&tbuf, 0, sizeof(tbuf)); |
| 475 | shp = shm_lock(shmid); |
| 476 | if(shp==NULL) { |
| 477 | err = -EINVAL; |
| 478 | goto out; |
| 479 | } else if(cmd==SHM_STAT) { |
| 480 | err = -EINVAL; |
| 481 | if (shmid > shm_ids.max_id) |
| 482 | goto out_unlock; |
| 483 | result = shm_buildid(shmid, shp->shm_perm.seq); |
| 484 | } else { |
| 485 | err = shm_checkid(shp,shmid); |
| 486 | if(err) |
| 487 | goto out_unlock; |
| 488 | result = 0; |
| 489 | } |
| 490 | err=-EACCES; |
| 491 | if (ipcperms (&shp->shm_perm, S_IRUGO)) |
| 492 | goto out_unlock; |
| 493 | err = security_shm_shmctl(shp, cmd); |
| 494 | if (err) |
| 495 | goto out_unlock; |
| 496 | kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm); |
| 497 | tbuf.shm_segsz = shp->shm_segsz; |
| 498 | tbuf.shm_atime = shp->shm_atim; |
| 499 | tbuf.shm_dtime = shp->shm_dtim; |
| 500 | tbuf.shm_ctime = shp->shm_ctim; |
| 501 | tbuf.shm_cpid = shp->shm_cprid; |
| 502 | tbuf.shm_lpid = shp->shm_lprid; |
| 503 | if (!is_file_hugepages(shp->shm_file)) |
| 504 | tbuf.shm_nattch = shp->shm_nattch; |
| 505 | else |
| 506 | tbuf.shm_nattch = file_count(shp->shm_file) - 1; |
| 507 | shm_unlock(shp); |
| 508 | if(copy_shmid_to_user (buf, &tbuf, version)) |
| 509 | err = -EFAULT; |
| 510 | else |
| 511 | err = result; |
| 512 | goto out; |
| 513 | } |
| 514 | case SHM_LOCK: |
| 515 | case SHM_UNLOCK: |
| 516 | { |
| 517 | shp = shm_lock(shmid); |
| 518 | if(shp==NULL) { |
| 519 | err = -EINVAL; |
| 520 | goto out; |
| 521 | } |
| 522 | err = shm_checkid(shp,shmid); |
| 523 | if(err) |
| 524 | goto out_unlock; |
| 525 | |
| 526 | if (!capable(CAP_IPC_LOCK)) { |
| 527 | err = -EPERM; |
| 528 | if (current->euid != shp->shm_perm.uid && |
| 529 | current->euid != shp->shm_perm.cuid) |
| 530 | goto out_unlock; |
| 531 | if (cmd == SHM_LOCK && |
| 532 | !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur) |
| 533 | goto out_unlock; |
| 534 | } |
| 535 | |
| 536 | err = security_shm_shmctl(shp, cmd); |
| 537 | if (err) |
| 538 | goto out_unlock; |
| 539 | |
| 540 | if(cmd==SHM_LOCK) { |
| 541 | struct user_struct * user = current->user; |
| 542 | if (!is_file_hugepages(shp->shm_file)) { |
| 543 | err = shmem_lock(shp->shm_file, 1, user); |
| 544 | if (!err) { |
| 545 | shp->shm_flags |= SHM_LOCKED; |
| 546 | shp->mlock_user = user; |
| 547 | } |
| 548 | } |
| 549 | } else if (!is_file_hugepages(shp->shm_file)) { |
| 550 | shmem_lock(shp->shm_file, 0, shp->mlock_user); |
| 551 | shp->shm_flags &= ~SHM_LOCKED; |
| 552 | shp->mlock_user = NULL; |
| 553 | } |
| 554 | shm_unlock(shp); |
| 555 | goto out; |
| 556 | } |
| 557 | case IPC_RMID: |
| 558 | { |
| 559 | /* |
| 560 | * We cannot simply remove the file. The SVID states |
| 561 | * that the block remains until the last person |
| 562 | * detaches from it, then is deleted. A shmat() on |
| 563 | * an RMID segment is legal in older Linux and if |
| 564 | * we change it apps break... |
| 565 | * |
| 566 | * Instead we set a destroyed flag, and then blow |
| 567 | * the name away when the usage hits zero. |
| 568 | */ |
| 569 | down(&shm_ids.sem); |
| 570 | shp = shm_lock(shmid); |
| 571 | err = -EINVAL; |
| 572 | if (shp == NULL) |
| 573 | goto out_up; |
| 574 | err = shm_checkid(shp, shmid); |
| 575 | if(err) |
| 576 | goto out_unlock_up; |
| 577 | |
| 578 | if (current->euid != shp->shm_perm.uid && |
| 579 | current->euid != shp->shm_perm.cuid && |
| 580 | !capable(CAP_SYS_ADMIN)) { |
| 581 | err=-EPERM; |
| 582 | goto out_unlock_up; |
| 583 | } |
| 584 | |
| 585 | err = security_shm_shmctl(shp, cmd); |
| 586 | if (err) |
| 587 | goto out_unlock_up; |
| 588 | |
| 589 | if (shp->shm_nattch){ |
| 590 | shp->shm_flags |= SHM_DEST; |
| 591 | /* Do not find it any more */ |
| 592 | shp->shm_perm.key = IPC_PRIVATE; |
| 593 | shm_unlock(shp); |
| 594 | } else |
| 595 | shm_destroy (shp); |
| 596 | up(&shm_ids.sem); |
| 597 | goto out; |
| 598 | } |
| 599 | |
| 600 | case IPC_SET: |
| 601 | { |
| 602 | if (copy_shmid_from_user (&setbuf, buf, version)) { |
| 603 | err = -EFAULT; |
| 604 | goto out; |
| 605 | } |
| 606 | if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode))) |
| 607 | return err; |
| 608 | down(&shm_ids.sem); |
| 609 | shp = shm_lock(shmid); |
| 610 | err=-EINVAL; |
| 611 | if(shp==NULL) |
| 612 | goto out_up; |
| 613 | err = shm_checkid(shp,shmid); |
| 614 | if(err) |
| 615 | goto out_unlock_up; |
| 616 | err=-EPERM; |
| 617 | if (current->euid != shp->shm_perm.uid && |
| 618 | current->euid != shp->shm_perm.cuid && |
| 619 | !capable(CAP_SYS_ADMIN)) { |
| 620 | goto out_unlock_up; |
| 621 | } |
| 622 | |
| 623 | err = security_shm_shmctl(shp, cmd); |
| 624 | if (err) |
| 625 | goto out_unlock_up; |
| 626 | |
| 627 | shp->shm_perm.uid = setbuf.uid; |
| 628 | shp->shm_perm.gid = setbuf.gid; |
| 629 | shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO) |
| 630 | | (setbuf.mode & S_IRWXUGO); |
| 631 | shp->shm_ctim = get_seconds(); |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | default: |
| 636 | err = -EINVAL; |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | err = 0; |
| 641 | out_unlock_up: |
| 642 | shm_unlock(shp); |
| 643 | out_up: |
| 644 | up(&shm_ids.sem); |
| 645 | goto out; |
| 646 | out_unlock: |
| 647 | shm_unlock(shp); |
| 648 | out: |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists. |
| 654 | * |
| 655 | * NOTE! Despite the name, this is NOT a direct system call entrypoint. The |
| 656 | * "raddr" thing points to kernel space, and there has to be a wrapper around |
| 657 | * this. |
| 658 | */ |
| 659 | long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr) |
| 660 | { |
| 661 | struct shmid_kernel *shp; |
| 662 | unsigned long addr; |
| 663 | unsigned long size; |
| 664 | struct file * file; |
| 665 | int err; |
| 666 | unsigned long flags; |
| 667 | unsigned long prot; |
| 668 | unsigned long o_flags; |
| 669 | int acc_mode; |
| 670 | void *user_addr; |
| 671 | |
| 672 | if (shmid < 0) { |
| 673 | err = -EINVAL; |
| 674 | goto out; |
| 675 | } else if ((addr = (ulong)shmaddr)) { |
| 676 | if (addr & (SHMLBA-1)) { |
| 677 | if (shmflg & SHM_RND) |
| 678 | addr &= ~(SHMLBA-1); /* round down */ |
| 679 | else |
| 680 | #ifndef __ARCH_FORCE_SHMLBA |
| 681 | if (addr & ~PAGE_MASK) |
| 682 | #endif |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | flags = MAP_SHARED | MAP_FIXED; |
| 686 | } else { |
| 687 | if ((shmflg & SHM_REMAP)) |
| 688 | return -EINVAL; |
| 689 | |
| 690 | flags = MAP_SHARED; |
| 691 | } |
| 692 | |
| 693 | if (shmflg & SHM_RDONLY) { |
| 694 | prot = PROT_READ; |
| 695 | o_flags = O_RDONLY; |
| 696 | acc_mode = S_IRUGO; |
| 697 | } else { |
| 698 | prot = PROT_READ | PROT_WRITE; |
| 699 | o_flags = O_RDWR; |
| 700 | acc_mode = S_IRUGO | S_IWUGO; |
| 701 | } |
| 702 | if (shmflg & SHM_EXEC) { |
| 703 | prot |= PROT_EXEC; |
| 704 | acc_mode |= S_IXUGO; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * We cannot rely on the fs check since SYSV IPC does have an |
| 709 | * additional creator id... |
| 710 | */ |
| 711 | shp = shm_lock(shmid); |
| 712 | if(shp == NULL) { |
| 713 | err = -EINVAL; |
| 714 | goto out; |
| 715 | } |
| 716 | err = shm_checkid(shp,shmid); |
| 717 | if (err) { |
| 718 | shm_unlock(shp); |
| 719 | goto out; |
| 720 | } |
| 721 | if (ipcperms(&shp->shm_perm, acc_mode)) { |
| 722 | shm_unlock(shp); |
| 723 | err = -EACCES; |
| 724 | goto out; |
| 725 | } |
| 726 | |
| 727 | err = security_shm_shmat(shp, shmaddr, shmflg); |
| 728 | if (err) { |
| 729 | shm_unlock(shp); |
| 730 | return err; |
| 731 | } |
| 732 | |
| 733 | file = shp->shm_file; |
| 734 | size = i_size_read(file->f_dentry->d_inode); |
| 735 | shp->shm_nattch++; |
| 736 | shm_unlock(shp); |
| 737 | |
| 738 | down_write(¤t->mm->mmap_sem); |
| 739 | if (addr && !(shmflg & SHM_REMAP)) { |
| 740 | user_addr = ERR_PTR(-EINVAL); |
| 741 | if (find_vma_intersection(current->mm, addr, addr + size)) |
| 742 | goto invalid; |
| 743 | /* |
| 744 | * If shm segment goes below stack, make sure there is some |
| 745 | * space left for the stack to grow (at least 4 pages). |
| 746 | */ |
| 747 | if (addr < current->mm->start_stack && |
| 748 | addr > current->mm->start_stack - size - PAGE_SIZE * 5) |
| 749 | goto invalid; |
| 750 | } |
| 751 | |
| 752 | user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0); |
| 753 | |
| 754 | invalid: |
| 755 | up_write(¤t->mm->mmap_sem); |
| 756 | |
| 757 | down (&shm_ids.sem); |
| 758 | if(!(shp = shm_lock(shmid))) |
| 759 | BUG(); |
| 760 | shp->shm_nattch--; |
| 761 | if(shp->shm_nattch == 0 && |
| 762 | shp->shm_flags & SHM_DEST) |
| 763 | shm_destroy (shp); |
| 764 | else |
| 765 | shm_unlock(shp); |
| 766 | up (&shm_ids.sem); |
| 767 | |
| 768 | *raddr = (unsigned long) user_addr; |
| 769 | err = 0; |
| 770 | if (IS_ERR(user_addr)) |
| 771 | err = PTR_ERR(user_addr); |
| 772 | out: |
| 773 | return err; |
| 774 | } |
| 775 | |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 776 | asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg) |
| 777 | { |
| 778 | unsigned long ret; |
| 779 | long err; |
| 780 | |
| 781 | err = do_shmat(shmid, shmaddr, shmflg, &ret); |
| 782 | if (err) |
| 783 | return err; |
| 784 | force_successful_syscall_return(); |
| 785 | return (long)ret; |
| 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | /* |
| 789 | * detach and kill segment if marked destroyed. |
| 790 | * The work is done in shm_close. |
| 791 | */ |
| 792 | asmlinkage long sys_shmdt(char __user *shmaddr) |
| 793 | { |
| 794 | struct mm_struct *mm = current->mm; |
| 795 | struct vm_area_struct *vma, *next; |
| 796 | unsigned long addr = (unsigned long)shmaddr; |
| 797 | loff_t size = 0; |
| 798 | int retval = -EINVAL; |
| 799 | |
| 800 | down_write(&mm->mmap_sem); |
| 801 | |
| 802 | /* |
| 803 | * This function tries to be smart and unmap shm segments that |
| 804 | * were modified by partial mlock or munmap calls: |
| 805 | * - It first determines the size of the shm segment that should be |
| 806 | * unmapped: It searches for a vma that is backed by shm and that |
| 807 | * started at address shmaddr. It records it's size and then unmaps |
| 808 | * it. |
| 809 | * - Then it unmaps all shm vmas that started at shmaddr and that |
| 810 | * are within the initially determined size. |
| 811 | * Errors from do_munmap are ignored: the function only fails if |
| 812 | * it's called with invalid parameters or if it's called to unmap |
| 813 | * a part of a vma. Both calls in this function are for full vmas, |
| 814 | * the parameters are directly copied from the vma itself and always |
| 815 | * valid - therefore do_munmap cannot fail. (famous last words?) |
| 816 | */ |
| 817 | /* |
| 818 | * If it had been mremap()'d, the starting address would not |
| 819 | * match the usual checks anyway. So assume all vma's are |
| 820 | * above the starting address given. |
| 821 | */ |
| 822 | vma = find_vma(mm, addr); |
| 823 | |
| 824 | while (vma) { |
| 825 | next = vma->vm_next; |
| 826 | |
| 827 | /* |
| 828 | * Check if the starting address would match, i.e. it's |
| 829 | * a fragment created by mprotect() and/or munmap(), or it |
| 830 | * otherwise it starts at this address with no hassles. |
| 831 | */ |
| 832 | if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) && |
| 833 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { |
| 834 | |
| 835 | |
| 836 | size = vma->vm_file->f_dentry->d_inode->i_size; |
| 837 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
| 838 | /* |
| 839 | * We discovered the size of the shm segment, so |
| 840 | * break out of here and fall through to the next |
| 841 | * loop that uses the size information to stop |
| 842 | * searching for matching vma's. |
| 843 | */ |
| 844 | retval = 0; |
| 845 | vma = next; |
| 846 | break; |
| 847 | } |
| 848 | vma = next; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * We need look no further than the maximum address a fragment |
| 853 | * could possibly have landed at. Also cast things to loff_t to |
| 854 | * prevent overflows and make comparisions vs. equal-width types. |
| 855 | */ |
| 856 | while (vma && (loff_t)(vma->vm_end - addr) <= size) { |
| 857 | next = vma->vm_next; |
| 858 | |
| 859 | /* finding a matching vma now does not alter retval */ |
| 860 | if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) && |
| 861 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) |
| 862 | |
| 863 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
| 864 | vma = next; |
| 865 | } |
| 866 | |
| 867 | up_write(&mm->mmap_sem); |
| 868 | return retval; |
| 869 | } |
| 870 | |
| 871 | #ifdef CONFIG_PROC_FS |
| 872 | static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data) |
| 873 | { |
| 874 | off_t pos = 0; |
| 875 | off_t begin = 0; |
| 876 | int i, len = 0; |
| 877 | |
| 878 | down(&shm_ids.sem); |
| 879 | len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n"); |
| 880 | |
| 881 | for(i = 0; i <= shm_ids.max_id; i++) { |
| 882 | struct shmid_kernel* shp; |
| 883 | |
| 884 | shp = shm_lock(i); |
| 885 | if(shp!=NULL) { |
| 886 | #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
| 887 | #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
| 888 | char *format; |
| 889 | |
| 890 | if (sizeof(size_t) <= sizeof(int)) |
| 891 | format = SMALL_STRING; |
| 892 | else |
| 893 | format = BIG_STRING; |
| 894 | len += sprintf(buffer + len, format, |
| 895 | shp->shm_perm.key, |
| 896 | shm_buildid(i, shp->shm_perm.seq), |
| 897 | shp->shm_flags, |
| 898 | shp->shm_segsz, |
| 899 | shp->shm_cprid, |
| 900 | shp->shm_lprid, |
| 901 | is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch, |
| 902 | shp->shm_perm.uid, |
| 903 | shp->shm_perm.gid, |
| 904 | shp->shm_perm.cuid, |
| 905 | shp->shm_perm.cgid, |
| 906 | shp->shm_atim, |
| 907 | shp->shm_dtim, |
| 908 | shp->shm_ctim); |
| 909 | shm_unlock(shp); |
| 910 | |
| 911 | pos += len; |
| 912 | if(pos < offset) { |
| 913 | len = 0; |
| 914 | begin = pos; |
| 915 | } |
| 916 | if(pos > offset + length) |
| 917 | goto done; |
| 918 | } |
| 919 | } |
| 920 | *eof = 1; |
| 921 | done: |
| 922 | up(&shm_ids.sem); |
| 923 | *start = buffer + (offset - begin); |
| 924 | len -= (offset - begin); |
| 925 | if(len > length) |
| 926 | len = length; |
| 927 | if(len < 0) |
| 928 | len = 0; |
| 929 | return len; |
| 930 | } |
| 931 | #endif |