b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * linux/ipc/shm.c |
| 4 | * Copyright (C) 1992, 1993 Krishna Balasubramanian |
| 5 | * Many improvements/fixes by Bruno Haible. |
| 6 | * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994. |
| 7 | * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli. |
| 8 | * |
| 9 | * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com> |
| 10 | * BIGMEM support, Andrea Arcangeli <andrea@suse.de> |
| 11 | * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr> |
| 12 | * HIGHMEM support, Ingo Molnar <mingo@redhat.com> |
| 13 | * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com> |
| 14 | * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com> |
| 15 | * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com> |
| 16 | * |
| 17 | * support for audit of ipc object properties and permission changes |
| 18 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> |
| 19 | * |
| 20 | * namespaces support |
| 21 | * OpenVZ, SWsoft Inc. |
| 22 | * Pavel Emelianov <xemul@openvz.org> |
| 23 | * |
| 24 | * Better ipc lock (kern_ipc_perm.lock) handling |
| 25 | * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/hugetlb.h> |
| 31 | #include <linux/shm.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/file.h> |
| 34 | #include <linux/mman.h> |
| 35 | #include <linux/shmem_fs.h> |
| 36 | #include <linux/security.h> |
| 37 | #include <linux/syscalls.h> |
| 38 | #include <linux/audit.h> |
| 39 | #include <linux/capability.h> |
| 40 | #include <linux/ptrace.h> |
| 41 | #include <linux/seq_file.h> |
| 42 | #include <linux/rwsem.h> |
| 43 | #include <linux/nsproxy.h> |
| 44 | #include <linux/mount.h> |
| 45 | #include <linux/ipc_namespace.h> |
| 46 | #include <linux/rhashtable.h> |
| 47 | |
| 48 | #include <linux/uaccess.h> |
| 49 | |
| 50 | #include "util.h" |
| 51 | |
| 52 | struct shmid_kernel /* private to the kernel */ |
| 53 | { |
| 54 | struct kern_ipc_perm shm_perm; |
| 55 | struct file *shm_file; |
| 56 | unsigned long shm_nattch; |
| 57 | unsigned long shm_segsz; |
| 58 | time64_t shm_atim; |
| 59 | time64_t shm_dtim; |
| 60 | time64_t shm_ctim; |
| 61 | struct pid *shm_cprid; |
| 62 | struct pid *shm_lprid; |
| 63 | struct user_struct *mlock_user; |
| 64 | |
| 65 | /* |
| 66 | * The task created the shm object, for |
| 67 | * task_lock(shp->shm_creator) |
| 68 | */ |
| 69 | struct task_struct *shm_creator; |
| 70 | |
| 71 | /* |
| 72 | * List by creator. task_lock(->shm_creator) required for read/write. |
| 73 | * If list_empty(), then the creator is dead already. |
| 74 | */ |
| 75 | struct list_head shm_clist; |
| 76 | struct ipc_namespace *ns; |
| 77 | } __randomize_layout; |
| 78 | |
| 79 | /* shm_mode upper byte flags */ |
| 80 | #define SHM_DEST 01000 /* segment will be destroyed on last detach */ |
| 81 | #define SHM_LOCKED 02000 /* segment will not be swapped */ |
| 82 | |
| 83 | struct shm_file_data { |
| 84 | int id; |
| 85 | struct ipc_namespace *ns; |
| 86 | struct file *file; |
| 87 | const struct vm_operations_struct *vm_ops; |
| 88 | }; |
| 89 | |
| 90 | #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data)) |
| 91 | |
| 92 | static const struct file_operations shm_file_operations; |
| 93 | static const struct vm_operations_struct shm_vm_ops; |
| 94 | |
| 95 | #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS]) |
| 96 | |
| 97 | #define shm_unlock(shp) \ |
| 98 | ipc_unlock(&(shp)->shm_perm) |
| 99 | |
| 100 | static int newseg(struct ipc_namespace *, struct ipc_params *); |
| 101 | static void shm_open(struct vm_area_struct *vma); |
| 102 | static void shm_close(struct vm_area_struct *vma); |
| 103 | static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp); |
| 104 | #ifdef CONFIG_PROC_FS |
| 105 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it); |
| 106 | #endif |
| 107 | |
| 108 | void shm_init_ns(struct ipc_namespace *ns) |
| 109 | { |
| 110 | ns->shm_ctlmax = SHMMAX; |
| 111 | ns->shm_ctlall = SHMALL; |
| 112 | ns->shm_ctlmni = SHMMNI; |
| 113 | ns->shm_rmid_forced = 0; |
| 114 | ns->shm_tot = 0; |
| 115 | ipc_init_ids(&shm_ids(ns)); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Called with shm_ids.rwsem (writer) and the shp structure locked. |
| 120 | * Only shm_ids.rwsem remains locked on exit. |
| 121 | */ |
| 122 | static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) |
| 123 | { |
| 124 | struct shmid_kernel *shp; |
| 125 | |
| 126 | shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 127 | WARN_ON(ns != shp->ns); |
| 128 | |
| 129 | if (shp->shm_nattch) { |
| 130 | shp->shm_perm.mode |= SHM_DEST; |
| 131 | /* Do not find it any more */ |
| 132 | ipc_set_key_private(&shm_ids(ns), &shp->shm_perm); |
| 133 | shm_unlock(shp); |
| 134 | } else |
| 135 | shm_destroy(ns, shp); |
| 136 | } |
| 137 | |
| 138 | #ifdef CONFIG_IPC_NS |
| 139 | void shm_exit_ns(struct ipc_namespace *ns) |
| 140 | { |
| 141 | free_ipcs(ns, &shm_ids(ns), do_shm_rmid); |
| 142 | idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr); |
| 143 | rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht); |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | static int __init ipc_ns_init(void) |
| 148 | { |
| 149 | shm_init_ns(&init_ipc_ns); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | pure_initcall(ipc_ns_init); |
| 154 | |
| 155 | void __init shm_init(void) |
| 156 | { |
| 157 | if (IS_ENABLED(CONFIG_PROC_STRIPPED)) |
| 158 | return; |
| 159 | ipc_init_proc_interface("sysvipc/shm", |
| 160 | #if BITS_PER_LONG <= 32 |
| 161 | " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n", |
| 162 | #else |
| 163 | " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n", |
| 164 | #endif |
| 165 | IPC_SHM_IDS, sysvipc_shm_proc_show); |
| 166 | } |
| 167 | |
| 168 | static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id) |
| 169 | { |
| 170 | struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id); |
| 171 | |
| 172 | if (IS_ERR(ipcp)) |
| 173 | return ERR_CAST(ipcp); |
| 174 | |
| 175 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
| 176 | } |
| 177 | |
| 178 | static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id) |
| 179 | { |
| 180 | struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id); |
| 181 | |
| 182 | if (IS_ERR(ipcp)) |
| 183 | return ERR_CAST(ipcp); |
| 184 | |
| 185 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * shm_lock_(check_) routines are called in the paths where the rwsem |
| 190 | * is not necessarily held. |
| 191 | */ |
| 192 | static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id) |
| 193 | { |
| 194 | struct kern_ipc_perm *ipcp; |
| 195 | |
| 196 | rcu_read_lock(); |
| 197 | ipcp = ipc_obtain_object_idr(&shm_ids(ns), id); |
| 198 | if (IS_ERR(ipcp)) |
| 199 | goto err; |
| 200 | |
| 201 | ipc_lock_object(ipcp); |
| 202 | /* |
| 203 | * ipc_rmid() may have already freed the ID while ipc_lock_object() |
| 204 | * was spinning: here verify that the structure is still valid. |
| 205 | * Upon races with RMID, return -EIDRM, thus indicating that |
| 206 | * the ID points to a removed identifier. |
| 207 | */ |
| 208 | if (ipc_valid_object(ipcp)) { |
| 209 | /* return a locked ipc object upon success */ |
| 210 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
| 211 | } |
| 212 | |
| 213 | ipc_unlock_object(ipcp); |
| 214 | ipcp = ERR_PTR(-EIDRM); |
| 215 | err: |
| 216 | rcu_read_unlock(); |
| 217 | /* |
| 218 | * Callers of shm_lock() must validate the status of the returned ipc |
| 219 | * object pointer and error out as appropriate. |
| 220 | */ |
| 221 | return ERR_CAST(ipcp); |
| 222 | } |
| 223 | |
| 224 | static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp) |
| 225 | { |
| 226 | rcu_read_lock(); |
| 227 | ipc_lock_object(&ipcp->shm_perm); |
| 228 | } |
| 229 | |
| 230 | static void shm_rcu_free(struct rcu_head *head) |
| 231 | { |
| 232 | struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm, |
| 233 | rcu); |
| 234 | struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel, |
| 235 | shm_perm); |
| 236 | security_shm_free(&shp->shm_perm); |
| 237 | kvfree(shp); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * It has to be called with shp locked. |
| 242 | * It must be called before ipc_rmid() |
| 243 | */ |
| 244 | static inline void shm_clist_rm(struct shmid_kernel *shp) |
| 245 | { |
| 246 | struct task_struct *creator; |
| 247 | |
| 248 | /* ensure that shm_creator does not disappear */ |
| 249 | rcu_read_lock(); |
| 250 | |
| 251 | /* |
| 252 | * A concurrent exit_shm may do a list_del_init() as well. |
| 253 | * Just do nothing if exit_shm already did the work |
| 254 | */ |
| 255 | if (!list_empty(&shp->shm_clist)) { |
| 256 | /* |
| 257 | * shp->shm_creator is guaranteed to be valid *only* |
| 258 | * if shp->shm_clist is not empty. |
| 259 | */ |
| 260 | creator = shp->shm_creator; |
| 261 | |
| 262 | task_lock(creator); |
| 263 | /* |
| 264 | * list_del_init() is a nop if the entry was already removed |
| 265 | * from the list. |
| 266 | */ |
| 267 | list_del_init(&shp->shm_clist); |
| 268 | task_unlock(creator); |
| 269 | } |
| 270 | rcu_read_unlock(); |
| 271 | } |
| 272 | |
| 273 | static inline void shm_rmid(struct shmid_kernel *s) |
| 274 | { |
| 275 | shm_clist_rm(s); |
| 276 | ipc_rmid(&shm_ids(s->ns), &s->shm_perm); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static int __shm_open(struct vm_area_struct *vma) |
| 281 | { |
| 282 | struct file *file = vma->vm_file; |
| 283 | struct shm_file_data *sfd = shm_file_data(file); |
| 284 | struct shmid_kernel *shp; |
| 285 | |
| 286 | shp = shm_lock(sfd->ns, sfd->id); |
| 287 | |
| 288 | if (IS_ERR(shp)) |
| 289 | return PTR_ERR(shp); |
| 290 | |
| 291 | if (shp->shm_file != sfd->file) { |
| 292 | /* ID was reused */ |
| 293 | shm_unlock(shp); |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | shp->shm_atim = ktime_get_real_seconds(); |
| 298 | ipc_update_pid(&shp->shm_lprid, task_tgid(current)); |
| 299 | shp->shm_nattch++; |
| 300 | shm_unlock(shp); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* This is called by fork, once for every shm attach. */ |
| 305 | static void shm_open(struct vm_area_struct *vma) |
| 306 | { |
| 307 | int err = __shm_open(vma); |
| 308 | /* |
| 309 | * We raced in the idr lookup or with shm_destroy(). |
| 310 | * Either way, the ID is busted. |
| 311 | */ |
| 312 | WARN_ON_ONCE(err); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * shm_destroy - free the struct shmid_kernel |
| 317 | * |
| 318 | * @ns: namespace |
| 319 | * @shp: struct to free |
| 320 | * |
| 321 | * It has to be called with shp and shm_ids.rwsem (writer) locked, |
| 322 | * but returns with shp unlocked and freed. |
| 323 | */ |
| 324 | static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp) |
| 325 | { |
| 326 | struct file *shm_file; |
| 327 | |
| 328 | shm_file = shp->shm_file; |
| 329 | shp->shm_file = NULL; |
| 330 | ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 331 | shm_rmid(shp); |
| 332 | shm_unlock(shp); |
| 333 | if (!is_file_hugepages(shm_file)) |
| 334 | shmem_lock(shm_file, 0, shp->mlock_user); |
| 335 | else if (shp->mlock_user) |
| 336 | user_shm_unlock(i_size_read(file_inode(shm_file)), |
| 337 | shp->mlock_user); |
| 338 | fput(shm_file); |
| 339 | ipc_update_pid(&shp->shm_cprid, NULL); |
| 340 | ipc_update_pid(&shp->shm_lprid, NULL); |
| 341 | ipc_rcu_putref(&shp->shm_perm, shm_rcu_free); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * shm_may_destroy - identifies whether shm segment should be destroyed now |
| 346 | * |
| 347 | * Returns true if and only if there are no active users of the segment and |
| 348 | * one of the following is true: |
| 349 | * |
| 350 | * 1) shmctl(id, IPC_RMID, NULL) was called for this shp |
| 351 | * |
| 352 | * 2) sysctl kernel.shm_rmid_forced is set to 1. |
| 353 | */ |
| 354 | static bool shm_may_destroy(struct shmid_kernel *shp) |
| 355 | { |
| 356 | return (shp->shm_nattch == 0) && |
| 357 | (shp->ns->shm_rmid_forced || |
| 358 | (shp->shm_perm.mode & SHM_DEST)); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * remove the attach descriptor vma. |
| 363 | * free memory for segment if it is marked destroyed. |
| 364 | * The descriptor has already been removed from the current->mm->mmap list |
| 365 | * and will later be kfree()d. |
| 366 | */ |
| 367 | static void shm_close(struct vm_area_struct *vma) |
| 368 | { |
| 369 | struct file *file = vma->vm_file; |
| 370 | struct shm_file_data *sfd = shm_file_data(file); |
| 371 | struct shmid_kernel *shp; |
| 372 | struct ipc_namespace *ns = sfd->ns; |
| 373 | |
| 374 | down_write(&shm_ids(ns).rwsem); |
| 375 | /* remove from the list of attaches of the shm segment */ |
| 376 | shp = shm_lock(ns, sfd->id); |
| 377 | |
| 378 | /* |
| 379 | * We raced in the idr lookup or with shm_destroy(). |
| 380 | * Either way, the ID is busted. |
| 381 | */ |
| 382 | if (WARN_ON_ONCE(IS_ERR(shp))) |
| 383 | goto done; /* no-op */ |
| 384 | |
| 385 | ipc_update_pid(&shp->shm_lprid, task_tgid(current)); |
| 386 | shp->shm_dtim = ktime_get_real_seconds(); |
| 387 | shp->shm_nattch--; |
| 388 | if (shm_may_destroy(shp)) |
| 389 | shm_destroy(ns, shp); |
| 390 | else |
| 391 | shm_unlock(shp); |
| 392 | done: |
| 393 | up_write(&shm_ids(ns).rwsem); |
| 394 | } |
| 395 | |
| 396 | /* Called with ns->shm_ids(ns).rwsem locked */ |
| 397 | static int shm_try_destroy_orphaned(int id, void *p, void *data) |
| 398 | { |
| 399 | struct ipc_namespace *ns = data; |
| 400 | struct kern_ipc_perm *ipcp = p; |
| 401 | struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 402 | |
| 403 | /* |
| 404 | * We want to destroy segments without users and with already |
| 405 | * exit'ed originating process. |
| 406 | * |
| 407 | * As shp->* are changed under rwsem, it's safe to skip shp locking. |
| 408 | */ |
| 409 | if (!list_empty(&shp->shm_clist)) |
| 410 | return 0; |
| 411 | |
| 412 | if (shm_may_destroy(shp)) { |
| 413 | shm_lock_by_ptr(shp); |
| 414 | shm_destroy(ns, shp); |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | void shm_destroy_orphaned(struct ipc_namespace *ns) |
| 420 | { |
| 421 | down_write(&shm_ids(ns).rwsem); |
| 422 | if (shm_ids(ns).in_use) |
| 423 | idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns); |
| 424 | up_write(&shm_ids(ns).rwsem); |
| 425 | } |
| 426 | |
| 427 | /* Locking assumes this will only be called with task == current */ |
| 428 | void exit_shm(struct task_struct *task) |
| 429 | { |
| 430 | for (;;) { |
| 431 | struct shmid_kernel *shp; |
| 432 | struct ipc_namespace *ns; |
| 433 | |
| 434 | task_lock(task); |
| 435 | |
| 436 | if (list_empty(&task->sysvshm.shm_clist)) { |
| 437 | task_unlock(task); |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | shp = list_first_entry(&task->sysvshm.shm_clist, struct shmid_kernel, |
| 442 | shm_clist); |
| 443 | |
| 444 | /* |
| 445 | * 1) Get pointer to the ipc namespace. It is worth to say |
| 446 | * that this pointer is guaranteed to be valid because |
| 447 | * shp lifetime is always shorter than namespace lifetime |
| 448 | * in which shp lives. |
| 449 | * We taken task_lock it means that shp won't be freed. |
| 450 | */ |
| 451 | ns = shp->ns; |
| 452 | |
| 453 | /* |
| 454 | * 2) If kernel.shm_rmid_forced is not set then only keep track of |
| 455 | * which shmids are orphaned, so that a later set of the sysctl |
| 456 | * can clean them up. |
| 457 | */ |
| 458 | if (!ns->shm_rmid_forced) |
| 459 | goto unlink_continue; |
| 460 | |
| 461 | /* |
| 462 | * 3) get a reference to the namespace. |
| 463 | * The refcount could be already 0. If it is 0, then |
| 464 | * the shm objects will be free by free_ipc_work(). |
| 465 | */ |
| 466 | ns = get_ipc_ns_not_zero(ns); |
| 467 | if (!ns) { |
| 468 | unlink_continue: |
| 469 | list_del_init(&shp->shm_clist); |
| 470 | task_unlock(task); |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * 4) get a reference to shp. |
| 476 | * This cannot fail: shm_clist_rm() is called before |
| 477 | * ipc_rmid(), thus the refcount cannot be 0. |
| 478 | */ |
| 479 | WARN_ON(!ipc_rcu_getref(&shp->shm_perm)); |
| 480 | |
| 481 | /* |
| 482 | * 5) unlink the shm segment from the list of segments |
| 483 | * created by current. |
| 484 | * This must be done last. After unlinking, |
| 485 | * only the refcounts obtained above prevent IPC_RMID |
| 486 | * from destroying the segment or the namespace. |
| 487 | */ |
| 488 | list_del_init(&shp->shm_clist); |
| 489 | |
| 490 | task_unlock(task); |
| 491 | |
| 492 | /* |
| 493 | * 6) we have all references |
| 494 | * Thus lock & if needed destroy shp. |
| 495 | */ |
| 496 | down_write(&shm_ids(ns).rwsem); |
| 497 | shm_lock_by_ptr(shp); |
| 498 | /* |
| 499 | * rcu_read_lock was implicitly taken in shm_lock_by_ptr, it's |
| 500 | * safe to call ipc_rcu_putref here |
| 501 | */ |
| 502 | ipc_rcu_putref(&shp->shm_perm, shm_rcu_free); |
| 503 | |
| 504 | if (ipc_valid_object(&shp->shm_perm)) { |
| 505 | if (shm_may_destroy(shp)) |
| 506 | shm_destroy(ns, shp); |
| 507 | else |
| 508 | shm_unlock(shp); |
| 509 | } else { |
| 510 | /* |
| 511 | * Someone else deleted the shp from namespace |
| 512 | * idr/kht while we have waited. |
| 513 | * Just unlock and continue. |
| 514 | */ |
| 515 | shm_unlock(shp); |
| 516 | } |
| 517 | |
| 518 | up_write(&shm_ids(ns).rwsem); |
| 519 | put_ipc_ns(ns); /* paired with get_ipc_ns_not_zero */ |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static vm_fault_t shm_fault(struct vm_fault *vmf) |
| 524 | { |
| 525 | struct file *file = vmf->vma->vm_file; |
| 526 | struct shm_file_data *sfd = shm_file_data(file); |
| 527 | |
| 528 | return sfd->vm_ops->fault(vmf); |
| 529 | } |
| 530 | |
| 531 | static int shm_split(struct vm_area_struct *vma, unsigned long addr) |
| 532 | { |
| 533 | struct file *file = vma->vm_file; |
| 534 | struct shm_file_data *sfd = shm_file_data(file); |
| 535 | |
| 536 | if (sfd->vm_ops->split) |
| 537 | return sfd->vm_ops->split(vma, addr); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static unsigned long shm_pagesize(struct vm_area_struct *vma) |
| 543 | { |
| 544 | struct file *file = vma->vm_file; |
| 545 | struct shm_file_data *sfd = shm_file_data(file); |
| 546 | |
| 547 | if (sfd->vm_ops->pagesize) |
| 548 | return sfd->vm_ops->pagesize(vma); |
| 549 | |
| 550 | return PAGE_SIZE; |
| 551 | } |
| 552 | |
| 553 | #ifdef CONFIG_NUMA |
| 554 | static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new) |
| 555 | { |
| 556 | struct file *file = vma->vm_file; |
| 557 | struct shm_file_data *sfd = shm_file_data(file); |
| 558 | int err = 0; |
| 559 | |
| 560 | if (sfd->vm_ops->set_policy) |
| 561 | err = sfd->vm_ops->set_policy(vma, new); |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | static struct mempolicy *shm_get_policy(struct vm_area_struct *vma, |
| 566 | unsigned long addr) |
| 567 | { |
| 568 | struct file *file = vma->vm_file; |
| 569 | struct shm_file_data *sfd = shm_file_data(file); |
| 570 | struct mempolicy *pol = NULL; |
| 571 | |
| 572 | if (sfd->vm_ops->get_policy) |
| 573 | pol = sfd->vm_ops->get_policy(vma, addr); |
| 574 | else if (vma->vm_policy) |
| 575 | pol = vma->vm_policy; |
| 576 | |
| 577 | return pol; |
| 578 | } |
| 579 | #endif |
| 580 | |
| 581 | static int shm_mmap(struct file *file, struct vm_area_struct *vma) |
| 582 | { |
| 583 | struct shm_file_data *sfd = shm_file_data(file); |
| 584 | int ret; |
| 585 | |
| 586 | /* |
| 587 | * In case of remap_file_pages() emulation, the file can represent an |
| 588 | * IPC ID that was removed, and possibly even reused by another shm |
| 589 | * segment already. Propagate this case as an error to caller. |
| 590 | */ |
| 591 | ret = __shm_open(vma); |
| 592 | if (ret) |
| 593 | return ret; |
| 594 | |
| 595 | ret = call_mmap(sfd->file, vma); |
| 596 | if (ret) { |
| 597 | shm_close(vma); |
| 598 | return ret; |
| 599 | } |
| 600 | sfd->vm_ops = vma->vm_ops; |
| 601 | #ifdef CONFIG_MMU |
| 602 | WARN_ON(!sfd->vm_ops->fault); |
| 603 | #endif |
| 604 | vma->vm_ops = &shm_vm_ops; |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int shm_release(struct inode *ino, struct file *file) |
| 609 | { |
| 610 | struct shm_file_data *sfd = shm_file_data(file); |
| 611 | |
| 612 | put_ipc_ns(sfd->ns); |
| 613 | fput(sfd->file); |
| 614 | shm_file_data(file) = NULL; |
| 615 | kfree(sfd); |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 620 | { |
| 621 | struct shm_file_data *sfd = shm_file_data(file); |
| 622 | |
| 623 | if (!sfd->file->f_op->fsync) |
| 624 | return -EINVAL; |
| 625 | return sfd->file->f_op->fsync(sfd->file, start, end, datasync); |
| 626 | } |
| 627 | |
| 628 | static long shm_fallocate(struct file *file, int mode, loff_t offset, |
| 629 | loff_t len) |
| 630 | { |
| 631 | struct shm_file_data *sfd = shm_file_data(file); |
| 632 | |
| 633 | if (!sfd->file->f_op->fallocate) |
| 634 | return -EOPNOTSUPP; |
| 635 | return sfd->file->f_op->fallocate(file, mode, offset, len); |
| 636 | } |
| 637 | |
| 638 | static unsigned long shm_get_unmapped_area(struct file *file, |
| 639 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 640 | unsigned long flags) |
| 641 | { |
| 642 | struct shm_file_data *sfd = shm_file_data(file); |
| 643 | |
| 644 | return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len, |
| 645 | pgoff, flags); |
| 646 | } |
| 647 | |
| 648 | static const struct file_operations shm_file_operations = { |
| 649 | .mmap = shm_mmap, |
| 650 | .fsync = shm_fsync, |
| 651 | .release = shm_release, |
| 652 | .get_unmapped_area = shm_get_unmapped_area, |
| 653 | .llseek = noop_llseek, |
| 654 | .fallocate = shm_fallocate, |
| 655 | }; |
| 656 | |
| 657 | /* |
| 658 | * shm_file_operations_huge is now identical to shm_file_operations, |
| 659 | * but we keep it distinct for the sake of is_file_shm_hugepages(). |
| 660 | */ |
| 661 | static const struct file_operations shm_file_operations_huge = { |
| 662 | .mmap = shm_mmap, |
| 663 | .fsync = shm_fsync, |
| 664 | .release = shm_release, |
| 665 | .get_unmapped_area = shm_get_unmapped_area, |
| 666 | .llseek = noop_llseek, |
| 667 | .fallocate = shm_fallocate, |
| 668 | }; |
| 669 | |
| 670 | bool is_file_shm_hugepages(struct file *file) |
| 671 | { |
| 672 | return file->f_op == &shm_file_operations_huge; |
| 673 | } |
| 674 | |
| 675 | static const struct vm_operations_struct shm_vm_ops = { |
| 676 | .open = shm_open, /* callback for a new vm-area open */ |
| 677 | .close = shm_close, /* callback for when the vm-area is released */ |
| 678 | .fault = shm_fault, |
| 679 | .split = shm_split, |
| 680 | .pagesize = shm_pagesize, |
| 681 | #if defined(CONFIG_NUMA) |
| 682 | .set_policy = shm_set_policy, |
| 683 | .get_policy = shm_get_policy, |
| 684 | #endif |
| 685 | }; |
| 686 | |
| 687 | /** |
| 688 | * newseg - Create a new shared memory segment |
| 689 | * @ns: namespace |
| 690 | * @params: ptr to the structure that contains key, size and shmflg |
| 691 | * |
| 692 | * Called with shm_ids.rwsem held as a writer. |
| 693 | */ |
| 694 | static int newseg(struct ipc_namespace *ns, struct ipc_params *params) |
| 695 | { |
| 696 | key_t key = params->key; |
| 697 | int shmflg = params->flg; |
| 698 | size_t size = params->u.size; |
| 699 | int error; |
| 700 | struct shmid_kernel *shp; |
| 701 | size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 702 | struct file *file; |
| 703 | char name[13]; |
| 704 | vm_flags_t acctflag = 0; |
| 705 | |
| 706 | if (size < SHMMIN || size > ns->shm_ctlmax) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | if (numpages << PAGE_SHIFT < size) |
| 710 | return -ENOSPC; |
| 711 | |
| 712 | if (ns->shm_tot + numpages < ns->shm_tot || |
| 713 | ns->shm_tot + numpages > ns->shm_ctlall) |
| 714 | return -ENOSPC; |
| 715 | |
| 716 | shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT); |
| 717 | if (unlikely(!shp)) |
| 718 | return -ENOMEM; |
| 719 | |
| 720 | shp->shm_perm.key = key; |
| 721 | shp->shm_perm.mode = (shmflg & S_IRWXUGO); |
| 722 | shp->mlock_user = NULL; |
| 723 | |
| 724 | shp->shm_perm.security = NULL; |
| 725 | error = security_shm_alloc(&shp->shm_perm); |
| 726 | if (error) { |
| 727 | kvfree(shp); |
| 728 | return error; |
| 729 | } |
| 730 | |
| 731 | sprintf(name, "SYSV%08x", key); |
| 732 | if (shmflg & SHM_HUGETLB) { |
| 733 | struct hstate *hs; |
| 734 | size_t hugesize; |
| 735 | |
| 736 | hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK); |
| 737 | if (!hs) { |
| 738 | error = -EINVAL; |
| 739 | goto no_file; |
| 740 | } |
| 741 | hugesize = ALIGN(size, huge_page_size(hs)); |
| 742 | |
| 743 | /* hugetlb_file_setup applies strict accounting */ |
| 744 | if (shmflg & SHM_NORESERVE) |
| 745 | acctflag = VM_NORESERVE; |
| 746 | file = hugetlb_file_setup(name, hugesize, acctflag, |
| 747 | &shp->mlock_user, HUGETLB_SHMFS_INODE, |
| 748 | (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK); |
| 749 | } else { |
| 750 | /* |
| 751 | * Do not allow no accounting for OVERCOMMIT_NEVER, even |
| 752 | * if it's asked for. |
| 753 | */ |
| 754 | if ((shmflg & SHM_NORESERVE) && |
| 755 | sysctl_overcommit_memory != OVERCOMMIT_NEVER) |
| 756 | acctflag = VM_NORESERVE; |
| 757 | file = shmem_kernel_file_setup(name, size, acctflag); |
| 758 | } |
| 759 | error = PTR_ERR(file); |
| 760 | if (IS_ERR(file)) |
| 761 | goto no_file; |
| 762 | |
| 763 | shp->shm_cprid = get_pid(task_tgid(current)); |
| 764 | shp->shm_lprid = NULL; |
| 765 | shp->shm_atim = shp->shm_dtim = 0; |
| 766 | shp->shm_ctim = ktime_get_real_seconds(); |
| 767 | shp->shm_segsz = size; |
| 768 | shp->shm_nattch = 0; |
| 769 | shp->shm_file = file; |
| 770 | shp->shm_creator = current; |
| 771 | |
| 772 | /* ipc_addid() locks shp upon success. */ |
| 773 | error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni); |
| 774 | if (error < 0) |
| 775 | goto no_id; |
| 776 | |
| 777 | shp->ns = ns; |
| 778 | |
| 779 | task_lock(current); |
| 780 | list_add(&shp->shm_clist, ¤t->sysvshm.shm_clist); |
| 781 | task_unlock(current); |
| 782 | |
| 783 | /* |
| 784 | * shmid gets reported as "inode#" in /proc/pid/maps. |
| 785 | * proc-ps tools use this. Changing this will break them. |
| 786 | */ |
| 787 | file_inode(file)->i_ino = shp->shm_perm.id; |
| 788 | |
| 789 | ns->shm_tot += numpages; |
| 790 | error = shp->shm_perm.id; |
| 791 | |
| 792 | ipc_unlock_object(&shp->shm_perm); |
| 793 | rcu_read_unlock(); |
| 794 | return error; |
| 795 | |
| 796 | no_id: |
| 797 | ipc_update_pid(&shp->shm_cprid, NULL); |
| 798 | ipc_update_pid(&shp->shm_lprid, NULL); |
| 799 | if (is_file_hugepages(file) && shp->mlock_user) |
| 800 | user_shm_unlock(size, shp->mlock_user); |
| 801 | fput(file); |
| 802 | ipc_rcu_putref(&shp->shm_perm, shm_rcu_free); |
| 803 | return error; |
| 804 | no_file: |
| 805 | call_rcu(&shp->shm_perm.rcu, shm_rcu_free); |
| 806 | return error; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * Called with shm_ids.rwsem and ipcp locked. |
| 811 | */ |
| 812 | static inline int shm_more_checks(struct kern_ipc_perm *ipcp, |
| 813 | struct ipc_params *params) |
| 814 | { |
| 815 | struct shmid_kernel *shp; |
| 816 | |
| 817 | shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 818 | if (shp->shm_segsz < params->u.size) |
| 819 | return -EINVAL; |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | long ksys_shmget(key_t key, size_t size, int shmflg) |
| 825 | { |
| 826 | struct ipc_namespace *ns; |
| 827 | static const struct ipc_ops shm_ops = { |
| 828 | .getnew = newseg, |
| 829 | .associate = security_shm_associate, |
| 830 | .more_checks = shm_more_checks, |
| 831 | }; |
| 832 | struct ipc_params shm_params; |
| 833 | |
| 834 | ns = current->nsproxy->ipc_ns; |
| 835 | |
| 836 | shm_params.key = key; |
| 837 | shm_params.flg = shmflg; |
| 838 | shm_params.u.size = size; |
| 839 | |
| 840 | return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params); |
| 841 | } |
| 842 | |
| 843 | SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg) |
| 844 | { |
| 845 | return ksys_shmget(key, size, shmflg); |
| 846 | } |
| 847 | |
| 848 | static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version) |
| 849 | { |
| 850 | switch (version) { |
| 851 | case IPC_64: |
| 852 | return copy_to_user(buf, in, sizeof(*in)); |
| 853 | case IPC_OLD: |
| 854 | { |
| 855 | struct shmid_ds out; |
| 856 | |
| 857 | memset(&out, 0, sizeof(out)); |
| 858 | ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm); |
| 859 | out.shm_segsz = in->shm_segsz; |
| 860 | out.shm_atime = in->shm_atime; |
| 861 | out.shm_dtime = in->shm_dtime; |
| 862 | out.shm_ctime = in->shm_ctime; |
| 863 | out.shm_cpid = in->shm_cpid; |
| 864 | out.shm_lpid = in->shm_lpid; |
| 865 | out.shm_nattch = in->shm_nattch; |
| 866 | |
| 867 | return copy_to_user(buf, &out, sizeof(out)); |
| 868 | } |
| 869 | default: |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | static inline unsigned long |
| 875 | copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version) |
| 876 | { |
| 877 | switch (version) { |
| 878 | case IPC_64: |
| 879 | if (copy_from_user(out, buf, sizeof(*out))) |
| 880 | return -EFAULT; |
| 881 | return 0; |
| 882 | case IPC_OLD: |
| 883 | { |
| 884 | struct shmid_ds tbuf_old; |
| 885 | |
| 886 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
| 887 | return -EFAULT; |
| 888 | |
| 889 | out->shm_perm.uid = tbuf_old.shm_perm.uid; |
| 890 | out->shm_perm.gid = tbuf_old.shm_perm.gid; |
| 891 | out->shm_perm.mode = tbuf_old.shm_perm.mode; |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | default: |
| 896 | return -EINVAL; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version) |
| 901 | { |
| 902 | switch (version) { |
| 903 | case IPC_64: |
| 904 | return copy_to_user(buf, in, sizeof(*in)); |
| 905 | case IPC_OLD: |
| 906 | { |
| 907 | struct shminfo out; |
| 908 | |
| 909 | if (in->shmmax > INT_MAX) |
| 910 | out.shmmax = INT_MAX; |
| 911 | else |
| 912 | out.shmmax = (int)in->shmmax; |
| 913 | |
| 914 | out.shmmin = in->shmmin; |
| 915 | out.shmmni = in->shmmni; |
| 916 | out.shmseg = in->shmseg; |
| 917 | out.shmall = in->shmall; |
| 918 | |
| 919 | return copy_to_user(buf, &out, sizeof(out)); |
| 920 | } |
| 921 | default: |
| 922 | return -EINVAL; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Calculate and add used RSS and swap pages of a shm. |
| 928 | * Called with shm_ids.rwsem held as a reader |
| 929 | */ |
| 930 | static void shm_add_rss_swap(struct shmid_kernel *shp, |
| 931 | unsigned long *rss_add, unsigned long *swp_add) |
| 932 | { |
| 933 | struct inode *inode; |
| 934 | |
| 935 | inode = file_inode(shp->shm_file); |
| 936 | |
| 937 | if (is_file_hugepages(shp->shm_file)) { |
| 938 | struct address_space *mapping = inode->i_mapping; |
| 939 | struct hstate *h = hstate_file(shp->shm_file); |
| 940 | *rss_add += pages_per_huge_page(h) * mapping->nrpages; |
| 941 | } else { |
| 942 | #ifdef CONFIG_SHMEM |
| 943 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 944 | |
| 945 | spin_lock_irq(&info->lock); |
| 946 | *rss_add += inode->i_mapping->nrpages; |
| 947 | *swp_add += info->swapped; |
| 948 | spin_unlock_irq(&info->lock); |
| 949 | #else |
| 950 | *rss_add += inode->i_mapping->nrpages; |
| 951 | #endif |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Called with shm_ids.rwsem held as a reader |
| 957 | */ |
| 958 | static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss, |
| 959 | unsigned long *swp) |
| 960 | { |
| 961 | int next_id; |
| 962 | int total, in_use; |
| 963 | |
| 964 | *rss = 0; |
| 965 | *swp = 0; |
| 966 | |
| 967 | in_use = shm_ids(ns).in_use; |
| 968 | |
| 969 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
| 970 | struct kern_ipc_perm *ipc; |
| 971 | struct shmid_kernel *shp; |
| 972 | |
| 973 | ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id); |
| 974 | if (ipc == NULL) |
| 975 | continue; |
| 976 | shp = container_of(ipc, struct shmid_kernel, shm_perm); |
| 977 | |
| 978 | shm_add_rss_swap(shp, rss, swp); |
| 979 | |
| 980 | total++; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * This function handles some shmctl commands which require the rwsem |
| 986 | * to be held in write mode. |
| 987 | * NOTE: no locks must be held, the rwsem is taken inside this function. |
| 988 | */ |
| 989 | static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd, |
| 990 | struct shmid64_ds *shmid64) |
| 991 | { |
| 992 | struct kern_ipc_perm *ipcp; |
| 993 | struct shmid_kernel *shp; |
| 994 | int err; |
| 995 | |
| 996 | down_write(&shm_ids(ns).rwsem); |
| 997 | rcu_read_lock(); |
| 998 | |
| 999 | ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd, |
| 1000 | &shmid64->shm_perm, 0); |
| 1001 | if (IS_ERR(ipcp)) { |
| 1002 | err = PTR_ERR(ipcp); |
| 1003 | goto out_unlock1; |
| 1004 | } |
| 1005 | |
| 1006 | shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 1007 | |
| 1008 | err = security_shm_shmctl(&shp->shm_perm, cmd); |
| 1009 | if (err) |
| 1010 | goto out_unlock1; |
| 1011 | |
| 1012 | switch (cmd) { |
| 1013 | case IPC_RMID: |
| 1014 | ipc_lock_object(&shp->shm_perm); |
| 1015 | /* do_shm_rmid unlocks the ipc object and rcu */ |
| 1016 | do_shm_rmid(ns, ipcp); |
| 1017 | goto out_up; |
| 1018 | case IPC_SET: |
| 1019 | ipc_lock_object(&shp->shm_perm); |
| 1020 | err = ipc_update_perm(&shmid64->shm_perm, ipcp); |
| 1021 | if (err) |
| 1022 | goto out_unlock0; |
| 1023 | shp->shm_ctim = ktime_get_real_seconds(); |
| 1024 | break; |
| 1025 | default: |
| 1026 | err = -EINVAL; |
| 1027 | goto out_unlock1; |
| 1028 | } |
| 1029 | |
| 1030 | out_unlock0: |
| 1031 | ipc_unlock_object(&shp->shm_perm); |
| 1032 | out_unlock1: |
| 1033 | rcu_read_unlock(); |
| 1034 | out_up: |
| 1035 | up_write(&shm_ids(ns).rwsem); |
| 1036 | return err; |
| 1037 | } |
| 1038 | |
| 1039 | static int shmctl_ipc_info(struct ipc_namespace *ns, |
| 1040 | struct shminfo64 *shminfo) |
| 1041 | { |
| 1042 | int err = security_shm_shmctl(NULL, IPC_INFO); |
| 1043 | if (!err) { |
| 1044 | memset(shminfo, 0, sizeof(*shminfo)); |
| 1045 | shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni; |
| 1046 | shminfo->shmmax = ns->shm_ctlmax; |
| 1047 | shminfo->shmall = ns->shm_ctlall; |
| 1048 | shminfo->shmmin = SHMMIN; |
| 1049 | down_read(&shm_ids(ns).rwsem); |
| 1050 | err = ipc_get_maxidx(&shm_ids(ns)); |
| 1051 | up_read(&shm_ids(ns).rwsem); |
| 1052 | if (err < 0) |
| 1053 | err = 0; |
| 1054 | } |
| 1055 | return err; |
| 1056 | } |
| 1057 | |
| 1058 | static int shmctl_shm_info(struct ipc_namespace *ns, |
| 1059 | struct shm_info *shm_info) |
| 1060 | { |
| 1061 | int err = security_shm_shmctl(NULL, SHM_INFO); |
| 1062 | if (!err) { |
| 1063 | memset(shm_info, 0, sizeof(*shm_info)); |
| 1064 | down_read(&shm_ids(ns).rwsem); |
| 1065 | shm_info->used_ids = shm_ids(ns).in_use; |
| 1066 | shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp); |
| 1067 | shm_info->shm_tot = ns->shm_tot; |
| 1068 | shm_info->swap_attempts = 0; |
| 1069 | shm_info->swap_successes = 0; |
| 1070 | err = ipc_get_maxidx(&shm_ids(ns)); |
| 1071 | up_read(&shm_ids(ns).rwsem); |
| 1072 | if (err < 0) |
| 1073 | err = 0; |
| 1074 | } |
| 1075 | return err; |
| 1076 | } |
| 1077 | |
| 1078 | static int shmctl_stat(struct ipc_namespace *ns, int shmid, |
| 1079 | int cmd, struct shmid64_ds *tbuf) |
| 1080 | { |
| 1081 | struct shmid_kernel *shp; |
| 1082 | int err; |
| 1083 | |
| 1084 | memset(tbuf, 0, sizeof(*tbuf)); |
| 1085 | |
| 1086 | rcu_read_lock(); |
| 1087 | if (cmd == SHM_STAT || cmd == SHM_STAT_ANY) { |
| 1088 | shp = shm_obtain_object(ns, shmid); |
| 1089 | if (IS_ERR(shp)) { |
| 1090 | err = PTR_ERR(shp); |
| 1091 | goto out_unlock; |
| 1092 | } |
| 1093 | } else { /* IPC_STAT */ |
| 1094 | shp = shm_obtain_object_check(ns, shmid); |
| 1095 | if (IS_ERR(shp)) { |
| 1096 | err = PTR_ERR(shp); |
| 1097 | goto out_unlock; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Semantically SHM_STAT_ANY ought to be identical to |
| 1103 | * that functionality provided by the /proc/sysvipc/ |
| 1104 | * interface. As such, only audit these calls and |
| 1105 | * do not do traditional S_IRUGO permission checks on |
| 1106 | * the ipc object. |
| 1107 | */ |
| 1108 | if (cmd == SHM_STAT_ANY) |
| 1109 | audit_ipc_obj(&shp->shm_perm); |
| 1110 | else { |
| 1111 | err = -EACCES; |
| 1112 | if (ipcperms(ns, &shp->shm_perm, S_IRUGO)) |
| 1113 | goto out_unlock; |
| 1114 | } |
| 1115 | |
| 1116 | err = security_shm_shmctl(&shp->shm_perm, cmd); |
| 1117 | if (err) |
| 1118 | goto out_unlock; |
| 1119 | |
| 1120 | ipc_lock_object(&shp->shm_perm); |
| 1121 | |
| 1122 | if (!ipc_valid_object(&shp->shm_perm)) { |
| 1123 | ipc_unlock_object(&shp->shm_perm); |
| 1124 | err = -EIDRM; |
| 1125 | goto out_unlock; |
| 1126 | } |
| 1127 | |
| 1128 | kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm); |
| 1129 | tbuf->shm_segsz = shp->shm_segsz; |
| 1130 | tbuf->shm_atime = shp->shm_atim; |
| 1131 | tbuf->shm_dtime = shp->shm_dtim; |
| 1132 | tbuf->shm_ctime = shp->shm_ctim; |
| 1133 | #ifndef CONFIG_64BIT |
| 1134 | tbuf->shm_atime_high = shp->shm_atim >> 32; |
| 1135 | tbuf->shm_dtime_high = shp->shm_dtim >> 32; |
| 1136 | tbuf->shm_ctime_high = shp->shm_ctim >> 32; |
| 1137 | #endif |
| 1138 | tbuf->shm_cpid = pid_vnr(shp->shm_cprid); |
| 1139 | tbuf->shm_lpid = pid_vnr(shp->shm_lprid); |
| 1140 | tbuf->shm_nattch = shp->shm_nattch; |
| 1141 | |
| 1142 | if (cmd == IPC_STAT) { |
| 1143 | /* |
| 1144 | * As defined in SUS: |
| 1145 | * Return 0 on success |
| 1146 | */ |
| 1147 | err = 0; |
| 1148 | } else { |
| 1149 | /* |
| 1150 | * SHM_STAT and SHM_STAT_ANY (both Linux specific) |
| 1151 | * Return the full id, including the sequence number |
| 1152 | */ |
| 1153 | err = shp->shm_perm.id; |
| 1154 | } |
| 1155 | |
| 1156 | ipc_unlock_object(&shp->shm_perm); |
| 1157 | out_unlock: |
| 1158 | rcu_read_unlock(); |
| 1159 | return err; |
| 1160 | } |
| 1161 | |
| 1162 | static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd) |
| 1163 | { |
| 1164 | struct shmid_kernel *shp; |
| 1165 | struct file *shm_file; |
| 1166 | int err; |
| 1167 | |
| 1168 | rcu_read_lock(); |
| 1169 | shp = shm_obtain_object_check(ns, shmid); |
| 1170 | if (IS_ERR(shp)) { |
| 1171 | err = PTR_ERR(shp); |
| 1172 | goto out_unlock1; |
| 1173 | } |
| 1174 | |
| 1175 | audit_ipc_obj(&(shp->shm_perm)); |
| 1176 | err = security_shm_shmctl(&shp->shm_perm, cmd); |
| 1177 | if (err) |
| 1178 | goto out_unlock1; |
| 1179 | |
| 1180 | ipc_lock_object(&shp->shm_perm); |
| 1181 | |
| 1182 | /* check if shm_destroy() is tearing down shp */ |
| 1183 | if (!ipc_valid_object(&shp->shm_perm)) { |
| 1184 | err = -EIDRM; |
| 1185 | goto out_unlock0; |
| 1186 | } |
| 1187 | |
| 1188 | if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) { |
| 1189 | kuid_t euid = current_euid(); |
| 1190 | |
| 1191 | if (!uid_eq(euid, shp->shm_perm.uid) && |
| 1192 | !uid_eq(euid, shp->shm_perm.cuid)) { |
| 1193 | err = -EPERM; |
| 1194 | goto out_unlock0; |
| 1195 | } |
| 1196 | if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) { |
| 1197 | err = -EPERM; |
| 1198 | goto out_unlock0; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | shm_file = shp->shm_file; |
| 1203 | if (is_file_hugepages(shm_file)) |
| 1204 | goto out_unlock0; |
| 1205 | |
| 1206 | if (cmd == SHM_LOCK) { |
| 1207 | struct user_struct *user = current_user(); |
| 1208 | |
| 1209 | err = shmem_lock(shm_file, 1, user); |
| 1210 | if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) { |
| 1211 | shp->shm_perm.mode |= SHM_LOCKED; |
| 1212 | shp->mlock_user = user; |
| 1213 | } |
| 1214 | goto out_unlock0; |
| 1215 | } |
| 1216 | |
| 1217 | /* SHM_UNLOCK */ |
| 1218 | if (!(shp->shm_perm.mode & SHM_LOCKED)) |
| 1219 | goto out_unlock0; |
| 1220 | shmem_lock(shm_file, 0, shp->mlock_user); |
| 1221 | shp->shm_perm.mode &= ~SHM_LOCKED; |
| 1222 | shp->mlock_user = NULL; |
| 1223 | get_file(shm_file); |
| 1224 | ipc_unlock_object(&shp->shm_perm); |
| 1225 | rcu_read_unlock(); |
| 1226 | shmem_unlock_mapping(shm_file->f_mapping); |
| 1227 | |
| 1228 | fput(shm_file); |
| 1229 | return err; |
| 1230 | |
| 1231 | out_unlock0: |
| 1232 | ipc_unlock_object(&shp->shm_perm); |
| 1233 | out_unlock1: |
| 1234 | rcu_read_unlock(); |
| 1235 | return err; |
| 1236 | } |
| 1237 | |
| 1238 | static long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf, int version) |
| 1239 | { |
| 1240 | int err; |
| 1241 | struct ipc_namespace *ns; |
| 1242 | struct shmid64_ds sem64; |
| 1243 | |
| 1244 | if (cmd < 0 || shmid < 0) |
| 1245 | return -EINVAL; |
| 1246 | |
| 1247 | ns = current->nsproxy->ipc_ns; |
| 1248 | |
| 1249 | switch (cmd) { |
| 1250 | case IPC_INFO: { |
| 1251 | struct shminfo64 shminfo; |
| 1252 | err = shmctl_ipc_info(ns, &shminfo); |
| 1253 | if (err < 0) |
| 1254 | return err; |
| 1255 | if (copy_shminfo_to_user(buf, &shminfo, version)) |
| 1256 | err = -EFAULT; |
| 1257 | return err; |
| 1258 | } |
| 1259 | case SHM_INFO: { |
| 1260 | struct shm_info shm_info; |
| 1261 | err = shmctl_shm_info(ns, &shm_info); |
| 1262 | if (err < 0) |
| 1263 | return err; |
| 1264 | if (copy_to_user(buf, &shm_info, sizeof(shm_info))) |
| 1265 | err = -EFAULT; |
| 1266 | return err; |
| 1267 | } |
| 1268 | case SHM_STAT: |
| 1269 | case SHM_STAT_ANY: |
| 1270 | case IPC_STAT: { |
| 1271 | err = shmctl_stat(ns, shmid, cmd, &sem64); |
| 1272 | if (err < 0) |
| 1273 | return err; |
| 1274 | if (copy_shmid_to_user(buf, &sem64, version)) |
| 1275 | err = -EFAULT; |
| 1276 | return err; |
| 1277 | } |
| 1278 | case IPC_SET: |
| 1279 | if (copy_shmid_from_user(&sem64, buf, version)) |
| 1280 | return -EFAULT; |
| 1281 | /* fallthru */ |
| 1282 | case IPC_RMID: |
| 1283 | return shmctl_down(ns, shmid, cmd, &sem64); |
| 1284 | case SHM_LOCK: |
| 1285 | case SHM_UNLOCK: |
| 1286 | return shmctl_do_lock(ns, shmid, cmd); |
| 1287 | default: |
| 1288 | return -EINVAL; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf) |
| 1293 | { |
| 1294 | return ksys_shmctl(shmid, cmd, buf, IPC_64); |
| 1295 | } |
| 1296 | |
| 1297 | #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION |
| 1298 | long ksys_old_shmctl(int shmid, int cmd, struct shmid_ds __user *buf) |
| 1299 | { |
| 1300 | int version = ipc_parse_version(&cmd); |
| 1301 | |
| 1302 | return ksys_shmctl(shmid, cmd, buf, version); |
| 1303 | } |
| 1304 | |
| 1305 | SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf) |
| 1306 | { |
| 1307 | return ksys_old_shmctl(shmid, cmd, buf); |
| 1308 | } |
| 1309 | #endif |
| 1310 | |
| 1311 | #ifdef CONFIG_COMPAT |
| 1312 | |
| 1313 | struct compat_shmid_ds { |
| 1314 | struct compat_ipc_perm shm_perm; |
| 1315 | int shm_segsz; |
| 1316 | old_time32_t shm_atime; |
| 1317 | old_time32_t shm_dtime; |
| 1318 | old_time32_t shm_ctime; |
| 1319 | compat_ipc_pid_t shm_cpid; |
| 1320 | compat_ipc_pid_t shm_lpid; |
| 1321 | unsigned short shm_nattch; |
| 1322 | unsigned short shm_unused; |
| 1323 | compat_uptr_t shm_unused2; |
| 1324 | compat_uptr_t shm_unused3; |
| 1325 | }; |
| 1326 | |
| 1327 | struct compat_shminfo64 { |
| 1328 | compat_ulong_t shmmax; |
| 1329 | compat_ulong_t shmmin; |
| 1330 | compat_ulong_t shmmni; |
| 1331 | compat_ulong_t shmseg; |
| 1332 | compat_ulong_t shmall; |
| 1333 | compat_ulong_t __unused1; |
| 1334 | compat_ulong_t __unused2; |
| 1335 | compat_ulong_t __unused3; |
| 1336 | compat_ulong_t __unused4; |
| 1337 | }; |
| 1338 | |
| 1339 | struct compat_shm_info { |
| 1340 | compat_int_t used_ids; |
| 1341 | compat_ulong_t shm_tot, shm_rss, shm_swp; |
| 1342 | compat_ulong_t swap_attempts, swap_successes; |
| 1343 | }; |
| 1344 | |
| 1345 | static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in, |
| 1346 | int version) |
| 1347 | { |
| 1348 | if (in->shmmax > INT_MAX) |
| 1349 | in->shmmax = INT_MAX; |
| 1350 | if (version == IPC_64) { |
| 1351 | struct compat_shminfo64 info; |
| 1352 | memset(&info, 0, sizeof(info)); |
| 1353 | info.shmmax = in->shmmax; |
| 1354 | info.shmmin = in->shmmin; |
| 1355 | info.shmmni = in->shmmni; |
| 1356 | info.shmseg = in->shmseg; |
| 1357 | info.shmall = in->shmall; |
| 1358 | return copy_to_user(buf, &info, sizeof(info)); |
| 1359 | } else { |
| 1360 | struct shminfo info; |
| 1361 | memset(&info, 0, sizeof(info)); |
| 1362 | info.shmmax = in->shmmax; |
| 1363 | info.shmmin = in->shmmin; |
| 1364 | info.shmmni = in->shmmni; |
| 1365 | info.shmseg = in->shmseg; |
| 1366 | info.shmall = in->shmall; |
| 1367 | return copy_to_user(buf, &info, sizeof(info)); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | static int put_compat_shm_info(struct shm_info *ip, |
| 1372 | struct compat_shm_info __user *uip) |
| 1373 | { |
| 1374 | struct compat_shm_info info; |
| 1375 | |
| 1376 | memset(&info, 0, sizeof(info)); |
| 1377 | info.used_ids = ip->used_ids; |
| 1378 | info.shm_tot = ip->shm_tot; |
| 1379 | info.shm_rss = ip->shm_rss; |
| 1380 | info.shm_swp = ip->shm_swp; |
| 1381 | info.swap_attempts = ip->swap_attempts; |
| 1382 | info.swap_successes = ip->swap_successes; |
| 1383 | return copy_to_user(uip, &info, sizeof(info)); |
| 1384 | } |
| 1385 | |
| 1386 | static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in, |
| 1387 | int version) |
| 1388 | { |
| 1389 | if (version == IPC_64) { |
| 1390 | struct compat_shmid64_ds v; |
| 1391 | memset(&v, 0, sizeof(v)); |
| 1392 | to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm); |
| 1393 | v.shm_atime = lower_32_bits(in->shm_atime); |
| 1394 | v.shm_atime_high = upper_32_bits(in->shm_atime); |
| 1395 | v.shm_dtime = lower_32_bits(in->shm_dtime); |
| 1396 | v.shm_dtime_high = upper_32_bits(in->shm_dtime); |
| 1397 | v.shm_ctime = lower_32_bits(in->shm_ctime); |
| 1398 | v.shm_ctime_high = upper_32_bits(in->shm_ctime); |
| 1399 | v.shm_segsz = in->shm_segsz; |
| 1400 | v.shm_nattch = in->shm_nattch; |
| 1401 | v.shm_cpid = in->shm_cpid; |
| 1402 | v.shm_lpid = in->shm_lpid; |
| 1403 | return copy_to_user(buf, &v, sizeof(v)); |
| 1404 | } else { |
| 1405 | struct compat_shmid_ds v; |
| 1406 | memset(&v, 0, sizeof(v)); |
| 1407 | to_compat_ipc_perm(&v.shm_perm, &in->shm_perm); |
| 1408 | v.shm_perm.key = in->shm_perm.key; |
| 1409 | v.shm_atime = in->shm_atime; |
| 1410 | v.shm_dtime = in->shm_dtime; |
| 1411 | v.shm_ctime = in->shm_ctime; |
| 1412 | v.shm_segsz = in->shm_segsz; |
| 1413 | v.shm_nattch = in->shm_nattch; |
| 1414 | v.shm_cpid = in->shm_cpid; |
| 1415 | v.shm_lpid = in->shm_lpid; |
| 1416 | return copy_to_user(buf, &v, sizeof(v)); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf, |
| 1421 | int version) |
| 1422 | { |
| 1423 | memset(out, 0, sizeof(*out)); |
| 1424 | if (version == IPC_64) { |
| 1425 | struct compat_shmid64_ds __user *p = buf; |
| 1426 | return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm); |
| 1427 | } else { |
| 1428 | struct compat_shmid_ds __user *p = buf; |
| 1429 | return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr, int version) |
| 1434 | { |
| 1435 | struct ipc_namespace *ns; |
| 1436 | struct shmid64_ds sem64; |
| 1437 | int err; |
| 1438 | |
| 1439 | ns = current->nsproxy->ipc_ns; |
| 1440 | |
| 1441 | if (cmd < 0 || shmid < 0) |
| 1442 | return -EINVAL; |
| 1443 | |
| 1444 | switch (cmd) { |
| 1445 | case IPC_INFO: { |
| 1446 | struct shminfo64 shminfo; |
| 1447 | err = shmctl_ipc_info(ns, &shminfo); |
| 1448 | if (err < 0) |
| 1449 | return err; |
| 1450 | if (copy_compat_shminfo_to_user(uptr, &shminfo, version)) |
| 1451 | err = -EFAULT; |
| 1452 | return err; |
| 1453 | } |
| 1454 | case SHM_INFO: { |
| 1455 | struct shm_info shm_info; |
| 1456 | err = shmctl_shm_info(ns, &shm_info); |
| 1457 | if (err < 0) |
| 1458 | return err; |
| 1459 | if (put_compat_shm_info(&shm_info, uptr)) |
| 1460 | err = -EFAULT; |
| 1461 | return err; |
| 1462 | } |
| 1463 | case IPC_STAT: |
| 1464 | case SHM_STAT_ANY: |
| 1465 | case SHM_STAT: |
| 1466 | err = shmctl_stat(ns, shmid, cmd, &sem64); |
| 1467 | if (err < 0) |
| 1468 | return err; |
| 1469 | if (copy_compat_shmid_to_user(uptr, &sem64, version)) |
| 1470 | err = -EFAULT; |
| 1471 | return err; |
| 1472 | |
| 1473 | case IPC_SET: |
| 1474 | if (copy_compat_shmid_from_user(&sem64, uptr, version)) |
| 1475 | return -EFAULT; |
| 1476 | /* fallthru */ |
| 1477 | case IPC_RMID: |
| 1478 | return shmctl_down(ns, shmid, cmd, &sem64); |
| 1479 | case SHM_LOCK: |
| 1480 | case SHM_UNLOCK: |
| 1481 | return shmctl_do_lock(ns, shmid, cmd); |
| 1482 | break; |
| 1483 | default: |
| 1484 | return -EINVAL; |
| 1485 | } |
| 1486 | return err; |
| 1487 | } |
| 1488 | |
| 1489 | COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr) |
| 1490 | { |
| 1491 | return compat_ksys_shmctl(shmid, cmd, uptr, IPC_64); |
| 1492 | } |
| 1493 | |
| 1494 | #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION |
| 1495 | long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr) |
| 1496 | { |
| 1497 | int version = compat_ipc_parse_version(&cmd); |
| 1498 | |
| 1499 | return compat_ksys_shmctl(shmid, cmd, uptr, version); |
| 1500 | } |
| 1501 | |
| 1502 | COMPAT_SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, void __user *, uptr) |
| 1503 | { |
| 1504 | return compat_ksys_old_shmctl(shmid, cmd, uptr); |
| 1505 | } |
| 1506 | #endif |
| 1507 | #endif |
| 1508 | |
| 1509 | /* |
| 1510 | * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists. |
| 1511 | * |
| 1512 | * NOTE! Despite the name, this is NOT a direct system call entrypoint. The |
| 1513 | * "raddr" thing points to kernel space, and there has to be a wrapper around |
| 1514 | * this. |
| 1515 | */ |
| 1516 | long do_shmat(int shmid, char __user *shmaddr, int shmflg, |
| 1517 | ulong *raddr, unsigned long shmlba) |
| 1518 | { |
| 1519 | struct shmid_kernel *shp; |
| 1520 | unsigned long addr = (unsigned long)shmaddr; |
| 1521 | unsigned long size; |
| 1522 | struct file *file, *base; |
| 1523 | int err; |
| 1524 | unsigned long flags = MAP_SHARED; |
| 1525 | unsigned long prot; |
| 1526 | int acc_mode; |
| 1527 | struct ipc_namespace *ns; |
| 1528 | struct shm_file_data *sfd; |
| 1529 | int f_flags; |
| 1530 | unsigned long populate = 0; |
| 1531 | |
| 1532 | err = -EINVAL; |
| 1533 | if (shmid < 0) |
| 1534 | goto out; |
| 1535 | |
| 1536 | if (addr) { |
| 1537 | if (addr & (shmlba - 1)) { |
| 1538 | if (shmflg & SHM_RND) { |
| 1539 | addr &= ~(shmlba - 1); /* round down */ |
| 1540 | |
| 1541 | /* |
| 1542 | * Ensure that the round-down is non-nil |
| 1543 | * when remapping. This can happen for |
| 1544 | * cases when addr < shmlba. |
| 1545 | */ |
| 1546 | if (!addr && (shmflg & SHM_REMAP)) |
| 1547 | goto out; |
| 1548 | } else |
| 1549 | #ifndef __ARCH_FORCE_SHMLBA |
| 1550 | if (addr & ~PAGE_MASK) |
| 1551 | #endif |
| 1552 | goto out; |
| 1553 | } |
| 1554 | |
| 1555 | flags |= MAP_FIXED; |
| 1556 | } else if ((shmflg & SHM_REMAP)) |
| 1557 | goto out; |
| 1558 | |
| 1559 | if (shmflg & SHM_RDONLY) { |
| 1560 | prot = PROT_READ; |
| 1561 | acc_mode = S_IRUGO; |
| 1562 | f_flags = O_RDONLY; |
| 1563 | } else { |
| 1564 | prot = PROT_READ | PROT_WRITE; |
| 1565 | acc_mode = S_IRUGO | S_IWUGO; |
| 1566 | f_flags = O_RDWR; |
| 1567 | } |
| 1568 | if (shmflg & SHM_EXEC) { |
| 1569 | prot |= PROT_EXEC; |
| 1570 | acc_mode |= S_IXUGO; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * We cannot rely on the fs check since SYSV IPC does have an |
| 1575 | * additional creator id... |
| 1576 | */ |
| 1577 | ns = current->nsproxy->ipc_ns; |
| 1578 | rcu_read_lock(); |
| 1579 | shp = shm_obtain_object_check(ns, shmid); |
| 1580 | if (IS_ERR(shp)) { |
| 1581 | err = PTR_ERR(shp); |
| 1582 | goto out_unlock; |
| 1583 | } |
| 1584 | |
| 1585 | err = -EACCES; |
| 1586 | if (ipcperms(ns, &shp->shm_perm, acc_mode)) |
| 1587 | goto out_unlock; |
| 1588 | |
| 1589 | err = security_shm_shmat(&shp->shm_perm, shmaddr, shmflg); |
| 1590 | if (err) |
| 1591 | goto out_unlock; |
| 1592 | |
| 1593 | ipc_lock_object(&shp->shm_perm); |
| 1594 | |
| 1595 | /* check if shm_destroy() is tearing down shp */ |
| 1596 | if (!ipc_valid_object(&shp->shm_perm)) { |
| 1597 | ipc_unlock_object(&shp->shm_perm); |
| 1598 | err = -EIDRM; |
| 1599 | goto out_unlock; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
| 1603 | * We need to take a reference to the real shm file to prevent the |
| 1604 | * pointer from becoming stale in cases where the lifetime of the outer |
| 1605 | * file extends beyond that of the shm segment. It's not usually |
| 1606 | * possible, but it can happen during remap_file_pages() emulation as |
| 1607 | * that unmaps the memory, then does ->mmap() via file reference only. |
| 1608 | * We'll deny the ->mmap() if the shm segment was since removed, but to |
| 1609 | * detect shm ID reuse we need to compare the file pointers. |
| 1610 | */ |
| 1611 | base = get_file(shp->shm_file); |
| 1612 | shp->shm_nattch++; |
| 1613 | size = i_size_read(file_inode(base)); |
| 1614 | ipc_unlock_object(&shp->shm_perm); |
| 1615 | rcu_read_unlock(); |
| 1616 | |
| 1617 | err = -ENOMEM; |
| 1618 | sfd = kzalloc(sizeof(*sfd), GFP_KERNEL); |
| 1619 | if (!sfd) { |
| 1620 | fput(base); |
| 1621 | goto out_nattch; |
| 1622 | } |
| 1623 | |
| 1624 | file = alloc_file_clone(base, f_flags, |
| 1625 | is_file_hugepages(base) ? |
| 1626 | &shm_file_operations_huge : |
| 1627 | &shm_file_operations); |
| 1628 | err = PTR_ERR(file); |
| 1629 | if (IS_ERR(file)) { |
| 1630 | kfree(sfd); |
| 1631 | fput(base); |
| 1632 | goto out_nattch; |
| 1633 | } |
| 1634 | |
| 1635 | sfd->id = shp->shm_perm.id; |
| 1636 | sfd->ns = get_ipc_ns(ns); |
| 1637 | sfd->file = base; |
| 1638 | sfd->vm_ops = NULL; |
| 1639 | file->private_data = sfd; |
| 1640 | |
| 1641 | err = security_mmap_file(file, prot, flags); |
| 1642 | if (err) |
| 1643 | goto out_fput; |
| 1644 | |
| 1645 | if (down_write_killable(¤t->mm->mmap_sem)) { |
| 1646 | err = -EINTR; |
| 1647 | goto out_fput; |
| 1648 | } |
| 1649 | |
| 1650 | if (addr && !(shmflg & SHM_REMAP)) { |
| 1651 | err = -EINVAL; |
| 1652 | if (addr + size < addr) |
| 1653 | goto invalid; |
| 1654 | |
| 1655 | if (find_vma_intersection(current->mm, addr, addr + size)) |
| 1656 | goto invalid; |
| 1657 | } |
| 1658 | |
| 1659 | addr = do_mmap_pgoff(file, addr, size, prot, flags, 0, &populate, NULL); |
| 1660 | *raddr = addr; |
| 1661 | err = 0; |
| 1662 | if (IS_ERR_VALUE(addr)) |
| 1663 | err = (long)addr; |
| 1664 | invalid: |
| 1665 | up_write(¤t->mm->mmap_sem); |
| 1666 | if (populate) |
| 1667 | mm_populate(addr, populate); |
| 1668 | |
| 1669 | out_fput: |
| 1670 | fput(file); |
| 1671 | |
| 1672 | out_nattch: |
| 1673 | down_write(&shm_ids(ns).rwsem); |
| 1674 | shp = shm_lock(ns, shmid); |
| 1675 | shp->shm_nattch--; |
| 1676 | |
| 1677 | if (shm_may_destroy(shp)) |
| 1678 | shm_destroy(ns, shp); |
| 1679 | else |
| 1680 | shm_unlock(shp); |
| 1681 | up_write(&shm_ids(ns).rwsem); |
| 1682 | return err; |
| 1683 | |
| 1684 | out_unlock: |
| 1685 | rcu_read_unlock(); |
| 1686 | out: |
| 1687 | return err; |
| 1688 | } |
| 1689 | |
| 1690 | SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg) |
| 1691 | { |
| 1692 | unsigned long ret; |
| 1693 | long err; |
| 1694 | |
| 1695 | err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA); |
| 1696 | if (err) |
| 1697 | return err; |
| 1698 | force_successful_syscall_return(); |
| 1699 | return (long)ret; |
| 1700 | } |
| 1701 | |
| 1702 | #ifdef CONFIG_COMPAT |
| 1703 | |
| 1704 | #ifndef COMPAT_SHMLBA |
| 1705 | #define COMPAT_SHMLBA SHMLBA |
| 1706 | #endif |
| 1707 | |
| 1708 | COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg) |
| 1709 | { |
| 1710 | unsigned long ret; |
| 1711 | long err; |
| 1712 | |
| 1713 | err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA); |
| 1714 | if (err) |
| 1715 | return err; |
| 1716 | force_successful_syscall_return(); |
| 1717 | return (long)ret; |
| 1718 | } |
| 1719 | #endif |
| 1720 | |
| 1721 | /* |
| 1722 | * detach and kill segment if marked destroyed. |
| 1723 | * The work is done in shm_close. |
| 1724 | */ |
| 1725 | long ksys_shmdt(char __user *shmaddr) |
| 1726 | { |
| 1727 | struct mm_struct *mm = current->mm; |
| 1728 | struct vm_area_struct *vma; |
| 1729 | unsigned long addr = (unsigned long)shmaddr; |
| 1730 | int retval = -EINVAL; |
| 1731 | #ifdef CONFIG_MMU |
| 1732 | loff_t size = 0; |
| 1733 | struct file *file; |
| 1734 | struct vm_area_struct *next; |
| 1735 | #endif |
| 1736 | |
| 1737 | if (addr & ~PAGE_MASK) |
| 1738 | return retval; |
| 1739 | |
| 1740 | if (down_write_killable(&mm->mmap_sem)) |
| 1741 | return -EINTR; |
| 1742 | |
| 1743 | /* |
| 1744 | * This function tries to be smart and unmap shm segments that |
| 1745 | * were modified by partial mlock or munmap calls: |
| 1746 | * - It first determines the size of the shm segment that should be |
| 1747 | * unmapped: It searches for a vma that is backed by shm and that |
| 1748 | * started at address shmaddr. It records it's size and then unmaps |
| 1749 | * it. |
| 1750 | * - Then it unmaps all shm vmas that started at shmaddr and that |
| 1751 | * are within the initially determined size and that are from the |
| 1752 | * same shm segment from which we determined the size. |
| 1753 | * Errors from do_munmap are ignored: the function only fails if |
| 1754 | * it's called with invalid parameters or if it's called to unmap |
| 1755 | * a part of a vma. Both calls in this function are for full vmas, |
| 1756 | * the parameters are directly copied from the vma itself and always |
| 1757 | * valid - therefore do_munmap cannot fail. (famous last words?) |
| 1758 | */ |
| 1759 | /* |
| 1760 | * If it had been mremap()'d, the starting address would not |
| 1761 | * match the usual checks anyway. So assume all vma's are |
| 1762 | * above the starting address given. |
| 1763 | */ |
| 1764 | vma = find_vma(mm, addr); |
| 1765 | |
| 1766 | #ifdef CONFIG_MMU |
| 1767 | while (vma) { |
| 1768 | next = vma->vm_next; |
| 1769 | |
| 1770 | /* |
| 1771 | * Check if the starting address would match, i.e. it's |
| 1772 | * a fragment created by mprotect() and/or munmap(), or it |
| 1773 | * otherwise it starts at this address with no hassles. |
| 1774 | */ |
| 1775 | if ((vma->vm_ops == &shm_vm_ops) && |
| 1776 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { |
| 1777 | |
| 1778 | /* |
| 1779 | * Record the file of the shm segment being |
| 1780 | * unmapped. With mremap(), someone could place |
| 1781 | * page from another segment but with equal offsets |
| 1782 | * in the range we are unmapping. |
| 1783 | */ |
| 1784 | file = vma->vm_file; |
| 1785 | size = i_size_read(file_inode(vma->vm_file)); |
| 1786 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL); |
| 1787 | /* |
| 1788 | * We discovered the size of the shm segment, so |
| 1789 | * break out of here and fall through to the next |
| 1790 | * loop that uses the size information to stop |
| 1791 | * searching for matching vma's. |
| 1792 | */ |
| 1793 | retval = 0; |
| 1794 | vma = next; |
| 1795 | break; |
| 1796 | } |
| 1797 | vma = next; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * We need look no further than the maximum address a fragment |
| 1802 | * could possibly have landed at. Also cast things to loff_t to |
| 1803 | * prevent overflows and make comparisons vs. equal-width types. |
| 1804 | */ |
| 1805 | size = PAGE_ALIGN(size); |
| 1806 | while (vma && (loff_t)(vma->vm_end - addr) <= size) { |
| 1807 | next = vma->vm_next; |
| 1808 | |
| 1809 | /* finding a matching vma now does not alter retval */ |
| 1810 | if ((vma->vm_ops == &shm_vm_ops) && |
| 1811 | ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) && |
| 1812 | (vma->vm_file == file)) |
| 1813 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL); |
| 1814 | vma = next; |
| 1815 | } |
| 1816 | |
| 1817 | #else /* CONFIG_MMU */ |
| 1818 | /* under NOMMU conditions, the exact address to be destroyed must be |
| 1819 | * given |
| 1820 | */ |
| 1821 | if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) { |
| 1822 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL); |
| 1823 | retval = 0; |
| 1824 | } |
| 1825 | |
| 1826 | #endif |
| 1827 | |
| 1828 | up_write(&mm->mmap_sem); |
| 1829 | return retval; |
| 1830 | } |
| 1831 | |
| 1832 | SYSCALL_DEFINE1(shmdt, char __user *, shmaddr) |
| 1833 | { |
| 1834 | return ksys_shmdt(shmaddr); |
| 1835 | } |
| 1836 | |
| 1837 | #ifdef CONFIG_PROC_FS |
| 1838 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it) |
| 1839 | { |
| 1840 | struct pid_namespace *pid_ns = ipc_seq_pid_ns(s); |
| 1841 | struct user_namespace *user_ns = seq_user_ns(s); |
| 1842 | struct kern_ipc_perm *ipcp = it; |
| 1843 | struct shmid_kernel *shp; |
| 1844 | unsigned long rss = 0, swp = 0; |
| 1845 | |
| 1846 | shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 1847 | shm_add_rss_swap(shp, &rss, &swp); |
| 1848 | |
| 1849 | #if BITS_PER_LONG <= 32 |
| 1850 | #define SIZE_SPEC "%10lu" |
| 1851 | #else |
| 1852 | #define SIZE_SPEC "%21lu" |
| 1853 | #endif |
| 1854 | |
| 1855 | seq_printf(s, |
| 1856 | "%10d %10d %4o " SIZE_SPEC " %5u %5u " |
| 1857 | "%5lu %5u %5u %5u %5u %10llu %10llu %10llu " |
| 1858 | SIZE_SPEC " " SIZE_SPEC "\n", |
| 1859 | shp->shm_perm.key, |
| 1860 | shp->shm_perm.id, |
| 1861 | shp->shm_perm.mode, |
| 1862 | shp->shm_segsz, |
| 1863 | pid_nr_ns(shp->shm_cprid, pid_ns), |
| 1864 | pid_nr_ns(shp->shm_lprid, pid_ns), |
| 1865 | shp->shm_nattch, |
| 1866 | from_kuid_munged(user_ns, shp->shm_perm.uid), |
| 1867 | from_kgid_munged(user_ns, shp->shm_perm.gid), |
| 1868 | from_kuid_munged(user_ns, shp->shm_perm.cuid), |
| 1869 | from_kgid_munged(user_ns, shp->shm_perm.cgid), |
| 1870 | shp->shm_atim, |
| 1871 | shp->shm_dtim, |
| 1872 | shp->shm_ctim, |
| 1873 | rss * PAGE_SIZE, |
| 1874 | swp * PAGE_SIZE); |
| 1875 | |
| 1876 | return 0; |
| 1877 | } |
| 1878 | #endif |