blob: dcc333239683d7d15ea427a19b236a6fd769d69d [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)
66#define shm_get(ns, id) \
67 ((struct shmid_kernel*)ipc_get(&shm_ids(ns),id))
68#define shm_buildid(ns, id, seq) \
69 ipc_buildid(&shm_ids(ns), id, seq)
70
71static int newseg (struct ipc_namespace *ns, key_t key,
72 int shmflg, size_t size);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080073static void shm_open(struct vm_area_struct *vma);
74static void shm_close(struct vm_area_struct *vma);
Kirill Korotaev4e982312006-10-02 02:18:22 -070075static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070077static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#endif
79
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070080static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaev4e982312006-10-02 02:18:22 -070081{
82 ns->ids[IPC_SHM_IDS] = ids;
83 ns->shm_ctlmax = SHMMAX;
84 ns->shm_ctlall = SHMALL;
85 ns->shm_ctlmni = SHMMNI;
86 ns->shm_tot = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070087 ipc_init_ids(ids);
Kirill Korotaev4e982312006-10-02 02:18:22 -070088}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Kirill Korotaev4e982312006-10-02 02:18:22 -070090static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
91{
92 if (shp->shm_nattch){
93 shp->shm_perm.mode |= SHM_DEST;
94 /* Do not find it any more */
95 shp->shm_perm.key = IPC_PRIVATE;
96 shm_unlock(shp);
97 } else
98 shm_destroy(ns, shp);
99}
100
Kirill Korotaev4e982312006-10-02 02:18:22 -0700101int shm_init_ns(struct ipc_namespace *ns)
102{
103 struct ipc_ids *ids;
104
105 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
106 if (ids == NULL)
107 return -ENOMEM;
108
109 __shm_init_ns(ns, ids);
110 return 0;
111}
112
113void shm_exit_ns(struct ipc_namespace *ns)
114{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700115 struct shmid_kernel *shp;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700116 int next_id;
117 int total, in_use;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700118
119 mutex_lock(&shm_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700120
121 in_use = shm_ids(ns).in_use;
122
123 for (total = 0, next_id = 0; total < in_use; next_id++) {
124 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700125 if (shp == NULL)
126 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700127 ipc_lock_by_ptr(&shp->shm_perm);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700128 do_shm_rmid(ns, shp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700129 total++;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700130 }
131 mutex_unlock(&shm_ids(ns).mutex);
132
133 kfree(ns->ids[IPC_SHM_IDS]);
134 ns->ids[IPC_SHM_IDS] = NULL;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137void __init shm_init (void)
138{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700139 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700140 ipc_init_proc_interface("sysvipc/shm",
141 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
Kirill Korotaev4e982312006-10-02 02:18:22 -0700142 IPC_SHM_IDS, sysvipc_shm_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Kirill Korotaev4e982312006-10-02 02:18:22 -0700145static inline int shm_checkid(struct ipc_namespace *ns,
146 struct shmid_kernel *s, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700148 if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -EIDRM;
150 return 0;
151}
152
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700153static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700155 ipc_rmid(&shm_ids(ns), &s->shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Kirill Korotaev4e982312006-10-02 02:18:22 -0700158static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700160 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163
164
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800165/* This is called by fork, once for every shm attach. */
166static void shm_open(struct vm_area_struct *vma)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700167{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800168 struct file *file = vma->vm_file;
169 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct shmid_kernel *shp;
171
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800172 shp = shm_lock(sfd->ns, sfd->id);
Eric Sesterhenn9ba025f2006-04-02 13:42:42 +0200173 BUG_ON(!shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 shp->shm_atim = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700175 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 shp->shm_nattch++;
177 shm_unlock(shp);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * shm_destroy - free the struct shmid_kernel
182 *
183 * @shp: struct to free
184 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800185 * It has to be called with shp and shm_ids.mutex locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * but returns with shp unlocked and freed.
187 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700188static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700190 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700191 shm_rmid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 shm_unlock(shp);
193 if (!is_file_hugepages(shp->shm_file))
194 shmem_lock(shp->shm_file, 0, shp->mlock_user);
195 else
Josef Sipek6d630792006-12-08 02:37:11 -0800196 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 shp->mlock_user);
198 fput (shp->shm_file);
199 security_shm_free(shp);
200 ipc_rcu_putref(shp);
201}
202
203/*
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800204 * remove the attach descriptor vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * free memory for segment if it is marked destroyed.
206 * The descriptor has already been removed from the current->mm->mmap list
207 * and will later be kfree()d.
208 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800209static void shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800211 struct file * file = vma->vm_file;
212 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct shmid_kernel *shp;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800214 struct ipc_namespace *ns = sfd->ns;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700215
216 mutex_lock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* remove from the list of attaches of the shm segment */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800218 shp = shm_lock(ns, sfd->id);
Eric Sesterhenn9ba025f2006-04-02 13:42:42 +0200219 BUG_ON(!shp);
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700220 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 shp->shm_dtim = get_seconds();
222 shp->shm_nattch--;
223 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800224 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700225 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 else
227 shm_unlock(shp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700228 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Nick Piggind0217ac2007-07-19 01:47:03 -0700231static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800232{
233 struct file *file = vma->vm_file;
234 struct shm_file_data *sfd = shm_file_data(file);
235
Nick Piggind0217ac2007-07-19 01:47:03 -0700236 return sfd->vm_ops->fault(vma, vmf);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800237}
238
239#ifdef CONFIG_NUMA
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700240static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800241{
242 struct file *file = vma->vm_file;
243 struct shm_file_data *sfd = shm_file_data(file);
244 int err = 0;
245 if (sfd->vm_ops->set_policy)
246 err = sfd->vm_ops->set_policy(vma, new);
247 return err;
248}
249
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700250static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
251 unsigned long addr)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800252{
253 struct file *file = vma->vm_file;
254 struct shm_file_data *sfd = shm_file_data(file);
255 struct mempolicy *pol = NULL;
256
257 if (sfd->vm_ops->get_policy)
258 pol = sfd->vm_ops->get_policy(vma, addr);
Adam Litke22741922007-06-16 10:16:15 -0700259 else if (vma->vm_policy)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800260 pol = vma->vm_policy;
Adam Litke22741922007-06-16 10:16:15 -0700261 else
262 pol = current->mempolicy;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800263 return pol;
264}
265#endif
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static int shm_mmap(struct file * file, struct vm_area_struct * vma)
268{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800269 struct shm_file_data *sfd = shm_file_data(file);
David Howellsb0e15192006-01-06 00:11:42 -0800270 int ret;
271
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800272 ret = sfd->file->f_op->mmap(sfd->file, vma);
273 if (ret != 0)
274 return ret;
275 sfd->vm_ops = vma->vm_ops;
David Howells2e92a3b2007-07-31 00:37:24 -0700276#ifdef CONFIG_MMU
Nick Piggin54cb8822007-07-19 01:46:59 -0700277 BUG_ON(!sfd->vm_ops->fault);
David Howells2e92a3b2007-07-31 00:37:24 -0700278#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800279 vma->vm_ops = &shm_vm_ops;
280 shm_open(vma);
David Howellsb0e15192006-01-06 00:11:42 -0800281
282 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Kirill Korotaev4e982312006-10-02 02:18:22 -0700285static int shm_release(struct inode *ino, struct file *file)
286{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800287 struct shm_file_data *sfd = shm_file_data(file);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700288
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800289 put_ipc_ns(sfd->ns);
290 shm_file_data(file) = NULL;
291 kfree(sfd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700292 return 0;
293}
294
Adam Litke516dffd2007-03-01 15:46:08 -0800295static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
296{
297 int (*fsync) (struct file *, struct dentry *, int datasync);
298 struct shm_file_data *sfd = shm_file_data(file);
299 int ret = -EINVAL;
300
301 fsync = sfd->file->f_op->fsync;
302 if (fsync)
303 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
304 return ret;
305}
306
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800307static unsigned long shm_get_unmapped_area(struct file *file,
308 unsigned long addr, unsigned long len, unsigned long pgoff,
309 unsigned long flags)
310{
311 struct shm_file_data *sfd = shm_file_data(file);
Adam Litke516dffd2007-03-01 15:46:08 -0800312 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800313}
Adam Litke516dffd2007-03-01 15:46:08 -0800314
315int is_file_shm_hugepages(struct file *file)
316{
317 int ret = 0;
318
319 if (file->f_op == &shm_file_operations) {
320 struct shm_file_data *sfd;
321 sfd = shm_file_data(file);
322 ret = is_file_hugepages(sfd->file);
323 }
324 return ret;
325}
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800326
Arjan van de Ven9a321442007-02-12 00:55:35 -0800327static const struct file_operations shm_file_operations = {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700328 .mmap = shm_mmap,
Adam Litke516dffd2007-03-01 15:46:08 -0800329 .fsync = shm_fsync,
Kirill Korotaev4e982312006-10-02 02:18:22 -0700330 .release = shm_release,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800331 .get_unmapped_area = shm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332};
333
334static struct vm_operations_struct shm_vm_ops = {
335 .open = shm_open, /* callback for a new vm-area open */
336 .close = shm_close, /* callback for when the vm-area is released */
Nick Piggin54cb8822007-07-19 01:46:59 -0700337 .fault = shm_fault,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800338#if defined(CONFIG_NUMA)
339 .set_policy = shm_set_policy,
340 .get_policy = shm_get_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif
342};
343
Kirill Korotaev4e982312006-10-02 02:18:22 -0700344static int newseg (struct ipc_namespace *ns, key_t key, int shmflg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 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
426asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
427{
428 struct shmid_kernel *shp;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700429 int err;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700430 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Kirill Korotaev4e982312006-10-02 02:18:22 -0700432 ns = current->nsproxy->ipc_ns;
433
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700434 err = idr_pre_get(&shm_ids(ns).ipcs_idr, GFP_KERNEL);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (key == IPC_PRIVATE) {
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700437 if (!err)
438 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 else {
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700440 mutex_lock(&shm_ids(ns).mutex);
441 err = newseg(ns, key, shmflg, size);
442 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700444 } else {
445 mutex_lock(&shm_ids(ns).mutex);
446 shp = (struct shmid_kernel *) ipc_findkey(&shm_ids(ns), key);
447 if (shp == NULL) {
448 if (!(shmflg & IPC_CREAT))
449 err = -ENOENT;
450 else if (!err)
451 err = -ENOMEM;
452 else
453 err = newseg(ns, key, shmflg, size);
454 } else {
455 /* shp has been locked by ipc_findkey() */
456
457 if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL))
458 err = -EEXIST;
459 else {
460 if (shp->shm_segsz < size)
461 err = -EINVAL;
462 else if (ipcperms(&shp->shm_perm, shmflg))
463 err = -EACCES;
464 else {
465 err = security_shm_associate(shp,
466 shmflg);
467 if (!err)
468 err = shp->shm_perm.id;
469 }
470 }
471 shm_unlock(shp);
472 }
473 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return err;
477}
478
479static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
480{
481 switch(version) {
482 case IPC_64:
483 return copy_to_user(buf, in, sizeof(*in));
484 case IPC_OLD:
485 {
486 struct shmid_ds out;
487
488 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
489 out.shm_segsz = in->shm_segsz;
490 out.shm_atime = in->shm_atime;
491 out.shm_dtime = in->shm_dtime;
492 out.shm_ctime = in->shm_ctime;
493 out.shm_cpid = in->shm_cpid;
494 out.shm_lpid = in->shm_lpid;
495 out.shm_nattch = in->shm_nattch;
496
497 return copy_to_user(buf, &out, sizeof(out));
498 }
499 default:
500 return -EINVAL;
501 }
502}
503
504struct shm_setbuf {
505 uid_t uid;
506 gid_t gid;
507 mode_t mode;
508};
509
510static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
511{
512 switch(version) {
513 case IPC_64:
514 {
515 struct shmid64_ds tbuf;
516
517 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
518 return -EFAULT;
519
520 out->uid = tbuf.shm_perm.uid;
521 out->gid = tbuf.shm_perm.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800522 out->mode = tbuf.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 return 0;
525 }
526 case IPC_OLD:
527 {
528 struct shmid_ds tbuf_old;
529
530 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
531 return -EFAULT;
532
533 out->uid = tbuf_old.shm_perm.uid;
534 out->gid = tbuf_old.shm_perm.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800535 out->mode = tbuf_old.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return 0;
538 }
539 default:
540 return -EINVAL;
541 }
542}
543
544static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
545{
546 switch(version) {
547 case IPC_64:
548 return copy_to_user(buf, in, sizeof(*in));
549 case IPC_OLD:
550 {
551 struct shminfo out;
552
553 if(in->shmmax > INT_MAX)
554 out.shmmax = INT_MAX;
555 else
556 out.shmmax = (int)in->shmmax;
557
558 out.shmmin = in->shmmin;
559 out.shmmni = in->shmmni;
560 out.shmseg = in->shmseg;
561 out.shmall = in->shmall;
562
563 return copy_to_user(buf, &out, sizeof(out));
564 }
565 default:
566 return -EINVAL;
567 }
568}
569
Kirill Korotaev4e982312006-10-02 02:18:22 -0700570static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
571 unsigned long *swp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700573 int next_id;
574 int total, in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 *rss = 0;
577 *swp = 0;
578
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700579 in_use = shm_ids(ns).in_use;
580
581 for (total = 0, next_id = 0; total < in_use; next_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct shmid_kernel *shp;
583 struct inode *inode;
584
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700585 shp = shm_get(ns, next_id);
586 if (shp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 continue;
588
Josef Sipek6d630792006-12-08 02:37:11 -0800589 inode = shp->shm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (is_file_hugepages(shp->shm_file)) {
592 struct address_space *mapping = inode->i_mapping;
593 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
594 } else {
595 struct shmem_inode_info *info = SHMEM_I(inode);
596 spin_lock(&info->lock);
597 *rss += inode->i_mapping->nrpages;
598 *swp += info->swapped;
599 spin_unlock(&info->lock);
600 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700601
602 total++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604}
605
606asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
607{
608 struct shm_setbuf setbuf;
609 struct shmid_kernel *shp;
610 int err, version;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700611 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (cmd < 0 || shmid < 0) {
614 err = -EINVAL;
615 goto out;
616 }
617
618 version = ipc_parse_version(&cmd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700619 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 switch (cmd) { /* replace with proc interface ? */
622 case IPC_INFO:
623 {
624 struct shminfo64 shminfo;
625
626 err = security_shm_shmctl(NULL, cmd);
627 if (err)
628 return err;
629
630 memset(&shminfo,0,sizeof(shminfo));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700631 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
632 shminfo.shmmax = ns->shm_ctlmax;
633 shminfo.shmall = ns->shm_ctlall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 shminfo.shmmin = SHMMIN;
636 if(copy_shminfo_to_user (buf, &shminfo, version))
637 return -EFAULT;
638 /* reading a integer is always atomic */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700639 err = ipc_get_maxid(&shm_ids(ns));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if(err<0)
641 err = 0;
642 goto out;
643 }
644 case SHM_INFO:
645 {
646 struct shm_info shm_info;
647
648 err = security_shm_shmctl(NULL, cmd);
649 if (err)
650 return err;
651
652 memset(&shm_info,0,sizeof(shm_info));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700653 mutex_lock(&shm_ids(ns).mutex);
654 shm_info.used_ids = shm_ids(ns).in_use;
655 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
656 shm_info.shm_tot = ns->shm_tot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 shm_info.swap_attempts = 0;
658 shm_info.swap_successes = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700659 err = ipc_get_maxid(&shm_ids(ns));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700660 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
662 err = -EFAULT;
663 goto out;
664 }
665
666 err = err < 0 ? 0 : err;
667 goto out;
668 }
669 case SHM_STAT:
670 case IPC_STAT:
671 {
672 struct shmid64_ds tbuf;
673 int result;
674 memset(&tbuf, 0, sizeof(tbuf));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700675 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if(shp==NULL) {
677 err = -EINVAL;
678 goto out;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700679 } else if (cmd == SHM_STAT) {
680 result = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 } else {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700682 err = shm_checkid(ns, shp,shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if(err)
684 goto out_unlock;
685 result = 0;
686 }
687 err=-EACCES;
688 if (ipcperms (&shp->shm_perm, S_IRUGO))
689 goto out_unlock;
690 err = security_shm_shmctl(shp, cmd);
691 if (err)
692 goto out_unlock;
693 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
694 tbuf.shm_segsz = shp->shm_segsz;
695 tbuf.shm_atime = shp->shm_atim;
696 tbuf.shm_dtime = shp->shm_dtim;
697 tbuf.shm_ctime = shp->shm_ctim;
698 tbuf.shm_cpid = shp->shm_cprid;
699 tbuf.shm_lpid = shp->shm_lprid;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800700 tbuf.shm_nattch = shp->shm_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 shm_unlock(shp);
702 if(copy_shmid_to_user (buf, &tbuf, version))
703 err = -EFAULT;
704 else
705 err = result;
706 goto out;
707 }
708 case SHM_LOCK:
709 case SHM_UNLOCK:
710 {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700711 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if(shp==NULL) {
713 err = -EINVAL;
714 goto out;
715 }
Kirill Korotaev4e982312006-10-02 02:18:22 -0700716 err = shm_checkid(ns, shp,shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if(err)
718 goto out_unlock;
719
Steve Grubb073115d2006-04-02 17:07:33 -0400720 err = audit_ipc_obj(&(shp->shm_perm));
721 if (err)
722 goto out_unlock;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!capable(CAP_IPC_LOCK)) {
725 err = -EPERM;
726 if (current->euid != shp->shm_perm.uid &&
727 current->euid != shp->shm_perm.cuid)
728 goto out_unlock;
729 if (cmd == SHM_LOCK &&
730 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
731 goto out_unlock;
732 }
733
734 err = security_shm_shmctl(shp, cmd);
735 if (err)
736 goto out_unlock;
737
738 if(cmd==SHM_LOCK) {
739 struct user_struct * user = current->user;
740 if (!is_file_hugepages(shp->shm_file)) {
741 err = shmem_lock(shp->shm_file, 1, user);
Pavel Emelianov7be77e22007-07-31 00:38:48 -0700742 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
Andrew Mortonb33291c2006-01-08 01:02:21 -0800743 shp->shm_perm.mode |= SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 shp->mlock_user = user;
745 }
746 }
747 } else if (!is_file_hugepages(shp->shm_file)) {
748 shmem_lock(shp->shm_file, 0, shp->mlock_user);
Andrew Mortonb33291c2006-01-08 01:02:21 -0800749 shp->shm_perm.mode &= ~SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 shp->mlock_user = NULL;
751 }
752 shm_unlock(shp);
753 goto out;
754 }
755 case IPC_RMID:
756 {
757 /*
758 * We cannot simply remove the file. The SVID states
759 * that the block remains until the last person
760 * detaches from it, then is deleted. A shmat() on
761 * an RMID segment is legal in older Linux and if
762 * we change it apps break...
763 *
764 * Instead we set a destroyed flag, and then blow
765 * the name away when the usage hits zero.
766 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700767 mutex_lock(&shm_ids(ns).mutex);
768 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 err = -EINVAL;
770 if (shp == NULL)
771 goto out_up;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700772 err = shm_checkid(ns, shp, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if(err)
774 goto out_unlock_up;
775
Steve Grubb073115d2006-04-02 17:07:33 -0400776 err = audit_ipc_obj(&(shp->shm_perm));
777 if (err)
778 goto out_unlock_up;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (current->euid != shp->shm_perm.uid &&
781 current->euid != shp->shm_perm.cuid &&
782 !capable(CAP_SYS_ADMIN)) {
783 err=-EPERM;
784 goto out_unlock_up;
785 }
786
787 err = security_shm_shmctl(shp, cmd);
788 if (err)
789 goto out_unlock_up;
790
Kirill Korotaev4e982312006-10-02 02:18:22 -0700791 do_shm_rmid(ns, shp);
792 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto out;
794 }
795
796 case IPC_SET:
797 {
798 if (copy_shmid_from_user (&setbuf, buf, version)) {
799 err = -EFAULT;
800 goto out;
801 }
Kirill Korotaev4e982312006-10-02 02:18:22 -0700802 mutex_lock(&shm_ids(ns).mutex);
803 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 err=-EINVAL;
805 if(shp==NULL)
806 goto out_up;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700807 err = shm_checkid(ns, shp,shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if(err)
809 goto out_unlock_up;
Steve Grubb073115d2006-04-02 17:07:33 -0400810 err = audit_ipc_obj(&(shp->shm_perm));
811 if (err)
812 goto out_unlock_up;
Linda Knippersac032212006-05-16 22:03:48 -0400813 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400814 if (err)
815 goto out_unlock_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 err=-EPERM;
817 if (current->euid != shp->shm_perm.uid &&
818 current->euid != shp->shm_perm.cuid &&
819 !capable(CAP_SYS_ADMIN)) {
820 goto out_unlock_up;
821 }
822
823 err = security_shm_shmctl(shp, cmd);
824 if (err)
825 goto out_unlock_up;
826
827 shp->shm_perm.uid = setbuf.uid;
828 shp->shm_perm.gid = setbuf.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800829 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 | (setbuf.mode & S_IRWXUGO);
831 shp->shm_ctim = get_seconds();
832 break;
833 }
834
835 default:
836 err = -EINVAL;
837 goto out;
838 }
839
840 err = 0;
841out_unlock_up:
842 shm_unlock(shp);
843out_up:
Kirill Korotaev4e982312006-10-02 02:18:22 -0700844 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto out;
846out_unlock:
847 shm_unlock(shp);
848out:
849 return err;
850}
851
852/*
853 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
854 *
855 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
856 * "raddr" thing points to kernel space, and there has to be a wrapper around
857 * this.
858 */
859long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
860{
861 struct shmid_kernel *shp;
862 unsigned long addr;
863 unsigned long size;
864 struct file * file;
865 int err;
866 unsigned long flags;
867 unsigned long prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int acc_mode;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800869 unsigned long user_addr;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700870 struct ipc_namespace *ns;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800871 struct shm_file_data *sfd;
872 struct path path;
873 mode_t f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800875 err = -EINVAL;
876 if (shmid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800878 else if ((addr = (ulong)shmaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (addr & (SHMLBA-1)) {
880 if (shmflg & SHM_RND)
881 addr &= ~(SHMLBA-1); /* round down */
882 else
883#ifndef __ARCH_FORCE_SHMLBA
884 if (addr & ~PAGE_MASK)
885#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800886 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 flags = MAP_SHARED | MAP_FIXED;
889 } else {
890 if ((shmflg & SHM_REMAP))
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 flags = MAP_SHARED;
894 }
895
896 if (shmflg & SHM_RDONLY) {
897 prot = PROT_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 acc_mode = S_IRUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800899 f_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 } else {
901 prot = PROT_READ | PROT_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 acc_mode = S_IRUGO | S_IWUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800903 f_mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 if (shmflg & SHM_EXEC) {
906 prot |= PROT_EXEC;
907 acc_mode |= S_IXUGO;
908 }
909
910 /*
911 * We cannot rely on the fs check since SYSV IPC does have an
912 * additional creator id...
913 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700914 ns = current->nsproxy->ipc_ns;
915 shp = shm_lock(ns, shmid);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800916 if(shp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800918
Kirill Korotaev4e982312006-10-02 02:18:22 -0700919 err = shm_checkid(ns, shp,shmid);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800920 if (err)
921 goto out_unlock;
922
923 err = -EACCES;
924 if (ipcperms(&shp->shm_perm, acc_mode))
925 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 err = security_shm_shmat(shp, shmaddr, shmflg);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800928 if (err)
929 goto out_unlock;
930
931 path.dentry = dget(shp->shm_file->f_path.dentry);
Dave Hansence8d2cd2007-10-16 23:31:13 -0700932 path.mnt = shp->shm_file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 shp->shm_nattch++;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800934 size = i_size_read(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 shm_unlock(shp);
936
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800937 err = -ENOMEM;
938 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
939 if (!sfd)
Dave Hansence8d2cd2007-10-16 23:31:13 -0700940 goto out_put_dentry;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800941
942 err = -ENOMEM;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700943
944 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800945 if (!file)
946 goto out_free;
947
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800948 file->private_data = sfd;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800949 file->f_mapping = shp->shm_file->f_mapping;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700950 sfd->id = shp->shm_perm.id;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800951 sfd->ns = get_ipc_ns(ns);
952 sfd->file = shp->shm_file;
953 sfd->vm_ops = NULL;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 down_write(&current->mm->mmap_sem);
956 if (addr && !(shmflg & SHM_REMAP)) {
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800957 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (find_vma_intersection(current->mm, addr, addr + size))
959 goto invalid;
960 /*
961 * If shm segment goes below stack, make sure there is some
962 * space left for the stack to grow (at least 4 pages).
963 */
964 if (addr < current->mm->start_stack &&
965 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
966 goto invalid;
967 }
968
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800969 user_addr = do_mmap (file, addr, size, prot, flags, 0);
970 *raddr = user_addr;
971 err = 0;
972 if (IS_ERR_VALUE(user_addr))
973 err = (long)user_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974invalid:
975 up_write(&current->mm->mmap_sem);
976
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800977 fput(file);
978
979out_nattch:
Kirill Korotaev4e982312006-10-02 02:18:22 -0700980 mutex_lock(&shm_ids(ns).mutex);
981 shp = shm_lock(ns, shmid);
Eric Sesterhenn9ba025f2006-04-02 13:42:42 +0200982 BUG_ON(!shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 shp->shm_nattch--;
984 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800985 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700986 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 else
988 shm_unlock(shp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700989 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991out:
992 return err;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800993
994out_unlock:
995 shm_unlock(shp);
996 goto out;
997
998out_free:
999 kfree(sfd);
Dave Hansence8d2cd2007-10-16 23:31:13 -07001000out_put_dentry:
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001001 dput(path.dentry);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001002 goto out_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -07001005asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1006{
1007 unsigned long ret;
1008 long err;
1009
1010 err = do_shmat(shmid, shmaddr, shmflg, &ret);
1011 if (err)
1012 return err;
1013 force_successful_syscall_return();
1014 return (long)ret;
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/*
1018 * detach and kill segment if marked destroyed.
1019 * The work is done in shm_close.
1020 */
1021asmlinkage long sys_shmdt(char __user *shmaddr)
1022{
1023 struct mm_struct *mm = current->mm;
1024 struct vm_area_struct *vma, *next;
1025 unsigned long addr = (unsigned long)shmaddr;
1026 loff_t size = 0;
1027 int retval = -EINVAL;
1028
Hugh Dickinsdf1e2fb2006-03-24 03:18:06 -08001029 if (addr & ~PAGE_MASK)
1030 return retval;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 down_write(&mm->mmap_sem);
1033
1034 /*
1035 * This function tries to be smart and unmap shm segments that
1036 * were modified by partial mlock or munmap calls:
1037 * - It first determines the size of the shm segment that should be
1038 * unmapped: It searches for a vma that is backed by shm and that
1039 * started at address shmaddr. It records it's size and then unmaps
1040 * it.
1041 * - Then it unmaps all shm vmas that started at shmaddr and that
1042 * are within the initially determined size.
1043 * Errors from do_munmap are ignored: the function only fails if
1044 * it's called with invalid parameters or if it's called to unmap
1045 * a part of a vma. Both calls in this function are for full vmas,
1046 * the parameters are directly copied from the vma itself and always
1047 * valid - therefore do_munmap cannot fail. (famous last words?)
1048 */
1049 /*
1050 * If it had been mremap()'d, the starting address would not
1051 * match the usual checks anyway. So assume all vma's are
1052 * above the starting address given.
1053 */
1054 vma = find_vma(mm, addr);
1055
1056 while (vma) {
1057 next = vma->vm_next;
1058
1059 /*
1060 * Check if the starting address would match, i.e. it's
1061 * a fragment created by mprotect() and/or munmap(), or it
1062 * otherwise it starts at this address with no hassles.
1063 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001064 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1066
1067
Josef Sipek6d630792006-12-08 02:37:11 -08001068 size = vma->vm_file->f_path.dentry->d_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1070 /*
1071 * We discovered the size of the shm segment, so
1072 * break out of here and fall through to the next
1073 * loop that uses the size information to stop
1074 * searching for matching vma's.
1075 */
1076 retval = 0;
1077 vma = next;
1078 break;
1079 }
1080 vma = next;
1081 }
1082
1083 /*
1084 * We need look no further than the maximum address a fragment
1085 * could possibly have landed at. Also cast things to loff_t to
1086 * prevent overflows and make comparisions vs. equal-width types.
1087 */
KAMEZAWA Hiroyuki8e367092006-02-10 01:51:12 -08001088 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1090 next = vma->vm_next;
1091
1092 /* finding a matching vma now does not alter retval */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001093 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1095
1096 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1097 vma = next;
1098 }
1099
1100 up_write(&mm->mmap_sem);
1101 return retval;
1102}
1103
1104#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001105static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Mike Waychison19b49462005-09-06 15:17:10 -07001107 struct shmid_kernel *shp = it;
1108 char *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1111#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 -07001112
Mike Waychison19b49462005-09-06 15:17:10 -07001113 if (sizeof(size_t) <= sizeof(int))
1114 format = SMALL_STRING;
1115 else
1116 format = BIG_STRING;
1117 return seq_printf(s, format,
1118 shp->shm_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001119 shp->shm_perm.id,
Andrew Mortonb33291c2006-01-08 01:02:21 -08001120 shp->shm_perm.mode,
Mike Waychison19b49462005-09-06 15:17:10 -07001121 shp->shm_segsz,
1122 shp->shm_cprid,
1123 shp->shm_lprid,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001124 shp->shm_nattch,
Mike Waychison19b49462005-09-06 15:17:10 -07001125 shp->shm_perm.uid,
1126 shp->shm_perm.gid,
1127 shp->shm_perm.cuid,
1128 shp->shm_perm.cgid,
1129 shp->shm_atim,
1130 shp->shm_dtim,
1131 shp->shm_ctim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133#endif