blob: de93d01bfce29fe286e8e50ced2fffdc432b5e10 [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>
Davidlohr Buesoc2c737a2013-09-11 14:26:23 -070022 *
23 * Better ipc lock (kern_ipc_perm.lock) handling
24 * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/mm.h>
29#include <linux/hugetlb.h>
30#include <linux/shm.h>
31#include <linux/init.h>
32#include <linux/file.h>
33#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/shmem_fs.h>
35#include <linux/security.h>
36#include <linux/syscalls.h>
37#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070039#include <linux/ptrace.h>
Mike Waychison19b49462005-09-06 15:17:10 -070040#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070041#include <linux/rwsem.h>
Kirill Korotaev4e982312006-10-02 02:18:22 -070042#include <linux/nsproxy.h>
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080043#include <linux/mount.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080044#include <linux/ipc_namespace.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070045
Paul McQuade7153e402014-06-06 14:37:37 -070046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include "util.h"
49
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080050struct shm_file_data {
51 int id;
52 struct ipc_namespace *ns;
53 struct file *file;
54 const struct vm_operations_struct *vm_ops;
55};
56
57#define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
58
Arjan van de Ven9a321442007-02-12 00:55:35 -080059static const struct file_operations shm_file_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040060static const struct vm_operations_struct shm_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080062#define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Kirill Korotaev4e982312006-10-02 02:18:22 -070064#define shm_unlock(shp) \
65 ipc_unlock(&(shp)->shm_perm)
Kirill Korotaev4e982312006-10-02 02:18:22 -070066
Nadia Derbey7748dbf2007-10-18 23:40:49 -070067static int newseg(struct ipc_namespace *, struct ipc_params *);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080068static void shm_open(struct vm_area_struct *vma);
69static void shm_close(struct vm_area_struct *vma);
Manfred Spraul239521f2014-01-27 17:07:04 -080070static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070072static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080075void shm_init_ns(struct ipc_namespace *ns)
Kirill Korotaev4e982312006-10-02 02:18:22 -070076{
Kirill Korotaev4e982312006-10-02 02:18:22 -070077 ns->shm_ctlmax = SHMMAX;
78 ns->shm_ctlall = SHMALL;
79 ns->shm_ctlmni = SHMMNI;
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -070080 ns->shm_rmid_forced = 0;
Kirill Korotaev4e982312006-10-02 02:18:22 -070081 ns->shm_tot = 0;
WANG Conge8148f72009-01-06 14:42:49 -080082 ipc_init_ids(&shm_ids(ns));
Kirill Korotaev4e982312006-10-02 02:18:22 -070083}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Nadia Derbeyf4566f02007-10-18 23:40:53 -070085/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -070086 * Called with shm_ids.rwsem (writer) and the shp structure locked.
87 * Only shm_ids.rwsem remains locked on exit.
Nadia Derbeyf4566f02007-10-18 23:40:53 -070088 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -080089static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Kirill Korotaev4e982312006-10-02 02:18:22 -070090{
Pierre Peiffer01b8b072008-02-08 04:18:57 -080091 struct shmid_kernel *shp;
92 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
93
Manfred Spraul239521f2014-01-27 17:07:04 -080094 if (shp->shm_nattch) {
Kirill Korotaev4e982312006-10-02 02:18:22 -070095 shp->shm_perm.mode |= SHM_DEST;
96 /* Do not find it any more */
97 shp->shm_perm.key = IPC_PRIVATE;
98 shm_unlock(shp);
99 } else
100 shm_destroy(ns, shp);
101}
102
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800103#ifdef CONFIG_IPC_NS
Kirill Korotaev4e982312006-10-02 02:18:22 -0700104void shm_exit_ns(struct ipc_namespace *ns)
105{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800106 free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800107 idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700108}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds140d0b22011-08-04 19:35:59 -1000111static int __init ipc_ns_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800113 shm_init_ns(&init_ipc_ns);
Linus Torvalds140d0b22011-08-04 19:35:59 -1000114 return 0;
115}
116
117pure_initcall(ipc_ns_init);
118
Manfred Spraul239521f2014-01-27 17:07:04 -0800119void __init shm_init(void)
Linus Torvalds140d0b22011-08-04 19:35:59 -1000120{
Mike Waychison19b49462005-09-06 15:17:10 -0700121 ipc_init_proc_interface("sysvipc/shm",
Helge Dellerb7952182010-10-27 15:34:16 -0700122#if BITS_PER_LONG <= 32
123 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
124#else
125 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
126#endif
Kirill Korotaev4e982312006-10-02 02:18:22 -0700127 IPC_SHM_IDS, sysvipc_shm_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Davidlohr Bueso8b8d52a2013-09-11 14:26:15 -0700130static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
131{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700132 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
Davidlohr Bueso8b8d52a2013-09-11 14:26:15 -0700133
134 if (IS_ERR(ipcp))
135 return ERR_CAST(ipcp);
136
137 return container_of(ipcp, struct shmid_kernel, shm_perm);
138}
139
140static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
141{
142 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
143
144 if (IS_ERR(ipcp))
145 return ERR_CAST(ipcp);
146
147 return container_of(ipcp, struct shmid_kernel, shm_perm);
148}
149
Nadia Derbey3e148c72007-10-18 23:40:54 -0700150/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700151 * shm_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey00c2bf82008-07-25 01:48:03 -0700152 * is not necessarily held.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700153 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700154static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700156 struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
157
Davidlohr Buesoc5c89752015-06-30 14:58:36 -0700158 /*
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800159 * Callers of shm_lock() must validate the status of the returned ipc
160 * object pointer (as returned by ipc_lock()), and error out as
161 * appropriate.
Davidlohr Buesoc5c89752015-06-30 14:58:36 -0700162 */
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800163 if (IS_ERR(ipcp))
164 return (void *)ipcp;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700165 return container_of(ipcp, struct shmid_kernel, shm_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700166}
167
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400168static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
169{
170 rcu_read_lock();
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700171 ipc_lock_object(&ipcp->shm_perm);
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400172}
173
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700174static void shm_rcu_free(struct rcu_head *head)
175{
176 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
177 struct shmid_kernel *shp = ipc_rcu_to_struct(p);
178
179 security_shm_free(shp);
180 ipc_rcu_free(head);
181}
182
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700183static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Jack Millerab602f72014-08-08 14:23:19 -0700185 list_del(&s->shm_clist);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700186 ipc_rmid(&shm_ids(ns), &s->shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800190static int __shm_open(struct vm_area_struct *vma)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700191{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800192 struct file *file = vma->vm_file;
193 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct shmid_kernel *shp;
195
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800196 shp = shm_lock(sfd->ns, sfd->id);
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800197
198 if (IS_ERR(shp))
199 return PTR_ERR(shp);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 shp->shm_atim = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700202 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 shp->shm_nattch++;
204 shm_unlock(shp);
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800205 return 0;
206}
207
208/* This is called by fork, once for every shm attach. */
209static void shm_open(struct vm_area_struct *vma)
210{
211 int err = __shm_open(vma);
212 /*
213 * We raced in the idr lookup or with shm_destroy().
214 * Either way, the ID is busted.
215 */
216 WARN_ON_ONCE(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * shm_destroy - free the struct shmid_kernel
221 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700222 * @ns: namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * @shp: struct to free
224 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700225 * It has to be called with shp and shm_ids.rwsem (writer) locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * but returns with shp unlocked and freed.
227 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700228static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Greg Thelena399b292013-11-21 14:32:00 -0800230 struct file *shm_file;
231
232 shm_file = shp->shm_file;
233 shp->shm_file = NULL;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700234 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700235 shm_rmid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 shm_unlock(shp);
Greg Thelena399b292013-11-21 14:32:00 -0800237 if (!is_file_hugepages(shm_file))
238 shmem_lock(shm_file, 0, shp->mlock_user);
Hugh Dickins353d5c32009-08-24 16:30:28 +0100239 else if (shp->mlock_user)
Dave Hansen07a46ed2014-12-12 16:58:22 -0800240 user_shm_unlock(i_size_read(file_inode(shm_file)),
241 shp->mlock_user);
Greg Thelena399b292013-11-21 14:32:00 -0800242 fput(shm_file);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700243 ipc_rcu_putref(shp, shm_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700247 * shm_may_destroy - identifies whether shm segment should be destroyed now
248 *
249 * Returns true if and only if there are no active users of the segment and
250 * one of the following is true:
251 *
252 * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
253 *
254 * 2) sysctl kernel.shm_rmid_forced is set to 1.
255 */
256static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
257{
258 return (shp->shm_nattch == 0) &&
259 (ns->shm_rmid_forced ||
260 (shp->shm_perm.mode & SHM_DEST));
261}
262
263/*
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800264 * remove the attach descriptor vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * free memory for segment if it is marked destroyed.
266 * The descriptor has already been removed from the current->mm->mmap list
267 * and will later be kfree()d.
268 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800269static void shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Manfred Spraul239521f2014-01-27 17:07:04 -0800271 struct file *file = vma->vm_file;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800272 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct shmid_kernel *shp;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800274 struct ipc_namespace *ns = sfd->ns;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700275
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700276 down_write(&shm_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* remove from the list of attaches of the shm segment */
Nadia Derbey00c2bf82008-07-25 01:48:03 -0700278 shp = shm_lock(ns, sfd->id);
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800279
280 /*
281 * We raced in the idr lookup or with shm_destroy().
282 * Either way, the ID is busted.
283 */
284 if (WARN_ON_ONCE(IS_ERR(shp)))
285 goto done; /* no-op */
286
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700287 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 shp->shm_dtim = get_seconds();
289 shp->shm_nattch--;
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700290 if (shm_may_destroy(ns, shp))
Kirill Korotaev4e982312006-10-02 02:18:22 -0700291 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else
293 shm_unlock(shp);
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800294done:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700295 up_write(&shm_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700298/* Called with ns->shm_ids(ns).rwsem locked */
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700299static int shm_try_destroy_orphaned(int id, void *p, void *data)
300{
301 struct ipc_namespace *ns = data;
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400302 struct kern_ipc_perm *ipcp = p;
303 struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700304
305 /*
306 * We want to destroy segments without users and with already
307 * exit'ed originating process.
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400308 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700309 * As shp->* are changed under rwsem, it's safe to skip shp locking.
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700310 */
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400311 if (shp->shm_creator != NULL)
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700312 return 0;
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700313
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400314 if (shm_may_destroy(ns, shp)) {
315 shm_lock_by_ptr(shp);
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700316 shm_destroy(ns, shp);
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400317 }
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700318 return 0;
319}
320
321void shm_destroy_orphaned(struct ipc_namespace *ns)
322{
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700323 down_write(&shm_ids(ns).rwsem);
Vasiliy Kulikov33a30ed2011-08-03 22:26:55 +0400324 if (shm_ids(ns).in_use)
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400325 idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700326 up_write(&shm_ids(ns).rwsem);
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700327}
328
Jack Miller83293c02014-08-08 14:23:21 -0700329/* Locking assumes this will only be called with task == current */
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700330void exit_shm(struct task_struct *task)
331{
Vasiliy Kulikov4c677e22011-07-29 03:56:40 +0400332 struct ipc_namespace *ns = task->nsproxy->ipc_ns;
Jack Millerab602f72014-08-08 14:23:19 -0700333 struct shmid_kernel *shp, *n;
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700334
Jack Miller83293c02014-08-08 14:23:21 -0700335 if (list_empty(&task->sysvshm.shm_clist))
Vasiliy Kulikov298507d2011-08-03 22:28:26 +0400336 return;
337
Jack Miller83293c02014-08-08 14:23:21 -0700338 /*
339 * If kernel.shm_rmid_forced is not set then only keep track of
340 * which shmids are orphaned, so that a later set of the sysctl
341 * can clean them up.
342 */
343 if (!ns->shm_rmid_forced) {
344 down_read(&shm_ids(ns).rwsem);
345 list_for_each_entry(shp, &task->sysvshm.shm_clist, shm_clist)
346 shp->shm_creator = NULL;
347 /*
348 * Only under read lock but we are only called on current
349 * so no entry on the list will be shared.
350 */
351 list_del(&task->sysvshm.shm_clist);
352 up_read(&shm_ids(ns).rwsem);
353 return;
354 }
355
356 /*
357 * Destroy all already created segments, that were not yet mapped,
358 * and mark any mapped as orphan to cover the sysctl toggling.
359 * Destroy is skipped if shm_may_destroy() returns false.
360 */
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700361 down_write(&shm_ids(ns).rwsem);
Jack Miller83293c02014-08-08 14:23:21 -0700362 list_for_each_entry_safe(shp, n, &task->sysvshm.shm_clist, shm_clist) {
363 shp->shm_creator = NULL;
364
365 if (shm_may_destroy(ns, shp)) {
366 shm_lock_by_ptr(shp);
367 shm_destroy(ns, shp);
368 }
369 }
370
371 /* Remove the list head from any segments still attached. */
Jack Millerab602f72014-08-08 14:23:19 -0700372 list_del(&task->sysvshm.shm_clist);
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700373 up_write(&shm_ids(ns).rwsem);
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -0700374}
375
Nick Piggind0217ac2007-07-19 01:47:03 -0700376static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800377{
378 struct file *file = vma->vm_file;
379 struct shm_file_data *sfd = shm_file_data(file);
380
Nick Piggind0217ac2007-07-19 01:47:03 -0700381 return sfd->vm_ops->fault(vma, vmf);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800382}
383
Mike Kravetz86c8c892018-03-28 16:01:01 -0700384static int shm_split(struct vm_area_struct *vma, unsigned long addr)
385{
386 struct file *file = vma->vm_file;
387 struct shm_file_data *sfd = shm_file_data(file);
388
389 if (sfd->vm_ops && sfd->vm_ops->split)
390 return sfd->vm_ops->split(vma, addr);
391
392 return 0;
393}
394
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800395#ifdef CONFIG_NUMA
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700396static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800397{
398 struct file *file = vma->vm_file;
399 struct shm_file_data *sfd = shm_file_data(file);
400 int err = 0;
401 if (sfd->vm_ops->set_policy)
402 err = sfd->vm_ops->set_policy(vma, new);
403 return err;
404}
405
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700406static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
407 unsigned long addr)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800408{
409 struct file *file = vma->vm_file;
410 struct shm_file_data *sfd = shm_file_data(file);
411 struct mempolicy *pol = NULL;
412
413 if (sfd->vm_ops->get_policy)
414 pol = sfd->vm_ops->get_policy(vma, addr);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700415 else if (vma->vm_policy)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800416 pol = vma->vm_policy;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700417
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800418 return pol;
419}
420#endif
421
Manfred Spraul239521f2014-01-27 17:07:04 -0800422static int shm_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800424 struct shm_file_data *sfd = shm_file_data(file);
David Howellsb0e15192006-01-06 00:11:42 -0800425 int ret;
426
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800427 /*
428 * In case of remap_file_pages() emulation, the file can represent
429 * removed IPC ID: propogate shm_lock() error to caller.
430 */
431 ret =__shm_open(vma);
432 if (ret)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800433 return ret;
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800434
435 ret = sfd->file->f_op->mmap(sfd->file, vma);
436 if (ret) {
437 shm_close(vma);
438 return ret;
439 }
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800440 sfd->vm_ops = vma->vm_ops;
David Howells2e92a3b2007-07-31 00:37:24 -0700441#ifdef CONFIG_MMU
Davidlohr Buesod0edd852015-09-09 15:39:20 -0700442 WARN_ON(!sfd->vm_ops->fault);
David Howells2e92a3b2007-07-31 00:37:24 -0700443#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800444 vma->vm_ops = &shm_vm_ops;
Kirill A. Shutemov1ac0b6d2016-02-17 13:11:35 -0800445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Kirill Korotaev4e982312006-10-02 02:18:22 -0700448static int shm_release(struct inode *ino, struct file *file)
449{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800450 struct shm_file_data *sfd = shm_file_data(file);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700451
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800452 put_ipc_ns(sfd->ns);
453 shm_file_data(file) = NULL;
454 kfree(sfd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700455 return 0;
456}
457
Josef Bacik02c24a82011-07-16 20:44:56 -0400458static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Adam Litke516dffd2007-03-01 15:46:08 -0800459{
Adam Litke516dffd2007-03-01 15:46:08 -0800460 struct shm_file_data *sfd = shm_file_data(file);
Adam Litke516dffd2007-03-01 15:46:08 -0800461
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200462 if (!sfd->file->f_op->fsync)
463 return -EINVAL;
Josef Bacik02c24a82011-07-16 20:44:56 -0400464 return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
Adam Litke516dffd2007-03-01 15:46:08 -0800465}
466
Will Deacon7d8a4562012-06-07 14:21:13 -0700467static long shm_fallocate(struct file *file, int mode, loff_t offset,
468 loff_t len)
469{
470 struct shm_file_data *sfd = shm_file_data(file);
471
472 if (!sfd->file->f_op->fallocate)
473 return -EOPNOTSUPP;
474 return sfd->file->f_op->fallocate(file, mode, offset, len);
475}
476
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800477static unsigned long shm_get_unmapped_area(struct file *file,
478 unsigned long addr, unsigned long len, unsigned long pgoff,
479 unsigned long flags)
480{
481 struct shm_file_data *sfd = shm_file_data(file);
Al Viroc4caa772009-11-30 08:38:43 -0500482 return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
483 pgoff, flags);
Adam Litke516dffd2007-03-01 15:46:08 -0800484}
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800485
Arjan van de Ven9a321442007-02-12 00:55:35 -0800486static const struct file_operations shm_file_operations = {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700487 .mmap = shm_mmap,
Adam Litke516dffd2007-03-01 15:46:08 -0800488 .fsync = shm_fsync,
Kirill Korotaev4e982312006-10-02 02:18:22 -0700489 .release = shm_release,
David Howellsed5e5892010-01-15 17:01:32 -0800490 .get_unmapped_area = shm_get_unmapped_area,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200491 .llseek = noop_llseek,
Will Deacon7d8a4562012-06-07 14:21:13 -0700492 .fallocate = shm_fallocate,
Al Viroc4caa772009-11-30 08:38:43 -0500493};
494
Hugh Dickinsc01d5b32016-07-26 15:26:15 -0700495/*
496 * shm_file_operations_huge is now identical to shm_file_operations,
497 * but we keep it distinct for the sake of is_file_shm_hugepages().
498 */
Al Viroc4caa772009-11-30 08:38:43 -0500499static const struct file_operations shm_file_operations_huge = {
500 .mmap = shm_mmap,
501 .fsync = shm_fsync,
502 .release = shm_release,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800503 .get_unmapped_area = shm_get_unmapped_area,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200504 .llseek = noop_llseek,
Will Deacon7d8a4562012-06-07 14:21:13 -0700505 .fallocate = shm_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506};
507
Yaowei Bai2954e4402016-01-20 15:01:11 -0800508bool is_file_shm_hugepages(struct file *file)
Al Viroc4caa772009-11-30 08:38:43 -0500509{
510 return file->f_op == &shm_file_operations_huge;
511}
512
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400513static const struct vm_operations_struct shm_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .open = shm_open, /* callback for a new vm-area open */
515 .close = shm_close, /* callback for when the vm-area is released */
Nick Piggin54cb8822007-07-19 01:46:59 -0700516 .fault = shm_fault,
Mike Kravetz86c8c892018-03-28 16:01:01 -0700517 .split = shm_split,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800518#if defined(CONFIG_NUMA)
519 .set_policy = shm_set_policy,
520 .get_policy = shm_get_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#endif
522};
523
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700524/**
525 * newseg - Create a new shared memory segment
526 * @ns: namespace
527 * @params: ptr to the structure that contains key, size and shmflg
528 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700529 * Called with shm_ids.rwsem held as a writer.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700530 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700531static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700533 key_t key = params->key;
534 int shmflg = params->flg;
535 size_t size = params->u.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int error;
537 struct shmid_kernel *shp;
Robin Holtd69f3ba2013-04-30 19:15:54 -0700538 size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Manfred Spraul239521f2014-01-27 17:07:04 -0800539 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 char name[13];
541 int id;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900542 vm_flags_t acctflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Kirill Korotaev4e982312006-10-02 02:18:22 -0700544 if (size < SHMMIN || size > ns->shm_ctlmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -EINVAL;
546
Manfred Spraul13763272014-06-06 14:37:41 -0700547 if (numpages << PAGE_SHIFT < size)
548 return -ENOSPC;
549
Manfred Spraul09c6eb12014-06-06 14:37:40 -0700550 if (ns->shm_tot + numpages < ns->shm_tot ||
551 ns->shm_tot + numpages > ns->shm_ctlall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -ENOSPC;
553
554 shp = ipc_rcu_alloc(sizeof(*shp));
555 if (!shp)
556 return -ENOMEM;
557
558 shp->shm_perm.key = key;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800559 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 shp->mlock_user = NULL;
561
562 shp->shm_perm.security = NULL;
563 error = security_shm_alloc(shp);
564 if (error) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700565 ipc_rcu_putref(shp, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return error;
567 }
568
Manfred Spraul239521f2014-01-27 17:07:04 -0800569 sprintf(name, "SYSV%08x", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (shmflg & SHM_HUGETLB) {
Andrew Mortonc103a4d2013-07-08 16:01:08 -0700571 struct hstate *hs;
Li Zefan091d0d52013-05-09 15:08:15 +0800572 size_t hugesize;
573
Andrew Mortonc103a4d2013-07-08 16:01:08 -0700574 hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +0800575 if (!hs) {
576 error = -EINVAL;
577 goto no_file;
578 }
579 hugesize = ALIGN(size, huge_page_size(hs));
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -0700580
Mel Gorman5a6fe122009-02-10 14:02:27 +0000581 /* hugetlb_file_setup applies strict accounting */
582 if (shmflg & SHM_NORESERVE)
583 acctflag = VM_NORESERVE;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -0700584 file = hugetlb_file_setup(name, hugesize, acctflag,
Andi Kleen42d73952012-12-11 16:01:34 -0800585 &shp->mlock_user, HUGETLB_SHMFS_INODE,
586 (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } else {
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800588 /*
589 * Do not allow no accounting for OVERCOMMIT_NEVER, even
Manfred Spraul239521f2014-01-27 17:07:04 -0800590 * if it's asked for.
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800591 */
592 if ((shmflg & SHM_NORESERVE) &&
593 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -0800594 acctflag = VM_NORESERVE;
Stephen Smalleye1832f22015-08-06 15:46:55 -0700595 file = shmem_kernel_file_setup(name, size, acctflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 error = PTR_ERR(file);
598 if (IS_ERR(file))
599 goto no_file;
600
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700601 shp->shm_cprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 shp->shm_lprid = 0;
603 shp->shm_atim = shp->shm_dtim = 0;
604 shp->shm_ctim = get_seconds();
605 shp->shm_segsz = size;
606 shp->shm_nattch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 shp->shm_file = file;
Vasiliy Kulikov5774ed02011-07-29 03:55:31 +0400608 shp->shm_creator = current;
Linus Torvaldsb9a53222015-09-30 12:48:40 -0400609
610 id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
611 if (id < 0) {
612 error = id;
613 goto no_id;
614 }
615
Jack Millerab602f72014-08-08 14:23:19 -0700616 list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
Davidlohr Buesodbfcd912013-07-08 16:01:09 -0700617
Badari Pulavarty30475cc2007-06-16 10:15:59 -0700618 /*
619 * shmid gets reported as "inode#" in /proc/pid/maps.
620 * proc-ps tools use this. Changing this will break them.
621 */
Al Viro496ad9a2013-01-23 17:07:38 -0500622 file_inode(file)->i_ino = shp->shm_perm.id;
Krishnakumar R551110a2005-10-29 18:16:45 -0700623
Kirill Korotaev4e982312006-10-02 02:18:22 -0700624 ns->shm_tot += numpages;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700625 error = shp->shm_perm.id;
Davidlohr Buesodbfcd912013-07-08 16:01:09 -0700626
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700627 ipc_unlock_object(&shp->shm_perm);
Davidlohr Buesodbfcd912013-07-08 16:01:09 -0700628 rcu_read_unlock();
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700629 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631no_id:
Hugh Dickins2195d282009-09-12 12:21:27 +0100632 if (is_file_hugepages(file) && shp->mlock_user)
Hugh Dickins353d5c32009-08-24 16:30:28 +0100633 user_shm_unlock(size, shp->mlock_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 fput(file);
635no_file:
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700636 ipc_rcu_putref(shp, shm_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return error;
638}
639
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700640/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700641 * Called with shm_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700642 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700643static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700644{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700645 struct shmid_kernel *shp;
646
647 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
648 return security_shm_associate(shp, shmflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700649}
650
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700651/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700652 * Called with shm_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700653 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700654static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
655 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700656{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700657 struct shmid_kernel *shp;
658
659 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
660 if (shp->shm_segsz < params->u.size)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700661 return -EINVAL;
662
663 return 0;
664}
665
Heiko Carstensd5460c92009-01-14 14:14:27 +0100666SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700668 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700669 static const struct ipc_ops shm_ops = {
670 .getnew = newseg,
671 .associate = shm_security,
672 .more_checks = shm_more_checks,
673 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700674 struct ipc_params shm_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Kirill Korotaev4e982312006-10-02 02:18:22 -0700676 ns = current->nsproxy->ipc_ns;
677
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700678 shm_params.key = key;
679 shm_params.flg = shmflg;
680 shm_params.u.size = size;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700681
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700682 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
686{
Manfred Spraul239521f2014-01-27 17:07:04 -0800687 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 case IPC_64:
689 return copy_to_user(buf, in, sizeof(*in));
690 case IPC_OLD:
691 {
692 struct shmid_ds out;
693
Vasiliy Kulikov3af54c92010-10-30 18:22:49 +0400694 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
696 out.shm_segsz = in->shm_segsz;
697 out.shm_atime = in->shm_atime;
698 out.shm_dtime = in->shm_dtime;
699 out.shm_ctime = in->shm_ctime;
700 out.shm_cpid = in->shm_cpid;
701 out.shm_lpid = in->shm_lpid;
702 out.shm_nattch = in->shm_nattch;
703
704 return copy_to_user(buf, &out, sizeof(out));
705 }
706 default:
707 return -EINVAL;
708 }
709}
710
Pierre Peiffer016d7132008-04-29 01:00:50 -0700711static inline unsigned long
712copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Manfred Spraul239521f2014-01-27 17:07:04 -0800714 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -0700716 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case IPC_OLD:
720 {
721 struct shmid_ds tbuf_old;
722
723 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
724 return -EFAULT;
725
Pierre Peiffer016d7132008-04-29 01:00:50 -0700726 out->shm_perm.uid = tbuf_old.shm_perm.uid;
727 out->shm_perm.gid = tbuf_old.shm_perm.gid;
728 out->shm_perm.mode = tbuf_old.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 return 0;
731 }
732 default:
733 return -EINVAL;
734 }
735}
736
737static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
738{
Manfred Spraul239521f2014-01-27 17:07:04 -0800739 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 case IPC_64:
741 return copy_to_user(buf, in, sizeof(*in));
742 case IPC_OLD:
743 {
744 struct shminfo out;
745
Manfred Spraul239521f2014-01-27 17:07:04 -0800746 if (in->shmmax > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 out.shmmax = INT_MAX;
748 else
749 out.shmmax = (int)in->shmmax;
750
751 out.shmmin = in->shmmin;
752 out.shmmni = in->shmmni;
753 out.shmseg = in->shmseg;
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700754 out.shmall = in->shmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 return copy_to_user(buf, &out, sizeof(out));
757 }
758 default:
759 return -EINVAL;
760 }
761}
762
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700763/*
Helge Dellerb7952182010-10-27 15:34:16 -0700764 * Calculate and add used RSS and swap pages of a shm.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700765 * Called with shm_ids.rwsem held as a reader
Helge Dellerb7952182010-10-27 15:34:16 -0700766 */
767static void shm_add_rss_swap(struct shmid_kernel *shp,
768 unsigned long *rss_add, unsigned long *swp_add)
769{
770 struct inode *inode;
771
Al Viro496ad9a2013-01-23 17:07:38 -0500772 inode = file_inode(shp->shm_file);
Helge Dellerb7952182010-10-27 15:34:16 -0700773
774 if (is_file_hugepages(shp->shm_file)) {
775 struct address_space *mapping = inode->i_mapping;
776 struct hstate *h = hstate_file(shp->shm_file);
777 *rss_add += pages_per_huge_page(h) * mapping->nrpages;
778 } else {
779#ifdef CONFIG_SHMEM
780 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700781 spin_lock_irq(&info->lock);
Helge Dellerb7952182010-10-27 15:34:16 -0700782 *rss_add += inode->i_mapping->nrpages;
783 *swp_add += info->swapped;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700784 spin_unlock_irq(&info->lock);
Helge Dellerb7952182010-10-27 15:34:16 -0700785#else
786 *rss_add += inode->i_mapping->nrpages;
787#endif
788 }
789}
790
791/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700792 * Called with shm_ids.rwsem held as a reader
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700793 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700794static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
795 unsigned long *swp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700797 int next_id;
798 int total, in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 *rss = 0;
801 *swp = 0;
802
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700803 in_use = shm_ids(ns).in_use;
804
805 for (total = 0, next_id = 0; total < in_use; next_id++) {
Tony Battersbye562aeb2009-04-02 16:58:26 -0700806 struct kern_ipc_perm *ipc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct shmid_kernel *shp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Tony Battersbye562aeb2009-04-02 16:58:26 -0700809 ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
810 if (ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 continue;
Tony Battersbye562aeb2009-04-02 16:58:26 -0700812 shp = container_of(ipc, struct shmid_kernel, shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Helge Dellerb7952182010-10-27 15:34:16 -0700814 shm_add_rss_swap(shp, rss, swp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700815
816 total++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818}
819
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700820/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700821 * This function handles some shmctl commands which require the rwsem
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700822 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700823 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700824 */
825static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
826 struct shmid_ds __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700828 struct kern_ipc_perm *ipcp;
Pierre Peiffer016d7132008-04-29 01:00:50 -0700829 struct shmid64_ds shmid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct shmid_kernel *shp;
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700831 int err;
832
833 if (cmd == IPC_SET) {
Pierre Peiffer016d7132008-04-29 01:00:50 -0700834 if (copy_shmid_from_user(&shmid64, buf, version))
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700835 return -EFAULT;
836 }
837
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700838 down_write(&shm_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700839 rcu_read_lock();
840
Davidlohr Bueso79ccf0f2013-09-11 14:26:16 -0700841 ipcp = ipcctl_pre_down_nolock(ns, &shm_ids(ns), shmid, cmd,
842 &shmid64.shm_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700843 if (IS_ERR(ipcp)) {
844 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700845 goto out_unlock1;
846 }
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700847
Pierre Peiffera5f75e72008-04-29 01:00:54 -0700848 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700849
850 err = security_shm_shmctl(shp, cmd);
851 if (err)
Davidlohr Bueso79ccf0f2013-09-11 14:26:16 -0700852 goto out_unlock1;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700853
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700854 switch (cmd) {
855 case IPC_RMID:
Davidlohr Bueso79ccf0f2013-09-11 14:26:16 -0700856 ipc_lock_object(&shp->shm_perm);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700857 /* do_shm_rmid unlocks the ipc object and rcu */
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700858 do_shm_rmid(ns, ipcp);
859 goto out_up;
860 case IPC_SET:
Davidlohr Bueso79ccf0f2013-09-11 14:26:16 -0700861 ipc_lock_object(&shp->shm_perm);
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800862 err = ipc_update_perm(&shmid64.shm_perm, ipcp);
863 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700864 goto out_unlock0;
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700865 shp->shm_ctim = get_seconds();
866 break;
867 default:
868 err = -EINVAL;
Davidlohr Bueso79ccf0f2013-09-11 14:26:16 -0700869 goto out_unlock1;
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700870 }
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700871
872out_unlock0:
873 ipc_unlock_object(&shp->shm_perm);
874out_unlock1:
875 rcu_read_unlock();
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700876out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700877 up_write(&shm_ids(ns).rwsem);
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700878 return err;
879}
880
Davidlohr Bueso68eccc12013-09-11 14:26:18 -0700881static int shmctl_nolock(struct ipc_namespace *ns, int shmid,
882 int cmd, int version, void __user *buf)
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700883{
Davidlohr Bueso68eccc12013-09-11 14:26:18 -0700884 int err;
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -0700885 struct shmid_kernel *shp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Davidlohr Bueso68eccc12013-09-11 14:26:18 -0700887 /* preliminary security checks for *_INFO */
888 if (cmd == IPC_INFO || cmd == SHM_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 err = security_shm_shmctl(NULL, cmd);
890 if (err)
891 return err;
Davidlohr Bueso68eccc12013-09-11 14:26:18 -0700892 }
893
894 switch (cmd) {
895 case IPC_INFO:
896 {
897 struct shminfo64 shminfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
WANG Conge8148f72009-01-06 14:42:49 -0800899 memset(&shminfo, 0, sizeof(shminfo));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700900 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
901 shminfo.shmmax = ns->shm_ctlmax;
902 shminfo.shmall = ns->shm_ctlall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 shminfo.shmmin = SHMMIN;
Manfred Spraul239521f2014-01-27 17:07:04 -0800905 if (copy_shminfo_to_user(buf, &shminfo, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -EFAULT;
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700907
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700908 down_read(&shm_ids(ns).rwsem);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700909 err = ipc_get_maxid(&shm_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700910 up_read(&shm_ids(ns).rwsem);
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700911
Manfred Spraul239521f2014-01-27 17:07:04 -0800912 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 err = 0;
914 goto out;
915 }
916 case SHM_INFO:
917 {
918 struct shm_info shm_info;
919
WANG Conge8148f72009-01-06 14:42:49 -0800920 memset(&shm_info, 0, sizeof(shm_info));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700921 down_read(&shm_ids(ns).rwsem);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700922 shm_info.used_ids = shm_ids(ns).in_use;
Manfred Spraul239521f2014-01-27 17:07:04 -0800923 shm_get_stat(ns, &shm_info.shm_rss, &shm_info.shm_swp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700924 shm_info.shm_tot = ns->shm_tot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 shm_info.swap_attempts = 0;
926 shm_info.swap_successes = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700927 err = ipc_get_maxid(&shm_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700928 up_read(&shm_ids(ns).rwsem);
WANG Conge8148f72009-01-06 14:42:49 -0800929 if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 err = -EFAULT;
931 goto out;
932 }
933
934 err = err < 0 ? 0 : err;
935 goto out;
936 }
937 case SHM_STAT:
938 case IPC_STAT:
939 {
940 struct shmid64_ds tbuf;
941 int result;
Nadia Derbey023a5352007-10-18 23:40:51 -0700942
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700943 rcu_read_lock();
Nadia Derbey023a5352007-10-18 23:40:51 -0700944 if (cmd == SHM_STAT) {
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700945 shp = shm_obtain_object(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700946 if (IS_ERR(shp)) {
947 err = PTR_ERR(shp);
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700948 goto out_unlock;
Nadia Derbey023a5352007-10-18 23:40:51 -0700949 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700950 result = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 } else {
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700952 shp = shm_obtain_object_check(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700953 if (IS_ERR(shp)) {
954 err = PTR_ERR(shp);
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700955 goto out_unlock;
Nadia Derbey023a5352007-10-18 23:40:51 -0700956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 result = 0;
958 }
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700959
WANG Conge8148f72009-01-06 14:42:49 -0800960 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700961 if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto out_unlock;
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 err = security_shm_shmctl(shp, cmd);
965 if (err)
966 goto out_unlock;
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700967
Nadia Derbey023a5352007-10-18 23:40:51 -0700968 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
970 tbuf.shm_segsz = shp->shm_segsz;
971 tbuf.shm_atime = shp->shm_atim;
972 tbuf.shm_dtime = shp->shm_dtim;
973 tbuf.shm_ctime = shp->shm_ctim;
974 tbuf.shm_cpid = shp->shm_cprid;
975 tbuf.shm_lpid = shp->shm_lprid;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800976 tbuf.shm_nattch = shp->shm_nattch;
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700977 rcu_read_unlock();
978
979 if (copy_shmid_to_user(buf, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 err = -EFAULT;
981 else
982 err = result;
983 goto out;
984 }
Davidlohr Bueso68eccc12013-09-11 14:26:18 -0700985 default:
986 return -EINVAL;
987 }
988
989out_unlock:
Davidlohr Buesoc97cb9c2013-09-11 14:26:20 -0700990 rcu_read_unlock();
Davidlohr Bueso68eccc12013-09-11 14:26:18 -0700991out:
992 return err;
993}
994
995SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
996{
997 struct shmid_kernel *shp;
998 int err, version;
999 struct ipc_namespace *ns;
1000
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001001 if (cmd < 0 || shmid < 0)
1002 return -EINVAL;
Davidlohr Bueso68eccc12013-09-11 14:26:18 -07001003
1004 version = ipc_parse_version(&cmd);
1005 ns = current->nsproxy->ipc_ns;
1006
1007 switch (cmd) {
1008 case IPC_INFO:
1009 case SHM_INFO:
1010 case SHM_STAT:
1011 case IPC_STAT:
1012 return shmctl_nolock(ns, shmid, cmd, version, buf);
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001013 case IPC_RMID:
1014 case IPC_SET:
1015 return shmctl_down(ns, shmid, cmd, buf, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 case SHM_LOCK:
1017 case SHM_UNLOCK:
1018 {
Hugh Dickins850465792012-01-20 14:34:19 -08001019 struct file *shm_file;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001020
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001021 rcu_read_lock();
1022 shp = shm_obtain_object_check(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001023 if (IS_ERR(shp)) {
1024 err = PTR_ERR(shp);
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001025 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Al Viroa33e6752008-12-10 03:40:06 -05001028 audit_ipc_obj(&(shp->shm_perm));
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001029 err = security_shm_shmctl(shp, cmd);
1030 if (err)
1031 goto out_unlock1;
Steve Grubb073115d2006-04-02 17:07:33 -04001032
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001033 ipc_lock_object(&shp->shm_perm);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001034
1035 /* check if shm_destroy() is tearing down shp */
1036 if (!ipc_valid_object(&shp->shm_perm)) {
1037 err = -EIDRM;
1038 goto out_unlock0;
1039 }
1040
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001041 if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001042 kuid_t euid = current_euid();
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001043 if (!uid_eq(euid, shp->shm_perm.uid) &&
Jesper Nilsson3a726602013-11-21 14:32:08 -08001044 !uid_eq(euid, shp->shm_perm.cuid)) {
1045 err = -EPERM;
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001046 goto out_unlock0;
Jesper Nilsson3a726602013-11-21 14:32:08 -08001047 }
1048 if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
1049 err = -EPERM;
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001050 goto out_unlock0;
Jesper Nilsson3a726602013-11-21 14:32:08 -08001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Hugh Dickins850465792012-01-20 14:34:19 -08001054 shm_file = shp->shm_file;
1055 if (is_file_hugepages(shm_file))
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001056 goto out_unlock0;
Hugh Dickins850465792012-01-20 14:34:19 -08001057
1058 if (cmd == SHM_LOCK) {
David Howells86a264a2008-11-14 10:39:18 +11001059 struct user_struct *user = current_user();
Hugh Dickins850465792012-01-20 14:34:19 -08001060 err = shmem_lock(shm_file, 1, user);
1061 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
1062 shp->shm_perm.mode |= SHM_LOCKED;
1063 shp->mlock_user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001065 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Hugh Dickins850465792012-01-20 14:34:19 -08001067
1068 /* SHM_UNLOCK */
1069 if (!(shp->shm_perm.mode & SHM_LOCKED))
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001070 goto out_unlock0;
Hugh Dickins850465792012-01-20 14:34:19 -08001071 shmem_lock(shm_file, 0, shp->mlock_user);
1072 shp->shm_perm.mode &= ~SHM_LOCKED;
1073 shp->mlock_user = NULL;
1074 get_file(shm_file);
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001075 ipc_unlock_object(&shp->shm_perm);
1076 rcu_read_unlock();
Hugh Dickins24513262012-01-20 14:34:21 -08001077 shmem_unlock_mapping(shm_file->f_mapping);
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001078
Hugh Dickins850465792012-01-20 14:34:19 -08001079 fput(shm_file);
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -07001080 return err;
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 default:
Pierre Peiffer8d4cc8b2008-04-29 01:00:47 -07001083 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Davidlohr Bueso2caacaa2013-09-11 14:26:21 -07001086out_unlock0:
1087 ipc_unlock_object(&shp->shm_perm);
1088out_unlock1:
1089 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return err;
1091}
1092
1093/*
1094 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
1095 *
1096 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
1097 * "raddr" thing points to kernel space, and there has to be a wrapper around
1098 * this.
1099 */
Davidlohr Bueso270e84a2017-02-27 14:28:24 -08001100long do_shmat(int shmid, char __user *shmaddr, int shmflg,
1101 ulong *raddr, unsigned long shmlba)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct shmid_kernel *shp;
1104 unsigned long addr;
1105 unsigned long size;
Manfred Spraul239521f2014-01-27 17:07:04 -08001106 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int err;
1108 unsigned long flags;
1109 unsigned long prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int acc_mode;
Kirill Korotaev4e982312006-10-02 02:18:22 -07001111 struct ipc_namespace *ns;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001112 struct shm_file_data *sfd;
1113 struct path path;
Al Viroaeb5d722008-09-02 15:28:45 -04001114 fmode_t f_mode;
Michel Lespinasse41badc12013-02-22 16:32:47 -08001115 unsigned long populate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001117 err = -EINVAL;
1118 if (shmid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001120 else if ((addr = (ulong)shmaddr)) {
Will Deacon079a96a2012-07-30 14:42:38 -07001121 if (addr & (shmlba - 1)) {
Davidlohr Bueso270e84a2017-02-27 14:28:24 -08001122 /*
1123 * Round down to the nearest multiple of shmlba.
1124 * For sane do_mmap_pgoff() parameters, avoid
1125 * round downs that trigger nil-page and MAP_FIXED.
1126 */
1127 if ((shmflg & SHM_RND) && addr >= shmlba)
1128 addr &= ~(shmlba - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 else
1130#ifndef __ARCH_FORCE_SHMLBA
1131 if (addr & ~PAGE_MASK)
1132#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 flags = MAP_SHARED | MAP_FIXED;
1136 } else {
1137 if ((shmflg & SHM_REMAP))
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 flags = MAP_SHARED;
1141 }
1142
1143 if (shmflg & SHM_RDONLY) {
1144 prot = PROT_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 acc_mode = S_IRUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001146 f_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 } else {
1148 prot = PROT_READ | PROT_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 acc_mode = S_IRUGO | S_IWUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001150 f_mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 if (shmflg & SHM_EXEC) {
1153 prot |= PROT_EXEC;
1154 acc_mode |= S_IXUGO;
1155 }
1156
1157 /*
1158 * We cannot rely on the fs check since SYSV IPC does have an
1159 * additional creator id...
1160 */
Kirill Korotaev4e982312006-10-02 02:18:22 -07001161 ns = current->nsproxy->ipc_ns;
Davidlohr Buesoc2c737a2013-09-11 14:26:23 -07001162 rcu_read_lock();
1163 shp = shm_obtain_object_check(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001164 if (IS_ERR(shp)) {
1165 err = PTR_ERR(shp);
Davidlohr Buesoc2c737a2013-09-11 14:26:23 -07001166 goto out_unlock;
Nadia Derbey023a5352007-10-18 23:40:51 -07001167 }
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001168
1169 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001170 if (ipcperms(ns, &shp->shm_perm, acc_mode))
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001171 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 err = security_shm_shmat(shp, shmaddr, shmflg);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001174 if (err)
1175 goto out_unlock;
1176
Davidlohr Buesoc2c737a2013-09-11 14:26:23 -07001177 ipc_lock_object(&shp->shm_perm);
Greg Thelena399b292013-11-21 14:32:00 -08001178
1179 /* check if shm_destroy() is tearing down shp */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001180 if (!ipc_valid_object(&shp->shm_perm)) {
Greg Thelena399b292013-11-21 14:32:00 -08001181 ipc_unlock_object(&shp->shm_perm);
1182 err = -EIDRM;
1183 goto out_unlock;
1184 }
1185
Al Viro2c48b9c2009-08-09 00:52:35 +04001186 path = shp->shm_file->f_path;
1187 path_get(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 shp->shm_nattch++;
David Howells75c3cfa2015-03-17 22:26:12 +00001189 size = i_size_read(d_inode(path.dentry));
Davidlohr Buesoc2c737a2013-09-11 14:26:23 -07001190 ipc_unlock_object(&shp->shm_perm);
1191 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001193 err = -ENOMEM;
1194 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
Davidlohr Buesof42569b2013-09-11 14:26:22 -07001195 if (!sfd) {
1196 path_put(&path);
1197 goto out_nattch;
1198 }
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001199
Al Viro2c48b9c2009-08-09 00:52:35 +04001200 file = alloc_file(&path, f_mode,
1201 is_file_hugepages(shp->shm_file) ?
Al Viroc4caa772009-11-30 08:38:43 -05001202 &shm_file_operations_huge :
1203 &shm_file_operations);
Anatol Pomozov39b65252012-09-12 20:11:55 -07001204 err = PTR_ERR(file);
Davidlohr Buesof42569b2013-09-11 14:26:22 -07001205 if (IS_ERR(file)) {
1206 kfree(sfd);
1207 path_put(&path);
1208 goto out_nattch;
1209 }
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001210
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001211 file->private_data = sfd;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001212 file->f_mapping = shp->shm_file->f_mapping;
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001213 sfd->id = shp->shm_perm.id;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001214 sfd->ns = get_ipc_ns(ns);
1215 sfd->file = shp->shm_file;
1216 sfd->vm_ops = NULL;
1217
Al Viro8b3ec682012-05-30 17:11:23 -04001218 err = security_mmap_file(file, prot, flags);
1219 if (err)
1220 goto out_fput;
1221
Michal Hocko91f4f942016-05-23 16:25:51 -07001222 if (down_write_killable(&current->mm->mmap_sem)) {
1223 err = -EINTR;
1224 goto out_fput;
1225 }
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (addr && !(shmflg & SHM_REMAP)) {
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001228 err = -EINVAL;
Manfred Spraul247a8ce2014-06-06 14:37:38 -07001229 if (addr + size < addr)
1230 goto invalid;
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (find_vma_intersection(current->mm, addr, addr + size))
1233 goto invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Davidlohr Buesof42569b2013-09-11 14:26:22 -07001235
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001236 addr = do_mmap_pgoff(file, addr, size, prot, flags, 0, &populate);
1237 *raddr = addr;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001238 err = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001239 if (IS_ERR_VALUE(addr))
1240 err = (long)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241invalid:
1242 up_write(&current->mm->mmap_sem);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001243 if (populate)
Michel Lespinasse41badc12013-02-22 16:32:47 -08001244 mm_populate(addr, populate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Al Viro8b3ec682012-05-30 17:11:23 -04001246out_fput:
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001247 fput(file);
1248
1249out_nattch:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001250 down_write(&shm_ids(ns).rwsem);
Nadia Derbey00c2bf82008-07-25 01:48:03 -07001251 shp = shm_lock(ns, shmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 shp->shm_nattch--;
Vasiliy Kulikovb34a6b12011-07-26 16:08:48 -07001253 if (shm_may_destroy(ns, shp))
Kirill Korotaev4e982312006-10-02 02:18:22 -07001254 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 else
1256 shm_unlock(shp);
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001257 up_write(&shm_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return err;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001259
1260out_unlock:
Davidlohr Buesoc2c737a2013-09-11 14:26:23 -07001261 rcu_read_unlock();
Davidlohr Buesof42569b2013-09-11 14:26:22 -07001262out:
1263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Heiko Carstensd5460c92009-01-14 14:14:27 +01001266SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -07001267{
1268 unsigned long ret;
1269 long err;
1270
Will Deacon079a96a2012-07-30 14:42:38 -07001271 err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -07001272 if (err)
1273 return err;
1274 force_successful_syscall_return();
1275 return (long)ret;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/*
1279 * detach and kill segment if marked destroyed.
1280 * The work is done in shm_close.
1281 */
Heiko Carstensd5460c92009-01-14 14:14:27 +01001282SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct mm_struct *mm = current->mm;
Mike Frysinger586c7e62009-06-09 16:26:23 -07001285 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 unsigned long addr = (unsigned long)shmaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int retval = -EINVAL;
Mike Frysinger586c7e62009-06-09 16:26:23 -07001288#ifdef CONFIG_MMU
1289 loff_t size = 0;
Dave Hansend3c97902014-12-12 16:58:19 -08001290 struct file *file;
Mike Frysinger586c7e62009-06-09 16:26:23 -07001291 struct vm_area_struct *next;
1292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Hugh Dickinsdf1e2fb2006-03-24 03:18:06 -08001294 if (addr & ~PAGE_MASK)
1295 return retval;
1296
Michal Hocko91f4f942016-05-23 16:25:51 -07001297 if (down_write_killable(&mm->mmap_sem))
1298 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /*
1301 * This function tries to be smart and unmap shm segments that
1302 * were modified by partial mlock or munmap calls:
1303 * - It first determines the size of the shm segment that should be
1304 * unmapped: It searches for a vma that is backed by shm and that
1305 * started at address shmaddr. It records it's size and then unmaps
1306 * it.
1307 * - Then it unmaps all shm vmas that started at shmaddr and that
Dave Hansend3c97902014-12-12 16:58:19 -08001308 * are within the initially determined size and that are from the
1309 * same shm segment from which we determined the size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * Errors from do_munmap are ignored: the function only fails if
1311 * it's called with invalid parameters or if it's called to unmap
1312 * a part of a vma. Both calls in this function are for full vmas,
1313 * the parameters are directly copied from the vma itself and always
1314 * valid - therefore do_munmap cannot fail. (famous last words?)
1315 */
1316 /*
1317 * If it had been mremap()'d, the starting address would not
1318 * match the usual checks anyway. So assume all vma's are
1319 * above the starting address given.
1320 */
1321 vma = find_vma(mm, addr);
1322
David Howells8feae132009-01-08 12:04:47 +00001323#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 while (vma) {
1325 next = vma->vm_next;
1326
1327 /*
1328 * Check if the starting address would match, i.e. it's
1329 * a fragment created by mprotect() and/or munmap(), or it
1330 * otherwise it starts at this address with no hassles.
1331 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001332 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1334
Dave Hansend3c97902014-12-12 16:58:19 -08001335 /*
1336 * Record the file of the shm segment being
1337 * unmapped. With mremap(), someone could place
1338 * page from another segment but with equal offsets
1339 * in the range we are unmapping.
1340 */
1341 file = vma->vm_file;
Dave Hansen07a46ed2014-12-12 16:58:22 -08001342 size = i_size_read(file_inode(vma->vm_file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1344 /*
1345 * We discovered the size of the shm segment, so
1346 * break out of here and fall through to the next
1347 * loop that uses the size information to stop
1348 * searching for matching vma's.
1349 */
1350 retval = 0;
1351 vma = next;
1352 break;
1353 }
1354 vma = next;
1355 }
1356
1357 /*
1358 * We need look no further than the maximum address a fragment
1359 * could possibly have landed at. Also cast things to loff_t to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001360 * prevent overflows and make comparisons vs. equal-width types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
KAMEZAWA Hiroyuki8e367092006-02-10 01:51:12 -08001362 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1364 next = vma->vm_next;
1365
1366 /* finding a matching vma now does not alter retval */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001367 if ((vma->vm_ops == &shm_vm_ops) &&
Dave Hansend3c97902014-12-12 16:58:19 -08001368 ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
1369 (vma->vm_file == file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1371 vma = next;
1372 }
1373
David Howells8feae132009-01-08 12:04:47 +00001374#else /* CONFIG_MMU */
1375 /* under NOMMU conditions, the exact address to be destroyed must be
1376 * given */
Davidlohr Bueso530fcd162013-09-11 14:26:28 -07001377 if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
David Howells8feae132009-01-08 12:04:47 +00001378 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1379 retval = 0;
1380 }
1381
1382#endif
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 up_write(&mm->mmap_sem);
1385 return retval;
1386}
1387
1388#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001389static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001391 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07001392 struct shmid_kernel *shp = it;
Helge Dellerb7952182010-10-27 15:34:16 -07001393 unsigned long rss = 0, swp = 0;
1394
1395 shm_add_rss_swap(shp, &rss, &swp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Paul Menage6c826812008-06-12 15:21:49 -07001397#if BITS_PER_LONG <= 32
1398#define SIZE_SPEC "%10lu"
1399#else
1400#define SIZE_SPEC "%21lu"
1401#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Joe Perches7f032d62015-04-15 16:17:54 -07001403 seq_printf(s,
1404 "%10d %10d %4o " SIZE_SPEC " %5u %5u "
1405 "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
1406 SIZE_SPEC " " SIZE_SPEC "\n",
1407 shp->shm_perm.key,
1408 shp->shm_perm.id,
1409 shp->shm_perm.mode,
1410 shp->shm_segsz,
1411 shp->shm_cprid,
1412 shp->shm_lprid,
1413 shp->shm_nattch,
1414 from_kuid_munged(user_ns, shp->shm_perm.uid),
1415 from_kgid_munged(user_ns, shp->shm_perm.gid),
1416 from_kuid_munged(user_ns, shp->shm_perm.cuid),
1417 from_kgid_munged(user_ns, shp->shm_perm.cgid),
1418 shp->shm_atim,
1419 shp->shm_dtim,
1420 shp->shm_ctim,
1421 rss * PAGE_SIZE,
1422 swp * PAGE_SIZE);
1423
1424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426#endif