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