blob: 8cf1cf3d5becc6180f32ae7a8d0ea41f2878bb10 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Grubb073115d2006-04-02 17:07:33 -040016 * support for audit of ipc object properties and permission changes
17 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev4e982312006-10-02 02:18:22 -070018 *
19 * namespaces support
20 * OpenVZ, SWsoft Inc.
21 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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 Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/shmem_fs.h>
32#include <linux/security.h>
33#include <linux/syscalls.h>
34#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070036#include <linux/ptrace.h>
Mike Waychison19b49462005-09-06 15:17:10 -070037#include <linux/seq_file.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080038#include <linux/mutex.h>
Kirill Korotaev4e982312006-10-02 02:18:22 -070039#include <linux/nsproxy.h>
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080040#include <linux/mount.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43
44#include "util.h"
45
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080046struct 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 Ven9a321442007-02-12 00:55:35 -080055static const struct file_operations shm_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct vm_operations_struct shm_vm_ops;
57
Kirill Korotaev4e982312006-10-02 02:18:22 -070058static struct ipc_ids init_shm_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Kirill Korotaev4e982312006-10-02 02:18:22 -070060#define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Kirill Korotaev4e982312006-10-02 02:18:22 -070062#define shm_lock(ns, id) \
63 ((struct shmid_kernel*)ipc_lock(&shm_ids(ns),id))
64#define shm_unlock(shp) \
65 ipc_unlock(&(shp)->shm_perm)
Kirill Korotaev4e982312006-10-02 02:18:22 -070066#define shm_buildid(ns, id, seq) \
67 ipc_buildid(&shm_ids(ns), id, seq)
68
Nadia Derbey7748dbf2007-10-18 23:40:49 -070069static int newseg(struct ipc_namespace *, struct ipc_params *);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080070static void shm_open(struct vm_area_struct *vma);
71static void shm_close(struct vm_area_struct *vma);
Kirill Korotaev4e982312006-10-02 02:18:22 -070072static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070074static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070077static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaev4e982312006-10-02 02:18:22 -070078{
79 ns->ids[IPC_SHM_IDS] = ids;
80 ns->shm_ctlmax = SHMMAX;
81 ns->shm_ctlall = SHMALL;
82 ns->shm_ctlmni = SHMMNI;
83 ns->shm_tot = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070084 ipc_init_ids(ids);
Kirill Korotaev4e982312006-10-02 02:18:22 -070085}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Kirill Korotaev4e982312006-10-02 02:18:22 -070087static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
88{
89 if (shp->shm_nattch){
90 shp->shm_perm.mode |= SHM_DEST;
91 /* Do not find it any more */
92 shp->shm_perm.key = IPC_PRIVATE;
93 shm_unlock(shp);
94 } else
95 shm_destroy(ns, shp);
96}
97
Kirill Korotaev4e982312006-10-02 02:18:22 -070098int shm_init_ns(struct ipc_namespace *ns)
99{
100 struct ipc_ids *ids;
101
102 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
103 if (ids == NULL)
104 return -ENOMEM;
105
106 __shm_init_ns(ns, ids);
107 return 0;
108}
109
110void shm_exit_ns(struct ipc_namespace *ns)
111{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700112 struct shmid_kernel *shp;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700113 int next_id;
114 int total, in_use;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700115
116 mutex_lock(&shm_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700117
118 in_use = shm_ids(ns).in_use;
119
120 for (total = 0, next_id = 0; total < in_use; next_id++) {
121 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700122 if (shp == NULL)
123 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700124 ipc_lock_by_ptr(&shp->shm_perm);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700125 do_shm_rmid(ns, shp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700126 total++;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700127 }
128 mutex_unlock(&shm_ids(ns).mutex);
129
130 kfree(ns->ids[IPC_SHM_IDS]);
131 ns->ids[IPC_SHM_IDS] = NULL;
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134void __init shm_init (void)
135{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700136 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700137 ipc_init_proc_interface("sysvipc/shm",
138 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
Kirill Korotaev4e982312006-10-02 02:18:22 -0700139 IPC_SHM_IDS, sysvipc_shm_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Kirill Korotaev4e982312006-10-02 02:18:22 -0700142static inline int shm_checkid(struct ipc_namespace *ns,
143 struct shmid_kernel *s, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700145 if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -EIDRM;
147 return 0;
148}
149
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700150static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700152 ipc_rmid(&shm_ids(ns), &s->shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Kirill Korotaev4e982312006-10-02 02:18:22 -0700155static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700157 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160
161
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800162/* This is called by fork, once for every shm attach. */
163static void shm_open(struct vm_area_struct *vma)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700164{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800165 struct file *file = vma->vm_file;
166 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct shmid_kernel *shp;
168
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800169 shp = shm_lock(sfd->ns, sfd->id);
Eric Sesterhenn9ba025f2006-04-02 13:42:42 +0200170 BUG_ON(!shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 shp->shm_atim = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700172 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 shp->shm_nattch++;
174 shm_unlock(shp);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
178 * shm_destroy - free the struct shmid_kernel
179 *
180 * @shp: struct to free
181 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800182 * It has to be called with shp and shm_ids.mutex locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * but returns with shp unlocked and freed.
184 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700185static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700187 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700188 shm_rmid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 shm_unlock(shp);
190 if (!is_file_hugepages(shp->shm_file))
191 shmem_lock(shp->shm_file, 0, shp->mlock_user);
192 else
Josef Sipek6d630792006-12-08 02:37:11 -0800193 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 shp->mlock_user);
195 fput (shp->shm_file);
196 security_shm_free(shp);
197 ipc_rcu_putref(shp);
198}
199
200/*
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800201 * remove the attach descriptor vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * free memory for segment if it is marked destroyed.
203 * The descriptor has already been removed from the current->mm->mmap list
204 * and will later be kfree()d.
205 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800206static void shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800208 struct file * file = vma->vm_file;
209 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct shmid_kernel *shp;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800211 struct ipc_namespace *ns = sfd->ns;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700212
213 mutex_lock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* remove from the list of attaches of the shm segment */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800215 shp = shm_lock(ns, sfd->id);
Eric Sesterhenn9ba025f2006-04-02 13:42:42 +0200216 BUG_ON(!shp);
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700217 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 shp->shm_dtim = get_seconds();
219 shp->shm_nattch--;
220 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800221 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700222 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 else
224 shm_unlock(shp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700225 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Nick Piggind0217ac2007-07-19 01:47:03 -0700228static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800229{
230 struct file *file = vma->vm_file;
231 struct shm_file_data *sfd = shm_file_data(file);
232
Nick Piggind0217ac2007-07-19 01:47:03 -0700233 return sfd->vm_ops->fault(vma, vmf);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800234}
235
236#ifdef CONFIG_NUMA
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700237static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800238{
239 struct file *file = vma->vm_file;
240 struct shm_file_data *sfd = shm_file_data(file);
241 int err = 0;
242 if (sfd->vm_ops->set_policy)
243 err = sfd->vm_ops->set_policy(vma, new);
244 return err;
245}
246
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700247static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
248 unsigned long addr)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800249{
250 struct file *file = vma->vm_file;
251 struct shm_file_data *sfd = shm_file_data(file);
252 struct mempolicy *pol = NULL;
253
254 if (sfd->vm_ops->get_policy)
255 pol = sfd->vm_ops->get_policy(vma, addr);
Adam Litke22741922007-06-16 10:16:15 -0700256 else if (vma->vm_policy)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800257 pol = vma->vm_policy;
Adam Litke22741922007-06-16 10:16:15 -0700258 else
259 pol = current->mempolicy;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800260 return pol;
261}
262#endif
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int shm_mmap(struct file * file, struct vm_area_struct * vma)
265{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800266 struct shm_file_data *sfd = shm_file_data(file);
David Howellsb0e15192006-01-06 00:11:42 -0800267 int ret;
268
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800269 ret = sfd->file->f_op->mmap(sfd->file, vma);
270 if (ret != 0)
271 return ret;
272 sfd->vm_ops = vma->vm_ops;
David Howells2e92a3b2007-07-31 00:37:24 -0700273#ifdef CONFIG_MMU
Nick Piggin54cb8822007-07-19 01:46:59 -0700274 BUG_ON(!sfd->vm_ops->fault);
David Howells2e92a3b2007-07-31 00:37:24 -0700275#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800276 vma->vm_ops = &shm_vm_ops;
277 shm_open(vma);
David Howellsb0e15192006-01-06 00:11:42 -0800278
279 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Kirill Korotaev4e982312006-10-02 02:18:22 -0700282static int shm_release(struct inode *ino, struct file *file)
283{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800284 struct shm_file_data *sfd = shm_file_data(file);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700285
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800286 put_ipc_ns(sfd->ns);
287 shm_file_data(file) = NULL;
288 kfree(sfd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700289 return 0;
290}
291
Adam Litke516dffd2007-03-01 15:46:08 -0800292static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
293{
294 int (*fsync) (struct file *, struct dentry *, int datasync);
295 struct shm_file_data *sfd = shm_file_data(file);
296 int ret = -EINVAL;
297
298 fsync = sfd->file->f_op->fsync;
299 if (fsync)
300 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
301 return ret;
302}
303
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800304static unsigned long shm_get_unmapped_area(struct file *file,
305 unsigned long addr, unsigned long len, unsigned long pgoff,
306 unsigned long flags)
307{
308 struct shm_file_data *sfd = shm_file_data(file);
Adam Litke516dffd2007-03-01 15:46:08 -0800309 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800310}
Adam Litke516dffd2007-03-01 15:46:08 -0800311
312int is_file_shm_hugepages(struct file *file)
313{
314 int ret = 0;
315
316 if (file->f_op == &shm_file_operations) {
317 struct shm_file_data *sfd;
318 sfd = shm_file_data(file);
319 ret = is_file_hugepages(sfd->file);
320 }
321 return ret;
322}
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800323
Arjan van de Ven9a321442007-02-12 00:55:35 -0800324static const struct file_operations shm_file_operations = {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700325 .mmap = shm_mmap,
Adam Litke516dffd2007-03-01 15:46:08 -0800326 .fsync = shm_fsync,
Kirill Korotaev4e982312006-10-02 02:18:22 -0700327 .release = shm_release,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800328 .get_unmapped_area = shm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
331static struct vm_operations_struct shm_vm_ops = {
332 .open = shm_open, /* callback for a new vm-area open */
333 .close = shm_close, /* callback for when the vm-area is released */
Nick Piggin54cb8822007-07-19 01:46:59 -0700334 .fault = shm_fault,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800335#if defined(CONFIG_NUMA)
336 .set_policy = shm_set_policy,
337 .get_policy = shm_get_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
339};
340
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700341static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700343 key_t key = params->key;
344 int shmflg = params->flg;
345 size_t size = params->u.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int error;
347 struct shmid_kernel *shp;
348 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
349 struct file * file;
350 char name[13];
351 int id;
352
Kirill Korotaev4e982312006-10-02 02:18:22 -0700353 if (size < SHMMIN || size > ns->shm_ctlmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EINVAL;
355
Guy Streeterf66d45e2007-01-23 12:20:04 -0600356 if (ns->shm_tot + numpages > ns->shm_ctlall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -ENOSPC;
358
359 shp = ipc_rcu_alloc(sizeof(*shp));
360 if (!shp)
361 return -ENOMEM;
362
363 shp->shm_perm.key = key;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800364 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 shp->mlock_user = NULL;
366
367 shp->shm_perm.security = NULL;
368 error = security_shm_alloc(shp);
369 if (error) {
370 ipc_rcu_putref(shp);
371 return error;
372 }
373
Eric W. Biederman9d665862007-06-16 10:16:16 -0700374 sprintf (name, "SYSV%08x", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (shmflg & SHM_HUGETLB) {
Eric W. Biederman9d665862007-06-16 10:16:16 -0700376 /* hugetlb_file_setup takes care of mlock user accounting */
377 file = hugetlb_file_setup(name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 shp->mlock_user = current->user;
379 } else {
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800380 int acctflag = VM_ACCOUNT;
381 /*
382 * Do not allow no accounting for OVERCOMMIT_NEVER, even
383 * if it's asked for.
384 */
385 if ((shmflg & SHM_NORESERVE) &&
386 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
387 acctflag = 0;
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800388 file = shmem_file_setup(name, size, acctflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 error = PTR_ERR(file);
391 if (IS_ERR(file))
392 goto no_file;
393
394 error = -ENOSPC;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700395 id = shm_addid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if(id == -1)
397 goto no_id;
398
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700399 shp->shm_cprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 shp->shm_lprid = 0;
401 shp->shm_atim = shp->shm_dtim = 0;
402 shp->shm_ctim = get_seconds();
403 shp->shm_segsz = size;
404 shp->shm_nattch = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700405 shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 shp->shm_file = file;
Badari Pulavarty30475cc2007-06-16 10:15:59 -0700407 /*
408 * shmid gets reported as "inode#" in /proc/pid/maps.
409 * proc-ps tools use this. Changing this will break them.
410 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700411 file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
Krishnakumar R551110a2005-10-29 18:16:45 -0700412
Kirill Korotaev4e982312006-10-02 02:18:22 -0700413 ns->shm_tot += numpages;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700414 error = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 shm_unlock(shp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700416 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418no_id:
419 fput(file);
420no_file:
421 security_shm_free(shp);
422 ipc_rcu_putref(shp);
423 return error;
424}
425
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700426static inline int shm_security(void *shp, int shmflg)
427{
428 return security_shm_associate((struct shmid_kernel *) shp, shmflg);
429}
430
431static inline int shm_more_checks(void *shp, struct ipc_params *params)
432{
433 if (((struct shmid_kernel *)shp)->shm_segsz < params->u.size)
434 return -EINVAL;
435
436 return 0;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
440{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700441 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700442 struct ipc_ops shm_ops;
443 struct ipc_params shm_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Kirill Korotaev4e982312006-10-02 02:18:22 -0700445 ns = current->nsproxy->ipc_ns;
446
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700447 shm_ops.getnew = newseg;
448 shm_ops.associate = shm_security;
449 shm_ops.more_checks = shm_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700450
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700451 shm_params.key = key;
452 shm_params.flg = shmflg;
453 shm_params.u.size = size;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700454
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700455 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
459{
460 switch(version) {
461 case IPC_64:
462 return copy_to_user(buf, in, sizeof(*in));
463 case IPC_OLD:
464 {
465 struct shmid_ds out;
466
467 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
468 out.shm_segsz = in->shm_segsz;
469 out.shm_atime = in->shm_atime;
470 out.shm_dtime = in->shm_dtime;
471 out.shm_ctime = in->shm_ctime;
472 out.shm_cpid = in->shm_cpid;
473 out.shm_lpid = in->shm_lpid;
474 out.shm_nattch = in->shm_nattch;
475
476 return copy_to_user(buf, &out, sizeof(out));
477 }
478 default:
479 return -EINVAL;
480 }
481}
482
483struct shm_setbuf {
484 uid_t uid;
485 gid_t gid;
486 mode_t mode;
487};
488
489static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
490{
491 switch(version) {
492 case IPC_64:
493 {
494 struct shmid64_ds tbuf;
495
496 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
497 return -EFAULT;
498
499 out->uid = tbuf.shm_perm.uid;
500 out->gid = tbuf.shm_perm.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800501 out->mode = tbuf.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 return 0;
504 }
505 case IPC_OLD:
506 {
507 struct shmid_ds tbuf_old;
508
509 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
510 return -EFAULT;
511
512 out->uid = tbuf_old.shm_perm.uid;
513 out->gid = tbuf_old.shm_perm.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800514 out->mode = tbuf_old.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return 0;
517 }
518 default:
519 return -EINVAL;
520 }
521}
522
523static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
524{
525 switch(version) {
526 case IPC_64:
527 return copy_to_user(buf, in, sizeof(*in));
528 case IPC_OLD:
529 {
530 struct shminfo out;
531
532 if(in->shmmax > INT_MAX)
533 out.shmmax = INT_MAX;
534 else
535 out.shmmax = (int)in->shmmax;
536
537 out.shmmin = in->shmmin;
538 out.shmmni = in->shmmni;
539 out.shmseg = in->shmseg;
540 out.shmall = in->shmall;
541
542 return copy_to_user(buf, &out, sizeof(out));
543 }
544 default:
545 return -EINVAL;
546 }
547}
548
Kirill Korotaev4e982312006-10-02 02:18:22 -0700549static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
550 unsigned long *swp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700552 int next_id;
553 int total, in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 *rss = 0;
556 *swp = 0;
557
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700558 in_use = shm_ids(ns).in_use;
559
560 for (total = 0, next_id = 0; total < in_use; next_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct shmid_kernel *shp;
562 struct inode *inode;
563
Nadia Derbey637c3662007-10-18 23:40:50 -0700564 /*
565 * idr_find() is called via shm_get(), so with shm_ids.mutex
566 * locked. Since ipc_addid() is also called with
567 * shm_ids.mutex down, there is no need to add read barriers
568 * here to gurantee the writes in ipc_addid() are seen in
569 * order here (for Alpha).
570 * However idr_find() itself does not necessary require
571 * ipc_ids.mutex down. So if idr_find() is used by other
572 * places without ipc_ids.mutex down, then it needs read
573 * read memory barriers as ipc_lock() does.
574 */
575
576 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700577 if (shp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 continue;
579
Josef Sipek6d630792006-12-08 02:37:11 -0800580 inode = shp->shm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (is_file_hugepages(shp->shm_file)) {
583 struct address_space *mapping = inode->i_mapping;
584 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
585 } else {
586 struct shmem_inode_info *info = SHMEM_I(inode);
587 spin_lock(&info->lock);
588 *rss += inode->i_mapping->nrpages;
589 *swp += info->swapped;
590 spin_unlock(&info->lock);
591 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700592
593 total++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595}
596
597asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
598{
599 struct shm_setbuf setbuf;
600 struct shmid_kernel *shp;
601 int err, version;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700602 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (cmd < 0 || shmid < 0) {
605 err = -EINVAL;
606 goto out;
607 }
608
609 version = ipc_parse_version(&cmd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700610 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 switch (cmd) { /* replace with proc interface ? */
613 case IPC_INFO:
614 {
615 struct shminfo64 shminfo;
616
617 err = security_shm_shmctl(NULL, cmd);
618 if (err)
619 return err;
620
621 memset(&shminfo,0,sizeof(shminfo));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700622 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
623 shminfo.shmmax = ns->shm_ctlmax;
624 shminfo.shmall = ns->shm_ctlall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 shminfo.shmmin = SHMMIN;
627 if(copy_shminfo_to_user (buf, &shminfo, version))
628 return -EFAULT;
629 /* reading a integer is always atomic */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700630 err = ipc_get_maxid(&shm_ids(ns));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if(err<0)
632 err = 0;
633 goto out;
634 }
635 case SHM_INFO:
636 {
637 struct shm_info shm_info;
638
639 err = security_shm_shmctl(NULL, cmd);
640 if (err)
641 return err;
642
643 memset(&shm_info,0,sizeof(shm_info));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700644 mutex_lock(&shm_ids(ns).mutex);
645 shm_info.used_ids = shm_ids(ns).in_use;
646 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
647 shm_info.shm_tot = ns->shm_tot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 shm_info.swap_attempts = 0;
649 shm_info.swap_successes = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700650 err = ipc_get_maxid(&shm_ids(ns));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700651 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
653 err = -EFAULT;
654 goto out;
655 }
656
657 err = err < 0 ? 0 : err;
658 goto out;
659 }
660 case SHM_STAT:
661 case IPC_STAT:
662 {
663 struct shmid64_ds tbuf;
664 int result;
665 memset(&tbuf, 0, sizeof(tbuf));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700666 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if(shp==NULL) {
668 err = -EINVAL;
669 goto out;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700670 } else if (cmd == SHM_STAT) {
671 result = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 } else {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700673 err = shm_checkid(ns, shp,shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if(err)
675 goto out_unlock;
676 result = 0;
677 }
678 err=-EACCES;
679 if (ipcperms (&shp->shm_perm, S_IRUGO))
680 goto out_unlock;
681 err = security_shm_shmctl(shp, cmd);
682 if (err)
683 goto out_unlock;
684 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
685 tbuf.shm_segsz = shp->shm_segsz;
686 tbuf.shm_atime = shp->shm_atim;
687 tbuf.shm_dtime = shp->shm_dtim;
688 tbuf.shm_ctime = shp->shm_ctim;
689 tbuf.shm_cpid = shp->shm_cprid;
690 tbuf.shm_lpid = shp->shm_lprid;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800691 tbuf.shm_nattch = shp->shm_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 shm_unlock(shp);
693 if(copy_shmid_to_user (buf, &tbuf, version))
694 err = -EFAULT;
695 else
696 err = result;
697 goto out;
698 }
699 case SHM_LOCK:
700 case SHM_UNLOCK:
701 {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700702 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if(shp==NULL) {
704 err = -EINVAL;
705 goto out;
706 }
Kirill Korotaev4e982312006-10-02 02:18:22 -0700707 err = shm_checkid(ns, shp,shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if(err)
709 goto out_unlock;
710
Steve Grubb073115d2006-04-02 17:07:33 -0400711 err = audit_ipc_obj(&(shp->shm_perm));
712 if (err)
713 goto out_unlock;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!capable(CAP_IPC_LOCK)) {
716 err = -EPERM;
717 if (current->euid != shp->shm_perm.uid &&
718 current->euid != shp->shm_perm.cuid)
719 goto out_unlock;
720 if (cmd == SHM_LOCK &&
721 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
722 goto out_unlock;
723 }
724
725 err = security_shm_shmctl(shp, cmd);
726 if (err)
727 goto out_unlock;
728
729 if(cmd==SHM_LOCK) {
730 struct user_struct * user = current->user;
731 if (!is_file_hugepages(shp->shm_file)) {
732 err = shmem_lock(shp->shm_file, 1, user);
Pavel Emelianov7be77e22007-07-31 00:38:48 -0700733 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
Andrew Mortonb33291c2006-01-08 01:02:21 -0800734 shp->shm_perm.mode |= SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 shp->mlock_user = user;
736 }
737 }
738 } else if (!is_file_hugepages(shp->shm_file)) {
739 shmem_lock(shp->shm_file, 0, shp->mlock_user);
Andrew Mortonb33291c2006-01-08 01:02:21 -0800740 shp->shm_perm.mode &= ~SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 shp->mlock_user = NULL;
742 }
743 shm_unlock(shp);
744 goto out;
745 }
746 case IPC_RMID:
747 {
748 /*
749 * We cannot simply remove the file. The SVID states
750 * that the block remains until the last person
751 * detaches from it, then is deleted. A shmat() on
752 * an RMID segment is legal in older Linux and if
753 * we change it apps break...
754 *
755 * Instead we set a destroyed flag, and then blow
756 * the name away when the usage hits zero.
757 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700758 mutex_lock(&shm_ids(ns).mutex);
759 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 err = -EINVAL;
761 if (shp == NULL)
762 goto out_up;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700763 err = shm_checkid(ns, shp, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if(err)
765 goto out_unlock_up;
766
Steve Grubb073115d2006-04-02 17:07:33 -0400767 err = audit_ipc_obj(&(shp->shm_perm));
768 if (err)
769 goto out_unlock_up;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (current->euid != shp->shm_perm.uid &&
772 current->euid != shp->shm_perm.cuid &&
773 !capable(CAP_SYS_ADMIN)) {
774 err=-EPERM;
775 goto out_unlock_up;
776 }
777
778 err = security_shm_shmctl(shp, cmd);
779 if (err)
780 goto out_unlock_up;
781
Kirill Korotaev4e982312006-10-02 02:18:22 -0700782 do_shm_rmid(ns, shp);
783 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto out;
785 }
786
787 case IPC_SET:
788 {
789 if (copy_shmid_from_user (&setbuf, buf, version)) {
790 err = -EFAULT;
791 goto out;
792 }
Kirill Korotaev4e982312006-10-02 02:18:22 -0700793 mutex_lock(&shm_ids(ns).mutex);
794 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 err=-EINVAL;
796 if(shp==NULL)
797 goto out_up;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700798 err = shm_checkid(ns, shp,shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if(err)
800 goto out_unlock_up;
Steve Grubb073115d2006-04-02 17:07:33 -0400801 err = audit_ipc_obj(&(shp->shm_perm));
802 if (err)
803 goto out_unlock_up;
Linda Knippersac032212006-05-16 22:03:48 -0400804 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400805 if (err)
806 goto out_unlock_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 err=-EPERM;
808 if (current->euid != shp->shm_perm.uid &&
809 current->euid != shp->shm_perm.cuid &&
810 !capable(CAP_SYS_ADMIN)) {
811 goto out_unlock_up;
812 }
813
814 err = security_shm_shmctl(shp, cmd);
815 if (err)
816 goto out_unlock_up;
817
818 shp->shm_perm.uid = setbuf.uid;
819 shp->shm_perm.gid = setbuf.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800820 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 | (setbuf.mode & S_IRWXUGO);
822 shp->shm_ctim = get_seconds();
823 break;
824 }
825
826 default:
827 err = -EINVAL;
828 goto out;
829 }
830
831 err = 0;
832out_unlock_up:
833 shm_unlock(shp);
834out_up:
Kirill Korotaev4e982312006-10-02 02:18:22 -0700835 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto out;
837out_unlock:
838 shm_unlock(shp);
839out:
840 return err;
841}
842
843/*
844 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
845 *
846 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
847 * "raddr" thing points to kernel space, and there has to be a wrapper around
848 * this.
849 */
850long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
851{
852 struct shmid_kernel *shp;
853 unsigned long addr;
854 unsigned long size;
855 struct file * file;
856 int err;
857 unsigned long flags;
858 unsigned long prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int acc_mode;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800860 unsigned long user_addr;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700861 struct ipc_namespace *ns;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800862 struct shm_file_data *sfd;
863 struct path path;
864 mode_t f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800866 err = -EINVAL;
867 if (shmid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800869 else if ((addr = (ulong)shmaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (addr & (SHMLBA-1)) {
871 if (shmflg & SHM_RND)
872 addr &= ~(SHMLBA-1); /* round down */
873 else
874#ifndef __ARCH_FORCE_SHMLBA
875 if (addr & ~PAGE_MASK)
876#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 flags = MAP_SHARED | MAP_FIXED;
880 } else {
881 if ((shmflg & SHM_REMAP))
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 flags = MAP_SHARED;
885 }
886
887 if (shmflg & SHM_RDONLY) {
888 prot = PROT_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 acc_mode = S_IRUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800890 f_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 } else {
892 prot = PROT_READ | PROT_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 acc_mode = S_IRUGO | S_IWUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800894 f_mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 if (shmflg & SHM_EXEC) {
897 prot |= PROT_EXEC;
898 acc_mode |= S_IXUGO;
899 }
900
901 /*
902 * We cannot rely on the fs check since SYSV IPC does have an
903 * additional creator id...
904 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700905 ns = current->nsproxy->ipc_ns;
906 shp = shm_lock(ns, shmid);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800907 if(shp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800909
Kirill Korotaev4e982312006-10-02 02:18:22 -0700910 err = shm_checkid(ns, shp,shmid);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800911 if (err)
912 goto out_unlock;
913
914 err = -EACCES;
915 if (ipcperms(&shp->shm_perm, acc_mode))
916 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 err = security_shm_shmat(shp, shmaddr, shmflg);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800919 if (err)
920 goto out_unlock;
921
922 path.dentry = dget(shp->shm_file->f_path.dentry);
Dave Hansence8d2cd2007-10-16 23:31:13 -0700923 path.mnt = shp->shm_file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 shp->shm_nattch++;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800925 size = i_size_read(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 shm_unlock(shp);
927
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800928 err = -ENOMEM;
929 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
930 if (!sfd)
Dave Hansence8d2cd2007-10-16 23:31:13 -0700931 goto out_put_dentry;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800932
933 err = -ENOMEM;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700934
935 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800936 if (!file)
937 goto out_free;
938
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800939 file->private_data = sfd;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800940 file->f_mapping = shp->shm_file->f_mapping;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700941 sfd->id = shp->shm_perm.id;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800942 sfd->ns = get_ipc_ns(ns);
943 sfd->file = shp->shm_file;
944 sfd->vm_ops = NULL;
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 down_write(&current->mm->mmap_sem);
947 if (addr && !(shmflg & SHM_REMAP)) {
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800948 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (find_vma_intersection(current->mm, addr, addr + size))
950 goto invalid;
951 /*
952 * If shm segment goes below stack, make sure there is some
953 * space left for the stack to grow (at least 4 pages).
954 */
955 if (addr < current->mm->start_stack &&
956 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
957 goto invalid;
958 }
959
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800960 user_addr = do_mmap (file, addr, size, prot, flags, 0);
961 *raddr = user_addr;
962 err = 0;
963 if (IS_ERR_VALUE(user_addr))
964 err = (long)user_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965invalid:
966 up_write(&current->mm->mmap_sem);
967
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800968 fput(file);
969
970out_nattch:
Kirill Korotaev4e982312006-10-02 02:18:22 -0700971 mutex_lock(&shm_ids(ns).mutex);
972 shp = shm_lock(ns, shmid);
Eric Sesterhenn9ba025f2006-04-02 13:42:42 +0200973 BUG_ON(!shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 shp->shm_nattch--;
975 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800976 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700977 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 else
979 shm_unlock(shp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700980 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982out:
983 return err;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800984
985out_unlock:
986 shm_unlock(shp);
987 goto out;
988
989out_free:
990 kfree(sfd);
Dave Hansence8d2cd2007-10-16 23:31:13 -0700991out_put_dentry:
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800992 dput(path.dentry);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800993 goto out_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -0700996asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
997{
998 unsigned long ret;
999 long err;
1000
1001 err = do_shmat(shmid, shmaddr, shmflg, &ret);
1002 if (err)
1003 return err;
1004 force_successful_syscall_return();
1005 return (long)ret;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * detach and kill segment if marked destroyed.
1010 * The work is done in shm_close.
1011 */
1012asmlinkage long sys_shmdt(char __user *shmaddr)
1013{
1014 struct mm_struct *mm = current->mm;
1015 struct vm_area_struct *vma, *next;
1016 unsigned long addr = (unsigned long)shmaddr;
1017 loff_t size = 0;
1018 int retval = -EINVAL;
1019
Hugh Dickinsdf1e2fb2006-03-24 03:18:06 -08001020 if (addr & ~PAGE_MASK)
1021 return retval;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 down_write(&mm->mmap_sem);
1024
1025 /*
1026 * This function tries to be smart and unmap shm segments that
1027 * were modified by partial mlock or munmap calls:
1028 * - It first determines the size of the shm segment that should be
1029 * unmapped: It searches for a vma that is backed by shm and that
1030 * started at address shmaddr. It records it's size and then unmaps
1031 * it.
1032 * - Then it unmaps all shm vmas that started at shmaddr and that
1033 * are within the initially determined size.
1034 * Errors from do_munmap are ignored: the function only fails if
1035 * it's called with invalid parameters or if it's called to unmap
1036 * a part of a vma. Both calls in this function are for full vmas,
1037 * the parameters are directly copied from the vma itself and always
1038 * valid - therefore do_munmap cannot fail. (famous last words?)
1039 */
1040 /*
1041 * If it had been mremap()'d, the starting address would not
1042 * match the usual checks anyway. So assume all vma's are
1043 * above the starting address given.
1044 */
1045 vma = find_vma(mm, addr);
1046
1047 while (vma) {
1048 next = vma->vm_next;
1049
1050 /*
1051 * Check if the starting address would match, i.e. it's
1052 * a fragment created by mprotect() and/or munmap(), or it
1053 * otherwise it starts at this address with no hassles.
1054 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001055 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1057
1058
Josef Sipek6d630792006-12-08 02:37:11 -08001059 size = vma->vm_file->f_path.dentry->d_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1061 /*
1062 * We discovered the size of the shm segment, so
1063 * break out of here and fall through to the next
1064 * loop that uses the size information to stop
1065 * searching for matching vma's.
1066 */
1067 retval = 0;
1068 vma = next;
1069 break;
1070 }
1071 vma = next;
1072 }
1073
1074 /*
1075 * We need look no further than the maximum address a fragment
1076 * could possibly have landed at. Also cast things to loff_t to
1077 * prevent overflows and make comparisions vs. equal-width types.
1078 */
KAMEZAWA Hiroyuki8e367092006-02-10 01:51:12 -08001079 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1081 next = vma->vm_next;
1082
1083 /* finding a matching vma now does not alter retval */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001084 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1086
1087 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1088 vma = next;
1089 }
1090
1091 up_write(&mm->mmap_sem);
1092 return retval;
1093}
1094
1095#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001096static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Mike Waychison19b49462005-09-06 15:17:10 -07001098 struct shmid_kernel *shp = it;
1099 char *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1102#define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Mike Waychison19b49462005-09-06 15:17:10 -07001104 if (sizeof(size_t) <= sizeof(int))
1105 format = SMALL_STRING;
1106 else
1107 format = BIG_STRING;
1108 return seq_printf(s, format,
1109 shp->shm_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001110 shp->shm_perm.id,
Andrew Mortonb33291c2006-01-08 01:02:21 -08001111 shp->shm_perm.mode,
Mike Waychison19b49462005-09-06 15:17:10 -07001112 shp->shm_segsz,
1113 shp->shm_cprid,
1114 shp->shm_lprid,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001115 shp->shm_nattch,
Mike Waychison19b49462005-09-06 15:17:10 -07001116 shp->shm_perm.uid,
1117 shp->shm_perm.gid,
1118 shp->shm_perm.cuid,
1119 shp->shm_perm.cgid,
1120 shp->shm_atim,
1121 shp->shm_dtim,
1122 shp->shm_ctim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124#endif