blob: 65c3a294aba5cf265823f3d6f369b7b206643d22 [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>
Nadia Derbey3e148c72007-10-18 23:40:54 -070038#include <linux/rwsem.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_unlock(shp) \
63 ipc_unlock(&(shp)->shm_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070064#define shm_buildid(id, seq) ipc_buildid(id, seq)
Kirill Korotaev4e982312006-10-02 02:18:22 -070065
Nadia Derbey7748dbf2007-10-18 23:40:49 -070066static int newseg(struct ipc_namespace *, struct ipc_params *);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080067static void shm_open(struct vm_area_struct *vma);
68static void shm_close(struct vm_area_struct *vma);
Kirill Korotaev4e982312006-10-02 02:18:22 -070069static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070071static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif
73
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070074static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaev4e982312006-10-02 02:18:22 -070075{
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 Derbey7ca7e562007-10-18 23:40:48 -070081 ipc_init_ids(ids);
Kirill Korotaev4e982312006-10-02 02:18:22 -070082}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Nadia Derbeyf4566f02007-10-18 23:40:53 -070084/*
Nadia Derbey3e148c72007-10-18 23:40:54 -070085 * Called with shm_ids.rw_mutex (writer) and the shp structure locked.
86 * Only shm_ids.rw_mutex remains locked on exit.
Nadia Derbeyf4566f02007-10-18 23:40:53 -070087 */
Kirill Korotaev4e982312006-10-02 02:18:22 -070088static 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 Korotaev4e982312006-10-02 02:18:22 -070099int 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
111void shm_exit_ns(struct ipc_namespace *ns)
112{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700113 struct shmid_kernel *shp;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800114 struct kern_ipc_perm *perm;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700115 int next_id;
116 int total, in_use;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700117
Nadia Derbey3e148c72007-10-18 23:40:54 -0700118 down_write(&shm_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700119
120 in_use = shm_ids(ns).in_use;
121
122 for (total = 0, next_id = 0; total < in_use; next_id++) {
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800123 perm = idr_find(&shm_ids(ns).ipcs_idr, next_id);
124 if (perm == NULL)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700125 continue;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800126 ipc_lock_by_ptr(perm);
127 shp = container_of(perm, struct shmid_kernel, 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 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700131 up_write(&shm_ids(ns).rw_mutex);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700132
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
Nadia Derbey3e148c72007-10-18 23:40:54 -0700145/*
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 */
149static 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 Peifferb1ed88b2008-02-06 01:36:23 -0800154 if (IS_ERR(ipcp))
155 return (struct shmid_kernel *)ipcp;
156
Nadia Derbey3e148c72007-10-18 23:40:54 -0700157 return container_of(ipcp, struct shmid_kernel, shm_perm);
158}
159
160static 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 Peifferb1ed88b2008-02-06 01:36:23 -0800166 if (IS_ERR(ipcp))
167 return (struct shmid_kernel *)ipcp;
168
Nadia Derbey3e148c72007-10-18 23:40:54 -0700169 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 Derbey023a5352007-10-18 23:40:51 -0700176static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700178 struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
179
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800180 if (IS_ERR(ipcp))
181 return (struct shmid_kernel *)ipcp;
182
Nadia Derbey03f02c72007-10-18 23:40:51 -0700183 return container_of(ipcp, struct shmid_kernel, shm_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700184}
185
186static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
187 int id)
188{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700189 struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
190
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800191 if (IS_ERR(ipcp))
192 return (struct shmid_kernel *)ipcp;
193
Nadia Derbey03f02c72007-10-18 23:40:51 -0700194 return container_of(ipcp, struct shmid_kernel, shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700197static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700199 ipc_rmid(&shm_ids(ns), &s->shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Kirill Korotaev4e982312006-10-02 02:18:22 -0700202static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700204 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207
208
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800209/* This is called by fork, once for every shm attach. */
210static void shm_open(struct vm_area_struct *vma)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700211{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800212 struct file *file = vma->vm_file;
213 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct shmid_kernel *shp;
215
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800216 shp = shm_lock(sfd->ns, sfd->id);
Nadia Derbey023a5352007-10-18 23:40:51 -0700217 BUG_ON(IS_ERR(shp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 shp->shm_atim = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700219 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 shp->shm_nattch++;
221 shm_unlock(shp);
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * shm_destroy - free the struct shmid_kernel
226 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700227 * @ns: namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * @shp: struct to free
229 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700230 * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * but returns with shp unlocked and freed.
232 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700233static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700235 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700236 shm_rmid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 shm_unlock(shp);
238 if (!is_file_hugepages(shp->shm_file))
239 shmem_lock(shp->shm_file, 0, shp->mlock_user);
240 else
Josef Sipek6d630792006-12-08 02:37:11 -0800241 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 shp->mlock_user);
243 fput (shp->shm_file);
244 security_shm_free(shp);
245 ipc_rcu_putref(shp);
246}
247
248/*
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800249 * remove the attach descriptor vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * 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. Biedermanbc56bba2007-02-20 13:57:53 -0800254static void shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800256 struct file * file = vma->vm_file;
257 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct shmid_kernel *shp;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800259 struct ipc_namespace *ns = sfd->ns;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700260
Nadia Derbey3e148c72007-10-18 23:40:54 -0700261 down_write(&shm_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* remove from the list of attaches of the shm segment */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700263 shp = shm_lock_down(ns, sfd->id);
Nadia Derbey023a5352007-10-18 23:40:51 -0700264 BUG_ON(IS_ERR(shp));
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700265 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 shp->shm_dtim = get_seconds();
267 shp->shm_nattch--;
268 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800269 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700270 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 else
272 shm_unlock(shp);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700273 up_write(&shm_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Nick Piggind0217ac2007-07-19 01:47:03 -0700276static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800277{
278 struct file *file = vma->vm_file;
279 struct shm_file_data *sfd = shm_file_data(file);
280
Nick Piggind0217ac2007-07-19 01:47:03 -0700281 return sfd->vm_ops->fault(vma, vmf);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800282}
283
284#ifdef CONFIG_NUMA
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700285static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800286{
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 Bunkd823e3e2007-10-16 23:26:42 -0700295static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
296 unsigned long addr)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800297{
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 Litke22741922007-06-16 10:16:15 -0700304 else if (vma->vm_policy)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800305 pol = vma->vm_policy;
Adam Litke22741922007-06-16 10:16:15 -0700306 else
307 pol = current->mempolicy;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800308 return pol;
309}
310#endif
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static int shm_mmap(struct file * file, struct vm_area_struct * vma)
313{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800314 struct shm_file_data *sfd = shm_file_data(file);
David Howellsb0e15192006-01-06 00:11:42 -0800315 int ret;
316
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800317 ret = sfd->file->f_op->mmap(sfd->file, vma);
318 if (ret != 0)
319 return ret;
320 sfd->vm_ops = vma->vm_ops;
David Howells2e92a3b2007-07-31 00:37:24 -0700321#ifdef CONFIG_MMU
Nick Piggin54cb8822007-07-19 01:46:59 -0700322 BUG_ON(!sfd->vm_ops->fault);
David Howells2e92a3b2007-07-31 00:37:24 -0700323#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800324 vma->vm_ops = &shm_vm_ops;
325 shm_open(vma);
David Howellsb0e15192006-01-06 00:11:42 -0800326
327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Kirill Korotaev4e982312006-10-02 02:18:22 -0700330static int shm_release(struct inode *ino, struct file *file)
331{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800332 struct shm_file_data *sfd = shm_file_data(file);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700333
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800334 put_ipc_ns(sfd->ns);
335 shm_file_data(file) = NULL;
336 kfree(sfd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700337 return 0;
338}
339
Adam Litke516dffd2007-03-01 15:46:08 -0800340static 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. Biedermanbc56bba2007-02-20 13:57:53 -0800352static 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 Litke516dffd2007-03-01 15:46:08 -0800357 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800358}
Adam Litke516dffd2007-03-01 15:46:08 -0800359
360int 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. Biedermanbc56bba2007-02-20 13:57:53 -0800371
Arjan van de Ven9a321442007-02-12 00:55:35 -0800372static const struct file_operations shm_file_operations = {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700373 .mmap = shm_mmap,
Adam Litke516dffd2007-03-01 15:46:08 -0800374 .fsync = shm_fsync,
Kirill Korotaev4e982312006-10-02 02:18:22 -0700375 .release = shm_release,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800376 .get_unmapped_area = shm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377};
378
379static 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 Piggin54cb8822007-07-19 01:46:59 -0700382 .fault = shm_fault,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800383#if defined(CONFIG_NUMA)
384 .set_policy = shm_set_policy,
385 .get_policy = shm_get_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#endif
387};
388
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700389/**
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 Derbey3e148c72007-10-18 23:40:54 -0700394 * Called with shm_ids.rw_mutex held as a writer.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700395 */
396
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700397static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700399 key_t key = params->key;
400 int shmflg = params->flg;
401 size_t size = params->u.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 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 Korotaev4e982312006-10-02 02:18:22 -0700409 if (size < SHMMIN || size > ns->shm_ctlmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EINVAL;
411
Guy Streeterf66d45e2007-01-23 12:20:04 -0600412 if (ns->shm_tot + numpages > ns->shm_ctlall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -ENOSPC;
414
415 shp = ipc_rcu_alloc(sizeof(*shp));
416 if (!shp)
417 return -ENOMEM;
418
419 shp->shm_perm.key = key;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800420 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 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. Biederman9d665862007-06-16 10:16:16 -0700430 sprintf (name, "SYSV%08x", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (shmflg & SHM_HUGETLB) {
Eric W. Biederman9d665862007-06-16 10:16:16 -0700432 /* hugetlb_file_setup takes care of mlock user accounting */
433 file = hugetlb_file_setup(name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 shp->mlock_user = current->user;
435 } else {
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800436 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 Pulavartybf8f9722005-11-07 00:59:27 -0800444 file = shmem_file_setup(name, size, acctflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 error = PTR_ERR(file);
447 if (IS_ERR(file))
448 goto no_file;
449
Kirill Korotaev4e982312006-10-02 02:18:22 -0700450 id = shm_addid(ns, shp);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700451 if (id < 0) {
452 error = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto no_id;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700456 shp->shm_cprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 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 Derbey1b531f22007-10-18 23:40:55 -0700462 shp->shm_perm.id = shm_buildid(id, shp->shm_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 shp->shm_file = file;
Badari Pulavarty30475cc2007-06-16 10:15:59 -0700464 /*
465 * shmid gets reported as "inode#" in /proc/pid/maps.
466 * proc-ps tools use this. Changing this will break them.
467 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700468 file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
Krishnakumar R551110a2005-10-29 18:16:45 -0700469
Kirill Korotaev4e982312006-10-02 02:18:22 -0700470 ns->shm_tot += numpages;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700471 error = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 shm_unlock(shp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475no_id:
476 fput(file);
477no_file:
478 security_shm_free(shp);
479 ipc_rcu_putref(shp);
480 return error;
481}
482
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700483/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700484 * Called with shm_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700485 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700486static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700487{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700488 struct shmid_kernel *shp;
489
490 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
491 return security_shm_associate(shp, shmflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700492}
493
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700494/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700495 * Called with shm_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700496 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700497static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
498 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700499{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700500 struct shmid_kernel *shp;
501
502 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
503 if (shp->shm_segsz < params->u.size)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700504 return -EINVAL;
505
506 return 0;
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
510{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700511 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700512 struct ipc_ops shm_ops;
513 struct ipc_params shm_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Kirill Korotaev4e982312006-10-02 02:18:22 -0700515 ns = current->nsproxy->ipc_ns;
516
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700517 shm_ops.getnew = newseg;
518 shm_ops.associate = shm_security;
519 shm_ops.more_checks = shm_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700520
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700521 shm_params.key = key;
522 shm_params.flg = shmflg;
523 shm_params.u.size = size;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700524
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700525 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528static 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
553struct shm_setbuf {
554 uid_t uid;
555 gid_t gid;
556 mode_t mode;
557};
558
559static 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 Mortonb33291c2006-01-08 01:02:21 -0800571 out->mode = tbuf.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
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 Mortonb33291c2006-01-08 01:02:21 -0800584 out->mode = tbuf_old.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 return 0;
587 }
588 default:
589 return -EINVAL;
590 }
591}
592
593static 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 Derbeyf4566f02007-10-18 23:40:53 -0700619/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700620 * Called with shm_ids.rw_mutex held as a reader
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700621 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700622static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
623 unsigned long *swp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700625 int next_id;
626 int total, in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 *rss = 0;
629 *swp = 0;
630
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700631 in_use = shm_ids(ns).in_use;
632
633 for (total = 0, next_id = 0; total < in_use; next_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct shmid_kernel *shp;
635 struct inode *inode;
636
Nadia Derbey637c3662007-10-18 23:40:50 -0700637 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700638 if (shp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 continue;
640
Josef Sipek6d630792006-12-08 02:37:11 -0800641 inode = shp->shm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
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 Derbey7ca7e562007-10-18 23:40:48 -0700653
654 total++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656}
657
658asmlinkage 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 Korotaev4e982312006-10-02 02:18:22 -0700663 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (cmd < 0 || shmid < 0) {
666 err = -EINVAL;
667 goto out;
668 }
669
670 version = ipc_parse_version(&cmd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700671 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
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 Korotaev4e982312006-10-02 02:18:22 -0700683 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
684 shminfo.shmmax = ns->shm_ctlmax;
685 shminfo.shmall = ns->shm_ctlall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 shminfo.shmmin = SHMMIN;
688 if(copy_shminfo_to_user (buf, &shminfo, version))
689 return -EFAULT;
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700690
Nadia Derbey3e148c72007-10-18 23:40:54 -0700691 down_read(&shm_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700692 err = ipc_get_maxid(&shm_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700693 up_read(&shm_ids(ns).rw_mutex);
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 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 Derbey3e148c72007-10-18 23:40:54 -0700708 down_read(&shm_ids(ns).rw_mutex);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700709 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 Torvalds1da177e2005-04-16 15:20:36 -0700712 shm_info.swap_attempts = 0;
713 shm_info.swap_successes = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700714 err = ipc_get_maxid(&shm_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700715 up_read(&shm_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 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 Derbey023a5352007-10-18 23:40:51 -0700729
730 if (!buf) {
731 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto out;
Nadia Derbey023a5352007-10-18 23:40:51 -0700733 }
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 Derbey7ca7e562007-10-18 23:40:48 -0700741 result = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700743 shp = shm_lock_check(ns, shmid);
744 if (IS_ERR(shp)) {
745 err = PTR_ERR(shp);
746 goto out;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 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 Derbey023a5352007-10-18 23:40:51 -0700756 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 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. Biedermanbc56bba2007-02-20 13:57:53 -0800764 tbuf.shm_nattch = shp->shm_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 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 Derbey023a5352007-10-18 23:40:51 -0700775 shp = shm_lock_check(ns, shmid);
776 if (IS_ERR(shp)) {
777 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto out;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Steve Grubb073115d2006-04-02 17:07:33 -0400781 err = audit_ipc_obj(&(shp->shm_perm));
782 if (err)
783 goto out_unlock;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 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 Emelianov7be77e22007-07-31 00:38:48 -0700803 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
Andrew Mortonb33291c2006-01-08 01:02:21 -0800804 shp->shm_perm.mode |= SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 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 Mortonb33291c2006-01-08 01:02:21 -0800810 shp->shm_perm.mode &= ~SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 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 Derbey3e148c72007-10-18 23:40:54 -0700828 down_write(&shm_ids(ns).rw_mutex);
829 shp = shm_lock_check_down(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700830 if (IS_ERR(shp)) {
831 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Steve Grubb073115d2006-04-02 17:07:33 -0400835 err = audit_ipc_obj(&(shp->shm_perm));
836 if (err)
837 goto out_unlock_up;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 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 Korotaev4e982312006-10-02 02:18:22 -0700850 do_shm_rmid(ns, shp);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700851 up_write(&shm_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto out;
853 }
854
855 case IPC_SET:
856 {
Nadia Derbey023a5352007-10-18 23:40:51 -0700857 if (!buf) {
858 err = -EFAULT;
859 goto out;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (copy_shmid_from_user (&setbuf, buf, version)) {
863 err = -EFAULT;
864 goto out;
865 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700866 down_write(&shm_ids(ns).rw_mutex);
867 shp = shm_lock_check_down(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700868 if (IS_ERR(shp)) {
869 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700871 }
Steve Grubb073115d2006-04-02 17:07:33 -0400872 err = audit_ipc_obj(&(shp->shm_perm));
873 if (err)
874 goto out_unlock_up;
Linda Knippersac032212006-05-16 22:03:48 -0400875 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400876 if (err)
877 goto out_unlock_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 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 Mortonb33291c2006-01-08 01:02:21 -0800891 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 | (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;
903out_unlock_up:
904 shm_unlock(shp);
905out_up:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700906 up_write(&shm_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto out;
908out_unlock:
909 shm_unlock(shp);
910out:
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 */
921long 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 Torvalds1da177e2005-04-16 15:20:36 -0700930 int acc_mode;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800931 unsigned long user_addr;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700932 struct ipc_namespace *ns;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800933 struct shm_file_data *sfd;
934 struct path path;
935 mode_t f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800937 err = -EINVAL;
938 if (shmid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800940 else if ((addr = (ulong)shmaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 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. Biedermanbc56bba2007-02-20 13:57:53 -0800948 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 flags = MAP_SHARED | MAP_FIXED;
951 } else {
952 if ((shmflg & SHM_REMAP))
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 flags = MAP_SHARED;
956 }
957
958 if (shmflg & SHM_RDONLY) {
959 prot = PROT_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 acc_mode = S_IRUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800961 f_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 } else {
963 prot = PROT_READ | PROT_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 acc_mode = S_IRUGO | S_IWUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800965 f_mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
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 Korotaev4e982312006-10-02 02:18:22 -0700976 ns = current->nsproxy->ipc_ns;
Nadia Derbey023a5352007-10-18 23:40:51 -0700977 shp = shm_lock_check(ns, shmid);
978 if (IS_ERR(shp)) {
979 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 goto out;
Nadia Derbey023a5352007-10-18 23:40:51 -0700981 }
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800982
983 err = -EACCES;
984 if (ipcperms(&shp->shm_perm, acc_mode))
985 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 err = security_shm_shmat(shp, shmaddr, shmflg);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800988 if (err)
989 goto out_unlock;
990
991 path.dentry = dget(shp->shm_file->f_path.dentry);
Dave Hansence8d2cd2007-10-16 23:31:13 -0700992 path.mnt = shp->shm_file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 shp->shm_nattch++;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800994 size = i_size_read(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 shm_unlock(shp);
996
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800997 err = -ENOMEM;
998 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
999 if (!sfd)
Dave Hansence8d2cd2007-10-16 23:31:13 -07001000 goto out_put_dentry;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001001
1002 err = -ENOMEM;
Dave Hansence8d2cd2007-10-16 23:31:13 -07001003
1004 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001005 if (!file)
1006 goto out_free;
1007
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001008 file->private_data = sfd;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001009 file->f_mapping = shp->shm_file->f_mapping;
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001010 sfd->id = shp->shm_perm.id;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001011 sfd->ns = get_ipc_ns(ns);
1012 sfd->file = shp->shm_file;
1013 sfd->vm_ops = NULL;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 down_write(&current->mm->mmap_sem);
1016 if (addr && !(shmflg & SHM_REMAP)) {
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001017 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 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. Biedermanbc56bba2007-02-20 13:57:53 -08001029 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 Torvalds1da177e2005-04-16 15:20:36 -07001034invalid:
1035 up_write(&current->mm->mmap_sem);
1036
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001037 fput(file);
1038
1039out_nattch:
Nadia Derbey3e148c72007-10-18 23:40:54 -07001040 down_write(&shm_ids(ns).rw_mutex);
1041 shp = shm_lock_down(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001042 BUG_ON(IS_ERR(shp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 shp->shm_nattch--;
1044 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -08001045 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -07001046 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 else
1048 shm_unlock(shp);
Nadia Derbey3e148c72007-10-18 23:40:54 -07001049 up_write(&shm_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051out:
1052 return err;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001053
1054out_unlock:
1055 shm_unlock(shp);
1056 goto out;
1057
1058out_free:
1059 kfree(sfd);
Dave Hansence8d2cd2007-10-16 23:31:13 -07001060out_put_dentry:
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001061 dput(path.dentry);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001062 goto out_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -07001065asmlinkage 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 Torvalds1da177e2005-04-16 15:20:36 -07001077/*
1078 * detach and kill segment if marked destroyed.
1079 * The work is done in shm_close.
1080 */
1081asmlinkage 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 Dickinsdf1e2fb2006-03-24 03:18:06 -08001089 if (addr & ~PAGE_MASK)
1090 return retval;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 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. Biedermanbc56bba2007-02-20 13:57:53 -08001124 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1126
1127
Josef Sipek6d630792006-12-08 02:37:11 -08001128 size = vma->vm_file->f_path.dentry->d_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 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 Hiroyuki8e367092006-02-10 01:51:12 -08001148 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 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. Biedermanbc56bba2007-02-20 13:57:53 -08001153 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 (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 Waychison19b49462005-09-06 15:17:10 -07001165static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Mike Waychison19b49462005-09-06 15:17:10 -07001167 struct shmid_kernel *shp = it;
1168 char *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170#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 Torvalds1da177e2005-04-16 15:20:36 -07001172
Mike Waychison19b49462005-09-06 15:17:10 -07001173 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 Derbey7ca7e562007-10-18 23:40:48 -07001179 shp->shm_perm.id,
Andrew Mortonb33291c2006-01-08 01:02:21 -08001180 shp->shm_perm.mode,
Mike Waychison19b49462005-09-06 15:17:10 -07001181 shp->shm_segsz,
1182 shp->shm_cprid,
1183 shp->shm_lprid,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001184 shp->shm_nattch,
Mike Waychison19b49462005-09-06 15:17:10 -07001185 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 Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193#endif