xref: /openbmc/linux/ipc/shm.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/ipc/shm.c
41da177e4SLinus Torvalds  * Copyright (C) 1992, 1993 Krishna Balasubramanian
51da177e4SLinus Torvalds  *	 Many improvements/fixes by Bruno Haible.
61da177e4SLinus Torvalds  * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
71da177e4SLinus Torvalds  * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
101da177e4SLinus Torvalds  * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
111da177e4SLinus Torvalds  * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
121da177e4SLinus Torvalds  * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
131da177e4SLinus Torvalds  * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
141da177e4SLinus Torvalds  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
151da177e4SLinus Torvalds  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
161da177e4SLinus Torvalds  *
17073115d6SSteve Grubb  * support for audit of ipc object properties and permission changes
18073115d6SSteve Grubb  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
194e982311SKirill Korotaev  *
204e982311SKirill Korotaev  * namespaces support
214e982311SKirill Korotaev  * OpenVZ, SWsoft Inc.
224e982311SKirill Korotaev  * Pavel Emelianov <xemul@openvz.org>
23c2c737a0SDavidlohr Bueso  *
24c2c737a0SDavidlohr Bueso  * Better ipc lock (kern_ipc_perm.lock) handling
25c2c737a0SDavidlohr Bueso  * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/hugetlb.h>
311da177e4SLinus Torvalds #include <linux/shm.h>
321da177e4SLinus Torvalds #include <linux/init.h>
331da177e4SLinus Torvalds #include <linux/file.h>
341da177e4SLinus Torvalds #include <linux/mman.h>
351da177e4SLinus Torvalds #include <linux/shmem_fs.h>
361da177e4SLinus Torvalds #include <linux/security.h>
371da177e4SLinus Torvalds #include <linux/syscalls.h>
381da177e4SLinus Torvalds #include <linux/audit.h>
39c59ede7bSRandy.Dunlap #include <linux/capability.h>
407d87e14cSStephen Rothwell #include <linux/ptrace.h>
4119b4946cSMike Waychison #include <linux/seq_file.h>
423e148c79SNadia Derbey #include <linux/rwsem.h>
434e982311SKirill Korotaev #include <linux/nsproxy.h>
44bc56bba8SEric W. Biederman #include <linux/mount.h>
45ae5e1b22SPavel Emelyanov #include <linux/ipc_namespace.h>
460eb71a9dSNeilBrown #include <linux/rhashtable.h>
477d87e14cSStephen Rothwell 
487153e402SPaul McQuade #include <linux/uaccess.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include "util.h"
511da177e4SLinus Torvalds 
52a2e102cdSEric W. Biederman struct shmid_kernel /* private to the kernel */
53a2e102cdSEric W. Biederman {
54a2e102cdSEric W. Biederman 	struct kern_ipc_perm	shm_perm;
55a2e102cdSEric W. Biederman 	struct file		*shm_file;
56a2e102cdSEric W. Biederman 	unsigned long		shm_nattch;
57a2e102cdSEric W. Biederman 	unsigned long		shm_segsz;
58a2e102cdSEric W. Biederman 	time64_t		shm_atim;
59a2e102cdSEric W. Biederman 	time64_t		shm_dtim;
60a2e102cdSEric W. Biederman 	time64_t		shm_ctim;
6198f929b1SEric W. Biederman 	struct pid		*shm_cprid;
6298f929b1SEric W. Biederman 	struct pid		*shm_lprid;
63d7c9e99aSAlexey Gladkov 	struct ucounts		*mlock_ucounts;
64a2e102cdSEric W. Biederman 
6585b6d246SAlexander Mikhalitsyn 	/*
6685b6d246SAlexander Mikhalitsyn 	 * The task created the shm object, for
6785b6d246SAlexander Mikhalitsyn 	 * task_lock(shp->shm_creator)
6885b6d246SAlexander Mikhalitsyn 	 */
69a2e102cdSEric W. Biederman 	struct task_struct	*shm_creator;
7085b6d246SAlexander Mikhalitsyn 
7185b6d246SAlexander Mikhalitsyn 	/*
7285b6d246SAlexander Mikhalitsyn 	 * List by creator. task_lock(->shm_creator) required for read/write.
7385b6d246SAlexander Mikhalitsyn 	 * If list_empty(), then the creator is dead already.
7485b6d246SAlexander Mikhalitsyn 	 */
7585b6d246SAlexander Mikhalitsyn 	struct list_head	shm_clist;
7685b6d246SAlexander Mikhalitsyn 	struct ipc_namespace	*ns;
77a2e102cdSEric W. Biederman } __randomize_layout;
78a2e102cdSEric W. Biederman 
79a2e102cdSEric W. Biederman /* shm_mode upper byte flags */
80a2e102cdSEric W. Biederman #define SHM_DEST	01000	/* segment will be destroyed on last detach */
81a2e102cdSEric W. Biederman #define SHM_LOCKED	02000   /* segment will not be swapped */
82a2e102cdSEric W. Biederman 
83bc56bba8SEric W. Biederman struct shm_file_data {
84bc56bba8SEric W. Biederman 	int id;
85bc56bba8SEric W. Biederman 	struct ipc_namespace *ns;
86bc56bba8SEric W. Biederman 	struct file *file;
87bc56bba8SEric W. Biederman 	const struct vm_operations_struct *vm_ops;
88bc56bba8SEric W. Biederman };
89bc56bba8SEric W. Biederman 
90bc56bba8SEric W. Biederman #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
91bc56bba8SEric W. Biederman 
929a32144eSArjan van de Ven static const struct file_operations shm_file_operations;
93f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shm_vm_ops;
941da177e4SLinus Torvalds 
95ed2ddbf8SPierre Peiffer #define shm_ids(ns)	((ns)->ids[IPC_SHM_IDS])
961da177e4SLinus Torvalds 
974e982311SKirill Korotaev #define shm_unlock(shp)			\
984e982311SKirill Korotaev 	ipc_unlock(&(shp)->shm_perm)
994e982311SKirill Korotaev 
1007748dbfaSNadia Derbey static int newseg(struct ipc_namespace *, struct ipc_params *);
101bc56bba8SEric W. Biederman static void shm_open(struct vm_area_struct *vma);
102bc56bba8SEric W. Biederman static void shm_close(struct vm_area_struct *vma);
1034e982311SKirill Korotaev static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
1041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10519b4946cSMike Waychison static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds 
shm_init_ns(struct ipc_namespace * ns)108eae04d25SDavidlohr Bueso void shm_init_ns(struct ipc_namespace *ns)
1094e982311SKirill Korotaev {
1104e982311SKirill Korotaev 	ns->shm_ctlmax = SHMMAX;
1114e982311SKirill Korotaev 	ns->shm_ctlall = SHMALL;
1124e982311SKirill Korotaev 	ns->shm_ctlmni = SHMMNI;
113b34a6b1dSVasiliy Kulikov 	ns->shm_rmid_forced = 0;
1144e982311SKirill Korotaev 	ns->shm_tot = 0;
115eae04d25SDavidlohr Bueso 	ipc_init_ids(&shm_ids(ns));
1164e982311SKirill Korotaev }
1171da177e4SLinus Torvalds 
118f4566f04SNadia Derbey /*
119d9a605e4SDavidlohr Bueso  * Called with shm_ids.rwsem (writer) and the shp structure locked.
120d9a605e4SDavidlohr Bueso  * Only shm_ids.rwsem remains locked on exit.
121f4566f04SNadia Derbey  */
do_shm_rmid(struct ipc_namespace * ns,struct kern_ipc_perm * ipcp)12201b8b07aSPierre Peiffer static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1234e982311SKirill Korotaev {
12401b8b07aSPierre Peiffer 	struct shmid_kernel *shp;
12563980c80SShailesh Pandey 
12601b8b07aSPierre Peiffer 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
12785b6d246SAlexander Mikhalitsyn 	WARN_ON(ns != shp->ns);
12801b8b07aSPierre Peiffer 
1294e982311SKirill Korotaev 	if (shp->shm_nattch) {
1304e982311SKirill Korotaev 		shp->shm_perm.mode |= SHM_DEST;
1314e982311SKirill Korotaev 		/* Do not find it any more */
1320cfb6aeeSGuillaume Knispel 		ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
1334e982311SKirill Korotaev 		shm_unlock(shp);
1344e982311SKirill Korotaev 	} else
1354e982311SKirill Korotaev 		shm_destroy(ns, shp);
1364e982311SKirill Korotaev }
1374e982311SKirill Korotaev 
138ae5e1b22SPavel Emelyanov #ifdef CONFIG_IPC_NS
shm_exit_ns(struct ipc_namespace * ns)1394e982311SKirill Korotaev void shm_exit_ns(struct ipc_namespace *ns)
1404e982311SKirill Korotaev {
14101b8b07aSPierre Peiffer 	free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
1427d6feeb2SSerge E. Hallyn 	idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
1430cfb6aeeSGuillaume Knispel 	rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
1444e982311SKirill Korotaev }
145ae5e1b22SPavel Emelyanov #endif
1461da177e4SLinus Torvalds 
ipc_ns_init(void)147140d0b21SLinus Torvalds static int __init ipc_ns_init(void)
1481da177e4SLinus Torvalds {
149eae04d25SDavidlohr Bueso 	shm_init_ns(&init_ipc_ns);
150eae04d25SDavidlohr Bueso 	return 0;
151140d0b21SLinus Torvalds }
152140d0b21SLinus Torvalds 
153140d0b21SLinus Torvalds pure_initcall(ipc_ns_init);
154140d0b21SLinus Torvalds 
shm_init(void)155140d0b21SLinus Torvalds void __init shm_init(void)
156140d0b21SLinus Torvalds {
15719b4946cSMike Waychison 	ipc_init_proc_interface("sysvipc/shm",
158b7952180SHelge Deller #if BITS_PER_LONG <= 32
159b7952180SHelge Deller 				"       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime        rss       swap\n",
160b7952180SHelge Deller #else
161b7952180SHelge Deller 				"       key      shmid perms                  size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime                   rss                  swap\n",
162b7952180SHelge Deller #endif
1634e982311SKirill Korotaev 				IPC_SHM_IDS, sysvipc_shm_proc_show);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
shm_obtain_object(struct ipc_namespace * ns,int id)1668b8d52acSDavidlohr Bueso static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
1678b8d52acSDavidlohr Bueso {
16855b7ae50SDavidlohr Bueso 	struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
1698b8d52acSDavidlohr Bueso 
1708b8d52acSDavidlohr Bueso 	if (IS_ERR(ipcp))
1718b8d52acSDavidlohr Bueso 		return ERR_CAST(ipcp);
1728b8d52acSDavidlohr Bueso 
1738b8d52acSDavidlohr Bueso 	return container_of(ipcp, struct shmid_kernel, shm_perm);
1748b8d52acSDavidlohr Bueso }
1758b8d52acSDavidlohr Bueso 
shm_obtain_object_check(struct ipc_namespace * ns,int id)1768b8d52acSDavidlohr Bueso static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
1778b8d52acSDavidlohr Bueso {
1788b8d52acSDavidlohr Bueso 	struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
1798b8d52acSDavidlohr Bueso 
1808b8d52acSDavidlohr Bueso 	if (IS_ERR(ipcp))
1818b8d52acSDavidlohr Bueso 		return ERR_CAST(ipcp);
1828b8d52acSDavidlohr Bueso 
1838b8d52acSDavidlohr Bueso 	return container_of(ipcp, struct shmid_kernel, shm_perm);
1848b8d52acSDavidlohr Bueso }
1858b8d52acSDavidlohr Bueso 
1863e148c79SNadia Derbey /*
187d9a605e4SDavidlohr Bueso  * shm_lock_(check_) routines are called in the paths where the rwsem
18800c2bf85SNadia Derbey  * is not necessarily held.
1893e148c79SNadia Derbey  */
shm_lock(struct ipc_namespace * ns,int id)190023a5355SNadia Derbey static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
1911da177e4SLinus Torvalds {
19282061c57SDavidlohr Bueso 	struct kern_ipc_perm *ipcp;
19303f02c76SNadia Derbey 
19482061c57SDavidlohr Bueso 	rcu_read_lock();
19582061c57SDavidlohr Bueso 	ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
19682061c57SDavidlohr Bueso 	if (IS_ERR(ipcp))
19782061c57SDavidlohr Bueso 		goto err;
19882061c57SDavidlohr Bueso 
19982061c57SDavidlohr Bueso 	ipc_lock_object(ipcp);
20082061c57SDavidlohr Bueso 	/*
20182061c57SDavidlohr Bueso 	 * ipc_rmid() may have already freed the ID while ipc_lock_object()
20282061c57SDavidlohr Bueso 	 * was spinning: here verify that the structure is still valid.
20382061c57SDavidlohr Bueso 	 * Upon races with RMID, return -EIDRM, thus indicating that
20482061c57SDavidlohr Bueso 	 * the ID points to a removed identifier.
20582061c57SDavidlohr Bueso 	 */
20682061c57SDavidlohr Bueso 	if (ipc_valid_object(ipcp)) {
20782061c57SDavidlohr Bueso 		/* return a locked ipc object upon success */
20882061c57SDavidlohr Bueso 		return container_of(ipcp, struct shmid_kernel, shm_perm);
20982061c57SDavidlohr Bueso 	}
21082061c57SDavidlohr Bueso 
21182061c57SDavidlohr Bueso 	ipc_unlock_object(ipcp);
2129c21dae2SDavidlohr Bueso 	ipcp = ERR_PTR(-EIDRM);
21382061c57SDavidlohr Bueso err:
21482061c57SDavidlohr Bueso 	rcu_read_unlock();
215c5c8975bSDavidlohr Bueso 	/*
2161ac0b6deSKirill A. Shutemov 	 * Callers of shm_lock() must validate the status of the returned ipc
21782061c57SDavidlohr Bueso 	 * object pointer and error out as appropriate.
218c5c8975bSDavidlohr Bueso 	 */
21959cf0a93SKees Cook 	return ERR_CAST(ipcp);
220023a5355SNadia Derbey }
221023a5355SNadia Derbey 
shm_lock_by_ptr(struct shmid_kernel * ipcp)2224c677e2eSVasiliy Kulikov static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
2234c677e2eSVasiliy Kulikov {
2244c677e2eSVasiliy Kulikov 	rcu_read_lock();
225cf9d5d78SDavidlohr Bueso 	ipc_lock_object(&ipcp->shm_perm);
2264c677e2eSVasiliy Kulikov }
2274c677e2eSVasiliy Kulikov 
shm_rcu_free(struct rcu_head * head)22853dad6d3SDavidlohr Bueso static void shm_rcu_free(struct rcu_head *head)
22953dad6d3SDavidlohr Bueso {
230dba4cdd3SManfred Spraul 	struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
231dba4cdd3SManfred Spraul 							rcu);
232dba4cdd3SManfred Spraul 	struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
233dba4cdd3SManfred Spraul 							shm_perm);
2347191adffSEric W. Biederman 	security_shm_free(&shp->shm_perm);
235bc8136a5SVasily Averin 	kfree(shp);
23653dad6d3SDavidlohr Bueso }
23753dad6d3SDavidlohr Bueso 
23885b6d246SAlexander Mikhalitsyn /*
23985b6d246SAlexander Mikhalitsyn  * It has to be called with shp locked.
24085b6d246SAlexander Mikhalitsyn  * It must be called before ipc_rmid()
24185b6d246SAlexander Mikhalitsyn  */
shm_clist_rm(struct shmid_kernel * shp)24285b6d246SAlexander Mikhalitsyn static inline void shm_clist_rm(struct shmid_kernel *shp)
2431da177e4SLinus Torvalds {
24485b6d246SAlexander Mikhalitsyn 	struct task_struct *creator;
24585b6d246SAlexander Mikhalitsyn 
24685b6d246SAlexander Mikhalitsyn 	/* ensure that shm_creator does not disappear */
24785b6d246SAlexander Mikhalitsyn 	rcu_read_lock();
24885b6d246SAlexander Mikhalitsyn 
24985b6d246SAlexander Mikhalitsyn 	/*
25085b6d246SAlexander Mikhalitsyn 	 * A concurrent exit_shm may do a list_del_init() as well.
25185b6d246SAlexander Mikhalitsyn 	 * Just do nothing if exit_shm already did the work
25285b6d246SAlexander Mikhalitsyn 	 */
25385b6d246SAlexander Mikhalitsyn 	if (!list_empty(&shp->shm_clist)) {
25485b6d246SAlexander Mikhalitsyn 		/*
25585b6d246SAlexander Mikhalitsyn 		 * shp->shm_creator is guaranteed to be valid *only*
25685b6d246SAlexander Mikhalitsyn 		 * if shp->shm_clist is not empty.
25785b6d246SAlexander Mikhalitsyn 		 */
25885b6d246SAlexander Mikhalitsyn 		creator = shp->shm_creator;
25985b6d246SAlexander Mikhalitsyn 
26085b6d246SAlexander Mikhalitsyn 		task_lock(creator);
26185b6d246SAlexander Mikhalitsyn 		/*
26285b6d246SAlexander Mikhalitsyn 		 * list_del_init() is a nop if the entry was already removed
26385b6d246SAlexander Mikhalitsyn 		 * from the list.
26485b6d246SAlexander Mikhalitsyn 		 */
26585b6d246SAlexander Mikhalitsyn 		list_del_init(&shp->shm_clist);
26685b6d246SAlexander Mikhalitsyn 		task_unlock(creator);
26785b6d246SAlexander Mikhalitsyn 	}
26885b6d246SAlexander Mikhalitsyn 	rcu_read_unlock();
26985b6d246SAlexander Mikhalitsyn }
27085b6d246SAlexander Mikhalitsyn 
shm_rmid(struct shmid_kernel * s)27185b6d246SAlexander Mikhalitsyn static inline void shm_rmid(struct shmid_kernel *s)
27285b6d246SAlexander Mikhalitsyn {
27385b6d246SAlexander Mikhalitsyn 	shm_clist_rm(s);
27485b6d246SAlexander Mikhalitsyn 	ipc_rmid(&shm_ids(s->ns), &s->shm_perm);
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 
__shm_open(struct shm_file_data * sfd)278b6305049SMike Kravetz static int __shm_open(struct shm_file_data *sfd)
2794e982311SKirill Korotaev {
2801da177e4SLinus Torvalds 	struct shmid_kernel *shp;
2811da177e4SLinus Torvalds 
282bc56bba8SEric W. Biederman 	shp = shm_lock(sfd->ns, sfd->id);
2831ac0b6deSKirill A. Shutemov 
2841ac0b6deSKirill A. Shutemov 	if (IS_ERR(shp))
2851ac0b6deSKirill A. Shutemov 		return PTR_ERR(shp);
2861ac0b6deSKirill A. Shutemov 
2873f05317dSEric Biggers 	if (shp->shm_file != sfd->file) {
2883f05317dSEric Biggers 		/* ID was reused */
2893f05317dSEric Biggers 		shm_unlock(shp);
2903f05317dSEric Biggers 		return -EINVAL;
2913f05317dSEric Biggers 	}
2923f05317dSEric Biggers 
2937ff2819eSDeepa Dinamani 	shp->shm_atim = ktime_get_real_seconds();
29498f929b1SEric W. Biederman 	ipc_update_pid(&shp->shm_lprid, task_tgid(current));
2951da177e4SLinus Torvalds 	shp->shm_nattch++;
2961da177e4SLinus Torvalds 	shm_unlock(shp);
2971ac0b6deSKirill A. Shutemov 	return 0;
2981ac0b6deSKirill A. Shutemov }
2991ac0b6deSKirill A. Shutemov 
3001ac0b6deSKirill A. Shutemov /* This is called by fork, once for every shm attach. */
shm_open(struct vm_area_struct * vma)3011ac0b6deSKirill A. Shutemov static void shm_open(struct vm_area_struct *vma)
3021ac0b6deSKirill A. Shutemov {
303b6305049SMike Kravetz 	struct file *file = vma->vm_file;
304b6305049SMike Kravetz 	struct shm_file_data *sfd = shm_file_data(file);
305b6305049SMike Kravetz 	int err;
306b6305049SMike Kravetz 
307b6305049SMike Kravetz 	/* Always call underlying open if present */
308b6305049SMike Kravetz 	if (sfd->vm_ops->open)
309b6305049SMike Kravetz 		sfd->vm_ops->open(vma);
310b6305049SMike Kravetz 
311b6305049SMike Kravetz 	err = __shm_open(sfd);
3121ac0b6deSKirill A. Shutemov 	/*
3131ac0b6deSKirill A. Shutemov 	 * We raced in the idr lookup or with shm_destroy().
3141ac0b6deSKirill A. Shutemov 	 * Either way, the ID is busted.
3151ac0b6deSKirill A. Shutemov 	 */
3161ac0b6deSKirill A. Shutemov 	WARN_ON_ONCE(err);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
3201da177e4SLinus Torvalds  * shm_destroy - free the struct shmid_kernel
3211da177e4SLinus Torvalds  *
322f4566f04SNadia Derbey  * @ns: namespace
3231da177e4SLinus Torvalds  * @shp: struct to free
3241da177e4SLinus Torvalds  *
325d9a605e4SDavidlohr Bueso  * It has to be called with shp and shm_ids.rwsem (writer) locked,
3261da177e4SLinus Torvalds  * but returns with shp unlocked and freed.
3271da177e4SLinus Torvalds  */
shm_destroy(struct ipc_namespace * ns,struct shmid_kernel * shp)3284e982311SKirill Korotaev static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
3291da177e4SLinus Torvalds {
330a399b29dSGreg Thelen 	struct file *shm_file;
331a399b29dSGreg Thelen 
332a399b29dSGreg Thelen 	shm_file = shp->shm_file;
333a399b29dSGreg Thelen 	shp->shm_file = NULL;
3344e982311SKirill Korotaev 	ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
33585b6d246SAlexander Mikhalitsyn 	shm_rmid(shp);
3361da177e4SLinus Torvalds 	shm_unlock(shp);
337a399b29dSGreg Thelen 	if (!is_file_hugepages(shm_file))
338d7c9e99aSAlexey Gladkov 		shmem_lock(shm_file, 0, shp->mlock_ucounts);
339a399b29dSGreg Thelen 	fput(shm_file);
34098f929b1SEric W. Biederman 	ipc_update_pid(&shp->shm_cprid, NULL);
34198f929b1SEric W. Biederman 	ipc_update_pid(&shp->shm_lprid, NULL);
342dba4cdd3SManfred Spraul 	ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*
346b34a6b1dSVasiliy Kulikov  * shm_may_destroy - identifies whether shm segment should be destroyed now
347b34a6b1dSVasiliy Kulikov  *
348b34a6b1dSVasiliy Kulikov  * Returns true if and only if there are no active users of the segment and
349b34a6b1dSVasiliy Kulikov  * one of the following is true:
350b34a6b1dSVasiliy Kulikov  *
351b34a6b1dSVasiliy Kulikov  * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
352b34a6b1dSVasiliy Kulikov  *
353b34a6b1dSVasiliy Kulikov  * 2) sysctl kernel.shm_rmid_forced is set to 1.
354b34a6b1dSVasiliy Kulikov  */
shm_may_destroy(struct shmid_kernel * shp)35585b6d246SAlexander Mikhalitsyn static bool shm_may_destroy(struct shmid_kernel *shp)
356b34a6b1dSVasiliy Kulikov {
357b34a6b1dSVasiliy Kulikov 	return (shp->shm_nattch == 0) &&
35885b6d246SAlexander Mikhalitsyn 	       (shp->ns->shm_rmid_forced ||
359b34a6b1dSVasiliy Kulikov 		(shp->shm_perm.mode & SHM_DEST));
360b34a6b1dSVasiliy Kulikov }
361b34a6b1dSVasiliy Kulikov 
362b34a6b1dSVasiliy Kulikov /*
363bc56bba8SEric W. Biederman  * remove the attach descriptor vma.
3641da177e4SLinus Torvalds  * free memory for segment if it is marked destroyed.
3651da177e4SLinus Torvalds  * The descriptor has already been removed from the current->mm->mmap list
3661da177e4SLinus Torvalds  * and will later be kfree()d.
3671da177e4SLinus Torvalds  */
__shm_close(struct shm_file_data * sfd)368b6305049SMike Kravetz static void __shm_close(struct shm_file_data *sfd)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct shmid_kernel *shp;
371bc56bba8SEric W. Biederman 	struct ipc_namespace *ns = sfd->ns;
3724e982311SKirill Korotaev 
373d9a605e4SDavidlohr Bueso 	down_write(&shm_ids(ns).rwsem);
3741da177e4SLinus Torvalds 	/* remove from the list of attaches of the shm segment */
37500c2bf85SNadia Derbey 	shp = shm_lock(ns, sfd->id);
3761ac0b6deSKirill A. Shutemov 
3771ac0b6deSKirill A. Shutemov 	/*
3781ac0b6deSKirill A. Shutemov 	 * We raced in the idr lookup or with shm_destroy().
3791ac0b6deSKirill A. Shutemov 	 * Either way, the ID is busted.
3801ac0b6deSKirill A. Shutemov 	 */
3811ac0b6deSKirill A. Shutemov 	if (WARN_ON_ONCE(IS_ERR(shp)))
3821ac0b6deSKirill A. Shutemov 		goto done; /* no-op */
3831ac0b6deSKirill A. Shutemov 
38498f929b1SEric W. Biederman 	ipc_update_pid(&shp->shm_lprid, task_tgid(current));
3857ff2819eSDeepa Dinamani 	shp->shm_dtim = ktime_get_real_seconds();
3861da177e4SLinus Torvalds 	shp->shm_nattch--;
38785b6d246SAlexander Mikhalitsyn 	if (shm_may_destroy(shp))
3884e982311SKirill Korotaev 		shm_destroy(ns, shp);
3891da177e4SLinus Torvalds 	else
3901da177e4SLinus Torvalds 		shm_unlock(shp);
3911ac0b6deSKirill A. Shutemov done:
392d9a605e4SDavidlohr Bueso 	up_write(&shm_ids(ns).rwsem);
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
shm_close(struct vm_area_struct * vma)395b6305049SMike Kravetz static void shm_close(struct vm_area_struct *vma)
396b6305049SMike Kravetz {
397b6305049SMike Kravetz 	struct file *file = vma->vm_file;
398b6305049SMike Kravetz 	struct shm_file_data *sfd = shm_file_data(file);
399b6305049SMike Kravetz 
400b6305049SMike Kravetz 	/* Always call underlying close if present */
401b6305049SMike Kravetz 	if (sfd->vm_ops->close)
402b6305049SMike Kravetz 		sfd->vm_ops->close(vma);
403b6305049SMike Kravetz 
404b6305049SMike Kravetz 	__shm_close(sfd);
405b6305049SMike Kravetz }
406b6305049SMike Kravetz 
407d9a605e4SDavidlohr Bueso /* Called with ns->shm_ids(ns).rwsem locked */
shm_try_destroy_orphaned(int id,void * p,void * data)408b34a6b1dSVasiliy Kulikov static int shm_try_destroy_orphaned(int id, void *p, void *data)
409b34a6b1dSVasiliy Kulikov {
410b34a6b1dSVasiliy Kulikov 	struct ipc_namespace *ns = data;
4114c677e2eSVasiliy Kulikov 	struct kern_ipc_perm *ipcp = p;
4124c677e2eSVasiliy Kulikov 	struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
413b34a6b1dSVasiliy Kulikov 
414b34a6b1dSVasiliy Kulikov 	/*
415b34a6b1dSVasiliy Kulikov 	 * We want to destroy segments without users and with already
416b34a6b1dSVasiliy Kulikov 	 * exit'ed originating process.
4174c677e2eSVasiliy Kulikov 	 *
418d9a605e4SDavidlohr Bueso 	 * As shp->* are changed under rwsem, it's safe to skip shp locking.
419b34a6b1dSVasiliy Kulikov 	 */
42085b6d246SAlexander Mikhalitsyn 	if (!list_empty(&shp->shm_clist))
421b34a6b1dSVasiliy Kulikov 		return 0;
422b34a6b1dSVasiliy Kulikov 
42385b6d246SAlexander Mikhalitsyn 	if (shm_may_destroy(shp)) {
4244c677e2eSVasiliy Kulikov 		shm_lock_by_ptr(shp);
425b34a6b1dSVasiliy Kulikov 		shm_destroy(ns, shp);
4264c677e2eSVasiliy Kulikov 	}
427b34a6b1dSVasiliy Kulikov 	return 0;
428b34a6b1dSVasiliy Kulikov }
429b34a6b1dSVasiliy Kulikov 
shm_destroy_orphaned(struct ipc_namespace * ns)430b34a6b1dSVasiliy Kulikov void shm_destroy_orphaned(struct ipc_namespace *ns)
431b34a6b1dSVasiliy Kulikov {
432d9a605e4SDavidlohr Bueso 	down_write(&shm_ids(ns).rwsem);
43333a30ed4SVasiliy Kulikov 	if (shm_ids(ns).in_use)
434b34a6b1dSVasiliy Kulikov 		idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
435d9a605e4SDavidlohr Bueso 	up_write(&shm_ids(ns).rwsem);
436b34a6b1dSVasiliy Kulikov }
437b34a6b1dSVasiliy Kulikov 
43883293c0fSJack Miller /* Locking assumes this will only be called with task == current */
exit_shm(struct task_struct * task)439b34a6b1dSVasiliy Kulikov void exit_shm(struct task_struct *task)
440b34a6b1dSVasiliy Kulikov {
44185b6d246SAlexander Mikhalitsyn 	for (;;) {
44285b6d246SAlexander Mikhalitsyn 		struct shmid_kernel *shp;
44385b6d246SAlexander Mikhalitsyn 		struct ipc_namespace *ns;
444b34a6b1dSVasiliy Kulikov 
44585b6d246SAlexander Mikhalitsyn 		task_lock(task);
44685b6d246SAlexander Mikhalitsyn 
44785b6d246SAlexander Mikhalitsyn 		if (list_empty(&task->sysvshm.shm_clist)) {
44885b6d246SAlexander Mikhalitsyn 			task_unlock(task);
44985b6d246SAlexander Mikhalitsyn 			break;
45085b6d246SAlexander Mikhalitsyn 		}
45185b6d246SAlexander Mikhalitsyn 
45285b6d246SAlexander Mikhalitsyn 		shp = list_first_entry(&task->sysvshm.shm_clist, struct shmid_kernel,
45385b6d246SAlexander Mikhalitsyn 				shm_clist);
454298507d4SVasiliy Kulikov 
45583293c0fSJack Miller 		/*
45685b6d246SAlexander Mikhalitsyn 		 * 1) Get pointer to the ipc namespace. It is worth to say
45785b6d246SAlexander Mikhalitsyn 		 * that this pointer is guaranteed to be valid because
45885b6d246SAlexander Mikhalitsyn 		 * shp lifetime is always shorter than namespace lifetime
45985b6d246SAlexander Mikhalitsyn 		 * in which shp lives.
46085b6d246SAlexander Mikhalitsyn 		 * We taken task_lock it means that shp won't be freed.
46185b6d246SAlexander Mikhalitsyn 		 */
46285b6d246SAlexander Mikhalitsyn 		ns = shp->ns;
46385b6d246SAlexander Mikhalitsyn 
46485b6d246SAlexander Mikhalitsyn 		/*
46585b6d246SAlexander Mikhalitsyn 		 * 2) If kernel.shm_rmid_forced is not set then only keep track of
46683293c0fSJack Miller 		 * which shmids are orphaned, so that a later set of the sysctl
46783293c0fSJack Miller 		 * can clean them up.
46883293c0fSJack Miller 		 */
46985b6d246SAlexander Mikhalitsyn 		if (!ns->shm_rmid_forced)
47085b6d246SAlexander Mikhalitsyn 			goto unlink_continue;
47185b6d246SAlexander Mikhalitsyn 
47283293c0fSJack Miller 		/*
47385b6d246SAlexander Mikhalitsyn 		 * 3) get a reference to the namespace.
47485b6d246SAlexander Mikhalitsyn 		 *    The refcount could be already 0. If it is 0, then
47585b6d246SAlexander Mikhalitsyn 		 *    the shm objects will be free by free_ipc_work().
47683293c0fSJack Miller 		 */
47785b6d246SAlexander Mikhalitsyn 		ns = get_ipc_ns_not_zero(ns);
47885b6d246SAlexander Mikhalitsyn 		if (!ns) {
47985b6d246SAlexander Mikhalitsyn unlink_continue:
48085b6d246SAlexander Mikhalitsyn 			list_del_init(&shp->shm_clist);
48185b6d246SAlexander Mikhalitsyn 			task_unlock(task);
48285b6d246SAlexander Mikhalitsyn 			continue;
48383293c0fSJack Miller 		}
48483293c0fSJack Miller 
48583293c0fSJack Miller 		/*
48685b6d246SAlexander Mikhalitsyn 		 * 4) get a reference to shp.
48785b6d246SAlexander Mikhalitsyn 		 *   This cannot fail: shm_clist_rm() is called before
48885b6d246SAlexander Mikhalitsyn 		 *   ipc_rmid(), thus the refcount cannot be 0.
48985b6d246SAlexander Mikhalitsyn 		 */
49085b6d246SAlexander Mikhalitsyn 		WARN_ON(!ipc_rcu_getref(&shp->shm_perm));
49185b6d246SAlexander Mikhalitsyn 
49285b6d246SAlexander Mikhalitsyn 		/*
49385b6d246SAlexander Mikhalitsyn 		 * 5) unlink the shm segment from the list of segments
49485b6d246SAlexander Mikhalitsyn 		 *    created by current.
49585b6d246SAlexander Mikhalitsyn 		 *    This must be done last. After unlinking,
49685b6d246SAlexander Mikhalitsyn 		 *    only the refcounts obtained above prevent IPC_RMID
49785b6d246SAlexander Mikhalitsyn 		 *    from destroying the segment or the namespace.
49885b6d246SAlexander Mikhalitsyn 		 */
49985b6d246SAlexander Mikhalitsyn 		list_del_init(&shp->shm_clist);
50085b6d246SAlexander Mikhalitsyn 
50185b6d246SAlexander Mikhalitsyn 		task_unlock(task);
50285b6d246SAlexander Mikhalitsyn 
50385b6d246SAlexander Mikhalitsyn 		/*
50485b6d246SAlexander Mikhalitsyn 		 * 6) we have all references
50585b6d246SAlexander Mikhalitsyn 		 *    Thus lock & if needed destroy shp.
50683293c0fSJack Miller 		 */
507d9a605e4SDavidlohr Bueso 		down_write(&shm_ids(ns).rwsem);
50883293c0fSJack Miller 		shm_lock_by_ptr(shp);
50985b6d246SAlexander Mikhalitsyn 		/*
51085b6d246SAlexander Mikhalitsyn 		 * rcu_read_lock was implicitly taken in shm_lock_by_ptr, it's
51185b6d246SAlexander Mikhalitsyn 		 * safe to call ipc_rcu_putref here
51285b6d246SAlexander Mikhalitsyn 		 */
51385b6d246SAlexander Mikhalitsyn 		ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
51485b6d246SAlexander Mikhalitsyn 
51585b6d246SAlexander Mikhalitsyn 		if (ipc_valid_object(&shp->shm_perm)) {
51685b6d246SAlexander Mikhalitsyn 			if (shm_may_destroy(shp))
51783293c0fSJack Miller 				shm_destroy(ns, shp);
51885b6d246SAlexander Mikhalitsyn 			else
51985b6d246SAlexander Mikhalitsyn 				shm_unlock(shp);
52085b6d246SAlexander Mikhalitsyn 		} else {
52185b6d246SAlexander Mikhalitsyn 			/*
52285b6d246SAlexander Mikhalitsyn 			 * Someone else deleted the shp from namespace
52385b6d246SAlexander Mikhalitsyn 			 * idr/kht while we have waited.
52485b6d246SAlexander Mikhalitsyn 			 * Just unlock and continue.
52585b6d246SAlexander Mikhalitsyn 			 */
52685b6d246SAlexander Mikhalitsyn 			shm_unlock(shp);
52783293c0fSJack Miller 		}
52883293c0fSJack Miller 
529d9a605e4SDavidlohr Bueso 		up_write(&shm_ids(ns).rwsem);
53085b6d246SAlexander Mikhalitsyn 		put_ipc_ns(ns); /* paired with get_ipc_ns_not_zero */
53185b6d246SAlexander Mikhalitsyn 	}
532b34a6b1dSVasiliy Kulikov }
533b34a6b1dSVasiliy Kulikov 
shm_fault(struct vm_fault * vmf)53414f28f57SSouptick Joarder static vm_fault_t shm_fault(struct vm_fault *vmf)
535bc56bba8SEric W. Biederman {
53611bac800SDave Jiang 	struct file *file = vmf->vma->vm_file;
537bc56bba8SEric W. Biederman 	struct shm_file_data *sfd = shm_file_data(file);
538bc56bba8SEric W. Biederman 
53911bac800SDave Jiang 	return sfd->vm_ops->fault(vmf);
540bc56bba8SEric W. Biederman }
541bc56bba8SEric W. Biederman 
shm_may_split(struct vm_area_struct * vma,unsigned long addr)542dd3b614fSDmitry Safonov static int shm_may_split(struct vm_area_struct *vma, unsigned long addr)
5433d942ee0SMike Kravetz {
5443d942ee0SMike Kravetz 	struct file *file = vma->vm_file;
5453d942ee0SMike Kravetz 	struct shm_file_data *sfd = shm_file_data(file);
5463d942ee0SMike Kravetz 
547dd3b614fSDmitry Safonov 	if (sfd->vm_ops->may_split)
548dd3b614fSDmitry Safonov 		return sfd->vm_ops->may_split(vma, addr);
5493d942ee0SMike Kravetz 
5503d942ee0SMike Kravetz 	return 0;
5513d942ee0SMike Kravetz }
5523d942ee0SMike Kravetz 
shm_pagesize(struct vm_area_struct * vma)553eec3636aSJane Chu static unsigned long shm_pagesize(struct vm_area_struct *vma)
554eec3636aSJane Chu {
555eec3636aSJane Chu 	struct file *file = vma->vm_file;
556eec3636aSJane Chu 	struct shm_file_data *sfd = shm_file_data(file);
557eec3636aSJane Chu 
558eec3636aSJane Chu 	if (sfd->vm_ops->pagesize)
559eec3636aSJane Chu 		return sfd->vm_ops->pagesize(vma);
560eec3636aSJane Chu 
561eec3636aSJane Chu 	return PAGE_SIZE;
562eec3636aSJane Chu }
563eec3636aSJane Chu 
564bc56bba8SEric W. Biederman #ifdef CONFIG_NUMA
shm_set_policy(struct vm_area_struct * vma,struct mempolicy * new)565d823e3e7SAdrian Bunk static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
566bc56bba8SEric W. Biederman {
567bc56bba8SEric W. Biederman 	struct file *file = vma->vm_file;
568bc56bba8SEric W. Biederman 	struct shm_file_data *sfd = shm_file_data(file);
569bc56bba8SEric W. Biederman 	int err = 0;
57063980c80SShailesh Pandey 
571bc56bba8SEric W. Biederman 	if (sfd->vm_ops->set_policy)
572bc56bba8SEric W. Biederman 		err = sfd->vm_ops->set_policy(vma, new);
573bc56bba8SEric W. Biederman 	return err;
574bc56bba8SEric W. Biederman }
575bc56bba8SEric W. Biederman 
shm_get_policy(struct vm_area_struct * vma,unsigned long addr)576d823e3e7SAdrian Bunk static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
577d823e3e7SAdrian Bunk 					unsigned long addr)
578bc56bba8SEric W. Biederman {
579bc56bba8SEric W. Biederman 	struct file *file = vma->vm_file;
580bc56bba8SEric W. Biederman 	struct shm_file_data *sfd = shm_file_data(file);
581bc56bba8SEric W. Biederman 	struct mempolicy *pol = NULL;
582bc56bba8SEric W. Biederman 
583bc56bba8SEric W. Biederman 	if (sfd->vm_ops->get_policy)
584bc56bba8SEric W. Biederman 		pol = sfd->vm_ops->get_policy(vma, addr);
58552cd3b07SLee Schermerhorn 	else if (vma->vm_policy)
586bc56bba8SEric W. Biederman 		pol = vma->vm_policy;
58752cd3b07SLee Schermerhorn 
588bc56bba8SEric W. Biederman 	return pol;
589bc56bba8SEric W. Biederman }
590bc56bba8SEric W. Biederman #endif
591bc56bba8SEric W. Biederman 
shm_mmap(struct file * file,struct vm_area_struct * vma)5921da177e4SLinus Torvalds static int shm_mmap(struct file *file, struct vm_area_struct *vma)
5931da177e4SLinus Torvalds {
594bc56bba8SEric W. Biederman 	struct shm_file_data *sfd = shm_file_data(file);
595b0e15190SDavid Howells 	int ret;
596b0e15190SDavid Howells 
5971ac0b6deSKirill A. Shutemov 	/*
5983f05317dSEric Biggers 	 * In case of remap_file_pages() emulation, the file can represent an
5993f05317dSEric Biggers 	 * IPC ID that was removed, and possibly even reused by another shm
6003f05317dSEric Biggers 	 * segment already.  Propagate this case as an error to caller.
6011ac0b6deSKirill A. Shutemov 	 */
602b6305049SMike Kravetz 	ret = __shm_open(sfd);
6031ac0b6deSKirill A. Shutemov 	if (ret)
604bc56bba8SEric W. Biederman 		return ret;
6051ac0b6deSKirill A. Shutemov 
606f74ac015SMiklos Szeredi 	ret = call_mmap(sfd->file, vma);
6071ac0b6deSKirill A. Shutemov 	if (ret) {
608b6305049SMike Kravetz 		__shm_close(sfd);
6091ac0b6deSKirill A. Shutemov 		return ret;
6101ac0b6deSKirill A. Shutemov 	}
611bc56bba8SEric W. Biederman 	sfd->vm_ops = vma->vm_ops;
6122e92a3baSDavid Howells #ifdef CONFIG_MMU
613d0edd852SDavidlohr Bueso 	WARN_ON(!sfd->vm_ops->fault);
6142e92a3baSDavid Howells #endif
6151da177e4SLinus Torvalds 	vma->vm_ops = &shm_vm_ops;
6161ac0b6deSKirill A. Shutemov 	return 0;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
shm_release(struct inode * ino,struct file * file)6194e982311SKirill Korotaev static int shm_release(struct inode *ino, struct file *file)
6204e982311SKirill Korotaev {
621bc56bba8SEric W. Biederman 	struct shm_file_data *sfd = shm_file_data(file);
6224e982311SKirill Korotaev 
623bc56bba8SEric W. Biederman 	put_ipc_ns(sfd->ns);
6243f05317dSEric Biggers 	fput(sfd->file);
625bc56bba8SEric W. Biederman 	shm_file_data(file) = NULL;
626bc56bba8SEric W. Biederman 	kfree(sfd);
6274e982311SKirill Korotaev 	return 0;
6284e982311SKirill Korotaev }
6294e982311SKirill Korotaev 
shm_fsync(struct file * file,loff_t start,loff_t end,int datasync)63002c24a82SJosef Bacik static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
631516dffdcSAdam Litke {
632516dffdcSAdam Litke 	struct shm_file_data *sfd = shm_file_data(file);
633516dffdcSAdam Litke 
6347ea80859SChristoph Hellwig 	if (!sfd->file->f_op->fsync)
6357ea80859SChristoph Hellwig 		return -EINVAL;
6360f41074aSJeff Layton 	return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
637516dffdcSAdam Litke }
638516dffdcSAdam Litke 
shm_fallocate(struct file * file,int mode,loff_t offset,loff_t len)6397d8a4569SWill Deacon static long shm_fallocate(struct file *file, int mode, loff_t offset,
6407d8a4569SWill Deacon 			  loff_t len)
6417d8a4569SWill Deacon {
6427d8a4569SWill Deacon 	struct shm_file_data *sfd = shm_file_data(file);
6437d8a4569SWill Deacon 
6447d8a4569SWill Deacon 	if (!sfd->file->f_op->fallocate)
6457d8a4569SWill Deacon 		return -EOPNOTSUPP;
6467d8a4569SWill Deacon 	return sfd->file->f_op->fallocate(file, mode, offset, len);
6477d8a4569SWill Deacon }
6487d8a4569SWill Deacon 
shm_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)649bc56bba8SEric W. Biederman static unsigned long shm_get_unmapped_area(struct file *file,
650bc56bba8SEric W. Biederman 	unsigned long addr, unsigned long len, unsigned long pgoff,
651bc56bba8SEric W. Biederman 	unsigned long flags)
652bc56bba8SEric W. Biederman {
653bc56bba8SEric W. Biederman 	struct shm_file_data *sfd = shm_file_data(file);
65463980c80SShailesh Pandey 
655c4caa778SAl Viro 	return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
656c4caa778SAl Viro 						pgoff, flags);
657516dffdcSAdam Litke }
658bc56bba8SEric W. Biederman 
6599a32144eSArjan van de Ven static const struct file_operations shm_file_operations = {
660b0e15190SDavid Howells 	.mmap		= shm_mmap,
661516dffdcSAdam Litke 	.fsync		= shm_fsync,
6624e982311SKirill Korotaev 	.release	= shm_release,
663ed5e5894SDavid Howells 	.get_unmapped_area	= shm_get_unmapped_area,
6646038f373SArnd Bergmann 	.llseek		= noop_llseek,
6657d8a4569SWill Deacon 	.fallocate	= shm_fallocate,
666c4caa778SAl Viro };
667c4caa778SAl Viro 
668c01d5b30SHugh Dickins /*
669c01d5b30SHugh Dickins  * shm_file_operations_huge is now identical to shm_file_operations,
670c01d5b30SHugh Dickins  * but we keep it distinct for the sake of is_file_shm_hugepages().
671c01d5b30SHugh Dickins  */
672c4caa778SAl Viro static const struct file_operations shm_file_operations_huge = {
673c4caa778SAl Viro 	.mmap		= shm_mmap,
674c4caa778SAl Viro 	.fsync		= shm_fsync,
675c4caa778SAl Viro 	.release	= shm_release,
676bc56bba8SEric W. Biederman 	.get_unmapped_area	= shm_get_unmapped_area,
6776038f373SArnd Bergmann 	.llseek		= noop_llseek,
6787d8a4569SWill Deacon 	.fallocate	= shm_fallocate,
6791da177e4SLinus Torvalds };
6801da177e4SLinus Torvalds 
is_file_shm_hugepages(struct file * file)6812954e440SYaowei Bai bool is_file_shm_hugepages(struct file *file)
682c4caa778SAl Viro {
683c4caa778SAl Viro 	return file->f_op == &shm_file_operations_huge;
684c4caa778SAl Viro }
685c4caa778SAl Viro 
686f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shm_vm_ops = {
6871da177e4SLinus Torvalds 	.open	= shm_open,	/* callback for a new vm-area open */
6881da177e4SLinus Torvalds 	.close	= shm_close,	/* callback for when the vm-area is released */
68954cb8821SNick Piggin 	.fault	= shm_fault,
690dd3b614fSDmitry Safonov 	.may_split = shm_may_split,
691eec3636aSJane Chu 	.pagesize = shm_pagesize,
692bc56bba8SEric W. Biederman #if defined(CONFIG_NUMA)
693bc56bba8SEric W. Biederman 	.set_policy = shm_set_policy,
694bc56bba8SEric W. Biederman 	.get_policy = shm_get_policy,
6951da177e4SLinus Torvalds #endif
6961da177e4SLinus Torvalds };
6971da177e4SLinus Torvalds 
698f4566f04SNadia Derbey /**
699f4566f04SNadia Derbey  * newseg - Create a new shared memory segment
700f4566f04SNadia Derbey  * @ns: namespace
701f4566f04SNadia Derbey  * @params: ptr to the structure that contains key, size and shmflg
702f4566f04SNadia Derbey  *
703d9a605e4SDavidlohr Bueso  * Called with shm_ids.rwsem held as a writer.
704f4566f04SNadia Derbey  */
newseg(struct ipc_namespace * ns,struct ipc_params * params)7057748dbfaSNadia Derbey static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
7061da177e4SLinus Torvalds {
7077748dbfaSNadia Derbey 	key_t key = params->key;
7087748dbfaSNadia Derbey 	int shmflg = params->flg;
7097748dbfaSNadia Derbey 	size_t size = params->u.size;
7101da177e4SLinus Torvalds 	int error;
7111da177e4SLinus Torvalds 	struct shmid_kernel *shp;
712d69f3badSRobin Holt 	size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
7131da177e4SLinus Torvalds 	struct file *file;
7141da177e4SLinus Torvalds 	char name[13];
715ca16d140SKOSAKI Motohiro 	vm_flags_t acctflag = 0;
7161da177e4SLinus Torvalds 
7174e982311SKirill Korotaev 	if (size < SHMMIN || size > ns->shm_ctlmax)
7181da177e4SLinus Torvalds 		return -EINVAL;
7191da177e4SLinus Torvalds 
7201376327cSManfred Spraul 	if (numpages << PAGE_SHIFT < size)
7211376327cSManfred Spraul 		return -ENOSPC;
7221376327cSManfred Spraul 
72309c6eb1fSManfred Spraul 	if (ns->shm_tot + numpages < ns->shm_tot ||
72409c6eb1fSManfred Spraul 			ns->shm_tot + numpages > ns->shm_ctlall)
7251da177e4SLinus Torvalds 		return -ENOSPC;
7261da177e4SLinus Torvalds 
72718319498SVasily Averin 	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
72842e618f7SKees Cook 	if (unlikely(!shp))
7291da177e4SLinus Torvalds 		return -ENOMEM;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	shp->shm_perm.key = key;
732b33291c0SAndrew Morton 	shp->shm_perm.mode = (shmflg & S_IRWXUGO);
733d7c9e99aSAlexey Gladkov 	shp->mlock_ucounts = NULL;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	shp->shm_perm.security = NULL;
7367191adffSEric W. Biederman 	error = security_shm_alloc(&shp->shm_perm);
7371da177e4SLinus Torvalds 	if (error) {
738bc8136a5SVasily Averin 		kfree(shp);
7391da177e4SLinus Torvalds 		return error;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
7429d66586fSEric W. Biederman 	sprintf(name, "SYSV%08x", key);
7431da177e4SLinus Torvalds 	if (shmflg & SHM_HUGETLB) {
744c103a4dcSAndrew Morton 		struct hstate *hs;
745091d0d55SLi Zefan 		size_t hugesize;
746091d0d55SLi Zefan 
747c103a4dcSAndrew Morton 		hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
748091d0d55SLi Zefan 		if (!hs) {
749091d0d55SLi Zefan 			error = -EINVAL;
750091d0d55SLi Zefan 			goto no_file;
751091d0d55SLi Zefan 		}
752091d0d55SLi Zefan 		hugesize = ALIGN(size, huge_page_size(hs));
753af73e4d9SNaoya Horiguchi 
7545a6fe125SMel Gorman 		/* hugetlb_file_setup applies strict accounting */
7555a6fe125SMel Gorman 		if (shmflg & SHM_NORESERVE)
7565a6fe125SMel Gorman 			acctflag = VM_NORESERVE;
757af73e4d9SNaoya Horiguchi 		file = hugetlb_file_setup(name, hugesize, acctflag,
75883c1fd76Szhangyiru 				HUGETLB_SHMFS_INODE, (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
7591da177e4SLinus Torvalds 	} else {
760bf8f972dSBadari Pulavarty 		/*
761bf8f972dSBadari Pulavarty 		 * Do not allow no accounting for OVERCOMMIT_NEVER, even
762bf8f972dSBadari Pulavarty 		 * if it's asked for.
763bf8f972dSBadari Pulavarty 		 */
764bf8f972dSBadari Pulavarty 		if  ((shmflg & SHM_NORESERVE) &&
765bf8f972dSBadari Pulavarty 				sysctl_overcommit_memory != OVERCOMMIT_NEVER)
766fc8744adSLinus Torvalds 			acctflag = VM_NORESERVE;
767e1832f29SStephen Smalley 		file = shmem_kernel_file_setup(name, size, acctflag);
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	error = PTR_ERR(file);
7701da177e4SLinus Torvalds 	if (IS_ERR(file))
7711da177e4SLinus Torvalds 		goto no_file;
7721da177e4SLinus Torvalds 
77398f929b1SEric W. Biederman 	shp->shm_cprid = get_pid(task_tgid(current));
77498f929b1SEric W. Biederman 	shp->shm_lprid = NULL;
7751da177e4SLinus Torvalds 	shp->shm_atim = shp->shm_dtim = 0;
7767ff2819eSDeepa Dinamani 	shp->shm_ctim = ktime_get_real_seconds();
7771da177e4SLinus Torvalds 	shp->shm_segsz = size;
7781da177e4SLinus Torvalds 	shp->shm_nattch = 0;
7791da177e4SLinus Torvalds 	shp->shm_file = file;
7805774ed01SVasiliy Kulikov 	shp->shm_creator = current;
781b9a53227SLinus Torvalds 
78239c96a1bSDavidlohr Bueso 	/* ipc_addid() locks shp upon success. */
783a2642f87SManfred Spraul 	error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
784a2642f87SManfred Spraul 	if (error < 0)
785b9a53227SLinus Torvalds 		goto no_id;
786b9a53227SLinus Torvalds 
78785b6d246SAlexander Mikhalitsyn 	shp->ns = ns;
78885b6d246SAlexander Mikhalitsyn 
78985b6d246SAlexander Mikhalitsyn 	task_lock(current);
790ab602f79SJack Miller 	list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
79185b6d246SAlexander Mikhalitsyn 	task_unlock(current);
792dbfcd91fSDavidlohr Bueso 
79330475cc1SBadari Pulavarty 	/*
79430475cc1SBadari Pulavarty 	 * shmid gets reported as "inode#" in /proc/pid/maps.
79530475cc1SBadari Pulavarty 	 * proc-ps tools use this. Changing this will break them.
79630475cc1SBadari Pulavarty 	 */
797496ad9aaSAl Viro 	file_inode(file)->i_ino = shp->shm_perm.id;
798551110a9SKrishnakumar R 
7994e982311SKirill Korotaev 	ns->shm_tot += numpages;
8007ca7e564SNadia Derbey 	error = shp->shm_perm.id;
801dbfcd91fSDavidlohr Bueso 
802cf9d5d78SDavidlohr Bueso 	ipc_unlock_object(&shp->shm_perm);
803dbfcd91fSDavidlohr Bueso 	rcu_read_unlock();
8047ca7e564SNadia Derbey 	return error;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds no_id:
8072236d4d3SEric W. Biederman 	ipc_update_pid(&shp->shm_cprid, NULL);
8082236d4d3SEric W. Biederman 	ipc_update_pid(&shp->shm_lprid, NULL);
8091da177e4SLinus Torvalds 	fput(file);
81039cfffd7SManfred Spraul 	ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
81139cfffd7SManfred Spraul 	return error;
8121da177e4SLinus Torvalds no_file:
813a2642f87SManfred Spraul 	call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
8141da177e4SLinus Torvalds 	return error;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
817f4566f04SNadia Derbey /*
818d9a605e4SDavidlohr Bueso  * Called with shm_ids.rwsem and ipcp locked.
819f4566f04SNadia Derbey  */
shm_more_checks(struct kern_ipc_perm * ipcp,struct ipc_params * params)82000898e85SAlexey Dobriyan static int shm_more_checks(struct kern_ipc_perm *ipcp, struct ipc_params *params)
8217748dbfaSNadia Derbey {
82203f02c76SNadia Derbey 	struct shmid_kernel *shp;
82303f02c76SNadia Derbey 
82403f02c76SNadia Derbey 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
82503f02c76SNadia Derbey 	if (shp->shm_segsz < params->u.size)
8267748dbfaSNadia Derbey 		return -EINVAL;
8277748dbfaSNadia Derbey 
8287748dbfaSNadia Derbey 	return 0;
8297748dbfaSNadia Derbey }
8307748dbfaSNadia Derbey 
ksys_shmget(key_t key,size_t size,int shmflg)83165749e0bSDominik Brodowski long ksys_shmget(key_t key, size_t size, int shmflg)
8321da177e4SLinus Torvalds {
8334e982311SKirill Korotaev 	struct ipc_namespace *ns;
834eb66ec44SMathias Krause 	static const struct ipc_ops shm_ops = {
835eb66ec44SMathias Krause 		.getnew = newseg,
83650ab44b1SEric W. Biederman 		.associate = security_shm_associate,
837eb66ec44SMathias Krause 		.more_checks = shm_more_checks,
838eb66ec44SMathias Krause 	};
8397748dbfaSNadia Derbey 	struct ipc_params shm_params;
8401da177e4SLinus Torvalds 
8414e982311SKirill Korotaev 	ns = current->nsproxy->ipc_ns;
8424e982311SKirill Korotaev 
8437748dbfaSNadia Derbey 	shm_params.key = key;
8447748dbfaSNadia Derbey 	shm_params.flg = shmflg;
8457748dbfaSNadia Derbey 	shm_params.u.size = size;
8467ca7e564SNadia Derbey 
8477748dbfaSNadia Derbey 	return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
SYSCALL_DEFINE3(shmget,key_t,key,size_t,size,int,shmflg)85065749e0bSDominik Brodowski SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
85165749e0bSDominik Brodowski {
85265749e0bSDominik Brodowski 	return ksys_shmget(key, size, shmflg);
85365749e0bSDominik Brodowski }
85465749e0bSDominik Brodowski 
copy_shmid_to_user(void __user * buf,struct shmid64_ds * in,int version)8551da177e4SLinus Torvalds static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	switch (version) {
8581da177e4SLinus Torvalds 	case IPC_64:
8591da177e4SLinus Torvalds 		return copy_to_user(buf, in, sizeof(*in));
8601da177e4SLinus Torvalds 	case IPC_OLD:
8611da177e4SLinus Torvalds 	    {
8621da177e4SLinus Torvalds 		struct shmid_ds out;
8631da177e4SLinus Torvalds 
8643af54c9bSVasiliy Kulikov 		memset(&out, 0, sizeof(out));
8651da177e4SLinus Torvalds 		ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
8661da177e4SLinus Torvalds 		out.shm_segsz	= in->shm_segsz;
8671da177e4SLinus Torvalds 		out.shm_atime	= in->shm_atime;
8681da177e4SLinus Torvalds 		out.shm_dtime	= in->shm_dtime;
8691da177e4SLinus Torvalds 		out.shm_ctime	= in->shm_ctime;
8701da177e4SLinus Torvalds 		out.shm_cpid	= in->shm_cpid;
8711da177e4SLinus Torvalds 		out.shm_lpid	= in->shm_lpid;
8721da177e4SLinus Torvalds 		out.shm_nattch	= in->shm_nattch;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 		return copy_to_user(buf, &out, sizeof(out));
8751da177e4SLinus Torvalds 	    }
8761da177e4SLinus Torvalds 	default:
8771da177e4SLinus Torvalds 		return -EINVAL;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
881016d7132SPierre Peiffer static inline unsigned long
copy_shmid_from_user(struct shmid64_ds * out,void __user * buf,int version)882016d7132SPierre Peiffer copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	switch (version) {
8851da177e4SLinus Torvalds 	case IPC_64:
886016d7132SPierre Peiffer 		if (copy_from_user(out, buf, sizeof(*out)))
8871da177e4SLinus Torvalds 			return -EFAULT;
8881da177e4SLinus Torvalds 		return 0;
8891da177e4SLinus Torvalds 	case IPC_OLD:
8901da177e4SLinus Torvalds 	    {
8911da177e4SLinus Torvalds 		struct shmid_ds tbuf_old;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 		if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
8941da177e4SLinus Torvalds 			return -EFAULT;
8951da177e4SLinus Torvalds 
896016d7132SPierre Peiffer 		out->shm_perm.uid	= tbuf_old.shm_perm.uid;
897016d7132SPierre Peiffer 		out->shm_perm.gid	= tbuf_old.shm_perm.gid;
898016d7132SPierre Peiffer 		out->shm_perm.mode	= tbuf_old.shm_perm.mode;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 		return 0;
9011da177e4SLinus Torvalds 	    }
9021da177e4SLinus Torvalds 	default:
9031da177e4SLinus Torvalds 		return -EINVAL;
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
copy_shminfo_to_user(void __user * buf,struct shminfo64 * in,int version)9071da177e4SLinus Torvalds static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	switch (version) {
9101da177e4SLinus Torvalds 	case IPC_64:
9111da177e4SLinus Torvalds 		return copy_to_user(buf, in, sizeof(*in));
9121da177e4SLinus Torvalds 	case IPC_OLD:
9131da177e4SLinus Torvalds 	    {
9141da177e4SLinus Torvalds 		struct shminfo out;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 		if (in->shmmax > INT_MAX)
9171da177e4SLinus Torvalds 			out.shmmax = INT_MAX;
9181da177e4SLinus Torvalds 		else
9191da177e4SLinus Torvalds 			out.shmmax = (int)in->shmmax;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 		out.shmmin	= in->shmmin;
9221da177e4SLinus Torvalds 		out.shmmni	= in->shmmni;
9231da177e4SLinus Torvalds 		out.shmseg	= in->shmseg;
9241da177e4SLinus Torvalds 		out.shmall	= in->shmall;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 		return copy_to_user(buf, &out, sizeof(out));
9271da177e4SLinus Torvalds 	    }
9281da177e4SLinus Torvalds 	default:
9291da177e4SLinus Torvalds 		return -EINVAL;
9301da177e4SLinus Torvalds 	}
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
933f4566f04SNadia Derbey /*
934b7952180SHelge Deller  * Calculate and add used RSS and swap pages of a shm.
935d9a605e4SDavidlohr Bueso  * Called with shm_ids.rwsem held as a reader
936b7952180SHelge Deller  */
shm_add_rss_swap(struct shmid_kernel * shp,unsigned long * rss_add,unsigned long * swp_add)937b7952180SHelge Deller static void shm_add_rss_swap(struct shmid_kernel *shp,
938b7952180SHelge Deller 	unsigned long *rss_add, unsigned long *swp_add)
939b7952180SHelge Deller {
940b7952180SHelge Deller 	struct inode *inode;
941b7952180SHelge Deller 
942496ad9aaSAl Viro 	inode = file_inode(shp->shm_file);
943b7952180SHelge Deller 
944b7952180SHelge Deller 	if (is_file_hugepages(shp->shm_file)) {
945b7952180SHelge Deller 		struct address_space *mapping = inode->i_mapping;
946b7952180SHelge Deller 		struct hstate *h = hstate_file(shp->shm_file);
947b7952180SHelge Deller 		*rss_add += pages_per_huge_page(h) * mapping->nrpages;
948b7952180SHelge Deller 	} else {
949b7952180SHelge Deller #ifdef CONFIG_SHMEM
950b7952180SHelge Deller 		struct shmem_inode_info *info = SHMEM_I(inode);
95163980c80SShailesh Pandey 
9524595ef88SKirill A. Shutemov 		spin_lock_irq(&info->lock);
953b7952180SHelge Deller 		*rss_add += inode->i_mapping->nrpages;
954b7952180SHelge Deller 		*swp_add += info->swapped;
9554595ef88SKirill A. Shutemov 		spin_unlock_irq(&info->lock);
956b7952180SHelge Deller #else
957b7952180SHelge Deller 		*rss_add += inode->i_mapping->nrpages;
958b7952180SHelge Deller #endif
959b7952180SHelge Deller 	}
960b7952180SHelge Deller }
961b7952180SHelge Deller 
962b7952180SHelge Deller /*
963d9a605e4SDavidlohr Bueso  * Called with shm_ids.rwsem held as a reader
964f4566f04SNadia Derbey  */
shm_get_stat(struct ipc_namespace * ns,unsigned long * rss,unsigned long * swp)9654e982311SKirill Korotaev static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
9664e982311SKirill Korotaev 		unsigned long *swp)
9671da177e4SLinus Torvalds {
9687ca7e564SNadia Derbey 	int next_id;
9697ca7e564SNadia Derbey 	int total, in_use;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	*rss = 0;
9721da177e4SLinus Torvalds 	*swp = 0;
9731da177e4SLinus Torvalds 
9747ca7e564SNadia Derbey 	in_use = shm_ids(ns).in_use;
9757ca7e564SNadia Derbey 
9767ca7e564SNadia Derbey 	for (total = 0, next_id = 0; total < in_use; next_id++) {
977e562aebcSTony Battersby 		struct kern_ipc_perm *ipc;
9781da177e4SLinus Torvalds 		struct shmid_kernel *shp;
9791da177e4SLinus Torvalds 
980e562aebcSTony Battersby 		ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
981e562aebcSTony Battersby 		if (ipc == NULL)
9821da177e4SLinus Torvalds 			continue;
983e562aebcSTony Battersby 		shp = container_of(ipc, struct shmid_kernel, shm_perm);
9841da177e4SLinus Torvalds 
985b7952180SHelge Deller 		shm_add_rss_swap(shp, rss, swp);
9867ca7e564SNadia Derbey 
9877ca7e564SNadia Derbey 		total++;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9918d4cc8b5SPierre Peiffer /*
992d9a605e4SDavidlohr Bueso  * This function handles some shmctl commands which require the rwsem
9938d4cc8b5SPierre Peiffer  * to be held in write mode.
994d9a605e4SDavidlohr Bueso  * NOTE: no locks must be held, the rwsem is taken inside this function.
9958d4cc8b5SPierre Peiffer  */
shmctl_down(struct ipc_namespace * ns,int shmid,int cmd,struct shmid64_ds * shmid64)9968d4cc8b5SPierre Peiffer static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
9979ba720c1SAl Viro 		       struct shmid64_ds *shmid64)
9988d4cc8b5SPierre Peiffer {
9998d4cc8b5SPierre Peiffer 	struct kern_ipc_perm *ipcp;
10008d4cc8b5SPierre Peiffer 	struct shmid_kernel *shp;
10018d4cc8b5SPierre Peiffer 	int err;
10028d4cc8b5SPierre Peiffer 
1003d9a605e4SDavidlohr Bueso 	down_write(&shm_ids(ns).rwsem);
10047b4cc5d8SDavidlohr Bueso 	rcu_read_lock();
10057b4cc5d8SDavidlohr Bueso 
10064241c1a3SManfred Spraul 	ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd,
10079ba720c1SAl Viro 				      &shmid64->shm_perm, 0);
10087b4cc5d8SDavidlohr Bueso 	if (IS_ERR(ipcp)) {
10097b4cc5d8SDavidlohr Bueso 		err = PTR_ERR(ipcp);
10107b4cc5d8SDavidlohr Bueso 		goto out_unlock1;
10117b4cc5d8SDavidlohr Bueso 	}
10128d4cc8b5SPierre Peiffer 
1013a5f75e7fSPierre Peiffer 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
10148d4cc8b5SPierre Peiffer 
10157191adffSEric W. Biederman 	err = security_shm_shmctl(&shp->shm_perm, cmd);
10168d4cc8b5SPierre Peiffer 	if (err)
101779ccf0f8SDavidlohr Bueso 		goto out_unlock1;
10187b4cc5d8SDavidlohr Bueso 
10198d4cc8b5SPierre Peiffer 	switch (cmd) {
10208d4cc8b5SPierre Peiffer 	case IPC_RMID:
102179ccf0f8SDavidlohr Bueso 		ipc_lock_object(&shp->shm_perm);
10227b4cc5d8SDavidlohr Bueso 		/* do_shm_rmid unlocks the ipc object and rcu */
10238d4cc8b5SPierre Peiffer 		do_shm_rmid(ns, ipcp);
10248d4cc8b5SPierre Peiffer 		goto out_up;
10258d4cc8b5SPierre Peiffer 	case IPC_SET:
102679ccf0f8SDavidlohr Bueso 		ipc_lock_object(&shp->shm_perm);
10279ba720c1SAl Viro 		err = ipc_update_perm(&shmid64->shm_perm, ipcp);
10281efdb69bSEric W. Biederman 		if (err)
10297b4cc5d8SDavidlohr Bueso 			goto out_unlock0;
10307ff2819eSDeepa Dinamani 		shp->shm_ctim = ktime_get_real_seconds();
10318d4cc8b5SPierre Peiffer 		break;
10328d4cc8b5SPierre Peiffer 	default:
10338d4cc8b5SPierre Peiffer 		err = -EINVAL;
103479ccf0f8SDavidlohr Bueso 		goto out_unlock1;
10358d4cc8b5SPierre Peiffer 	}
10367b4cc5d8SDavidlohr Bueso 
10377b4cc5d8SDavidlohr Bueso out_unlock0:
10387b4cc5d8SDavidlohr Bueso 	ipc_unlock_object(&shp->shm_perm);
10397b4cc5d8SDavidlohr Bueso out_unlock1:
10407b4cc5d8SDavidlohr Bueso 	rcu_read_unlock();
10418d4cc8b5SPierre Peiffer out_up:
1042d9a605e4SDavidlohr Bueso 	up_write(&shm_ids(ns).rwsem);
10438d4cc8b5SPierre Peiffer 	return err;
10448d4cc8b5SPierre Peiffer }
10458d4cc8b5SPierre Peiffer 
shmctl_ipc_info(struct ipc_namespace * ns,struct shminfo64 * shminfo)10469ba720c1SAl Viro static int shmctl_ipc_info(struct ipc_namespace *ns,
10479ba720c1SAl Viro 			   struct shminfo64 *shminfo)
10481da177e4SLinus Torvalds {
10499ba720c1SAl Viro 	int err = security_shm_shmctl(NULL, IPC_INFO);
10509ba720c1SAl Viro 	if (!err) {
10519ba720c1SAl Viro 		memset(shminfo, 0, sizeof(*shminfo));
10529ba720c1SAl Viro 		shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni;
10539ba720c1SAl Viro 		shminfo->shmmax = ns->shm_ctlmax;
10549ba720c1SAl Viro 		shminfo->shmall = ns->shm_ctlall;
10559ba720c1SAl Viro 		shminfo->shmmin = SHMMIN;
10569ba720c1SAl Viro 		down_read(&shm_ids(ns).rwsem);
105727c331a1SManfred Spraul 		err = ipc_get_maxidx(&shm_ids(ns));
10589ba720c1SAl Viro 		up_read(&shm_ids(ns).rwsem);
10599ba720c1SAl Viro 		if (err < 0)
10609ba720c1SAl Viro 			err = 0;
10619ba720c1SAl Viro 	}
10621da177e4SLinus Torvalds 	return err;
106368eccc1dSDavidlohr Bueso }
106468eccc1dSDavidlohr Bueso 
shmctl_shm_info(struct ipc_namespace * ns,struct shm_info * shm_info)10659ba720c1SAl Viro static int shmctl_shm_info(struct ipc_namespace *ns,
10669ba720c1SAl Viro 			   struct shm_info *shm_info)
106768eccc1dSDavidlohr Bueso {
10689ba720c1SAl Viro 	int err = security_shm_shmctl(NULL, SHM_INFO);
10699ba720c1SAl Viro 	if (!err) {
10709ba720c1SAl Viro 		memset(shm_info, 0, sizeof(*shm_info));
1071d9a605e4SDavidlohr Bueso 		down_read(&shm_ids(ns).rwsem);
10729ba720c1SAl Viro 		shm_info->used_ids = shm_ids(ns).in_use;
10739ba720c1SAl Viro 		shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp);
10749ba720c1SAl Viro 		shm_info->shm_tot = ns->shm_tot;
10759ba720c1SAl Viro 		shm_info->swap_attempts = 0;
10769ba720c1SAl Viro 		shm_info->swap_successes = 0;
107727c331a1SManfred Spraul 		err = ipc_get_maxidx(&shm_ids(ns));
1078d9a605e4SDavidlohr Bueso 		up_read(&shm_ids(ns).rwsem);
10791da177e4SLinus Torvalds 		if (err < 0)
10801da177e4SLinus Torvalds 			err = 0;
10811da177e4SLinus Torvalds 	}
10829ba720c1SAl Viro 	return err;
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
shmctl_stat(struct ipc_namespace * ns,int shmid,int cmd,struct shmid64_ds * tbuf)10859ba720c1SAl Viro static int shmctl_stat(struct ipc_namespace *ns, int shmid,
10869ba720c1SAl Viro 			int cmd, struct shmid64_ds *tbuf)
10871da177e4SLinus Torvalds {
10889ba720c1SAl Viro 	struct shmid_kernel *shp;
10899ba720c1SAl Viro 	int err;
1090023a5355SNadia Derbey 
109187ad4b0dSPhilippe Mikoyan 	memset(tbuf, 0, sizeof(*tbuf));
109287ad4b0dSPhilippe Mikoyan 
1093c97cb9ccSDavidlohr Bueso 	rcu_read_lock();
1094c21a6970SDavidlohr Bueso 	if (cmd == SHM_STAT || cmd == SHM_STAT_ANY) {
1095c97cb9ccSDavidlohr Bueso 		shp = shm_obtain_object(ns, shmid);
1096023a5355SNadia Derbey 		if (IS_ERR(shp)) {
1097023a5355SNadia Derbey 			err = PTR_ERR(shp);
1098c97cb9ccSDavidlohr Bueso 			goto out_unlock;
1099023a5355SNadia Derbey 		}
1100c21a6970SDavidlohr Bueso 	} else { /* IPC_STAT */
1101c97cb9ccSDavidlohr Bueso 		shp = shm_obtain_object_check(ns, shmid);
1102023a5355SNadia Derbey 		if (IS_ERR(shp)) {
1103023a5355SNadia Derbey 			err = PTR_ERR(shp);
1104c97cb9ccSDavidlohr Bueso 			goto out_unlock;
1105023a5355SNadia Derbey 		}
11061da177e4SLinus Torvalds 	}
1107c97cb9ccSDavidlohr Bueso 
1108c21a6970SDavidlohr Bueso 	/*
1109c21a6970SDavidlohr Bueso 	 * Semantically SHM_STAT_ANY ought to be identical to
1110c21a6970SDavidlohr Bueso 	 * that functionality provided by the /proc/sysvipc/
1111c21a6970SDavidlohr Bueso 	 * interface. As such, only audit these calls and
1112c21a6970SDavidlohr Bueso 	 * do not do traditional S_IRUGO permission checks on
1113c21a6970SDavidlohr Bueso 	 * the ipc object.
1114c21a6970SDavidlohr Bueso 	 */
1115c21a6970SDavidlohr Bueso 	if (cmd == SHM_STAT_ANY)
1116c21a6970SDavidlohr Bueso 		audit_ipc_obj(&shp->shm_perm);
1117c21a6970SDavidlohr Bueso 	else {
11181da177e4SLinus Torvalds 		err = -EACCES;
1119b0e77598SSerge E. Hallyn 		if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
11201da177e4SLinus Torvalds 			goto out_unlock;
1121c21a6970SDavidlohr Bueso 	}
1122c97cb9ccSDavidlohr Bueso 
11237191adffSEric W. Biederman 	err = security_shm_shmctl(&shp->shm_perm, cmd);
11241da177e4SLinus Torvalds 	if (err)
11251da177e4SLinus Torvalds 		goto out_unlock;
1126c97cb9ccSDavidlohr Bueso 
112787ad4b0dSPhilippe Mikoyan 	ipc_lock_object(&shp->shm_perm);
112887ad4b0dSPhilippe Mikoyan 
112987ad4b0dSPhilippe Mikoyan 	if (!ipc_valid_object(&shp->shm_perm)) {
113087ad4b0dSPhilippe Mikoyan 		ipc_unlock_object(&shp->shm_perm);
113187ad4b0dSPhilippe Mikoyan 		err = -EIDRM;
113287ad4b0dSPhilippe Mikoyan 		goto out_unlock;
113387ad4b0dSPhilippe Mikoyan 	}
113487ad4b0dSPhilippe Mikoyan 
11359ba720c1SAl Viro 	kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm);
11369ba720c1SAl Viro 	tbuf->shm_segsz	= shp->shm_segsz;
11379ba720c1SAl Viro 	tbuf->shm_atime	= shp->shm_atim;
11389ba720c1SAl Viro 	tbuf->shm_dtime	= shp->shm_dtim;
11399ba720c1SAl Viro 	tbuf->shm_ctime	= shp->shm_ctim;
1140c2ab975cSArnd Bergmann #ifndef CONFIG_64BIT
1141c2ab975cSArnd Bergmann 	tbuf->shm_atime_high = shp->shm_atim >> 32;
1142c2ab975cSArnd Bergmann 	tbuf->shm_dtime_high = shp->shm_dtim >> 32;
1143c2ab975cSArnd Bergmann 	tbuf->shm_ctime_high = shp->shm_ctim >> 32;
1144c2ab975cSArnd Bergmann #endif
114598f929b1SEric W. Biederman 	tbuf->shm_cpid	= pid_vnr(shp->shm_cprid);
114698f929b1SEric W. Biederman 	tbuf->shm_lpid	= pid_vnr(shp->shm_lprid);
11479ba720c1SAl Viro 	tbuf->shm_nattch = shp->shm_nattch;
114887ad4b0dSPhilippe Mikoyan 
1149615c999cSManfred Spraul 	if (cmd == IPC_STAT) {
1150615c999cSManfred Spraul 		/*
1151615c999cSManfred Spraul 		 * As defined in SUS:
1152615c999cSManfred Spraul 		 * Return 0 on success
1153615c999cSManfred Spraul 		 */
1154615c999cSManfred Spraul 		err = 0;
1155615c999cSManfred Spraul 	} else {
1156615c999cSManfred Spraul 		/*
1157615c999cSManfred Spraul 		 * SHM_STAT and SHM_STAT_ANY (both Linux specific)
1158615c999cSManfred Spraul 		 * Return the full id, including the sequence number
1159615c999cSManfred Spraul 		 */
1160615c999cSManfred Spraul 		err = shp->shm_perm.id;
1161615c999cSManfred Spraul 	}
116268eccc1dSDavidlohr Bueso 
1163615c999cSManfred Spraul 	ipc_unlock_object(&shp->shm_perm);
116468eccc1dSDavidlohr Bueso out_unlock:
1165c97cb9ccSDavidlohr Bueso 	rcu_read_unlock();
116668eccc1dSDavidlohr Bueso 	return err;
116768eccc1dSDavidlohr Bueso }
116868eccc1dSDavidlohr Bueso 
shmctl_do_lock(struct ipc_namespace * ns,int shmid,int cmd)11699ba720c1SAl Viro static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd)
117068eccc1dSDavidlohr Bueso {
117168eccc1dSDavidlohr Bueso 	struct shmid_kernel *shp;
117285046579SHugh Dickins 	struct file *shm_file;
11739ba720c1SAl Viro 	int err;
117489e004eaSLee Schermerhorn 
11752caacaa8SDavidlohr Bueso 	rcu_read_lock();
11762caacaa8SDavidlohr Bueso 	shp = shm_obtain_object_check(ns, shmid);
1177023a5355SNadia Derbey 	if (IS_ERR(shp)) {
1178023a5355SNadia Derbey 		err = PTR_ERR(shp);
11792caacaa8SDavidlohr Bueso 		goto out_unlock1;
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 
1182a33e6751SAl Viro 	audit_ipc_obj(&(shp->shm_perm));
11837191adffSEric W. Biederman 	err = security_shm_shmctl(&shp->shm_perm, cmd);
11842caacaa8SDavidlohr Bueso 	if (err)
11852caacaa8SDavidlohr Bueso 		goto out_unlock1;
1186073115d6SSteve Grubb 
11872caacaa8SDavidlohr Bueso 	ipc_lock_object(&shp->shm_perm);
11880f3d2b01SRafael Aquini 
11890f3d2b01SRafael Aquini 	/* check if shm_destroy() is tearing down shp */
11900f3d2b01SRafael Aquini 	if (!ipc_valid_object(&shp->shm_perm)) {
11910f3d2b01SRafael Aquini 		err = -EIDRM;
11920f3d2b01SRafael Aquini 		goto out_unlock0;
11930f3d2b01SRafael Aquini 	}
11940f3d2b01SRafael Aquini 
1195b0e77598SSerge E. Hallyn 	if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
11961efdb69bSEric W. Biederman 		kuid_t euid = current_euid();
119763980c80SShailesh Pandey 
11981efdb69bSEric W. Biederman 		if (!uid_eq(euid, shp->shm_perm.uid) &&
11993a72660bSJesper Nilsson 		    !uid_eq(euid, shp->shm_perm.cuid)) {
12003a72660bSJesper Nilsson 			err = -EPERM;
12012caacaa8SDavidlohr Bueso 			goto out_unlock0;
12023a72660bSJesper Nilsson 		}
12033a72660bSJesper Nilsson 		if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
12043a72660bSJesper Nilsson 			err = -EPERM;
12052caacaa8SDavidlohr Bueso 			goto out_unlock0;
12061da177e4SLinus Torvalds 		}
12073a72660bSJesper Nilsson 	}
12081da177e4SLinus Torvalds 
120985046579SHugh Dickins 	shm_file = shp->shm_file;
121085046579SHugh Dickins 	if (is_file_hugepages(shm_file))
12112caacaa8SDavidlohr Bueso 		goto out_unlock0;
121285046579SHugh Dickins 
12131da177e4SLinus Torvalds 	if (cmd == SHM_LOCK) {
1214d7c9e99aSAlexey Gladkov 		struct ucounts *ucounts = current_ucounts();
121563980c80SShailesh Pandey 
1216d7c9e99aSAlexey Gladkov 		err = shmem_lock(shm_file, 1, ucounts);
12177be77e20SPavel Emelianov 		if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
1218b33291c0SAndrew Morton 			shp->shm_perm.mode |= SHM_LOCKED;
1219d7c9e99aSAlexey Gladkov 			shp->mlock_ucounts = ucounts;
12201da177e4SLinus Torvalds 		}
12212caacaa8SDavidlohr Bueso 		goto out_unlock0;
12221da177e4SLinus Torvalds 	}
122385046579SHugh Dickins 
122485046579SHugh Dickins 	/* SHM_UNLOCK */
122585046579SHugh Dickins 	if (!(shp->shm_perm.mode & SHM_LOCKED))
12262caacaa8SDavidlohr Bueso 		goto out_unlock0;
1227d7c9e99aSAlexey Gladkov 	shmem_lock(shm_file, 0, shp->mlock_ucounts);
1228b33291c0SAndrew Morton 	shp->shm_perm.mode &= ~SHM_LOCKED;
1229d7c9e99aSAlexey Gladkov 	shp->mlock_ucounts = NULL;
123085046579SHugh Dickins 	get_file(shm_file);
12312caacaa8SDavidlohr Bueso 	ipc_unlock_object(&shp->shm_perm);
12322caacaa8SDavidlohr Bueso 	rcu_read_unlock();
123324513264SHugh Dickins 	shmem_unlock_mapping(shm_file->f_mapping);
12342caacaa8SDavidlohr Bueso 
123585046579SHugh Dickins 	fput(shm_file);
12368d4cc8b5SPierre Peiffer 	return err;
12371da177e4SLinus Torvalds 
12382caacaa8SDavidlohr Bueso out_unlock0:
12392caacaa8SDavidlohr Bueso 	ipc_unlock_object(&shp->shm_perm);
12402caacaa8SDavidlohr Bueso out_unlock1:
12412caacaa8SDavidlohr Bueso 	rcu_read_unlock();
12421da177e4SLinus Torvalds 	return err;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
ksys_shmctl(int shmid,int cmd,struct shmid_ds __user * buf,int version)1245275f2214SArnd Bergmann static long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf, int version)
12469ba720c1SAl Viro {
1247275f2214SArnd Bergmann 	int err;
12489ba720c1SAl Viro 	struct ipc_namespace *ns;
1249553f770eSAl Viro 	struct shmid64_ds sem64;
12509ba720c1SAl Viro 
12519ba720c1SAl Viro 	if (cmd < 0 || shmid < 0)
12529ba720c1SAl Viro 		return -EINVAL;
12539ba720c1SAl Viro 
12549ba720c1SAl Viro 	ns = current->nsproxy->ipc_ns;
12559ba720c1SAl Viro 
12569ba720c1SAl Viro 	switch (cmd) {
12579ba720c1SAl Viro 	case IPC_INFO: {
12589ba720c1SAl Viro 		struct shminfo64 shminfo;
12599ba720c1SAl Viro 		err = shmctl_ipc_info(ns, &shminfo);
12609ba720c1SAl Viro 		if (err < 0)
12619ba720c1SAl Viro 			return err;
12629ba720c1SAl Viro 		if (copy_shminfo_to_user(buf, &shminfo, version))
12639ba720c1SAl Viro 			err = -EFAULT;
12649ba720c1SAl Viro 		return err;
12659ba720c1SAl Viro 	}
12669ba720c1SAl Viro 	case SHM_INFO: {
12679ba720c1SAl Viro 		struct shm_info shm_info;
12689ba720c1SAl Viro 		err = shmctl_shm_info(ns, &shm_info);
12699ba720c1SAl Viro 		if (err < 0)
12709ba720c1SAl Viro 			return err;
12719ba720c1SAl Viro 		if (copy_to_user(buf, &shm_info, sizeof(shm_info)))
12729ba720c1SAl Viro 			err = -EFAULT;
12739ba720c1SAl Viro 		return err;
12749ba720c1SAl Viro 	}
12759ba720c1SAl Viro 	case SHM_STAT:
1276c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
12779ba720c1SAl Viro 	case IPC_STAT: {
1278553f770eSAl Viro 		err = shmctl_stat(ns, shmid, cmd, &sem64);
12799ba720c1SAl Viro 		if (err < 0)
12809ba720c1SAl Viro 			return err;
1281553f770eSAl Viro 		if (copy_shmid_to_user(buf, &sem64, version))
12829ba720c1SAl Viro 			err = -EFAULT;
12839ba720c1SAl Viro 		return err;
12849ba720c1SAl Viro 	}
12859ba720c1SAl Viro 	case IPC_SET:
1286553f770eSAl Viro 		if (copy_shmid_from_user(&sem64, buf, version))
12879ba720c1SAl Viro 			return -EFAULT;
1288df561f66SGustavo A. R. Silva 		fallthrough;
12899ba720c1SAl Viro 	case IPC_RMID:
1290553f770eSAl Viro 		return shmctl_down(ns, shmid, cmd, &sem64);
12919ba720c1SAl Viro 	case SHM_LOCK:
12929ba720c1SAl Viro 	case SHM_UNLOCK:
12939ba720c1SAl Viro 		return shmctl_do_lock(ns, shmid, cmd);
12949ba720c1SAl Viro 	default:
12959ba720c1SAl Viro 		return -EINVAL;
12969ba720c1SAl Viro 	}
12979ba720c1SAl Viro }
12989ba720c1SAl Viro 
SYSCALL_DEFINE3(shmctl,int,shmid,int,cmd,struct shmid_ds __user *,buf)1299c84d0791SDominik Brodowski SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
1300c84d0791SDominik Brodowski {
1301275f2214SArnd Bergmann 	return ksys_shmctl(shmid, cmd, buf, IPC_64);
1302c84d0791SDominik Brodowski }
1303c84d0791SDominik Brodowski 
1304275f2214SArnd Bergmann #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
ksys_old_shmctl(int shmid,int cmd,struct shmid_ds __user * buf)1305275f2214SArnd Bergmann long ksys_old_shmctl(int shmid, int cmd, struct shmid_ds __user *buf)
1306275f2214SArnd Bergmann {
1307275f2214SArnd Bergmann 	int version = ipc_parse_version(&cmd);
1308275f2214SArnd Bergmann 
1309275f2214SArnd Bergmann 	return ksys_shmctl(shmid, cmd, buf, version);
1310275f2214SArnd Bergmann }
1311275f2214SArnd Bergmann 
SYSCALL_DEFINE3(old_shmctl,int,shmid,int,cmd,struct shmid_ds __user *,buf)1312275f2214SArnd Bergmann SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
1313275f2214SArnd Bergmann {
1314275f2214SArnd Bergmann 	return ksys_old_shmctl(shmid, cmd, buf);
1315275f2214SArnd Bergmann }
1316275f2214SArnd Bergmann #endif
1317275f2214SArnd Bergmann 
1318553f770eSAl Viro #ifdef CONFIG_COMPAT
1319553f770eSAl Viro 
1320553f770eSAl Viro struct compat_shmid_ds {
1321553f770eSAl Viro 	struct compat_ipc_perm shm_perm;
1322553f770eSAl Viro 	int shm_segsz;
13239afc5eeeSArnd Bergmann 	old_time32_t shm_atime;
13249afc5eeeSArnd Bergmann 	old_time32_t shm_dtime;
13259afc5eeeSArnd Bergmann 	old_time32_t shm_ctime;
1326553f770eSAl Viro 	compat_ipc_pid_t shm_cpid;
1327553f770eSAl Viro 	compat_ipc_pid_t shm_lpid;
1328553f770eSAl Viro 	unsigned short shm_nattch;
1329553f770eSAl Viro 	unsigned short shm_unused;
1330553f770eSAl Viro 	compat_uptr_t shm_unused2;
1331553f770eSAl Viro 	compat_uptr_t shm_unused3;
1332553f770eSAl Viro };
1333553f770eSAl Viro 
1334553f770eSAl Viro struct compat_shminfo64 {
1335553f770eSAl Viro 	compat_ulong_t shmmax;
1336553f770eSAl Viro 	compat_ulong_t shmmin;
1337553f770eSAl Viro 	compat_ulong_t shmmni;
1338553f770eSAl Viro 	compat_ulong_t shmseg;
1339553f770eSAl Viro 	compat_ulong_t shmall;
1340553f770eSAl Viro 	compat_ulong_t __unused1;
1341553f770eSAl Viro 	compat_ulong_t __unused2;
1342553f770eSAl Viro 	compat_ulong_t __unused3;
1343553f770eSAl Viro 	compat_ulong_t __unused4;
1344553f770eSAl Viro };
1345553f770eSAl Viro 
1346553f770eSAl Viro struct compat_shm_info {
1347553f770eSAl Viro 	compat_int_t used_ids;
1348553f770eSAl Viro 	compat_ulong_t shm_tot, shm_rss, shm_swp;
1349553f770eSAl Viro 	compat_ulong_t swap_attempts, swap_successes;
1350553f770eSAl Viro };
1351553f770eSAl Viro 
copy_compat_shminfo_to_user(void __user * buf,struct shminfo64 * in,int version)1352553f770eSAl Viro static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
1353553f770eSAl Viro 					int version)
1354553f770eSAl Viro {
1355553f770eSAl Viro 	if (in->shmmax > INT_MAX)
1356553f770eSAl Viro 		in->shmmax = INT_MAX;
1357553f770eSAl Viro 	if (version == IPC_64) {
1358553f770eSAl Viro 		struct compat_shminfo64 info;
1359553f770eSAl Viro 		memset(&info, 0, sizeof(info));
1360553f770eSAl Viro 		info.shmmax = in->shmmax;
1361553f770eSAl Viro 		info.shmmin = in->shmmin;
1362553f770eSAl Viro 		info.shmmni = in->shmmni;
1363553f770eSAl Viro 		info.shmseg = in->shmseg;
1364553f770eSAl Viro 		info.shmall = in->shmall;
1365553f770eSAl Viro 		return copy_to_user(buf, &info, sizeof(info));
1366553f770eSAl Viro 	} else {
1367553f770eSAl Viro 		struct shminfo info;
1368553f770eSAl Viro 		memset(&info, 0, sizeof(info));
1369553f770eSAl Viro 		info.shmmax = in->shmmax;
1370553f770eSAl Viro 		info.shmmin = in->shmmin;
1371553f770eSAl Viro 		info.shmmni = in->shmmni;
1372553f770eSAl Viro 		info.shmseg = in->shmseg;
1373553f770eSAl Viro 		info.shmall = in->shmall;
1374553f770eSAl Viro 		return copy_to_user(buf, &info, sizeof(info));
1375553f770eSAl Viro 	}
1376553f770eSAl Viro }
1377553f770eSAl Viro 
put_compat_shm_info(struct shm_info * ip,struct compat_shm_info __user * uip)1378553f770eSAl Viro static int put_compat_shm_info(struct shm_info *ip,
1379553f770eSAl Viro 				struct compat_shm_info __user *uip)
1380553f770eSAl Viro {
1381553f770eSAl Viro 	struct compat_shm_info info;
1382553f770eSAl Viro 
1383553f770eSAl Viro 	memset(&info, 0, sizeof(info));
1384553f770eSAl Viro 	info.used_ids = ip->used_ids;
1385553f770eSAl Viro 	info.shm_tot = ip->shm_tot;
1386553f770eSAl Viro 	info.shm_rss = ip->shm_rss;
1387553f770eSAl Viro 	info.shm_swp = ip->shm_swp;
1388553f770eSAl Viro 	info.swap_attempts = ip->swap_attempts;
1389553f770eSAl Viro 	info.swap_successes = ip->swap_successes;
1390b776e4b1SAl Viro 	return copy_to_user(uip, &info, sizeof(info));
1391553f770eSAl Viro }
1392553f770eSAl Viro 
copy_compat_shmid_to_user(void __user * buf,struct shmid64_ds * in,int version)1393553f770eSAl Viro static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in,
1394553f770eSAl Viro 					int version)
1395553f770eSAl Viro {
1396553f770eSAl Viro 	if (version == IPC_64) {
1397553f770eSAl Viro 		struct compat_shmid64_ds v;
1398553f770eSAl Viro 		memset(&v, 0, sizeof(v));
139928327faeSAl Viro 		to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm);
1400c2ab975cSArnd Bergmann 		v.shm_atime	 = lower_32_bits(in->shm_atime);
1401c2ab975cSArnd Bergmann 		v.shm_atime_high = upper_32_bits(in->shm_atime);
1402c2ab975cSArnd Bergmann 		v.shm_dtime	 = lower_32_bits(in->shm_dtime);
1403c2ab975cSArnd Bergmann 		v.shm_dtime_high = upper_32_bits(in->shm_dtime);
1404c2ab975cSArnd Bergmann 		v.shm_ctime	 = lower_32_bits(in->shm_ctime);
1405c2ab975cSArnd Bergmann 		v.shm_ctime_high = upper_32_bits(in->shm_ctime);
1406553f770eSAl Viro 		v.shm_segsz = in->shm_segsz;
1407553f770eSAl Viro 		v.shm_nattch = in->shm_nattch;
1408553f770eSAl Viro 		v.shm_cpid = in->shm_cpid;
1409553f770eSAl Viro 		v.shm_lpid = in->shm_lpid;
1410553f770eSAl Viro 		return copy_to_user(buf, &v, sizeof(v));
1411553f770eSAl Viro 	} else {
1412553f770eSAl Viro 		struct compat_shmid_ds v;
1413553f770eSAl Viro 		memset(&v, 0, sizeof(v));
141428327faeSAl Viro 		to_compat_ipc_perm(&v.shm_perm, &in->shm_perm);
1415553f770eSAl Viro 		v.shm_perm.key = in->shm_perm.key;
1416553f770eSAl Viro 		v.shm_atime = in->shm_atime;
1417553f770eSAl Viro 		v.shm_dtime = in->shm_dtime;
1418553f770eSAl Viro 		v.shm_ctime = in->shm_ctime;
1419553f770eSAl Viro 		v.shm_segsz = in->shm_segsz;
1420553f770eSAl Viro 		v.shm_nattch = in->shm_nattch;
1421553f770eSAl Viro 		v.shm_cpid = in->shm_cpid;
1422553f770eSAl Viro 		v.shm_lpid = in->shm_lpid;
1423553f770eSAl Viro 		return copy_to_user(buf, &v, sizeof(v));
1424553f770eSAl Viro 	}
1425553f770eSAl Viro }
1426553f770eSAl Viro 
copy_compat_shmid_from_user(struct shmid64_ds * out,void __user * buf,int version)1427553f770eSAl Viro static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf,
1428553f770eSAl Viro 					int version)
1429553f770eSAl Viro {
1430553f770eSAl Viro 	memset(out, 0, sizeof(*out));
1431553f770eSAl Viro 	if (version == IPC_64) {
14326aa211e8SLinus Torvalds 		struct compat_shmid64_ds __user *p = buf;
143328327faeSAl Viro 		return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm);
1434553f770eSAl Viro 	} else {
14356aa211e8SLinus Torvalds 		struct compat_shmid_ds __user *p = buf;
143628327faeSAl Viro 		return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm);
1437553f770eSAl Viro 	}
1438553f770eSAl Viro }
1439553f770eSAl Viro 
compat_ksys_shmctl(int shmid,int cmd,void __user * uptr,int version)14401cd377baSJason Yan static long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr, int version)
1441553f770eSAl Viro {
1442553f770eSAl Viro 	struct ipc_namespace *ns;
1443553f770eSAl Viro 	struct shmid64_ds sem64;
1444553f770eSAl Viro 	int err;
1445553f770eSAl Viro 
1446553f770eSAl Viro 	ns = current->nsproxy->ipc_ns;
1447553f770eSAl Viro 
1448553f770eSAl Viro 	if (cmd < 0 || shmid < 0)
1449553f770eSAl Viro 		return -EINVAL;
1450553f770eSAl Viro 
1451553f770eSAl Viro 	switch (cmd) {
1452553f770eSAl Viro 	case IPC_INFO: {
1453553f770eSAl Viro 		struct shminfo64 shminfo;
1454553f770eSAl Viro 		err = shmctl_ipc_info(ns, &shminfo);
1455553f770eSAl Viro 		if (err < 0)
1456553f770eSAl Viro 			return err;
1457553f770eSAl Viro 		if (copy_compat_shminfo_to_user(uptr, &shminfo, version))
1458553f770eSAl Viro 			err = -EFAULT;
1459553f770eSAl Viro 		return err;
1460553f770eSAl Viro 	}
1461553f770eSAl Viro 	case SHM_INFO: {
1462553f770eSAl Viro 		struct shm_info shm_info;
1463553f770eSAl Viro 		err = shmctl_shm_info(ns, &shm_info);
1464553f770eSAl Viro 		if (err < 0)
1465553f770eSAl Viro 			return err;
1466553f770eSAl Viro 		if (put_compat_shm_info(&shm_info, uptr))
1467553f770eSAl Viro 			err = -EFAULT;
1468553f770eSAl Viro 		return err;
1469553f770eSAl Viro 	}
1470553f770eSAl Viro 	case IPC_STAT:
1471c21a6970SDavidlohr Bueso 	case SHM_STAT_ANY:
1472553f770eSAl Viro 	case SHM_STAT:
1473553f770eSAl Viro 		err = shmctl_stat(ns, shmid, cmd, &sem64);
1474553f770eSAl Viro 		if (err < 0)
1475553f770eSAl Viro 			return err;
147658aff0afSWill Deacon 		if (copy_compat_shmid_to_user(uptr, &sem64, version))
1477553f770eSAl Viro 			err = -EFAULT;
1478553f770eSAl Viro 		return err;
1479553f770eSAl Viro 
1480553f770eSAl Viro 	case IPC_SET:
1481553f770eSAl Viro 		if (copy_compat_shmid_from_user(&sem64, uptr, version))
1482553f770eSAl Viro 			return -EFAULT;
1483df561f66SGustavo A. R. Silva 		fallthrough;
1484553f770eSAl Viro 	case IPC_RMID:
1485553f770eSAl Viro 		return shmctl_down(ns, shmid, cmd, &sem64);
1486553f770eSAl Viro 	case SHM_LOCK:
1487553f770eSAl Viro 	case SHM_UNLOCK:
1488553f770eSAl Viro 		return shmctl_do_lock(ns, shmid, cmd);
1489553f770eSAl Viro 	default:
1490553f770eSAl Viro 		return -EINVAL;
1491553f770eSAl Viro 	}
1492553f770eSAl Viro 	return err;
1493553f770eSAl Viro }
1494c84d0791SDominik Brodowski 
COMPAT_SYSCALL_DEFINE3(shmctl,int,shmid,int,cmd,void __user *,uptr)1495c84d0791SDominik Brodowski COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr)
1496c84d0791SDominik Brodowski {
1497275f2214SArnd Bergmann 	return compat_ksys_shmctl(shmid, cmd, uptr, IPC_64);
1498c84d0791SDominik Brodowski }
1499275f2214SArnd Bergmann 
1500275f2214SArnd Bergmann #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
compat_ksys_old_shmctl(int shmid,int cmd,void __user * uptr)1501275f2214SArnd Bergmann long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr)
1502275f2214SArnd Bergmann {
1503275f2214SArnd Bergmann 	int version = compat_ipc_parse_version(&cmd);
1504275f2214SArnd Bergmann 
1505275f2214SArnd Bergmann 	return compat_ksys_shmctl(shmid, cmd, uptr, version);
1506275f2214SArnd Bergmann }
1507275f2214SArnd Bergmann 
COMPAT_SYSCALL_DEFINE3(old_shmctl,int,shmid,int,cmd,void __user *,uptr)1508275f2214SArnd Bergmann COMPAT_SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, void __user *, uptr)
1509275f2214SArnd Bergmann {
1510275f2214SArnd Bergmann 	return compat_ksys_old_shmctl(shmid, cmd, uptr);
1511275f2214SArnd Bergmann }
1512275f2214SArnd Bergmann #endif
1513553f770eSAl Viro #endif
1514553f770eSAl Viro 
15151da177e4SLinus Torvalds /*
15161da177e4SLinus Torvalds  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
15171da177e4SLinus Torvalds  *
15181da177e4SLinus Torvalds  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
15191da177e4SLinus Torvalds  * "raddr" thing points to kernel space, and there has to be a wrapper around
15201da177e4SLinus Torvalds  * this.
15211da177e4SLinus Torvalds  */
do_shmat(int shmid,char __user * shmaddr,int shmflg,ulong * raddr,unsigned long shmlba)152295e91b83SDavidlohr Bueso long do_shmat(int shmid, char __user *shmaddr, int shmflg,
152395e91b83SDavidlohr Bueso 	      ulong *raddr, unsigned long shmlba)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	struct shmid_kernel *shp;
1526f0cb8802SDavidlohr Bueso 	unsigned long addr = (unsigned long)shmaddr;
15271da177e4SLinus Torvalds 	unsigned long size;
15284f089accSAl Viro 	struct file *file, *base;
15291da177e4SLinus Torvalds 	int    err;
1530f0cb8802SDavidlohr Bueso 	unsigned long flags = MAP_SHARED;
15311da177e4SLinus Torvalds 	unsigned long prot;
15321da177e4SLinus Torvalds 	int acc_mode;
15334e982311SKirill Korotaev 	struct ipc_namespace *ns;
1534bc56bba8SEric W. Biederman 	struct shm_file_data *sfd;
1535c9c554f2SAl Viro 	int f_flags;
153641badc15SMichel Lespinasse 	unsigned long populate = 0;
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	err = -EINVAL;
1539bc56bba8SEric W. Biederman 	if (shmid < 0)
15401da177e4SLinus Torvalds 		goto out;
1541f0cb8802SDavidlohr Bueso 
1542f0cb8802SDavidlohr Bueso 	if (addr) {
1543079a96aeSWill Deacon 		if (addr & (shmlba - 1)) {
15448f89c007SDavidlohr Bueso 			if (shmflg & SHM_RND) {
1545a73ab244SDavidlohr Bueso 				addr &= ~(shmlba - 1);  /* round down */
15468f89c007SDavidlohr Bueso 
15478f89c007SDavidlohr Bueso 				/*
15488f89c007SDavidlohr Bueso 				 * Ensure that the round-down is non-nil
15498f89c007SDavidlohr Bueso 				 * when remapping. This can happen for
15508f89c007SDavidlohr Bueso 				 * cases when addr < shmlba.
15518f89c007SDavidlohr Bueso 				 */
15528f89c007SDavidlohr Bueso 				if (!addr && (shmflg & SHM_REMAP))
15538f89c007SDavidlohr Bueso 					goto out;
15548f89c007SDavidlohr Bueso 			} else
15551da177e4SLinus Torvalds #ifndef __ARCH_FORCE_SHMLBA
15561da177e4SLinus Torvalds 				if (addr & ~PAGE_MASK)
15571da177e4SLinus Torvalds #endif
1558bc56bba8SEric W. Biederman 					goto out;
15591da177e4SLinus Torvalds 		}
15601da177e4SLinus Torvalds 
1561f0cb8802SDavidlohr Bueso 		flags |= MAP_FIXED;
1562f0cb8802SDavidlohr Bueso 	} else if ((shmflg & SHM_REMAP))
1563f0cb8802SDavidlohr Bueso 		goto out;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY) {
15661da177e4SLinus Torvalds 		prot = PROT_READ;
15671da177e4SLinus Torvalds 		acc_mode = S_IRUGO;
1568c9c554f2SAl Viro 		f_flags = O_RDONLY;
15691da177e4SLinus Torvalds 	} else {
15701da177e4SLinus Torvalds 		prot = PROT_READ | PROT_WRITE;
15711da177e4SLinus Torvalds 		acc_mode = S_IRUGO | S_IWUGO;
1572c9c554f2SAl Viro 		f_flags = O_RDWR;
15731da177e4SLinus Torvalds 	}
15741da177e4SLinus Torvalds 	if (shmflg & SHM_EXEC) {
15751da177e4SLinus Torvalds 		prot |= PROT_EXEC;
15761da177e4SLinus Torvalds 		acc_mode |= S_IXUGO;
15771da177e4SLinus Torvalds 	}
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	/*
15801da177e4SLinus Torvalds 	 * We cannot rely on the fs check since SYSV IPC does have an
15811da177e4SLinus Torvalds 	 * additional creator id...
15821da177e4SLinus Torvalds 	 */
15834e982311SKirill Korotaev 	ns = current->nsproxy->ipc_ns;
1584c2c737a0SDavidlohr Bueso 	rcu_read_lock();
1585c2c737a0SDavidlohr Bueso 	shp = shm_obtain_object_check(ns, shmid);
1586023a5355SNadia Derbey 	if (IS_ERR(shp)) {
1587023a5355SNadia Derbey 		err = PTR_ERR(shp);
1588c2c737a0SDavidlohr Bueso 		goto out_unlock;
1589023a5355SNadia Derbey 	}
1590bc56bba8SEric W. Biederman 
15911da177e4SLinus Torvalds 	err = -EACCES;
1592b0e77598SSerge E. Hallyn 	if (ipcperms(ns, &shp->shm_perm, acc_mode))
1593bc56bba8SEric W. Biederman 		goto out_unlock;
15941da177e4SLinus Torvalds 
15957191adffSEric W. Biederman 	err = security_shm_shmat(&shp->shm_perm, shmaddr, shmflg);
1596bc56bba8SEric W. Biederman 	if (err)
1597bc56bba8SEric W. Biederman 		goto out_unlock;
15981da177e4SLinus Torvalds 
1599c2c737a0SDavidlohr Bueso 	ipc_lock_object(&shp->shm_perm);
1600a399b29dSGreg Thelen 
1601a399b29dSGreg Thelen 	/* check if shm_destroy() is tearing down shp */
16020f3d2b01SRafael Aquini 	if (!ipc_valid_object(&shp->shm_perm)) {
1603a399b29dSGreg Thelen 		ipc_unlock_object(&shp->shm_perm);
1604a399b29dSGreg Thelen 		err = -EIDRM;
1605a399b29dSGreg Thelen 		goto out_unlock;
1606a399b29dSGreg Thelen 	}
1607a399b29dSGreg Thelen 
16083f05317dSEric Biggers 	/*
16093f05317dSEric Biggers 	 * We need to take a reference to the real shm file to prevent the
16103f05317dSEric Biggers 	 * pointer from becoming stale in cases where the lifetime of the outer
16113f05317dSEric Biggers 	 * file extends beyond that of the shm segment.  It's not usually
16123f05317dSEric Biggers 	 * possible, but it can happen during remap_file_pages() emulation as
16133f05317dSEric Biggers 	 * that unmaps the memory, then does ->mmap() via file reference only.
16143f05317dSEric Biggers 	 * We'll deny the ->mmap() if the shm segment was since removed, but to
16153f05317dSEric Biggers 	 * detect shm ID reuse we need to compare the file pointers.
16163f05317dSEric Biggers 	 */
16174f089accSAl Viro 	base = get_file(shp->shm_file);
16184f089accSAl Viro 	shp->shm_nattch++;
16194f089accSAl Viro 	size = i_size_read(file_inode(base));
16204f089accSAl Viro 	ipc_unlock_object(&shp->shm_perm);
16214f089accSAl Viro 	rcu_read_unlock();
16224f089accSAl Viro 
16234f089accSAl Viro 	err = -ENOMEM;
16244f089accSAl Viro 	sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
16254f089accSAl Viro 	if (!sfd) {
16264f089accSAl Viro 		fput(base);
16274f089accSAl Viro 		goto out_nattch;
16284f089accSAl Viro 	}
16294f089accSAl Viro 
16304f089accSAl Viro 	file = alloc_file_clone(base, f_flags,
16314f089accSAl Viro 			  is_file_hugepages(base) ?
16324f089accSAl Viro 				&shm_file_operations_huge :
16334f089accSAl Viro 				&shm_file_operations);
16344f089accSAl Viro 	err = PTR_ERR(file);
16354f089accSAl Viro 	if (IS_ERR(file)) {
16364f089accSAl Viro 		kfree(sfd);
16374f089accSAl Viro 		fput(base);
16384f089accSAl Viro 		goto out_nattch;
16394f089accSAl Viro 	}
16404f089accSAl Viro 
16414f089accSAl Viro 	sfd->id = shp->shm_perm.id;
16424f089accSAl Viro 	sfd->ns = get_ipc_ns(ns);
16434f089accSAl Viro 	sfd->file = base;
1644bc56bba8SEric W. Biederman 	sfd->vm_ops = NULL;
16454f089accSAl Viro 	file->private_data = sfd;
1646bc56bba8SEric W. Biederman 
16478b3ec681SAl Viro 	err = security_mmap_file(file, prot, flags);
16488b3ec681SAl Viro 	if (err)
16498b3ec681SAl Viro 		goto out_fput;
16508b3ec681SAl Viro 
1651d8ed45c5SMichel Lespinasse 	if (mmap_write_lock_killable(current->mm)) {
165291f4f94eSMichal Hocko 		err = -EINTR;
165391f4f94eSMichal Hocko 		goto out_fput;
165491f4f94eSMichal Hocko 	}
165591f4f94eSMichal Hocko 
16561da177e4SLinus Torvalds 	if (addr && !(shmflg & SHM_REMAP)) {
1657bc56bba8SEric W. Biederman 		err = -EINVAL;
1658247a8ce8SManfred Spraul 		if (addr + size < addr)
1659247a8ce8SManfred Spraul 			goto invalid;
1660247a8ce8SManfred Spraul 
16611da177e4SLinus Torvalds 		if (find_vma_intersection(current->mm, addr, addr + size))
16621da177e4SLinus Torvalds 			goto invalid;
16631da177e4SLinus Torvalds 	}
16641da177e4SLinus Torvalds 
1665*592b5fadSYu-cheng Yu 	addr = do_mmap(file, addr, size, prot, flags, 0, 0, &populate, NULL);
1666bebeb3d6SMichel Lespinasse 	*raddr = addr;
1667bc56bba8SEric W. Biederman 	err = 0;
1668bebeb3d6SMichel Lespinasse 	if (IS_ERR_VALUE(addr))
1669bebeb3d6SMichel Lespinasse 		err = (long)addr;
16701da177e4SLinus Torvalds invalid:
1671d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
1672bebeb3d6SMichel Lespinasse 	if (populate)
167341badc15SMichel Lespinasse 		mm_populate(addr, populate);
16741da177e4SLinus Torvalds 
16758b3ec681SAl Viro out_fput:
1676bc56bba8SEric W. Biederman 	fput(file);
1677bc56bba8SEric W. Biederman 
1678bc56bba8SEric W. Biederman out_nattch:
1679d9a605e4SDavidlohr Bueso 	down_write(&shm_ids(ns).rwsem);
168000c2bf85SNadia Derbey 	shp = shm_lock(ns, shmid);
16811da177e4SLinus Torvalds 	shp->shm_nattch--;
168285b6d246SAlexander Mikhalitsyn 
168385b6d246SAlexander Mikhalitsyn 	if (shm_may_destroy(shp))
16844e982311SKirill Korotaev 		shm_destroy(ns, shp);
16851da177e4SLinus Torvalds 	else
16861da177e4SLinus Torvalds 		shm_unlock(shp);
1687d9a605e4SDavidlohr Bueso 	up_write(&shm_ids(ns).rwsem);
16881da177e4SLinus Torvalds 	return err;
1689bc56bba8SEric W. Biederman 
1690bc56bba8SEric W. Biederman out_unlock:
1691c2c737a0SDavidlohr Bueso 	rcu_read_unlock();
1692f42569b1SDavidlohr Bueso out:
1693f42569b1SDavidlohr Bueso 	return err;
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
SYSCALL_DEFINE3(shmat,int,shmid,char __user *,shmaddr,int,shmflg)1696d5460c99SHeiko Carstens SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
16977d87e14cSStephen Rothwell {
16987d87e14cSStephen Rothwell 	unsigned long ret;
16997d87e14cSStephen Rothwell 	long err;
17007d87e14cSStephen Rothwell 
1701079a96aeSWill Deacon 	err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
17027d87e14cSStephen Rothwell 	if (err)
17037d87e14cSStephen Rothwell 		return err;
17047d87e14cSStephen Rothwell 	force_successful_syscall_return();
17057d87e14cSStephen Rothwell 	return (long)ret;
17067d87e14cSStephen Rothwell }
17077d87e14cSStephen Rothwell 
1708a78ee9edSAl Viro #ifdef CONFIG_COMPAT
1709a78ee9edSAl Viro 
1710a78ee9edSAl Viro #ifndef COMPAT_SHMLBA
1711a78ee9edSAl Viro #define COMPAT_SHMLBA	SHMLBA
1712a78ee9edSAl Viro #endif
1713a78ee9edSAl Viro 
COMPAT_SYSCALL_DEFINE3(shmat,int,shmid,compat_uptr_t,shmaddr,int,shmflg)1714a78ee9edSAl Viro COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg)
1715a78ee9edSAl Viro {
1716a78ee9edSAl Viro 	unsigned long ret;
1717a78ee9edSAl Viro 	long err;
1718a78ee9edSAl Viro 
1719a78ee9edSAl Viro 	err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
1720a78ee9edSAl Viro 	if (err)
1721a78ee9edSAl Viro 		return err;
1722a78ee9edSAl Viro 	force_successful_syscall_return();
1723a78ee9edSAl Viro 	return (long)ret;
1724a78ee9edSAl Viro }
1725a78ee9edSAl Viro #endif
1726a78ee9edSAl Viro 
17271da177e4SLinus Torvalds /*
17281da177e4SLinus Torvalds  * detach and kill segment if marked destroyed.
17291da177e4SLinus Torvalds  * The work is done in shm_close.
17301da177e4SLinus Torvalds  */
ksys_shmdt(char __user * shmaddr)1731da1e2744SDominik Brodowski long ksys_shmdt(char __user *shmaddr)
17321da177e4SLinus Torvalds {
17331da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
1734586c7e6aSMike Frysinger 	struct vm_area_struct *vma;
17351da177e4SLinus Torvalds 	unsigned long addr = (unsigned long)shmaddr;
17361da177e4SLinus Torvalds 	int retval = -EINVAL;
1737586c7e6aSMike Frysinger #ifdef CONFIG_MMU
1738586c7e6aSMike Frysinger 	loff_t size = 0;
1739d3c97900SDave Hansen 	struct file *file;
174001293a62SLiam R. Howlett 	VMA_ITERATOR(vmi, mm, addr);
1741586c7e6aSMike Frysinger #endif
17421da177e4SLinus Torvalds 
1743df1e2fb5SHugh Dickins 	if (addr & ~PAGE_MASK)
1744df1e2fb5SHugh Dickins 		return retval;
1745df1e2fb5SHugh Dickins 
1746d8ed45c5SMichel Lespinasse 	if (mmap_write_lock_killable(mm))
174791f4f94eSMichal Hocko 		return -EINTR;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	/*
17501da177e4SLinus Torvalds 	 * This function tries to be smart and unmap shm segments that
17511da177e4SLinus Torvalds 	 * were modified by partial mlock or munmap calls:
17521da177e4SLinus Torvalds 	 * - It first determines the size of the shm segment that should be
17531da177e4SLinus Torvalds 	 *   unmapped: It searches for a vma that is backed by shm and that
17541da177e4SLinus Torvalds 	 *   started at address shmaddr. It records it's size and then unmaps
17551da177e4SLinus Torvalds 	 *   it.
17561da177e4SLinus Torvalds 	 * - Then it unmaps all shm vmas that started at shmaddr and that
1757d3c97900SDave Hansen 	 *   are within the initially determined size and that are from the
1758d3c97900SDave Hansen 	 *   same shm segment from which we determined the size.
17591da177e4SLinus Torvalds 	 * Errors from do_munmap are ignored: the function only fails if
17601da177e4SLinus Torvalds 	 * it's called with invalid parameters or if it's called to unmap
17611da177e4SLinus Torvalds 	 * a part of a vma. Both calls in this function are for full vmas,
17621da177e4SLinus Torvalds 	 * the parameters are directly copied from the vma itself and always
17631da177e4SLinus Torvalds 	 * valid - therefore do_munmap cannot fail. (famous last words?)
17641da177e4SLinus Torvalds 	 */
17651da177e4SLinus Torvalds 	/*
17661da177e4SLinus Torvalds 	 * If it had been mremap()'d, the starting address would not
17671da177e4SLinus Torvalds 	 * match the usual checks anyway. So assume all vma's are
17681da177e4SLinus Torvalds 	 * above the starting address given.
17691da177e4SLinus Torvalds 	 */
17701da177e4SLinus Torvalds 
17718feae131SDavid Howells #ifdef CONFIG_MMU
177201293a62SLiam R. Howlett 	for_each_vma(vmi, vma) {
17731da177e4SLinus Torvalds 		/*
17741da177e4SLinus Torvalds 		 * Check if the starting address would match, i.e. it's
17751da177e4SLinus Torvalds 		 * a fragment created by mprotect() and/or munmap(), or it
17761da177e4SLinus Torvalds 		 * otherwise it starts at this address with no hassles.
17771da177e4SLinus Torvalds 		 */
1778bc56bba8SEric W. Biederman 		if ((vma->vm_ops == &shm_vm_ops) &&
17791da177e4SLinus Torvalds 			(vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
17801da177e4SLinus Torvalds 
1781d3c97900SDave Hansen 			/*
1782d3c97900SDave Hansen 			 * Record the file of the shm segment being
1783d3c97900SDave Hansen 			 * unmapped.  With mremap(), someone could place
1784d3c97900SDave Hansen 			 * page from another segment but with equal offsets
1785d3c97900SDave Hansen 			 * in the range we are unmapping.
1786d3c97900SDave Hansen 			 */
1787d3c97900SDave Hansen 			file = vma->vm_file;
178807a46ed2SDave Hansen 			size = i_size_read(file_inode(vma->vm_file));
178927b26701SLiam R. Howlett 			do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end,
179027b26701SLiam R. Howlett 				      NULL, false);
17911da177e4SLinus Torvalds 			/*
17921da177e4SLinus Torvalds 			 * We discovered the size of the shm segment, so
17931da177e4SLinus Torvalds 			 * break out of here and fall through to the next
17941da177e4SLinus Torvalds 			 * loop that uses the size information to stop
17951da177e4SLinus Torvalds 			 * searching for matching vma's.
17961da177e4SLinus Torvalds 			 */
17971da177e4SLinus Torvalds 			retval = 0;
179801293a62SLiam R. Howlett 			vma = vma_next(&vmi);
17991da177e4SLinus Torvalds 			break;
18001da177e4SLinus Torvalds 		}
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	/*
18041da177e4SLinus Torvalds 	 * We need look no further than the maximum address a fragment
18051da177e4SLinus Torvalds 	 * could possibly have landed at. Also cast things to loff_t to
180625985edcSLucas De Marchi 	 * prevent overflows and make comparisons vs. equal-width types.
18071da177e4SLinus Torvalds 	 */
18088e36709dSKAMEZAWA Hiroyuki 	size = PAGE_ALIGN(size);
18091da177e4SLinus Torvalds 	while (vma && (loff_t)(vma->vm_end - addr) <= size) {
18101da177e4SLinus Torvalds 		/* finding a matching vma now does not alter retval */
1811bc56bba8SEric W. Biederman 		if ((vma->vm_ops == &shm_vm_ops) &&
1812d3c97900SDave Hansen 		    ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
181327b26701SLiam R. Howlett 		    (vma->vm_file == file)) {
181427b26701SLiam R. Howlett 			do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end,
181527b26701SLiam R. Howlett 				      NULL, false);
181627b26701SLiam R. Howlett 		}
181701293a62SLiam R. Howlett 
181801293a62SLiam R. Howlett 		vma = vma_next(&vmi);
18191da177e4SLinus Torvalds 	}
18201da177e4SLinus Torvalds 
18218feae131SDavid Howells #else	/* CONFIG_MMU */
182201293a62SLiam R. Howlett 	vma = vma_lookup(mm, addr);
18238feae131SDavid Howells 	/* under NOMMU conditions, the exact address to be destroyed must be
182463980c80SShailesh Pandey 	 * given
182563980c80SShailesh Pandey 	 */
1826530fcd16SDavidlohr Bueso 	if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
1827897ab3e0SMike Rapoport 		do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
18288feae131SDavid Howells 		retval = 0;
18298feae131SDavid Howells 	}
18308feae131SDavid Howells 
18318feae131SDavid Howells #endif
18328feae131SDavid Howells 
1833d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
18341da177e4SLinus Torvalds 	return retval;
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds 
SYSCALL_DEFINE1(shmdt,char __user *,shmaddr)1837da1e2744SDominik Brodowski SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1838da1e2744SDominik Brodowski {
1839da1e2744SDominik Brodowski 	return ksys_shmdt(shmaddr);
1840da1e2744SDominik Brodowski }
1841da1e2744SDominik Brodowski 
18421da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
sysvipc_shm_proc_show(struct seq_file * s,void * it)184319b4946cSMike Waychison static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
18441da177e4SLinus Torvalds {
184598f929b1SEric W. Biederman 	struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
18461efdb69bSEric W. Biederman 	struct user_namespace *user_ns = seq_user_ns(s);
1847ade9f91bSKees Cook 	struct kern_ipc_perm *ipcp = it;
1848ade9f91bSKees Cook 	struct shmid_kernel *shp;
1849b7952180SHelge Deller 	unsigned long rss = 0, swp = 0;
1850b7952180SHelge Deller 
1851ade9f91bSKees Cook 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
1852b7952180SHelge Deller 	shm_add_rss_swap(shp, &rss, &swp);
18531da177e4SLinus Torvalds 
18546c826818SPaul Menage #if BITS_PER_LONG <= 32
18556c826818SPaul Menage #define SIZE_SPEC "%10lu"
18566c826818SPaul Menage #else
18576c826818SPaul Menage #define SIZE_SPEC "%21lu"
18586c826818SPaul Menage #endif
18591da177e4SLinus Torvalds 
18607f032d6eSJoe Perches 	seq_printf(s,
18616c826818SPaul Menage 		   "%10d %10d  %4o " SIZE_SPEC " %5u %5u  "
18627ff2819eSDeepa Dinamani 		   "%5lu %5u %5u %5u %5u %10llu %10llu %10llu "
1863b7952180SHelge Deller 		   SIZE_SPEC " " SIZE_SPEC "\n",
18641da177e4SLinus Torvalds 		   shp->shm_perm.key,
18657ca7e564SNadia Derbey 		   shp->shm_perm.id,
1866b33291c0SAndrew Morton 		   shp->shm_perm.mode,
18671da177e4SLinus Torvalds 		   shp->shm_segsz,
186898f929b1SEric W. Biederman 		   pid_nr_ns(shp->shm_cprid, pid_ns),
186998f929b1SEric W. Biederman 		   pid_nr_ns(shp->shm_lprid, pid_ns),
1870bc56bba8SEric W. Biederman 		   shp->shm_nattch,
18711efdb69bSEric W. Biederman 		   from_kuid_munged(user_ns, shp->shm_perm.uid),
18721efdb69bSEric W. Biederman 		   from_kgid_munged(user_ns, shp->shm_perm.gid),
18731efdb69bSEric W. Biederman 		   from_kuid_munged(user_ns, shp->shm_perm.cuid),
18741efdb69bSEric W. Biederman 		   from_kgid_munged(user_ns, shp->shm_perm.cgid),
18751da177e4SLinus Torvalds 		   shp->shm_atim,
18761da177e4SLinus Torvalds 		   shp->shm_dtim,
1877b7952180SHelge Deller 		   shp->shm_ctim,
1878b7952180SHelge Deller 		   rss * PAGE_SIZE,
1879b7952180SHelge Deller 		   swp * PAGE_SIZE);
18807f032d6eSJoe Perches 
18817f032d6eSJoe Perches 	return 0;
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds #endif
1884