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 17:07:57 +03:00
// SPDX-License-Identifier: GPL-2.0
2012-09-27 05:52:08 +04:00
# include <linux/slab.h>
# include <linux/file.h>
# include <linux/fdtable.h>
2016-11-10 21:46:38 +03:00
# include <linux/freezer.h>
2012-09-27 05:52:08 +04:00
# include <linux/mm.h>
# include <linux/stat.h>
# include <linux/fcntl.h>
# include <linux/swap.h>
2019-08-03 07:49:05 +03:00
# include <linux/ctype.h>
2012-09-27 05:52:08 +04: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 04:15:24 +04:00
# include <linux/coredump.h>
2017-02-08 20:51:30 +03:00
# include <linux/sched/coredump.h>
2017-02-08 20:51:30 +03:00
# include <linux/sched/signal.h>
2017-02-08 20:51:37 +03:00
# include <linux/sched/task_stack.h>
2012-09-27 05:52:08 +04: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/tracehook.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-23 00:25:36 +03:00
# include <linux/fs.h>
# include <linux/path.h>
2015-11-25 18:22:25 +03:00
# include <linux/timekeeping.h>
2012-09-27 05:52:08 +04:00
2016-12-24 22:46:01 +03:00
# include <linux/uaccess.h>
2012-09-27 05:52:08 +04: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>
int core_uses_pid ;
unsigned int core_pipe_limit ;
2013-07-04 02:08:22 +04:00
char core_pattern [ CORENAME_MAX_SIZE ] = " core " ;
static int core_name_size = CORENAME_MAX_SIZE ;
2012-09-27 05:52:08 +04:00
struct core_name {
char * corename ;
int used , size ;
} ;
/* The maximal length of core_pattern is also specified in sysctl.c */
2013-07-04 02:08:22 +04:00
static int expand_corename ( struct core_name * cn , int size )
2012-09-27 05:52:08 +04:00
{
2013-07-04 02:08:16 +04:00
char * corename = krealloc ( cn - > corename , size , GFP_KERNEL ) ;
2012-09-27 05:52:08 +04:00
2013-07-04 02:08:16 +04:00
if ( ! corename )
2012-09-27 05:52:08 +04:00
return - ENOMEM ;
2013-07-04 02:08:22 +04:00
if ( size > core_name_size ) /* racy but harmless */
core_name_size = size ;
cn - > size = ksize ( corename ) ;
2013-07-04 02:08:16 +04:00
cn - > corename = corename ;
2012-09-27 05:52:08 +04:00
return 0 ;
}
2015-06-26 01:03:53 +03:00
static __printf ( 2 , 0 ) int cn_vprintf ( struct core_name * cn , const char * fmt ,
va_list arg )
2012-09-27 05:52:08 +04:00
{
2013-07-04 02:08:19 +04:00
int free , need ;
2014-04-19 21:15:07 +04:00
va_list arg_copy ;
2012-09-27 05:52:08 +04:00
2013-07-04 02:08:19 +04:00
again :
free = cn - > size - cn - > used ;
2014-04-19 21:15:07 +04:00
va_copy ( arg_copy , arg ) ;
need = vsnprintf ( cn - > corename + cn - > used , free , fmt , arg_copy ) ;
va_end ( arg_copy ) ;
2013-07-04 02:08:19 +04:00
if ( need < free ) {
cn - > used + = need ;
return 0 ;
}
2012-09-27 05:52:08 +04:00
2013-07-04 02:08:22 +04:00
if ( ! expand_corename ( cn , cn - > size + need - free + 1 ) )
2013-07-04 02:08:19 +04:00
goto again ;
2012-09-27 05:52:08 +04:00
2013-07-04 02:08:19 +04:00
return - ENOMEM ;
2012-09-27 05:52:08 +04:00
}
2015-06-26 01:03:53 +03:00
static __printf ( 2 , 3 ) int cn_printf ( struct core_name * cn , const char * fmt , . . . )
2013-07-04 02:08:17 +04:00
{
va_list arg ;
int ret ;
va_start ( arg , fmt ) ;
ret = cn_vprintf ( cn , fmt , arg ) ;
va_end ( arg ) ;
return ret ;
}
2015-06-26 01:03:53 +03:00
static __printf ( 2 , 3 )
int cn_esc_printf ( struct core_name * cn , const char * fmt , . . . )
2012-09-27 05:52:08 +04:00
{
2013-07-04 02:08:20 +04: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-21 02:00:08 +03: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-04 02:08:20 +04:00
for ( ; cur < cn - > used ; + + cur ) {
if ( cn - > corename [ cur ] = = ' / ' )
cn - > corename [ cur ] = ' ! ' ;
}
return ret ;
2012-09-27 05:52:08 +04:00
}
static int cn_print_exe_file ( struct core_name * cn )
{
struct file * exe_file ;
char * pathbuf , * path ;
int ret ;
exe_file = get_mm_exe_file ( current - > mm ) ;
2013-07-04 02:08:20 +04:00
if ( ! exe_file )
return cn_esc_printf ( cn , " %s (path unknown) " , current->comm) ;
2012-09-27 05:52:08 +04:00
2017-09-14 02:28:29 +03:00
pathbuf = kmalloc ( PATH_MAX , GFP_KERNEL ) ;
2012-09-27 05:52:08 +04:00
if ( ! pathbuf ) {
ret = - ENOMEM ;
goto put_exe_file ;
}
2015-06-19 11:29:13 +03:00
path = file_path ( exe_file , pathbuf , PATH_MAX ) ;
2012-09-27 05:52:08 +04:00
if ( IS_ERR ( path ) ) {
ret = PTR_ERR ( path ) ;
goto free_buf ;
}
2013-07-04 02:08:20 +04:00
ret = cn_esc_printf ( cn , " %s " , path ) ;
2012-09-27 05:52:08 +04: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 07:49:05 +03:00
static int format_corename ( struct core_name * cn , struct coredump_params * cprm ,
size_t * * argv , int * argc )
2012-09-27 05:52:08 +04:00
{
const struct cred * cred = current_cred ( ) ;
const char * pat_ptr = core_pattern ;
int ispipe = ( * pat_ptr = = ' | ' ) ;
2019-08-03 07:49:05 +03:00
bool was_space = false ;
2012-09-27 05:52:08 +04:00
int pid_in_pattern = 0 ;
int err = 0 ;
2013-07-04 02:08:16 +04:00
cn - > used = 0 ;
2013-07-04 02:08:22 +04:00
cn - > corename = NULL ;
if ( expand_corename ( cn , core_name_size ) )
2012-09-27 05:52:08 +04:00
return - ENOMEM ;
2013-07-04 02:08:23 +04:00
cn - > corename [ 0 ] = ' \0 ' ;
2019-08-03 07:49:05 +03: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-04 02:08:23 +04:00
+ + pat_ptr ;
2019-08-03 07:49:05 +03:00
}
2012-09-27 05:52:08 +04:00
/* Repeat as long as we have more pattern to process and more output
space */
while ( * pat_ptr ) {
2019-08-03 07:49:05 +03: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 ) ) {
was_space = true ;
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 05:52:08 +04: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-12 01:24:32 +04:00
/* global pid */
case ' P ' :
err = cn_printf ( cn , " %d " ,
task_tgid_nr ( current ) ) ;
break ;
2014-10-14 02:53:35 +04: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 05:52:08 +04: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-26 01:03:51 +03:00
err = cn_printf ( cn , " %u " ,
from_kuid ( & init_user_ns ,
cred - > uid ) ) ;
2012-09-27 05:52:08 +04: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-26 01:03:51 +03:00
err = cn_printf ( cn , " %u " ,
from_kgid ( & init_user_ns ,
cred - > gid ) ) ;
2012-09-27 05:52:08 +04:00
break ;
2012-10-05 04:15:25 +04:00
case ' d ' :
err = cn_printf ( cn , " %d " ,
__get_dumpable ( cprm - > mm_flags ) ) ;
break ;
2012-09-27 05:52:08 +04:00
/* signal that caused the coredump */
case ' s ' :
2015-06-26 01:03:53 +03:00
err = cn_printf ( cn , " %d " ,
cprm - > siginfo - > si_signo ) ;
2012-09-27 05:52:08 +04:00
break ;
/* UNIX time of coredump */
case ' t ' : {
2015-11-25 18:22:25 +03:00
time64_t time ;
time = ktime_get_real_seconds ( ) ;
err = cn_printf ( cn , " %lld " , time ) ;
2012-09-27 05:52:08 +04:00
break ;
}
/* hostname */
2013-07-04 02:08:20 +04:00
case ' h ' :
2012-09-27 05:52:08 +04:00
down_read ( & uts_sem ) ;
2013-07-04 02:08:20 +04:00
err = cn_esc_printf ( cn , " %s " ,
2012-09-27 05:52:08 +04:00
utsname ( ) - > nodename ) ;
up_read ( & uts_sem ) ;
break ;
/* executable */
2013-07-04 02:08:20 +04:00
case ' e ' :
err = cn_esc_printf ( cn , " %s " , current - > comm ) ;
2012-09-27 05:52:08 +04:00
break ;
case ' E ' :
err = cn_print_exe_file ( cn ) ;
break ;
/* core limit size */
case ' c ' :
err = cn_printf ( cn , " %lu " ,
rlimit ( RLIMIT_CORE ) ) ;
break ;
default :
break ;
}
+ + pat_ptr ;
}
if ( err )
return err ;
}
2013-07-04 02:08:23 +04:00
out :
2012-09-27 05:52:08 +04: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 ;
}
2015-11-07 03:32:31 +03:00
static int zap_process ( struct task_struct * start , int exit_code , int flags )
2012-09-27 05:52:08 +04:00
{
struct task_struct * t ;
int nr = 0 ;
2015-11-07 03:32:31 +03:00
/* ignore all signals except SIGKILL, see prepare_signal() */
start - > signal - > flags = SIGNAL_GROUP_COREDUMP | flags ;
2012-09-27 05:52:08 +04:00
start - > signal - > group_exit_code = exit_code ;
start - > signal - > group_stop_count = 0 ;
2015-11-07 03:32:34 +03:00
for_each_thread ( start , t ) {
2012-09-27 05:52:08 +04:00
task_clear_jobctl_pending ( t , JOBCTL_PENDING_MASK ) ;
if ( t ! = current & & t - > mm ) {
sigaddset ( & t - > pending . signal , SIGKILL ) ;
signal_wake_up ( t , 1 ) ;
nr + + ;
}
2015-11-07 03:32:34 +03:00
}
2012-09-27 05:52:08 +04:00
return nr ;
}
2013-05-01 02:28:10 +04:00
static int zap_threads ( struct task_struct * tsk , struct mm_struct * mm ,
struct core_state * core_state , int exit_code )
2012-09-27 05:52:08 +04:00
{
struct task_struct * g , * p ;
unsigned long flags ;
int nr = - EAGAIN ;
spin_lock_irq ( & tsk - > sighand - > siglock ) ;
if ( ! signal_group_exit ( tsk - > signal ) ) {
mm - > core_state = core_state ;
2013-05-01 02:28:12 +04:00
tsk - > signal - > group_exit_task = tsk ;
2015-11-07 03:32:31 +03:00
nr = zap_process ( tsk , exit_code , 0 ) ;
2013-05-01 02:28:10 +04:00
clear_tsk_thread_flag ( tsk , TIF_SIGPENDING ) ;
2012-09-27 05:52:08 +04:00
}
spin_unlock_irq ( & tsk - > sighand - > siglock ) ;
if ( unlikely ( nr < 0 ) )
return nr ;
2014-07-24 00:59:59 +04:00
tsk - > flags | = PF_DUMPCORE ;
2012-09-27 05:52:08 +04:00
if ( atomic_read ( & mm - > mm_users ) = = nr + 1 )
goto done ;
/*
* We should find and kill all tasks which use this mm , and we should
* count them correctly into - > nr_threads . We don ' t take tasklist
* lock , but this is safe wrt :
*
* fork :
* None of sub - threads can fork after zap_process ( leader ) . All
* processes which were created before this point should be
* visible to zap_threads ( ) because copy_process ( ) adds the new
* process to the tail of init_task . tasks list , and lock / unlock
* of - > siglock provides a memory barrier .
*
* do_exit :
* The caller holds mm - > mmap_sem . This means that the task which
* uses this mm can ' t pass exit_mm ( ) , so it can ' t exit or clear
* its - > mm .
*
* de_thread :
* It does list_replace_rcu ( & leader - > tasks , & current - > tasks ) ,
* we must see either old or new leader , this does not matter .
* However , it can change p - > sighand , so lock_task_sighand ( p )
* must be used . Since p - > mm ! = NULL and we hold - > mmap_sem
* it can ' t fail .
*
* Note also that " g " can be the old leader with - > mm = = NULL
* and already unhashed and thus removed from - > thread_group .
* This is OK , __unhash_process ( ) - > list_del_rcu ( ) does not
* clear the - > next pointer , we will find the new leader via
* next_thread ( ) .
*/
rcu_read_lock ( ) ;
for_each_process ( g ) {
if ( g = = tsk - > group_leader )
continue ;
if ( g - > flags & PF_KTHREAD )
continue ;
2015-11-07 03:32:34 +03:00
for_each_thread ( g , p ) {
if ( unlikely ( ! p - > mm ) )
continue ;
if ( unlikely ( p - > mm = = mm ) ) {
lock_task_sighand ( p , & flags ) ;
nr + = zap_process ( p , exit_code ,
SIGNAL_GROUP_EXIT ) ;
unlock_task_sighand ( p , & flags ) ;
2012-09-27 05:52:08 +04:00
}
2015-11-07 03:32:34 +03:00
break ;
}
2012-09-27 05:52:08 +04:00
}
rcu_read_unlock ( ) ;
done :
atomic_set ( & core_state - > nr_threads , nr ) ;
return nr ;
}
static int coredump_wait ( int exit_code , struct core_state * core_state )
{
struct task_struct * tsk = current ;
struct mm_struct * mm = tsk - > mm ;
int core_waiters = - EBUSY ;
init_completion ( & core_state - > startup ) ;
core_state - > dumper . task = tsk ;
core_state - > dumper . next = NULL ;
2016-05-24 02:25:57 +03:00
if ( down_write_killable ( & mm - > mmap_sem ) )
return - EINTR ;
2012-09-27 05:52:08 +04:00
if ( ! mm - > core_state )
core_waiters = zap_threads ( tsk , mm , core_state , exit_code ) ;
up_write ( & mm - > mmap_sem ) ;
if ( core_waiters > 0 ) {
struct core_thread * ptr ;
2016-11-10 21:46:38 +03:00
freezer_do_not_count ( ) ;
2012-09-27 05:52:08 +04:00
wait_for_completion ( & core_state - > startup ) ;
2016-11-10 21:46:38 +03:00
freezer_count ( ) ;
2012-09-27 05:52:08 +04: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 ) {
wait_task_inactive ( ptr - > task , 0 ) ;
ptr = ptr - > next ;
}
}
return core_waiters ;
}
2013-05-01 02:28:13 +04:00
static void coredump_finish ( struct mm_struct * mm , bool core_dumped )
2012-09-27 05:52:08 +04:00
{
struct core_thread * curr , * next ;
struct task_struct * task ;
2013-05-01 02:28:12 +04:00
spin_lock_irq ( & current - > sighand - > siglock ) ;
2013-05-01 02:28:13 +04:00
if ( core_dumped & & ! __fatal_signal_pending ( current ) )
current - > signal - > group_exit_code | = 0x80 ;
2013-05-01 02:28:12 +04:00
current - > signal - > group_exit_task = NULL ;
current - > signal - > flags = SIGNAL_GROUP_EXIT ;
spin_unlock_irq ( & current - > sighand - > siglock ) ;
2012-09-27 05:52:08 +04:00
next = mm - > core_state - > dumper . next ;
while ( ( curr = next ) ! = NULL ) {
next = curr - > next ;
task = curr - > task ;
/*
* see exit_mm ( ) , curr - > task must not see
* - > task = = NULL before we read - > next .
*/
smp_mb ( ) ;
curr - > task = NULL ;
wake_up_process ( task ) ;
}
mm - > core_state = NULL ;
}
2013-05-01 02:28:15 +04: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 .
*/
return signal_pending ( current ) ;
}
2012-09-27 05:52:08 +04:00
static void wait_for_dump_helpers ( struct file * file )
{
2013-03-21 19:16:56 +04:00
struct pipe_inode_info * pipe = file - > private_data ;
2012-09-27 05:52:08 +04:00
pipe_lock ( pipe ) ;
pipe - > readers + + ;
pipe - > writers - - ;
2013-05-01 02:28:17 +04:00
wake_up_interruptible_sync ( & pipe - > wait ) ;
kill_fasync ( & pipe - > fasync_readers , SIGIO , POLL_IN ) ;
pipe_unlock ( pipe ) ;
2012-09-27 05:52:08 +04:00
2013-05-01 02:28:17 +04:00
/*
* We actually want wait_event_freezable ( ) but then we need
* to clear TIF_SIGPENDING and improve dump_interrupted ( ) .
*/
wait_event_interruptible ( pipe - > wait , pipe - > readers = = 1 ) ;
2012-09-27 05:52:08 +04:00
2013-05-01 02:28:17 +04:00
pipe_lock ( pipe ) ;
2012-09-27 05:52:08 +04: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 21:30:07 +04:00
err = replace_fd ( 0 , files [ 0 ] , 0 ) ;
fput ( files [ 0 ] ) ;
2012-09-27 05:52:08 +04:00
/* and disallow core files too */
current - > signal - > rlim [ RLIMIT_CORE ] = ( struct rlimit ) { 1 , 1 } ;
2012-10-16 21:30:07 +04:00
return err ;
2012-09-27 05:52:08 +04:00
}
2018-09-25 12:27:20 +03:00
void do_coredump ( const kernel_siginfo_t * siginfo )
2012-09-27 05:52:08 +04: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 ;
int retval = 0 ;
int ispipe ;
2019-08-03 07:49:05 +03:00
size_t * argv = NULL ;
int argc = 0 ;
2012-09-27 05:52:08 +04:00
struct files_struct * displaced ;
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-10 01:38:28 +03:00
/* require nonrelative corefile path and be extra careful */
bool need_suid_safe = false ;
2013-05-01 02:28:13 +04:00
bool core_dumped = false ;
2012-09-27 05:52:08 +04:00
static atomic_t core_dump_count = ATOMIC_INIT ( 0 ) ;
struct coredump_params cprm = {
2012-10-05 04:15:29 +04:00
. siginfo = siginfo ,
2012-11-05 22:11:26 +04:00
. regs = signal_pt_regs ( ) ,
2012-09-27 05:52:08 +04: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 ,
} ;
2012-10-05 04:15:29 +04:00
audit_core_dumps ( siginfo - > si_signo ) ;
2012-09-27 05:52:08 +04:00
binfmt = mm - > binfmt ;
if ( ! binfmt | | ! binfmt - > core_dump )
goto fail ;
if ( ! __get_dumpable ( cprm . mm_flags ) )
goto fail ;
cred = prepare_creds ( ) ;
if ( ! cred )
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 05:03:15 +04:00
if ( __get_dumpable ( cprm . mm_flags ) = = SUID_DUMP_ROOT ) {
2012-09-27 05:52:08 +04: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-10 01:38:28 +03:00
need_suid_safe = true ;
2012-09-27 05:52:08 +04:00
}
2012-10-05 04:15:29 +04:00
retval = coredump_wait ( siginfo - > si_signo , & core_state ) ;
2012-09-27 05:52:08 +04:00
if ( retval < 0 )
goto fail_creds ;
old_cred = override_creds ( cred ) ;
2019-08-03 07:49:05 +03:00
ispipe = format_corename ( & cn , & cprm , & argv , & argc ) ;
2012-09-27 05:52:08 +04:00
2013-05-01 02:28:06 +04:00
if ( ispipe ) {
2019-08-03 07:49:05 +03:00
int argi ;
2012-09-27 05:52:08 +04:00
int dump_count ;
char * * helper_argv ;
2013-05-01 02:28:07 +04:00
struct subprocess_info * sub_info ;
2012-09-27 05:52:08 +04:00
if ( ispipe < 0 ) {
printk ( KERN_WARNING " format_corename failed \n " ) ;
printk ( KERN_WARNING " Aborting core \n " ) ;
2013-07-04 02:08:16 +04:00
goto fail_unlock ;
2012-09-27 05:52:08 +04: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 16:35:05 +03:00
* cprm . limit of 1 here as a special value , this is a
2012-09-27 05:52:08 +04: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 .
*/
printk ( KERN_WARNING
" Process %d(%s) has RLIMIT_CORE set to 1 \n " ,
task_tgid_vnr ( current ) , current - > comm ) ;
printk ( KERN_WARNING " Aborting core \n " ) ;
goto fail_unlock ;
}
cprm . limit = RLIM_INFINITY ;
dump_count = atomic_inc_return ( & core_dump_count ) ;
if ( core_pipe_limit & & ( core_pipe_limit < dump_count ) ) {
printk ( KERN_WARNING " Pid %d(%s) over core_pipe_limit \n " ,
task_tgid_vnr ( current ) , current - > comm ) ;
printk ( KERN_WARNING " Skipping core dump \n " ) ;
goto fail_dropcount ;
}
2019-08-03 07:49:05 +03:00
helper_argv = kmalloc_array ( argc + 1 , sizeof ( * helper_argv ) ,
GFP_KERNEL ) ;
2012-09-27 05:52:08 +04:00
if ( ! helper_argv ) {
printk ( KERN_WARNING " %s failed to allocate memory \n " ,
__func__ ) ;
goto fail_dropcount ;
}
2019-08-03 07:49:05 +03:00
for ( argi = 0 ; argi < argc ; argi + + )
helper_argv [ argi ] = cn . corename + argv [ argi ] ;
helper_argv [ argi ] = NULL ;
2012-09-27 05:52:08 +04:00
2013-05-01 02:28:07 +04: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 07:49:05 +03:00
kfree ( helper_argv ) ;
2012-09-27 05:52:08 +04:00
if ( retval ) {
2013-07-04 02:08:23 +04:00
printk ( KERN_INFO " Core dump to |%s pipe failed \n " ,
2012-09-27 05:52:08 +04:00
cn . corename ) ;
goto close_fail ;
2013-05-01 02:28:06 +04:00
}
2012-09-27 05:52:08 +04:00
} else {
struct inode * inode ;
2016-03-23 00:25:36 +03:00
int open_flags = O_CREAT | O_RDWR | O_NOFOLLOW |
O_LARGEFILE | O_EXCL ;
2012-09-27 05:52:08 +04:00
if ( cprm . limit < binfmt - > min_coredump )
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-10 01:38:28 +03:00
if ( need_suid_safe & & cn . corename [ 0 ] ! = ' / ' ) {
2012-09-27 05:52:08 +04:00
printk ( KERN_WARNING " Pid %d(%s) can only dump core " \
" to fully qualified path! \n " ,
task_tgid_vnr ( current ) , current - > comm ) ;
printk ( KERN_WARNING " Skipping core dump \n " ) ;
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-10 01:38:28 +03: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 13:44:46 +03: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-10 01:38:28 +03: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-23 00:25:36 +03: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 ) ;
cprm . file = file_open_root ( root . dentry , root . mnt ,
cn . corename , open_flags , 0600 ) ;
path_put ( & root ) ;
} else {
cprm . file = filp_open ( cn . corename , open_flags , 0600 ) ;
}
2012-09-27 05:52:08 +04:00
if ( IS_ERR ( cprm . file ) )
goto fail_unlock ;
2013-01-24 02:07:38 +04:00
inode = file_inode ( cprm . file ) ;
2012-09-27 05:52:08 +04:00
if ( inode - > i_nlink > 1 )
goto close_fail ;
if ( d_unhashed ( cprm . file - > f_path . dentry ) )
goto close_fail ;
/*
* AK : actually i see no reason to not allow this for named
* pipes etc , but keep the previous behaviour for now .
*/
if ( ! S_ISREG ( inode - > i_mode ) )
goto close_fail ;
/*
2015-09-10 01:38:30 +03: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 05:52:08 +04:00
*/
if ( ! uid_eq ( inode - > i_uid , current_fsuid ( ) ) )
goto close_fail ;
2015-09-10 01:38:30 +03:00
if ( ( inode - > i_mode & 0677 ) ! = 0600 )
goto close_fail ;
2015-04-03 22:23:17 +03:00
if ( ! ( cprm . file - > f_mode & FMODE_CAN_WRITE ) )
2012-09-27 05:52:08 +04:00
goto close_fail ;
if ( do_truncate ( cprm . file - > f_path . dentry , 0 , 0 , cprm . file ) )
goto close_fail ;
}
/* get us an unshared descriptor table; almost always a no-op */
retval = unshare_files ( & displaced ) ;
if ( retval )
goto close_fail ;
if ( displaced )
put_files_struct ( displaced ) ;
2013-05-04 22:45:54 +04:00
if ( ! dump_interrupted ( ) ) {
file_start_write ( cprm . file ) ;
core_dumped = binfmt - > core_dump ( & cprm ) ;
file_end_write ( cprm . file ) ;
}
2012-09-27 05:52:08 +04: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 07:49:05 +03:00
kfree ( argv ) ;
2012-09-27 05:52:08 +04:00
kfree ( cn . corename ) ;
2013-05-01 02:28:13 +04:00
coredump_finish ( mm , core_dumped ) ;
2012-09-27 05:52:08 +04:00
revert_creds ( old_cred ) ;
fail_creds :
put_cred ( cred ) ;
fail :
return ;
}
/*
* 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 .
*/
2013-10-05 23:32:35 +04:00
int dump_emit ( struct coredump_params * cprm , const void * addr , int nr )
{
struct file * file = cprm - > file ;
2013-10-08 17:11:48 +04:00
loff_t pos = file - > f_pos ;
ssize_t n ;
2016-05-12 01:16:37 +03:00
if ( cprm - > written + nr > cprm - > limit )
2013-10-05 23:32:35 +04:00
return 0 ;
2013-10-08 17:11:48 +04:00
while ( nr ) {
if ( dump_interrupted ( ) )
return 0 ;
2013-11-16 06:58:33 +04:00
n = __kernel_write ( file , addr , nr , & pos ) ;
2013-10-08 17:11:48 +04:00
if ( n < = 0 )
return 0 ;
file - > f_pos = pos ;
2016-05-12 01:16:37 +03:00
cprm - > written + = n ;
2016-06-06 00:14:14 +03:00
cprm - > pos + = n ;
2013-10-08 17:11:48 +04:00
nr - = n ;
}
2013-10-05 23:32:35 +04:00
return 1 ;
}
EXPORT_SYMBOL ( dump_emit ) ;
2013-10-08 17:26:08 +04:00
int dump_skip ( struct coredump_params * cprm , size_t nr )
2012-09-27 05:52:08 +04:00
{
2013-10-08 17:26:08 +04:00
static char zeroes [ PAGE_SIZE ] ;
struct file * file = cprm - > file ;
2012-09-27 05:52:08 +04:00
if ( file - > f_op - > llseek & & file - > f_op - > llseek ! = no_llseek ) {
2013-05-01 02:28:15 +04:00
if ( dump_interrupted ( ) | |
2013-10-08 17:26:08 +04:00
file - > f_op - > llseek ( file , nr , SEEK_CUR ) < 0 )
2012-09-27 05:52:08 +04:00
return 0 ;
2016-06-06 00:14:14 +03:00
cprm - > pos + = nr ;
2013-10-08 17:26:08 +04:00
return 1 ;
2012-09-27 05:52:08 +04:00
} else {
2013-10-08 17:26:08 +04:00
while ( nr > PAGE_SIZE ) {
if ( ! dump_emit ( cprm , zeroes , PAGE_SIZE ) )
return 0 ;
nr - = PAGE_SIZE ;
2012-09-27 05:52:08 +04:00
}
2013-10-08 17:26:08 +04:00
return dump_emit ( cprm , zeroes , nr ) ;
2012-09-27 05:52:08 +04:00
}
}
2013-10-08 17:26:08 +04:00
EXPORT_SYMBOL ( dump_skip ) ;
2013-10-08 19:05:01 +04:00
int dump_align ( struct coredump_params * cprm , int align )
{
2016-06-06 00:14:14 +03:00
unsigned mod = cprm - > pos & ( align - 1 ) ;
2013-10-08 19:05:01 +04:00
if ( align & ( align - 1 ) )
2013-11-16 06:55:52 +04:00
return 0 ;
return mod ? dump_skip ( cprm , align - mod ) : 1 ;
2013-10-08 19:05:01 +04:00
}
EXPORT_SYMBOL ( dump_align ) ;
2017-01-11 22:25:00 +03: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 .
*/
void dump_truncate ( struct coredump_params * cprm )
{
struct file * file = cprm - > file ;
loff_t offset ;
if ( file - > f_op - > llseek & & file - > f_op - > llseek ! = no_llseek ) {
offset = file - > f_op - > llseek ( file , 0 , SEEK_CUR ) ;
if ( i_size_read ( file - > f_mapping - > host ) < offset )
do_truncate ( file - > f_path . dentry , offset , 0 , file ) ;
}
}
EXPORT_SYMBOL ( dump_truncate ) ;