License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* linux/ipc/shm.c
|
|
|
|
* Copyright (C) 1992, 1993 Krishna Balasubramanian
|
|
|
|
* Many improvements/fixes by Bruno Haible.
|
|
|
|
* Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
|
|
|
|
* Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
|
|
|
|
*
|
|
|
|
* /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
|
|
|
|
* BIGMEM support, Andrea Arcangeli <andrea@suse.de>
|
|
|
|
* SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
|
|
|
|
* HIGHMEM support, Ingo Molnar <mingo@redhat.com>
|
|
|
|
* Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
|
|
|
|
* Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
|
|
|
|
* Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
|
|
|
|
*
|
2006-04-02 21:07:33 +00:00
|
|
|
* support for audit of ipc object properties and permission changes
|
|
|
|
* Dustin Kirkland <dustin.kirkland@us.ibm.com>
|
2006-10-02 09:18:22 +00:00
|
|
|
*
|
|
|
|
* namespaces support
|
|
|
|
* OpenVZ, SWsoft Inc.
|
|
|
|
* Pavel Emelianov <xemul@openvz.org>
|
2013-09-11 21:26:23 +00:00
|
|
|
*
|
|
|
|
* Better ipc lock (kern_ipc_perm.lock) handling
|
|
|
|
* Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/hugetlb.h>
|
|
|
|
#include <linux/shm.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/mman.h>
|
|
|
|
#include <linux/shmem_fs.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/audit.h>
|
2006-01-11 20:17:46 +00:00
|
|
|
#include <linux/capability.h>
|
2005-05-01 15:59:12 +00:00
|
|
|
#include <linux/ptrace.h>
|
2005-09-06 22:17:10 +00:00
|
|
|
#include <linux/seq_file.h>
|
2007-10-19 06:40:54 +00:00
|
|
|
#include <linux/rwsem.h>
|
2006-10-02 09:18:22 +00:00
|
|
|
#include <linux/nsproxy.h>
|
2007-02-20 21:57:53 +00:00
|
|
|
#include <linux/mount.h>
|
2008-02-08 12:18:22 +00:00
|
|
|
#include <linux/ipc_namespace.h>
|
2018-06-18 02:52:50 +00:00
|
|
|
#include <linux/rhashtable.h>
|
2005-05-01 15:59:12 +00:00
|
|
|
|
2014-06-06 21:37:37 +00:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2018-03-23 02:34:44 +00:00
|
|
|
struct shmid_kernel /* private to the kernel */
|
|
|
|
{
|
|
|
|
struct kern_ipc_perm shm_perm;
|
|
|
|
struct file *shm_file;
|
|
|
|
unsigned long shm_nattch;
|
|
|
|
unsigned long shm_segsz;
|
|
|
|
time64_t shm_atim;
|
|
|
|
time64_t shm_dtim;
|
|
|
|
time64_t shm_ctim;
|
2018-03-23 05:29:57 +00:00
|
|
|
struct pid *shm_cprid;
|
|
|
|
struct pid *shm_lprid;
|
2018-03-23 02:34:44 +00:00
|
|
|
struct user_struct *mlock_user;
|
|
|
|
|
|
|
|
/* The task created the shm object. NULL if the task is dead. */
|
|
|
|
struct task_struct *shm_creator;
|
|
|
|
struct list_head shm_clist; /* list by creator */
|
|
|
|
} __randomize_layout;
|
|
|
|
|
|
|
|
/* shm_mode upper byte flags */
|
|
|
|
#define SHM_DEST 01000 /* segment will be destroyed on last detach */
|
|
|
|
#define SHM_LOCKED 02000 /* segment will not be swapped */
|
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
struct shm_file_data {
|
|
|
|
int id;
|
|
|
|
struct ipc_namespace *ns;
|
|
|
|
struct file *file;
|
|
|
|
const struct vm_operations_struct *vm_ops;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
|
|
|
|
|
2007-02-12 08:55:35 +00:00
|
|
|
static const struct file_operations shm_file_operations;
|
2009-09-27 18:29:37 +00:00
|
|
|
static const struct vm_operations_struct shm_vm_ops;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-08 12:18:57 +00:00
|
|
|
#define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-10-02 09:18:22 +00:00
|
|
|
#define shm_unlock(shp) \
|
|
|
|
ipc_unlock(&(shp)->shm_perm)
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-19 06:40:49 +00:00
|
|
|
static int newseg(struct ipc_namespace *, struct ipc_params *);
|
2007-02-20 21:57:53 +00:00
|
|
|
static void shm_open(struct vm_area_struct *vma);
|
|
|
|
static void shm_close(struct vm_area_struct *vma);
|
2014-01-28 01:07:04 +00:00
|
|
|
static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2005-09-06 22:17:10 +00:00
|
|
|
static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
2018-08-22 05:01:56 +00:00
|
|
|
void shm_init_ns(struct ipc_namespace *ns)
|
2006-10-02 09:18:22 +00:00
|
|
|
{
|
|
|
|
ns->shm_ctlmax = SHMMAX;
|
|
|
|
ns->shm_ctlall = SHMALL;
|
|
|
|
ns->shm_ctlmni = SHMMNI;
|
2011-07-26 23:08:48 +00:00
|
|
|
ns->shm_rmid_forced = 0;
|
2006-10-02 09:18:22 +00:00
|
|
|
ns->shm_tot = 0;
|
2018-08-22 05:01:56 +00:00
|
|
|
ipc_init_ids(&shm_ids(ns));
|
2006-10-02 09:18:22 +00:00
|
|
|
}
|
|
|
|
|
2007-10-19 06:40:53 +00:00
|
|
|
/*
|
2013-09-11 21:26:24 +00:00
|
|
|
* Called with shm_ids.rwsem (writer) and the shp structure locked.
|
|
|
|
* Only shm_ids.rwsem remains locked on exit.
|
2007-10-19 06:40:53 +00:00
|
|
|
*/
|
2008-02-08 12:18:57 +00:00
|
|
|
static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
|
2006-10-02 09:18:22 +00:00
|
|
|
{
|
2008-02-08 12:18:57 +00:00
|
|
|
struct shmid_kernel *shp;
|
2016-12-14 23:06:10 +00:00
|
|
|
|
2008-02-08 12:18:57 +00:00
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
|
2014-01-28 01:07:04 +00:00
|
|
|
if (shp->shm_nattch) {
|
2006-10-02 09:18:22 +00:00
|
|
|
shp->shm_perm.mode |= SHM_DEST;
|
|
|
|
/* Do not find it any more */
|
ipc: optimize semget/shmget/msgget for lots of keys
ipc_findkey() used to scan all objects to look for the wanted key. This
is slow when using a high number of keys. This change adds an rhashtable
of kern_ipc_perm objects in ipc_ids, so that one lookup cease to be O(n).
This change gives a 865% improvement of benchmark reaim.jobs_per_min on a
56 threads Intel(R) Xeon(R) CPU E5-2695 v3 @ 2.30GHz with 256G memory [1]
Other (more micro) benchmark results, by the author: On an i5 laptop, the
following loop executed right after a reboot took, without and with this
change:
for (int i = 0, k=0x424242; i < KEYS; ++i)
semget(k++, 1, IPC_CREAT | 0600);
total total max single max single
KEYS without with call without call with
1 3.5 4.9 µs 3.5 4.9
10 7.6 8.6 µs 3.7 4.7
32 16.2 15.9 µs 4.3 5.3
100 72.9 41.8 µs 3.7 4.7
1000 5,630.0 502.0 µs * *
10000 1,340,000.0 7,240.0 µs * *
31900 17,600,000.0 22,200.0 µs * *
*: unreliable measure: high variance
The duration for a lookup-only usage was obtained by the same loop once
the keys are present:
total total max single max single
KEYS without with call without call with
1 2.1 2.5 µs 2.1 2.5
10 4.5 4.8 µs 2.2 2.3
32 13.0 10.8 µs 2.3 2.8
100 82.9 25.1 µs * 2.3
1000 5,780.0 217.0 µs * *
10000 1,470,000.0 2,520.0 µs * *
31900 17,400,000.0 7,810.0 µs * *
Finally, executing each semget() in a new process gave, when still
summing only the durations of these syscalls:
creation:
total total
KEYS without with
1 3.7 5.0 µs
10 32.9 36.7 µs
32 125.0 109.0 µs
100 523.0 353.0 µs
1000 20,300.0 3,280.0 µs
10000 2,470,000.0 46,700.0 µs
31900 27,800,000.0 219,000.0 µs
lookup-only:
total total
KEYS without with
1 2.5 2.7 µs
10 25.4 24.4 µs
32 106.0 72.6 µs
100 591.0 352.0 µs
1000 22,400.0 2,250.0 µs
10000 2,510,000.0 25,700.0 µs
31900 28,200,000.0 115,000.0 µs
[1] http://lkml.kernel.org/r/20170814060507.GE23258@yexl-desktop
Link: http://lkml.kernel.org/r/20170815194954.ck32ta2z35yuzpwp@debix
Signed-off-by: Guillaume Knispel <guillaume.knispel@supersonicimagine.com>
Reviewed-by: Marc Pardo <marc.pardo@supersonicimagine.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Andrey Vagin <avagin@openvz.org>
Cc: Guillaume Knispel <guillaume.knispel@supersonicimagine.com>
Cc: Marc Pardo <marc.pardo@supersonicimagine.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-09-08 23:17:55 +00:00
|
|
|
ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
|
2006-10-02 09:18:22 +00:00
|
|
|
shm_unlock(shp);
|
|
|
|
} else
|
|
|
|
shm_destroy(ns, shp);
|
|
|
|
}
|
|
|
|
|
2008-02-08 12:18:22 +00:00
|
|
|
#ifdef CONFIG_IPC_NS
|
2006-10-02 09:18:22 +00:00
|
|
|
void shm_exit_ns(struct ipc_namespace *ns)
|
|
|
|
{
|
2008-02-08 12:18:57 +00:00
|
|
|
free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
|
2009-12-16 00:47:27 +00:00
|
|
|
idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
|
ipc: optimize semget/shmget/msgget for lots of keys
ipc_findkey() used to scan all objects to look for the wanted key. This
is slow when using a high number of keys. This change adds an rhashtable
of kern_ipc_perm objects in ipc_ids, so that one lookup cease to be O(n).
This change gives a 865% improvement of benchmark reaim.jobs_per_min on a
56 threads Intel(R) Xeon(R) CPU E5-2695 v3 @ 2.30GHz with 256G memory [1]
Other (more micro) benchmark results, by the author: On an i5 laptop, the
following loop executed right after a reboot took, without and with this
change:
for (int i = 0, k=0x424242; i < KEYS; ++i)
semget(k++, 1, IPC_CREAT | 0600);
total total max single max single
KEYS without with call without call with
1 3.5 4.9 µs 3.5 4.9
10 7.6 8.6 µs 3.7 4.7
32 16.2 15.9 µs 4.3 5.3
100 72.9 41.8 µs 3.7 4.7
1000 5,630.0 502.0 µs * *
10000 1,340,000.0 7,240.0 µs * *
31900 17,600,000.0 22,200.0 µs * *
*: unreliable measure: high variance
The duration for a lookup-only usage was obtained by the same loop once
the keys are present:
total total max single max single
KEYS without with call without call with
1 2.1 2.5 µs 2.1 2.5
10 4.5 4.8 µs 2.2 2.3
32 13.0 10.8 µs 2.3 2.8
100 82.9 25.1 µs * 2.3
1000 5,780.0 217.0 µs * *
10000 1,470,000.0 2,520.0 µs * *
31900 17,400,000.0 7,810.0 µs * *
Finally, executing each semget() in a new process gave, when still
summing only the durations of these syscalls:
creation:
total total
KEYS without with
1 3.7 5.0 µs
10 32.9 36.7 µs
32 125.0 109.0 µs
100 523.0 353.0 µs
1000 20,300.0 3,280.0 µs
10000 2,470,000.0 46,700.0 µs
31900 27,800,000.0 219,000.0 µs
lookup-only:
total total
KEYS without with
1 2.5 2.7 µs
10 25.4 24.4 µs
32 106.0 72.6 µs
100 591.0 352.0 µs
1000 22,400.0 2,250.0 µs
10000 2,510,000.0 25,700.0 µs
31900 28,200,000.0 115,000.0 µs
[1] http://lkml.kernel.org/r/20170814060507.GE23258@yexl-desktop
Link: http://lkml.kernel.org/r/20170815194954.ck32ta2z35yuzpwp@debix
Signed-off-by: Guillaume Knispel <guillaume.knispel@supersonicimagine.com>
Reviewed-by: Marc Pardo <marc.pardo@supersonicimagine.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Andrey Vagin <avagin@openvz.org>
Cc: Guillaume Knispel <guillaume.knispel@supersonicimagine.com>
Cc: Marc Pardo <marc.pardo@supersonicimagine.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-09-08 23:17:55 +00:00
|
|
|
rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
|
2006-10-02 09:18:22 +00:00
|
|
|
}
|
2008-02-08 12:18:22 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-08-05 05:35:59 +00:00
|
|
|
static int __init ipc_ns_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-08-22 05:01:56 +00:00
|
|
|
shm_init_ns(&init_ipc_ns);
|
|
|
|
return 0;
|
2011-08-05 05:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pure_initcall(ipc_ns_init);
|
|
|
|
|
2014-01-28 01:07:04 +00:00
|
|
|
void __init shm_init(void)
|
2011-08-05 05:35:59 +00:00
|
|
|
{
|
2005-09-06 22:17:10 +00:00
|
|
|
ipc_init_proc_interface("sysvipc/shm",
|
2010-10-27 22:34:16 +00:00
|
|
|
#if BITS_PER_LONG <= 32
|
|
|
|
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
|
|
|
|
#else
|
|
|
|
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
|
|
|
|
#endif
|
2006-10-02 09:18:22 +00:00
|
|
|
IPC_SHM_IDS, sysvipc_shm_proc_show);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
ipc,shm: introduce lockless functions to obtain the ipc object
This is the third and final patchset that deals with reducing the amount
of contention we impose on the ipc lock (kern_ipc_perm.lock). These
changes mostly deal with shared memory, previous work has already been
done for semaphores and message queues:
http://lkml.org/lkml/2013/3/20/546 (sems)
http://lkml.org/lkml/2013/5/15/584 (mqueues)
With these patches applied, a custom shm microbenchmark stressing shmctl
doing IPC_STAT with 4 threads a million times, reduces the execution
time by 50%. A similar run, this time with IPC_SET, reduces the
execution time from 3 mins and 35 secs to 27 seconds.
Patches 1-8: replaces blindly taking the ipc lock for a smarter
combination of rcu and ipc_obtain_object, only acquiring the spinlock
when updating.
Patch 9: renames the ids rw_mutex to rwsem, which is what it already was.
Patch 10: is a trivial mqueue leftover cleanup
Patch 11: adds a brief lock scheme description, requested by Andrew.
This patch:
Add shm_obtain_object() and shm_obtain_object_check(), which will allow us
to get the ipc object without acquiring the lock. Just as with other
forms of ipc, these functions are basically wrappers around
ipc_obtain_object*().
Signed-off-by: Davidlohr Bueso <davidlohr.bueso@hp.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-09-11 21:26:15 +00:00
|
|
|
static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
|
|
|
|
{
|
2015-06-30 21:58:42 +00:00
|
|
|
struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
|
ipc,shm: introduce lockless functions to obtain the ipc object
This is the third and final patchset that deals with reducing the amount
of contention we impose on the ipc lock (kern_ipc_perm.lock). These
changes mostly deal with shared memory, previous work has already been
done for semaphores and message queues:
http://lkml.org/lkml/2013/3/20/546 (sems)
http://lkml.org/lkml/2013/5/15/584 (mqueues)
With these patches applied, a custom shm microbenchmark stressing shmctl
doing IPC_STAT with 4 threads a million times, reduces the execution
time by 50%. A similar run, this time with IPC_SET, reduces the
execution time from 3 mins and 35 secs to 27 seconds.
Patches 1-8: replaces blindly taking the ipc lock for a smarter
combination of rcu and ipc_obtain_object, only acquiring the spinlock
when updating.
Patch 9: renames the ids rw_mutex to rwsem, which is what it already was.
Patch 10: is a trivial mqueue leftover cleanup
Patch 11: adds a brief lock scheme description, requested by Andrew.
This patch:
Add shm_obtain_object() and shm_obtain_object_check(), which will allow us
to get the ipc object without acquiring the lock. Just as with other
forms of ipc, these functions are basically wrappers around
ipc_obtain_object*().
Signed-off-by: Davidlohr Bueso <davidlohr.bueso@hp.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-09-11 21:26:15 +00:00
|
|
|
|
|
|
|
if (IS_ERR(ipcp))
|
|
|
|
return ERR_CAST(ipcp);
|
|
|
|
|
|
|
|
return container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
|
|
|
|
{
|
|
|
|
struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
|
|
|
|
|
|
|
|
if (IS_ERR(ipcp))
|
|
|
|
return ERR_CAST(ipcp);
|
|
|
|
|
|
|
|
return container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
}
|
|
|
|
|
2007-10-19 06:40:54 +00:00
|
|
|
/*
|
2013-09-11 21:26:24 +00:00
|
|
|
* shm_lock_(check_) routines are called in the paths where the rwsem
|
2008-07-25 08:48:03 +00:00
|
|
|
* is not necessarily held.
|
2007-10-19 06:40:54 +00:00
|
|
|
*/
|
2007-10-19 06:40:51 +00:00
|
|
|
static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-08-22 05:01:41 +00:00
|
|
|
struct kern_ipc_perm *ipcp;
|
2007-10-19 06:40:51 +00:00
|
|
|
|
2018-08-22 05:01:41 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
|
|
|
|
if (IS_ERR(ipcp))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ipc_lock_object(ipcp);
|
|
|
|
/*
|
|
|
|
* ipc_rmid() may have already freed the ID while ipc_lock_object()
|
|
|
|
* was spinning: here verify that the structure is still valid.
|
|
|
|
* Upon races with RMID, return -EIDRM, thus indicating that
|
|
|
|
* the ID points to a removed identifier.
|
|
|
|
*/
|
|
|
|
if (ipc_valid_object(ipcp)) {
|
|
|
|
/* return a locked ipc object upon success */
|
|
|
|
return container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_unlock_object(ipcp);
|
2018-09-04 22:46:02 +00:00
|
|
|
ipcp = ERR_PTR(-EIDRM);
|
2018-08-22 05:01:41 +00:00
|
|
|
err:
|
|
|
|
rcu_read_unlock();
|
2015-06-30 21:58:36 +00:00
|
|
|
/*
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
* Callers of shm_lock() must validate the status of the returned ipc
|
2018-08-22 05:01:41 +00:00
|
|
|
* object pointer and error out as appropriate.
|
2015-06-30 21:58:36 +00:00
|
|
|
*/
|
2018-10-05 22:51:48 +00:00
|
|
|
return ERR_CAST(ipcp);
|
2007-10-19 06:40:51 +00:00
|
|
|
}
|
|
|
|
|
2011-07-28 23:56:40 +00:00
|
|
|
static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
2013-07-08 23:01:11 +00:00
|
|
|
ipc_lock_object(&ipcp->shm_perm);
|
2011-07-28 23:56:40 +00:00
|
|
|
}
|
|
|
|
|
ipc: fix race with LSMs
Currently, IPC mechanisms do security and auditing related checks under
RCU. However, since security modules can free the security structure,
for example, through selinux_[sem,msg_queue,shm]_free_security(), we can
race if the structure is freed before other tasks are done with it,
creating a use-after-free condition. Manfred illustrates this nicely,
for instance with shared mem and selinux:
-> do_shmat calls rcu_read_lock()
-> do_shmat calls shm_object_check().
Checks that the object is still valid - but doesn't acquire any locks.
Then it returns.
-> do_shmat calls security_shm_shmat (e.g. selinux_shm_shmat)
-> selinux_shm_shmat calls ipc_has_perm()
-> ipc_has_perm accesses ipc_perms->security
shm_close()
-> shm_close acquires rw_mutex & shm_lock
-> shm_close calls shm_destroy
-> shm_destroy calls security_shm_free (e.g. selinux_shm_free_security)
-> selinux_shm_free_security calls ipc_free_security(&shp->shm_perm)
-> ipc_free_security calls kfree(ipc_perms->security)
This patch delays the freeing of the security structures after all RCU
readers are done. Furthermore it aligns the security life cycle with
that of the rest of IPC - freeing them based on the reference counter.
For situations where we need not free security, the current behavior is
kept. Linus states:
"... the old behavior was suspect for another reason too: having the
security blob go away from under a user sounds like it could cause
various other problems anyway, so I think the old code was at least
_prone_ to bugs even if it didn't have catastrophic behavior."
I have tested this patch with IPC testcases from LTP on both my
quad-core laptop and on a 64 core NUMA server. In both cases selinux is
enabled, and tests pass for both voluntary and forced preemption models.
While the mentioned races are theoretical (at least no one as reported
them), I wanted to make sure that this new logic doesn't break anything
we weren't aware of.
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Davidlohr Bueso <davidlohr@hp.com>
Acked-by: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-09-24 00:04:45 +00:00
|
|
|
static void shm_rcu_free(struct rcu_head *head)
|
|
|
|
{
|
2017-07-12 21:34:41 +00:00
|
|
|
struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
|
|
|
|
rcu);
|
|
|
|
struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
|
|
|
|
shm_perm);
|
2018-03-23 02:08:27 +00:00
|
|
|
security_shm_free(&shp->shm_perm);
|
2017-07-12 21:35:25 +00:00
|
|
|
kvfree(shp);
|
ipc: fix race with LSMs
Currently, IPC mechanisms do security and auditing related checks under
RCU. However, since security modules can free the security structure,
for example, through selinux_[sem,msg_queue,shm]_free_security(), we can
race if the structure is freed before other tasks are done with it,
creating a use-after-free condition. Manfred illustrates this nicely,
for instance with shared mem and selinux:
-> do_shmat calls rcu_read_lock()
-> do_shmat calls shm_object_check().
Checks that the object is still valid - but doesn't acquire any locks.
Then it returns.
-> do_shmat calls security_shm_shmat (e.g. selinux_shm_shmat)
-> selinux_shm_shmat calls ipc_has_perm()
-> ipc_has_perm accesses ipc_perms->security
shm_close()
-> shm_close acquires rw_mutex & shm_lock
-> shm_close calls shm_destroy
-> shm_destroy calls security_shm_free (e.g. selinux_shm_free_security)
-> selinux_shm_free_security calls ipc_free_security(&shp->shm_perm)
-> ipc_free_security calls kfree(ipc_perms->security)
This patch delays the freeing of the security structures after all RCU
readers are done. Furthermore it aligns the security life cycle with
that of the rest of IPC - freeing them based on the reference counter.
For situations where we need not free security, the current behavior is
kept. Linus states:
"... the old behavior was suspect for another reason too: having the
security blob go away from under a user sounds like it could cause
various other problems anyway, so I think the old code was at least
_prone_ to bugs even if it didn't have catastrophic behavior."
I have tested this patch with IPC testcases from LTP on both my
quad-core laptop and on a 64 core NUMA server. In both cases selinux is
enabled, and tests pass for both voluntary and forced preemption models.
While the mentioned races are theoretical (at least no one as reported
them), I wanted to make sure that this new logic doesn't break anything
we weren't aware of.
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Davidlohr Bueso <davidlohr@hp.com>
Acked-by: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-09-24 00:04:45 +00:00
|
|
|
}
|
|
|
|
|
2007-10-19 06:40:48 +00:00
|
|
|
static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
shm: make exit_shm work proportional to task activity
This is small set of patches our team has had kicking around for a few
versions internally that fixes tasks getting hung on shm_exit when there
are many threads hammering it at once.
Anton wrote a simple test to cause the issue:
http://ozlabs.org/~anton/junkcode/bust_shm_exit.c
Before applying this patchset, this test code will cause either hanging
tracebacks or pthread out of memory errors.
After this patchset, it will still produce output like:
root@somehost:~# ./bust_shm_exit 1024 160
...
INFO: rcu_sched detected stalls on CPUs/tasks: {} (detected by 116, t=2111 jiffies, g=241, c=240, q=7113)
INFO: Stall ended before state dump start
...
But the task will continue to run along happily, so we consider this an
improvement over hanging, even if it's a bit noisy.
This patch (of 3):
exit_shm obtains the ipc_ns shm rwsem for write and holds it while it
walks every shared memory segment in the namespace. Thus the amount of
work is related to the number of shm segments in the namespace not the
number of segments that might need to be cleaned.
In addition, this occurs after the task has been notified the thread has
exited, so the number of tasks waiting for the ns shm rwsem can grow
without bound until memory is exausted.
Add a list to the task struct of all shmids allocated by this task. Init
the list head in copy_process. Use the ns->rwsem for locking. Add
segments after id is added, remove before removing from id.
On unshare of NEW_IPCNS orphan any ids as if the task had exited, similar
to handling of semaphore undo.
I chose a define for the init sequence since its a simple list init,
otherwise it would require a function call to avoid include loops between
the semaphore code and the task struct. Converting the list_del to
list_del_init for the unshare cases would remove the exit followed by
init, but I left it blow up if not inited.
Signed-off-by: Milton Miller <miltonm@bga.com>
Signed-off-by: Jack Miller <millerjo@us.ibm.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Anton Blanchard <anton@samba.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:23:19 +00:00
|
|
|
list_del(&s->shm_clist);
|
2007-10-19 06:40:48 +00:00
|
|
|
ipc_rmid(&shm_ids(ns), &s->shm_perm);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
static int __shm_open(struct vm_area_struct *vma)
|
2006-10-02 09:18:22 +00:00
|
|
|
{
|
2007-02-20 21:57:53 +00:00
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2005-04-16 22:20:36 +00:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
shp = shm_lock(sfd->ns, sfd->id);
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
|
|
|
|
if (IS_ERR(shp))
|
|
|
|
return PTR_ERR(shp);
|
|
|
|
|
ipc/shm: fix use-after-free of shm file via remap_file_pages()
syzbot reported a use-after-free of shm_file_data(file)->file->f_op in
shm_get_unmapped_area(), called via sys_remap_file_pages().
Unfortunately it couldn't generate a reproducer, but I found a bug which
I think caused it. When remap_file_pages() is passed a full System V
shared memory segment, the memory is first unmapped, then a new map is
created using the ->vm_file. Between these steps, the shm ID can be
removed and reused for a new shm segment. But, shm_mmap() only checks
whether the ID is currently valid before calling the underlying file's
->mmap(); it doesn't check whether it was reused. Thus it can use the
wrong underlying file, one that was already freed.
Fix this by making the "outer" shm file (the one that gets put in
->vm_file) hold a reference to the real shm file, and by making
__shm_open() require that the file associated with the shm ID matches
the one associated with the "outer" file.
Taking the reference to the real shm file is needed to fully solve the
problem, since otherwise sfd->file could point to a freed file, which
then could be reallocated for the reused shm ID, causing the wrong shm
segment to be mapped (and without the required permission checks).
Commit 1ac0b6dec656 ("ipc/shm: handle removed segments gracefully in
shm_mmap()") almost fixed this bug, but it didn't go far enough because
it didn't consider the case where the shm ID is reused.
The following program usually reproduces this bug:
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
int is_parent = (fork() != 0);
srand(getpid());
for (;;) {
int id = shmget(0xF00F, 4096, IPC_CREAT|0700);
if (is_parent) {
void *addr = shmat(id, NULL, 0);
usleep(rand() % 50);
while (!syscall(__NR_remap_file_pages, addr, 4096, 0, 0, 0));
} else {
usleep(rand() % 50);
shmctl(id, IPC_RMID, NULL);
}
}
}
It causes the following NULL pointer dereference due to a 'struct file'
being used while it's being freed. (I couldn't actually get a KASAN
use-after-free splat like in the syzbot report. But I think it's
possible with this bug; it would just take a more extraordinary race...)
BUG: unable to handle kernel NULL pointer dereference at 0000000000000058
PGD 0 P4D 0
Oops: 0000 [#1] SMP NOPTI
CPU: 9 PID: 258 Comm: syz_ipc Not tainted 4.16.0-05140-gf8cf2f16a7c95 #189
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-20171110_100015-anatol 04/01/2014
RIP: 0010:d_inode include/linux/dcache.h:519 [inline]
RIP: 0010:touch_atime+0x25/0xd0 fs/inode.c:1724
[...]
Call Trace:
file_accessed include/linux/fs.h:2063 [inline]
shmem_mmap+0x25/0x40 mm/shmem.c:2149
call_mmap include/linux/fs.h:1789 [inline]
shm_mmap+0x34/0x80 ipc/shm.c:465
call_mmap include/linux/fs.h:1789 [inline]
mmap_region+0x309/0x5b0 mm/mmap.c:1712
do_mmap+0x294/0x4a0 mm/mmap.c:1483
do_mmap_pgoff include/linux/mm.h:2235 [inline]
SYSC_remap_file_pages mm/mmap.c:2853 [inline]
SyS_remap_file_pages+0x232/0x310 mm/mmap.c:2769
do_syscall_64+0x64/0x1a0 arch/x86/entry/common.c:287
entry_SYSCALL_64_after_hwframe+0x42/0xb7
[ebiggers@google.com: add comment]
Link: http://lkml.kernel.org/r/20180410192850.235835-1-ebiggers3@gmail.com
Link: http://lkml.kernel.org/r/20180409043039.28915-1-ebiggers3@gmail.com
Reported-by: syzbot+d11f321e7f1923157eac80aa990b446596f46439@syzkaller.appspotmail.com
Fixes: c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Davidlohr Bueso <dbueso@suse.de>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-13 22:35:30 +00:00
|
|
|
if (shp->shm_file != sfd->file) {
|
|
|
|
/* ID was reused */
|
|
|
|
shm_unlock(shp);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-08-03 02:51:14 +00:00
|
|
|
shp->shm_atim = ktime_get_real_seconds();
|
2018-03-23 05:29:57 +00:00
|
|
|
ipc_update_pid(&shp->shm_lprid, task_tgid(current));
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->shm_nattch++;
|
|
|
|
shm_unlock(shp);
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called by fork, once for every shm attach. */
|
|
|
|
static void shm_open(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
int err = __shm_open(vma);
|
|
|
|
/*
|
|
|
|
* We raced in the idr lookup or with shm_destroy().
|
|
|
|
* Either way, the ID is busted.
|
|
|
|
*/
|
|
|
|
WARN_ON_ONCE(err);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shm_destroy - free the struct shmid_kernel
|
|
|
|
*
|
2007-10-19 06:40:53 +00:00
|
|
|
* @ns: namespace
|
2005-04-16 22:20:36 +00:00
|
|
|
* @shp: struct to free
|
|
|
|
*
|
2013-09-11 21:26:24 +00:00
|
|
|
* It has to be called with shp and shm_ids.rwsem (writer) locked,
|
2005-04-16 22:20:36 +00:00
|
|
|
* but returns with shp unlocked and freed.
|
|
|
|
*/
|
2006-10-02 09:18:22 +00:00
|
|
|
static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-11-21 22:32:00 +00:00
|
|
|
struct file *shm_file;
|
|
|
|
|
|
|
|
shm_file = shp->shm_file;
|
|
|
|
shp->shm_file = NULL;
|
2006-10-02 09:18:22 +00:00
|
|
|
ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
2007-10-19 06:40:48 +00:00
|
|
|
shm_rmid(ns, shp);
|
2005-04-16 22:20:36 +00:00
|
|
|
shm_unlock(shp);
|
2013-11-21 22:32:00 +00:00
|
|
|
if (!is_file_hugepages(shm_file))
|
|
|
|
shmem_lock(shm_file, 0, shp->mlock_user);
|
2009-08-24 15:30:28 +00:00
|
|
|
else if (shp->mlock_user)
|
2014-12-13 00:58:22 +00:00
|
|
|
user_shm_unlock(i_size_read(file_inode(shm_file)),
|
|
|
|
shp->mlock_user);
|
2013-11-21 22:32:00 +00:00
|
|
|
fput(shm_file);
|
2018-03-23 05:29:57 +00:00
|
|
|
ipc_update_pid(&shp->shm_cprid, NULL);
|
|
|
|
ipc_update_pid(&shp->shm_lprid, NULL);
|
2017-07-12 21:34:41 +00:00
|
|
|
ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-26 23:08:48 +00:00
|
|
|
/*
|
|
|
|
* shm_may_destroy - identifies whether shm segment should be destroyed now
|
|
|
|
*
|
|
|
|
* Returns true if and only if there are no active users of the segment and
|
|
|
|
* one of the following is true:
|
|
|
|
*
|
|
|
|
* 1) shmctl(id, IPC_RMID, NULL) was called for this shp
|
|
|
|
*
|
|
|
|
* 2) sysctl kernel.shm_rmid_forced is set to 1.
|
|
|
|
*/
|
|
|
|
static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
|
|
|
|
{
|
|
|
|
return (shp->shm_nattch == 0) &&
|
|
|
|
(ns->shm_rmid_forced ||
|
|
|
|
(shp->shm_perm.mode & SHM_DEST));
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2007-02-20 21:57:53 +00:00
|
|
|
* remove the attach descriptor vma.
|
2005-04-16 22:20:36 +00:00
|
|
|
* free memory for segment if it is marked destroyed.
|
|
|
|
* The descriptor has already been removed from the current->mm->mmap list
|
|
|
|
* and will later be kfree()d.
|
|
|
|
*/
|
2007-02-20 21:57:53 +00:00
|
|
|
static void shm_close(struct vm_area_struct *vma)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2014-01-28 01:07:04 +00:00
|
|
|
struct file *file = vma->vm_file;
|
2007-02-20 21:57:53 +00:00
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2005-04-16 22:20:36 +00:00
|
|
|
struct shmid_kernel *shp;
|
2007-02-20 21:57:53 +00:00
|
|
|
struct ipc_namespace *ns = sfd->ns;
|
2006-10-02 09:18:22 +00:00
|
|
|
|
2013-09-11 21:26:24 +00:00
|
|
|
down_write(&shm_ids(ns).rwsem);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* remove from the list of attaches of the shm segment */
|
2008-07-25 08:48:03 +00:00
|
|
|
shp = shm_lock(ns, sfd->id);
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We raced in the idr lookup or with shm_destroy().
|
|
|
|
* Either way, the ID is busted.
|
|
|
|
*/
|
|
|
|
if (WARN_ON_ONCE(IS_ERR(shp)))
|
|
|
|
goto done; /* no-op */
|
|
|
|
|
2018-03-23 05:29:57 +00:00
|
|
|
ipc_update_pid(&shp->shm_lprid, task_tgid(current));
|
2017-08-03 02:51:14 +00:00
|
|
|
shp->shm_dtim = ktime_get_real_seconds();
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->shm_nattch--;
|
2011-07-26 23:08:48 +00:00
|
|
|
if (shm_may_destroy(ns, shp))
|
|
|
|
shm_destroy(ns, shp);
|
|
|
|
else
|
|
|
|
shm_unlock(shp);
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
done:
|
2013-09-11 21:26:24 +00:00
|
|
|
up_write(&shm_ids(ns).rwsem);
|
2011-07-26 23:08:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 21:26:24 +00:00
|
|
|
/* Called with ns->shm_ids(ns).rwsem locked */
|
2011-07-26 23:08:48 +00:00
|
|
|
static int shm_try_destroy_orphaned(int id, void *p, void *data)
|
|
|
|
{
|
|
|
|
struct ipc_namespace *ns = data;
|
2011-07-28 23:56:40 +00:00
|
|
|
struct kern_ipc_perm *ipcp = p;
|
|
|
|
struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
2011-07-26 23:08:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to destroy segments without users and with already
|
|
|
|
* exit'ed originating process.
|
2011-07-28 23:56:40 +00:00
|
|
|
*
|
2013-09-11 21:26:24 +00:00
|
|
|
* As shp->* are changed under rwsem, it's safe to skip shp locking.
|
2011-07-26 23:08:48 +00:00
|
|
|
*/
|
2011-07-28 23:56:40 +00:00
|
|
|
if (shp->shm_creator != NULL)
|
2011-07-26 23:08:48 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-07-28 23:56:40 +00:00
|
|
|
if (shm_may_destroy(ns, shp)) {
|
|
|
|
shm_lock_by_ptr(shp);
|
2006-10-02 09:18:22 +00:00
|
|
|
shm_destroy(ns, shp);
|
2011-07-28 23:56:40 +00:00
|
|
|
}
|
2011-07-26 23:08:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shm_destroy_orphaned(struct ipc_namespace *ns)
|
|
|
|
{
|
2013-09-11 21:26:24 +00:00
|
|
|
down_write(&shm_ids(ns).rwsem);
|
2011-08-03 18:26:55 +00:00
|
|
|
if (shm_ids(ns).in_use)
|
2011-07-28 23:56:40 +00:00
|
|
|
idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
|
2013-09-11 21:26:24 +00:00
|
|
|
up_write(&shm_ids(ns).rwsem);
|
2011-07-26 23:08:48 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 21:23:21 +00:00
|
|
|
/* Locking assumes this will only be called with task == current */
|
2011-07-26 23:08:48 +00:00
|
|
|
void exit_shm(struct task_struct *task)
|
|
|
|
{
|
2011-07-28 23:56:40 +00:00
|
|
|
struct ipc_namespace *ns = task->nsproxy->ipc_ns;
|
shm: make exit_shm work proportional to task activity
This is small set of patches our team has had kicking around for a few
versions internally that fixes tasks getting hung on shm_exit when there
are many threads hammering it at once.
Anton wrote a simple test to cause the issue:
http://ozlabs.org/~anton/junkcode/bust_shm_exit.c
Before applying this patchset, this test code will cause either hanging
tracebacks or pthread out of memory errors.
After this patchset, it will still produce output like:
root@somehost:~# ./bust_shm_exit 1024 160
...
INFO: rcu_sched detected stalls on CPUs/tasks: {} (detected by 116, t=2111 jiffies, g=241, c=240, q=7113)
INFO: Stall ended before state dump start
...
But the task will continue to run along happily, so we consider this an
improvement over hanging, even if it's a bit noisy.
This patch (of 3):
exit_shm obtains the ipc_ns shm rwsem for write and holds it while it
walks every shared memory segment in the namespace. Thus the amount of
work is related to the number of shm segments in the namespace not the
number of segments that might need to be cleaned.
In addition, this occurs after the task has been notified the thread has
exited, so the number of tasks waiting for the ns shm rwsem can grow
without bound until memory is exausted.
Add a list to the task struct of all shmids allocated by this task. Init
the list head in copy_process. Use the ns->rwsem for locking. Add
segments after id is added, remove before removing from id.
On unshare of NEW_IPCNS orphan any ids as if the task had exited, similar
to handling of semaphore undo.
I chose a define for the init sequence since its a simple list init,
otherwise it would require a function call to avoid include loops between
the semaphore code and the task struct. Converting the list_del to
list_del_init for the unshare cases would remove the exit followed by
init, but I left it blow up if not inited.
Signed-off-by: Milton Miller <miltonm@bga.com>
Signed-off-by: Jack Miller <millerjo@us.ibm.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Anton Blanchard <anton@samba.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:23:19 +00:00
|
|
|
struct shmid_kernel *shp, *n;
|
2011-07-26 23:08:48 +00:00
|
|
|
|
2014-08-08 21:23:21 +00:00
|
|
|
if (list_empty(&task->sysvshm.shm_clist))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If kernel.shm_rmid_forced is not set then only keep track of
|
|
|
|
* which shmids are orphaned, so that a later set of the sysctl
|
|
|
|
* can clean them up.
|
|
|
|
*/
|
|
|
|
if (!ns->shm_rmid_forced) {
|
|
|
|
down_read(&shm_ids(ns).rwsem);
|
|
|
|
list_for_each_entry(shp, &task->sysvshm.shm_clist, shm_clist)
|
|
|
|
shp->shm_creator = NULL;
|
|
|
|
/*
|
|
|
|
* Only under read lock but we are only called on current
|
|
|
|
* so no entry on the list will be shared.
|
|
|
|
*/
|
|
|
|
list_del(&task->sysvshm.shm_clist);
|
|
|
|
up_read(&shm_ids(ns).rwsem);
|
2011-08-03 18:28:26 +00:00
|
|
|
return;
|
2014-08-08 21:23:21 +00:00
|
|
|
}
|
2011-08-03 18:28:26 +00:00
|
|
|
|
2014-08-08 21:23:21 +00:00
|
|
|
/*
|
|
|
|
* Destroy all already created segments, that were not yet mapped,
|
|
|
|
* and mark any mapped as orphan to cover the sysctl toggling.
|
|
|
|
* Destroy is skipped if shm_may_destroy() returns false.
|
|
|
|
*/
|
2013-09-11 21:26:24 +00:00
|
|
|
down_write(&shm_ids(ns).rwsem);
|
2014-08-08 21:23:21 +00:00
|
|
|
list_for_each_entry_safe(shp, n, &task->sysvshm.shm_clist, shm_clist) {
|
|
|
|
shp->shm_creator = NULL;
|
|
|
|
|
|
|
|
if (shm_may_destroy(ns, shp)) {
|
|
|
|
shm_lock_by_ptr(shp);
|
|
|
|
shm_destroy(ns, shp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the list head from any segments still attached. */
|
shm: make exit_shm work proportional to task activity
This is small set of patches our team has had kicking around for a few
versions internally that fixes tasks getting hung on shm_exit when there
are many threads hammering it at once.
Anton wrote a simple test to cause the issue:
http://ozlabs.org/~anton/junkcode/bust_shm_exit.c
Before applying this patchset, this test code will cause either hanging
tracebacks or pthread out of memory errors.
After this patchset, it will still produce output like:
root@somehost:~# ./bust_shm_exit 1024 160
...
INFO: rcu_sched detected stalls on CPUs/tasks: {} (detected by 116, t=2111 jiffies, g=241, c=240, q=7113)
INFO: Stall ended before state dump start
...
But the task will continue to run along happily, so we consider this an
improvement over hanging, even if it's a bit noisy.
This patch (of 3):
exit_shm obtains the ipc_ns shm rwsem for write and holds it while it
walks every shared memory segment in the namespace. Thus the amount of
work is related to the number of shm segments in the namespace not the
number of segments that might need to be cleaned.
In addition, this occurs after the task has been notified the thread has
exited, so the number of tasks waiting for the ns shm rwsem can grow
without bound until memory is exausted.
Add a list to the task struct of all shmids allocated by this task. Init
the list head in copy_process. Use the ns->rwsem for locking. Add
segments after id is added, remove before removing from id.
On unshare of NEW_IPCNS orphan any ids as if the task had exited, similar
to handling of semaphore undo.
I chose a define for the init sequence since its a simple list init,
otherwise it would require a function call to avoid include loops between
the semaphore code and the task struct. Converting the list_del to
list_del_init for the unshare cases would remove the exit followed by
init, but I left it blow up if not inited.
Signed-off-by: Milton Miller <miltonm@bga.com>
Signed-off-by: Jack Miller <millerjo@us.ibm.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Anton Blanchard <anton@samba.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:23:19 +00:00
|
|
|
list_del(&task->sysvshm.shm_clist);
|
2013-09-11 21:26:24 +00:00
|
|
|
up_write(&shm_ids(ns).rwsem);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 22:27:55 +00:00
|
|
|
static vm_fault_t shm_fault(struct vm_fault *vmf)
|
2007-02-20 21:57:53 +00:00
|
|
|
{
|
2017-02-24 22:56:41 +00:00
|
|
|
struct file *file = vmf->vma->vm_file;
|
2007-02-20 21:57:53 +00:00
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
2017-02-24 22:56:41 +00:00
|
|
|
return sfd->vm_ops->fault(vmf);
|
2007-02-20 21:57:53 +00:00
|
|
|
}
|
|
|
|
|
ipc/shm.c: add split function to shm_vm_ops
If System V shmget/shmat operations are used to create a hugetlbfs
backed mapping, it is possible to munmap part of the mapping and split
the underlying vma such that it is not huge page aligned. This will
untimately result in the following BUG:
kernel BUG at /build/linux-jWa1Fv/linux-4.15.0/mm/hugetlb.c:3310!
Oops: Exception in kernel mode, sig: 5 [#1]
LE SMP NR_CPUS=2048 NUMA PowerNV
Modules linked in: kcm nfc af_alg caif_socket caif phonet fcrypt
CPU: 18 PID: 43243 Comm: trinity-subchil Tainted: G C E 4.15.0-10-generic #11-Ubuntu
NIP: c00000000036e764 LR: c00000000036ee48 CTR: 0000000000000009
REGS: c000003fbcdcf810 TRAP: 0700 Tainted: G C E (4.15.0-10-generic)
MSR: 9000000000029033 <SF,HV,EE,ME,IR,DR,RI,LE> CR: 24002222 XER: 20040000
CFAR: c00000000036ee44 SOFTE: 1
NIP __unmap_hugepage_range+0xa4/0x760
LR __unmap_hugepage_range_final+0x28/0x50
Call Trace:
0x7115e4e00000 (unreliable)
__unmap_hugepage_range_final+0x28/0x50
unmap_single_vma+0x11c/0x190
unmap_vmas+0x94/0x140
exit_mmap+0x9c/0x1d0
mmput+0xa8/0x1d0
do_exit+0x360/0xc80
do_group_exit+0x60/0x100
SyS_exit_group+0x24/0x30
system_call+0x58/0x6c
---[ end trace ee88f958a1c62605 ]---
This bug was introduced by commit 31383c6865a5 ("mm, hugetlbfs:
introduce ->split() to vm_operations_struct"). A split function was
added to vm_operations_struct to determine if a mapping can be split.
This was mostly for device-dax and hugetlbfs mappings which have
specific alignment constraints.
Mappings initiated via shmget/shmat have their original vm_ops
overwritten with shm_vm_ops. shm_vm_ops functions will call back to the
original vm_ops if needed. Add such a split function to shm_vm_ops.
Link: http://lkml.kernel.org/r/20180321161314.7711-1-mike.kravetz@oracle.com
Fixes: 31383c6865a5 ("mm, hugetlbfs: introduce ->split() to vm_operations_struct")
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reported-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Reviewed-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Tested-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-03-28 23:01:01 +00:00
|
|
|
static int shm_split(struct vm_area_struct *vma, unsigned long addr)
|
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
2018-04-10 23:35:42 +00:00
|
|
|
if (sfd->vm_ops->split)
|
ipc/shm.c: add split function to shm_vm_ops
If System V shmget/shmat operations are used to create a hugetlbfs
backed mapping, it is possible to munmap part of the mapping and split
the underlying vma such that it is not huge page aligned. This will
untimately result in the following BUG:
kernel BUG at /build/linux-jWa1Fv/linux-4.15.0/mm/hugetlb.c:3310!
Oops: Exception in kernel mode, sig: 5 [#1]
LE SMP NR_CPUS=2048 NUMA PowerNV
Modules linked in: kcm nfc af_alg caif_socket caif phonet fcrypt
CPU: 18 PID: 43243 Comm: trinity-subchil Tainted: G C E 4.15.0-10-generic #11-Ubuntu
NIP: c00000000036e764 LR: c00000000036ee48 CTR: 0000000000000009
REGS: c000003fbcdcf810 TRAP: 0700 Tainted: G C E (4.15.0-10-generic)
MSR: 9000000000029033 <SF,HV,EE,ME,IR,DR,RI,LE> CR: 24002222 XER: 20040000
CFAR: c00000000036ee44 SOFTE: 1
NIP __unmap_hugepage_range+0xa4/0x760
LR __unmap_hugepage_range_final+0x28/0x50
Call Trace:
0x7115e4e00000 (unreliable)
__unmap_hugepage_range_final+0x28/0x50
unmap_single_vma+0x11c/0x190
unmap_vmas+0x94/0x140
exit_mmap+0x9c/0x1d0
mmput+0xa8/0x1d0
do_exit+0x360/0xc80
do_group_exit+0x60/0x100
SyS_exit_group+0x24/0x30
system_call+0x58/0x6c
---[ end trace ee88f958a1c62605 ]---
This bug was introduced by commit 31383c6865a5 ("mm, hugetlbfs:
introduce ->split() to vm_operations_struct"). A split function was
added to vm_operations_struct to determine if a mapping can be split.
This was mostly for device-dax and hugetlbfs mappings which have
specific alignment constraints.
Mappings initiated via shmget/shmat have their original vm_ops
overwritten with shm_vm_ops. shm_vm_ops functions will call back to the
original vm_ops if needed. Add such a split function to shm_vm_ops.
Link: http://lkml.kernel.org/r/20180321161314.7711-1-mike.kravetz@oracle.com
Fixes: 31383c6865a5 ("mm, hugetlbfs: introduce ->split() to vm_operations_struct")
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reported-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Reviewed-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Tested-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-03-28 23:01:01 +00:00
|
|
|
return sfd->vm_ops->split(vma, addr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:05 +00:00
|
|
|
static unsigned long shm_pagesize(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
|
|
|
if (sfd->vm_ops->pagesize)
|
|
|
|
return sfd->vm_ops->pagesize(vma);
|
|
|
|
|
|
|
|
return PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
#ifdef CONFIG_NUMA
|
2007-10-17 06:26:42 +00:00
|
|
|
static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
|
2007-02-20 21:57:53 +00:00
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
int err = 0;
|
2016-12-14 23:06:10 +00:00
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
if (sfd->vm_ops->set_policy)
|
|
|
|
err = sfd->vm_ops->set_policy(vma, new);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-10-17 06:26:42 +00:00
|
|
|
static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
|
|
|
|
unsigned long addr)
|
2007-02-20 21:57:53 +00:00
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
struct mempolicy *pol = NULL;
|
|
|
|
|
|
|
|
if (sfd->vm_ops->get_policy)
|
|
|
|
pol = sfd->vm_ops->get_policy(vma, addr);
|
mempolicy: rework mempolicy Reference Counting [yet again]
After further discussion with Christoph Lameter, it has become clear that my
earlier attempts to clean up the mempolicy reference counting were a bit of
overkill in some areas, resulting in superflous ref/unref in what are usually
fast paths. In other areas, further inspection reveals that I botched the
unref for interleave policies.
A separate patch, suitable for upstream/stable trees, fixes up the known
errors in the previous attempt to fix reference counting.
This patch reworks the memory policy referencing counting and, one hopes,
simplifies the code. Maybe I'll get it right this time.
See the update to the numa_memory_policy.txt document for a discussion of
memory policy reference counting that motivates this patch.
Summary:
Lookup of mempolicy, based on (vma, address) need only add a reference for
shared policy, and we need only unref the policy when finished for shared
policies. So, this patch backs out all of the unneeded extra reference
counting added by my previous attempt. It then unrefs only shared policies
when we're finished with them, using the mpol_cond_put() [conditional put]
helper function introduced by this patch.
Note that shmem_swapin() calls read_swap_cache_async() with a dummy vma
containing just the policy. read_swap_cache_async() can call alloc_page_vma()
multiple times, so we can't let alloc_page_vma() unref the shared policy in
this case. To avoid this, we make a copy of any non-null shared policy and
remove the MPOL_F_SHARED flag from the copy. This copy occurs before reading
a page [or multiple pages] from swap, so the overhead should not be an issue
here.
I introduced a new static inline function "mpol_cond_copy()" to copy the
shared policy to an on-stack policy and remove the flags that would require a
conditional free. The current implementation of mpol_cond_copy() assumes that
the struct mempolicy contains no pointers to dynamically allocated structures
that must be duplicated or reference counted during copy.
Signed-off-by: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 09:13:16 +00:00
|
|
|
else if (vma->vm_policy)
|
2007-02-20 21:57:53 +00:00
|
|
|
pol = vma->vm_policy;
|
mempolicy: rework mempolicy Reference Counting [yet again]
After further discussion with Christoph Lameter, it has become clear that my
earlier attempts to clean up the mempolicy reference counting were a bit of
overkill in some areas, resulting in superflous ref/unref in what are usually
fast paths. In other areas, further inspection reveals that I botched the
unref for interleave policies.
A separate patch, suitable for upstream/stable trees, fixes up the known
errors in the previous attempt to fix reference counting.
This patch reworks the memory policy referencing counting and, one hopes,
simplifies the code. Maybe I'll get it right this time.
See the update to the numa_memory_policy.txt document for a discussion of
memory policy reference counting that motivates this patch.
Summary:
Lookup of mempolicy, based on (vma, address) need only add a reference for
shared policy, and we need only unref the policy when finished for shared
policies. So, this patch backs out all of the unneeded extra reference
counting added by my previous attempt. It then unrefs only shared policies
when we're finished with them, using the mpol_cond_put() [conditional put]
helper function introduced by this patch.
Note that shmem_swapin() calls read_swap_cache_async() with a dummy vma
containing just the policy. read_swap_cache_async() can call alloc_page_vma()
multiple times, so we can't let alloc_page_vma() unref the shared policy in
this case. To avoid this, we make a copy of any non-null shared policy and
remove the MPOL_F_SHARED flag from the copy. This copy occurs before reading
a page [or multiple pages] from swap, so the overhead should not be an issue
here.
I introduced a new static inline function "mpol_cond_copy()" to copy the
shared policy to an on-stack policy and remove the flags that would require a
conditional free. The current implementation of mpol_cond_copy() assumes that
the struct mempolicy contains no pointers to dynamically allocated structures
that must be duplicated or reference counted during copy.
Signed-off-by: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 09:13:16 +00:00
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
return pol;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-28 01:07:04 +00:00
|
|
|
static int shm_mmap(struct file *file, struct vm_area_struct *vma)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-02-20 21:57:53 +00:00
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2006-01-06 08:11:42 +00:00
|
|
|
int ret;
|
|
|
|
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
/*
|
ipc/shm: fix use-after-free of shm file via remap_file_pages()
syzbot reported a use-after-free of shm_file_data(file)->file->f_op in
shm_get_unmapped_area(), called via sys_remap_file_pages().
Unfortunately it couldn't generate a reproducer, but I found a bug which
I think caused it. When remap_file_pages() is passed a full System V
shared memory segment, the memory is first unmapped, then a new map is
created using the ->vm_file. Between these steps, the shm ID can be
removed and reused for a new shm segment. But, shm_mmap() only checks
whether the ID is currently valid before calling the underlying file's
->mmap(); it doesn't check whether it was reused. Thus it can use the
wrong underlying file, one that was already freed.
Fix this by making the "outer" shm file (the one that gets put in
->vm_file) hold a reference to the real shm file, and by making
__shm_open() require that the file associated with the shm ID matches
the one associated with the "outer" file.
Taking the reference to the real shm file is needed to fully solve the
problem, since otherwise sfd->file could point to a freed file, which
then could be reallocated for the reused shm ID, causing the wrong shm
segment to be mapped (and without the required permission checks).
Commit 1ac0b6dec656 ("ipc/shm: handle removed segments gracefully in
shm_mmap()") almost fixed this bug, but it didn't go far enough because
it didn't consider the case where the shm ID is reused.
The following program usually reproduces this bug:
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
int is_parent = (fork() != 0);
srand(getpid());
for (;;) {
int id = shmget(0xF00F, 4096, IPC_CREAT|0700);
if (is_parent) {
void *addr = shmat(id, NULL, 0);
usleep(rand() % 50);
while (!syscall(__NR_remap_file_pages, addr, 4096, 0, 0, 0));
} else {
usleep(rand() % 50);
shmctl(id, IPC_RMID, NULL);
}
}
}
It causes the following NULL pointer dereference due to a 'struct file'
being used while it's being freed. (I couldn't actually get a KASAN
use-after-free splat like in the syzbot report. But I think it's
possible with this bug; it would just take a more extraordinary race...)
BUG: unable to handle kernel NULL pointer dereference at 0000000000000058
PGD 0 P4D 0
Oops: 0000 [#1] SMP NOPTI
CPU: 9 PID: 258 Comm: syz_ipc Not tainted 4.16.0-05140-gf8cf2f16a7c95 #189
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-20171110_100015-anatol 04/01/2014
RIP: 0010:d_inode include/linux/dcache.h:519 [inline]
RIP: 0010:touch_atime+0x25/0xd0 fs/inode.c:1724
[...]
Call Trace:
file_accessed include/linux/fs.h:2063 [inline]
shmem_mmap+0x25/0x40 mm/shmem.c:2149
call_mmap include/linux/fs.h:1789 [inline]
shm_mmap+0x34/0x80 ipc/shm.c:465
call_mmap include/linux/fs.h:1789 [inline]
mmap_region+0x309/0x5b0 mm/mmap.c:1712
do_mmap+0x294/0x4a0 mm/mmap.c:1483
do_mmap_pgoff include/linux/mm.h:2235 [inline]
SYSC_remap_file_pages mm/mmap.c:2853 [inline]
SyS_remap_file_pages+0x232/0x310 mm/mmap.c:2769
do_syscall_64+0x64/0x1a0 arch/x86/entry/common.c:287
entry_SYSCALL_64_after_hwframe+0x42/0xb7
[ebiggers@google.com: add comment]
Link: http://lkml.kernel.org/r/20180410192850.235835-1-ebiggers3@gmail.com
Link: http://lkml.kernel.org/r/20180409043039.28915-1-ebiggers3@gmail.com
Reported-by: syzbot+d11f321e7f1923157eac80aa990b446596f46439@syzkaller.appspotmail.com
Fixes: c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Davidlohr Bueso <dbueso@suse.de>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-13 22:35:30 +00:00
|
|
|
* In case of remap_file_pages() emulation, the file can represent an
|
|
|
|
* IPC ID that was removed, and possibly even reused by another shm
|
|
|
|
* segment already. Propagate this case as an error to caller.
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
*/
|
2016-12-14 23:06:10 +00:00
|
|
|
ret = __shm_open(vma);
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-02-20 15:51:23 +00:00
|
|
|
ret = call_mmap(sfd->file, vma);
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
if (ret) {
|
|
|
|
shm_close(vma);
|
2007-02-20 21:57:53 +00:00
|
|
|
return ret;
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
}
|
2007-02-20 21:57:53 +00:00
|
|
|
sfd->vm_ops = vma->vm_ops;
|
2007-07-31 07:37:24 +00:00
|
|
|
#ifdef CONFIG_MMU
|
2015-09-09 22:39:20 +00:00
|
|
|
WARN_ON(!sfd->vm_ops->fault);
|
2007-07-31 07:37:24 +00:00
|
|
|
#endif
|
2007-02-20 21:57:53 +00:00
|
|
|
vma->vm_ops = &shm_vm_ops;
|
ipc/shm: handle removed segments gracefully in shm_mmap()
remap_file_pages(2) emulation can reach file which represents removed
IPC ID as long as a memory segment is mapped. It breaks expectations of
IPC subsystem.
Test case (rewritten to be more human readable, originally autogenerated
by syzkaller[1]):
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define PAGE_SIZE 4096
int main()
{
int id;
void *p;
id = shmget(IPC_PRIVATE, 3 * PAGE_SIZE, 0);
p = shmat(id, NULL, 0);
shmctl(id, IPC_RMID, NULL);
remap_file_pages(p, 3 * PAGE_SIZE, 0, 7, 0);
return 0;
}
The patch changes shm_mmap() and code around shm_lock() to propagate
locking error back to caller of shm_mmap().
[1] http://github.com/google/syzkaller
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-02-17 21:11:35 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 09:18:22 +00:00
|
|
|
static int shm_release(struct inode *ino, struct file *file)
|
|
|
|
{
|
2007-02-20 21:57:53 +00:00
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2006-10-02 09:18:22 +00:00
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
put_ipc_ns(sfd->ns);
|
ipc/shm: fix use-after-free of shm file via remap_file_pages()
syzbot reported a use-after-free of shm_file_data(file)->file->f_op in
shm_get_unmapped_area(), called via sys_remap_file_pages().
Unfortunately it couldn't generate a reproducer, but I found a bug which
I think caused it. When remap_file_pages() is passed a full System V
shared memory segment, the memory is first unmapped, then a new map is
created using the ->vm_file. Between these steps, the shm ID can be
removed and reused for a new shm segment. But, shm_mmap() only checks
whether the ID is currently valid before calling the underlying file's
->mmap(); it doesn't check whether it was reused. Thus it can use the
wrong underlying file, one that was already freed.
Fix this by making the "outer" shm file (the one that gets put in
->vm_file) hold a reference to the real shm file, and by making
__shm_open() require that the file associated with the shm ID matches
the one associated with the "outer" file.
Taking the reference to the real shm file is needed to fully solve the
problem, since otherwise sfd->file could point to a freed file, which
then could be reallocated for the reused shm ID, causing the wrong shm
segment to be mapped (and without the required permission checks).
Commit 1ac0b6dec656 ("ipc/shm: handle removed segments gracefully in
shm_mmap()") almost fixed this bug, but it didn't go far enough because
it didn't consider the case where the shm ID is reused.
The following program usually reproduces this bug:
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
int is_parent = (fork() != 0);
srand(getpid());
for (;;) {
int id = shmget(0xF00F, 4096, IPC_CREAT|0700);
if (is_parent) {
void *addr = shmat(id, NULL, 0);
usleep(rand() % 50);
while (!syscall(__NR_remap_file_pages, addr, 4096, 0, 0, 0));
} else {
usleep(rand() % 50);
shmctl(id, IPC_RMID, NULL);
}
}
}
It causes the following NULL pointer dereference due to a 'struct file'
being used while it's being freed. (I couldn't actually get a KASAN
use-after-free splat like in the syzbot report. But I think it's
possible with this bug; it would just take a more extraordinary race...)
BUG: unable to handle kernel NULL pointer dereference at 0000000000000058
PGD 0 P4D 0
Oops: 0000 [#1] SMP NOPTI
CPU: 9 PID: 258 Comm: syz_ipc Not tainted 4.16.0-05140-gf8cf2f16a7c95 #189
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-20171110_100015-anatol 04/01/2014
RIP: 0010:d_inode include/linux/dcache.h:519 [inline]
RIP: 0010:touch_atime+0x25/0xd0 fs/inode.c:1724
[...]
Call Trace:
file_accessed include/linux/fs.h:2063 [inline]
shmem_mmap+0x25/0x40 mm/shmem.c:2149
call_mmap include/linux/fs.h:1789 [inline]
shm_mmap+0x34/0x80 ipc/shm.c:465
call_mmap include/linux/fs.h:1789 [inline]
mmap_region+0x309/0x5b0 mm/mmap.c:1712
do_mmap+0x294/0x4a0 mm/mmap.c:1483
do_mmap_pgoff include/linux/mm.h:2235 [inline]
SYSC_remap_file_pages mm/mmap.c:2853 [inline]
SyS_remap_file_pages+0x232/0x310 mm/mmap.c:2769
do_syscall_64+0x64/0x1a0 arch/x86/entry/common.c:287
entry_SYSCALL_64_after_hwframe+0x42/0xb7
[ebiggers@google.com: add comment]
Link: http://lkml.kernel.org/r/20180410192850.235835-1-ebiggers3@gmail.com
Link: http://lkml.kernel.org/r/20180409043039.28915-1-ebiggers3@gmail.com
Reported-by: syzbot+d11f321e7f1923157eac80aa990b446596f46439@syzkaller.appspotmail.com
Fixes: c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Davidlohr Bueso <dbueso@suse.de>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-13 22:35:30 +00:00
|
|
|
fput(sfd->file);
|
2007-02-20 21:57:53 +00:00
|
|
|
shm_file_data(file) = NULL;
|
|
|
|
kfree(sfd);
|
2006-10-02 09:18:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-17 00:44:56 +00:00
|
|
|
static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
|
2007-03-01 23:46:08 +00:00
|
|
|
{
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
2010-05-26 15:53:25 +00:00
|
|
|
if (!sfd->file->f_op->fsync)
|
|
|
|
return -EINVAL;
|
2017-07-05 19:26:50 +00:00
|
|
|
return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
|
2007-03-01 23:46:08 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 21:21:13 +00:00
|
|
|
static long shm_fallocate(struct file *file, int mode, loff_t offset,
|
|
|
|
loff_t len)
|
|
|
|
{
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
|
|
|
|
|
|
|
if (!sfd->file->f_op->fallocate)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
return sfd->file->f_op->fallocate(file, mode, offset, len);
|
|
|
|
}
|
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
static unsigned long shm_get_unmapped_area(struct file *file,
|
|
|
|
unsigned long addr, unsigned long len, unsigned long pgoff,
|
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
struct shm_file_data *sfd = shm_file_data(file);
|
2016-12-14 23:06:10 +00:00
|
|
|
|
2009-11-30 13:38:43 +00:00
|
|
|
return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
|
|
|
|
pgoff, flags);
|
2007-02-20 21:57:53 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 08:55:35 +00:00
|
|
|
static const struct file_operations shm_file_operations = {
|
2006-10-02 09:18:22 +00:00
|
|
|
.mmap = shm_mmap,
|
2007-03-01 23:46:08 +00:00
|
|
|
.fsync = shm_fsync,
|
2006-10-02 09:18:22 +00:00
|
|
|
.release = shm_release,
|
2010-01-16 01:01:32 +00:00
|
|
|
.get_unmapped_area = shm_get_unmapped_area,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 16:52:59 +00:00
|
|
|
.llseek = noop_llseek,
|
2012-06-07 21:21:13 +00:00
|
|
|
.fallocate = shm_fallocate,
|
2009-11-30 13:38:43 +00:00
|
|
|
};
|
|
|
|
|
2016-07-26 22:26:15 +00:00
|
|
|
/*
|
|
|
|
* shm_file_operations_huge is now identical to shm_file_operations,
|
|
|
|
* but we keep it distinct for the sake of is_file_shm_hugepages().
|
|
|
|
*/
|
2009-11-30 13:38:43 +00:00
|
|
|
static const struct file_operations shm_file_operations_huge = {
|
|
|
|
.mmap = shm_mmap,
|
|
|
|
.fsync = shm_fsync,
|
|
|
|
.release = shm_release,
|
2007-02-20 21:57:53 +00:00
|
|
|
.get_unmapped_area = shm_get_unmapped_area,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 16:52:59 +00:00
|
|
|
.llseek = noop_llseek,
|
2012-06-07 21:21:13 +00:00
|
|
|
.fallocate = shm_fallocate,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2016-01-20 23:01:11 +00:00
|
|
|
bool is_file_shm_hugepages(struct file *file)
|
2009-11-30 13:38:43 +00:00
|
|
|
{
|
|
|
|
return file->f_op == &shm_file_operations_huge;
|
|
|
|
}
|
|
|
|
|
2009-09-27 18:29:37 +00:00
|
|
|
static const struct vm_operations_struct shm_vm_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.open = shm_open, /* callback for a new vm-area open */
|
|
|
|
.close = shm_close, /* callback for when the vm-area is released */
|
2007-07-19 08:46:59 +00:00
|
|
|
.fault = shm_fault,
|
ipc/shm.c: add split function to shm_vm_ops
If System V shmget/shmat operations are used to create a hugetlbfs
backed mapping, it is possible to munmap part of the mapping and split
the underlying vma such that it is not huge page aligned. This will
untimately result in the following BUG:
kernel BUG at /build/linux-jWa1Fv/linux-4.15.0/mm/hugetlb.c:3310!
Oops: Exception in kernel mode, sig: 5 [#1]
LE SMP NR_CPUS=2048 NUMA PowerNV
Modules linked in: kcm nfc af_alg caif_socket caif phonet fcrypt
CPU: 18 PID: 43243 Comm: trinity-subchil Tainted: G C E 4.15.0-10-generic #11-Ubuntu
NIP: c00000000036e764 LR: c00000000036ee48 CTR: 0000000000000009
REGS: c000003fbcdcf810 TRAP: 0700 Tainted: G C E (4.15.0-10-generic)
MSR: 9000000000029033 <SF,HV,EE,ME,IR,DR,RI,LE> CR: 24002222 XER: 20040000
CFAR: c00000000036ee44 SOFTE: 1
NIP __unmap_hugepage_range+0xa4/0x760
LR __unmap_hugepage_range_final+0x28/0x50
Call Trace:
0x7115e4e00000 (unreliable)
__unmap_hugepage_range_final+0x28/0x50
unmap_single_vma+0x11c/0x190
unmap_vmas+0x94/0x140
exit_mmap+0x9c/0x1d0
mmput+0xa8/0x1d0
do_exit+0x360/0xc80
do_group_exit+0x60/0x100
SyS_exit_group+0x24/0x30
system_call+0x58/0x6c
---[ end trace ee88f958a1c62605 ]---
This bug was introduced by commit 31383c6865a5 ("mm, hugetlbfs:
introduce ->split() to vm_operations_struct"). A split function was
added to vm_operations_struct to determine if a mapping can be split.
This was mostly for device-dax and hugetlbfs mappings which have
specific alignment constraints.
Mappings initiated via shmget/shmat have their original vm_ops
overwritten with shm_vm_ops. shm_vm_ops functions will call back to the
original vm_ops if needed. Add such a split function to shm_vm_ops.
Link: http://lkml.kernel.org/r/20180321161314.7711-1-mike.kravetz@oracle.com
Fixes: 31383c6865a5 ("mm, hugetlbfs: introduce ->split() to vm_operations_struct")
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reported-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Reviewed-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Tested-by: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-03-28 23:01:01 +00:00
|
|
|
.split = shm_split,
|
2018-08-02 22:36:05 +00:00
|
|
|
.pagesize = shm_pagesize,
|
2007-02-20 21:57:53 +00:00
|
|
|
#if defined(CONFIG_NUMA)
|
|
|
|
.set_policy = shm_set_policy,
|
|
|
|
.get_policy = shm_get_policy,
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-10-19 06:40:53 +00:00
|
|
|
/**
|
|
|
|
* newseg - Create a new shared memory segment
|
|
|
|
* @ns: namespace
|
|
|
|
* @params: ptr to the structure that contains key, size and shmflg
|
|
|
|
*
|
2013-09-11 21:26:24 +00:00
|
|
|
* Called with shm_ids.rwsem held as a writer.
|
2007-10-19 06:40:53 +00:00
|
|
|
*/
|
2007-10-19 06:40:49 +00:00
|
|
|
static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-10-19 06:40:49 +00:00
|
|
|
key_t key = params->key;
|
|
|
|
int shmflg = params->flg;
|
|
|
|
size_t size = params->u.size;
|
2005-04-16 22:20:36 +00:00
|
|
|
int error;
|
|
|
|
struct shmid_kernel *shp;
|
2013-05-01 02:15:54 +00:00
|
|
|
size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
2014-01-28 01:07:04 +00:00
|
|
|
struct file *file;
|
2005-04-16 22:20:36 +00:00
|
|
|
char name[13];
|
2011-05-26 10:16:19 +00:00
|
|
|
vm_flags_t acctflag = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-10-02 09:18:22 +00:00
|
|
|
if (size < SHMMIN || size > ns->shm_ctlmax)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2014-06-06 21:37:41 +00:00
|
|
|
if (numpages << PAGE_SHIFT < size)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2014-06-06 21:37:40 +00:00
|
|
|
if (ns->shm_tot + numpages < ns->shm_tot ||
|
|
|
|
ns->shm_tot + numpages > ns->shm_ctlall)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOSPC;
|
|
|
|
|
2017-07-12 21:35:25 +00:00
|
|
|
shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
|
|
|
|
if (unlikely(!shp))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
shp->shm_perm.key = key;
|
2006-01-08 09:02:21 +00:00
|
|
|
shp->shm_perm.mode = (shmflg & S_IRWXUGO);
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->mlock_user = NULL;
|
|
|
|
|
|
|
|
shp->shm_perm.security = NULL;
|
2018-03-23 02:08:27 +00:00
|
|
|
error = security_shm_alloc(&shp->shm_perm);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (error) {
|
2017-07-12 21:35:25 +00:00
|
|
|
kvfree(shp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-01-28 01:07:04 +00:00
|
|
|
sprintf(name, "SYSV%08x", key);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (shmflg & SHM_HUGETLB) {
|
2013-07-08 23:01:08 +00:00
|
|
|
struct hstate *hs;
|
2013-05-09 07:08:15 +00:00
|
|
|
size_t hugesize;
|
|
|
|
|
2013-07-08 23:01:08 +00:00
|
|
|
hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
|
2013-05-09 07:08:15 +00:00
|
|
|
if (!hs) {
|
|
|
|
error = -EINVAL;
|
|
|
|
goto no_file;
|
|
|
|
}
|
|
|
|
hugesize = ALIGN(size, huge_page_size(hs));
|
2013-05-07 23:18:13 +00:00
|
|
|
|
2009-02-10 14:02:27 +00:00
|
|
|
/* hugetlb_file_setup applies strict accounting */
|
|
|
|
if (shmflg & SHM_NORESERVE)
|
|
|
|
acctflag = VM_NORESERVE;
|
2013-05-07 23:18:13 +00:00
|
|
|
file = hugetlb_file_setup(name, hugesize, acctflag,
|
2012-12-12 00:01:34 +00:00
|
|
|
&shp->mlock_user, HUGETLB_SHMFS_INODE,
|
|
|
|
(shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
2005-11-07 08:59:27 +00:00
|
|
|
/*
|
|
|
|
* Do not allow no accounting for OVERCOMMIT_NEVER, even
|
2014-01-28 01:07:04 +00:00
|
|
|
* if it's asked for.
|
2005-11-07 08:59:27 +00:00
|
|
|
*/
|
|
|
|
if ((shmflg & SHM_NORESERVE) &&
|
|
|
|
sysctl_overcommit_memory != OVERCOMMIT_NEVER)
|
2009-01-31 23:08:56 +00:00
|
|
|
acctflag = VM_NORESERVE;
|
2015-08-06 22:46:55 +00:00
|
|
|
file = shmem_kernel_file_setup(name, size, acctflag);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
error = PTR_ERR(file);
|
|
|
|
if (IS_ERR(file))
|
|
|
|
goto no_file;
|
|
|
|
|
2018-03-23 05:29:57 +00:00
|
|
|
shp->shm_cprid = get_pid(task_tgid(current));
|
|
|
|
shp->shm_lprid = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->shm_atim = shp->shm_dtim = 0;
|
2017-08-03 02:51:14 +00:00
|
|
|
shp->shm_ctim = ktime_get_real_seconds();
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->shm_segsz = size;
|
|
|
|
shp->shm_nattch = 0;
|
|
|
|
shp->shm_file = file;
|
2011-07-28 23:55:31 +00:00
|
|
|
shp->shm_creator = current;
|
2015-09-30 16:48:40 +00:00
|
|
|
|
2017-11-17 23:31:11 +00:00
|
|
|
/* ipc_addid() locks shp upon success. */
|
2017-07-12 21:35:16 +00:00
|
|
|
error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
|
|
|
|
if (error < 0)
|
2015-09-30 16:48:40 +00:00
|
|
|
goto no_id;
|
|
|
|
|
shm: make exit_shm work proportional to task activity
This is small set of patches our team has had kicking around for a few
versions internally that fixes tasks getting hung on shm_exit when there
are many threads hammering it at once.
Anton wrote a simple test to cause the issue:
http://ozlabs.org/~anton/junkcode/bust_shm_exit.c
Before applying this patchset, this test code will cause either hanging
tracebacks or pthread out of memory errors.
After this patchset, it will still produce output like:
root@somehost:~# ./bust_shm_exit 1024 160
...
INFO: rcu_sched detected stalls on CPUs/tasks: {} (detected by 116, t=2111 jiffies, g=241, c=240, q=7113)
INFO: Stall ended before state dump start
...
But the task will continue to run along happily, so we consider this an
improvement over hanging, even if it's a bit noisy.
This patch (of 3):
exit_shm obtains the ipc_ns shm rwsem for write and holds it while it
walks every shared memory segment in the namespace. Thus the amount of
work is related to the number of shm segments in the namespace not the
number of segments that might need to be cleaned.
In addition, this occurs after the task has been notified the thread has
exited, so the number of tasks waiting for the ns shm rwsem can grow
without bound until memory is exausted.
Add a list to the task struct of all shmids allocated by this task. Init
the list head in copy_process. Use the ns->rwsem for locking. Add
segments after id is added, remove before removing from id.
On unshare of NEW_IPCNS orphan any ids as if the task had exited, similar
to handling of semaphore undo.
I chose a define for the init sequence since its a simple list init,
otherwise it would require a function call to avoid include loops between
the semaphore code and the task struct. Converting the list_del to
list_del_init for the unshare cases would remove the exit followed by
init, but I left it blow up if not inited.
Signed-off-by: Milton Miller <miltonm@bga.com>
Signed-off-by: Jack Miller <millerjo@us.ibm.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Anton Blanchard <anton@samba.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:23:19 +00:00
|
|
|
list_add(&shp->shm_clist, ¤t->sysvshm.shm_clist);
|
ipc: move rcu lock out of ipc_addid
This patchset continues the work that began in the sysv ipc semaphore
scaling series, see
https://lkml.org/lkml/2013/3/20/546
Just like semaphores used to be, sysv shared memory and msg queues also
abuse the ipc lock, unnecessarily holding it for operations such as
permission and security checks.
This patchset mostly deals with mqueues, and while shared mem can be
done in a very similar way, I want to get these patches out in the open
first. It also does some pending cleanups, mostly focused on the two
level locking we have in ipc code, taking care of ipc_addid() and
ipcctl_pre_down_nolock() - yes there are still functions that need to be
updated as well.
This patch:
Make all callers explicitly take and release the RCU read lock.
This addresses the two level locking seen in newary(), newseg() and
newqueue(). For the last two, explicitly unlock the ipc object and the
rcu lock, instead of calling the custom shm_unlock and msg_unlock
functions. The next patch will deal with the open coded locking for
->perm.lock
Signed-off-by: Davidlohr Bueso <davidlohr.bueso@hp.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-07-08 23:01:09 +00:00
|
|
|
|
2007-06-16 17:15:59 +00:00
|
|
|
/*
|
|
|
|
* shmid gets reported as "inode#" in /proc/pid/maps.
|
|
|
|
* proc-ps tools use this. Changing this will break them.
|
|
|
|
*/
|
2013-01-23 22:07:38 +00:00
|
|
|
file_inode(file)->i_ino = shp->shm_perm.id;
|
2005-10-30 01:16:45 +00:00
|
|
|
|
2006-10-02 09:18:22 +00:00
|
|
|
ns->shm_tot += numpages;
|
2007-10-19 06:40:48 +00:00
|
|
|
error = shp->shm_perm.id;
|
ipc: move rcu lock out of ipc_addid
This patchset continues the work that began in the sysv ipc semaphore
scaling series, see
https://lkml.org/lkml/2013/3/20/546
Just like semaphores used to be, sysv shared memory and msg queues also
abuse the ipc lock, unnecessarily holding it for operations such as
permission and security checks.
This patchset mostly deals with mqueues, and while shared mem can be
done in a very similar way, I want to get these patches out in the open
first. It also does some pending cleanups, mostly focused on the two
level locking we have in ipc code, taking care of ipc_addid() and
ipcctl_pre_down_nolock() - yes there are still functions that need to be
updated as well.
This patch:
Make all callers explicitly take and release the RCU read lock.
This addresses the two level locking seen in newary(), newseg() and
newqueue(). For the last two, explicitly unlock the ipc object and the
rcu lock, instead of calling the custom shm_unlock and msg_unlock
functions. The next patch will deal with the open coded locking for
->perm.lock
Signed-off-by: Davidlohr Bueso <davidlohr.bueso@hp.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-07-08 23:01:09 +00:00
|
|
|
|
2013-07-08 23:01:11 +00:00
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
ipc: move rcu lock out of ipc_addid
This patchset continues the work that began in the sysv ipc semaphore
scaling series, see
https://lkml.org/lkml/2013/3/20/546
Just like semaphores used to be, sysv shared memory and msg queues also
abuse the ipc lock, unnecessarily holding it for operations such as
permission and security checks.
This patchset mostly deals with mqueues, and while shared mem can be
done in a very similar way, I want to get these patches out in the open
first. It also does some pending cleanups, mostly focused on the two
level locking we have in ipc code, taking care of ipc_addid() and
ipcctl_pre_down_nolock() - yes there are still functions that need to be
updated as well.
This patch:
Make all callers explicitly take and release the RCU read lock.
This addresses the two level locking seen in newary(), newseg() and
newqueue(). For the last two, explicitly unlock the ipc object and the
rcu lock, instead of calling the custom shm_unlock and msg_unlock
functions. The next patch will deal with the open coded locking for
->perm.lock
Signed-off-by: Davidlohr Bueso <davidlohr.bueso@hp.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-07-08 23:01:09 +00:00
|
|
|
rcu_read_unlock();
|
2007-10-19 06:40:48 +00:00
|
|
|
return error;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
no_id:
|
2018-03-28 18:38:55 +00:00
|
|
|
ipc_update_pid(&shp->shm_cprid, NULL);
|
|
|
|
ipc_update_pid(&shp->shm_lprid, NULL);
|
2009-09-12 11:21:27 +00:00
|
|
|
if (is_file_hugepages(file) && shp->mlock_user)
|
2009-08-24 15:30:28 +00:00
|
|
|
user_shm_unlock(size, shp->mlock_user);
|
2005-04-16 22:20:36 +00:00
|
|
|
fput(file);
|
2018-08-22 05:01:29 +00:00
|
|
|
ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
|
|
|
|
return error;
|
2005-04-16 22:20:36 +00:00
|
|
|
no_file:
|
2017-07-12 21:35:16 +00:00
|
|
|
call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-10-19 06:40:53 +00:00
|
|
|
/*
|
2013-09-11 21:26:24 +00:00
|
|
|
* Called with shm_ids.rwsem and ipcp locked.
|
2007-10-19 06:40:53 +00:00
|
|
|
*/
|
2007-10-19 06:40:51 +00:00
|
|
|
static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
|
|
|
|
struct ipc_params *params)
|
2007-10-19 06:40:49 +00:00
|
|
|
{
|
2007-10-19 06:40:51 +00:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
|
|
|
if (shp->shm_segsz < params->u.size)
|
2007-10-19 06:40:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:07:53 +00:00
|
|
|
long ksys_shmget(key_t key, size_t size, int shmflg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-10-02 09:18:22 +00:00
|
|
|
struct ipc_namespace *ns;
|
2014-06-06 21:37:36 +00:00
|
|
|
static const struct ipc_ops shm_ops = {
|
|
|
|
.getnew = newseg,
|
2018-03-24 04:41:55 +00:00
|
|
|
.associate = security_shm_associate,
|
2014-06-06 21:37:36 +00:00
|
|
|
.more_checks = shm_more_checks,
|
|
|
|
};
|
2007-10-19 06:40:49 +00:00
|
|
|
struct ipc_params shm_params;
|
2006-10-02 09:18:22 +00:00
|
|
|
|
|
|
|
ns = current->nsproxy->ipc_ns;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-19 06:40:49 +00:00
|
|
|
shm_params.key = key;
|
|
|
|
shm_params.flg = shmflg;
|
|
|
|
shm_params.u.size = size;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-19 06:40:49 +00:00
|
|
|
return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 19:07:53 +00:00
|
|
|
SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
|
|
|
|
{
|
|
|
|
return ksys_shmget(key, size, shmflg);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
|
|
|
|
{
|
2014-01-28 01:07:04 +00:00
|
|
|
switch (version) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case IPC_64:
|
|
|
|
return copy_to_user(buf, in, sizeof(*in));
|
|
|
|
case IPC_OLD:
|
|
|
|
{
|
|
|
|
struct shmid_ds out;
|
|
|
|
|
2010-10-30 14:22:49 +00:00
|
|
|
memset(&out, 0, sizeof(out));
|
2005-04-16 22:20:36 +00:00
|
|
|
ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
|
|
|
|
out.shm_segsz = in->shm_segsz;
|
|
|
|
out.shm_atime = in->shm_atime;
|
|
|
|
out.shm_dtime = in->shm_dtime;
|
|
|
|
out.shm_ctime = in->shm_ctime;
|
|
|
|
out.shm_cpid = in->shm_cpid;
|
|
|
|
out.shm_lpid = in->shm_lpid;
|
|
|
|
out.shm_nattch = in->shm_nattch;
|
|
|
|
|
|
|
|
return copy_to_user(buf, &out, sizeof(out));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 08:00:50 +00:00
|
|
|
static inline unsigned long
|
|
|
|
copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2014-01-28 01:07:04 +00:00
|
|
|
switch (version) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case IPC_64:
|
2008-04-29 08:00:50 +00:00
|
|
|
if (copy_from_user(out, buf, sizeof(*out)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
case IPC_OLD:
|
|
|
|
{
|
|
|
|
struct shmid_ds tbuf_old;
|
|
|
|
|
|
|
|
if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2008-04-29 08:00:50 +00:00
|
|
|
out->shm_perm.uid = tbuf_old.shm_perm.uid;
|
|
|
|
out->shm_perm.gid = tbuf_old.shm_perm.gid;
|
|
|
|
out->shm_perm.mode = tbuf_old.shm_perm.mode;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
|
|
|
|
{
|
2014-01-28 01:07:04 +00:00
|
|
|
switch (version) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case IPC_64:
|
|
|
|
return copy_to_user(buf, in, sizeof(*in));
|
|
|
|
case IPC_OLD:
|
|
|
|
{
|
|
|
|
struct shminfo out;
|
|
|
|
|
2014-01-28 01:07:04 +00:00
|
|
|
if (in->shmmax > INT_MAX)
|
2005-04-16 22:20:36 +00:00
|
|
|
out.shmmax = INT_MAX;
|
|
|
|
else
|
|
|
|
out.shmmax = (int)in->shmmax;
|
|
|
|
|
|
|
|
out.shmmin = in->shmmin;
|
|
|
|
out.shmmni = in->shmmni;
|
|
|
|
out.shmseg = in->shmseg;
|
2014-06-06 21:37:37 +00:00
|
|
|
out.shmall = in->shmall;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return copy_to_user(buf, &out, sizeof(out));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-27 22:34:16 +00:00
|
|
|
/*
|
|
|
|
* Calculate and add used RSS and swap pages of a shm.
|
2013-09-11 21:26:24 +00:00
|
|
|
* Called with shm_ids.rwsem held as a reader
|
2010-10-27 22:34:16 +00:00
|
|
|
*/
|
|
|
|
static void shm_add_rss_swap(struct shmid_kernel *shp,
|
|
|
|
unsigned long *rss_add, unsigned long *swp_add)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
2013-01-23 22:07:38 +00:00
|
|
|
inode = file_inode(shp->shm_file);
|
2010-10-27 22:34:16 +00:00
|
|
|
|
|
|
|
if (is_file_hugepages(shp->shm_file)) {
|
|
|
|
struct address_space *mapping = inode->i_mapping;
|
|
|
|
struct hstate *h = hstate_file(shp->shm_file);
|
|
|
|
*rss_add += pages_per_huge_page(h) * mapping->nrpages;
|
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_SHMEM
|
|
|
|
struct shmem_inode_info *info = SHMEM_I(inode);
|
2016-12-14 23:06:10 +00:00
|
|
|
|
2016-07-26 22:26:29 +00:00
|
|
|
spin_lock_irq(&info->lock);
|
2010-10-27 22:34:16 +00:00
|
|
|
*rss_add += inode->i_mapping->nrpages;
|
|
|
|
*swp_add += info->swapped;
|
2016-07-26 22:26:29 +00:00
|
|
|
spin_unlock_irq(&info->lock);
|
2010-10-27 22:34:16 +00:00
|
|
|
#else
|
|
|
|
*rss_add += inode->i_mapping->nrpages;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 06:40:53 +00:00
|
|
|
/*
|
2013-09-11 21:26:24 +00:00
|
|
|
* Called with shm_ids.rwsem held as a reader
|
2007-10-19 06:40:53 +00:00
|
|
|
*/
|
2006-10-02 09:18:22 +00:00
|
|
|
static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
|
|
|
|
unsigned long *swp)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-10-19 06:40:48 +00:00
|
|
|
int next_id;
|
|
|
|
int total, in_use;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
*rss = 0;
|
|
|
|
*swp = 0;
|
|
|
|
|
2007-10-19 06:40:48 +00:00
|
|
|
in_use = shm_ids(ns).in_use;
|
|
|
|
|
|
|
|
for (total = 0, next_id = 0; total < in_use; next_id++) {
|
2009-04-02 23:58:26 +00:00
|
|
|
struct kern_ipc_perm *ipc;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct shmid_kernel *shp;
|
|
|
|
|
2009-04-02 23:58:26 +00:00
|
|
|
ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
|
|
|
|
if (ipc == NULL)
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
2009-04-02 23:58:26 +00:00
|
|
|
shp = container_of(ipc, struct shmid_kernel, shm_perm);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-10-27 22:34:16 +00:00
|
|
|
shm_add_rss_swap(shp, rss, swp);
|
2007-10-19 06:40:48 +00:00
|
|
|
|
|
|
|
total++;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 08:00:47 +00:00
|
|
|
/*
|
2013-09-11 21:26:24 +00:00
|
|
|
* This function handles some shmctl commands which require the rwsem
|
2008-04-29 08:00:47 +00:00
|
|
|
* to be held in write mode.
|
2013-09-11 21:26:24 +00:00
|
|
|
* NOTE: no locks must be held, the rwsem is taken inside this function.
|
2008-04-29 08:00:47 +00:00
|
|
|
*/
|
|
|
|
static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
|
2017-07-09 00:58:06 +00:00
|
|
|
struct shmid64_ds *shmid64)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-04-29 08:00:47 +00:00
|
|
|
struct kern_ipc_perm *ipcp;
|
|
|
|
struct shmid_kernel *shp;
|
|
|
|
int err;
|
|
|
|
|
2013-09-11 21:26:24 +00:00
|
|
|
down_write(&shm_ids(ns).rwsem);
|
2013-07-08 23:01:12 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
|
2018-08-22 05:01:34 +00:00
|
|
|
ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd,
|
2017-07-09 00:58:06 +00:00
|
|
|
&shmid64->shm_perm, 0);
|
2013-07-08 23:01:12 +00:00
|
|
|
if (IS_ERR(ipcp)) {
|
|
|
|
err = PTR_ERR(ipcp);
|
|
|
|
goto out_unlock1;
|
|
|
|
}
|
2008-04-29 08:00:47 +00:00
|
|
|
|
2008-04-29 08:00:54 +00:00
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
2008-04-29 08:00:47 +00:00
|
|
|
|
2018-03-23 02:08:27 +00:00
|
|
|
err = security_shm_shmctl(&shp->shm_perm, cmd);
|
2008-04-29 08:00:47 +00:00
|
|
|
if (err)
|
2013-09-11 21:26:16 +00:00
|
|
|
goto out_unlock1;
|
2013-07-08 23:01:12 +00:00
|
|
|
|
2008-04-29 08:00:47 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case IPC_RMID:
|
2013-09-11 21:26:16 +00:00
|
|
|
ipc_lock_object(&shp->shm_perm);
|
2013-07-08 23:01:12 +00:00
|
|
|
/* do_shm_rmid unlocks the ipc object and rcu */
|
2008-04-29 08:00:47 +00:00
|
|
|
do_shm_rmid(ns, ipcp);
|
|
|
|
goto out_up;
|
|
|
|
case IPC_SET:
|
2013-09-11 21:26:16 +00:00
|
|
|
ipc_lock_object(&shp->shm_perm);
|
2017-07-09 00:58:06 +00:00
|
|
|
err = ipc_update_perm(&shmid64->shm_perm, ipcp);
|
2012-02-08 00:54:11 +00:00
|
|
|
if (err)
|
2013-07-08 23:01:12 +00:00
|
|
|
goto out_unlock0;
|
2017-08-03 02:51:14 +00:00
|
|
|
shp->shm_ctim = ktime_get_real_seconds();
|
2008-04-29 08:00:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
2013-09-11 21:26:16 +00:00
|
|
|
goto out_unlock1;
|
2008-04-29 08:00:47 +00:00
|
|
|
}
|
2013-07-08 23:01:12 +00:00
|
|
|
|
|
|
|
out_unlock0:
|
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
|
|
|
out_unlock1:
|
|
|
|
rcu_read_unlock();
|
2008-04-29 08:00:47 +00:00
|
|
|
out_up:
|
2013-09-11 21:26:24 +00:00
|
|
|
up_write(&shm_ids(ns).rwsem);
|
2008-04-29 08:00:47 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
static int shmctl_ipc_info(struct ipc_namespace *ns,
|
|
|
|
struct shminfo64 *shminfo)
|
2008-04-29 08:00:47 +00:00
|
|
|
{
|
2017-07-09 00:58:06 +00:00
|
|
|
int err = security_shm_shmctl(NULL, IPC_INFO);
|
|
|
|
if (!err) {
|
|
|
|
memset(shminfo, 0, sizeof(*shminfo));
|
|
|
|
shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni;
|
|
|
|
shminfo->shmmax = ns->shm_ctlmax;
|
|
|
|
shminfo->shmall = ns->shm_ctlall;
|
|
|
|
shminfo->shmmin = SHMMIN;
|
2013-09-11 21:26:24 +00:00
|
|
|
down_read(&shm_ids(ns).rwsem);
|
2018-08-22 05:02:00 +00:00
|
|
|
err = ipc_get_maxidx(&shm_ids(ns));
|
2013-09-11 21:26:24 +00:00
|
|
|
up_read(&shm_ids(ns).rwsem);
|
2014-01-28 01:07:04 +00:00
|
|
|
if (err < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
err = 0;
|
|
|
|
}
|
2017-07-09 00:58:06 +00:00
|
|
|
return err;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
static int shmctl_shm_info(struct ipc_namespace *ns,
|
|
|
|
struct shm_info *shm_info)
|
|
|
|
{
|
|
|
|
int err = security_shm_shmctl(NULL, SHM_INFO);
|
|
|
|
if (!err) {
|
|
|
|
memset(shm_info, 0, sizeof(*shm_info));
|
2013-09-11 21:26:24 +00:00
|
|
|
down_read(&shm_ids(ns).rwsem);
|
2017-07-09 00:58:06 +00:00
|
|
|
shm_info->used_ids = shm_ids(ns).in_use;
|
|
|
|
shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp);
|
|
|
|
shm_info->shm_tot = ns->shm_tot;
|
|
|
|
shm_info->swap_attempts = 0;
|
|
|
|
shm_info->swap_successes = 0;
|
2018-08-22 05:02:00 +00:00
|
|
|
err = ipc_get_maxidx(&shm_ids(ns));
|
2013-09-11 21:26:24 +00:00
|
|
|
up_read(&shm_ids(ns).rwsem);
|
2017-07-09 00:58:06 +00:00
|
|
|
if (err < 0)
|
|
|
|
err = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2017-07-09 00:58:06 +00:00
|
|
|
return err;
|
|
|
|
}
|
2013-09-11 21:26:20 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
static int shmctl_stat(struct ipc_namespace *ns, int shmid,
|
|
|
|
int cmd, struct shmid64_ds *tbuf)
|
|
|
|
{
|
|
|
|
struct shmid_kernel *shp;
|
|
|
|
int err;
|
2013-09-11 21:26:20 +00:00
|
|
|
|
2018-02-06 23:40:49 +00:00
|
|
|
memset(tbuf, 0, sizeof(*tbuf));
|
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
rcu_read_lock();
|
ipc/shm: introduce shmctl(SHM_STAT_ANY)
Patch series "sysvipc: introduce STAT_ANY commands", v2.
The following patches adds the discussed (see [1]) new command for shm
as well as for sems and msq as they are subject to the same
discrepancies for ipc object permission checks between the syscall and
via procfs. These new commands are justified in that (1) we are stuck
with this semantics as changing syscall and procfs can break userland;
and (2) some users can benefit from performance (for large amounts of
shm segments, for example) from not having to parse the procfs
interface.
Once merged, I will submit the necesary manpage updates. But I'm thinking
something like:
: diff --git a/man2/shmctl.2 b/man2/shmctl.2
: index 7bb503999941..bb00bbe21a57 100644
: --- a/man2/shmctl.2
: +++ b/man2/shmctl.2
: @@ -41,6 +41,7 @@
: .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
: .\" attaches to a segment that has already been marked for deletion.
: .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
: +.\" 2018-02-13, dbueso: Added SHM_STAT_ANY description.
: .\"
: .TH SHMCTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
: .SH NAME
: @@ -242,6 +243,18 @@ However, the
: argument is not a segment identifier, but instead an index into
: the kernel's internal array that maintains information about
: all shared memory segments on the system.
: +.TP
: +.BR SHM_STAT_ANY " (Linux-specific)"
: +Return a
: +.I shmid_ds
: +structure as for
: +.BR SHM_STAT .
: +However, the
: +.I shm_perm.mode
: +is not checked for read access for
: +.IR shmid ,
: +resembing the behaviour of
: +/proc/sysvipc/shm.
: .PP
: The caller can prevent or allow swapping of a shared
: memory segment with the following \fIcmd\fP values:
: @@ -287,7 +300,7 @@ operation returns the index of the highest used entry in the
: kernel's internal array recording information about all
: shared memory segments.
: (This information can be used with repeated
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operations to obtain information about all shared memory segments
: on the system.)
: A successful
: @@ -328,7 +341,7 @@ isn't accessible.
: \fIshmid\fP is not a valid identifier, or \fIcmd\fP
: is not a valid command.
: Or: for a
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operation, the index value specified in
: .I shmid
: referred to an array slot that is currently unused.
This patch (of 3):
There is a permission discrepancy when consulting shm ipc object metadata
between /proc/sysvipc/shm (0444) and the SHM_STAT shmctl command. The
later does permission checks for the object vs S_IRUGO. As such there can
be cases where EACCESS is returned via syscall but the info is displayed
anyways in the procfs files.
While this might have security implications via info leaking (albeit no
writing to the shm metadata), this behavior goes way back and showing all
the objects regardless of the permissions was most likely an overlook - so
we are stuck with it. Furthermore, modifying either the syscall or the
procfs file can cause userspace programs to break (ie ipcs). Some
applications require getting the procfs info (without root privileges) and
can be rather slow in comparison with a syscall -- up to 500x in some
reported cases.
This patch introduces a new SHM_STAT_ANY command such that the shm ipc
object permissions are ignored, and only audited instead. In addition,
I've left the lsm security hook checks in place, as if some policy can
block the call, then the user has no other choice than just parsing the
procfs file.
[1] https://lkml.org/lkml/2017/12/19/220
Link: http://lkml.kernel.org/r/20180215162458.10059-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Robert Kettler <robert.kettler@outlook.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-10 23:35:23 +00:00
|
|
|
if (cmd == SHM_STAT || cmd == SHM_STAT_ANY) {
|
2017-07-09 00:58:06 +00:00
|
|
|
shp = shm_obtain_object(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
ipc/shm: introduce shmctl(SHM_STAT_ANY)
Patch series "sysvipc: introduce STAT_ANY commands", v2.
The following patches adds the discussed (see [1]) new command for shm
as well as for sems and msq as they are subject to the same
discrepancies for ipc object permission checks between the syscall and
via procfs. These new commands are justified in that (1) we are stuck
with this semantics as changing syscall and procfs can break userland;
and (2) some users can benefit from performance (for large amounts of
shm segments, for example) from not having to parse the procfs
interface.
Once merged, I will submit the necesary manpage updates. But I'm thinking
something like:
: diff --git a/man2/shmctl.2 b/man2/shmctl.2
: index 7bb503999941..bb00bbe21a57 100644
: --- a/man2/shmctl.2
: +++ b/man2/shmctl.2
: @@ -41,6 +41,7 @@
: .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
: .\" attaches to a segment that has already been marked for deletion.
: .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
: +.\" 2018-02-13, dbueso: Added SHM_STAT_ANY description.
: .\"
: .TH SHMCTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
: .SH NAME
: @@ -242,6 +243,18 @@ However, the
: argument is not a segment identifier, but instead an index into
: the kernel's internal array that maintains information about
: all shared memory segments on the system.
: +.TP
: +.BR SHM_STAT_ANY " (Linux-specific)"
: +Return a
: +.I shmid_ds
: +structure as for
: +.BR SHM_STAT .
: +However, the
: +.I shm_perm.mode
: +is not checked for read access for
: +.IR shmid ,
: +resembing the behaviour of
: +/proc/sysvipc/shm.
: .PP
: The caller can prevent or allow swapping of a shared
: memory segment with the following \fIcmd\fP values:
: @@ -287,7 +300,7 @@ operation returns the index of the highest used entry in the
: kernel's internal array recording information about all
: shared memory segments.
: (This information can be used with repeated
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operations to obtain information about all shared memory segments
: on the system.)
: A successful
: @@ -328,7 +341,7 @@ isn't accessible.
: \fIshmid\fP is not a valid identifier, or \fIcmd\fP
: is not a valid command.
: Or: for a
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operation, the index value specified in
: .I shmid
: referred to an array slot that is currently unused.
This patch (of 3):
There is a permission discrepancy when consulting shm ipc object metadata
between /proc/sysvipc/shm (0444) and the SHM_STAT shmctl command. The
later does permission checks for the object vs S_IRUGO. As such there can
be cases where EACCESS is returned via syscall but the info is displayed
anyways in the procfs files.
While this might have security implications via info leaking (albeit no
writing to the shm metadata), this behavior goes way back and showing all
the objects regardless of the permissions was most likely an overlook - so
we are stuck with it. Furthermore, modifying either the syscall or the
procfs file can cause userspace programs to break (ie ipcs). Some
applications require getting the procfs info (without root privileges) and
can be rather slow in comparison with a syscall -- up to 500x in some
reported cases.
This patch introduces a new SHM_STAT_ANY command such that the shm ipc
object permissions are ignored, and only audited instead. In addition,
I've left the lsm security hook checks in place, as if some policy can
block the call, then the user has no other choice than just parsing the
procfs file.
[1] https://lkml.org/lkml/2017/12/19/220
Link: http://lkml.kernel.org/r/20180215162458.10059-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Robert Kettler <robert.kettler@outlook.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-10 23:35:23 +00:00
|
|
|
} else { /* IPC_STAT */
|
2017-07-09 00:58:06 +00:00
|
|
|
shp = shm_obtain_object_check(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2017-07-09 00:58:06 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
ipc/shm: introduce shmctl(SHM_STAT_ANY)
Patch series "sysvipc: introduce STAT_ANY commands", v2.
The following patches adds the discussed (see [1]) new command for shm
as well as for sems and msq as they are subject to the same
discrepancies for ipc object permission checks between the syscall and
via procfs. These new commands are justified in that (1) we are stuck
with this semantics as changing syscall and procfs can break userland;
and (2) some users can benefit from performance (for large amounts of
shm segments, for example) from not having to parse the procfs
interface.
Once merged, I will submit the necesary manpage updates. But I'm thinking
something like:
: diff --git a/man2/shmctl.2 b/man2/shmctl.2
: index 7bb503999941..bb00bbe21a57 100644
: --- a/man2/shmctl.2
: +++ b/man2/shmctl.2
: @@ -41,6 +41,7 @@
: .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
: .\" attaches to a segment that has already been marked for deletion.
: .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
: +.\" 2018-02-13, dbueso: Added SHM_STAT_ANY description.
: .\"
: .TH SHMCTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
: .SH NAME
: @@ -242,6 +243,18 @@ However, the
: argument is not a segment identifier, but instead an index into
: the kernel's internal array that maintains information about
: all shared memory segments on the system.
: +.TP
: +.BR SHM_STAT_ANY " (Linux-specific)"
: +Return a
: +.I shmid_ds
: +structure as for
: +.BR SHM_STAT .
: +However, the
: +.I shm_perm.mode
: +is not checked for read access for
: +.IR shmid ,
: +resembing the behaviour of
: +/proc/sysvipc/shm.
: .PP
: The caller can prevent or allow swapping of a shared
: memory segment with the following \fIcmd\fP values:
: @@ -287,7 +300,7 @@ operation returns the index of the highest used entry in the
: kernel's internal array recording information about all
: shared memory segments.
: (This information can be used with repeated
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operations to obtain information about all shared memory segments
: on the system.)
: A successful
: @@ -328,7 +341,7 @@ isn't accessible.
: \fIshmid\fP is not a valid identifier, or \fIcmd\fP
: is not a valid command.
: Or: for a
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operation, the index value specified in
: .I shmid
: referred to an array slot that is currently unused.
This patch (of 3):
There is a permission discrepancy when consulting shm ipc object metadata
between /proc/sysvipc/shm (0444) and the SHM_STAT shmctl command. The
later does permission checks for the object vs S_IRUGO. As such there can
be cases where EACCESS is returned via syscall but the info is displayed
anyways in the procfs files.
While this might have security implications via info leaking (albeit no
writing to the shm metadata), this behavior goes way back and showing all
the objects regardless of the permissions was most likely an overlook - so
we are stuck with it. Furthermore, modifying either the syscall or the
procfs file can cause userspace programs to break (ie ipcs). Some
applications require getting the procfs info (without root privileges) and
can be rather slow in comparison with a syscall -- up to 500x in some
reported cases.
This patch introduces a new SHM_STAT_ANY command such that the shm ipc
object permissions are ignored, and only audited instead. In addition,
I've left the lsm security hook checks in place, as if some policy can
block the call, then the user has no other choice than just parsing the
procfs file.
[1] https://lkml.org/lkml/2017/12/19/220
Link: http://lkml.kernel.org/r/20180215162458.10059-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Robert Kettler <robert.kettler@outlook.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-10 23:35:23 +00:00
|
|
|
/*
|
|
|
|
* Semantically SHM_STAT_ANY ought to be identical to
|
|
|
|
* that functionality provided by the /proc/sysvipc/
|
|
|
|
* interface. As such, only audit these calls and
|
|
|
|
* do not do traditional S_IRUGO permission checks on
|
|
|
|
* the ipc object.
|
|
|
|
*/
|
|
|
|
if (cmd == SHM_STAT_ANY)
|
|
|
|
audit_ipc_obj(&shp->shm_perm);
|
|
|
|
else {
|
|
|
|
err = -EACCES;
|
|
|
|
if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2013-09-11 21:26:20 +00:00
|
|
|
|
2018-03-23 02:08:27 +00:00
|
|
|
err = security_shm_shmctl(&shp->shm_perm, cmd);
|
2017-07-09 00:58:06 +00:00
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
|
|
|
|
2018-02-06 23:40:49 +00:00
|
|
|
ipc_lock_object(&shp->shm_perm);
|
|
|
|
|
|
|
|
if (!ipc_valid_object(&shp->shm_perm)) {
|
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
|
|
|
err = -EIDRM;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm);
|
|
|
|
tbuf->shm_segsz = shp->shm_segsz;
|
|
|
|
tbuf->shm_atime = shp->shm_atim;
|
|
|
|
tbuf->shm_dtime = shp->shm_dtim;
|
|
|
|
tbuf->shm_ctime = shp->shm_ctim;
|
2015-04-28 19:39:50 +00:00
|
|
|
#ifndef CONFIG_64BIT
|
|
|
|
tbuf->shm_atime_high = shp->shm_atim >> 32;
|
|
|
|
tbuf->shm_dtime_high = shp->shm_dtim >> 32;
|
|
|
|
tbuf->shm_ctime_high = shp->shm_ctim >> 32;
|
|
|
|
#endif
|
2018-03-23 05:29:57 +00:00
|
|
|
tbuf->shm_cpid = pid_vnr(shp->shm_cprid);
|
|
|
|
tbuf->shm_lpid = pid_vnr(shp->shm_lprid);
|
2017-07-09 00:58:06 +00:00
|
|
|
tbuf->shm_nattch = shp->shm_nattch;
|
2018-02-06 23:40:49 +00:00
|
|
|
|
2018-08-22 05:01:21 +00:00
|
|
|
if (cmd == IPC_STAT) {
|
|
|
|
/*
|
|
|
|
* As defined in SUS:
|
|
|
|
* Return 0 on success
|
|
|
|
*/
|
|
|
|
err = 0;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* SHM_STAT and SHM_STAT_ANY (both Linux specific)
|
|
|
|
* Return the full id, including the sequence number
|
|
|
|
*/
|
|
|
|
err = shp->shm_perm.id;
|
|
|
|
}
|
2013-09-11 21:26:18 +00:00
|
|
|
|
2018-08-22 05:01:21 +00:00
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
2013-09-11 21:26:18 +00:00
|
|
|
out_unlock:
|
2013-09-11 21:26:20 +00:00
|
|
|
rcu_read_unlock();
|
2013-09-11 21:26:18 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd)
|
2013-09-11 21:26:18 +00:00
|
|
|
{
|
|
|
|
struct shmid_kernel *shp;
|
2017-07-09 00:58:06 +00:00
|
|
|
struct file *shm_file;
|
|
|
|
int err;
|
2013-09-11 21:26:18 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
shp = shm_obtain_object_check(ns, shmid);
|
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
|
|
|
goto out_unlock1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2013-09-11 21:26:20 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
audit_ipc_obj(&(shp->shm_perm));
|
2018-03-23 02:08:27 +00:00
|
|
|
err = security_shm_shmctl(&shp->shm_perm, cmd);
|
2017-07-09 00:58:06 +00:00
|
|
|
if (err)
|
|
|
|
goto out_unlock1;
|
2013-09-11 21:26:20 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
ipc_lock_object(&shp->shm_perm);
|
2013-09-11 21:26:20 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
/* check if shm_destroy() is tearing down shp */
|
|
|
|
if (!ipc_valid_object(&shp->shm_perm)) {
|
|
|
|
err = -EIDRM;
|
|
|
|
goto out_unlock0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-04-02 21:07:33 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
|
|
|
|
kuid_t euid = current_euid();
|
2014-01-28 01:07:01 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
if (!uid_eq(euid, shp->shm_perm.uid) &&
|
|
|
|
!uid_eq(euid, shp->shm_perm.cuid)) {
|
|
|
|
err = -EPERM;
|
2014-01-28 01:07:01 +00:00
|
|
|
goto out_unlock0;
|
|
|
|
}
|
2017-07-09 00:58:06 +00:00
|
|
|
if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto out_unlock0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2013-09-11 21:26:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
shm_file = shp->shm_file;
|
|
|
|
if (is_file_hugepages(shm_file))
|
|
|
|
goto out_unlock0;
|
SHM_UNLOCK: fix long unpreemptible section
scan_mapping_unevictable_pages() is used to make SysV SHM_LOCKed pages
evictable again once the shared memory is unlocked. It does this with
pagevec_lookup()s across the whole object (which might occupy most of
memory), and takes 300ms to unlock 7GB here. A cond_resched() every
PAGEVEC_SIZE pages would be good.
However, KOSAKI-san points out that this is called under shmem.c's
info->lock, and it's also under shm.c's shm_lock(), both spinlocks.
There is no strong reason for that: we need to take these pages off the
unevictable list soonish, but those locks are not required for it.
So move the call to scan_mapping_unevictable_pages() from shmem.c's
unlock handling up to shm.c's unlock handling. Remove the recently
added barrier, not needed now we have spin_unlock() before the scan.
Use get_file(), with subsequent fput(), to make sure we have a reference
to mapping throughout scan_mapping_unevictable_pages(): that's something
that was previously guaranteed by the shm_lock().
Remove shmctl's lru_add_drain_all(): we don't fault in pages at SHM_LOCK
time, and we lazily discover them to be Unevictable later, so it serves
no purpose for SHM_LOCK; and serves no purpose for SHM_UNLOCK, since
pages still on pagevec are not marked Unevictable.
The original code avoided redundant rescans by checking VM_LOCKED flag
at its level: now avoid them by checking shp's SHM_LOCKED.
The original code called scan_mapping_unevictable_pages() on a locked
area at shm_destroy() time: perhaps we once had accounting cross-checks
which required that, but not now, so skip the overhead and just let
inode eviction deal with them.
Put check_move_unevictable_page() and scan_mapping_unevictable_pages()
under CONFIG_SHMEM (with stub for the TINY case when ramfs is used),
more as comment than to save space; comment them used for SHM_UNLOCK.
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Minchan Kim <minchan.kim@gmail.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Shaohua Li <shaohua.li@intel.com>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michel Lespinasse <walken@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-01-20 22:34:19 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
if (cmd == SHM_LOCK) {
|
|
|
|
struct user_struct *user = current_user();
|
2016-12-14 23:06:10 +00:00
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
err = shmem_lock(shm_file, 1, user);
|
|
|
|
if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
|
|
|
|
shp->shm_perm.mode |= SHM_LOCKED;
|
|
|
|
shp->mlock_user = user;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2017-07-09 00:58:06 +00:00
|
|
|
goto out_unlock0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 00:58:06 +00:00
|
|
|
/* SHM_UNLOCK */
|
|
|
|
if (!(shp->shm_perm.mode & SHM_LOCKED))
|
|
|
|
goto out_unlock0;
|
|
|
|
shmem_lock(shm_file, 0, shp->mlock_user);
|
|
|
|
shp->shm_perm.mode &= ~SHM_LOCKED;
|
|
|
|
shp->mlock_user = NULL;
|
|
|
|
get_file(shm_file);
|
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
|
|
|
rcu_read_unlock();
|
|
|
|
shmem_unlock_mapping(shm_file->f_mapping);
|
|
|
|
|
|
|
|
fput(shm_file);
|
|
|
|
return err;
|
|
|
|
|
2013-09-11 21:26:21 +00:00
|
|
|
out_unlock0:
|
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
|
|
|
out_unlock1:
|
2013-09-11 21:26:20 +00:00
|
|
|
rcu_read_unlock();
|
2013-09-11 21:26:18 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
ipc: rename old-style shmctl/semctl/msgctl syscalls
The behavior of these system calls is slightly different between
architectures, as determined by the CONFIG_ARCH_WANT_IPC_PARSE_VERSION
symbol. Most architectures that implement the split IPC syscalls don't set
that symbol and only get the modern version, but alpha, arm, microblaze,
mips-n32, mips-n64 and xtensa expect the caller to pass the IPC_64 flag.
For the architectures that so far only implement sys_ipc(), i.e. m68k,
mips-o32, powerpc, s390, sh, sparc, and x86-32, we want the new behavior
when adding the split syscalls, so we need to distinguish between the
two groups of architectures.
The method I picked for this distinction is to have a separate system call
entry point: sys_old_*ctl() now uses ipc_parse_version, while sys_*ctl()
does not. The system call tables of the five architectures are changed
accordingly.
As an additional benefit, we no longer need the configuration specific
definition for ipc_parse_version(), it always does the same thing now,
but simply won't get called on architectures with the modern interface.
A small downside is that on architectures that do set
ARCH_WANT_IPC_PARSE_VERSION, we now have an extra set of entry points
that are never called. They only add a few bytes of bloat, so it seems
better to keep them compared to adding yet another Kconfig symbol.
I considered adding new syscall numbers for the IPC_64 variants for
consistency, but decided against that for now.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-12-31 21:22:40 +00:00
|
|
|
static long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf, int version)
|
2013-09-11 21:26:18 +00:00
|
|
|
{
|
ipc: rename old-style shmctl/semctl/msgctl syscalls
The behavior of these system calls is slightly different between
architectures, as determined by the CONFIG_ARCH_WANT_IPC_PARSE_VERSION
symbol. Most architectures that implement the split IPC syscalls don't set
that symbol and only get the modern version, but alpha, arm, microblaze,
mips-n32, mips-n64 and xtensa expect the caller to pass the IPC_64 flag.
For the architectures that so far only implement sys_ipc(), i.e. m68k,
mips-o32, powerpc, s390, sh, sparc, and x86-32, we want the new behavior
when adding the split syscalls, so we need to distinguish between the
two groups of architectures.
The method I picked for this distinction is to have a separate system call
entry point: sys_old_*ctl() now uses ipc_parse_version, while sys_*ctl()
does not. The system call tables of the five architectures are changed
accordingly.
As an additional benefit, we no longer need the configuration specific
definition for ipc_parse_version(), it always does the same thing now,
but simply won't get called on architectures with the modern interface.
A small downside is that on architectures that do set
ARCH_WANT_IPC_PARSE_VERSION, we now have an extra set of entry points
that are never called. They only add a few bytes of bloat, so it seems
better to keep them compared to adding yet another Kconfig symbol.
I considered adding new syscall numbers for the IPC_64 variants for
consistency, but decided against that for now.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-12-31 21:22:40 +00:00
|
|
|
int err;
|
2013-09-11 21:26:18 +00:00
|
|
|
struct ipc_namespace *ns;
|
2017-07-09 02:52:47 +00:00
|
|
|
struct shmid64_ds sem64;
|
2013-09-11 21:26:18 +00:00
|
|
|
|
2013-09-11 21:26:21 +00:00
|
|
|
if (cmd < 0 || shmid < 0)
|
|
|
|
return -EINVAL;
|
2013-09-11 21:26:18 +00:00
|
|
|
|
|
|
|
ns = current->nsproxy->ipc_ns;
|
|
|
|
|
|
|
|
switch (cmd) {
|
2017-07-09 00:58:06 +00:00
|
|
|
case IPC_INFO: {
|
|
|
|
struct shminfo64 shminfo;
|
|
|
|
err = shmctl_ipc_info(ns, &shminfo);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
if (copy_shminfo_to_user(buf, &shminfo, version))
|
|
|
|
err = -EFAULT;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
case SHM_INFO: {
|
|
|
|
struct shm_info shm_info;
|
|
|
|
err = shmctl_shm_info(ns, &shm_info);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
if (copy_to_user(buf, &shm_info, sizeof(shm_info)))
|
|
|
|
err = -EFAULT;
|
|
|
|
return err;
|
|
|
|
}
|
2013-09-11 21:26:18 +00:00
|
|
|
case SHM_STAT:
|
ipc/shm: introduce shmctl(SHM_STAT_ANY)
Patch series "sysvipc: introduce STAT_ANY commands", v2.
The following patches adds the discussed (see [1]) new command for shm
as well as for sems and msq as they are subject to the same
discrepancies for ipc object permission checks between the syscall and
via procfs. These new commands are justified in that (1) we are stuck
with this semantics as changing syscall and procfs can break userland;
and (2) some users can benefit from performance (for large amounts of
shm segments, for example) from not having to parse the procfs
interface.
Once merged, I will submit the necesary manpage updates. But I'm thinking
something like:
: diff --git a/man2/shmctl.2 b/man2/shmctl.2
: index 7bb503999941..bb00bbe21a57 100644
: --- a/man2/shmctl.2
: +++ b/man2/shmctl.2
: @@ -41,6 +41,7 @@
: .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
: .\" attaches to a segment that has already been marked for deletion.
: .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
: +.\" 2018-02-13, dbueso: Added SHM_STAT_ANY description.
: .\"
: .TH SHMCTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
: .SH NAME
: @@ -242,6 +243,18 @@ However, the
: argument is not a segment identifier, but instead an index into
: the kernel's internal array that maintains information about
: all shared memory segments on the system.
: +.TP
: +.BR SHM_STAT_ANY " (Linux-specific)"
: +Return a
: +.I shmid_ds
: +structure as for
: +.BR SHM_STAT .
: +However, the
: +.I shm_perm.mode
: +is not checked for read access for
: +.IR shmid ,
: +resembing the behaviour of
: +/proc/sysvipc/shm.
: .PP
: The caller can prevent or allow swapping of a shared
: memory segment with the following \fIcmd\fP values:
: @@ -287,7 +300,7 @@ operation returns the index of the highest used entry in the
: kernel's internal array recording information about all
: shared memory segments.
: (This information can be used with repeated
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operations to obtain information about all shared memory segments
: on the system.)
: A successful
: @@ -328,7 +341,7 @@ isn't accessible.
: \fIshmid\fP is not a valid identifier, or \fIcmd\fP
: is not a valid command.
: Or: for a
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operation, the index value specified in
: .I shmid
: referred to an array slot that is currently unused.
This patch (of 3):
There is a permission discrepancy when consulting shm ipc object metadata
between /proc/sysvipc/shm (0444) and the SHM_STAT shmctl command. The
later does permission checks for the object vs S_IRUGO. As such there can
be cases where EACCESS is returned via syscall but the info is displayed
anyways in the procfs files.
While this might have security implications via info leaking (albeit no
writing to the shm metadata), this behavior goes way back and showing all
the objects regardless of the permissions was most likely an overlook - so
we are stuck with it. Furthermore, modifying either the syscall or the
procfs file can cause userspace programs to break (ie ipcs). Some
applications require getting the procfs info (without root privileges) and
can be rather slow in comparison with a syscall -- up to 500x in some
reported cases.
This patch introduces a new SHM_STAT_ANY command such that the shm ipc
object permissions are ignored, and only audited instead. In addition,
I've left the lsm security hook checks in place, as if some policy can
block the call, then the user has no other choice than just parsing the
procfs file.
[1] https://lkml.org/lkml/2017/12/19/220
Link: http://lkml.kernel.org/r/20180215162458.10059-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Robert Kettler <robert.kettler@outlook.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-10 23:35:23 +00:00
|
|
|
case SHM_STAT_ANY:
|
2017-07-09 00:58:06 +00:00
|
|
|
case IPC_STAT: {
|
2017-07-09 02:52:47 +00:00
|
|
|
err = shmctl_stat(ns, shmid, cmd, &sem64);
|
2017-07-09 00:58:06 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2017-07-09 02:52:47 +00:00
|
|
|
if (copy_shmid_to_user(buf, &sem64, version))
|
2017-07-09 00:58:06 +00:00
|
|
|
err = -EFAULT;
|
|
|
|
return err;
|
|
|
|
}
|
2013-09-11 21:26:21 +00:00
|
|
|
case IPC_SET:
|
2017-07-09 02:52:47 +00:00
|
|
|
if (copy_shmid_from_user(&sem64, buf, version))
|
2017-07-09 00:58:06 +00:00
|
|
|
return -EFAULT;
|
2017-07-09 02:52:47 +00:00
|
|
|
/* fallthru */
|
2017-07-09 00:58:06 +00:00
|
|
|
case IPC_RMID:
|
2017-07-09 02:52:47 +00:00
|
|
|
return shmctl_down(ns, shmid, cmd, &sem64);
|
2005-04-16 22:20:36 +00:00
|
|
|
case SHM_LOCK:
|
|
|
|
case SHM_UNLOCK:
|
2017-07-09 00:58:06 +00:00
|
|
|
return shmctl_do_lock(ns, shmid, cmd);
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2008-10-19 03:26:43 +00:00
|
|
|
|
2018-03-20 19:12:33 +00:00
|
|
|
SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
|
|
|
|
{
|
ipc: rename old-style shmctl/semctl/msgctl syscalls
The behavior of these system calls is slightly different between
architectures, as determined by the CONFIG_ARCH_WANT_IPC_PARSE_VERSION
symbol. Most architectures that implement the split IPC syscalls don't set
that symbol and only get the modern version, but alpha, arm, microblaze,
mips-n32, mips-n64 and xtensa expect the caller to pass the IPC_64 flag.
For the architectures that so far only implement sys_ipc(), i.e. m68k,
mips-o32, powerpc, s390, sh, sparc, and x86-32, we want the new behavior
when adding the split syscalls, so we need to distinguish between the
two groups of architectures.
The method I picked for this distinction is to have a separate system call
entry point: sys_old_*ctl() now uses ipc_parse_version, while sys_*ctl()
does not. The system call tables of the five architectures are changed
accordingly.
As an additional benefit, we no longer need the configuration specific
definition for ipc_parse_version(), it always does the same thing now,
but simply won't get called on architectures with the modern interface.
A small downside is that on architectures that do set
ARCH_WANT_IPC_PARSE_VERSION, we now have an extra set of entry points
that are never called. They only add a few bytes of bloat, so it seems
better to keep them compared to adding yet another Kconfig symbol.
I considered adding new syscall numbers for the IPC_64 variants for
consistency, but decided against that for now.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-12-31 21:22:40 +00:00
|
|
|
return ksys_shmctl(shmid, cmd, buf, IPC_64);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
|
|
|
|
long ksys_old_shmctl(int shmid, int cmd, struct shmid_ds __user *buf)
|
|
|
|
{
|
|
|
|
int version = ipc_parse_version(&cmd);
|
|
|
|
|
|
|
|
return ksys_shmctl(shmid, cmd, buf, version);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
|
|
|
|
{
|
|
|
|
return ksys_old_shmctl(shmid, cmd, buf);
|
2018-03-20 19:12:33 +00:00
|
|
|
}
|
ipc: rename old-style shmctl/semctl/msgctl syscalls
The behavior of these system calls is slightly different between
architectures, as determined by the CONFIG_ARCH_WANT_IPC_PARSE_VERSION
symbol. Most architectures that implement the split IPC syscalls don't set
that symbol and only get the modern version, but alpha, arm, microblaze,
mips-n32, mips-n64 and xtensa expect the caller to pass the IPC_64 flag.
For the architectures that so far only implement sys_ipc(), i.e. m68k,
mips-o32, powerpc, s390, sh, sparc, and x86-32, we want the new behavior
when adding the split syscalls, so we need to distinguish between the
two groups of architectures.
The method I picked for this distinction is to have a separate system call
entry point: sys_old_*ctl() now uses ipc_parse_version, while sys_*ctl()
does not. The system call tables of the five architectures are changed
accordingly.
As an additional benefit, we no longer need the configuration specific
definition for ipc_parse_version(), it always does the same thing now,
but simply won't get called on architectures with the modern interface.
A small downside is that on architectures that do set
ARCH_WANT_IPC_PARSE_VERSION, we now have an extra set of entry points
that are never called. They only add a few bytes of bloat, so it seems
better to keep them compared to adding yet another Kconfig symbol.
I considered adding new syscall numbers for the IPC_64 variants for
consistency, but decided against that for now.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-12-31 21:22:40 +00:00
|
|
|
#endif
|
2018-03-20 19:12:33 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
|
|
|
|
struct compat_shmid_ds {
|
|
|
|
struct compat_ipc_perm shm_perm;
|
|
|
|
int shm_segsz;
|
y2038: globally rename compat_time to old_time32
Christoph Hellwig suggested a slightly different path for handling
backwards compatibility with the 32-bit time_t based system calls:
Rather than simply reusing the compat_sys_* entry points on 32-bit
architectures unchanged, we get rid of those entry points and the
compat_time types by renaming them to something that makes more sense
on 32-bit architectures (which don't have a compat mode otherwise),
and then share the entry points under the new name with the 64-bit
architectures that use them for implementing the compatibility.
The following types and interfaces are renamed here, and moved
from linux/compat_time.h to linux/time32.h:
old new
--- ---
compat_time_t old_time32_t
struct compat_timeval struct old_timeval32
struct compat_timespec struct old_timespec32
struct compat_itimerspec struct old_itimerspec32
ns_to_compat_timeval() ns_to_old_timeval32()
get_compat_itimerspec64() get_old_itimerspec32()
put_compat_itimerspec64() put_old_itimerspec32()
compat_get_timespec64() get_old_timespec32()
compat_put_timespec64() put_old_timespec32()
As we already have aliases in place, this patch addresses only the
instances that are relevant to the system call interface in particular,
not those that occur in device drivers and other modules. Those
will get handled separately, while providing the 64-bit version
of the respective interfaces.
I'm not renaming the timex, rusage and itimerval structures, as we are
still debating what the new interface will look like, and whether we
will need a replacement at all.
This also doesn't change the names of the syscall entry points, which can
be done more easily when we actually switch over the 32-bit architectures
to use them, at that point we need to change COMPAT_SYSCALL_DEFINEx to
SYSCALL_DEFINEx with a new name, e.g. with a _time32 suffix.
Suggested-by: Christoph Hellwig <hch@infradead.org>
Link: https://lore.kernel.org/lkml/20180705222110.GA5698@infradead.org/
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-07-13 10:52:28 +00:00
|
|
|
old_time32_t shm_atime;
|
|
|
|
old_time32_t shm_dtime;
|
|
|
|
old_time32_t shm_ctime;
|
2017-07-09 02:52:47 +00:00
|
|
|
compat_ipc_pid_t shm_cpid;
|
|
|
|
compat_ipc_pid_t shm_lpid;
|
|
|
|
unsigned short shm_nattch;
|
|
|
|
unsigned short shm_unused;
|
|
|
|
compat_uptr_t shm_unused2;
|
|
|
|
compat_uptr_t shm_unused3;
|
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
struct compat_shminfo64 {
|
|
|
|
compat_ulong_t shmmax;
|
|
|
|
compat_ulong_t shmmin;
|
|
|
|
compat_ulong_t shmmni;
|
|
|
|
compat_ulong_t shmseg;
|
|
|
|
compat_ulong_t shmall;
|
|
|
|
compat_ulong_t __unused1;
|
|
|
|
compat_ulong_t __unused2;
|
|
|
|
compat_ulong_t __unused3;
|
|
|
|
compat_ulong_t __unused4;
|
|
|
|
};
|
2006-04-02 21:07:33 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
struct compat_shm_info {
|
|
|
|
compat_int_t used_ids;
|
|
|
|
compat_ulong_t shm_tot, shm_rss, shm_swp;
|
|
|
|
compat_ulong_t swap_attempts, swap_successes;
|
|
|
|
};
|
2014-01-28 01:07:01 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
|
|
|
|
int version)
|
|
|
|
{
|
|
|
|
if (in->shmmax > INT_MAX)
|
|
|
|
in->shmmax = INT_MAX;
|
|
|
|
if (version == IPC_64) {
|
|
|
|
struct compat_shminfo64 info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.shmmax = in->shmmax;
|
|
|
|
info.shmmin = in->shmmin;
|
|
|
|
info.shmmni = in->shmmni;
|
|
|
|
info.shmseg = in->shmseg;
|
|
|
|
info.shmall = in->shmall;
|
|
|
|
return copy_to_user(buf, &info, sizeof(info));
|
|
|
|
} else {
|
|
|
|
struct shminfo info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.shmmax = in->shmmax;
|
|
|
|
info.shmmin = in->shmmin;
|
|
|
|
info.shmmni = in->shmmni;
|
|
|
|
info.shmseg = in->shmseg;
|
|
|
|
info.shmall = in->shmall;
|
|
|
|
return copy_to_user(buf, &info, sizeof(info));
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 01:07:01 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
static int put_compat_shm_info(struct shm_info *ip,
|
|
|
|
struct compat_shm_info __user *uip)
|
|
|
|
{
|
|
|
|
struct compat_shm_info info;
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.used_ids = ip->used_ids;
|
|
|
|
info.shm_tot = ip->shm_tot;
|
|
|
|
info.shm_rss = ip->shm_rss;
|
|
|
|
info.shm_swp = ip->shm_swp;
|
|
|
|
info.swap_attempts = ip->swap_attempts;
|
|
|
|
info.swap_successes = ip->swap_successes;
|
2017-09-26 00:38:45 +00:00
|
|
|
return copy_to_user(uip, &info, sizeof(info));
|
2017-07-09 02:52:47 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in,
|
|
|
|
int version)
|
|
|
|
{
|
|
|
|
if (version == IPC_64) {
|
|
|
|
struct compat_shmid64_ds v;
|
|
|
|
memset(&v, 0, sizeof(v));
|
2017-07-09 14:10:32 +00:00
|
|
|
to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm);
|
2015-04-28 19:39:50 +00:00
|
|
|
v.shm_atime = lower_32_bits(in->shm_atime);
|
|
|
|
v.shm_atime_high = upper_32_bits(in->shm_atime);
|
|
|
|
v.shm_dtime = lower_32_bits(in->shm_dtime);
|
|
|
|
v.shm_dtime_high = upper_32_bits(in->shm_dtime);
|
|
|
|
v.shm_ctime = lower_32_bits(in->shm_ctime);
|
|
|
|
v.shm_ctime_high = upper_32_bits(in->shm_ctime);
|
2017-07-09 02:52:47 +00:00
|
|
|
v.shm_segsz = in->shm_segsz;
|
|
|
|
v.shm_nattch = in->shm_nattch;
|
|
|
|
v.shm_cpid = in->shm_cpid;
|
|
|
|
v.shm_lpid = in->shm_lpid;
|
|
|
|
return copy_to_user(buf, &v, sizeof(v));
|
|
|
|
} else {
|
|
|
|
struct compat_shmid_ds v;
|
|
|
|
memset(&v, 0, sizeof(v));
|
2017-07-09 14:10:32 +00:00
|
|
|
to_compat_ipc_perm(&v.shm_perm, &in->shm_perm);
|
2017-07-09 02:52:47 +00:00
|
|
|
v.shm_perm.key = in->shm_perm.key;
|
|
|
|
v.shm_atime = in->shm_atime;
|
|
|
|
v.shm_dtime = in->shm_dtime;
|
|
|
|
v.shm_ctime = in->shm_ctime;
|
|
|
|
v.shm_segsz = in->shm_segsz;
|
|
|
|
v.shm_nattch = in->shm_nattch;
|
|
|
|
v.shm_cpid = in->shm_cpid;
|
|
|
|
v.shm_lpid = in->shm_lpid;
|
|
|
|
return copy_to_user(buf, &v, sizeof(v));
|
|
|
|
}
|
|
|
|
}
|
SHM_UNLOCK: fix long unpreemptible section
scan_mapping_unevictable_pages() is used to make SysV SHM_LOCKed pages
evictable again once the shared memory is unlocked. It does this with
pagevec_lookup()s across the whole object (which might occupy most of
memory), and takes 300ms to unlock 7GB here. A cond_resched() every
PAGEVEC_SIZE pages would be good.
However, KOSAKI-san points out that this is called under shmem.c's
info->lock, and it's also under shm.c's shm_lock(), both spinlocks.
There is no strong reason for that: we need to take these pages off the
unevictable list soonish, but those locks are not required for it.
So move the call to scan_mapping_unevictable_pages() from shmem.c's
unlock handling up to shm.c's unlock handling. Remove the recently
added barrier, not needed now we have spin_unlock() before the scan.
Use get_file(), with subsequent fput(), to make sure we have a reference
to mapping throughout scan_mapping_unevictable_pages(): that's something
that was previously guaranteed by the shm_lock().
Remove shmctl's lru_add_drain_all(): we don't fault in pages at SHM_LOCK
time, and we lazily discover them to be Unevictable later, so it serves
no purpose for SHM_LOCK; and serves no purpose for SHM_UNLOCK, since
pages still on pagevec are not marked Unevictable.
The original code avoided redundant rescans by checking VM_LOCKED flag
at its level: now avoid them by checking shp's SHM_LOCKED.
The original code called scan_mapping_unevictable_pages() on a locked
area at shm_destroy() time: perhaps we once had accounting cross-checks
which required that, but not now, so skip the overhead and just let
inode eviction deal with them.
Put check_move_unevictable_page() and scan_mapping_unevictable_pages()
under CONFIG_SHMEM (with stub for the TINY case when ramfs is used),
more as comment than to save space; comment them used for SHM_UNLOCK.
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Minchan Kim <minchan.kim@gmail.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Shaohua Li <shaohua.li@intel.com>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michel Lespinasse <walken@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-01-20 22:34:19 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf,
|
|
|
|
int version)
|
|
|
|
{
|
|
|
|
memset(out, 0, sizeof(*out));
|
|
|
|
if (version == IPC_64) {
|
2017-09-26 01:37:28 +00:00
|
|
|
struct compat_shmid64_ds __user *p = buf;
|
2017-07-09 14:10:32 +00:00
|
|
|
return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm);
|
2017-07-09 02:52:47 +00:00
|
|
|
} else {
|
2017-09-26 01:37:28 +00:00
|
|
|
struct compat_shmid_ds __user *p = buf;
|
2017-07-09 14:10:32 +00:00
|
|
|
return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm);
|
2017-07-09 02:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 23:06:10 +00:00
|
|
|
|
2020-04-07 03:12:56 +00:00
|
|
|
static long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr, int version)
|
2017-07-09 02:52:47 +00:00
|
|
|
{
|
|
|
|
struct ipc_namespace *ns;
|
|
|
|
struct shmid64_ds sem64;
|
|
|
|
int err;
|
SHM_UNLOCK: fix long unpreemptible section
scan_mapping_unevictable_pages() is used to make SysV SHM_LOCKed pages
evictable again once the shared memory is unlocked. It does this with
pagevec_lookup()s across the whole object (which might occupy most of
memory), and takes 300ms to unlock 7GB here. A cond_resched() every
PAGEVEC_SIZE pages would be good.
However, KOSAKI-san points out that this is called under shmem.c's
info->lock, and it's also under shm.c's shm_lock(), both spinlocks.
There is no strong reason for that: we need to take these pages off the
unevictable list soonish, but those locks are not required for it.
So move the call to scan_mapping_unevictable_pages() from shmem.c's
unlock handling up to shm.c's unlock handling. Remove the recently
added barrier, not needed now we have spin_unlock() before the scan.
Use get_file(), with subsequent fput(), to make sure we have a reference
to mapping throughout scan_mapping_unevictable_pages(): that's something
that was previously guaranteed by the shm_lock().
Remove shmctl's lru_add_drain_all(): we don't fault in pages at SHM_LOCK
time, and we lazily discover them to be Unevictable later, so it serves
no purpose for SHM_LOCK; and serves no purpose for SHM_UNLOCK, since
pages still on pagevec are not marked Unevictable.
The original code avoided redundant rescans by checking VM_LOCKED flag
at its level: now avoid them by checking shp's SHM_LOCKED.
The original code called scan_mapping_unevictable_pages() on a locked
area at shm_destroy() time: perhaps we once had accounting cross-checks
which required that, but not now, so skip the overhead and just let
inode eviction deal with them.
Put check_move_unevictable_page() and scan_mapping_unevictable_pages()
under CONFIG_SHMEM (with stub for the TINY case when ramfs is used),
more as comment than to save space; comment them used for SHM_UNLOCK.
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Minchan Kim <minchan.kim@gmail.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Shaohua Li <shaohua.li@intel.com>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michel Lespinasse <walken@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-01-20 22:34:19 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
ns = current->nsproxy->ipc_ns;
|
|
|
|
|
|
|
|
if (cmd < 0 || shmid < 0)
|
|
|
|
return -EINVAL;
|
2013-09-11 21:26:21 +00:00
|
|
|
|
2017-07-09 02:52:47 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case IPC_INFO: {
|
|
|
|
struct shminfo64 shminfo;
|
|
|
|
err = shmctl_ipc_info(ns, &shminfo);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
if (copy_compat_shminfo_to_user(uptr, &shminfo, version))
|
|
|
|
err = -EFAULT;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
case SHM_INFO: {
|
|
|
|
struct shm_info shm_info;
|
|
|
|
err = shmctl_shm_info(ns, &shm_info);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
if (put_compat_shm_info(&shm_info, uptr))
|
|
|
|
err = -EFAULT;
|
2008-04-29 08:00:47 +00:00
|
|
|
return err;
|
2013-09-11 21:26:21 +00:00
|
|
|
}
|
2017-07-09 02:52:47 +00:00
|
|
|
case IPC_STAT:
|
ipc/shm: introduce shmctl(SHM_STAT_ANY)
Patch series "sysvipc: introduce STAT_ANY commands", v2.
The following patches adds the discussed (see [1]) new command for shm
as well as for sems and msq as they are subject to the same
discrepancies for ipc object permission checks between the syscall and
via procfs. These new commands are justified in that (1) we are stuck
with this semantics as changing syscall and procfs can break userland;
and (2) some users can benefit from performance (for large amounts of
shm segments, for example) from not having to parse the procfs
interface.
Once merged, I will submit the necesary manpage updates. But I'm thinking
something like:
: diff --git a/man2/shmctl.2 b/man2/shmctl.2
: index 7bb503999941..bb00bbe21a57 100644
: --- a/man2/shmctl.2
: +++ b/man2/shmctl.2
: @@ -41,6 +41,7 @@
: .\" 2005-04-25, mtk -- noted aberrant Linux behavior w.r.t. new
: .\" attaches to a segment that has already been marked for deletion.
: .\" 2005-08-02, mtk: Added IPC_INFO, SHM_INFO, SHM_STAT descriptions.
: +.\" 2018-02-13, dbueso: Added SHM_STAT_ANY description.
: .\"
: .TH SHMCTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
: .SH NAME
: @@ -242,6 +243,18 @@ However, the
: argument is not a segment identifier, but instead an index into
: the kernel's internal array that maintains information about
: all shared memory segments on the system.
: +.TP
: +.BR SHM_STAT_ANY " (Linux-specific)"
: +Return a
: +.I shmid_ds
: +structure as for
: +.BR SHM_STAT .
: +However, the
: +.I shm_perm.mode
: +is not checked for read access for
: +.IR shmid ,
: +resembing the behaviour of
: +/proc/sysvipc/shm.
: .PP
: The caller can prevent or allow swapping of a shared
: memory segment with the following \fIcmd\fP values:
: @@ -287,7 +300,7 @@ operation returns the index of the highest used entry in the
: kernel's internal array recording information about all
: shared memory segments.
: (This information can be used with repeated
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operations to obtain information about all shared memory segments
: on the system.)
: A successful
: @@ -328,7 +341,7 @@ isn't accessible.
: \fIshmid\fP is not a valid identifier, or \fIcmd\fP
: is not a valid command.
: Or: for a
: -.B SHM_STAT
: +.B SHM_STAT/SHM_STAT_ANY
: operation, the index value specified in
: .I shmid
: referred to an array slot that is currently unused.
This patch (of 3):
There is a permission discrepancy when consulting shm ipc object metadata
between /proc/sysvipc/shm (0444) and the SHM_STAT shmctl command. The
later does permission checks for the object vs S_IRUGO. As such there can
be cases where EACCESS is returned via syscall but the info is displayed
anyways in the procfs files.
While this might have security implications via info leaking (albeit no
writing to the shm metadata), this behavior goes way back and showing all
the objects regardless of the permissions was most likely an overlook - so
we are stuck with it. Furthermore, modifying either the syscall or the
procfs file can cause userspace programs to break (ie ipcs). Some
applications require getting the procfs info (without root privileges) and
can be rather slow in comparison with a syscall -- up to 500x in some
reported cases.
This patch introduces a new SHM_STAT_ANY command such that the shm ipc
object permissions are ignored, and only audited instead. In addition,
I've left the lsm security hook checks in place, as if some policy can
block the call, then the user has no other choice than just parsing the
procfs file.
[1] https://lkml.org/lkml/2017/12/19/220
Link: http://lkml.kernel.org/r/20180215162458.10059-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Robert Kettler <robert.kettler@outlook.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-10 23:35:23 +00:00
|
|
|
case SHM_STAT_ANY:
|
2017-07-09 02:52:47 +00:00
|
|
|
case SHM_STAT:
|
|
|
|
err = shmctl_stat(ns, shmid, cmd, &sem64);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2017-09-18 16:47:38 +00:00
|
|
|
if (copy_compat_shmid_to_user(uptr, &sem64, version))
|
2017-07-09 02:52:47 +00:00
|
|
|
err = -EFAULT;
|
|
|
|
return err;
|
|
|
|
|
|
|
|
case IPC_SET:
|
|
|
|
if (copy_compat_shmid_from_user(&sem64, uptr, version))
|
|
|
|
return -EFAULT;
|
|
|
|
/* fallthru */
|
|
|
|
case IPC_RMID:
|
|
|
|
return shmctl_down(ns, shmid, cmd, &sem64);
|
|
|
|
case SHM_LOCK:
|
|
|
|
case SHM_UNLOCK:
|
|
|
|
return shmctl_do_lock(ns, shmid, cmd);
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
default:
|
2008-04-29 08:00:47 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2018-03-20 19:12:33 +00:00
|
|
|
|
|
|
|
COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr)
|
|
|
|
{
|
ipc: rename old-style shmctl/semctl/msgctl syscalls
The behavior of these system calls is slightly different between
architectures, as determined by the CONFIG_ARCH_WANT_IPC_PARSE_VERSION
symbol. Most architectures that implement the split IPC syscalls don't set
that symbol and only get the modern version, but alpha, arm, microblaze,
mips-n32, mips-n64 and xtensa expect the caller to pass the IPC_64 flag.
For the architectures that so far only implement sys_ipc(), i.e. m68k,
mips-o32, powerpc, s390, sh, sparc, and x86-32, we want the new behavior
when adding the split syscalls, so we need to distinguish between the
two groups of architectures.
The method I picked for this distinction is to have a separate system call
entry point: sys_old_*ctl() now uses ipc_parse_version, while sys_*ctl()
does not. The system call tables of the five architectures are changed
accordingly.
As an additional benefit, we no longer need the configuration specific
definition for ipc_parse_version(), it always does the same thing now,
but simply won't get called on architectures with the modern interface.
A small downside is that on architectures that do set
ARCH_WANT_IPC_PARSE_VERSION, we now have an extra set of entry points
that are never called. They only add a few bytes of bloat, so it seems
better to keep them compared to adding yet another Kconfig symbol.
I considered adding new syscall numbers for the IPC_64 variants for
consistency, but decided against that for now.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-12-31 21:22:40 +00:00
|
|
|
return compat_ksys_shmctl(shmid, cmd, uptr, IPC_64);
|
2018-03-20 19:12:33 +00:00
|
|
|
}
|
ipc: rename old-style shmctl/semctl/msgctl syscalls
The behavior of these system calls is slightly different between
architectures, as determined by the CONFIG_ARCH_WANT_IPC_PARSE_VERSION
symbol. Most architectures that implement the split IPC syscalls don't set
that symbol and only get the modern version, but alpha, arm, microblaze,
mips-n32, mips-n64 and xtensa expect the caller to pass the IPC_64 flag.
For the architectures that so far only implement sys_ipc(), i.e. m68k,
mips-o32, powerpc, s390, sh, sparc, and x86-32, we want the new behavior
when adding the split syscalls, so we need to distinguish between the
two groups of architectures.
The method I picked for this distinction is to have a separate system call
entry point: sys_old_*ctl() now uses ipc_parse_version, while sys_*ctl()
does not. The system call tables of the five architectures are changed
accordingly.
As an additional benefit, we no longer need the configuration specific
definition for ipc_parse_version(), it always does the same thing now,
but simply won't get called on architectures with the modern interface.
A small downside is that on architectures that do set
ARCH_WANT_IPC_PARSE_VERSION, we now have an extra set of entry points
that are never called. They only add a few bytes of bloat, so it seems
better to keep them compared to adding yet another Kconfig symbol.
I considered adding new syscall numbers for the IPC_64 variants for
consistency, but decided against that for now.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-12-31 21:22:40 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
|
|
|
|
long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr)
|
|
|
|
{
|
|
|
|
int version = compat_ipc_parse_version(&cmd);
|
|
|
|
|
|
|
|
return compat_ksys_shmctl(shmid, cmd, uptr, version);
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPAT_SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, void __user *, uptr)
|
|
|
|
{
|
|
|
|
return compat_ksys_old_shmctl(shmid, cmd, uptr);
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-09 02:52:47 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
|
|
|
|
*
|
|
|
|
* NOTE! Despite the name, this is NOT a direct system call entrypoint. The
|
|
|
|
* "raddr" thing points to kernel space, and there has to be a wrapper around
|
|
|
|
* this.
|
|
|
|
*/
|
ipc/shm: Fix shmat mmap nil-page protection
The issue is described here, with a nice testcase:
https://bugzilla.kernel.org/show_bug.cgi?id=192931
The problem is that shmat() calls do_mmap_pgoff() with MAP_FIXED, and
the address rounded down to 0. For the regular mmap case, the
protection mentioned above is that the kernel gets to generate the
address -- arch_get_unmapped_area() will always check for MAP_FIXED and
return that address. So by the time we do security_mmap_addr(0) things
get funky for shmat().
The testcase itself shows that while a regular user crashes, root will
not have a problem attaching a nil-page. There are two possible fixes
to this. The first, and which this patch does, is to simply allow root
to crash as well -- this is also regular mmap behavior, ie when hacking
up the testcase and adding mmap(... |MAP_FIXED). While this approach
is the safer option, the second alternative is to ignore SHM_RND if the
rounded address is 0, thus only having MAP_SHARED flags. This makes the
behavior of shmat() identical to the mmap() case. The downside of this
is obviously user visible, but does make sense in that it maintains
semantics after the round-down wrt 0 address and mmap.
Passes shm related ltp tests.
Link: http://lkml.kernel.org/r/1486050195-18629-1-git-send-email-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Reported-by: Gareth Evans <gareth.evans@contextis.co.uk>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-02-27 22:28:24 +00:00
|
|
|
long do_shmat(int shmid, char __user *shmaddr, int shmflg,
|
|
|
|
ulong *raddr, unsigned long shmlba)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct shmid_kernel *shp;
|
2017-05-08 22:57:03 +00:00
|
|
|
unsigned long addr = (unsigned long)shmaddr;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long size;
|
2018-06-17 16:24:00 +00:00
|
|
|
struct file *file, *base;
|
2005-04-16 22:20:36 +00:00
|
|
|
int err;
|
2017-05-08 22:57:03 +00:00
|
|
|
unsigned long flags = MAP_SHARED;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long prot;
|
|
|
|
int acc_mode;
|
2006-10-02 09:18:22 +00:00
|
|
|
struct ipc_namespace *ns;
|
2007-02-20 21:57:53 +00:00
|
|
|
struct shm_file_data *sfd;
|
2018-07-11 18:19:04 +00:00
|
|
|
int f_flags;
|
2013-02-23 00:32:47 +00:00
|
|
|
unsigned long populate = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
err = -EINVAL;
|
|
|
|
if (shmid < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
2017-05-08 22:57:03 +00:00
|
|
|
|
|
|
|
if (addr) {
|
2012-07-30 21:42:38 +00:00
|
|
|
if (addr & (shmlba - 1)) {
|
2018-05-25 21:47:30 +00:00
|
|
|
if (shmflg & SHM_RND) {
|
2018-05-25 21:47:27 +00:00
|
|
|
addr &= ~(shmlba - 1); /* round down */
|
2018-05-25 21:47:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the round-down is non-nil
|
|
|
|
* when remapping. This can happen for
|
|
|
|
* cases when addr < shmlba.
|
|
|
|
*/
|
|
|
|
if (!addr && (shmflg & SHM_REMAP))
|
|
|
|
goto out;
|
|
|
|
} else
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __ARCH_FORCE_SHMLBA
|
|
|
|
if (addr & ~PAGE_MASK)
|
|
|
|
#endif
|
2007-02-20 21:57:53 +00:00
|
|
|
goto out;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 22:57:03 +00:00
|
|
|
flags |= MAP_FIXED;
|
|
|
|
} else if ((shmflg & SHM_REMAP))
|
|
|
|
goto out;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (shmflg & SHM_RDONLY) {
|
|
|
|
prot = PROT_READ;
|
|
|
|
acc_mode = S_IRUGO;
|
2018-07-11 18:19:04 +00:00
|
|
|
f_flags = O_RDONLY;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
acc_mode = S_IRUGO | S_IWUGO;
|
2018-07-11 18:19:04 +00:00
|
|
|
f_flags = O_RDWR;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
if (shmflg & SHM_EXEC) {
|
|
|
|
prot |= PROT_EXEC;
|
|
|
|
acc_mode |= S_IXUGO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot rely on the fs check since SYSV IPC does have an
|
|
|
|
* additional creator id...
|
|
|
|
*/
|
2006-10-02 09:18:22 +00:00
|
|
|
ns = current->nsproxy->ipc_ns;
|
2013-09-11 21:26:23 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
shp = shm_obtain_object_check(ns, shmid);
|
2007-10-19 06:40:51 +00:00
|
|
|
if (IS_ERR(shp)) {
|
|
|
|
err = PTR_ERR(shp);
|
2013-09-11 21:26:23 +00:00
|
|
|
goto out_unlock;
|
2007-10-19 06:40:51 +00:00
|
|
|
}
|
2007-02-20 21:57:53 +00:00
|
|
|
|
|
|
|
err = -EACCES;
|
2011-03-23 23:43:24 +00:00
|
|
|
if (ipcperms(ns, &shp->shm_perm, acc_mode))
|
2007-02-20 21:57:53 +00:00
|
|
|
goto out_unlock;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-03-23 02:08:27 +00:00
|
|
|
err = security_shm_shmat(&shp->shm_perm, shmaddr, shmflg);
|
2007-02-20 21:57:53 +00:00
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
|
|
|
|
2013-09-11 21:26:23 +00:00
|
|
|
ipc_lock_object(&shp->shm_perm);
|
2013-11-21 22:32:00 +00:00
|
|
|
|
|
|
|
/* check if shm_destroy() is tearing down shp */
|
2014-01-28 01:07:01 +00:00
|
|
|
if (!ipc_valid_object(&shp->shm_perm)) {
|
2013-11-21 22:32:00 +00:00
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
|
|
|
err = -EIDRM;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2018-06-17 16:24:00 +00:00
|
|
|
/*
|
|
|
|
* We need to take a reference to the real shm file to prevent the
|
|
|
|
* pointer from becoming stale in cases where the lifetime of the outer
|
|
|
|
* file extends beyond that of the shm segment. It's not usually
|
|
|
|
* possible, but it can happen during remap_file_pages() emulation as
|
|
|
|
* that unmaps the memory, then does ->mmap() via file reference only.
|
|
|
|
* We'll deny the ->mmap() if the shm segment was since removed, but to
|
|
|
|
* detect shm ID reuse we need to compare the file pointers.
|
|
|
|
*/
|
|
|
|
base = get_file(shp->shm_file);
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->shm_nattch++;
|
2018-06-17 16:24:00 +00:00
|
|
|
size = i_size_read(file_inode(base));
|
2013-09-11 21:26:23 +00:00
|
|
|
ipc_unlock_object(&shp->shm_perm);
|
|
|
|
rcu_read_unlock();
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-02-20 21:57:53 +00:00
|
|
|
err = -ENOMEM;
|
|
|
|
sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
|
2013-09-11 21:26:22 +00:00
|
|
|
if (!sfd) {
|
2018-06-17 16:24:00 +00:00
|
|
|
fput(base);
|
2013-09-11 21:26:22 +00:00
|
|
|
goto out_nattch;
|
|
|
|
}
|
2007-02-20 21:57:53 +00:00
|
|
|
|
2018-06-17 16:24:00 +00:00
|
|
|
file = alloc_file_clone(base, f_flags,
|
|
|
|
is_file_hugepages(base) ?
|
2009-11-30 13:38:43 +00:00
|
|
|
&shm_file_operations_huge :
|
|
|
|
&shm_file_operations);
|
2012-09-13 03:11:55 +00:00
|
|
|
err = PTR_ERR(file);
|
2013-09-11 21:26:22 +00:00
|
|
|
if (IS_ERR(file)) {
|
|
|
|
kfree(sfd);
|
2018-06-17 16:24:00 +00:00
|
|
|
fput(base);
|
2013-09-11 21:26:22 +00:00
|
|
|
goto out_nattch;
|
|
|
|
}
|
2007-02-20 21:57:53 +00:00
|
|
|
|
2007-10-19 06:40:48 +00:00
|
|
|
sfd->id = shp->shm_perm.id;
|
2007-02-20 21:57:53 +00:00
|
|
|
sfd->ns = get_ipc_ns(ns);
|
2018-06-17 16:24:00 +00:00
|
|
|
sfd->file = base;
|
2007-02-20 21:57:53 +00:00
|
|
|
sfd->vm_ops = NULL;
|
2018-06-17 16:24:00 +00:00
|
|
|
file->private_data = sfd;
|
2007-02-20 21:57:53 +00:00
|
|
|
|
2012-05-30 21:11:23 +00:00
|
|
|
err = security_mmap_file(file, prot, flags);
|
|
|
|
if (err)
|
|
|
|
goto out_fput;
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
if (mmap_write_lock_killable(current->mm)) {
|
2016-05-23 23:25:51 +00:00
|
|
|
err = -EINTR;
|
|
|
|
goto out_fput;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (addr && !(shmflg & SHM_REMAP)) {
|
2007-02-20 21:57:53 +00:00
|
|
|
err = -EINVAL;
|
ipc/shm.c: check for ulong overflows in shmat
The increase of SHMMAX/SHMALL is a 4 patch series.
The change itself is trivial, the only problem are interger overflows.
The overflows are not new, but if we make huge values the default, then
the code should be free from overflows.
SHMMAX:
- shmmem_file_setup places a hard limit on the segment size:
MAX_LFS_FILESIZE.
On 32-bit, the limit is > 1 TB, i.e. 4 GB-1 byte segments are
possible. Rounded up to full pages the actual allocated size
is 0. --> must be fixed, patch 3
- shmat:
- find_vma_intersection does not handle overflows properly.
--> must be fixed, patch 1
- the rest is fine, do_mmap_pgoff limits mappings to TASK_SIZE
and checks for overflows (i.e.: map 2 GB, starting from
addr=2.5GB fails).
SHMALL:
- after creating 8192 segments size (1L<<63)-1, shm_tot overflows and
returns 0. --> must be fixed, patch 2.
Userspace:
- Obviously, there could be overflows in userspace. There is nothing
we can do, only use values smaller than ULONG_MAX.
I ended with "ULONG_MAX - 1L<<24":
- TASK_SIZE cannot be used because it is the size of the current
task. Could be 4G if it's a 32-bit task on a 64-bit kernel.
- The maximum size is not standardized across archs:
I found TASK_MAX_SIZE, TASK_SIZE_MAX and TASK_SIZE_64.
- Just in case some arch revives a 4G/4G split, nearly
ULONG_MAX is a valid segment size.
- Using "0" as a magic value for infinity is even worse, because
right now 0 means 0, i.e. fail all allocations.
This patch (of 4):
find_vma_intersection() does not work as intended if addr+size overflows.
The patch adds a manual check before the call to find_vma_intersection.
Signed-off-by: Manfred Spraul <manfred@colorfullife.com>
Acked-by: Davidlohr Bueso <davidlohr@hp.com>
Acked-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-06-06 21:37:38 +00:00
|
|
|
if (addr + size < addr)
|
|
|
|
goto invalid;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (find_vma_intersection(current->mm, addr, addr + size))
|
|
|
|
goto invalid;
|
|
|
|
}
|
2013-09-11 21:26:22 +00:00
|
|
|
|
2020-08-07 06:23:37 +00:00
|
|
|
addr = do_mmap(file, addr, size, prot, flags, 0, &populate, NULL);
|
2013-02-23 00:32:37 +00:00
|
|
|
*raddr = addr;
|
2007-02-20 21:57:53 +00:00
|
|
|
err = 0;
|
2013-02-23 00:32:37 +00:00
|
|
|
if (IS_ERR_VALUE(addr))
|
|
|
|
err = (long)addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
invalid:
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(current->mm);
|
2013-02-23 00:32:37 +00:00
|
|
|
if (populate)
|
2013-02-23 00:32:47 +00:00
|
|
|
mm_populate(addr, populate);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-05-30 21:11:23 +00:00
|
|
|
out_fput:
|
2007-02-20 21:57:53 +00:00
|
|
|
fput(file);
|
|
|
|
|
|
|
|
out_nattch:
|
2013-09-11 21:26:24 +00:00
|
|
|
down_write(&shm_ids(ns).rwsem);
|
2008-07-25 08:48:03 +00:00
|
|
|
shp = shm_lock(ns, shmid);
|
2005-04-16 22:20:36 +00:00
|
|
|
shp->shm_nattch--;
|
2011-07-26 23:08:48 +00:00
|
|
|
if (shm_may_destroy(ns, shp))
|
2006-10-02 09:18:22 +00:00
|
|
|
shm_destroy(ns, shp);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
|
|
|
shm_unlock(shp);
|
2013-09-11 21:26:24 +00:00
|
|
|
up_write(&shm_ids(ns).rwsem);
|
2005-04-16 22:20:36 +00:00
|
|
|
return err;
|
2007-02-20 21:57:53 +00:00
|
|
|
|
|
|
|
out_unlock:
|
2013-09-11 21:26:23 +00:00
|
|
|
rcu_read_unlock();
|
2013-09-11 21:26:22 +00:00
|
|
|
out:
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2009-01-14 13:14:27 +00:00
|
|
|
SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
|
2005-05-01 15:59:12 +00:00
|
|
|
{
|
|
|
|
unsigned long ret;
|
|
|
|
long err;
|
|
|
|
|
2012-07-30 21:42:38 +00:00
|
|
|
err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
|
2005-05-01 15:59:12 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
force_successful_syscall_return();
|
|
|
|
return (long)ret;
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:38:28 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
|
|
|
|
#ifndef COMPAT_SHMLBA
|
|
|
|
#define COMPAT_SHMLBA SHMLBA
|
|
|
|
#endif
|
|
|
|
|
|
|
|
COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg)
|
|
|
|
{
|
|
|
|
unsigned long ret;
|
|
|
|
long err;
|
|
|
|
|
|
|
|
err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
force_successful_syscall_return();
|
|
|
|
return (long)ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* detach and kill segment if marked destroyed.
|
|
|
|
* The work is done in shm_close.
|
|
|
|
*/
|
2018-03-20 19:09:48 +00:00
|
|
|
long ksys_shmdt(char __user *shmaddr)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct mm_struct *mm = current->mm;
|
2009-06-09 23:26:23 +00:00
|
|
|
struct vm_area_struct *vma;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long addr = (unsigned long)shmaddr;
|
|
|
|
int retval = -EINVAL;
|
2009-06-09 23:26:23 +00:00
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
loff_t size = 0;
|
2014-12-13 00:58:19 +00:00
|
|
|
struct file *file;
|
2009-06-09 23:26:23 +00:00
|
|
|
struct vm_area_struct *next;
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-03-24 11:18:06 +00:00
|
|
|
if (addr & ~PAGE_MASK)
|
|
|
|
return retval;
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
if (mmap_write_lock_killable(mm))
|
2016-05-23 23:25:51 +00:00
|
|
|
return -EINTR;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function tries to be smart and unmap shm segments that
|
|
|
|
* were modified by partial mlock or munmap calls:
|
|
|
|
* - It first determines the size of the shm segment that should be
|
|
|
|
* unmapped: It searches for a vma that is backed by shm and that
|
|
|
|
* started at address shmaddr. It records it's size and then unmaps
|
|
|
|
* it.
|
|
|
|
* - Then it unmaps all shm vmas that started at shmaddr and that
|
2014-12-13 00:58:19 +00:00
|
|
|
* are within the initially determined size and that are from the
|
|
|
|
* same shm segment from which we determined the size.
|
2005-04-16 22:20:36 +00:00
|
|
|
* Errors from do_munmap are ignored: the function only fails if
|
|
|
|
* it's called with invalid parameters or if it's called to unmap
|
|
|
|
* a part of a vma. Both calls in this function are for full vmas,
|
|
|
|
* the parameters are directly copied from the vma itself and always
|
|
|
|
* valid - therefore do_munmap cannot fail. (famous last words?)
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* If it had been mremap()'d, the starting address would not
|
|
|
|
* match the usual checks anyway. So assume all vma's are
|
|
|
|
* above the starting address given.
|
|
|
|
*/
|
|
|
|
vma = find_vma(mm, addr);
|
|
|
|
|
2009-01-08 12:04:47 +00:00
|
|
|
#ifdef CONFIG_MMU
|
2005-04-16 22:20:36 +00:00
|
|
|
while (vma) {
|
|
|
|
next = vma->vm_next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the starting address would match, i.e. it's
|
|
|
|
* a fragment created by mprotect() and/or munmap(), or it
|
|
|
|
* otherwise it starts at this address with no hassles.
|
|
|
|
*/
|
2007-02-20 21:57:53 +00:00
|
|
|
if ((vma->vm_ops == &shm_vm_ops) &&
|
2005-04-16 22:20:36 +00:00
|
|
|
(vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
|
|
|
|
|
2014-12-13 00:58:19 +00:00
|
|
|
/*
|
|
|
|
* Record the file of the shm segment being
|
|
|
|
* unmapped. With mremap(), someone could place
|
|
|
|
* page from another segment but with equal offsets
|
|
|
|
* in the range we are unmapping.
|
|
|
|
*/
|
|
|
|
file = vma->vm_file;
|
2014-12-13 00:58:22 +00:00
|
|
|
size = i_size_read(file_inode(vma->vm_file));
|
2017-02-24 22:58:22 +00:00
|
|
|
do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* We discovered the size of the shm segment, so
|
|
|
|
* break out of here and fall through to the next
|
|
|
|
* loop that uses the size information to stop
|
|
|
|
* searching for matching vma's.
|
|
|
|
*/
|
|
|
|
retval = 0;
|
|
|
|
vma = next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vma = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need look no further than the maximum address a fragment
|
|
|
|
* could possibly have landed at. Also cast things to loff_t to
|
2011-03-31 01:57:33 +00:00
|
|
|
* prevent overflows and make comparisons vs. equal-width types.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2006-02-10 09:51:12 +00:00
|
|
|
size = PAGE_ALIGN(size);
|
2005-04-16 22:20:36 +00:00
|
|
|
while (vma && (loff_t)(vma->vm_end - addr) <= size) {
|
|
|
|
next = vma->vm_next;
|
|
|
|
|
|
|
|
/* finding a matching vma now does not alter retval */
|
2007-02-20 21:57:53 +00:00
|
|
|
if ((vma->vm_ops == &shm_vm_ops) &&
|
2014-12-13 00:58:19 +00:00
|
|
|
((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
|
|
|
|
(vma->vm_file == file))
|
2017-02-24 22:58:22 +00:00
|
|
|
do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
vma = next;
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:06:10 +00:00
|
|
|
#else /* CONFIG_MMU */
|
2009-01-08 12:04:47 +00:00
|
|
|
/* under NOMMU conditions, the exact address to be destroyed must be
|
2016-12-14 23:06:10 +00:00
|
|
|
* given
|
|
|
|
*/
|
2013-09-11 21:26:28 +00:00
|
|
|
if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
|
2017-02-24 22:58:22 +00:00
|
|
|
do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
|
2009-01-08 12:04:47 +00:00
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 04:33:25 +00:00
|
|
|
mmap_write_unlock(mm);
|
2005-04-16 22:20:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:09:48 +00:00
|
|
|
SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
|
|
|
|
{
|
|
|
|
return ksys_shmdt(shmaddr);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2005-09-06 22:17:10 +00:00
|
|
|
static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-03-23 05:29:57 +00:00
|
|
|
struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
|
2012-02-08 00:54:11 +00:00
|
|
|
struct user_namespace *user_ns = seq_user_ns(s);
|
2017-08-02 20:32:21 +00:00
|
|
|
struct kern_ipc_perm *ipcp = it;
|
|
|
|
struct shmid_kernel *shp;
|
2010-10-27 22:34:16 +00:00
|
|
|
unsigned long rss = 0, swp = 0;
|
|
|
|
|
2017-08-02 20:32:21 +00:00
|
|
|
shp = container_of(ipcp, struct shmid_kernel, shm_perm);
|
2010-10-27 22:34:16 +00:00
|
|
|
shm_add_rss_swap(shp, &rss, &swp);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-06-12 22:21:49 +00:00
|
|
|
#if BITS_PER_LONG <= 32
|
|
|
|
#define SIZE_SPEC "%10lu"
|
|
|
|
#else
|
|
|
|
#define SIZE_SPEC "%21lu"
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-04-15 23:17:54 +00:00
|
|
|
seq_printf(s,
|
|
|
|
"%10d %10d %4o " SIZE_SPEC " %5u %5u "
|
2017-08-03 02:51:14 +00:00
|
|
|
"%5lu %5u %5u %5u %5u %10llu %10llu %10llu "
|
2015-04-15 23:17:54 +00:00
|
|
|
SIZE_SPEC " " SIZE_SPEC "\n",
|
|
|
|
shp->shm_perm.key,
|
|
|
|
shp->shm_perm.id,
|
|
|
|
shp->shm_perm.mode,
|
|
|
|
shp->shm_segsz,
|
2018-03-23 05:29:57 +00:00
|
|
|
pid_nr_ns(shp->shm_cprid, pid_ns),
|
|
|
|
pid_nr_ns(shp->shm_lprid, pid_ns),
|
2015-04-15 23:17:54 +00:00
|
|
|
shp->shm_nattch,
|
|
|
|
from_kuid_munged(user_ns, shp->shm_perm.uid),
|
|
|
|
from_kgid_munged(user_ns, shp->shm_perm.gid),
|
|
|
|
from_kuid_munged(user_ns, shp->shm_perm.cuid),
|
|
|
|
from_kgid_munged(user_ns, shp->shm_perm.cgid),
|
|
|
|
shp->shm_atim,
|
|
|
|
shp->shm_dtim,
|
|
|
|
shp->shm_ctim,
|
|
|
|
rss * PAGE_SIZE,
|
|
|
|
swp * PAGE_SIZE);
|
|
|
|
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
#endif
|