xref: /openbmc/linux/ipc/shm.c (revision af9b2ff010f593d81e2f5fb04155e9fc25b9dfd0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/ipc/shm.c
4  * Copyright (C) 1992, 1993 Krishna Balasubramanian
5  *	 Many improvements/fixes by Bruno Haible.
6  * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
7  * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
8  *
9  * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
10  * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
11  * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
12  * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
13  * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
14  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
15  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
16  *
17  * support for audit of ipc object properties and permission changes
18  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
19  *
20  * namespaces support
21  * OpenVZ, SWsoft Inc.
22  * Pavel Emelianov <xemul@openvz.org>
23  *
24  * Better ipc lock (kern_ipc_perm.lock) handling
25  * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
26  */
27 
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/hugetlb.h>
31 #include <linux/shm.h>
32 #include <linux/init.h>
33 #include <linux/file.h>
34 #include <linux/mman.h>
35 #include <linux/shmem_fs.h>
36 #include <linux/security.h>
37 #include <linux/syscalls.h>
38 #include <linux/audit.h>
39 #include <linux/capability.h>
40 #include <linux/ptrace.h>
41 #include <linux/seq_file.h>
42 #include <linux/rwsem.h>
43 #include <linux/nsproxy.h>
44 #include <linux/mount.h>
45 #include <linux/ipc_namespace.h>
46 #include <linux/rhashtable.h>
47 
48 #include <linux/uaccess.h>
49 
50 #include "util.h"
51 
52 struct shmid_kernel /* private to the kernel */
53 {
54 	struct kern_ipc_perm	shm_perm;
55 	struct file		*shm_file;
56 	unsigned long		shm_nattch;
57 	unsigned long		shm_segsz;
58 	time64_t		shm_atim;
59 	time64_t		shm_dtim;
60 	time64_t		shm_ctim;
61 	struct pid		*shm_cprid;
62 	struct pid		*shm_lprid;
63 	struct ucounts		*mlock_ucounts;
64 
65 	/*
66 	 * The task created the shm object, for
67 	 * task_lock(shp->shm_creator)
68 	 */
69 	struct task_struct	*shm_creator;
70 
71 	/*
72 	 * List by creator. task_lock(->shm_creator) required for read/write.
73 	 * If list_empty(), then the creator is dead already.
74 	 */
75 	struct list_head	shm_clist;
76 	struct ipc_namespace	*ns;
77 } __randomize_layout;
78 
79 /* shm_mode upper byte flags */
80 #define SHM_DEST	01000	/* segment will be destroyed on last detach */
81 #define SHM_LOCKED	02000   /* segment will not be swapped */
82 
83 struct shm_file_data {
84 	int id;
85 	struct ipc_namespace *ns;
86 	struct file *file;
87 	const struct vm_operations_struct *vm_ops;
88 };
89 
90 #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
91 
92 static const struct file_operations shm_file_operations;
93 static const struct vm_operations_struct shm_vm_ops;
94 
95 #define shm_ids(ns)	((ns)->ids[IPC_SHM_IDS])
96 
97 #define shm_unlock(shp)			\
98 	ipc_unlock(&(shp)->shm_perm)
99 
100 static int newseg(struct ipc_namespace *, struct ipc_params *);
101 static void shm_open(struct vm_area_struct *vma);
102 static void shm_close(struct vm_area_struct *vma);
103 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
104 #ifdef CONFIG_PROC_FS
105 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
106 #endif
107 
shm_init_ns(struct ipc_namespace * ns)108 void shm_init_ns(struct ipc_namespace *ns)
109 {
110 	ns->shm_ctlmax = SHMMAX;
111 	ns->shm_ctlall = SHMALL;
112 	ns->shm_ctlmni = SHMMNI;
113 	ns->shm_rmid_forced = 0;
114 	ns->shm_tot = 0;
115 	ipc_init_ids(&shm_ids(ns));
116 }
117 
118 /*
119  * Called with shm_ids.rwsem (writer) and the shp structure locked.
120  * Only shm_ids.rwsem remains locked on exit.
121  */
do_shm_rmid(struct ipc_namespace * ns,struct kern_ipc_perm * ipcp)122 static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
123 {
124 	struct shmid_kernel *shp;
125 
126 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
127 	WARN_ON(ns != shp->ns);
128 
129 	if (shp->shm_nattch) {
130 		shp->shm_perm.mode |= SHM_DEST;
131 		/* Do not find it any more */
132 		ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
133 		shm_unlock(shp);
134 	} else
135 		shm_destroy(ns, shp);
136 }
137 
138 #ifdef CONFIG_IPC_NS
shm_exit_ns(struct ipc_namespace * ns)139 void shm_exit_ns(struct ipc_namespace *ns)
140 {
141 	free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
142 	idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
143 	rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
144 }
145 #endif
146 
ipc_ns_init(void)147 static int __init ipc_ns_init(void)
148 {
149 	shm_init_ns(&init_ipc_ns);
150 	return 0;
151 }
152 
153 pure_initcall(ipc_ns_init);
154 
shm_init(void)155 void __init shm_init(void)
156 {
157 	ipc_init_proc_interface("sysvipc/shm",
158 #if BITS_PER_LONG <= 32
159 				"       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime        rss       swap\n",
160 #else
161 				"       key      shmid perms                  size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime                   rss                  swap\n",
162 #endif
163 				IPC_SHM_IDS, sysvipc_shm_proc_show);
164 }
165 
shm_obtain_object(struct ipc_namespace * ns,int id)166 static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
167 {
168 	struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
169 
170 	if (IS_ERR(ipcp))
171 		return ERR_CAST(ipcp);
172 
173 	return container_of(ipcp, struct shmid_kernel, shm_perm);
174 }
175 
shm_obtain_object_check(struct ipc_namespace * ns,int id)176 static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
177 {
178 	struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
179 
180 	if (IS_ERR(ipcp))
181 		return ERR_CAST(ipcp);
182 
183 	return container_of(ipcp, struct shmid_kernel, shm_perm);
184 }
185 
186 /*
187  * shm_lock_(check_) routines are called in the paths where the rwsem
188  * is not necessarily held.
189  */
shm_lock(struct ipc_namespace * ns,int id)190 static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
191 {
192 	struct kern_ipc_perm *ipcp;
193 
194 	rcu_read_lock();
195 	ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
196 	if (IS_ERR(ipcp))
197 		goto err;
198 
199 	ipc_lock_object(ipcp);
200 	/*
201 	 * ipc_rmid() may have already freed the ID while ipc_lock_object()
202 	 * was spinning: here verify that the structure is still valid.
203 	 * Upon races with RMID, return -EIDRM, thus indicating that
204 	 * the ID points to a removed identifier.
205 	 */
206 	if (ipc_valid_object(ipcp)) {
207 		/* return a locked ipc object upon success */
208 		return container_of(ipcp, struct shmid_kernel, shm_perm);
209 	}
210 
211 	ipc_unlock_object(ipcp);
212 	ipcp = ERR_PTR(-EIDRM);
213 err:
214 	rcu_read_unlock();
215 	/*
216 	 * Callers of shm_lock() must validate the status of the returned ipc
217 	 * object pointer and error out as appropriate.
218 	 */
219 	return ERR_CAST(ipcp);
220 }
221 
shm_lock_by_ptr(struct shmid_kernel * ipcp)222 static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
223 {
224 	rcu_read_lock();
225 	ipc_lock_object(&ipcp->shm_perm);
226 }
227 
shm_rcu_free(struct rcu_head * head)228 static void shm_rcu_free(struct rcu_head *head)
229 {
230 	struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
231 							rcu);
232 	struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
233 							shm_perm);
234 	security_shm_free(&shp->shm_perm);
235 	kfree(shp);
236 }
237 
238 /*
239  * It has to be called with shp locked.
240  * It must be called before ipc_rmid()
241  */
shm_clist_rm(struct shmid_kernel * shp)242 static inline void shm_clist_rm(struct shmid_kernel *shp)
243 {
244 	struct task_struct *creator;
245 
246 	/* ensure that shm_creator does not disappear */
247 	rcu_read_lock();
248 
249 	/*
250 	 * A concurrent exit_shm may do a list_del_init() as well.
251 	 * Just do nothing if exit_shm already did the work
252 	 */
253 	if (!list_empty(&shp->shm_clist)) {
254 		/*
255 		 * shp->shm_creator is guaranteed to be valid *only*
256 		 * if shp->shm_clist is not empty.
257 		 */
258 		creator = shp->shm_creator;
259 
260 		task_lock(creator);
261 		/*
262 		 * list_del_init() is a nop if the entry was already removed
263 		 * from the list.
264 		 */
265 		list_del_init(&shp->shm_clist);
266 		task_unlock(creator);
267 	}
268 	rcu_read_unlock();
269 }
270 
shm_rmid(struct shmid_kernel * s)271 static inline void shm_rmid(struct shmid_kernel *s)
272 {
273 	shm_clist_rm(s);
274 	ipc_rmid(&shm_ids(s->ns), &s->shm_perm);
275 }
276 
277 
__shm_open(struct shm_file_data * sfd)278 static int __shm_open(struct shm_file_data *sfd)
279 {
280 	struct shmid_kernel *shp;
281 
282 	shp = shm_lock(sfd->ns, sfd->id);
283 
284 	if (IS_ERR(shp))
285 		return PTR_ERR(shp);
286 
287 	if (shp->shm_file != sfd->file) {
288 		/* ID was reused */
289 		shm_unlock(shp);
290 		return -EINVAL;
291 	}
292 
293 	shp->shm_atim = ktime_get_real_seconds();
294 	ipc_update_pid(&shp->shm_lprid, task_tgid(current));
295 	shp->shm_nattch++;
296 	shm_unlock(shp);
297 	return 0;
298 }
299 
300 /* This is called by fork, once for every shm attach. */
shm_open(struct vm_area_struct * vma)301 static void shm_open(struct vm_area_struct *vma)
302 {
303 	struct file *file = vma->vm_file;
304 	struct shm_file_data *sfd = shm_file_data(file);
305 	int err;
306 
307 	/* Always call underlying open if present */
308 	if (sfd->vm_ops->open)
309 		sfd->vm_ops->open(vma);
310 
311 	err = __shm_open(sfd);
312 	/*
313 	 * We raced in the idr lookup or with shm_destroy().
314 	 * Either way, the ID is busted.
315 	 */
316 	WARN_ON_ONCE(err);
317 }
318 
319 /*
320  * shm_destroy - free the struct shmid_kernel
321  *
322  * @ns: namespace
323  * @shp: struct to free
324  *
325  * It has to be called with shp and shm_ids.rwsem (writer) locked,
326  * but returns with shp unlocked and freed.
327  */
shm_destroy(struct ipc_namespace * ns,struct shmid_kernel * shp)328 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
329 {
330 	struct file *shm_file;
331 
332 	shm_file = shp->shm_file;
333 	shp->shm_file = NULL;
334 	ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
335 	shm_rmid(shp);
336 	shm_unlock(shp);
337 	if (!is_file_hugepages(shm_file))
338 		shmem_lock(shm_file, 0, shp->mlock_ucounts);
339 	fput(shm_file);
340 	ipc_update_pid(&shp->shm_cprid, NULL);
341 	ipc_update_pid(&shp->shm_lprid, NULL);
342 	ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
343 }
344 
345 /*
346  * shm_may_destroy - identifies whether shm segment should be destroyed now
347  *
348  * Returns true if and only if there are no active users of the segment and
349  * one of the following is true:
350  *
351  * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
352  *
353  * 2) sysctl kernel.shm_rmid_forced is set to 1.
354  */
shm_may_destroy(struct shmid_kernel * shp)355 static bool shm_may_destroy(struct shmid_kernel *shp)
356 {
357 	return (shp->shm_nattch == 0) &&
358 	       (shp->ns->shm_rmid_forced ||
359 		(shp->shm_perm.mode & SHM_DEST));
360 }
361 
362 /*
363  * remove the attach descriptor vma.
364  * free memory for segment if it is marked destroyed.
365  * The descriptor has already been removed from the current->mm->mmap list
366  * and will later be kfree()d.
367  */
__shm_close(struct shm_file_data * sfd)368 static void __shm_close(struct shm_file_data *sfd)
369 {
370 	struct shmid_kernel *shp;
371 	struct ipc_namespace *ns = sfd->ns;
372 
373 	down_write(&shm_ids(ns).rwsem);
374 	/* remove from the list of attaches of the shm segment */
375 	shp = shm_lock(ns, sfd->id);
376 
377 	/*
378 	 * We raced in the idr lookup or with shm_destroy().
379 	 * Either way, the ID is busted.
380 	 */
381 	if (WARN_ON_ONCE(IS_ERR(shp)))
382 		goto done; /* no-op */
383 
384 	ipc_update_pid(&shp->shm_lprid, task_tgid(current));
385 	shp->shm_dtim = ktime_get_real_seconds();
386 	shp->shm_nattch--;
387 	if (shm_may_destroy(shp))
388 		shm_destroy(ns, shp);
389 	else
390 		shm_unlock(shp);
391 done:
392 	up_write(&shm_ids(ns).rwsem);
393 }
394 
shm_close(struct vm_area_struct * vma)395 static void shm_close(struct vm_area_struct *vma)
396 {
397 	struct file *file = vma->vm_file;
398 	struct shm_file_data *sfd = shm_file_data(file);
399 
400 	/* Always call underlying close if present */
401 	if (sfd->vm_ops->close)
402 		sfd->vm_ops->close(vma);
403 
404 	__shm_close(sfd);
405 }
406 
407 /* Called with ns->shm_ids(ns).rwsem locked */
shm_try_destroy_orphaned(int id,void * p,void * data)408 static int shm_try_destroy_orphaned(int id, void *p, void *data)
409 {
410 	struct ipc_namespace *ns = data;
411 	struct kern_ipc_perm *ipcp = p;
412 	struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
413 
414 	/*
415 	 * We want to destroy segments without users and with already
416 	 * exit'ed originating process.
417 	 *
418 	 * As shp->* are changed under rwsem, it's safe to skip shp locking.
419 	 */
420 	if (!list_empty(&shp->shm_clist))
421 		return 0;
422 
423 	if (shm_may_destroy(shp)) {
424 		shm_lock_by_ptr(shp);
425 		shm_destroy(ns, shp);
426 	}
427 	return 0;
428 }
429 
shm_destroy_orphaned(struct ipc_namespace * ns)430 void shm_destroy_orphaned(struct ipc_namespace *ns)
431 {
432 	down_write(&shm_ids(ns).rwsem);
433 	if (shm_ids(ns).in_use) {
434 		rcu_read_lock();
435 		idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
436 		rcu_read_unlock();
437 	}
438 	up_write(&shm_ids(ns).rwsem);
439 }
440 
441 /* Locking assumes this will only be called with task == current */
exit_shm(struct task_struct * task)442 void exit_shm(struct task_struct *task)
443 {
444 	for (;;) {
445 		struct shmid_kernel *shp;
446 		struct ipc_namespace *ns;
447 
448 		task_lock(task);
449 
450 		if (list_empty(&task->sysvshm.shm_clist)) {
451 			task_unlock(task);
452 			break;
453 		}
454 
455 		shp = list_first_entry(&task->sysvshm.shm_clist, struct shmid_kernel,
456 				shm_clist);
457 
458 		/*
459 		 * 1) Get pointer to the ipc namespace. It is worth to say
460 		 * that this pointer is guaranteed to be valid because
461 		 * shp lifetime is always shorter than namespace lifetime
462 		 * in which shp lives.
463 		 * We taken task_lock it means that shp won't be freed.
464 		 */
465 		ns = shp->ns;
466 
467 		/*
468 		 * 2) If kernel.shm_rmid_forced is not set then only keep track of
469 		 * which shmids are orphaned, so that a later set of the sysctl
470 		 * can clean them up.
471 		 */
472 		if (!ns->shm_rmid_forced)
473 			goto unlink_continue;
474 
475 		/*
476 		 * 3) get a reference to the namespace.
477 		 *    The refcount could be already 0. If it is 0, then
478 		 *    the shm objects will be free by free_ipc_work().
479 		 */
480 		ns = get_ipc_ns_not_zero(ns);
481 		if (!ns) {
482 unlink_continue:
483 			list_del_init(&shp->shm_clist);
484 			task_unlock(task);
485 			continue;
486 		}
487 
488 		/*
489 		 * 4) get a reference to shp.
490 		 *   This cannot fail: shm_clist_rm() is called before
491 		 *   ipc_rmid(), thus the refcount cannot be 0.
492 		 */
493 		WARN_ON(!ipc_rcu_getref(&shp->shm_perm));
494 
495 		/*
496 		 * 5) unlink the shm segment from the list of segments
497 		 *    created by current.
498 		 *    This must be done last. After unlinking,
499 		 *    only the refcounts obtained above prevent IPC_RMID
500 		 *    from destroying the segment or the namespace.
501 		 */
502 		list_del_init(&shp->shm_clist);
503 
504 		task_unlock(task);
505 
506 		/*
507 		 * 6) we have all references
508 		 *    Thus lock & if needed destroy shp.
509 		 */
510 		down_write(&shm_ids(ns).rwsem);
511 		shm_lock_by_ptr(shp);
512 		/*
513 		 * rcu_read_lock was implicitly taken in shm_lock_by_ptr, it's
514 		 * safe to call ipc_rcu_putref here
515 		 */
516 		ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
517 
518 		if (ipc_valid_object(&shp->shm_perm)) {
519 			if (shm_may_destroy(shp))
520 				shm_destroy(ns, shp);
521 			else
522 				shm_unlock(shp);
523 		} else {
524 			/*
525 			 * Someone else deleted the shp from namespace
526 			 * idr/kht while we have waited.
527 			 * Just unlock and continue.
528 			 */
529 			shm_unlock(shp);
530 		}
531 
532 		up_write(&shm_ids(ns).rwsem);
533 		put_ipc_ns(ns); /* paired with get_ipc_ns_not_zero */
534 	}
535 }
536 
shm_fault(struct vm_fault * vmf)537 static vm_fault_t shm_fault(struct vm_fault *vmf)
538 {
539 	struct file *file = vmf->vma->vm_file;
540 	struct shm_file_data *sfd = shm_file_data(file);
541 
542 	return sfd->vm_ops->fault(vmf);
543 }
544 
shm_may_split(struct vm_area_struct * vma,unsigned long addr)545 static int shm_may_split(struct vm_area_struct *vma, unsigned long addr)
546 {
547 	struct file *file = vma->vm_file;
548 	struct shm_file_data *sfd = shm_file_data(file);
549 
550 	if (sfd->vm_ops->may_split)
551 		return sfd->vm_ops->may_split(vma, addr);
552 
553 	return 0;
554 }
555 
shm_pagesize(struct vm_area_struct * vma)556 static unsigned long shm_pagesize(struct vm_area_struct *vma)
557 {
558 	struct file *file = vma->vm_file;
559 	struct shm_file_data *sfd = shm_file_data(file);
560 
561 	if (sfd->vm_ops->pagesize)
562 		return sfd->vm_ops->pagesize(vma);
563 
564 	return PAGE_SIZE;
565 }
566 
567 #ifdef CONFIG_NUMA
shm_set_policy(struct vm_area_struct * vma,struct mempolicy * new)568 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
569 {
570 	struct file *file = vma->vm_file;
571 	struct shm_file_data *sfd = shm_file_data(file);
572 	int err = 0;
573 
574 	if (sfd->vm_ops->set_policy)
575 		err = sfd->vm_ops->set_policy(vma, new);
576 	return err;
577 }
578 
shm_get_policy(struct vm_area_struct * vma,unsigned long addr)579 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
580 					unsigned long addr)
581 {
582 	struct file *file = vma->vm_file;
583 	struct shm_file_data *sfd = shm_file_data(file);
584 	struct mempolicy *pol = NULL;
585 
586 	if (sfd->vm_ops->get_policy)
587 		pol = sfd->vm_ops->get_policy(vma, addr);
588 	else if (vma->vm_policy)
589 		pol = vma->vm_policy;
590 
591 	return pol;
592 }
593 #endif
594 
shm_mmap(struct file * file,struct vm_area_struct * vma)595 static int shm_mmap(struct file *file, struct vm_area_struct *vma)
596 {
597 	struct shm_file_data *sfd = shm_file_data(file);
598 	int ret;
599 
600 	/*
601 	 * In case of remap_file_pages() emulation, the file can represent an
602 	 * IPC ID that was removed, and possibly even reused by another shm
603 	 * segment already.  Propagate this case as an error to caller.
604 	 */
605 	ret = __shm_open(sfd);
606 	if (ret)
607 		return ret;
608 
609 	ret = call_mmap(sfd->file, vma);
610 	if (ret) {
611 		__shm_close(sfd);
612 		return ret;
613 	}
614 	sfd->vm_ops = vma->vm_ops;
615 #ifdef CONFIG_MMU
616 	WARN_ON(!sfd->vm_ops->fault);
617 #endif
618 	vma->vm_ops = &shm_vm_ops;
619 	return 0;
620 }
621 
shm_release(struct inode * ino,struct file * file)622 static int shm_release(struct inode *ino, struct file *file)
623 {
624 	struct shm_file_data *sfd = shm_file_data(file);
625 
626 	put_ipc_ns(sfd->ns);
627 	fput(sfd->file);
628 	shm_file_data(file) = NULL;
629 	kfree(sfd);
630 	return 0;
631 }
632 
shm_fsync(struct file * file,loff_t start,loff_t end,int datasync)633 static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
634 {
635 	struct shm_file_data *sfd = shm_file_data(file);
636 
637 	if (!sfd->file->f_op->fsync)
638 		return -EINVAL;
639 	return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
640 }
641 
shm_fallocate(struct file * file,int mode,loff_t offset,loff_t len)642 static long shm_fallocate(struct file *file, int mode, loff_t offset,
643 			  loff_t len)
644 {
645 	struct shm_file_data *sfd = shm_file_data(file);
646 
647 	if (!sfd->file->f_op->fallocate)
648 		return -EOPNOTSUPP;
649 	return sfd->file->f_op->fallocate(file, mode, offset, len);
650 }
651 
shm_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)652 static unsigned long shm_get_unmapped_area(struct file *file,
653 	unsigned long addr, unsigned long len, unsigned long pgoff,
654 	unsigned long flags)
655 {
656 	struct shm_file_data *sfd = shm_file_data(file);
657 
658 	return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
659 						pgoff, flags);
660 }
661 
662 static const struct file_operations shm_file_operations = {
663 	.mmap		= shm_mmap,
664 	.fsync		= shm_fsync,
665 	.release	= shm_release,
666 	.get_unmapped_area	= shm_get_unmapped_area,
667 	.llseek		= noop_llseek,
668 	.fallocate	= shm_fallocate,
669 };
670 
671 /*
672  * shm_file_operations_huge is now identical to shm_file_operations,
673  * but we keep it distinct for the sake of is_file_shm_hugepages().
674  */
675 static const struct file_operations shm_file_operations_huge = {
676 	.mmap		= shm_mmap,
677 	.fsync		= shm_fsync,
678 	.release	= shm_release,
679 	.get_unmapped_area	= shm_get_unmapped_area,
680 	.llseek		= noop_llseek,
681 	.fallocate	= shm_fallocate,
682 };
683 
is_file_shm_hugepages(struct file * file)684 bool is_file_shm_hugepages(struct file *file)
685 {
686 	return file->f_op == &shm_file_operations_huge;
687 }
688 
689 static const struct vm_operations_struct shm_vm_ops = {
690 	.open	= shm_open,	/* callback for a new vm-area open */
691 	.close	= shm_close,	/* callback for when the vm-area is released */
692 	.fault	= shm_fault,
693 	.may_split = shm_may_split,
694 	.pagesize = shm_pagesize,
695 #if defined(CONFIG_NUMA)
696 	.set_policy = shm_set_policy,
697 	.get_policy = shm_get_policy,
698 #endif
699 };
700 
701 /**
702  * newseg - Create a new shared memory segment
703  * @ns: namespace
704  * @params: ptr to the structure that contains key, size and shmflg
705  *
706  * Called with shm_ids.rwsem held as a writer.
707  */
newseg(struct ipc_namespace * ns,struct ipc_params * params)708 static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
709 {
710 	key_t key = params->key;
711 	int shmflg = params->flg;
712 	size_t size = params->u.size;
713 	int error;
714 	struct shmid_kernel *shp;
715 	size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
716 	struct file *file;
717 	char name[13];
718 	vm_flags_t acctflag = 0;
719 
720 	if (size < SHMMIN || size > ns->shm_ctlmax)
721 		return -EINVAL;
722 
723 	if (numpages << PAGE_SHIFT < size)
724 		return -ENOSPC;
725 
726 	if (ns->shm_tot + numpages < ns->shm_tot ||
727 			ns->shm_tot + numpages > ns->shm_ctlall)
728 		return -ENOSPC;
729 
730 	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
731 	if (unlikely(!shp))
732 		return -ENOMEM;
733 
734 	shp->shm_perm.key = key;
735 	shp->shm_perm.mode = (shmflg & S_IRWXUGO);
736 	shp->mlock_ucounts = NULL;
737 
738 	shp->shm_perm.security = NULL;
739 	error = security_shm_alloc(&shp->shm_perm);
740 	if (error) {
741 		kfree(shp);
742 		return error;
743 	}
744 
745 	sprintf(name, "SYSV%08x", key);
746 	if (shmflg & SHM_HUGETLB) {
747 		struct hstate *hs;
748 		size_t hugesize;
749 
750 		hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
751 		if (!hs) {
752 			error = -EINVAL;
753 			goto no_file;
754 		}
755 		hugesize = ALIGN(size, huge_page_size(hs));
756 
757 		/* hugetlb_file_setup applies strict accounting */
758 		if (shmflg & SHM_NORESERVE)
759 			acctflag = VM_NORESERVE;
760 		file = hugetlb_file_setup(name, hugesize, acctflag,
761 				HUGETLB_SHMFS_INODE, (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
762 	} else {
763 		/*
764 		 * Do not allow no accounting for OVERCOMMIT_NEVER, even
765 		 * if it's asked for.
766 		 */
767 		if  ((shmflg & SHM_NORESERVE) &&
768 				sysctl_overcommit_memory != OVERCOMMIT_NEVER)
769 			acctflag = VM_NORESERVE;
770 		file = shmem_kernel_file_setup(name, size, acctflag);
771 	}
772 	error = PTR_ERR(file);
773 	if (IS_ERR(file))
774 		goto no_file;
775 
776 	shp->shm_cprid = get_pid(task_tgid(current));
777 	shp->shm_lprid = NULL;
778 	shp->shm_atim = shp->shm_dtim = 0;
779 	shp->shm_ctim = ktime_get_real_seconds();
780 	shp->shm_segsz = size;
781 	shp->shm_nattch = 0;
782 	shp->shm_file = file;
783 	shp->shm_creator = current;
784 
785 	/* ipc_addid() locks shp upon success. */
786 	error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
787 	if (error < 0)
788 		goto no_id;
789 
790 	shp->ns = ns;
791 
792 	task_lock(current);
793 	list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
794 	task_unlock(current);
795 
796 	/*
797 	 * shmid gets reported as "inode#" in /proc/pid/maps.
798 	 * proc-ps tools use this. Changing this will break them.
799 	 */
800 	file_inode(file)->i_ino = shp->shm_perm.id;
801 
802 	ns->shm_tot += numpages;
803 	error = shp->shm_perm.id;
804 
805 	ipc_unlock_object(&shp->shm_perm);
806 	rcu_read_unlock();
807 	return error;
808 
809 no_id:
810 	ipc_update_pid(&shp->shm_cprid, NULL);
811 	ipc_update_pid(&shp->shm_lprid, NULL);
812 	fput(file);
813 	ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
814 	return error;
815 no_file:
816 	call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
817 	return error;
818 }
819 
820 /*
821  * Called with shm_ids.rwsem and ipcp locked.
822  */
shm_more_checks(struct kern_ipc_perm * ipcp,struct ipc_params * params)823 static int shm_more_checks(struct kern_ipc_perm *ipcp, struct ipc_params *params)
824 {
825 	struct shmid_kernel *shp;
826 
827 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
828 	if (shp->shm_segsz < params->u.size)
829 		return -EINVAL;
830 
831 	return 0;
832 }
833 
ksys_shmget(key_t key,size_t size,int shmflg)834 long ksys_shmget(key_t key, size_t size, int shmflg)
835 {
836 	struct ipc_namespace *ns;
837 	static const struct ipc_ops shm_ops = {
838 		.getnew = newseg,
839 		.associate = security_shm_associate,
840 		.more_checks = shm_more_checks,
841 	};
842 	struct ipc_params shm_params;
843 
844 	ns = current->nsproxy->ipc_ns;
845 
846 	shm_params.key = key;
847 	shm_params.flg = shmflg;
848 	shm_params.u.size = size;
849 
850 	return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
851 }
852 
SYSCALL_DEFINE3(shmget,key_t,key,size_t,size,int,shmflg)853 SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
854 {
855 	return ksys_shmget(key, size, shmflg);
856 }
857 
copy_shmid_to_user(void __user * buf,struct shmid64_ds * in,int version)858 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
859 {
860 	switch (version) {
861 	case IPC_64:
862 		return copy_to_user(buf, in, sizeof(*in));
863 	case IPC_OLD:
864 	    {
865 		struct shmid_ds out;
866 
867 		memset(&out, 0, sizeof(out));
868 		ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
869 		out.shm_segsz	= in->shm_segsz;
870 		out.shm_atime	= in->shm_atime;
871 		out.shm_dtime	= in->shm_dtime;
872 		out.shm_ctime	= in->shm_ctime;
873 		out.shm_cpid	= in->shm_cpid;
874 		out.shm_lpid	= in->shm_lpid;
875 		out.shm_nattch	= in->shm_nattch;
876 
877 		return copy_to_user(buf, &out, sizeof(out));
878 	    }
879 	default:
880 		return -EINVAL;
881 	}
882 }
883 
884 static inline unsigned long
copy_shmid_from_user(struct shmid64_ds * out,void __user * buf,int version)885 copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
886 {
887 	switch (version) {
888 	case IPC_64:
889 		if (copy_from_user(out, buf, sizeof(*out)))
890 			return -EFAULT;
891 		return 0;
892 	case IPC_OLD:
893 	    {
894 		struct shmid_ds tbuf_old;
895 
896 		if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
897 			return -EFAULT;
898 
899 		out->shm_perm.uid	= tbuf_old.shm_perm.uid;
900 		out->shm_perm.gid	= tbuf_old.shm_perm.gid;
901 		out->shm_perm.mode	= tbuf_old.shm_perm.mode;
902 
903 		return 0;
904 	    }
905 	default:
906 		return -EINVAL;
907 	}
908 }
909 
copy_shminfo_to_user(void __user * buf,struct shminfo64 * in,int version)910 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
911 {
912 	switch (version) {
913 	case IPC_64:
914 		return copy_to_user(buf, in, sizeof(*in));
915 	case IPC_OLD:
916 	    {
917 		struct shminfo out;
918 
919 		if (in->shmmax > INT_MAX)
920 			out.shmmax = INT_MAX;
921 		else
922 			out.shmmax = (int)in->shmmax;
923 
924 		out.shmmin	= in->shmmin;
925 		out.shmmni	= in->shmmni;
926 		out.shmseg	= in->shmseg;
927 		out.shmall	= in->shmall;
928 
929 		return copy_to_user(buf, &out, sizeof(out));
930 	    }
931 	default:
932 		return -EINVAL;
933 	}
934 }
935 
936 /*
937  * Calculate and add used RSS and swap pages of a shm.
938  * Called with shm_ids.rwsem held as a reader
939  */
shm_add_rss_swap(struct shmid_kernel * shp,unsigned long * rss_add,unsigned long * swp_add)940 static void shm_add_rss_swap(struct shmid_kernel *shp,
941 	unsigned long *rss_add, unsigned long *swp_add)
942 {
943 	struct inode *inode;
944 
945 	inode = file_inode(shp->shm_file);
946 
947 	if (is_file_hugepages(shp->shm_file)) {
948 		struct address_space *mapping = inode->i_mapping;
949 		struct hstate *h = hstate_file(shp->shm_file);
950 		*rss_add += pages_per_huge_page(h) * mapping->nrpages;
951 	} else {
952 #ifdef CONFIG_SHMEM
953 		struct shmem_inode_info *info = SHMEM_I(inode);
954 
955 		spin_lock_irq(&info->lock);
956 		*rss_add += inode->i_mapping->nrpages;
957 		*swp_add += info->swapped;
958 		spin_unlock_irq(&info->lock);
959 #else
960 		*rss_add += inode->i_mapping->nrpages;
961 #endif
962 	}
963 }
964 
965 /*
966  * Called with shm_ids.rwsem held as a reader
967  */
shm_get_stat(struct ipc_namespace * ns,unsigned long * rss,unsigned long * swp)968 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
969 		unsigned long *swp)
970 {
971 	int next_id;
972 	int total, in_use;
973 
974 	*rss = 0;
975 	*swp = 0;
976 
977 	in_use = shm_ids(ns).in_use;
978 
979 	for (total = 0, next_id = 0; total < in_use; next_id++) {
980 		struct kern_ipc_perm *ipc;
981 		struct shmid_kernel *shp;
982 
983 		ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
984 		if (ipc == NULL)
985 			continue;
986 		shp = container_of(ipc, struct shmid_kernel, shm_perm);
987 
988 		shm_add_rss_swap(shp, rss, swp);
989 
990 		total++;
991 	}
992 }
993 
994 /*
995  * This function handles some shmctl commands which require the rwsem
996  * to be held in write mode.
997  * NOTE: no locks must be held, the rwsem is taken inside this function.
998  */
shmctl_down(struct ipc_namespace * ns,int shmid,int cmd,struct shmid64_ds * shmid64)999 static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
1000 		       struct shmid64_ds *shmid64)
1001 {
1002 	struct kern_ipc_perm *ipcp;
1003 	struct shmid_kernel *shp;
1004 	int err;
1005 
1006 	down_write(&shm_ids(ns).rwsem);
1007 	rcu_read_lock();
1008 
1009 	ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd,
1010 				      &shmid64->shm_perm, 0);
1011 	if (IS_ERR(ipcp)) {
1012 		err = PTR_ERR(ipcp);
1013 		goto out_unlock1;
1014 	}
1015 
1016 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
1017 
1018 	err = security_shm_shmctl(&shp->shm_perm, cmd);
1019 	if (err)
1020 		goto out_unlock1;
1021 
1022 	switch (cmd) {
1023 	case IPC_RMID:
1024 		ipc_lock_object(&shp->shm_perm);
1025 		/* do_shm_rmid unlocks the ipc object and rcu */
1026 		do_shm_rmid(ns, ipcp);
1027 		goto out_up;
1028 	case IPC_SET:
1029 		ipc_lock_object(&shp->shm_perm);
1030 		err = ipc_update_perm(&shmid64->shm_perm, ipcp);
1031 		if (err)
1032 			goto out_unlock0;
1033 		shp->shm_ctim = ktime_get_real_seconds();
1034 		break;
1035 	default:
1036 		err = -EINVAL;
1037 		goto out_unlock1;
1038 	}
1039 
1040 out_unlock0:
1041 	ipc_unlock_object(&shp->shm_perm);
1042 out_unlock1:
1043 	rcu_read_unlock();
1044 out_up:
1045 	up_write(&shm_ids(ns).rwsem);
1046 	return err;
1047 }
1048 
shmctl_ipc_info(struct ipc_namespace * ns,struct shminfo64 * shminfo)1049 static int shmctl_ipc_info(struct ipc_namespace *ns,
1050 			   struct shminfo64 *shminfo)
1051 {
1052 	int err = security_shm_shmctl(NULL, IPC_INFO);
1053 	if (!err) {
1054 		memset(shminfo, 0, sizeof(*shminfo));
1055 		shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni;
1056 		shminfo->shmmax = ns->shm_ctlmax;
1057 		shminfo->shmall = ns->shm_ctlall;
1058 		shminfo->shmmin = SHMMIN;
1059 		down_read(&shm_ids(ns).rwsem);
1060 		err = ipc_get_maxidx(&shm_ids(ns));
1061 		up_read(&shm_ids(ns).rwsem);
1062 		if (err < 0)
1063 			err = 0;
1064 	}
1065 	return err;
1066 }
1067 
shmctl_shm_info(struct ipc_namespace * ns,struct shm_info * shm_info)1068 static int shmctl_shm_info(struct ipc_namespace *ns,
1069 			   struct shm_info *shm_info)
1070 {
1071 	int err = security_shm_shmctl(NULL, SHM_INFO);
1072 	if (!err) {
1073 		memset(shm_info, 0, sizeof(*shm_info));
1074 		down_read(&shm_ids(ns).rwsem);
1075 		shm_info->used_ids = shm_ids(ns).in_use;
1076 		shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp);
1077 		shm_info->shm_tot = ns->shm_tot;
1078 		shm_info->swap_attempts = 0;
1079 		shm_info->swap_successes = 0;
1080 		err = ipc_get_maxidx(&shm_ids(ns));
1081 		up_read(&shm_ids(ns).rwsem);
1082 		if (err < 0)
1083 			err = 0;
1084 	}
1085 	return err;
1086 }
1087 
shmctl_stat(struct ipc_namespace * ns,int shmid,int cmd,struct shmid64_ds * tbuf)1088 static int shmctl_stat(struct ipc_namespace *ns, int shmid,
1089 			int cmd, struct shmid64_ds *tbuf)
1090 {
1091 	struct shmid_kernel *shp;
1092 	int err;
1093 
1094 	memset(tbuf, 0, sizeof(*tbuf));
1095 
1096 	rcu_read_lock();
1097 	if (cmd == SHM_STAT || cmd == SHM_STAT_ANY) {
1098 		shp = shm_obtain_object(ns, shmid);
1099 		if (IS_ERR(shp)) {
1100 			err = PTR_ERR(shp);
1101 			goto out_unlock;
1102 		}
1103 	} else { /* IPC_STAT */
1104 		shp = shm_obtain_object_check(ns, shmid);
1105 		if (IS_ERR(shp)) {
1106 			err = PTR_ERR(shp);
1107 			goto out_unlock;
1108 		}
1109 	}
1110 
1111 	/*
1112 	 * Semantically SHM_STAT_ANY ought to be identical to
1113 	 * that functionality provided by the /proc/sysvipc/
1114 	 * interface. As such, only audit these calls and
1115 	 * do not do traditional S_IRUGO permission checks on
1116 	 * the ipc object.
1117 	 */
1118 	if (cmd == SHM_STAT_ANY)
1119 		audit_ipc_obj(&shp->shm_perm);
1120 	else {
1121 		err = -EACCES;
1122 		if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
1123 			goto out_unlock;
1124 	}
1125 
1126 	err = security_shm_shmctl(&shp->shm_perm, cmd);
1127 	if (err)
1128 		goto out_unlock;
1129 
1130 	ipc_lock_object(&shp->shm_perm);
1131 
1132 	if (!ipc_valid_object(&shp->shm_perm)) {
1133 		ipc_unlock_object(&shp->shm_perm);
1134 		err = -EIDRM;
1135 		goto out_unlock;
1136 	}
1137 
1138 	kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm);
1139 	tbuf->shm_segsz	= shp->shm_segsz;
1140 	tbuf->shm_atime	= shp->shm_atim;
1141 	tbuf->shm_dtime	= shp->shm_dtim;
1142 	tbuf->shm_ctime	= shp->shm_ctim;
1143 #ifndef CONFIG_64BIT
1144 	tbuf->shm_atime_high = shp->shm_atim >> 32;
1145 	tbuf->shm_dtime_high = shp->shm_dtim >> 32;
1146 	tbuf->shm_ctime_high = shp->shm_ctim >> 32;
1147 #endif
1148 	tbuf->shm_cpid	= pid_vnr(shp->shm_cprid);
1149 	tbuf->shm_lpid	= pid_vnr(shp->shm_lprid);
1150 	tbuf->shm_nattch = shp->shm_nattch;
1151 
1152 	if (cmd == IPC_STAT) {
1153 		/*
1154 		 * As defined in SUS:
1155 		 * Return 0 on success
1156 		 */
1157 		err = 0;
1158 	} else {
1159 		/*
1160 		 * SHM_STAT and SHM_STAT_ANY (both Linux specific)
1161 		 * Return the full id, including the sequence number
1162 		 */
1163 		err = shp->shm_perm.id;
1164 	}
1165 
1166 	ipc_unlock_object(&shp->shm_perm);
1167 out_unlock:
1168 	rcu_read_unlock();
1169 	return err;
1170 }
1171 
shmctl_do_lock(struct ipc_namespace * ns,int shmid,int cmd)1172 static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd)
1173 {
1174 	struct shmid_kernel *shp;
1175 	struct file *shm_file;
1176 	int err;
1177 
1178 	rcu_read_lock();
1179 	shp = shm_obtain_object_check(ns, shmid);
1180 	if (IS_ERR(shp)) {
1181 		err = PTR_ERR(shp);
1182 		goto out_unlock1;
1183 	}
1184 
1185 	audit_ipc_obj(&(shp->shm_perm));
1186 	err = security_shm_shmctl(&shp->shm_perm, cmd);
1187 	if (err)
1188 		goto out_unlock1;
1189 
1190 	ipc_lock_object(&shp->shm_perm);
1191 
1192 	/* check if shm_destroy() is tearing down shp */
1193 	if (!ipc_valid_object(&shp->shm_perm)) {
1194 		err = -EIDRM;
1195 		goto out_unlock0;
1196 	}
1197 
1198 	if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
1199 		kuid_t euid = current_euid();
1200 
1201 		if (!uid_eq(euid, shp->shm_perm.uid) &&
1202 		    !uid_eq(euid, shp->shm_perm.cuid)) {
1203 			err = -EPERM;
1204 			goto out_unlock0;
1205 		}
1206 		if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
1207 			err = -EPERM;
1208 			goto out_unlock0;
1209 		}
1210 	}
1211 
1212 	shm_file = shp->shm_file;
1213 	if (is_file_hugepages(shm_file))
1214 		goto out_unlock0;
1215 
1216 	if (cmd == SHM_LOCK) {
1217 		struct ucounts *ucounts = current_ucounts();
1218 
1219 		err = shmem_lock(shm_file, 1, ucounts);
1220 		if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
1221 			shp->shm_perm.mode |= SHM_LOCKED;
1222 			shp->mlock_ucounts = ucounts;
1223 		}
1224 		goto out_unlock0;
1225 	}
1226 
1227 	/* SHM_UNLOCK */
1228 	if (!(shp->shm_perm.mode & SHM_LOCKED))
1229 		goto out_unlock0;
1230 	shmem_lock(shm_file, 0, shp->mlock_ucounts);
1231 	shp->shm_perm.mode &= ~SHM_LOCKED;
1232 	shp->mlock_ucounts = NULL;
1233 	get_file(shm_file);
1234 	ipc_unlock_object(&shp->shm_perm);
1235 	rcu_read_unlock();
1236 	shmem_unlock_mapping(shm_file->f_mapping);
1237 
1238 	fput(shm_file);
1239 	return err;
1240 
1241 out_unlock0:
1242 	ipc_unlock_object(&shp->shm_perm);
1243 out_unlock1:
1244 	rcu_read_unlock();
1245 	return err;
1246 }
1247 
ksys_shmctl(int shmid,int cmd,struct shmid_ds __user * buf,int version)1248 static long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf, int version)
1249 {
1250 	int err;
1251 	struct ipc_namespace *ns;
1252 	struct shmid64_ds sem64;
1253 
1254 	if (cmd < 0 || shmid < 0)
1255 		return -EINVAL;
1256 
1257 	ns = current->nsproxy->ipc_ns;
1258 
1259 	switch (cmd) {
1260 	case IPC_INFO: {
1261 		struct shminfo64 shminfo;
1262 		err = shmctl_ipc_info(ns, &shminfo);
1263 		if (err < 0)
1264 			return err;
1265 		if (copy_shminfo_to_user(buf, &shminfo, version))
1266 			err = -EFAULT;
1267 		return err;
1268 	}
1269 	case SHM_INFO: {
1270 		struct shm_info shm_info;
1271 		err = shmctl_shm_info(ns, &shm_info);
1272 		if (err < 0)
1273 			return err;
1274 		if (copy_to_user(buf, &shm_info, sizeof(shm_info)))
1275 			err = -EFAULT;
1276 		return err;
1277 	}
1278 	case SHM_STAT:
1279 	case SHM_STAT_ANY:
1280 	case IPC_STAT: {
1281 		err = shmctl_stat(ns, shmid, cmd, &sem64);
1282 		if (err < 0)
1283 			return err;
1284 		if (copy_shmid_to_user(buf, &sem64, version))
1285 			err = -EFAULT;
1286 		return err;
1287 	}
1288 	case IPC_SET:
1289 		if (copy_shmid_from_user(&sem64, buf, version))
1290 			return -EFAULT;
1291 		fallthrough;
1292 	case IPC_RMID:
1293 		return shmctl_down(ns, shmid, cmd, &sem64);
1294 	case SHM_LOCK:
1295 	case SHM_UNLOCK:
1296 		return shmctl_do_lock(ns, shmid, cmd);
1297 	default:
1298 		return -EINVAL;
1299 	}
1300 }
1301 
SYSCALL_DEFINE3(shmctl,int,shmid,int,cmd,struct shmid_ds __user *,buf)1302 SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
1303 {
1304 	return ksys_shmctl(shmid, cmd, buf, IPC_64);
1305 }
1306 
1307 #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
ksys_old_shmctl(int shmid,int cmd,struct shmid_ds __user * buf)1308 long ksys_old_shmctl(int shmid, int cmd, struct shmid_ds __user *buf)
1309 {
1310 	int version = ipc_parse_version(&cmd);
1311 
1312 	return ksys_shmctl(shmid, cmd, buf, version);
1313 }
1314 
SYSCALL_DEFINE3(old_shmctl,int,shmid,int,cmd,struct shmid_ds __user *,buf)1315 SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
1316 {
1317 	return ksys_old_shmctl(shmid, cmd, buf);
1318 }
1319 #endif
1320 
1321 #ifdef CONFIG_COMPAT
1322 
1323 struct compat_shmid_ds {
1324 	struct compat_ipc_perm shm_perm;
1325 	int shm_segsz;
1326 	old_time32_t shm_atime;
1327 	old_time32_t shm_dtime;
1328 	old_time32_t shm_ctime;
1329 	compat_ipc_pid_t shm_cpid;
1330 	compat_ipc_pid_t shm_lpid;
1331 	unsigned short shm_nattch;
1332 	unsigned short shm_unused;
1333 	compat_uptr_t shm_unused2;
1334 	compat_uptr_t shm_unused3;
1335 };
1336 
1337 struct compat_shminfo64 {
1338 	compat_ulong_t shmmax;
1339 	compat_ulong_t shmmin;
1340 	compat_ulong_t shmmni;
1341 	compat_ulong_t shmseg;
1342 	compat_ulong_t shmall;
1343 	compat_ulong_t __unused1;
1344 	compat_ulong_t __unused2;
1345 	compat_ulong_t __unused3;
1346 	compat_ulong_t __unused4;
1347 };
1348 
1349 struct compat_shm_info {
1350 	compat_int_t used_ids;
1351 	compat_ulong_t shm_tot, shm_rss, shm_swp;
1352 	compat_ulong_t swap_attempts, swap_successes;
1353 };
1354 
copy_compat_shminfo_to_user(void __user * buf,struct shminfo64 * in,int version)1355 static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
1356 					int version)
1357 {
1358 	if (in->shmmax > INT_MAX)
1359 		in->shmmax = INT_MAX;
1360 	if (version == IPC_64) {
1361 		struct compat_shminfo64 info;
1362 		memset(&info, 0, sizeof(info));
1363 		info.shmmax = in->shmmax;
1364 		info.shmmin = in->shmmin;
1365 		info.shmmni = in->shmmni;
1366 		info.shmseg = in->shmseg;
1367 		info.shmall = in->shmall;
1368 		return copy_to_user(buf, &info, sizeof(info));
1369 	} else {
1370 		struct shminfo info;
1371 		memset(&info, 0, sizeof(info));
1372 		info.shmmax = in->shmmax;
1373 		info.shmmin = in->shmmin;
1374 		info.shmmni = in->shmmni;
1375 		info.shmseg = in->shmseg;
1376 		info.shmall = in->shmall;
1377 		return copy_to_user(buf, &info, sizeof(info));
1378 	}
1379 }
1380 
put_compat_shm_info(struct shm_info * ip,struct compat_shm_info __user * uip)1381 static int put_compat_shm_info(struct shm_info *ip,
1382 				struct compat_shm_info __user *uip)
1383 {
1384 	struct compat_shm_info info;
1385 
1386 	memset(&info, 0, sizeof(info));
1387 	info.used_ids = ip->used_ids;
1388 	info.shm_tot = ip->shm_tot;
1389 	info.shm_rss = ip->shm_rss;
1390 	info.shm_swp = ip->shm_swp;
1391 	info.swap_attempts = ip->swap_attempts;
1392 	info.swap_successes = ip->swap_successes;
1393 	return copy_to_user(uip, &info, sizeof(info));
1394 }
1395 
copy_compat_shmid_to_user(void __user * buf,struct shmid64_ds * in,int version)1396 static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in,
1397 					int version)
1398 {
1399 	if (version == IPC_64) {
1400 		struct compat_shmid64_ds v;
1401 		memset(&v, 0, sizeof(v));
1402 		to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm);
1403 		v.shm_atime	 = lower_32_bits(in->shm_atime);
1404 		v.shm_atime_high = upper_32_bits(in->shm_atime);
1405 		v.shm_dtime	 = lower_32_bits(in->shm_dtime);
1406 		v.shm_dtime_high = upper_32_bits(in->shm_dtime);
1407 		v.shm_ctime	 = lower_32_bits(in->shm_ctime);
1408 		v.shm_ctime_high = upper_32_bits(in->shm_ctime);
1409 		v.shm_segsz = in->shm_segsz;
1410 		v.shm_nattch = in->shm_nattch;
1411 		v.shm_cpid = in->shm_cpid;
1412 		v.shm_lpid = in->shm_lpid;
1413 		return copy_to_user(buf, &v, sizeof(v));
1414 	} else {
1415 		struct compat_shmid_ds v;
1416 		memset(&v, 0, sizeof(v));
1417 		to_compat_ipc_perm(&v.shm_perm, &in->shm_perm);
1418 		v.shm_perm.key = in->shm_perm.key;
1419 		v.shm_atime = in->shm_atime;
1420 		v.shm_dtime = in->shm_dtime;
1421 		v.shm_ctime = in->shm_ctime;
1422 		v.shm_segsz = in->shm_segsz;
1423 		v.shm_nattch = in->shm_nattch;
1424 		v.shm_cpid = in->shm_cpid;
1425 		v.shm_lpid = in->shm_lpid;
1426 		return copy_to_user(buf, &v, sizeof(v));
1427 	}
1428 }
1429 
copy_compat_shmid_from_user(struct shmid64_ds * out,void __user * buf,int version)1430 static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf,
1431 					int version)
1432 {
1433 	memset(out, 0, sizeof(*out));
1434 	if (version == IPC_64) {
1435 		struct compat_shmid64_ds __user *p = buf;
1436 		return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm);
1437 	} else {
1438 		struct compat_shmid_ds __user *p = buf;
1439 		return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm);
1440 	}
1441 }
1442 
compat_ksys_shmctl(int shmid,int cmd,void __user * uptr,int version)1443 static long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr, int version)
1444 {
1445 	struct ipc_namespace *ns;
1446 	struct shmid64_ds sem64;
1447 	int err;
1448 
1449 	ns = current->nsproxy->ipc_ns;
1450 
1451 	if (cmd < 0 || shmid < 0)
1452 		return -EINVAL;
1453 
1454 	switch (cmd) {
1455 	case IPC_INFO: {
1456 		struct shminfo64 shminfo;
1457 		err = shmctl_ipc_info(ns, &shminfo);
1458 		if (err < 0)
1459 			return err;
1460 		if (copy_compat_shminfo_to_user(uptr, &shminfo, version))
1461 			err = -EFAULT;
1462 		return err;
1463 	}
1464 	case SHM_INFO: {
1465 		struct shm_info shm_info;
1466 		err = shmctl_shm_info(ns, &shm_info);
1467 		if (err < 0)
1468 			return err;
1469 		if (put_compat_shm_info(&shm_info, uptr))
1470 			err = -EFAULT;
1471 		return err;
1472 	}
1473 	case IPC_STAT:
1474 	case SHM_STAT_ANY:
1475 	case SHM_STAT:
1476 		err = shmctl_stat(ns, shmid, cmd, &sem64);
1477 		if (err < 0)
1478 			return err;
1479 		if (copy_compat_shmid_to_user(uptr, &sem64, version))
1480 			err = -EFAULT;
1481 		return err;
1482 
1483 	case IPC_SET:
1484 		if (copy_compat_shmid_from_user(&sem64, uptr, version))
1485 			return -EFAULT;
1486 		fallthrough;
1487 	case IPC_RMID:
1488 		return shmctl_down(ns, shmid, cmd, &sem64);
1489 	case SHM_LOCK:
1490 	case SHM_UNLOCK:
1491 		return shmctl_do_lock(ns, shmid, cmd);
1492 	default:
1493 		return -EINVAL;
1494 	}
1495 	return err;
1496 }
1497 
COMPAT_SYSCALL_DEFINE3(shmctl,int,shmid,int,cmd,void __user *,uptr)1498 COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr)
1499 {
1500 	return compat_ksys_shmctl(shmid, cmd, uptr, IPC_64);
1501 }
1502 
1503 #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
compat_ksys_old_shmctl(int shmid,int cmd,void __user * uptr)1504 long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr)
1505 {
1506 	int version = compat_ipc_parse_version(&cmd);
1507 
1508 	return compat_ksys_shmctl(shmid, cmd, uptr, version);
1509 }
1510 
COMPAT_SYSCALL_DEFINE3(old_shmctl,int,shmid,int,cmd,void __user *,uptr)1511 COMPAT_SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, void __user *, uptr)
1512 {
1513 	return compat_ksys_old_shmctl(shmid, cmd, uptr);
1514 }
1515 #endif
1516 #endif
1517 
1518 /*
1519  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
1520  *
1521  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
1522  * "raddr" thing points to kernel space, and there has to be a wrapper around
1523  * this.
1524  */
do_shmat(int shmid,char __user * shmaddr,int shmflg,ulong * raddr,unsigned long shmlba)1525 long do_shmat(int shmid, char __user *shmaddr, int shmflg,
1526 	      ulong *raddr, unsigned long shmlba)
1527 {
1528 	struct shmid_kernel *shp;
1529 	unsigned long addr = (unsigned long)shmaddr;
1530 	unsigned long size;
1531 	struct file *file, *base;
1532 	int    err;
1533 	unsigned long flags = MAP_SHARED;
1534 	unsigned long prot;
1535 	int acc_mode;
1536 	struct ipc_namespace *ns;
1537 	struct shm_file_data *sfd;
1538 	int f_flags;
1539 	unsigned long populate = 0;
1540 
1541 	err = -EINVAL;
1542 	if (shmid < 0)
1543 		goto out;
1544 
1545 	if (addr) {
1546 		if (addr & (shmlba - 1)) {
1547 			if (shmflg & SHM_RND) {
1548 				addr &= ~(shmlba - 1);  /* round down */
1549 
1550 				/*
1551 				 * Ensure that the round-down is non-nil
1552 				 * when remapping. This can happen for
1553 				 * cases when addr < shmlba.
1554 				 */
1555 				if (!addr && (shmflg & SHM_REMAP))
1556 					goto out;
1557 			} else
1558 #ifndef __ARCH_FORCE_SHMLBA
1559 				if (addr & ~PAGE_MASK)
1560 #endif
1561 					goto out;
1562 		}
1563 
1564 		flags |= MAP_FIXED;
1565 	} else if ((shmflg & SHM_REMAP))
1566 		goto out;
1567 
1568 	if (shmflg & SHM_RDONLY) {
1569 		prot = PROT_READ;
1570 		acc_mode = S_IRUGO;
1571 		f_flags = O_RDONLY;
1572 	} else {
1573 		prot = PROT_READ | PROT_WRITE;
1574 		acc_mode = S_IRUGO | S_IWUGO;
1575 		f_flags = O_RDWR;
1576 	}
1577 	if (shmflg & SHM_EXEC) {
1578 		prot |= PROT_EXEC;
1579 		acc_mode |= S_IXUGO;
1580 	}
1581 
1582 	/*
1583 	 * We cannot rely on the fs check since SYSV IPC does have an
1584 	 * additional creator id...
1585 	 */
1586 	ns = current->nsproxy->ipc_ns;
1587 	rcu_read_lock();
1588 	shp = shm_obtain_object_check(ns, shmid);
1589 	if (IS_ERR(shp)) {
1590 		err = PTR_ERR(shp);
1591 		goto out_unlock;
1592 	}
1593 
1594 	err = -EACCES;
1595 	if (ipcperms(ns, &shp->shm_perm, acc_mode))
1596 		goto out_unlock;
1597 
1598 	err = security_shm_shmat(&shp->shm_perm, shmaddr, shmflg);
1599 	if (err)
1600 		goto out_unlock;
1601 
1602 	ipc_lock_object(&shp->shm_perm);
1603 
1604 	/* check if shm_destroy() is tearing down shp */
1605 	if (!ipc_valid_object(&shp->shm_perm)) {
1606 		ipc_unlock_object(&shp->shm_perm);
1607 		err = -EIDRM;
1608 		goto out_unlock;
1609 	}
1610 
1611 	/*
1612 	 * We need to take a reference to the real shm file to prevent the
1613 	 * pointer from becoming stale in cases where the lifetime of the outer
1614 	 * file extends beyond that of the shm segment.  It's not usually
1615 	 * possible, but it can happen during remap_file_pages() emulation as
1616 	 * that unmaps the memory, then does ->mmap() via file reference only.
1617 	 * We'll deny the ->mmap() if the shm segment was since removed, but to
1618 	 * detect shm ID reuse we need to compare the file pointers.
1619 	 */
1620 	base = get_file(shp->shm_file);
1621 	shp->shm_nattch++;
1622 	size = i_size_read(file_inode(base));
1623 	ipc_unlock_object(&shp->shm_perm);
1624 	rcu_read_unlock();
1625 
1626 	err = -ENOMEM;
1627 	sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
1628 	if (!sfd) {
1629 		fput(base);
1630 		goto out_nattch;
1631 	}
1632 
1633 	file = alloc_file_clone(base, f_flags,
1634 			  is_file_hugepages(base) ?
1635 				&shm_file_operations_huge :
1636 				&shm_file_operations);
1637 	err = PTR_ERR(file);
1638 	if (IS_ERR(file)) {
1639 		kfree(sfd);
1640 		fput(base);
1641 		goto out_nattch;
1642 	}
1643 
1644 	sfd->id = shp->shm_perm.id;
1645 	sfd->ns = get_ipc_ns(ns);
1646 	sfd->file = base;
1647 	sfd->vm_ops = NULL;
1648 	file->private_data = sfd;
1649 
1650 	err = security_mmap_file(file, prot, flags);
1651 	if (err)
1652 		goto out_fput;
1653 
1654 	if (mmap_write_lock_killable(current->mm)) {
1655 		err = -EINTR;
1656 		goto out_fput;
1657 	}
1658 
1659 	if (addr && !(shmflg & SHM_REMAP)) {
1660 		err = -EINVAL;
1661 		if (addr + size < addr)
1662 			goto invalid;
1663 
1664 		if (find_vma_intersection(current->mm, addr, addr + size))
1665 			goto invalid;
1666 	}
1667 
1668 	addr = do_mmap(file, addr, size, prot, flags, 0, 0, &populate, NULL);
1669 	*raddr = addr;
1670 	err = 0;
1671 	if (IS_ERR_VALUE(addr))
1672 		err = (long)addr;
1673 invalid:
1674 	mmap_write_unlock(current->mm);
1675 	if (populate)
1676 		mm_populate(addr, populate);
1677 
1678 out_fput:
1679 	fput(file);
1680 
1681 out_nattch:
1682 	down_write(&shm_ids(ns).rwsem);
1683 	shp = shm_lock(ns, shmid);
1684 	shp->shm_nattch--;
1685 
1686 	if (shm_may_destroy(shp))
1687 		shm_destroy(ns, shp);
1688 	else
1689 		shm_unlock(shp);
1690 	up_write(&shm_ids(ns).rwsem);
1691 	return err;
1692 
1693 out_unlock:
1694 	rcu_read_unlock();
1695 out:
1696 	return err;
1697 }
1698 
SYSCALL_DEFINE3(shmat,int,shmid,char __user *,shmaddr,int,shmflg)1699 SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
1700 {
1701 	unsigned long ret;
1702 	long err;
1703 
1704 	err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
1705 	if (err)
1706 		return err;
1707 	force_successful_syscall_return();
1708 	return (long)ret;
1709 }
1710 
1711 #ifdef CONFIG_COMPAT
1712 
1713 #ifndef COMPAT_SHMLBA
1714 #define COMPAT_SHMLBA	SHMLBA
1715 #endif
1716 
COMPAT_SYSCALL_DEFINE3(shmat,int,shmid,compat_uptr_t,shmaddr,int,shmflg)1717 COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg)
1718 {
1719 	unsigned long ret;
1720 	long err;
1721 
1722 	err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
1723 	if (err)
1724 		return err;
1725 	force_successful_syscall_return();
1726 	return (long)ret;
1727 }
1728 #endif
1729 
1730 /*
1731  * detach and kill segment if marked destroyed.
1732  * The work is done in shm_close.
1733  */
ksys_shmdt(char __user * shmaddr)1734 long ksys_shmdt(char __user *shmaddr)
1735 {
1736 	struct mm_struct *mm = current->mm;
1737 	struct vm_area_struct *vma;
1738 	unsigned long addr = (unsigned long)shmaddr;
1739 	int retval = -EINVAL;
1740 #ifdef CONFIG_MMU
1741 	loff_t size = 0;
1742 	struct file *file;
1743 	VMA_ITERATOR(vmi, mm, addr);
1744 #endif
1745 
1746 	if (addr & ~PAGE_MASK)
1747 		return retval;
1748 
1749 	if (mmap_write_lock_killable(mm))
1750 		return -EINTR;
1751 
1752 	/*
1753 	 * This function tries to be smart and unmap shm segments that
1754 	 * were modified by partial mlock or munmap calls:
1755 	 * - It first determines the size of the shm segment that should be
1756 	 *   unmapped: It searches for a vma that is backed by shm and that
1757 	 *   started at address shmaddr. It records it's size and then unmaps
1758 	 *   it.
1759 	 * - Then it unmaps all shm vmas that started at shmaddr and that
1760 	 *   are within the initially determined size and that are from the
1761 	 *   same shm segment from which we determined the size.
1762 	 * Errors from do_munmap are ignored: the function only fails if
1763 	 * it's called with invalid parameters or if it's called to unmap
1764 	 * a part of a vma. Both calls in this function are for full vmas,
1765 	 * the parameters are directly copied from the vma itself and always
1766 	 * valid - therefore do_munmap cannot fail. (famous last words?)
1767 	 */
1768 	/*
1769 	 * If it had been mremap()'d, the starting address would not
1770 	 * match the usual checks anyway. So assume all vma's are
1771 	 * above the starting address given.
1772 	 */
1773 
1774 #ifdef CONFIG_MMU
1775 	for_each_vma(vmi, vma) {
1776 		/*
1777 		 * Check if the starting address would match, i.e. it's
1778 		 * a fragment created by mprotect() and/or munmap(), or it
1779 		 * otherwise it starts at this address with no hassles.
1780 		 */
1781 		if ((vma->vm_ops == &shm_vm_ops) &&
1782 			(vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1783 
1784 			/*
1785 			 * Record the file of the shm segment being
1786 			 * unmapped.  With mremap(), someone could place
1787 			 * page from another segment but with equal offsets
1788 			 * in the range we are unmapping.
1789 			 */
1790 			file = vma->vm_file;
1791 			size = i_size_read(file_inode(vma->vm_file));
1792 			do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end,
1793 				      NULL, false);
1794 			/*
1795 			 * We discovered the size of the shm segment, so
1796 			 * break out of here and fall through to the next
1797 			 * loop that uses the size information to stop
1798 			 * searching for matching vma's.
1799 			 */
1800 			retval = 0;
1801 			vma = vma_next(&vmi);
1802 			break;
1803 		}
1804 	}
1805 
1806 	/*
1807 	 * We need look no further than the maximum address a fragment
1808 	 * could possibly have landed at. Also cast things to loff_t to
1809 	 * prevent overflows and make comparisons vs. equal-width types.
1810 	 */
1811 	size = PAGE_ALIGN(size);
1812 	while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1813 		/* finding a matching vma now does not alter retval */
1814 		if ((vma->vm_ops == &shm_vm_ops) &&
1815 		    ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
1816 		    (vma->vm_file == file)) {
1817 			do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end,
1818 				      NULL, false);
1819 		}
1820 
1821 		vma = vma_next(&vmi);
1822 	}
1823 
1824 #else	/* CONFIG_MMU */
1825 	vma = vma_lookup(mm, addr);
1826 	/* under NOMMU conditions, the exact address to be destroyed must be
1827 	 * given
1828 	 */
1829 	if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
1830 		do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1831 		retval = 0;
1832 	}
1833 
1834 #endif
1835 
1836 	mmap_write_unlock(mm);
1837 	return retval;
1838 }
1839 
SYSCALL_DEFINE1(shmdt,char __user *,shmaddr)1840 SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1841 {
1842 	return ksys_shmdt(shmaddr);
1843 }
1844 
1845 #ifdef CONFIG_PROC_FS
sysvipc_shm_proc_show(struct seq_file * s,void * it)1846 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1847 {
1848 	struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
1849 	struct user_namespace *user_ns = seq_user_ns(s);
1850 	struct kern_ipc_perm *ipcp = it;
1851 	struct shmid_kernel *shp;
1852 	unsigned long rss = 0, swp = 0;
1853 
1854 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
1855 	shm_add_rss_swap(shp, &rss, &swp);
1856 
1857 #if BITS_PER_LONG <= 32
1858 #define SIZE_SPEC "%10lu"
1859 #else
1860 #define SIZE_SPEC "%21lu"
1861 #endif
1862 
1863 	seq_printf(s,
1864 		   "%10d %10d  %4o " SIZE_SPEC " %5u %5u  "
1865 		   "%5lu %5u %5u %5u %5u %10llu %10llu %10llu "
1866 		   SIZE_SPEC " " SIZE_SPEC "\n",
1867 		   shp->shm_perm.key,
1868 		   shp->shm_perm.id,
1869 		   shp->shm_perm.mode,
1870 		   shp->shm_segsz,
1871 		   pid_nr_ns(shp->shm_cprid, pid_ns),
1872 		   pid_nr_ns(shp->shm_lprid, pid_ns),
1873 		   shp->shm_nattch,
1874 		   from_kuid_munged(user_ns, shp->shm_perm.uid),
1875 		   from_kgid_munged(user_ns, shp->shm_perm.gid),
1876 		   from_kuid_munged(user_ns, shp->shm_perm.cuid),
1877 		   from_kgid_munged(user_ns, shp->shm_perm.cgid),
1878 		   shp->shm_atim,
1879 		   shp->shm_dtim,
1880 		   shp->shm_ctim,
1881 		   rss * PAGE_SIZE,
1882 		   swp * PAGE_SIZE);
1883 
1884 	return 0;
1885 }
1886 #endif
1887