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
|
2012-09-27 01:52:08 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/fdtable.h>
|
2016-11-10 18:46:38 +00:00
|
|
|
#include <linux/freezer.h>
|
2012-09-27 01:52:08 +00:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/swap.h>
|
2019-08-03 04:49:05 +00:00
|
|
|
#include <linux/ctype.h>
|
2012-09-27 01:52:08 +00:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/perf_event.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/key.h>
|
|
|
|
#include <linux/personality.h>
|
|
|
|
#include <linux/binfmts.h>
|
2012-10-05 00:15:24 +00:00
|
|
|
#include <linux/coredump.h>
|
2024-08-06 18:16:02 +00:00
|
|
|
#include <linux/sort.h>
|
2017-02-08 17:51:30 +00:00
|
|
|
#include <linux/sched/coredump.h>
|
2017-02-08 17:51:30 +00:00
|
|
|
#include <linux/sched/signal.h>
|
2017-02-08 17:51:37 +00:00
|
|
|
#include <linux/sched/task_stack.h>
|
2012-09-27 01:52:08 +00:00
|
|
|
#include <linux/utsname.h>
|
|
|
|
#include <linux/pid_namespace.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/tsacct_kern.h>
|
|
|
|
#include <linux/cn_proc.h>
|
|
|
|
#include <linux/audit.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/fsnotify.h>
|
|
|
|
#include <linux/fs_struct.h>
|
|
|
|
#include <linux/pipe_fs_i.h>
|
|
|
|
#include <linux/oom.h>
|
|
|
|
#include <linux/compat.h>
|
2016-03-22 21:25:36 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/path.h>
|
2015-11-25 15:22:25 +00:00
|
|
|
#include <linux/timekeeping.h>
|
2022-01-22 06:13:38 +00:00
|
|
|
#include <linux/sysctl.h>
|
2022-01-26 02:57:39 +00:00
|
|
|
#include <linux/elf.h>
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2016-12-24 19:46:01 +00:00
|
|
|
#include <linux/uaccess.h>
|
2012-09-27 01:52:08 +00:00
|
|
|
#include <asm/mmu_context.h>
|
|
|
|
#include <asm/tlb.h>
|
|
|
|
#include <asm/exec.h>
|
|
|
|
|
|
|
|
#include <trace/events/task.h>
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#include <trace/events/sched.h>
|
|
|
|
|
2022-03-08 18:55:29 +00:00
|
|
|
static bool dump_vma_snapshot(struct coredump_params *cprm);
|
2022-03-08 19:04:19 +00:00
|
|
|
static void free_vma_snapshot(struct coredump_params *cprm);
|
2022-03-08 18:55:29 +00:00
|
|
|
|
2024-05-06 19:37:00 +00:00
|
|
|
#define CORE_FILE_NOTE_SIZE_DEFAULT (4*1024*1024)
|
|
|
|
/* Define a reasonable max cap */
|
|
|
|
#define CORE_FILE_NOTE_SIZE_MAX (16*1024*1024)
|
|
|
|
|
2022-01-22 06:13:38 +00:00
|
|
|
static int core_uses_pid;
|
|
|
|
static unsigned int core_pipe_limit;
|
|
|
|
static char core_pattern[CORENAME_MAX_SIZE] = "core";
|
2013-07-03 22:08:22 +00:00
|
|
|
static int core_name_size = CORENAME_MAX_SIZE;
|
2024-05-06 19:37:00 +00:00
|
|
|
unsigned int core_file_note_size_limit = CORE_FILE_NOTE_SIZE_DEFAULT;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
struct core_name {
|
|
|
|
char *corename;
|
|
|
|
int used, size;
|
|
|
|
};
|
|
|
|
|
2013-07-03 22:08:22 +00:00
|
|
|
static int expand_corename(struct core_name *cn, int size)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
2022-09-20 21:13:05 +00:00
|
|
|
char *corename;
|
|
|
|
|
|
|
|
size = kmalloc_size_roundup(size);
|
|
|
|
corename = krealloc(cn->corename, size, GFP_KERNEL);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-07-03 22:08:16 +00:00
|
|
|
if (!corename)
|
2012-09-27 01:52:08 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-07-03 22:08:22 +00:00
|
|
|
if (size > core_name_size) /* racy but harmless */
|
|
|
|
core_name_size = size;
|
|
|
|
|
2022-09-20 21:13:05 +00:00
|
|
|
cn->size = size;
|
2013-07-03 22:08:16 +00:00
|
|
|
cn->corename = corename;
|
2012-09-27 01:52:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:03:53 +00:00
|
|
|
static __printf(2, 0) int cn_vprintf(struct core_name *cn, const char *fmt,
|
|
|
|
va_list arg)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
2013-07-03 22:08:19 +00:00
|
|
|
int free, need;
|
2014-04-19 17:15:07 +00:00
|
|
|
va_list arg_copy;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-07-03 22:08:19 +00:00
|
|
|
again:
|
|
|
|
free = cn->size - cn->used;
|
2014-04-19 17:15:07 +00:00
|
|
|
|
|
|
|
va_copy(arg_copy, arg);
|
|
|
|
need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
|
|
|
|
va_end(arg_copy);
|
|
|
|
|
2013-07-03 22:08:19 +00:00
|
|
|
if (need < free) {
|
|
|
|
cn->used += need;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-07-03 22:08:22 +00:00
|
|
|
if (!expand_corename(cn, cn->size + need - free + 1))
|
2013-07-03 22:08:19 +00:00
|
|
|
goto again;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-07-03 22:08:19 +00:00
|
|
|
return -ENOMEM;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 22:03:53 +00:00
|
|
|
static __printf(2, 3) int cn_printf(struct core_name *cn, const char *fmt, ...)
|
2013-07-03 22:08:17 +00:00
|
|
|
{
|
|
|
|
va_list arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
ret = cn_vprintf(cn, fmt, arg);
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:03:53 +00:00
|
|
|
static __printf(2, 3)
|
|
|
|
int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
2013-07-03 22:08:20 +00:00
|
|
|
int cur = cn->used;
|
|
|
|
va_list arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
ret = cn_vprintf(cn, fmt, arg);
|
|
|
|
va_end(arg);
|
|
|
|
|
2016-01-20 23:00:08 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
/*
|
|
|
|
* Ensure that this coredump name component can't cause the
|
|
|
|
* resulting corefile path to consist of a ".." or ".".
|
|
|
|
*/
|
|
|
|
if ((cn->used - cur == 1 && cn->corename[cur] == '.') ||
|
|
|
|
(cn->used - cur == 2 && cn->corename[cur] == '.'
|
|
|
|
&& cn->corename[cur+1] == '.'))
|
|
|
|
cn->corename[cur] = '!';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Empty names are fishy and could be used to create a "//" in a
|
|
|
|
* corefile name, causing the coredump to happen one directory
|
|
|
|
* level too high. Enforce that all components of the core
|
|
|
|
* pattern are at least one character long.
|
|
|
|
*/
|
|
|
|
if (cn->used == cur)
|
|
|
|
ret = cn_printf(cn, "!");
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:08:20 +00:00
|
|
|
for (; cur < cn->used; ++cur) {
|
|
|
|
if (cn->corename[cur] == '/')
|
|
|
|
cn->corename[cur] = '!';
|
|
|
|
}
|
|
|
|
return ret;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 01:36:20 +00:00
|
|
|
static int cn_print_exe_file(struct core_name *cn, bool name_only)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
|
|
|
struct file *exe_file;
|
2020-08-12 01:36:20 +00:00
|
|
|
char *pathbuf, *path, *ptr;
|
2012-09-27 01:52:08 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
exe_file = get_mm_exe_file(current->mm);
|
2013-07-03 22:08:20 +00:00
|
|
|
if (!exe_file)
|
|
|
|
return cn_esc_printf(cn, "%s (path unknown)", current->comm);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2017-09-13 23:28:29 +00:00
|
|
|
pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
|
2012-09-27 01:52:08 +00:00
|
|
|
if (!pathbuf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto put_exe_file;
|
|
|
|
}
|
|
|
|
|
2015-06-19 08:29:13 +00:00
|
|
|
path = file_path(exe_file, pathbuf, PATH_MAX);
|
2012-09-27 01:52:08 +00:00
|
|
|
if (IS_ERR(path)) {
|
|
|
|
ret = PTR_ERR(path);
|
|
|
|
goto free_buf;
|
|
|
|
}
|
|
|
|
|
2020-08-12 01:36:20 +00:00
|
|
|
if (name_only) {
|
|
|
|
ptr = strrchr(path, '/');
|
|
|
|
if (ptr)
|
|
|
|
path = ptr + 1;
|
|
|
|
}
|
2013-07-03 22:08:20 +00:00
|
|
|
ret = cn_esc_printf(cn, "%s", path);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
free_buf:
|
|
|
|
kfree(pathbuf);
|
|
|
|
put_exe_file:
|
|
|
|
fput(exe_file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* format_corename will inspect the pattern parameter, and output a
|
|
|
|
* name into corename, which must have space for at least
|
|
|
|
* CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
|
|
|
|
*/
|
2019-08-03 04:49:05 +00:00
|
|
|
static int format_corename(struct core_name *cn, struct coredump_params *cprm,
|
|
|
|
size_t **argv, int *argc)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
|
|
|
const struct cred *cred = current_cred();
|
|
|
|
const char *pat_ptr = core_pattern;
|
|
|
|
int ispipe = (*pat_ptr == '|');
|
2019-08-03 04:49:05 +00:00
|
|
|
bool was_space = false;
|
2012-09-27 01:52:08 +00:00
|
|
|
int pid_in_pattern = 0;
|
|
|
|
int err = 0;
|
|
|
|
|
2013-07-03 22:08:16 +00:00
|
|
|
cn->used = 0;
|
2013-07-03 22:08:22 +00:00
|
|
|
cn->corename = NULL;
|
|
|
|
if (expand_corename(cn, core_name_size))
|
2012-09-27 01:52:08 +00:00
|
|
|
return -ENOMEM;
|
2013-07-03 22:08:23 +00:00
|
|
|
cn->corename[0] = '\0';
|
|
|
|
|
2019-08-03 04:49:05 +00:00
|
|
|
if (ispipe) {
|
|
|
|
int argvs = sizeof(core_pattern) / 2;
|
|
|
|
(*argv) = kmalloc_array(argvs, sizeof(**argv), GFP_KERNEL);
|
|
|
|
if (!(*argv))
|
|
|
|
return -ENOMEM;
|
|
|
|
(*argv)[(*argc)++] = 0;
|
2013-07-03 22:08:23 +00:00
|
|
|
++pat_ptr;
|
2020-04-21 01:14:20 +00:00
|
|
|
if (!(*pat_ptr))
|
|
|
|
return -ENOMEM;
|
2019-08-03 04:49:05 +00:00
|
|
|
}
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
/* Repeat as long as we have more pattern to process and more output
|
|
|
|
space */
|
|
|
|
while (*pat_ptr) {
|
2019-08-03 04:49:05 +00:00
|
|
|
/*
|
|
|
|
* Split on spaces before doing template expansion so that
|
|
|
|
* %e and %E don't get split if they have spaces in them
|
|
|
|
*/
|
|
|
|
if (ispipe) {
|
|
|
|
if (isspace(*pat_ptr)) {
|
2020-12-06 06:14:42 +00:00
|
|
|
if (cn->used != 0)
|
|
|
|
was_space = true;
|
2019-08-03 04:49:05 +00:00
|
|
|
pat_ptr++;
|
|
|
|
continue;
|
|
|
|
} else if (was_space) {
|
|
|
|
was_space = false;
|
|
|
|
err = cn_printf(cn, "%c", '\0');
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
(*argv)[(*argc)++] = cn->used;
|
|
|
|
}
|
|
|
|
}
|
2012-09-27 01:52:08 +00:00
|
|
|
if (*pat_ptr != '%') {
|
|
|
|
err = cn_printf(cn, "%c", *pat_ptr++);
|
|
|
|
} else {
|
|
|
|
switch (*++pat_ptr) {
|
|
|
|
/* single % at the end, drop that */
|
|
|
|
case 0:
|
|
|
|
goto out;
|
|
|
|
/* Double percent, output one percent */
|
|
|
|
case '%':
|
|
|
|
err = cn_printf(cn, "%c", '%');
|
|
|
|
break;
|
|
|
|
/* pid */
|
|
|
|
case 'p':
|
|
|
|
pid_in_pattern = 1;
|
|
|
|
err = cn_printf(cn, "%d",
|
|
|
|
task_tgid_vnr(current));
|
|
|
|
break;
|
2013-09-11 21:24:32 +00:00
|
|
|
/* global pid */
|
|
|
|
case 'P':
|
|
|
|
err = cn_printf(cn, "%d",
|
|
|
|
task_tgid_nr(current));
|
|
|
|
break;
|
2014-10-13 22:53:35 +00:00
|
|
|
case 'i':
|
|
|
|
err = cn_printf(cn, "%d",
|
|
|
|
task_pid_vnr(current));
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
err = cn_printf(cn, "%d",
|
|
|
|
task_pid_nr(current));
|
|
|
|
break;
|
2012-09-27 01:52:08 +00:00
|
|
|
/* uid */
|
|
|
|
case 'u':
|
coredump: use from_kuid/kgid when formatting corename
When adding __printf attribute to cn_printf, gcc reports some issues:
fs/coredump.c:213:5: warning: format '%d' expects argument of type
'int', but argument 3 has type 'kuid_t' [-Wformat=]
err = cn_printf(cn, "%d", cred->uid);
^
fs/coredump.c:217:5: warning: format '%d' expects argument of type
'int', but argument 3 has type 'kgid_t' [-Wformat=]
err = cn_printf(cn, "%d", cred->gid);
^
These warnings come from the fact that the value of uid/gid needs to be
extracted from the kuid_t/kgid_t structure before being used as an
integer. More precisely, cred->uid and cred->gid need to be converted to
either user-namespace uid/gid or to init_user_ns uid/gid.
Use init_user_ns in order not to break existing ABI, and document this in
Documentation/sysctl/kernel.txt.
While at it, format uid and gid values with %u instead of %d because
uid_t/__kernel_uid32_t and gid_t/__kernel_gid32_t are unsigned int.
Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-06-25 22:03:51 +00:00
|
|
|
err = cn_printf(cn, "%u",
|
|
|
|
from_kuid(&init_user_ns,
|
|
|
|
cred->uid));
|
2012-09-27 01:52:08 +00:00
|
|
|
break;
|
|
|
|
/* gid */
|
|
|
|
case 'g':
|
coredump: use from_kuid/kgid when formatting corename
When adding __printf attribute to cn_printf, gcc reports some issues:
fs/coredump.c:213:5: warning: format '%d' expects argument of type
'int', but argument 3 has type 'kuid_t' [-Wformat=]
err = cn_printf(cn, "%d", cred->uid);
^
fs/coredump.c:217:5: warning: format '%d' expects argument of type
'int', but argument 3 has type 'kgid_t' [-Wformat=]
err = cn_printf(cn, "%d", cred->gid);
^
These warnings come from the fact that the value of uid/gid needs to be
extracted from the kuid_t/kgid_t structure before being used as an
integer. More precisely, cred->uid and cred->gid need to be converted to
either user-namespace uid/gid or to init_user_ns uid/gid.
Use init_user_ns in order not to break existing ABI, and document this in
Documentation/sysctl/kernel.txt.
While at it, format uid and gid values with %u instead of %d because
uid_t/__kernel_uid32_t and gid_t/__kernel_gid32_t are unsigned int.
Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-06-25 22:03:51 +00:00
|
|
|
err = cn_printf(cn, "%u",
|
|
|
|
from_kgid(&init_user_ns,
|
|
|
|
cred->gid));
|
2012-09-27 01:52:08 +00:00
|
|
|
break;
|
2012-10-05 00:15:25 +00:00
|
|
|
case 'd':
|
|
|
|
err = cn_printf(cn, "%d",
|
|
|
|
__get_dumpable(cprm->mm_flags));
|
|
|
|
break;
|
2012-09-27 01:52:08 +00:00
|
|
|
/* signal that caused the coredump */
|
|
|
|
case 's':
|
2015-06-25 22:03:53 +00:00
|
|
|
err = cn_printf(cn, "%d",
|
|
|
|
cprm->siginfo->si_signo);
|
2012-09-27 01:52:08 +00:00
|
|
|
break;
|
|
|
|
/* UNIX time of coredump */
|
|
|
|
case 't': {
|
2015-11-25 15:22:25 +00:00
|
|
|
time64_t time;
|
|
|
|
|
|
|
|
time = ktime_get_real_seconds();
|
|
|
|
err = cn_printf(cn, "%lld", time);
|
2012-09-27 01:52:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* hostname */
|
2013-07-03 22:08:20 +00:00
|
|
|
case 'h':
|
2012-09-27 01:52:08 +00:00
|
|
|
down_read(&uts_sem);
|
2013-07-03 22:08:20 +00:00
|
|
|
err = cn_esc_printf(cn, "%s",
|
2012-09-27 01:52:08 +00:00
|
|
|
utsname()->nodename);
|
|
|
|
up_read(&uts_sem);
|
|
|
|
break;
|
2020-08-12 01:36:20 +00:00
|
|
|
/* executable, could be changed by prctl PR_SET_NAME etc */
|
2013-07-03 22:08:20 +00:00
|
|
|
case 'e':
|
|
|
|
err = cn_esc_printf(cn, "%s", current->comm);
|
2012-09-27 01:52:08 +00:00
|
|
|
break;
|
2020-08-12 01:36:20 +00:00
|
|
|
/* file name of executable */
|
|
|
|
case 'f':
|
|
|
|
err = cn_print_exe_file(cn, true);
|
|
|
|
break;
|
2012-09-27 01:52:08 +00:00
|
|
|
case 'E':
|
2020-08-12 01:36:20 +00:00
|
|
|
err = cn_print_exe_file(cn, false);
|
2012-09-27 01:52:08 +00:00
|
|
|
break;
|
|
|
|
/* core limit size */
|
|
|
|
case 'c':
|
|
|
|
err = cn_printf(cn, "%lu",
|
|
|
|
rlimit(RLIMIT_CORE));
|
|
|
|
break;
|
2022-09-03 06:43:30 +00:00
|
|
|
/* CPU the task ran on */
|
|
|
|
case 'C':
|
|
|
|
err = cn_printf(cn, "%d", cprm->cpu);
|
|
|
|
break;
|
2012-09-27 01:52:08 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pat_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:08:23 +00:00
|
|
|
out:
|
2012-09-27 01:52:08 +00:00
|
|
|
/* Backward compatibility with core_uses_pid:
|
|
|
|
*
|
|
|
|
* If core_pattern does not include a %p (as is the default)
|
|
|
|
* and core_uses_pid is set, then .%pid will be appended to
|
|
|
|
* the filename. Do not do this for piped commands. */
|
|
|
|
if (!ispipe && !pid_in_pattern && core_uses_pid) {
|
|
|
|
err = cn_printf(cn, ".%d", task_tgid_vnr(current));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return ispipe;
|
|
|
|
}
|
|
|
|
|
2024-06-25 14:03:11 +00:00
|
|
|
static int zap_process(struct signal_struct *signal, int exit_code)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
|
|
|
struct task_struct *t;
|
|
|
|
int nr = 0;
|
|
|
|
|
2024-06-25 14:03:11 +00:00
|
|
|
signal->flags = SIGNAL_GROUP_EXIT;
|
|
|
|
signal->group_exit_code = exit_code;
|
|
|
|
signal->group_stop_count = 0;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2024-06-25 14:03:11 +00:00
|
|
|
__for_each_thread(signal, t) {
|
2012-09-27 01:52:08 +00:00
|
|
|
task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
|
2021-09-01 16:33:50 +00:00
|
|
|
if (t != current && !(t->flags & PF_POSTCOREDUMP)) {
|
2012-09-27 01:52:08 +00:00
|
|
|
sigaddset(&t->pending.signal, SIGKILL);
|
|
|
|
signal_wake_up(t, 1);
|
2024-03-16 00:47:07 +00:00
|
|
|
nr++;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
2015-11-07 00:32:34 +00:00
|
|
|
}
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:24:02 +00:00
|
|
|
static int zap_threads(struct task_struct *tsk,
|
2013-04-30 22:28:10 +00:00
|
|
|
struct core_state *core_state, int exit_code)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
2021-06-24 07:14:30 +00:00
|
|
|
struct signal_struct *signal = tsk->signal;
|
2012-09-27 01:52:08 +00:00
|
|
|
int nr = -EAGAIN;
|
|
|
|
|
|
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
2021-06-24 07:14:30 +00:00
|
|
|
if (!(signal->flags & SIGNAL_GROUP_EXIT) && !signal->group_exec_task) {
|
2024-06-25 14:03:11 +00:00
|
|
|
/* Allow SIGKILL, see prepare_signal() */
|
2021-06-24 07:14:30 +00:00
|
|
|
signal->core_state = core_state;
|
2024-06-25 14:03:11 +00:00
|
|
|
nr = zap_process(signal, exit_code);
|
2013-04-30 22:28:10 +00:00
|
|
|
clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
|
2021-09-22 16:24:02 +00:00
|
|
|
tsk->flags |= PF_DUMPCORE;
|
|
|
|
atomic_set(&core_state->nr_threads, nr);
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int coredump_wait(int exit_code, struct core_state *core_state)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = current;
|
|
|
|
int core_waiters = -EBUSY;
|
|
|
|
|
|
|
|
init_completion(&core_state->startup);
|
|
|
|
core_state->dumper.task = tsk;
|
|
|
|
core_state->dumper.next = NULL;
|
|
|
|
|
2021-09-22 16:24:02 +00:00
|
|
|
core_waiters = zap_threads(tsk, core_state, exit_code);
|
2012-09-27 01:52:08 +00:00
|
|
|
if (core_waiters > 0) {
|
|
|
|
struct core_thread *ptr;
|
|
|
|
|
freezer,sched: Rewrite core freezer logic
Rewrite the core freezer to behave better wrt thawing and be simpler
in general.
By replacing PF_FROZEN with TASK_FROZEN, a special block state, it is
ensured frozen tasks stay frozen until thawed and don't randomly wake
up early, as is currently possible.
As such, it does away with PF_FROZEN and PF_FREEZER_SKIP, freeing up
two PF_flags (yay!).
Specifically; the current scheme works a little like:
freezer_do_not_count();
schedule();
freezer_count();
And either the task is blocked, or it lands in try_to_freezer()
through freezer_count(). Now, when it is blocked, the freezer
considers it frozen and continues.
However, on thawing, once pm_freezing is cleared, freezer_count()
stops working, and any random/spurious wakeup will let a task run
before its time.
That is, thawing tries to thaw things in explicit order; kernel
threads and workqueues before doing bringing SMP back before userspace
etc.. However due to the above mentioned races it is entirely possible
for userspace tasks to thaw (by accident) before SMP is back.
This can be a fatal problem in asymmetric ISA architectures (eg ARMv9)
where the userspace task requires a special CPU to run.
As said; replace this with a special task state TASK_FROZEN and add
the following state transitions:
TASK_FREEZABLE -> TASK_FROZEN
__TASK_STOPPED -> TASK_FROZEN
__TASK_TRACED -> TASK_FROZEN
The new TASK_FREEZABLE can be set on any state part of TASK_NORMAL
(IOW. TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE) -- any such state
is already required to deal with spurious wakeups and the freezer
causes one such when thawing the task (since the original state is
lost).
The special __TASK_{STOPPED,TRACED} states *can* be restored since
their canonical state is in ->jobctl.
With this, frozen tasks need an explicit TASK_FROZEN wakeup and are
free of undue (early / spurious) wakeups.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://lore.kernel.org/r/20220822114649.055452969@infradead.org
2022-08-22 11:18:22 +00:00
|
|
|
wait_for_completion_state(&core_state->startup,
|
|
|
|
TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
|
2012-09-27 01:52:08 +00:00
|
|
|
/*
|
|
|
|
* Wait for all the threads to become inactive, so that
|
|
|
|
* all the thread context (extended register state, like
|
|
|
|
* fpu etc) gets copied to the memory.
|
|
|
|
*/
|
|
|
|
ptr = core_state->dumper.next;
|
|
|
|
while (ptr != NULL) {
|
2022-09-06 10:39:55 +00:00
|
|
|
wait_task_inactive(ptr->task, TASK_ANY);
|
2012-09-27 01:52:08 +00:00
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return core_waiters;
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:24:02 +00:00
|
|
|
static void coredump_finish(bool core_dumped)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
|
|
|
struct core_thread *curr, *next;
|
|
|
|
struct task_struct *task;
|
|
|
|
|
2013-04-30 22:28:12 +00:00
|
|
|
spin_lock_irq(¤t->sighand->siglock);
|
2013-04-30 22:28:13 +00:00
|
|
|
if (core_dumped && !__fatal_signal_pending(current))
|
|
|
|
current->signal->group_exit_code |= 0x80;
|
2021-09-22 16:24:02 +00:00
|
|
|
next = current->signal->core_state->dumper.next;
|
|
|
|
current->signal->core_state = NULL;
|
2013-04-30 22:28:12 +00:00
|
|
|
spin_unlock_irq(¤t->sighand->siglock);
|
|
|
|
|
2012-09-27 01:52:08 +00:00
|
|
|
while ((curr = next) != NULL) {
|
|
|
|
next = curr->next;
|
|
|
|
task = curr->task;
|
|
|
|
/*
|
2021-09-01 16:33:50 +00:00
|
|
|
* see coredump_task_exit(), curr->task must not see
|
2012-09-27 01:52:08 +00:00
|
|
|
* ->task == NULL before we read ->next.
|
|
|
|
*/
|
|
|
|
smp_mb();
|
|
|
|
curr->task = NULL;
|
|
|
|
wake_up_process(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 22:28:15 +00:00
|
|
|
static bool dump_interrupted(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* SIGKILL or freezing() interrupt the coredumping. Perhaps we
|
|
|
|
* can do try_to_freeze() and check __fatal_signal_pending(),
|
|
|
|
* but then we need to teach dump_write() to restart and clear
|
|
|
|
* TIF_SIGPENDING.
|
|
|
|
*/
|
2024-09-26 18:39:02 +00:00
|
|
|
return fatal_signal_pending(current) || freezing(current);
|
2013-04-30 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 01:52:08 +00:00
|
|
|
static void wait_for_dump_helpers(struct file *file)
|
|
|
|
{
|
2013-03-21 15:16:56 +00:00
|
|
|
struct pipe_inode_info *pipe = file->private_data;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
pipe_lock(pipe);
|
|
|
|
pipe->readers++;
|
|
|
|
pipe->writers--;
|
pipe: use exclusive waits when reading or writing
This makes the pipe code use separate wait-queues and exclusive waiting
for readers and writers, avoiding a nasty thundering herd problem when
there are lots of readers waiting for data on a pipe (or, less commonly,
lots of writers waiting for a pipe to have space).
While this isn't a common occurrence in the traditional "use a pipe as a
data transport" case, where you typically only have a single reader and
a single writer process, there is one common special case: using a pipe
as a source of "locking tokens" rather than for data communication.
In particular, the GNU make jobserver code ends up using a pipe as a way
to limit parallelism, where each job consumes a token by reading a byte
from the jobserver pipe, and releases the token by writing a byte back
to the pipe.
This pattern is fairly traditional on Unix, and works very well, but
will waste a lot of time waking up a lot of processes when only a single
reader needs to be woken up when a writer releases a new token.
A simplified test-case of just this pipe interaction is to create 64
processes, and then pass a single token around between them (this
test-case also intentionally passes another token that gets ignored to
test the "wake up next" logic too, in case anybody wonders about it):
#include <unistd.h>
int main(int argc, char **argv)
{
int fd[2], counters[2];
pipe(fd);
counters[0] = 0;
counters[1] = -1;
write(fd[1], counters, sizeof(counters));
/* 64 processes */
fork(); fork(); fork(); fork(); fork(); fork();
do {
int i;
read(fd[0], &i, sizeof(i));
if (i < 0)
continue;
counters[0] = i+1;
write(fd[1], counters, (1+(i & 1)) *sizeof(int));
} while (counters[0] < 1000000);
return 0;
}
and in a perfect world, passing that token around should only cause one
context switch per transfer, when the writer of a token causes a
directed wakeup of just a single reader.
But with the "writer wakes all readers" model we traditionally had, on
my test box the above case causes more than an order of magnitude more
scheduling: instead of the expected ~1M context switches, "perf stat"
shows
231,852.37 msec task-clock # 15.857 CPUs utilized
11,250,961 context-switches # 0.049 M/sec
616,304 cpu-migrations # 0.003 M/sec
1,648 page-faults # 0.007 K/sec
1,097,903,998,514 cycles # 4.735 GHz
120,781,778,352 instructions # 0.11 insn per cycle
27,997,056,043 branches # 120.754 M/sec
283,581,233 branch-misses # 1.01% of all branches
14.621273891 seconds time elapsed
0.018243000 seconds user
3.611468000 seconds sys
before this commit.
After this commit, I get
5,229.55 msec task-clock # 3.072 CPUs utilized
1,212,233 context-switches # 0.232 M/sec
103,951 cpu-migrations # 0.020 M/sec
1,328 page-faults # 0.254 K/sec
21,307,456,166 cycles # 4.074 GHz
12,947,819,999 instructions # 0.61 insn per cycle
2,881,985,678 branches # 551.096 M/sec
64,267,015 branch-misses # 2.23% of all branches
1.702148350 seconds time elapsed
0.004868000 seconds user
0.110786000 seconds sys
instead. Much better.
[ Note! This kernel improvement seems to be very good at triggering a
race condition in the make jobserver (in GNU make 4.2.1) for me. It's
a long known bug that was fixed back in June 2017 by GNU make commit
b552b0525198 ("[SV 51159] Use a non-blocking read with pselect to
avoid hangs.").
But there wasn't a new release of GNU make until 4.3 on Jan 19 2020,
so a number of distributions may still have the buggy version. Some
have backported the fix to their 4.2.1 release, though, and even
without the fix it's quite timing-dependent whether the bug actually
is hit. ]
Josh Triplett says:
"I've been hammering on your pipe fix patch (switching to exclusive
wait queues) for a month or so, on several different systems, and I've
run into no issues with it. The patch *substantially* improves
parallel build times on large (~100 CPU) systems, both with parallel
make and with other things that use make's pipe-based jobserver.
All current distributions (including stable and long-term stable
distributions) have versions of GNU make that no longer have the
jobserver bug"
Tested-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-12-09 17:48:27 +00:00
|
|
|
wake_up_interruptible_sync(&pipe->rd_wait);
|
2013-04-30 22:28:17 +00:00
|
|
|
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
|
|
|
|
pipe_unlock(pipe);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-04-30 22:28:17 +00:00
|
|
|
/*
|
|
|
|
* We actually want wait_event_freezable() but then we need
|
|
|
|
* to clear TIF_SIGPENDING and improve dump_interrupted().
|
|
|
|
*/
|
pipe: use exclusive waits when reading or writing
This makes the pipe code use separate wait-queues and exclusive waiting
for readers and writers, avoiding a nasty thundering herd problem when
there are lots of readers waiting for data on a pipe (or, less commonly,
lots of writers waiting for a pipe to have space).
While this isn't a common occurrence in the traditional "use a pipe as a
data transport" case, where you typically only have a single reader and
a single writer process, there is one common special case: using a pipe
as a source of "locking tokens" rather than for data communication.
In particular, the GNU make jobserver code ends up using a pipe as a way
to limit parallelism, where each job consumes a token by reading a byte
from the jobserver pipe, and releases the token by writing a byte back
to the pipe.
This pattern is fairly traditional on Unix, and works very well, but
will waste a lot of time waking up a lot of processes when only a single
reader needs to be woken up when a writer releases a new token.
A simplified test-case of just this pipe interaction is to create 64
processes, and then pass a single token around between them (this
test-case also intentionally passes another token that gets ignored to
test the "wake up next" logic too, in case anybody wonders about it):
#include <unistd.h>
int main(int argc, char **argv)
{
int fd[2], counters[2];
pipe(fd);
counters[0] = 0;
counters[1] = -1;
write(fd[1], counters, sizeof(counters));
/* 64 processes */
fork(); fork(); fork(); fork(); fork(); fork();
do {
int i;
read(fd[0], &i, sizeof(i));
if (i < 0)
continue;
counters[0] = i+1;
write(fd[1], counters, (1+(i & 1)) *sizeof(int));
} while (counters[0] < 1000000);
return 0;
}
and in a perfect world, passing that token around should only cause one
context switch per transfer, when the writer of a token causes a
directed wakeup of just a single reader.
But with the "writer wakes all readers" model we traditionally had, on
my test box the above case causes more than an order of magnitude more
scheduling: instead of the expected ~1M context switches, "perf stat"
shows
231,852.37 msec task-clock # 15.857 CPUs utilized
11,250,961 context-switches # 0.049 M/sec
616,304 cpu-migrations # 0.003 M/sec
1,648 page-faults # 0.007 K/sec
1,097,903,998,514 cycles # 4.735 GHz
120,781,778,352 instructions # 0.11 insn per cycle
27,997,056,043 branches # 120.754 M/sec
283,581,233 branch-misses # 1.01% of all branches
14.621273891 seconds time elapsed
0.018243000 seconds user
3.611468000 seconds sys
before this commit.
After this commit, I get
5,229.55 msec task-clock # 3.072 CPUs utilized
1,212,233 context-switches # 0.232 M/sec
103,951 cpu-migrations # 0.020 M/sec
1,328 page-faults # 0.254 K/sec
21,307,456,166 cycles # 4.074 GHz
12,947,819,999 instructions # 0.61 insn per cycle
2,881,985,678 branches # 551.096 M/sec
64,267,015 branch-misses # 2.23% of all branches
1.702148350 seconds time elapsed
0.004868000 seconds user
0.110786000 seconds sys
instead. Much better.
[ Note! This kernel improvement seems to be very good at triggering a
race condition in the make jobserver (in GNU make 4.2.1) for me. It's
a long known bug that was fixed back in June 2017 by GNU make commit
b552b0525198 ("[SV 51159] Use a non-blocking read with pselect to
avoid hangs.").
But there wasn't a new release of GNU make until 4.3 on Jan 19 2020,
so a number of distributions may still have the buggy version. Some
have backported the fix to their 4.2.1 release, though, and even
without the fix it's quite timing-dependent whether the bug actually
is hit. ]
Josh Triplett says:
"I've been hammering on your pipe fix patch (switching to exclusive
wait queues) for a month or so, on several different systems, and I've
run into no issues with it. The patch *substantially* improves
parallel build times on large (~100 CPU) systems, both with parallel
make and with other things that use make's pipe-based jobserver.
All current distributions (including stable and long-term stable
distributions) have versions of GNU make that no longer have the
jobserver bug"
Tested-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-12-09 17:48:27 +00:00
|
|
|
wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-04-30 22:28:17 +00:00
|
|
|
pipe_lock(pipe);
|
2012-09-27 01:52:08 +00:00
|
|
|
pipe->readers--;
|
|
|
|
pipe->writers++;
|
|
|
|
pipe_unlock(pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* umh_pipe_setup
|
|
|
|
* helper function to customize the process used
|
|
|
|
* to collect the core in userspace. Specifically
|
|
|
|
* it sets up a pipe and installs it as fd 0 (stdin)
|
|
|
|
* for the process. Returns 0 on success, or
|
|
|
|
* PTR_ERR on failure.
|
|
|
|
* Note that it also sets the core limit to 1. This
|
|
|
|
* is a special value that we use to trap recursive
|
|
|
|
* core dumps
|
|
|
|
*/
|
|
|
|
static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
|
|
|
|
{
|
|
|
|
struct file *files[2];
|
|
|
|
struct coredump_params *cp = (struct coredump_params *)info->data;
|
|
|
|
int err = create_pipe_files(files, 0);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
cp->file = files[1];
|
|
|
|
|
2012-10-16 17:30:07 +00:00
|
|
|
err = replace_fd(0, files[0], 0);
|
|
|
|
fput(files[0]);
|
2012-09-27 01:52:08 +00:00
|
|
|
/* and disallow core files too */
|
|
|
|
current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
|
|
|
|
|
2012-10-16 17:30:07 +00:00
|
|
|
return err;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 18:39:02 +00:00
|
|
|
void do_coredump(const kernel_siginfo_t *siginfo)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
|
|
|
struct core_state core_state;
|
|
|
|
struct core_name cn;
|
|
|
|
struct mm_struct *mm = current->mm;
|
|
|
|
struct linux_binfmt * binfmt;
|
|
|
|
const struct cred *old_cred;
|
|
|
|
struct cred *cred;
|
2024-09-26 18:39:02 +00:00
|
|
|
int retval = 0;
|
2012-09-27 01:52:08 +00:00
|
|
|
int ispipe;
|
2019-08-03 04:49:05 +00:00
|
|
|
size_t *argv = NULL;
|
|
|
|
int argc = 0;
|
fs: if a coredump already exists, unlink and recreate with O_EXCL
It was possible for an attacking user to trick root (or another user) into
writing his coredumps into an attacker-readable, pre-existing file using
rename() or link(), causing the disclosure of secret data from the victim
process' virtual memory. Depending on the configuration, it was also
possible to trick root into overwriting system files with coredumps. Fix
that issue by never writing coredumps into existing files.
Requirements for the attack:
- The attack only applies if the victim's process has a nonzero
RLIMIT_CORE and is dumpable.
- The attacker can trick the victim into coredumping into an
attacker-writable directory D, either because the core_pattern is
relative and the victim's cwd is attacker-writable or because an
absolute core_pattern pointing to a world-writable directory is used.
- The attacker has one of these:
A: on a system with protected_hardlinks=0:
execute access to a folder containing a victim-owned,
attacker-readable file on the same partition as D, and the
victim-owned file will be deleted before the main part of the attack
takes place. (In practice, there are lots of files that fulfill
this condition, e.g. entries in Debian's /var/lib/dpkg/info/.)
This does not apply to most Linux systems because most distros set
protected_hardlinks=1.
B: on a system with protected_hardlinks=1:
execute access to a folder containing a victim-owned,
attacker-readable and attacker-writable file on the same partition
as D, and the victim-owned file will be deleted before the main part
of the attack takes place.
(This seems to be uncommon.)
C: on any system, independent of protected_hardlinks:
write access to a non-sticky folder containing a victim-owned,
attacker-readable file on the same partition as D
(This seems to be uncommon.)
The basic idea is that the attacker moves the victim-owned file to where
he expects the victim process to dump its core. The victim process dumps
its core into the existing file, and the attacker reads the coredump from
it.
If the attacker can't move the file because he does not have write access
to the containing directory, he can instead link the file to a directory
he controls, then wait for the original link to the file to be deleted
(because the kernel checks that the link count of the corefile is 1).
A less reliable variant that requires D to be non-sticky works with link()
and does not require deletion of the original link: link() the file into
D, but then unlink() it directly before the kernel performs the link count
check.
On systems with protected_hardlinks=0, this variant allows an attacker to
not only gain information from coredumps, but also clobber existing,
victim-writable files with coredumps. (This could theoretically lead to a
privilege escalation.)
Signed-off-by: Jann Horn <jann@thejh.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-09-09 22:38:28 +00:00
|
|
|
/* require nonrelative corefile path and be extra careful */
|
|
|
|
bool need_suid_safe = false;
|
2013-04-30 22:28:13 +00:00
|
|
|
bool core_dumped = false;
|
2012-09-27 01:52:08 +00:00
|
|
|
static atomic_t core_dump_count = ATOMIC_INIT(0);
|
|
|
|
struct coredump_params cprm = {
|
2012-10-05 00:15:29 +00:00
|
|
|
.siginfo = siginfo,
|
2012-09-27 01:52:08 +00:00
|
|
|
.limit = rlimit(RLIMIT_CORE),
|
|
|
|
/*
|
|
|
|
* We must use the same mm->flags while dumping core to avoid
|
|
|
|
* inconsistency of bit flags, since this flag is not protected
|
|
|
|
* by any locks.
|
|
|
|
*/
|
|
|
|
.mm_flags = mm->flags,
|
2022-03-08 18:55:29 +00:00
|
|
|
.vma_meta = NULL,
|
2022-09-03 06:43:30 +00:00
|
|
|
.cpu = raw_smp_processor_id(),
|
2012-09-27 01:52:08 +00:00
|
|
|
};
|
|
|
|
|
2012-10-05 00:15:29 +00:00
|
|
|
audit_core_dumps(siginfo->si_signo);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
binfmt = mm->binfmt;
|
2024-09-26 18:39:02 +00:00
|
|
|
if (!binfmt || !binfmt->core_dump)
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail;
|
2024-09-26 18:39:02 +00:00
|
|
|
if (!__get_dumpable(cprm.mm_flags))
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
cred = prepare_creds();
|
2024-09-26 18:39:02 +00:00
|
|
|
if (!cred)
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail;
|
|
|
|
/*
|
|
|
|
* We cannot trust fsuid as being the "true" uid of the process
|
|
|
|
* nor do we know its entire history. We only know it was tainted
|
|
|
|
* so we dump it as root in mode 2, and only into a controlled
|
|
|
|
* environment (pipe handler or fully qualified path).
|
|
|
|
*/
|
2013-02-28 01:03:15 +00:00
|
|
|
if (__get_dumpable(cprm.mm_flags) == SUID_DUMP_ROOT) {
|
2012-09-27 01:52:08 +00:00
|
|
|
/* Setuid core dump mode */
|
|
|
|
cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
|
fs: if a coredump already exists, unlink and recreate with O_EXCL
It was possible for an attacking user to trick root (or another user) into
writing his coredumps into an attacker-readable, pre-existing file using
rename() or link(), causing the disclosure of secret data from the victim
process' virtual memory. Depending on the configuration, it was also
possible to trick root into overwriting system files with coredumps. Fix
that issue by never writing coredumps into existing files.
Requirements for the attack:
- The attack only applies if the victim's process has a nonzero
RLIMIT_CORE and is dumpable.
- The attacker can trick the victim into coredumping into an
attacker-writable directory D, either because the core_pattern is
relative and the victim's cwd is attacker-writable or because an
absolute core_pattern pointing to a world-writable directory is used.
- The attacker has one of these:
A: on a system with protected_hardlinks=0:
execute access to a folder containing a victim-owned,
attacker-readable file on the same partition as D, and the
victim-owned file will be deleted before the main part of the attack
takes place. (In practice, there are lots of files that fulfill
this condition, e.g. entries in Debian's /var/lib/dpkg/info/.)
This does not apply to most Linux systems because most distros set
protected_hardlinks=1.
B: on a system with protected_hardlinks=1:
execute access to a folder containing a victim-owned,
attacker-readable and attacker-writable file on the same partition
as D, and the victim-owned file will be deleted before the main part
of the attack takes place.
(This seems to be uncommon.)
C: on any system, independent of protected_hardlinks:
write access to a non-sticky folder containing a victim-owned,
attacker-readable file on the same partition as D
(This seems to be uncommon.)
The basic idea is that the attacker moves the victim-owned file to where
he expects the victim process to dump its core. The victim process dumps
its core into the existing file, and the attacker reads the coredump from
it.
If the attacker can't move the file because he does not have write access
to the containing directory, he can instead link the file to a directory
he controls, then wait for the original link to the file to be deleted
(because the kernel checks that the link count of the corefile is 1).
A less reliable variant that requires D to be non-sticky works with link()
and does not require deletion of the original link: link() the file into
D, but then unlink() it directly before the kernel performs the link count
check.
On systems with protected_hardlinks=0, this variant allows an attacker to
not only gain information from coredumps, but also clobber existing,
victim-writable files with coredumps. (This could theoretically lead to a
privilege escalation.)
Signed-off-by: Jann Horn <jann@thejh.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-09-09 22:38:28 +00:00
|
|
|
need_suid_safe = true;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 00:15:29 +00:00
|
|
|
retval = coredump_wait(siginfo->si_signo, &core_state);
|
2012-09-27 01:52:08 +00:00
|
|
|
if (retval < 0)
|
|
|
|
goto fail_creds;
|
|
|
|
|
|
|
|
old_cred = override_creds(cred);
|
|
|
|
|
2019-08-03 04:49:05 +00:00
|
|
|
ispipe = format_corename(&cn, &cprm, &argv, &argc);
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-04-30 22:28:06 +00:00
|
|
|
if (ispipe) {
|
2019-08-03 04:49:05 +00:00
|
|
|
int argi;
|
2012-09-27 01:52:08 +00:00
|
|
|
int dump_count;
|
|
|
|
char **helper_argv;
|
2013-04-30 22:28:07 +00:00
|
|
|
struct subprocess_info *sub_info;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
|
|
|
if (ispipe < 0) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("format_corename failed, aborting core");
|
2013-07-03 22:08:16 +00:00
|
|
|
goto fail_unlock;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cprm.limit == 1) {
|
|
|
|
/* See umh_pipe_setup() which sets RLIMIT_CORE = 1.
|
|
|
|
*
|
|
|
|
* Normally core limits are irrelevant to pipes, since
|
|
|
|
* we're not writing to the file system, but we use
|
2015-02-05 13:35:05 +00:00
|
|
|
* cprm.limit of 1 here as a special value, this is a
|
2012-09-27 01:52:08 +00:00
|
|
|
* consistent way to catch recursive crashes.
|
|
|
|
* We can still crash if the core_pattern binary sets
|
|
|
|
* RLIM_CORE = !1, but it runs as root, and can do
|
|
|
|
* lots of stupid things.
|
|
|
|
*
|
|
|
|
* Note that we use task_tgid_vnr here to grab the pid
|
|
|
|
* of the process group leader. That way we get the
|
|
|
|
* right pid if a thread in a multi-threaded
|
|
|
|
* core_pattern process dies.
|
|
|
|
*/
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("RLIMIT_CORE is set to 1, aborting core");
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail_unlock;
|
|
|
|
}
|
|
|
|
cprm.limit = RLIM_INFINITY;
|
|
|
|
|
|
|
|
dump_count = atomic_inc_return(&core_dump_count);
|
|
|
|
if (core_pipe_limit && (core_pipe_limit < dump_count)) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("over core_pipe_limit, skipping core dump");
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail_dropcount;
|
|
|
|
}
|
|
|
|
|
2019-08-03 04:49:05 +00:00
|
|
|
helper_argv = kmalloc_array(argc + 1, sizeof(*helper_argv),
|
|
|
|
GFP_KERNEL);
|
2012-09-27 01:52:08 +00:00
|
|
|
if (!helper_argv) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("%s failed to allocate memory", __func__);
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail_dropcount;
|
|
|
|
}
|
2019-08-03 04:49:05 +00:00
|
|
|
for (argi = 0; argi < argc; argi++)
|
|
|
|
helper_argv[argi] = cn.corename + argv[argi];
|
|
|
|
helper_argv[argi] = NULL;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2013-04-30 22:28:07 +00:00
|
|
|
retval = -ENOMEM;
|
|
|
|
sub_info = call_usermodehelper_setup(helper_argv[0],
|
|
|
|
helper_argv, NULL, GFP_KERNEL,
|
|
|
|
umh_pipe_setup, NULL, &cprm);
|
|
|
|
if (sub_info)
|
|
|
|
retval = call_usermodehelper_exec(sub_info,
|
|
|
|
UMH_WAIT_EXEC);
|
|
|
|
|
2019-08-03 04:49:05 +00:00
|
|
|
kfree(helper_argv);
|
2012-09-27 01:52:08 +00:00
|
|
|
if (retval) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("|%s pipe failed", cn.corename);
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
2013-04-30 22:28:06 +00:00
|
|
|
}
|
2012-09-27 01:52:08 +00:00
|
|
|
} else {
|
2023-01-13 11:49:10 +00:00
|
|
|
struct mnt_idmap *idmap;
|
2012-09-27 01:52:08 +00:00
|
|
|
struct inode *inode;
|
coredump: require O_WRONLY instead of O_RDWR
The motivation for this patch has been to enable using a stricter
apparmor profile to prevent programs from reading any coredump in the
system.
However, this became something else. The following details are based on
Christian's and Linus' archeology into the history of the number "2" in
the coredump handling code.
To make sure we're not accidently introducing some subtle behavioral
change into the coredump code we set out on a voyage into the depths of
history.git to figure out why this was O_RDWR in the first place.
Coredump handling was introduced over 30 years ago in commit
ddc733f452e0 ("[PATCH] Linux-0.97 (August 1, 1992)").
The original code used O_WRONLY:
open_namei("core",O_CREAT | O_WRONLY | O_TRUNC,0600,&inode,NULL)
However, this changed in 1993 and starting with commit
9cb9f18b5d26 ("[PATCH] Linux-0.99.10 (June 7, 1993)") the coredump code
suddenly used the constant "2":
open_namei("core",O_CREAT | 2 | O_TRUNC,0600,&inode,NULL)
This was curious as in the same commit the kernel switched from
constants to proper defines in other places such as KERNEL_DS and
USER_DS and O_RDWR did already exist.
So why was "2" used? It turns out that open_namei() - an early version
of what later turned into filp_open() - didn't accept O_RDWR.
A semantic quirk of the open() uapi is the definition of the O_RDONLY
flag. It would seem natural to define:
#define O_RDWR (O_RDONLY | O_WRONLY)
but that isn't possible because:
#define O_RDONLY 0
This makes O_RDONLY effectively meaningless when passed to the kernel.
In other words, there has never been a way - until O_PATH at least - to
open a file without any permission; O_RDONLY was always implied on the
uapi side while the kernel does in fact allow opening files without
permissions.
The trouble comes when trying to map the uapi flags onto the
corresponding file mode flags FMODE_{READ,WRITE}. This mapping still
happens today and is causing issues to this day (We ran into this
during additions for openat2() for example.).
So the special value "3" was used to indicate that the file was opened
for special access:
f->f_flags = flag = flags;
f->f_mode = (flag+1) & O_ACCMODE;
if (f->f_mode)
flag++;
This allowed the file mode to be set to FMODE_READ | FMODE_WRITE mapping
the O_{RDONLY,WRONLY,RDWR} flags into the FMODE_{READ,WRITE} flags. The
special access then required read-write permissions and 0 was used to
access symlinks.
But back when ddc733f452e0 ("[PATCH] Linux-0.97 (August 1, 1992)") added
coredump handling open_namei() took the FMODE_{READ,WRITE} flags as an
argument. So the coredump handling introduced in
ddc733f452e0 ("[PATCH] Linux-0.97 (August 1, 1992)") was buggy because
O_WRONLY shouldn't have been passed. Since O_WRONLY is 1 but
open_namei() took FMODE_{READ,WRITE} it was passed FMODE_READ on
accident.
So 9cb9f18b5d26 ("[PATCH] Linux-0.99.10 (June 7, 1993)") was a bugfix
for this and the 2 didn't really mean O_RDWR, it meant FMODE_WRITE which
was correct.
The clue is that FMODE_{READ,WRITE} didn't exist yet and thus a raw "2"
value was passed.
Fast forward 5 years when around 2.2.4pre4 (February 16, 1999) this code
was changed to:
- dentry = open_namei(corefile,O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
...
+ file = filp_open(corefile,O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
At this point the raw "2" should have become O_WRONLY again as
filp_open() didn't take FMODE_{READ,WRITE} but O_{RDONLY,WRONLY,RDWR}.
Another 17 years later, the code was changed again cementing the mistake
and making it almost impossible to detect when commit
378c6520e7d2 ("fs/coredump: prevent fsuid=0 dumps into user-controlled directories")
replaced the raw "2" with O_RDWR.
And now, here we are with this patch that sent us on a quest to answer
the big questions in life such as "Why are coredump files opened with
O_RDWR?" and "Is it safe to just use O_WRONLY?".
So with this commit we're reintroducing O_WRONLY again and bringing this
code back to its original state when it was first introduced in commit
ddc733f452e0 ("[PATCH] Linux-0.97 (August 1, 1992)") over 30 years ago.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20230420120409.602576-1-vsementsov@yandex-team.ru>
[brauner@kernel.org: completely rewritten commit message]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2023-04-20 12:04:09 +00:00
|
|
|
int open_flags = O_CREAT | O_WRONLY | O_NOFOLLOW |
|
2016-03-22 21:25:36 +00:00
|
|
|
O_LARGEFILE | O_EXCL;
|
2012-09-27 01:52:08 +00:00
|
|
|
|
2024-09-26 18:39:02 +00:00
|
|
|
if (cprm.limit < binfmt->min_coredump)
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail_unlock;
|
|
|
|
|
fs: if a coredump already exists, unlink and recreate with O_EXCL
It was possible for an attacking user to trick root (or another user) into
writing his coredumps into an attacker-readable, pre-existing file using
rename() or link(), causing the disclosure of secret data from the victim
process' virtual memory. Depending on the configuration, it was also
possible to trick root into overwriting system files with coredumps. Fix
that issue by never writing coredumps into existing files.
Requirements for the attack:
- The attack only applies if the victim's process has a nonzero
RLIMIT_CORE and is dumpable.
- The attacker can trick the victim into coredumping into an
attacker-writable directory D, either because the core_pattern is
relative and the victim's cwd is attacker-writable or because an
absolute core_pattern pointing to a world-writable directory is used.
- The attacker has one of these:
A: on a system with protected_hardlinks=0:
execute access to a folder containing a victim-owned,
attacker-readable file on the same partition as D, and the
victim-owned file will be deleted before the main part of the attack
takes place. (In practice, there are lots of files that fulfill
this condition, e.g. entries in Debian's /var/lib/dpkg/info/.)
This does not apply to most Linux systems because most distros set
protected_hardlinks=1.
B: on a system with protected_hardlinks=1:
execute access to a folder containing a victim-owned,
attacker-readable and attacker-writable file on the same partition
as D, and the victim-owned file will be deleted before the main part
of the attack takes place.
(This seems to be uncommon.)
C: on any system, independent of protected_hardlinks:
write access to a non-sticky folder containing a victim-owned,
attacker-readable file on the same partition as D
(This seems to be uncommon.)
The basic idea is that the attacker moves the victim-owned file to where
he expects the victim process to dump its core. The victim process dumps
its core into the existing file, and the attacker reads the coredump from
it.
If the attacker can't move the file because he does not have write access
to the containing directory, he can instead link the file to a directory
he controls, then wait for the original link to the file to be deleted
(because the kernel checks that the link count of the corefile is 1).
A less reliable variant that requires D to be non-sticky works with link()
and does not require deletion of the original link: link() the file into
D, but then unlink() it directly before the kernel performs the link count
check.
On systems with protected_hardlinks=0, this variant allows an attacker to
not only gain information from coredumps, but also clobber existing,
victim-writable files with coredumps. (This could theoretically lead to a
privilege escalation.)
Signed-off-by: Jann Horn <jann@thejh.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-09-09 22:38:28 +00:00
|
|
|
if (need_suid_safe && cn.corename[0] != '/') {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure(
|
|
|
|
"this process can only dump core to a fully qualified path, skipping core dump");
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail_unlock;
|
|
|
|
}
|
|
|
|
|
fs: if a coredump already exists, unlink and recreate with O_EXCL
It was possible for an attacking user to trick root (or another user) into
writing his coredumps into an attacker-readable, pre-existing file using
rename() or link(), causing the disclosure of secret data from the victim
process' virtual memory. Depending on the configuration, it was also
possible to trick root into overwriting system files with coredumps. Fix
that issue by never writing coredumps into existing files.
Requirements for the attack:
- The attack only applies if the victim's process has a nonzero
RLIMIT_CORE and is dumpable.
- The attacker can trick the victim into coredumping into an
attacker-writable directory D, either because the core_pattern is
relative and the victim's cwd is attacker-writable or because an
absolute core_pattern pointing to a world-writable directory is used.
- The attacker has one of these:
A: on a system with protected_hardlinks=0:
execute access to a folder containing a victim-owned,
attacker-readable file on the same partition as D, and the
victim-owned file will be deleted before the main part of the attack
takes place. (In practice, there are lots of files that fulfill
this condition, e.g. entries in Debian's /var/lib/dpkg/info/.)
This does not apply to most Linux systems because most distros set
protected_hardlinks=1.
B: on a system with protected_hardlinks=1:
execute access to a folder containing a victim-owned,
attacker-readable and attacker-writable file on the same partition
as D, and the victim-owned file will be deleted before the main part
of the attack takes place.
(This seems to be uncommon.)
C: on any system, independent of protected_hardlinks:
write access to a non-sticky folder containing a victim-owned,
attacker-readable file on the same partition as D
(This seems to be uncommon.)
The basic idea is that the attacker moves the victim-owned file to where
he expects the victim process to dump its core. The victim process dumps
its core into the existing file, and the attacker reads the coredump from
it.
If the attacker can't move the file because he does not have write access
to the containing directory, he can instead link the file to a directory
he controls, then wait for the original link to the file to be deleted
(because the kernel checks that the link count of the corefile is 1).
A less reliable variant that requires D to be non-sticky works with link()
and does not require deletion of the original link: link() the file into
D, but then unlink() it directly before the kernel performs the link count
check.
On systems with protected_hardlinks=0, this variant allows an attacker to
not only gain information from coredumps, but also clobber existing,
victim-writable files with coredumps. (This could theoretically lead to a
privilege escalation.)
Signed-off-by: Jann Horn <jann@thejh.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-09-09 22:38:28 +00:00
|
|
|
/*
|
|
|
|
* Unlink the file if it exists unless this is a SUID
|
|
|
|
* binary - in that case, we're running around with root
|
|
|
|
* privs and don't want to unlink another user's coredump.
|
|
|
|
*/
|
|
|
|
if (!need_suid_safe) {
|
|
|
|
/*
|
|
|
|
* If it doesn't exist, that's fine. If there's some
|
|
|
|
* other problem, we'll catch it at the filp_open().
|
|
|
|
*/
|
2017-11-04 10:44:46 +00:00
|
|
|
do_unlinkat(AT_FDCWD, getname_kernel(cn.corename));
|
fs: if a coredump already exists, unlink and recreate with O_EXCL
It was possible for an attacking user to trick root (or another user) into
writing his coredumps into an attacker-readable, pre-existing file using
rename() or link(), causing the disclosure of secret data from the victim
process' virtual memory. Depending on the configuration, it was also
possible to trick root into overwriting system files with coredumps. Fix
that issue by never writing coredumps into existing files.
Requirements for the attack:
- The attack only applies if the victim's process has a nonzero
RLIMIT_CORE and is dumpable.
- The attacker can trick the victim into coredumping into an
attacker-writable directory D, either because the core_pattern is
relative and the victim's cwd is attacker-writable or because an
absolute core_pattern pointing to a world-writable directory is used.
- The attacker has one of these:
A: on a system with protected_hardlinks=0:
execute access to a folder containing a victim-owned,
attacker-readable file on the same partition as D, and the
victim-owned file will be deleted before the main part of the attack
takes place. (In practice, there are lots of files that fulfill
this condition, e.g. entries in Debian's /var/lib/dpkg/info/.)
This does not apply to most Linux systems because most distros set
protected_hardlinks=1.
B: on a system with protected_hardlinks=1:
execute access to a folder containing a victim-owned,
attacker-readable and attacker-writable file on the same partition
as D, and the victim-owned file will be deleted before the main part
of the attack takes place.
(This seems to be uncommon.)
C: on any system, independent of protected_hardlinks:
write access to a non-sticky folder containing a victim-owned,
attacker-readable file on the same partition as D
(This seems to be uncommon.)
The basic idea is that the attacker moves the victim-owned file to where
he expects the victim process to dump its core. The victim process dumps
its core into the existing file, and the attacker reads the coredump from
it.
If the attacker can't move the file because he does not have write access
to the containing directory, he can instead link the file to a directory
he controls, then wait for the original link to the file to be deleted
(because the kernel checks that the link count of the corefile is 1).
A less reliable variant that requires D to be non-sticky works with link()
and does not require deletion of the original link: link() the file into
D, but then unlink() it directly before the kernel performs the link count
check.
On systems with protected_hardlinks=0, this variant allows an attacker to
not only gain information from coredumps, but also clobber existing,
victim-writable files with coredumps. (This could theoretically lead to a
privilege escalation.)
Signed-off-by: Jann Horn <jann@thejh.net>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-09-09 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is a race between unlinking and creating the
|
|
|
|
* file, but if that causes an EEXIST here, that's
|
|
|
|
* fine - another process raced with us while creating
|
|
|
|
* the corefile, and the other process won. To userspace,
|
|
|
|
* what matters is that at least one of the two processes
|
|
|
|
* writes its coredump successfully, not which one.
|
|
|
|
*/
|
2016-03-22 21:25:36 +00:00
|
|
|
if (need_suid_safe) {
|
|
|
|
/*
|
|
|
|
* Using user namespaces, normal user tasks can change
|
|
|
|
* their current->fs->root to point to arbitrary
|
|
|
|
* directories. Since the intention of the "only dump
|
|
|
|
* with a fully qualified path" rule is to control where
|
|
|
|
* coredumps may be placed using root privileges,
|
|
|
|
* current->fs->root must not be used. Instead, use the
|
|
|
|
* root directory of init_task.
|
|
|
|
*/
|
|
|
|
struct path root;
|
|
|
|
|
|
|
|
task_lock(&init_task);
|
|
|
|
get_fs_root(init_task.fs, &root);
|
|
|
|
task_unlock(&init_task);
|
2021-04-01 23:00:57 +00:00
|
|
|
cprm.file = file_open_root(&root, cn.corename,
|
|
|
|
open_flags, 0600);
|
2016-03-22 21:25:36 +00:00
|
|
|
path_put(&root);
|
|
|
|
} else {
|
|
|
|
cprm.file = filp_open(cn.corename, open_flags, 0600);
|
|
|
|
}
|
2024-09-26 18:39:02 +00:00
|
|
|
if (IS_ERR(cprm.file))
|
2012-09-27 01:52:08 +00:00
|
|
|
goto fail_unlock;
|
|
|
|
|
2013-01-23 22:07:38 +00:00
|
|
|
inode = file_inode(cprm.file);
|
2024-09-26 18:39:02 +00:00
|
|
|
if (inode->i_nlink > 1)
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
2024-09-26 18:39:02 +00:00
|
|
|
if (d_unhashed(cprm.file->f_path.dentry))
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
|
|
|
/*
|
|
|
|
* AK: actually i see no reason to not allow this for named
|
|
|
|
* pipes etc, but keep the previous behaviour for now.
|
|
|
|
*/
|
2024-09-26 18:39:02 +00:00
|
|
|
if (!S_ISREG(inode->i_mode))
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
|
|
|
/*
|
2015-09-09 22:38:30 +00:00
|
|
|
* Don't dump core if the filesystem changed owner or mode
|
|
|
|
* of the file during file creation. This is an issue when
|
|
|
|
* a process dumps core while its cwd is e.g. on a vfat
|
|
|
|
* filesystem.
|
2012-09-27 01:52:08 +00:00
|
|
|
*/
|
2023-01-13 11:49:10 +00:00
|
|
|
idmap = file_mnt_idmap(cprm.file);
|
2023-01-13 11:49:30 +00:00
|
|
|
if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
|
2022-06-22 20:12:16 +00:00
|
|
|
current_fsuid())) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("Core dump to %s aborted: "
|
|
|
|
"cannot preserve file owner", cn.corename);
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
2021-09-08 03:00:29 +00:00
|
|
|
}
|
|
|
|
if ((inode->i_mode & 0677) != 0600) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("Core dump to %s aborted: "
|
|
|
|
"cannot preserve file permissions", cn.corename);
|
2015-09-09 22:38:30 +00:00
|
|
|
goto close_fail;
|
2021-09-08 03:00:29 +00:00
|
|
|
}
|
2024-09-26 18:39:02 +00:00
|
|
|
if (!(cprm.file->f_mode & FMODE_CAN_WRITE))
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
2024-09-26 18:39:02 +00:00
|
|
|
if (do_truncate(idmap, cprm.file->f_path.dentry,
|
|
|
|
0, 0, cprm.file))
|
2012-09-27 01:52:08 +00:00
|
|
|
goto close_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get us an unshared descriptor table; almost always a no-op */
|
2020-11-25 21:51:32 +00:00
|
|
|
/* The cell spufs coredump code reads the file descriptor tables */
|
2020-11-20 23:14:19 +00:00
|
|
|
retval = unshare_files();
|
2012-09-27 01:52:08 +00:00
|
|
|
if (retval)
|
|
|
|
goto close_fail;
|
2013-05-04 18:45:54 +00:00
|
|
|
if (!dump_interrupted()) {
|
2020-04-16 16:28:59 +00:00
|
|
|
/*
|
|
|
|
* umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
|
|
|
|
* have this set to NULL.
|
|
|
|
*/
|
|
|
|
if (!cprm.file) {
|
2024-07-18 18:27:24 +00:00
|
|
|
coredump_report_failure("Core dump to |%s disabled", cn.corename);
|
2020-04-16 16:28:59 +00:00
|
|
|
goto close_fail;
|
|
|
|
}
|
2024-09-26 18:39:02 +00:00
|
|
|
if (!dump_vma_snapshot(&cprm))
|
2022-03-08 18:55:29 +00:00
|
|
|
goto close_fail;
|
|
|
|
|
2013-05-04 18:45:54 +00:00
|
|
|
file_start_write(cprm.file);
|
|
|
|
core_dumped = binfmt->core_dump(&cprm);
|
2020-03-08 13:16:37 +00:00
|
|
|
/*
|
|
|
|
* Ensures that file size is big enough to contain the current
|
|
|
|
* file postion. This prevents gdb from complaining about
|
|
|
|
* a truncated file if the last "write" to the file was
|
|
|
|
* dump_skip.
|
|
|
|
*/
|
|
|
|
if (cprm.to_skip) {
|
|
|
|
cprm.to_skip--;
|
|
|
|
dump_emit(&cprm, "", 1);
|
|
|
|
}
|
2013-05-04 18:45:54 +00:00
|
|
|
file_end_write(cprm.file);
|
2022-03-08 19:04:19 +00:00
|
|
|
free_vma_snapshot(&cprm);
|
2013-05-04 18:45:54 +00:00
|
|
|
}
|
2012-09-27 01:52:08 +00:00
|
|
|
if (ispipe && core_pipe_limit)
|
|
|
|
wait_for_dump_helpers(cprm.file);
|
|
|
|
close_fail:
|
|
|
|
if (cprm.file)
|
|
|
|
filp_close(cprm.file, NULL);
|
|
|
|
fail_dropcount:
|
|
|
|
if (ispipe)
|
|
|
|
atomic_dec(&core_dump_count);
|
|
|
|
fail_unlock:
|
2019-08-03 04:49:05 +00:00
|
|
|
kfree(argv);
|
2012-09-27 01:52:08 +00:00
|
|
|
kfree(cn.corename);
|
2021-09-22 16:24:02 +00:00
|
|
|
coredump_finish(core_dumped);
|
2012-09-27 01:52:08 +00:00
|
|
|
revert_creds(old_cred);
|
|
|
|
fail_creds:
|
|
|
|
put_cred(cred);
|
|
|
|
fail:
|
2024-09-26 18:39:02 +00:00
|
|
|
return;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Core dumping helper functions. These are the only things you should
|
|
|
|
* do on a core-file: use only these functions to write out all the
|
|
|
|
* necessary info.
|
|
|
|
*/
|
2020-03-08 13:16:37 +00:00
|
|
|
static int __dump_emit(struct coredump_params *cprm, const void *addr, int nr)
|
2013-10-05 19:32:35 +00:00
|
|
|
{
|
|
|
|
struct file *file = cprm->file;
|
2013-10-08 13:11:48 +00:00
|
|
|
loff_t pos = file->f_pos;
|
|
|
|
ssize_t n;
|
2016-05-11 22:16:37 +00:00
|
|
|
if (cprm->written + nr > cprm->limit)
|
2013-10-05 19:32:35 +00:00
|
|
|
return 0;
|
2020-10-16 03:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (dump_interrupted())
|
|
|
|
return 0;
|
|
|
|
n = __kernel_write(file, addr, nr, &pos);
|
2024-09-26 18:39:02 +00:00
|
|
|
if (n != nr)
|
2020-10-16 03:12:43 +00:00
|
|
|
return 0;
|
|
|
|
file->f_pos = pos;
|
|
|
|
cprm->written += n;
|
|
|
|
cprm->pos += n;
|
|
|
|
|
2013-10-05 19:32:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:16:37 +00:00
|
|
|
static int __dump_skip(struct coredump_params *cprm, size_t nr)
|
2012-09-27 01:52:08 +00:00
|
|
|
{
|
2013-10-08 13:26:08 +00:00
|
|
|
static char zeroes[PAGE_SIZE];
|
|
|
|
struct file *file = cprm->file;
|
2022-06-29 13:06:59 +00:00
|
|
|
if (file->f_mode & FMODE_LSEEK) {
|
2024-09-26 18:39:02 +00:00
|
|
|
if (dump_interrupted() ||
|
|
|
|
vfs_llseek(file, nr, SEEK_CUR) < 0)
|
2012-09-27 01:52:08 +00:00
|
|
|
return 0;
|
2016-06-05 21:14:14 +00:00
|
|
|
cprm->pos += nr;
|
2013-10-08 13:26:08 +00:00
|
|
|
return 1;
|
2012-09-27 01:52:08 +00:00
|
|
|
} else {
|
2013-10-08 13:26:08 +00:00
|
|
|
while (nr > PAGE_SIZE) {
|
2020-03-08 13:16:37 +00:00
|
|
|
if (!__dump_emit(cprm, zeroes, PAGE_SIZE))
|
2013-10-08 13:26:08 +00:00
|
|
|
return 0;
|
|
|
|
nr -= PAGE_SIZE;
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
2020-03-08 13:16:37 +00:00
|
|
|
return __dump_emit(cprm, zeroes, nr);
|
2012-09-27 01:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 13:16:37 +00:00
|
|
|
|
2022-10-03 09:06:57 +00:00
|
|
|
int dump_emit(struct coredump_params *cprm, const void *addr, int nr)
|
|
|
|
{
|
|
|
|
if (cprm->to_skip) {
|
|
|
|
if (!__dump_skip(cprm, cprm->to_skip))
|
|
|
|
return 0;
|
|
|
|
cprm->to_skip = 0;
|
|
|
|
}
|
|
|
|
return __dump_emit(cprm, addr, nr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dump_emit);
|
|
|
|
|
|
|
|
void dump_skip_to(struct coredump_params *cprm, unsigned long pos)
|
|
|
|
{
|
|
|
|
cprm->to_skip = pos - cprm->pos;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dump_skip_to);
|
|
|
|
|
|
|
|
void dump_skip(struct coredump_params *cprm, size_t nr)
|
|
|
|
{
|
|
|
|
cprm->to_skip += nr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dump_skip);
|
|
|
|
|
|
|
|
#ifdef CONFIG_ELF_CORE
|
2022-09-26 15:59:14 +00:00
|
|
|
static int dump_emit_page(struct coredump_params *cprm, struct page *page)
|
|
|
|
{
|
2023-02-03 15:06:25 +00:00
|
|
|
struct bio_vec bvec;
|
2022-09-26 15:59:14 +00:00
|
|
|
struct iov_iter iter;
|
|
|
|
struct file *file = cprm->file;
|
2022-10-04 00:26:08 +00:00
|
|
|
loff_t pos;
|
2022-09-26 15:59:14 +00:00
|
|
|
ssize_t n;
|
|
|
|
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
if (!page)
|
|
|
|
return 0;
|
|
|
|
|
2022-09-26 15:59:14 +00:00
|
|
|
if (cprm->to_skip) {
|
|
|
|
if (!__dump_skip(cprm, cprm->to_skip))
|
|
|
|
return 0;
|
|
|
|
cprm->to_skip = 0;
|
|
|
|
}
|
|
|
|
if (cprm->written + PAGE_SIZE > cprm->limit)
|
|
|
|
return 0;
|
|
|
|
if (dump_interrupted())
|
|
|
|
return 0;
|
2022-10-04 00:26:08 +00:00
|
|
|
pos = file->f_pos;
|
2023-02-03 15:06:25 +00:00
|
|
|
bvec_set_page(&bvec, page, PAGE_SIZE, 0);
|
2022-09-16 00:25:47 +00:00
|
|
|
iov_iter_bvec(&iter, ITER_SOURCE, &bvec, 1, PAGE_SIZE);
|
2022-09-26 15:59:14 +00:00
|
|
|
n = __kernel_write_iter(cprm->file, &iter, &pos);
|
|
|
|
if (n != PAGE_SIZE)
|
|
|
|
return 0;
|
|
|
|
file->f_pos = pos;
|
|
|
|
cprm->written += PAGE_SIZE;
|
|
|
|
cprm->pos += PAGE_SIZE;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
/*
|
|
|
|
* If we might get machine checks from kernel accesses during the
|
|
|
|
* core dump, let's get those errors early rather than during the
|
|
|
|
* IO. This is not performance-critical enough to warrant having
|
|
|
|
* all the machine check logic in the iovec paths.
|
|
|
|
*/
|
|
|
|
#ifdef copy_mc_to_kernel
|
|
|
|
|
|
|
|
#define dump_page_alloc() alloc_page(GFP_KERNEL)
|
|
|
|
#define dump_page_free(x) __free_page(x)
|
|
|
|
static struct page *dump_page_copy(struct page *src, struct page *dst)
|
|
|
|
{
|
|
|
|
void *buf = kmap_local_page(src);
|
|
|
|
size_t left = copy_mc_to_kernel(page_address(dst), buf, PAGE_SIZE);
|
|
|
|
kunmap_local(buf);
|
|
|
|
return left ? NULL : dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* We just want to return non-NULL; it's never used. */
|
|
|
|
#define dump_page_alloc() ERR_PTR(-EINVAL)
|
|
|
|
#define dump_page_free(x) ((void)(x))
|
|
|
|
static inline struct page *dump_page_copy(struct page *src, struct page *dst)
|
|
|
|
{
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-16 03:12:46 +00:00
|
|
|
int dump_user_range(struct coredump_params *cprm, unsigned long start,
|
|
|
|
unsigned long len)
|
|
|
|
{
|
|
|
|
unsigned long addr;
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
struct page *dump_page;
|
|
|
|
|
|
|
|
dump_page = dump_page_alloc();
|
|
|
|
if (!dump_page)
|
|
|
|
return 0;
|
2020-10-16 03:12:46 +00:00
|
|
|
|
|
|
|
for (addr = start; addr < start + len; addr += PAGE_SIZE) {
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid having to allocate page tables for virtual address
|
|
|
|
* ranges that have never been used yet, and also to make it
|
|
|
|
* easy to generate sparse core files, use a helper that returns
|
|
|
|
* NULL when encountering an empty page table entry that would
|
|
|
|
* otherwise have been filled with the zero page.
|
|
|
|
*/
|
|
|
|
page = get_dump_page(addr);
|
|
|
|
if (page) {
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
int stop = !dump_emit_page(cprm, dump_page_copy(page, dump_page));
|
2020-10-16 03:12:46 +00:00
|
|
|
put_page(page);
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
if (stop) {
|
|
|
|
dump_page_free(dump_page);
|
2020-03-08 13:16:37 +00:00
|
|
|
return 0;
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
}
|
2020-10-16 03:12:46 +00:00
|
|
|
} else {
|
2020-03-08 13:16:37 +00:00
|
|
|
dump_skip(cprm, PAGE_SIZE);
|
2020-10-16 03:12:46 +00:00
|
|
|
}
|
2024-10-10 15:36:51 +00:00
|
|
|
cond_resched();
|
2020-10-16 03:12:46 +00:00
|
|
|
}
|
iov_iter: get rid of 'copy_mc' flag
This flag is only set by one single user: the magical core dumping code
that looks up user pages one by one, and then writes them out using
their kernel addresses (by using a BVEC_ITER).
That actually ends up being a huge problem, because while we do use
copy_mc_to_kernel() for this case and it is able to handle the possible
machine checks involved, nothing else is really ready to handle the
failures caused by the machine check.
In particular, as reported by Tong Tiangen, we don't actually support
fault_in_iov_iter_readable() on a machine check area.
As a result, the usual logic for writing things to a file under a
filesystem lock, which involves doing a copy with page faults disabled
and then if that fails trying to fault pages in without holding the
locks with fault_in_iov_iter_readable() does not work at all.
We could decide to always just make the MC copy "succeed" (and filling
the destination with zeroes), and that would then create a core dump
file that just ignores any machine checks.
But honestly, this single special case has been problematic before, and
means that all the normal iov_iter code ends up slightly more complex
and slower.
See for example commit c9eec08bac96 ("iov_iter: Don't deal with
iter->copy_mc in memcpy_from_iter_mc()") where David Howells
re-organized the code just to avoid having to check the 'copy_mc' flags
inside the inner iov_iter loops.
So considering that we have exactly one user, and that one user is a
non-critical special case that doesn't actually ever trigger in real
life (Tong found this with manual error injection), the sane solution is
to just decide that the onus on handling the machine check lines on that
user instead.
Ergo, do the copy_mc_to_kernel() in the core dump logic itself, copying
the user data to a stable kernel page before writing it out.
Fixes: f1982740f5e7 ("iov_iter: Convert iterate*() to inline funcs")
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Link: https://lore.kernel.org/r/20240305133336.3804360-1-tongtiangen@huawei.com
Link: https://lore.kernel.org/all/4e80924d-9c85-f13a-722a-6a5d2b1c225a@huawei.com/
Tested-by: David Howells <dhowells@redhat.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reported-by: Tong Tiangen <tongtiangen@huawei.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-03-05 13:33:36 +00:00
|
|
|
dump_page_free(dump_page);
|
2020-10-16 03:12:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-08 15:05:01 +00:00
|
|
|
int dump_align(struct coredump_params *cprm, int align)
|
|
|
|
{
|
2020-03-08 13:16:37 +00:00
|
|
|
unsigned mod = (cprm->pos + cprm->to_skip) & (align - 1);
|
2013-10-08 15:05:01 +00:00
|
|
|
if (align & (align - 1))
|
2013-11-16 02:55:52 +00:00
|
|
|
return 0;
|
2020-03-08 13:16:37 +00:00
|
|
|
if (mod)
|
|
|
|
cprm->to_skip += align - mod;
|
|
|
|
return 1;
|
2013-10-08 15:05:01 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dump_align);
|
2017-01-11 19:25:00 +00:00
|
|
|
|
2022-01-22 06:13:38 +00:00
|
|
|
#ifdef CONFIG_SYSCTL
|
|
|
|
|
|
|
|
void validate_coredump_safety(void)
|
|
|
|
{
|
|
|
|
if (suid_dumpable == SUID_DUMP_ROOT &&
|
|
|
|
core_pattern[0] != '/' && core_pattern[0] != '|') {
|
2024-07-18 18:27:24 +00:00
|
|
|
|
|
|
|
coredump_report_failure("Unsafe core_pattern used with fs.suid_dumpable=2: "
|
|
|
|
"pipe handler or fully qualified core dump path required. "
|
|
|
|
"Set kernel.core_pattern before fs.suid_dumpable.");
|
2022-01-22 06:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
sysctl: treewide: constify the ctl_table argument of proc_handlers
const qualify the struct ctl_table argument in the proc_handler function
signatures. This is a prerequisite to moving the static ctl_table
structs into .rodata data which will ensure that proc_handler function
pointers cannot be modified.
This patch has been generated by the following coccinelle script:
```
virtual patch
@r1@
identifier ctl, write, buffer, lenp, ppos;
identifier func !~ "appldata_(timer|interval)_handler|sched_(rt|rr)_handler|rds_tcp_skbuf_handler|proc_sctp_do_(hmac_alg|rto_min|rto_max|udp_port|alpha_beta|auth|probe_interval)";
@@
int func(
- struct ctl_table *ctl
+ const struct ctl_table *ctl
,int write, void *buffer, size_t *lenp, loff_t *ppos);
@r2@
identifier func, ctl, write, buffer, lenp, ppos;
@@
int func(
- struct ctl_table *ctl
+ const struct ctl_table *ctl
,int write, void *buffer, size_t *lenp, loff_t *ppos)
{ ... }
@r3@
identifier func;
@@
int func(
- struct ctl_table *
+ const struct ctl_table *
,int , void *, size_t *, loff_t *);
@r4@
identifier func, ctl;
@@
int func(
- struct ctl_table *ctl
+ const struct ctl_table *ctl
,int , void *, size_t *, loff_t *);
@r5@
identifier func, write, buffer, lenp, ppos;
@@
int func(
- struct ctl_table *
+ const struct ctl_table *
,int write, void *buffer, size_t *lenp, loff_t *ppos);
```
* Code formatting was adjusted in xfs_sysctl.c to comply with code
conventions. The xfs_stats_clear_proc_handler,
xfs_panic_mask_proc_handler and xfs_deprecated_dointvec_minmax where
adjusted.
* The ctl_table argument in proc_watchdog_common was const qualified.
This is called from a proc_handler itself and is calling back into
another proc_handler, making it necessary to change it as part of the
proc_handler migration.
Co-developed-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Co-developed-by: Joel Granados <j.granados@samsung.com>
Signed-off-by: Joel Granados <j.granados@samsung.com>
2024-07-24 18:59:29 +00:00
|
|
|
static int proc_dostring_coredump(const struct ctl_table *table, int write,
|
2022-01-22 06:13:38 +00:00
|
|
|
void *buffer, size_t *lenp, loff_t *ppos)
|
|
|
|
{
|
|
|
|
int error = proc_dostring(table, write, buffer, lenp, ppos);
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
validate_coredump_safety();
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2024-05-06 19:37:00 +00:00
|
|
|
static const unsigned int core_file_note_size_min = CORE_FILE_NOTE_SIZE_DEFAULT;
|
|
|
|
static const unsigned int core_file_note_size_max = CORE_FILE_NOTE_SIZE_MAX;
|
|
|
|
|
2022-01-22 06:13:38 +00:00
|
|
|
static struct ctl_table coredump_sysctls[] = {
|
|
|
|
{
|
|
|
|
.procname = "core_uses_pid",
|
|
|
|
.data = &core_uses_pid,
|
|
|
|
.maxlen = sizeof(int),
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_dointvec,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.procname = "core_pattern",
|
|
|
|
.data = core_pattern,
|
|
|
|
.maxlen = CORENAME_MAX_SIZE,
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_dostring_coredump,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.procname = "core_pipe_limit",
|
|
|
|
.data = &core_pipe_limit,
|
|
|
|
.maxlen = sizeof(unsigned int),
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_dointvec,
|
|
|
|
},
|
2024-05-06 19:37:00 +00:00
|
|
|
{
|
|
|
|
.procname = "core_file_note_size_limit",
|
|
|
|
.data = &core_file_note_size_limit,
|
|
|
|
.maxlen = sizeof(unsigned int),
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_douintvec_minmax,
|
|
|
|
.extra1 = (unsigned int *)&core_file_note_size_min,
|
|
|
|
.extra2 = (unsigned int *)&core_file_note_size_max,
|
|
|
|
},
|
2022-01-22 06:13:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init init_fs_coredump_sysctls(void)
|
|
|
|
{
|
|
|
|
register_sysctl_init("kernel", coredump_sysctls);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fs_initcall(init_fs_coredump_sysctls);
|
|
|
|
#endif /* CONFIG_SYSCTL */
|
|
|
|
|
2020-10-16 03:12:50 +00:00
|
|
|
/*
|
|
|
|
* The purpose of always_dump_vma() is to make sure that special kernel mappings
|
|
|
|
* that are useful for post-mortem analysis are included in every core dump.
|
|
|
|
* In that way we ensure that the core dump is fully interpretable later
|
|
|
|
* without matching up the same kernel and hardware config to see what PC values
|
|
|
|
* meant. These special mappings include - vDSO, vsyscall, and other
|
|
|
|
* architecture specific mappings
|
|
|
|
*/
|
|
|
|
static bool always_dump_vma(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
/* Any vsyscall mappings? */
|
|
|
|
if (vma == get_gate_vma(vma->vm_mm))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume that all vmas with a .name op should always be dumped.
|
|
|
|
* If this changes, a new vm_ops field can easily be added.
|
|
|
|
*/
|
|
|
|
if (vma->vm_ops && vma->vm_ops->name && vma->vm_ops->name(vma))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* arch_vma_name() returns non-NULL for special architecture mappings,
|
|
|
|
* such as vDSO sections.
|
|
|
|
*/
|
|
|
|
if (arch_vma_name(vma))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-26 02:57:39 +00:00
|
|
|
#define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1
|
|
|
|
|
2020-10-16 03:12:50 +00:00
|
|
|
/*
|
|
|
|
* Decide how much of @vma's contents should be included in a core dump.
|
|
|
|
*/
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
static unsigned long vma_dump_size(struct vm_area_struct *vma,
|
|
|
|
unsigned long mm_flags)
|
2020-10-16 03:12:50 +00:00
|
|
|
{
|
|
|
|
#define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
|
|
|
|
|
|
|
|
/* always dump the vdso and vsyscall sections */
|
|
|
|
if (always_dump_vma(vma))
|
|
|
|
goto whole;
|
|
|
|
|
|
|
|
if (vma->vm_flags & VM_DONTDUMP)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* support for DAX */
|
|
|
|
if (vma_is_dax(vma)) {
|
|
|
|
if ((vma->vm_flags & VM_SHARED) && FILTER(DAX_SHARED))
|
|
|
|
goto whole;
|
|
|
|
if (!(vma->vm_flags & VM_SHARED) && FILTER(DAX_PRIVATE))
|
|
|
|
goto whole;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hugetlb memory check */
|
|
|
|
if (is_vm_hugetlb_page(vma)) {
|
|
|
|
if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
|
|
|
|
goto whole;
|
|
|
|
if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
|
|
|
|
goto whole;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do not dump I/O mapped devices or special mappings */
|
|
|
|
if (vma->vm_flags & VM_IO)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* By default, dump shared memory if mapped from an anonymous file. */
|
|
|
|
if (vma->vm_flags & VM_SHARED) {
|
|
|
|
if (file_inode(vma->vm_file)->i_nlink == 0 ?
|
|
|
|
FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
|
|
|
|
goto whole;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump segments that have been written to. */
|
|
|
|
if ((!IS_ENABLED(CONFIG_MMU) || vma->anon_vma) && FILTER(ANON_PRIVATE))
|
|
|
|
goto whole;
|
|
|
|
if (vma->vm_file == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (FILTER(MAPPED_PRIVATE))
|
|
|
|
goto whole;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is the beginning of an executable file mapping,
|
|
|
|
* dump the first page to aid in determining what was mapped here.
|
|
|
|
*/
|
|
|
|
if (FILTER(ELF_HEADERS) &&
|
2022-01-26 02:57:39 +00:00
|
|
|
vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
|
|
|
|
if ((READ_ONCE(file_inode(vma->vm_file)->i_mode) & 0111) != 0)
|
|
|
|
return PAGE_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ELF libraries aren't always executable.
|
|
|
|
* We'll want to check whether the mapping starts with the ELF
|
|
|
|
* magic, but not now - we're holding the mmap lock,
|
|
|
|
* so copy_from_user() doesn't work here.
|
|
|
|
* Use a placeholder instead, and fix it up later in
|
|
|
|
* dump_vma_snapshot().
|
|
|
|
*/
|
|
|
|
return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER;
|
|
|
|
}
|
2020-10-16 03:12:50 +00:00
|
|
|
|
|
|
|
#undef FILTER
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
whole:
|
|
|
|
return vma->vm_end - vma->vm_start;
|
|
|
|
}
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function for iterating across a vma list. It ensures that the caller
|
|
|
|
* will visit `gate_vma' prior to terminating the search.
|
|
|
|
*/
|
2023-01-20 16:26:20 +00:00
|
|
|
static struct vm_area_struct *coredump_next_vma(struct vma_iterator *vmi,
|
2022-09-06 19:48:56 +00:00
|
|
|
struct vm_area_struct *vma,
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
struct vm_area_struct *gate_vma)
|
|
|
|
{
|
2022-09-06 19:48:56 +00:00
|
|
|
if (gate_vma && (vma == gate_vma))
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
return NULL;
|
2022-09-06 19:48:56 +00:00
|
|
|
|
2023-01-20 16:26:20 +00:00
|
|
|
vma = vma_next(vmi);
|
2022-09-06 19:48:56 +00:00
|
|
|
if (vma)
|
|
|
|
return vma;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
return gate_vma;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:04:19 +00:00
|
|
|
static void free_vma_snapshot(struct coredump_params *cprm)
|
|
|
|
{
|
|
|
|
if (cprm->vma_meta) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cprm->vma_count; i++) {
|
|
|
|
struct file *file = cprm->vma_meta[i].file;
|
|
|
|
if (file)
|
|
|
|
fput(file);
|
|
|
|
}
|
|
|
|
kvfree(cprm->vma_meta);
|
|
|
|
cprm->vma_meta = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-06 18:16:02 +00:00
|
|
|
static int cmp_vma_size(const void *vma_meta_lhs_ptr, const void *vma_meta_rhs_ptr)
|
|
|
|
{
|
|
|
|
const struct core_vma_metadata *vma_meta_lhs = vma_meta_lhs_ptr;
|
|
|
|
const struct core_vma_metadata *vma_meta_rhs = vma_meta_rhs_ptr;
|
|
|
|
|
|
|
|
if (vma_meta_lhs->dump_size < vma_meta_rhs->dump_size)
|
|
|
|
return -1;
|
|
|
|
if (vma_meta_lhs->dump_size > vma_meta_rhs->dump_size)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
/*
|
|
|
|
* Under the mmap_lock, take a snapshot of relevant information about the task's
|
|
|
|
* VMAs.
|
|
|
|
*/
|
2022-03-08 18:55:29 +00:00
|
|
|
static bool dump_vma_snapshot(struct coredump_params *cprm)
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
{
|
2022-09-06 19:48:56 +00:00
|
|
|
struct vm_area_struct *gate_vma, *vma = NULL;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
struct mm_struct *mm = current->mm;
|
2023-01-20 16:26:20 +00:00
|
|
|
VMA_ITERATOR(vmi, mm, 0);
|
2022-09-06 19:48:56 +00:00
|
|
|
int i = 0;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Once the stack expansion code is fixed to not change VMA bounds
|
|
|
|
* under mmap_lock in read mode, this can be changed to take the
|
|
|
|
* mmap_lock in read mode.
|
|
|
|
*/
|
|
|
|
if (mmap_write_lock_killable(mm))
|
2022-03-08 18:55:29 +00:00
|
|
|
return false;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
|
2022-03-08 18:55:29 +00:00
|
|
|
cprm->vma_data_size = 0;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
gate_vma = get_gate_vma(mm);
|
2022-03-08 18:55:29 +00:00
|
|
|
cprm->vma_count = mm->map_count + (gate_vma ? 1 : 0);
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
|
2022-03-08 18:55:29 +00:00
|
|
|
cprm->vma_meta = kvmalloc_array(cprm->vma_count, sizeof(*cprm->vma_meta), GFP_KERNEL);
|
|
|
|
if (!cprm->vma_meta) {
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
mmap_write_unlock(mm);
|
2022-03-08 18:55:29 +00:00
|
|
|
return false;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 16:26:20 +00:00
|
|
|
while ((vma = coredump_next_vma(&vmi, vma, gate_vma)) != NULL) {
|
2022-03-08 18:55:29 +00:00
|
|
|
struct core_vma_metadata *m = cprm->vma_meta + i;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
|
|
|
|
m->start = vma->vm_start;
|
|
|
|
m->end = vma->vm_end;
|
|
|
|
m->flags = vma->vm_flags;
|
|
|
|
m->dump_size = vma_dump_size(vma, cprm->mm_flags);
|
2022-03-08 19:04:19 +00:00
|
|
|
m->pgoff = vma->vm_pgoff;
|
|
|
|
m->file = vma->vm_file;
|
|
|
|
if (m->file)
|
|
|
|
get_file(m->file);
|
2022-09-06 19:48:56 +00:00
|
|
|
i++;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mmap_write_unlock(mm);
|
|
|
|
|
2022-03-08 18:55:29 +00:00
|
|
|
for (i = 0; i < cprm->vma_count; i++) {
|
|
|
|
struct core_vma_metadata *m = cprm->vma_meta + i;
|
2022-01-26 02:57:39 +00:00
|
|
|
|
|
|
|
if (m->dump_size == DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER) {
|
|
|
|
char elfmag[SELFMAG];
|
|
|
|
|
|
|
|
if (copy_from_user(elfmag, (void __user *)m->start, SELFMAG) ||
|
|
|
|
memcmp(elfmag, ELFMAG, SELFMAG) != 0) {
|
|
|
|
m->dump_size = 0;
|
|
|
|
} else {
|
|
|
|
m->dump_size = PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:55:29 +00:00
|
|
|
cprm->vma_data_size += m->dump_size;
|
2022-01-26 02:57:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 18:16:02 +00:00
|
|
|
sort(cprm->vma_meta, cprm->vma_count, sizeof(*cprm->vma_meta),
|
|
|
|
cmp_vma_size, NULL);
|
|
|
|
|
2022-03-08 18:55:29 +00:00
|
|
|
return true;
|
binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot
In both binfmt_elf and binfmt_elf_fdpic, use a new helper
dump_vma_snapshot() to take a snapshot of the VMA list (including the gate
VMA, if we have one) while protected by the mmap_lock, and then use that
snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_lock held across the
entire core dumping operation; however, keeping the mmap_lock locked while
we may be blocked for an unbounded amount of time (e.g. because we're
dumping to a FUSE filesystem or so) isn't really optimal; the mmap_lock
blocks things like the ->release handler of userfaultfd, and we don't
really want critical system daemons to grind to a halt just because
someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or
something like that.
Since both the normal ELF code and the FDPIC ELF code need this
functionality (and if any other binfmt wants to add coredump support in
the future, they'd probably need it, too), implement this with a common
helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel
memory per userspace VMA in the normal ELF case, and that we need O(n)
kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't
be terribly bad.
There currently is a data race between stack expansion and anything that
reads ->vm_start or ->vm_end under the mmap_lock held in read mode; to
mitigate that for core dumping, take the mmap_lock in write mode when
taking a snapshot of the VMA hierarchy. (If we only took the mmap_lock in
read mode, we could end up with a corrupted core dump if someone does
get_user_pages_remote() concurrently. Not really a major problem, but
taking the mmap_lock either way works here, so we might as well avoid the
issue.) (This doesn't do anything about the existing data races with stack
expansion in other mm code.)
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-6-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-10-16 03:12:54 +00:00
|
|
|
}
|