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 | * |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 16 | * support for audit of ipc object properties and permission changes |
| 17 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 18 | * |
| 19 | * namespaces support |
| 20 | * OpenVZ, SWsoft Inc. |
| 21 | * Pavel Emelianov <xemul@openvz.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/hugetlb.h> |
| 27 | #include <linux/shm.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/file.h> |
| 30 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/shmem_fs.h> |
| 32 | #include <linux/security.h> |
| 33 | #include <linux/syscalls.h> |
| 34 | #include <linux/audit.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 35 | #include <linux/capability.h> |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 36 | #include <linux/ptrace.h> |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 37 | #include <linux/seq_file.h> |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 38 | #include <linux/rwsem.h> |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 39 | #include <linux/nsproxy.h> |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 40 | #include <linux/mount.h> |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
| 43 | |
| 44 | #include "util.h" |
| 45 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 46 | struct shm_file_data { |
| 47 | int id; |
| 48 | struct ipc_namespace *ns; |
| 49 | struct file *file; |
| 50 | const struct vm_operations_struct *vm_ops; |
| 51 | }; |
| 52 | |
| 53 | #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data)) |
| 54 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 55 | static const struct file_operations shm_file_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct vm_operations_struct shm_vm_ops; |
| 57 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 58 | static struct ipc_ids init_shm_ids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 60 | #define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 62 | #define shm_unlock(shp) \ |
| 63 | ipc_unlock(&(shp)->shm_perm) |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 64 | #define shm_buildid(id, seq) ipc_buildid(id, seq) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 65 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 66 | static int newseg(struct ipc_namespace *, struct ipc_params *); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 67 | static void shm_open(struct vm_area_struct *vma); |
| 68 | static void shm_close(struct vm_area_struct *vma); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 69 | static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 71 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #endif |
| 73 | |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame] | 74 | static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 75 | { |
| 76 | ns->ids[IPC_SHM_IDS] = ids; |
| 77 | ns->shm_ctlmax = SHMMAX; |
| 78 | ns->shm_ctlall = SHMALL; |
| 79 | ns->shm_ctlmni = SHMMNI; |
| 80 | ns->shm_tot = 0; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 81 | ipc_init_ids(ids); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 82 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 84 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 85 | * Called with shm_ids.rw_mutex (writer) and the shp structure locked. |
| 86 | * Only shm_ids.rw_mutex remains locked on exit. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 87 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 88 | static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp) |
| 89 | { |
| 90 | if (shp->shm_nattch){ |
| 91 | shp->shm_perm.mode |= SHM_DEST; |
| 92 | /* Do not find it any more */ |
| 93 | shp->shm_perm.key = IPC_PRIVATE; |
| 94 | shm_unlock(shp); |
| 95 | } else |
| 96 | shm_destroy(ns, shp); |
| 97 | } |
| 98 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 99 | int shm_init_ns(struct ipc_namespace *ns) |
| 100 | { |
| 101 | struct ipc_ids *ids; |
| 102 | |
| 103 | ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL); |
| 104 | if (ids == NULL) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | __shm_init_ns(ns, ids); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | void shm_exit_ns(struct ipc_namespace *ns) |
| 112 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 113 | struct shmid_kernel *shp; |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 114 | struct kern_ipc_perm *perm; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 115 | int next_id; |
| 116 | int total, in_use; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 117 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 118 | down_write(&shm_ids(ns).rw_mutex); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 119 | |
| 120 | in_use = shm_ids(ns).in_use; |
| 121 | |
| 122 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 123 | perm = idr_find(&shm_ids(ns).ipcs_idr, next_id); |
| 124 | if (perm == NULL) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 125 | continue; |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 126 | ipc_lock_by_ptr(perm); |
| 127 | shp = container_of(perm, struct shmid_kernel, shm_perm); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 128 | do_shm_rmid(ns, shp); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 129 | total++; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 130 | } |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 131 | up_write(&shm_ids(ns).rw_mutex); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 132 | |
| 133 | kfree(ns->ids[IPC_SHM_IDS]); |
| 134 | ns->ids[IPC_SHM_IDS] = NULL; |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | void __init shm_init (void) |
| 138 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 139 | __shm_init_ns(&init_ipc_ns, &init_shm_ids); |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 140 | ipc_init_proc_interface("sysvipc/shm", |
| 141 | " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n", |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 142 | IPC_SHM_IDS, sysvipc_shm_proc_show); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 145 | /* |
| 146 | * shm_lock_(check_)down routines are called in the paths where the rw_mutex |
| 147 | * is held to protect access to the idr tree. |
| 148 | */ |
| 149 | static inline struct shmid_kernel *shm_lock_down(struct ipc_namespace *ns, |
| 150 | int id) |
| 151 | { |
| 152 | struct kern_ipc_perm *ipcp = ipc_lock_down(&shm_ids(ns), id); |
| 153 | |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 154 | if (IS_ERR(ipcp)) |
| 155 | return (struct shmid_kernel *)ipcp; |
| 156 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 157 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
| 158 | } |
| 159 | |
| 160 | static inline struct shmid_kernel *shm_lock_check_down( |
| 161 | struct ipc_namespace *ns, |
| 162 | int id) |
| 163 | { |
| 164 | struct kern_ipc_perm *ipcp = ipc_lock_check_down(&shm_ids(ns), id); |
| 165 | |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 166 | if (IS_ERR(ipcp)) |
| 167 | return (struct shmid_kernel *)ipcp; |
| 168 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 169 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * shm_lock_(check_) routines are called in the paths where the rw_mutex |
| 174 | * is not held. |
| 175 | */ |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 176 | static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 178 | struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id); |
| 179 | |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 180 | if (IS_ERR(ipcp)) |
| 181 | return (struct shmid_kernel *)ipcp; |
| 182 | |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 183 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns, |
| 187 | int id) |
| 188 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 189 | struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id); |
| 190 | |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame^] | 191 | if (IS_ERR(ipcp)) |
| 192 | return (struct shmid_kernel *)ipcp; |
| 193 | |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 194 | return container_of(ipcp, struct shmid_kernel, shm_perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 197 | static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 199 | ipc_rmid(&shm_ids(ns), &s->shm_perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 202 | static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 204 | return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | |
| 208 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 209 | /* This is called by fork, once for every shm attach. */ |
| 210 | static void shm_open(struct vm_area_struct *vma) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 211 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 212 | struct file *file = vma->vm_file; |
| 213 | struct shm_file_data *sfd = shm_file_data(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | struct shmid_kernel *shp; |
| 215 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 216 | shp = shm_lock(sfd->ns, sfd->id); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 217 | BUG_ON(IS_ERR(shp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | shp->shm_atim = get_seconds(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 219 | shp->shm_lprid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | shp->shm_nattch++; |
| 221 | shm_unlock(shp); |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * shm_destroy - free the struct shmid_kernel |
| 226 | * |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 227 | * @ns: namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | * @shp: struct to free |
| 229 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 230 | * It has to be called with shp and shm_ids.rw_mutex (writer) locked, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | * but returns with shp unlocked and freed. |
| 232 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 233 | static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 235 | ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 236 | shm_rmid(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | shm_unlock(shp); |
| 238 | if (!is_file_hugepages(shp->shm_file)) |
| 239 | shmem_lock(shp->shm_file, 0, shp->mlock_user); |
| 240 | else |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 241 | user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | shp->mlock_user); |
| 243 | fput (shp->shm_file); |
| 244 | security_shm_free(shp); |
| 245 | ipc_rcu_putref(shp); |
| 246 | } |
| 247 | |
| 248 | /* |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 249 | * remove the attach descriptor vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | * free memory for segment if it is marked destroyed. |
| 251 | * The descriptor has already been removed from the current->mm->mmap list |
| 252 | * and will later be kfree()d. |
| 253 | */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 254 | static void shm_close(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 256 | struct file * file = vma->vm_file; |
| 257 | struct shm_file_data *sfd = shm_file_data(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | struct shmid_kernel *shp; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 259 | struct ipc_namespace *ns = sfd->ns; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 260 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 261 | down_write(&shm_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* remove from the list of attaches of the shm segment */ |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 263 | shp = shm_lock_down(ns, sfd->id); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 264 | BUG_ON(IS_ERR(shp)); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 265 | shp->shm_lprid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | shp->shm_dtim = get_seconds(); |
| 267 | shp->shm_nattch--; |
| 268 | if(shp->shm_nattch == 0 && |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 269 | shp->shm_perm.mode & SHM_DEST) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 270 | shm_destroy(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | else |
| 272 | shm_unlock(shp); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 273 | up_write(&shm_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 276 | static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 277 | { |
| 278 | struct file *file = vma->vm_file; |
| 279 | struct shm_file_data *sfd = shm_file_data(file); |
| 280 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 281 | return sfd->vm_ops->fault(vma, vmf); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #ifdef CONFIG_NUMA |
Adrian Bunk | d823e3e | 2007-10-16 23:26:42 -0700 | [diff] [blame] | 285 | static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 286 | { |
| 287 | struct file *file = vma->vm_file; |
| 288 | struct shm_file_data *sfd = shm_file_data(file); |
| 289 | int err = 0; |
| 290 | if (sfd->vm_ops->set_policy) |
| 291 | err = sfd->vm_ops->set_policy(vma, new); |
| 292 | return err; |
| 293 | } |
| 294 | |
Adrian Bunk | d823e3e | 2007-10-16 23:26:42 -0700 | [diff] [blame] | 295 | static struct mempolicy *shm_get_policy(struct vm_area_struct *vma, |
| 296 | unsigned long addr) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 297 | { |
| 298 | struct file *file = vma->vm_file; |
| 299 | struct shm_file_data *sfd = shm_file_data(file); |
| 300 | struct mempolicy *pol = NULL; |
| 301 | |
| 302 | if (sfd->vm_ops->get_policy) |
| 303 | pol = sfd->vm_ops->get_policy(vma, addr); |
Adam Litke | 2274192 | 2007-06-16 10:16:15 -0700 | [diff] [blame] | 304 | else if (vma->vm_policy) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 305 | pol = vma->vm_policy; |
Adam Litke | 2274192 | 2007-06-16 10:16:15 -0700 | [diff] [blame] | 306 | else |
| 307 | pol = current->mempolicy; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 308 | return pol; |
| 309 | } |
| 310 | #endif |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | static int shm_mmap(struct file * file, struct vm_area_struct * vma) |
| 313 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 314 | struct shm_file_data *sfd = shm_file_data(file); |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 315 | int ret; |
| 316 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 317 | ret = sfd->file->f_op->mmap(sfd->file, vma); |
| 318 | if (ret != 0) |
| 319 | return ret; |
| 320 | sfd->vm_ops = vma->vm_ops; |
David Howells | 2e92a3b | 2007-07-31 00:37:24 -0700 | [diff] [blame] | 321 | #ifdef CONFIG_MMU |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 322 | BUG_ON(!sfd->vm_ops->fault); |
David Howells | 2e92a3b | 2007-07-31 00:37:24 -0700 | [diff] [blame] | 323 | #endif |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 324 | vma->vm_ops = &shm_vm_ops; |
| 325 | shm_open(vma); |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 326 | |
| 327 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 330 | static int shm_release(struct inode *ino, struct file *file) |
| 331 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 332 | struct shm_file_data *sfd = shm_file_data(file); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 333 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 334 | put_ipc_ns(sfd->ns); |
| 335 | shm_file_data(file) = NULL; |
| 336 | kfree(sfd); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 340 | static int shm_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 341 | { |
| 342 | int (*fsync) (struct file *, struct dentry *, int datasync); |
| 343 | struct shm_file_data *sfd = shm_file_data(file); |
| 344 | int ret = -EINVAL; |
| 345 | |
| 346 | fsync = sfd->file->f_op->fsync; |
| 347 | if (fsync) |
| 348 | ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 352 | static unsigned long shm_get_unmapped_area(struct file *file, |
| 353 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 354 | unsigned long flags) |
| 355 | { |
| 356 | struct shm_file_data *sfd = shm_file_data(file); |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 357 | return get_unmapped_area(sfd->file, addr, len, pgoff, flags); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 358 | } |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 359 | |
| 360 | int is_file_shm_hugepages(struct file *file) |
| 361 | { |
| 362 | int ret = 0; |
| 363 | |
| 364 | if (file->f_op == &shm_file_operations) { |
| 365 | struct shm_file_data *sfd; |
| 366 | sfd = shm_file_data(file); |
| 367 | ret = is_file_hugepages(sfd->file); |
| 368 | } |
| 369 | return ret; |
| 370 | } |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 371 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 372 | static const struct file_operations shm_file_operations = { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 373 | .mmap = shm_mmap, |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 374 | .fsync = shm_fsync, |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 375 | .release = shm_release, |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 376 | .get_unmapped_area = shm_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static struct vm_operations_struct shm_vm_ops = { |
| 380 | .open = shm_open, /* callback for a new vm-area open */ |
| 381 | .close = shm_close, /* callback for when the vm-area is released */ |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 382 | .fault = shm_fault, |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 383 | #if defined(CONFIG_NUMA) |
| 384 | .set_policy = shm_set_policy, |
| 385 | .get_policy = shm_get_policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | #endif |
| 387 | }; |
| 388 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 389 | /** |
| 390 | * newseg - Create a new shared memory segment |
| 391 | * @ns: namespace |
| 392 | * @params: ptr to the structure that contains key, size and shmflg |
| 393 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 394 | * Called with shm_ids.rw_mutex held as a writer. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 395 | */ |
| 396 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 397 | static int newseg(struct ipc_namespace *ns, struct ipc_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 399 | key_t key = params->key; |
| 400 | int shmflg = params->flg; |
| 401 | size_t size = params->u.size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | int error; |
| 403 | struct shmid_kernel *shp; |
| 404 | int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT; |
| 405 | struct file * file; |
| 406 | char name[13]; |
| 407 | int id; |
| 408 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 409 | if (size < SHMMIN || size > ns->shm_ctlmax) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | return -EINVAL; |
| 411 | |
Guy Streeter | f66d45e | 2007-01-23 12:20:04 -0600 | [diff] [blame] | 412 | if (ns->shm_tot + numpages > ns->shm_ctlall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return -ENOSPC; |
| 414 | |
| 415 | shp = ipc_rcu_alloc(sizeof(*shp)); |
| 416 | if (!shp) |
| 417 | return -ENOMEM; |
| 418 | |
| 419 | shp->shm_perm.key = key; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 420 | shp->shm_perm.mode = (shmflg & S_IRWXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | shp->mlock_user = NULL; |
| 422 | |
| 423 | shp->shm_perm.security = NULL; |
| 424 | error = security_shm_alloc(shp); |
| 425 | if (error) { |
| 426 | ipc_rcu_putref(shp); |
| 427 | return error; |
| 428 | } |
| 429 | |
Eric W. Biederman | 9d66586 | 2007-06-16 10:16:16 -0700 | [diff] [blame] | 430 | sprintf (name, "SYSV%08x", key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (shmflg & SHM_HUGETLB) { |
Eric W. Biederman | 9d66586 | 2007-06-16 10:16:16 -0700 | [diff] [blame] | 432 | /* hugetlb_file_setup takes care of mlock user accounting */ |
| 433 | file = hugetlb_file_setup(name, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | shp->mlock_user = current->user; |
| 435 | } else { |
Badari Pulavarty | bf8f972 | 2005-11-07 00:59:27 -0800 | [diff] [blame] | 436 | int acctflag = VM_ACCOUNT; |
| 437 | /* |
| 438 | * Do not allow no accounting for OVERCOMMIT_NEVER, even |
| 439 | * if it's asked for. |
| 440 | */ |
| 441 | if ((shmflg & SHM_NORESERVE) && |
| 442 | sysctl_overcommit_memory != OVERCOMMIT_NEVER) |
| 443 | acctflag = 0; |
Badari Pulavarty | bf8f972 | 2005-11-07 00:59:27 -0800 | [diff] [blame] | 444 | file = shmem_file_setup(name, size, acctflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | error = PTR_ERR(file); |
| 447 | if (IS_ERR(file)) |
| 448 | goto no_file; |
| 449 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 450 | id = shm_addid(ns, shp); |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 451 | if (id < 0) { |
| 452 | error = id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | goto no_id; |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 456 | shp->shm_cprid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | shp->shm_lprid = 0; |
| 458 | shp->shm_atim = shp->shm_dtim = 0; |
| 459 | shp->shm_ctim = get_seconds(); |
| 460 | shp->shm_segsz = size; |
| 461 | shp->shm_nattch = 0; |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 462 | shp->shm_perm.id = shm_buildid(id, shp->shm_perm.seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | shp->shm_file = file; |
Badari Pulavarty | 30475cc | 2007-06-16 10:15:59 -0700 | [diff] [blame] | 464 | /* |
| 465 | * shmid gets reported as "inode#" in /proc/pid/maps. |
| 466 | * proc-ps tools use this. Changing this will break them. |
| 467 | */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 468 | file->f_dentry->d_inode->i_ino = shp->shm_perm.id; |
Krishnakumar R | 551110a | 2005-10-29 18:16:45 -0700 | [diff] [blame] | 469 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 470 | ns->shm_tot += numpages; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 471 | error = shp->shm_perm.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | shm_unlock(shp); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 473 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | no_id: |
| 476 | fput(file); |
| 477 | no_file: |
| 478 | security_shm_free(shp); |
| 479 | ipc_rcu_putref(shp); |
| 480 | return error; |
| 481 | } |
| 482 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 483 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 484 | * Called with shm_ids.rw_mutex and ipcp locked. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 485 | */ |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 486 | static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 487 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 488 | struct shmid_kernel *shp; |
| 489 | |
| 490 | shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 491 | return security_shm_associate(shp, shmflg); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 494 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 495 | * Called with shm_ids.rw_mutex and ipcp locked. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 496 | */ |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 497 | static inline int shm_more_checks(struct kern_ipc_perm *ipcp, |
| 498 | struct ipc_params *params) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 499 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 500 | struct shmid_kernel *shp; |
| 501 | |
| 502 | shp = container_of(ipcp, struct shmid_kernel, shm_perm); |
| 503 | if (shp->shm_segsz < params->u.size) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 504 | return -EINVAL; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | asmlinkage long sys_shmget (key_t key, size_t size, int shmflg) |
| 510 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 511 | struct ipc_namespace *ns; |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 512 | struct ipc_ops shm_ops; |
| 513 | struct ipc_params shm_params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 515 | ns = current->nsproxy->ipc_ns; |
| 516 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 517 | shm_ops.getnew = newseg; |
| 518 | shm_ops.associate = shm_security; |
| 519 | shm_ops.more_checks = shm_more_checks; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 520 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 521 | shm_params.key = key; |
| 522 | shm_params.flg = shmflg; |
| 523 | shm_params.u.size = size; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 524 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 525 | return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version) |
| 529 | { |
| 530 | switch(version) { |
| 531 | case IPC_64: |
| 532 | return copy_to_user(buf, in, sizeof(*in)); |
| 533 | case IPC_OLD: |
| 534 | { |
| 535 | struct shmid_ds out; |
| 536 | |
| 537 | ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm); |
| 538 | out.shm_segsz = in->shm_segsz; |
| 539 | out.shm_atime = in->shm_atime; |
| 540 | out.shm_dtime = in->shm_dtime; |
| 541 | out.shm_ctime = in->shm_ctime; |
| 542 | out.shm_cpid = in->shm_cpid; |
| 543 | out.shm_lpid = in->shm_lpid; |
| 544 | out.shm_nattch = in->shm_nattch; |
| 545 | |
| 546 | return copy_to_user(buf, &out, sizeof(out)); |
| 547 | } |
| 548 | default: |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | struct shm_setbuf { |
| 554 | uid_t uid; |
| 555 | gid_t gid; |
| 556 | mode_t mode; |
| 557 | }; |
| 558 | |
| 559 | static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version) |
| 560 | { |
| 561 | switch(version) { |
| 562 | case IPC_64: |
| 563 | { |
| 564 | struct shmid64_ds tbuf; |
| 565 | |
| 566 | if (copy_from_user(&tbuf, buf, sizeof(tbuf))) |
| 567 | return -EFAULT; |
| 568 | |
| 569 | out->uid = tbuf.shm_perm.uid; |
| 570 | out->gid = tbuf.shm_perm.gid; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 571 | out->mode = tbuf.shm_perm.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | case IPC_OLD: |
| 576 | { |
| 577 | struct shmid_ds tbuf_old; |
| 578 | |
| 579 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
| 580 | return -EFAULT; |
| 581 | |
| 582 | out->uid = tbuf_old.shm_perm.uid; |
| 583 | out->gid = tbuf_old.shm_perm.gid; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 584 | out->mode = tbuf_old.shm_perm.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | default: |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version) |
| 594 | { |
| 595 | switch(version) { |
| 596 | case IPC_64: |
| 597 | return copy_to_user(buf, in, sizeof(*in)); |
| 598 | case IPC_OLD: |
| 599 | { |
| 600 | struct shminfo out; |
| 601 | |
| 602 | if(in->shmmax > INT_MAX) |
| 603 | out.shmmax = INT_MAX; |
| 604 | else |
| 605 | out.shmmax = (int)in->shmmax; |
| 606 | |
| 607 | out.shmmin = in->shmmin; |
| 608 | out.shmmni = in->shmmni; |
| 609 | out.shmseg = in->shmseg; |
| 610 | out.shmall = in->shmall; |
| 611 | |
| 612 | return copy_to_user(buf, &out, sizeof(out)); |
| 613 | } |
| 614 | default: |
| 615 | return -EINVAL; |
| 616 | } |
| 617 | } |
| 618 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 619 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 620 | * Called with shm_ids.rw_mutex held as a reader |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 621 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 622 | static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss, |
| 623 | unsigned long *swp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 625 | int next_id; |
| 626 | int total, in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | *rss = 0; |
| 629 | *swp = 0; |
| 630 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 631 | in_use = shm_ids(ns).in_use; |
| 632 | |
| 633 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | struct shmid_kernel *shp; |
| 635 | struct inode *inode; |
| 636 | |
Nadia Derbey | 637c366 | 2007-10-18 23:40:50 -0700 | [diff] [blame] | 637 | shp = idr_find(&shm_ids(ns).ipcs_idr, next_id); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 638 | if (shp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | continue; |
| 640 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 641 | inode = shp->shm_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | if (is_file_hugepages(shp->shm_file)) { |
| 644 | struct address_space *mapping = inode->i_mapping; |
| 645 | *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages; |
| 646 | } else { |
| 647 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 648 | spin_lock(&info->lock); |
| 649 | *rss += inode->i_mapping->nrpages; |
| 650 | *swp += info->swapped; |
| 651 | spin_unlock(&info->lock); |
| 652 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 653 | |
| 654 | total++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf) |
| 659 | { |
| 660 | struct shm_setbuf setbuf; |
| 661 | struct shmid_kernel *shp; |
| 662 | int err, version; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 663 | struct ipc_namespace *ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | if (cmd < 0 || shmid < 0) { |
| 666 | err = -EINVAL; |
| 667 | goto out; |
| 668 | } |
| 669 | |
| 670 | version = ipc_parse_version(&cmd); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 671 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | switch (cmd) { /* replace with proc interface ? */ |
| 674 | case IPC_INFO: |
| 675 | { |
| 676 | struct shminfo64 shminfo; |
| 677 | |
| 678 | err = security_shm_shmctl(NULL, cmd); |
| 679 | if (err) |
| 680 | return err; |
| 681 | |
| 682 | memset(&shminfo,0,sizeof(shminfo)); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 683 | shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni; |
| 684 | shminfo.shmmax = ns->shm_ctlmax; |
| 685 | shminfo.shmall = ns->shm_ctlall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
| 687 | shminfo.shmmin = SHMMIN; |
| 688 | if(copy_shminfo_to_user (buf, &shminfo, version)) |
| 689 | return -EFAULT; |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 690 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 691 | down_read(&shm_ids(ns).rw_mutex); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 692 | err = ipc_get_maxid(&shm_ids(ns)); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 693 | up_read(&shm_ids(ns).rw_mutex); |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | if(err<0) |
| 696 | err = 0; |
| 697 | goto out; |
| 698 | } |
| 699 | case SHM_INFO: |
| 700 | { |
| 701 | struct shm_info shm_info; |
| 702 | |
| 703 | err = security_shm_shmctl(NULL, cmd); |
| 704 | if (err) |
| 705 | return err; |
| 706 | |
| 707 | memset(&shm_info,0,sizeof(shm_info)); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 708 | down_read(&shm_ids(ns).rw_mutex); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 709 | shm_info.used_ids = shm_ids(ns).in_use; |
| 710 | shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp); |
| 711 | shm_info.shm_tot = ns->shm_tot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | shm_info.swap_attempts = 0; |
| 713 | shm_info.swap_successes = 0; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 714 | err = ipc_get_maxid(&shm_ids(ns)); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 715 | up_read(&shm_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | if(copy_to_user (buf, &shm_info, sizeof(shm_info))) { |
| 717 | err = -EFAULT; |
| 718 | goto out; |
| 719 | } |
| 720 | |
| 721 | err = err < 0 ? 0 : err; |
| 722 | goto out; |
| 723 | } |
| 724 | case SHM_STAT: |
| 725 | case IPC_STAT: |
| 726 | { |
| 727 | struct shmid64_ds tbuf; |
| 728 | int result; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 729 | |
| 730 | if (!buf) { |
| 731 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | goto out; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (cmd == SHM_STAT) { |
| 736 | shp = shm_lock(ns, shmid); |
| 737 | if (IS_ERR(shp)) { |
| 738 | err = PTR_ERR(shp); |
| 739 | goto out; |
| 740 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 741 | result = shp->shm_perm.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } else { |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 743 | shp = shm_lock_check(ns, shmid); |
| 744 | if (IS_ERR(shp)) { |
| 745 | err = PTR_ERR(shp); |
| 746 | goto out; |
| 747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | result = 0; |
| 749 | } |
| 750 | err=-EACCES; |
| 751 | if (ipcperms (&shp->shm_perm, S_IRUGO)) |
| 752 | goto out_unlock; |
| 753 | err = security_shm_shmctl(shp, cmd); |
| 754 | if (err) |
| 755 | goto out_unlock; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 756 | memset(&tbuf, 0, sizeof(tbuf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm); |
| 758 | tbuf.shm_segsz = shp->shm_segsz; |
| 759 | tbuf.shm_atime = shp->shm_atim; |
| 760 | tbuf.shm_dtime = shp->shm_dtim; |
| 761 | tbuf.shm_ctime = shp->shm_ctim; |
| 762 | tbuf.shm_cpid = shp->shm_cprid; |
| 763 | tbuf.shm_lpid = shp->shm_lprid; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 764 | tbuf.shm_nattch = shp->shm_nattch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | shm_unlock(shp); |
| 766 | if(copy_shmid_to_user (buf, &tbuf, version)) |
| 767 | err = -EFAULT; |
| 768 | else |
| 769 | err = result; |
| 770 | goto out; |
| 771 | } |
| 772 | case SHM_LOCK: |
| 773 | case SHM_UNLOCK: |
| 774 | { |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 775 | shp = shm_lock_check(ns, shmid); |
| 776 | if (IS_ERR(shp)) { |
| 777 | err = PTR_ERR(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | goto out; |
| 779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 781 | err = audit_ipc_obj(&(shp->shm_perm)); |
| 782 | if (err) |
| 783 | goto out_unlock; |
| 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (!capable(CAP_IPC_LOCK)) { |
| 786 | err = -EPERM; |
| 787 | if (current->euid != shp->shm_perm.uid && |
| 788 | current->euid != shp->shm_perm.cuid) |
| 789 | goto out_unlock; |
| 790 | if (cmd == SHM_LOCK && |
| 791 | !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur) |
| 792 | goto out_unlock; |
| 793 | } |
| 794 | |
| 795 | err = security_shm_shmctl(shp, cmd); |
| 796 | if (err) |
| 797 | goto out_unlock; |
| 798 | |
| 799 | if(cmd==SHM_LOCK) { |
| 800 | struct user_struct * user = current->user; |
| 801 | if (!is_file_hugepages(shp->shm_file)) { |
| 802 | err = shmem_lock(shp->shm_file, 1, user); |
Pavel Emelianov | 7be77e2 | 2007-07-31 00:38:48 -0700 | [diff] [blame] | 803 | if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){ |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 804 | shp->shm_perm.mode |= SHM_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | shp->mlock_user = user; |
| 806 | } |
| 807 | } |
| 808 | } else if (!is_file_hugepages(shp->shm_file)) { |
| 809 | shmem_lock(shp->shm_file, 0, shp->mlock_user); |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 810 | shp->shm_perm.mode &= ~SHM_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | shp->mlock_user = NULL; |
| 812 | } |
| 813 | shm_unlock(shp); |
| 814 | goto out; |
| 815 | } |
| 816 | case IPC_RMID: |
| 817 | { |
| 818 | /* |
| 819 | * We cannot simply remove the file. The SVID states |
| 820 | * that the block remains until the last person |
| 821 | * detaches from it, then is deleted. A shmat() on |
| 822 | * an RMID segment is legal in older Linux and if |
| 823 | * we change it apps break... |
| 824 | * |
| 825 | * Instead we set a destroyed flag, and then blow |
| 826 | * the name away when the usage hits zero. |
| 827 | */ |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 828 | down_write(&shm_ids(ns).rw_mutex); |
| 829 | shp = shm_lock_check_down(ns, shmid); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 830 | if (IS_ERR(shp)) { |
| 831 | err = PTR_ERR(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | goto out_up; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 835 | err = audit_ipc_obj(&(shp->shm_perm)); |
| 836 | if (err) |
| 837 | goto out_unlock_up; |
| 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | if (current->euid != shp->shm_perm.uid && |
| 840 | current->euid != shp->shm_perm.cuid && |
| 841 | !capable(CAP_SYS_ADMIN)) { |
| 842 | err=-EPERM; |
| 843 | goto out_unlock_up; |
| 844 | } |
| 845 | |
| 846 | err = security_shm_shmctl(shp, cmd); |
| 847 | if (err) |
| 848 | goto out_unlock_up; |
| 849 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 850 | do_shm_rmid(ns, shp); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 851 | up_write(&shm_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | case IPC_SET: |
| 856 | { |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 857 | if (!buf) { |
| 858 | err = -EFAULT; |
| 859 | goto out; |
| 860 | } |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | if (copy_shmid_from_user (&setbuf, buf, version)) { |
| 863 | err = -EFAULT; |
| 864 | goto out; |
| 865 | } |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 866 | down_write(&shm_ids(ns).rw_mutex); |
| 867 | shp = shm_lock_check_down(ns, shmid); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 868 | if (IS_ERR(shp)) { |
| 869 | err = PTR_ERR(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | goto out_up; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 871 | } |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 872 | err = audit_ipc_obj(&(shp->shm_perm)); |
| 873 | if (err) |
| 874 | goto out_unlock_up; |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 875 | err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 876 | if (err) |
| 877 | goto out_unlock_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | err=-EPERM; |
| 879 | if (current->euid != shp->shm_perm.uid && |
| 880 | current->euid != shp->shm_perm.cuid && |
| 881 | !capable(CAP_SYS_ADMIN)) { |
| 882 | goto out_unlock_up; |
| 883 | } |
| 884 | |
| 885 | err = security_shm_shmctl(shp, cmd); |
| 886 | if (err) |
| 887 | goto out_unlock_up; |
| 888 | |
| 889 | shp->shm_perm.uid = setbuf.uid; |
| 890 | shp->shm_perm.gid = setbuf.gid; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 891 | shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | | (setbuf.mode & S_IRWXUGO); |
| 893 | shp->shm_ctim = get_seconds(); |
| 894 | break; |
| 895 | } |
| 896 | |
| 897 | default: |
| 898 | err = -EINVAL; |
| 899 | goto out; |
| 900 | } |
| 901 | |
| 902 | err = 0; |
| 903 | out_unlock_up: |
| 904 | shm_unlock(shp); |
| 905 | out_up: |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 906 | up_write(&shm_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | goto out; |
| 908 | out_unlock: |
| 909 | shm_unlock(shp); |
| 910 | out: |
| 911 | return err; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists. |
| 916 | * |
| 917 | * NOTE! Despite the name, this is NOT a direct system call entrypoint. The |
| 918 | * "raddr" thing points to kernel space, and there has to be a wrapper around |
| 919 | * this. |
| 920 | */ |
| 921 | long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr) |
| 922 | { |
| 923 | struct shmid_kernel *shp; |
| 924 | unsigned long addr; |
| 925 | unsigned long size; |
| 926 | struct file * file; |
| 927 | int err; |
| 928 | unsigned long flags; |
| 929 | unsigned long prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | int acc_mode; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 931 | unsigned long user_addr; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 932 | struct ipc_namespace *ns; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 933 | struct shm_file_data *sfd; |
| 934 | struct path path; |
| 935 | mode_t f_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 937 | err = -EINVAL; |
| 938 | if (shmid < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | goto out; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 940 | else if ((addr = (ulong)shmaddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | if (addr & (SHMLBA-1)) { |
| 942 | if (shmflg & SHM_RND) |
| 943 | addr &= ~(SHMLBA-1); /* round down */ |
| 944 | else |
| 945 | #ifndef __ARCH_FORCE_SHMLBA |
| 946 | if (addr & ~PAGE_MASK) |
| 947 | #endif |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 948 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
| 950 | flags = MAP_SHARED | MAP_FIXED; |
| 951 | } else { |
| 952 | if ((shmflg & SHM_REMAP)) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 953 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
| 955 | flags = MAP_SHARED; |
| 956 | } |
| 957 | |
| 958 | if (shmflg & SHM_RDONLY) { |
| 959 | prot = PROT_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | acc_mode = S_IRUGO; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 961 | f_mode = FMODE_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } else { |
| 963 | prot = PROT_READ | PROT_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | acc_mode = S_IRUGO | S_IWUGO; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 965 | f_mode = FMODE_READ | FMODE_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | if (shmflg & SHM_EXEC) { |
| 968 | prot |= PROT_EXEC; |
| 969 | acc_mode |= S_IXUGO; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * We cannot rely on the fs check since SYSV IPC does have an |
| 974 | * additional creator id... |
| 975 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 976 | ns = current->nsproxy->ipc_ns; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 977 | shp = shm_lock_check(ns, shmid); |
| 978 | if (IS_ERR(shp)) { |
| 979 | err = PTR_ERR(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | goto out; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 981 | } |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 982 | |
| 983 | err = -EACCES; |
| 984 | if (ipcperms(&shp->shm_perm, acc_mode)) |
| 985 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
| 987 | err = security_shm_shmat(shp, shmaddr, shmflg); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 988 | if (err) |
| 989 | goto out_unlock; |
| 990 | |
| 991 | path.dentry = dget(shp->shm_file->f_path.dentry); |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 992 | path.mnt = shp->shm_file->f_path.mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | shp->shm_nattch++; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 994 | size = i_size_read(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | shm_unlock(shp); |
| 996 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 997 | err = -ENOMEM; |
| 998 | sfd = kzalloc(sizeof(*sfd), GFP_KERNEL); |
| 999 | if (!sfd) |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 1000 | goto out_put_dentry; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1001 | |
| 1002 | err = -ENOMEM; |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 1003 | |
| 1004 | file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1005 | if (!file) |
| 1006 | goto out_free; |
| 1007 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1008 | file->private_data = sfd; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1009 | file->f_mapping = shp->shm_file->f_mapping; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 1010 | sfd->id = shp->shm_perm.id; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1011 | sfd->ns = get_ipc_ns(ns); |
| 1012 | sfd->file = shp->shm_file; |
| 1013 | sfd->vm_ops = NULL; |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | down_write(¤t->mm->mmap_sem); |
| 1016 | if (addr && !(shmflg & SHM_REMAP)) { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1017 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (find_vma_intersection(current->mm, addr, addr + size)) |
| 1019 | goto invalid; |
| 1020 | /* |
| 1021 | * If shm segment goes below stack, make sure there is some |
| 1022 | * space left for the stack to grow (at least 4 pages). |
| 1023 | */ |
| 1024 | if (addr < current->mm->start_stack && |
| 1025 | addr > current->mm->start_stack - size - PAGE_SIZE * 5) |
| 1026 | goto invalid; |
| 1027 | } |
| 1028 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1029 | user_addr = do_mmap (file, addr, size, prot, flags, 0); |
| 1030 | *raddr = user_addr; |
| 1031 | err = 0; |
| 1032 | if (IS_ERR_VALUE(user_addr)) |
| 1033 | err = (long)user_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | invalid: |
| 1035 | up_write(¤t->mm->mmap_sem); |
| 1036 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1037 | fput(file); |
| 1038 | |
| 1039 | out_nattch: |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 1040 | down_write(&shm_ids(ns).rw_mutex); |
| 1041 | shp = shm_lock_down(ns, shmid); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1042 | BUG_ON(IS_ERR(shp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | shp->shm_nattch--; |
| 1044 | if(shp->shm_nattch == 0 && |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 1045 | shp->shm_perm.mode & SHM_DEST) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1046 | shm_destroy(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | else |
| 1048 | shm_unlock(shp); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 1049 | up_write(&shm_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | out: |
| 1052 | return err; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1053 | |
| 1054 | out_unlock: |
| 1055 | shm_unlock(shp); |
| 1056 | goto out; |
| 1057 | |
| 1058 | out_free: |
| 1059 | kfree(sfd); |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 1060 | out_put_dentry: |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1061 | dput(path.dentry); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1062 | goto out_nattch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 1065 | asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg) |
| 1066 | { |
| 1067 | unsigned long ret; |
| 1068 | long err; |
| 1069 | |
| 1070 | err = do_shmat(shmid, shmaddr, shmflg, &ret); |
| 1071 | if (err) |
| 1072 | return err; |
| 1073 | force_successful_syscall_return(); |
| 1074 | return (long)ret; |
| 1075 | } |
| 1076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | /* |
| 1078 | * detach and kill segment if marked destroyed. |
| 1079 | * The work is done in shm_close. |
| 1080 | */ |
| 1081 | asmlinkage long sys_shmdt(char __user *shmaddr) |
| 1082 | { |
| 1083 | struct mm_struct *mm = current->mm; |
| 1084 | struct vm_area_struct *vma, *next; |
| 1085 | unsigned long addr = (unsigned long)shmaddr; |
| 1086 | loff_t size = 0; |
| 1087 | int retval = -EINVAL; |
| 1088 | |
Hugh Dickins | df1e2fb | 2006-03-24 03:18:06 -0800 | [diff] [blame] | 1089 | if (addr & ~PAGE_MASK) |
| 1090 | return retval; |
| 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | down_write(&mm->mmap_sem); |
| 1093 | |
| 1094 | /* |
| 1095 | * This function tries to be smart and unmap shm segments that |
| 1096 | * were modified by partial mlock or munmap calls: |
| 1097 | * - It first determines the size of the shm segment that should be |
| 1098 | * unmapped: It searches for a vma that is backed by shm and that |
| 1099 | * started at address shmaddr. It records it's size and then unmaps |
| 1100 | * it. |
| 1101 | * - Then it unmaps all shm vmas that started at shmaddr and that |
| 1102 | * are within the initially determined size. |
| 1103 | * Errors from do_munmap are ignored: the function only fails if |
| 1104 | * it's called with invalid parameters or if it's called to unmap |
| 1105 | * a part of a vma. Both calls in this function are for full vmas, |
| 1106 | * the parameters are directly copied from the vma itself and always |
| 1107 | * valid - therefore do_munmap cannot fail. (famous last words?) |
| 1108 | */ |
| 1109 | /* |
| 1110 | * If it had been mremap()'d, the starting address would not |
| 1111 | * match the usual checks anyway. So assume all vma's are |
| 1112 | * above the starting address given. |
| 1113 | */ |
| 1114 | vma = find_vma(mm, addr); |
| 1115 | |
| 1116 | while (vma) { |
| 1117 | next = vma->vm_next; |
| 1118 | |
| 1119 | /* |
| 1120 | * Check if the starting address would match, i.e. it's |
| 1121 | * a fragment created by mprotect() and/or munmap(), or it |
| 1122 | * otherwise it starts at this address with no hassles. |
| 1123 | */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1124 | if ((vma->vm_ops == &shm_vm_ops) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { |
| 1126 | |
| 1127 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1128 | size = vma->vm_file->f_path.dentry->d_inode->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1130 | /* |
| 1131 | * We discovered the size of the shm segment, so |
| 1132 | * break out of here and fall through to the next |
| 1133 | * loop that uses the size information to stop |
| 1134 | * searching for matching vma's. |
| 1135 | */ |
| 1136 | retval = 0; |
| 1137 | vma = next; |
| 1138 | break; |
| 1139 | } |
| 1140 | vma = next; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * We need look no further than the maximum address a fragment |
| 1145 | * could possibly have landed at. Also cast things to loff_t to |
| 1146 | * prevent overflows and make comparisions vs. equal-width types. |
| 1147 | */ |
KAMEZAWA Hiroyuki | 8e36709 | 2006-02-10 01:51:12 -0800 | [diff] [blame] | 1148 | size = PAGE_ALIGN(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | while (vma && (loff_t)(vma->vm_end - addr) <= size) { |
| 1150 | next = vma->vm_next; |
| 1151 | |
| 1152 | /* finding a matching vma now does not alter retval */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1153 | if ((vma->vm_ops == &shm_vm_ops) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) |
| 1155 | |
| 1156 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1157 | vma = next; |
| 1158 | } |
| 1159 | |
| 1160 | up_write(&mm->mmap_sem); |
| 1161 | return retval; |
| 1162 | } |
| 1163 | |
| 1164 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1165 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | { |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1167 | struct shmid_kernel *shp = it; |
| 1168 | char *format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
| 1171 | #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1173 | if (sizeof(size_t) <= sizeof(int)) |
| 1174 | format = SMALL_STRING; |
| 1175 | else |
| 1176 | format = BIG_STRING; |
| 1177 | return seq_printf(s, format, |
| 1178 | shp->shm_perm.key, |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 1179 | shp->shm_perm.id, |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 1180 | shp->shm_perm.mode, |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1181 | shp->shm_segsz, |
| 1182 | shp->shm_cprid, |
| 1183 | shp->shm_lprid, |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1184 | shp->shm_nattch, |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1185 | shp->shm_perm.uid, |
| 1186 | shp->shm_perm.gid, |
| 1187 | shp->shm_perm.cuid, |
| 1188 | shp->shm_perm.cgid, |
| 1189 | shp->shm_atim, |
| 1190 | shp->shm_dtim, |
| 1191 | shp->shm_ctim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | #endif |