2005-04-17 02:20:36 +04:00
/*
* linux / fs / binfmt_elf . c
*
* These are the functions used to load ELF format executables as used
* on SVr4 machines . Information on the format may be found in the book
* " UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
* Tools " .
*
* Copyright 1993 , 1994 : Eric Youngdale ( ericy @ cais . com ) .
*/
# include <linux/module.h>
# include <linux/kernel.h>
# include <linux/fs.h>
# include <linux/mm.h>
# include <linux/mman.h>
# include <linux/errno.h>
# include <linux/signal.h>
# include <linux/binfmts.h>
# include <linux/string.h>
# include <linux/file.h>
# include <linux/slab.h>
# include <linux/personality.h>
# include <linux/elfcore.h>
# include <linux/init.h>
# include <linux/highuid.h>
# include <linux/compiler.h>
# include <linux/highmem.h>
# include <linux/pagemap.h>
# include <linux/security.h>
# include <linux/random.h>
2006-06-23 13:05:35 +04:00
# include <linux/elf.h>
2007-05-08 11:28:59 +04:00
# include <linux/utsname.h>
2010-03-06 00:44:06 +03:00
# include <linux/coredump.h>
2005-04-17 02:20:36 +04:00
# include <asm/uaccess.h>
# include <asm/param.h>
# include <asm/page.h>
2012-03-28 21:30:03 +04:00
# include <asm/exec.h>
2005-04-17 02:20:36 +04:00
2006-06-23 13:05:35 +04:00
static int load_elf_binary ( struct linux_binprm * bprm , struct pt_regs * regs ) ;
static int load_elf_library ( struct file * ) ;
x86: PIE executable randomization, checkpatch fixes
#39: FILE: arch/ia64/ia32/binfmt_elf32.c:229:
+elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type, unsigned long unused)
WARNING: no space between function name and open parenthesis '('
#39: FILE: arch/ia64/ia32/binfmt_elf32.c:229:
+elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type, unsigned long unused)
WARNING: line over 80 characters
#67: FILE: arch/x86/kernel/sys_x86_64.c:80:
+ new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
ERROR: use tabs not spaces
#110: FILE: arch/x86/kernel/sys_x86_64.c:185:
+ ^I mm->cached_hole_size = 0;$
ERROR: use tabs not spaces
#111: FILE: arch/x86/kernel/sys_x86_64.c:186:
+ ^I^Imm->free_area_cache = mm->mmap_base;$
ERROR: use tabs not spaces
#112: FILE: arch/x86/kernel/sys_x86_64.c:187:
+ ^I}$
ERROR: use tabs not spaces
#141: FILE: arch/x86/kernel/sys_x86_64.c:216:
+ ^I^I/* remember the largest hole we saw so far */$
ERROR: use tabs not spaces
#142: FILE: arch/x86/kernel/sys_x86_64.c:217:
+ ^I^Iif (addr + mm->cached_hole_size < vma->vm_start)$
ERROR: use tabs not spaces
#143: FILE: arch/x86/kernel/sys_x86_64.c:218:
+ ^I^I mm->cached_hole_size = vma->vm_start - addr;$
ERROR: use tabs not spaces
#157: FILE: arch/x86/kernel/sys_x86_64.c:232:
+ ^Imm->free_area_cache = TASK_UNMAPPED_BASE;$
ERROR: need a space before the open parenthesis '('
#291: FILE: arch/x86/mm/mmap_64.c:101:
+ } else if(mmap_is_legacy()) {
WARNING: braces {} are not necessary for single statement blocks
#302: FILE: arch/x86/mm/mmap_64.c:112:
+ if (current->flags & PF_RANDOMIZE) {
+ mm->mmap_base += ((long)rnd) << PAGE_SHIFT;
+ }
WARNING: line over 80 characters
#314: FILE: fs/binfmt_elf.c:48:
+static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int, unsigned long);
WARNING: no space between function name and open parenthesis '('
#314: FILE: fs/binfmt_elf.c:48:
+static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int, unsigned long);
WARNING: line over 80 characters
#429: FILE: fs/binfmt_elf.c:438:
+ eppnt, elf_prot, elf_type, total_size);
ERROR: need space after that ',' (ctx:VxV)
#480: FILE: fs/binfmt_elf.c:939:
+ elf_prot, elf_flags,0);
^
total: 9 errors, 7 warnings, 461 lines checked
Your patch has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
Please run checkpatch prior to sending patches
Cc: "Luck, Tony" <tony.luck@intel.com>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Roland McGrath <roland@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-01-30 15:31:07 +03:00
static unsigned long elf_map ( struct file * , unsigned long , struct elf_phdr * ,
int , int , unsigned long ) ;
2005-04-17 02:20:36 +04:00
/*
* If we don ' t support core dumping , then supply a NULL so we
* don ' t even try .
*/
2009-12-16 03:47:37 +03:00
# ifdef CONFIG_ELF_CORE
2009-12-18 02:27:16 +03:00
static int elf_core_dump ( struct coredump_params * cprm ) ;
2005-04-17 02:20:36 +04:00
# else
# define elf_core_dump NULL
# endif
# if ELF_EXEC_PAGESIZE > PAGE_SIZE
2006-06-23 13:05:35 +04:00
# define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
2005-04-17 02:20:36 +04:00
# else
2006-06-23 13:05:35 +04:00
# define ELF_MIN_ALIGN PAGE_SIZE
2005-04-17 02:20:36 +04:00
# endif
# ifndef ELF_CORE_EFLAGS
# define ELF_CORE_EFLAGS 0
# endif
# define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
# define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
# define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
static struct linux_binfmt elf_format = {
2011-01-13 04:00:02 +03:00
. module = THIS_MODULE ,
. load_binary = load_elf_binary ,
. load_shlib = load_elf_library ,
. core_dump = elf_core_dump ,
. min_coredump = ELF_EXEC_PAGESIZE ,
2005-04-17 02:20:36 +04:00
} ;
2007-07-21 15:37:32 +04:00
# define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
2005-04-17 02:20:36 +04:00
static int set_brk ( unsigned long start , unsigned long end )
{
start = ELF_PAGEALIGN ( start ) ;
end = ELF_PAGEALIGN ( end ) ;
if ( end > start ) {
unsigned long addr ;
2012-04-21 02:35:40 +04:00
addr = vm_brk ( start , end - start ) ;
2005-04-17 02:20:36 +04:00
if ( BAD_ADDR ( addr ) )
return addr ;
}
current - > mm - > start_brk = current - > mm - > brk = end ;
return 0 ;
}
/* We need to explicitly zero any fractional pages
after the data section ( i . e . bss ) . This would
contain the junk from the file that should not
2006-06-23 13:05:35 +04:00
be in memory
*/
2005-04-17 02:20:36 +04:00
static int padzero ( unsigned long elf_bss )
{
unsigned long nbyte ;
nbyte = ELF_PAGEOFFSET ( elf_bss ) ;
if ( nbyte ) {
nbyte = ELF_MIN_ALIGN - nbyte ;
if ( clear_user ( ( void __user * ) elf_bss , nbyte ) )
return - EFAULT ;
}
return 0 ;
}
2008-02-03 19:05:15 +03:00
/* Let's use some macros to make this stack manipulation a little clearer */
2005-04-17 02:20:36 +04:00
# ifdef CONFIG_STACK_GROWSUP
# define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
# define STACK_ROUND(sp, items) \
( ( 15 + ( unsigned long ) ( ( sp ) + ( items ) ) ) & ~ 15UL )
2006-06-23 13:05:35 +04:00
# define STACK_ALLOC(sp, len) ({ \
elf_addr_t __user * old_sp = ( elf_addr_t __user * ) sp ; sp + = len ; \
old_sp ; } )
2005-04-17 02:20:36 +04:00
# else
# define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
# define STACK_ROUND(sp, items) \
( ( ( unsigned long ) ( sp - items ) ) & ~ 15UL )
# define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
# endif
ELF loader support for auxvec base platform string
Some IBM POWER-based platforms have the ability to run in a
mode which mostly appears to the OS as a different processor from the
actual hardware. For example, a Power6 system may appear to be a
Power5+, which makes the AT_PLATFORM value "power5+". This means that
programs are restricted to the ISA supported by Power5+;
Power6-specific instructions are treated as illegal.
However, some applications (virtual machines, optimized libraries) can
benefit from knowledge of the underlying CPU model. A new aux vector
entry, AT_BASE_PLATFORM, will denote the actual hardware. For
example, on a Power6 system in Power5+ compatibility mode, AT_PLATFORM
will be "power5+" and AT_BASE_PLATFORM will be "power6". The idea is
that AT_PLATFORM indicates the instruction set supported, while
AT_BASE_PLATFORM indicates the underlying microarchitecture.
If the architecture has defined ELF_BASE_PLATFORM, copy that value to
the user stack in the same manner as ELF_PLATFORM.
Signed-off-by: Nathan Lynch <ntl@pobox.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2008-07-21 22:48:46 +04:00
# ifndef ELF_BASE_PLATFORM
/*
* AT_BASE_PLATFORM indicates the " real " hardware / microarchitecture .
* If the arch defines ELF_BASE_PLATFORM ( in asm / elf . h ) , the value
* will be copied to the user stack in the same manner as AT_PLATFORM .
*/
# define ELF_BASE_PLATFORM NULL
# endif
2005-04-17 02:20:36 +04:00
static int
2006-06-23 13:05:35 +04:00
create_elf_tables ( struct linux_binprm * bprm , struct elfhdr * exec ,
2008-02-08 15:21:54 +03:00
unsigned long load_addr , unsigned long interp_load_addr )
2005-04-17 02:20:36 +04:00
{
unsigned long p = bprm - > p ;
int argc = bprm - > argc ;
int envc = bprm - > envc ;
elf_addr_t __user * argv ;
elf_addr_t __user * envp ;
elf_addr_t __user * sp ;
elf_addr_t __user * u_platform ;
ELF loader support for auxvec base platform string
Some IBM POWER-based platforms have the ability to run in a
mode which mostly appears to the OS as a different processor from the
actual hardware. For example, a Power6 system may appear to be a
Power5+, which makes the AT_PLATFORM value "power5+". This means that
programs are restricted to the ISA supported by Power5+;
Power6-specific instructions are treated as illegal.
However, some applications (virtual machines, optimized libraries) can
benefit from knowledge of the underlying CPU model. A new aux vector
entry, AT_BASE_PLATFORM, will denote the actual hardware. For
example, on a Power6 system in Power5+ compatibility mode, AT_PLATFORM
will be "power5+" and AT_BASE_PLATFORM will be "power6". The idea is
that AT_PLATFORM indicates the instruction set supported, while
AT_BASE_PLATFORM indicates the underlying microarchitecture.
If the architecture has defined ELF_BASE_PLATFORM, copy that value to
the user stack in the same manner as ELF_PLATFORM.
Signed-off-by: Nathan Lynch <ntl@pobox.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2008-07-21 22:48:46 +04:00
elf_addr_t __user * u_base_platform ;
2009-01-08 05:08:52 +03:00
elf_addr_t __user * u_rand_bytes ;
2005-04-17 02:20:36 +04:00
const char * k_platform = ELF_PLATFORM ;
ELF loader support for auxvec base platform string
Some IBM POWER-based platforms have the ability to run in a
mode which mostly appears to the OS as a different processor from the
actual hardware. For example, a Power6 system may appear to be a
Power5+, which makes the AT_PLATFORM value "power5+". This means that
programs are restricted to the ISA supported by Power5+;
Power6-specific instructions are treated as illegal.
However, some applications (virtual machines, optimized libraries) can
benefit from knowledge of the underlying CPU model. A new aux vector
entry, AT_BASE_PLATFORM, will denote the actual hardware. For
example, on a Power6 system in Power5+ compatibility mode, AT_PLATFORM
will be "power5+" and AT_BASE_PLATFORM will be "power6". The idea is
that AT_PLATFORM indicates the instruction set supported, while
AT_BASE_PLATFORM indicates the underlying microarchitecture.
If the architecture has defined ELF_BASE_PLATFORM, copy that value to
the user stack in the same manner as ELF_PLATFORM.
Signed-off-by: Nathan Lynch <ntl@pobox.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2008-07-21 22:48:46 +04:00
const char * k_base_platform = ELF_BASE_PLATFORM ;
2009-01-08 05:08:52 +03:00
unsigned char k_rand_bytes [ 16 ] ;
2005-04-17 02:20:36 +04:00
int items ;
elf_addr_t * elf_info ;
int ei_index = 0 ;
2008-11-14 02:39:18 +03:00
const struct cred * cred = current_cred ( ) ;
2007-07-19 12:48:16 +04:00
struct vm_area_struct * vma ;
2005-04-17 02:20:36 +04:00
2007-10-17 10:30:24 +04:00
/*
* In some cases ( e . g . Hyper - Threading ) , we want to avoid L1
* evictions by the processes running on the same package . One
* thing we can do is to shuffle the initial stack for them .
*/
p = arch_align_stack ( p ) ;
2005-04-17 02:20:36 +04:00
/*
* If this architecture has a platform capability string , copy it
* to userspace . In some cases ( Sparc ) , this info is impossible
* for userspace to get any other way , in others ( i386 ) it is
* merely difficult .
*/
u_platform = NULL ;
if ( k_platform ) {
size_t len = strlen ( k_platform ) + 1 ;
u_platform = ( elf_addr_t __user * ) STACK_ALLOC ( p , len ) ;
if ( __copy_to_user ( u_platform , k_platform , len ) )
return - EFAULT ;
}
ELF loader support for auxvec base platform string
Some IBM POWER-based platforms have the ability to run in a
mode which mostly appears to the OS as a different processor from the
actual hardware. For example, a Power6 system may appear to be a
Power5+, which makes the AT_PLATFORM value "power5+". This means that
programs are restricted to the ISA supported by Power5+;
Power6-specific instructions are treated as illegal.
However, some applications (virtual machines, optimized libraries) can
benefit from knowledge of the underlying CPU model. A new aux vector
entry, AT_BASE_PLATFORM, will denote the actual hardware. For
example, on a Power6 system in Power5+ compatibility mode, AT_PLATFORM
will be "power5+" and AT_BASE_PLATFORM will be "power6". The idea is
that AT_PLATFORM indicates the instruction set supported, while
AT_BASE_PLATFORM indicates the underlying microarchitecture.
If the architecture has defined ELF_BASE_PLATFORM, copy that value to
the user stack in the same manner as ELF_PLATFORM.
Signed-off-by: Nathan Lynch <ntl@pobox.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2008-07-21 22:48:46 +04:00
/*
* If this architecture has a " base " platform capability
* string , copy it to userspace .
*/
u_base_platform = NULL ;
if ( k_base_platform ) {
size_t len = strlen ( k_base_platform ) + 1 ;
u_base_platform = ( elf_addr_t __user * ) STACK_ALLOC ( p , len ) ;
if ( __copy_to_user ( u_base_platform , k_base_platform , len ) )
return - EFAULT ;
}
2009-01-08 05:08:52 +03:00
/*
* Generate 16 random bytes for userspace PRNG seeding .
*/
get_random_bytes ( k_rand_bytes , sizeof ( k_rand_bytes ) ) ;
u_rand_bytes = ( elf_addr_t __user * )
STACK_ALLOC ( p , sizeof ( k_rand_bytes ) ) ;
if ( __copy_to_user ( u_rand_bytes , k_rand_bytes , sizeof ( k_rand_bytes ) ) )
return - EFAULT ;
2005-04-17 02:20:36 +04:00
/* Create the ELF interpreter info */
2006-06-23 13:05:35 +04:00
elf_info = ( elf_addr_t * ) current - > mm - > saved_auxv ;
2007-10-17 10:30:12 +04:00
/* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
2005-04-17 02:20:36 +04:00
# define NEW_AUX_ENT(id, val) \
2006-06-23 13:05:35 +04:00
do { \
2006-06-23 13:05:35 +04:00
elf_info [ ei_index + + ] = id ; \
elf_info [ ei_index + + ] = val ; \
2006-06-23 13:05:35 +04:00
} while ( 0 )
2005-04-17 02:20:36 +04:00
# ifdef ARCH_DLINFO
/*
* ARCH_DLINFO must come first so PPC can do its special alignment of
* AUXV .
2007-10-17 10:30:12 +04:00
* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT ( ) in
* ARCH_DLINFO changes
2005-04-17 02:20:36 +04:00
*/
ARCH_DLINFO ;
# endif
NEW_AUX_ENT ( AT_HWCAP , ELF_HWCAP ) ;
NEW_AUX_ENT ( AT_PAGESZ , ELF_EXEC_PAGESIZE ) ;
NEW_AUX_ENT ( AT_CLKTCK , CLOCKS_PER_SEC ) ;
NEW_AUX_ENT ( AT_PHDR , load_addr + exec - > e_phoff ) ;
2006-06-23 13:05:35 +04:00
NEW_AUX_ENT ( AT_PHENT , sizeof ( struct elf_phdr ) ) ;
2005-04-17 02:20:36 +04:00
NEW_AUX_ENT ( AT_PHNUM , exec - > e_phnum ) ;
NEW_AUX_ENT ( AT_BASE , interp_load_addr ) ;
NEW_AUX_ENT ( AT_FLAGS , 0 ) ;
NEW_AUX_ENT ( AT_ENTRY , exec - > e_entry ) ;
2012-02-08 06:36:10 +04:00
NEW_AUX_ENT ( AT_UID , from_kuid_munged ( cred - > user_ns , cred - > uid ) ) ;
NEW_AUX_ENT ( AT_EUID , from_kuid_munged ( cred - > user_ns , cred - > euid ) ) ;
NEW_AUX_ENT ( AT_GID , from_kgid_munged ( cred - > user_ns , cred - > gid ) ) ;
NEW_AUX_ENT ( AT_EGID , from_kgid_munged ( cred - > user_ns , cred - > egid ) ) ;
2006-06-23 13:05:35 +04:00
NEW_AUX_ENT ( AT_SECURE , security_bprm_secureexec ( bprm ) ) ;
2009-01-08 05:08:52 +03:00
NEW_AUX_ENT ( AT_RANDOM , ( elf_addr_t ) ( unsigned long ) u_rand_bytes ) ;
execve filename: document and export via auxiliary vector
The Linux kernel puts the filename argument of execve() into the new
address space. Many developers are surprised to learn this. Those who
know and could use it, object "But it's not documented."
Those who want to use it dislike the expression
(char *)(1+ strlen(env[-1+ n_env]) + env[-1+ n_env])
because it requires locating the last original environment variable,
and assumes that the filename follows the characters.
This patch documents the insertion of the filename, and makes it easier
to find by adding a new tag AT_EXECFN in the ElfXX_auxv_t; see <elf.h>.
In many cases readlink("/proc/self/exe",) gives the same answer. But if
all the original pages get unmapped, then the kernel erases the symlink
for /proc/self/exe. This can happen when a program decompressor does a
good job of cleaning up after uncompressing directly to memory, so that
the address space of the target program looks the same as if compression
had never happened. One example is http://upx.sourceforge.net .
One notable use of the underlying concept (what path containED the
executable) is glibc expanding $ORIGIN in DT_RUNPATH. In practice for
the near term, it may be a good idea for user-mode code to use both
/proc/self/exe and AT_EXECFN as fall-back methods for each other.
/proc/self/exe can fail due to unmapping, AT_EXECFN can fail because it
won't be present on non-new systems. The auxvec or {AT_EXECFN}.d_val
also can get overwritten, although in nearly all cases this would be the
result of a bug.
The runtime cost is one NEW_AUX_ENT using two words of stack space. The
underlying value is maintained already as bprm->exec; setup_arg_pages()
in fs/exec.c slides it for stack_shift, etc.
Signed-off-by: John Reiser <jreiser@BitWagon.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Ulrich Drepper <drepper@redhat.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-22 01:21:32 +04:00
NEW_AUX_ENT ( AT_EXECFN , bprm - > exec ) ;
2005-04-17 02:20:36 +04:00
if ( k_platform ) {
2006-06-23 13:05:35 +04:00
NEW_AUX_ENT ( AT_PLATFORM ,
2006-06-23 13:05:35 +04:00
( elf_addr_t ) ( unsigned long ) u_platform ) ;
2005-04-17 02:20:36 +04:00
}
ELF loader support for auxvec base platform string
Some IBM POWER-based platforms have the ability to run in a
mode which mostly appears to the OS as a different processor from the
actual hardware. For example, a Power6 system may appear to be a
Power5+, which makes the AT_PLATFORM value "power5+". This means that
programs are restricted to the ISA supported by Power5+;
Power6-specific instructions are treated as illegal.
However, some applications (virtual machines, optimized libraries) can
benefit from knowledge of the underlying CPU model. A new aux vector
entry, AT_BASE_PLATFORM, will denote the actual hardware. For
example, on a Power6 system in Power5+ compatibility mode, AT_PLATFORM
will be "power5+" and AT_BASE_PLATFORM will be "power6". The idea is
that AT_PLATFORM indicates the instruction set supported, while
AT_BASE_PLATFORM indicates the underlying microarchitecture.
If the architecture has defined ELF_BASE_PLATFORM, copy that value to
the user stack in the same manner as ELF_PLATFORM.
Signed-off-by: Nathan Lynch <ntl@pobox.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2008-07-21 22:48:46 +04:00
if ( k_base_platform ) {
NEW_AUX_ENT ( AT_BASE_PLATFORM ,
( elf_addr_t ) ( unsigned long ) u_base_platform ) ;
}
2005-04-17 02:20:36 +04:00
if ( bprm - > interp_flags & BINPRM_FLAGS_EXECFD ) {
2006-06-23 13:05:35 +04:00
NEW_AUX_ENT ( AT_EXECFD , bprm - > interp_data ) ;
2005-04-17 02:20:36 +04:00
}
# undef NEW_AUX_ENT
/* AT_NULL is zero; clear the rest too */
memset ( & elf_info [ ei_index ] , 0 ,
sizeof current - > mm - > saved_auxv - ei_index * sizeof elf_info [ 0 ] ) ;
/* And advance past the AT_NULL entry. */
ei_index + = 2 ;
sp = STACK_ADD ( p , ei_index ) ;
2008-02-08 15:21:54 +03:00
items = ( argc + 1 ) + ( envc + 1 ) + 1 ;
2005-04-17 02:20:36 +04:00
bprm - > p = STACK_ROUND ( sp , items ) ;
/* Point sp at the lowest address on the stack */
# ifdef CONFIG_STACK_GROWSUP
sp = ( elf_addr_t __user * ) bprm - > p - items - ei_index ;
2006-06-23 13:05:35 +04:00
bprm - > exec = ( unsigned long ) sp ; /* XXX: PARISC HACK */
2005-04-17 02:20:36 +04:00
# else
sp = ( elf_addr_t __user * ) bprm - > p ;
# endif
2007-07-19 12:48:16 +04:00
/*
* Grow the stack manually ; some architectures have a limit on how
* far ahead a user - space access may be in order to grow the stack .
*/
vma = find_extend_vma ( current - > mm , bprm - > p ) ;
if ( ! vma )
return - EFAULT ;
2005-04-17 02:20:36 +04:00
/* Now, let's put argc (and argv, envp if appropriate) on the stack */
if ( __put_user ( argc , sp + + ) )
return - EFAULT ;
2008-02-08 15:21:54 +03:00
argv = sp ;
envp = argv + argc + 1 ;
2005-04-17 02:20:36 +04:00
/* Populate argv and envp */
2005-05-11 11:10:44 +04:00
p = current - > mm - > arg_end = current - > mm - > arg_start ;
2005-04-17 02:20:36 +04:00
while ( argc - - > 0 ) {
size_t len ;
2006-12-07 07:36:35 +03:00
if ( __put_user ( ( elf_addr_t ) p , argv + + ) )
return - EFAULT ;
2007-07-19 12:48:16 +04:00
len = strnlen_user ( ( void __user * ) p , MAX_ARG_STRLEN ) ;
if ( ! len | | len > MAX_ARG_STRLEN )
2008-05-08 17:52:33 +04:00
return - EINVAL ;
2005-04-17 02:20:36 +04:00
p + = len ;
}
if ( __put_user ( 0 , argv ) )
return - EFAULT ;
current - > mm - > arg_end = current - > mm - > env_start = p ;
while ( envc - - > 0 ) {
size_t len ;
2006-12-07 07:36:35 +03:00
if ( __put_user ( ( elf_addr_t ) p , envp + + ) )
return - EFAULT ;
2007-07-19 12:48:16 +04:00
len = strnlen_user ( ( void __user * ) p , MAX_ARG_STRLEN ) ;
if ( ! len | | len > MAX_ARG_STRLEN )
2008-05-08 17:52:33 +04:00
return - EINVAL ;
2005-04-17 02:20:36 +04:00
p + = len ;
}
if ( __put_user ( 0 , envp ) )
return - EFAULT ;
current - > mm - > env_end = p ;
/* Put the elf_info on the stack in the right place. */
sp = ( elf_addr_t __user * ) envp + 1 ;
if ( copy_to_user ( sp , elf_info , ei_index * sizeof ( elf_addr_t ) ) )
return - EFAULT ;
return 0 ;
}
static unsigned long elf_map ( struct file * filep , unsigned long addr ,
2008-01-30 15:31:07 +03:00
struct elf_phdr * eppnt , int prot , int type ,
unsigned long total_size )
2005-04-17 02:20:36 +04:00
{
unsigned long map_addr ;
2008-01-30 15:31:07 +03:00
unsigned long size = eppnt - > p_filesz + ELF_PAGEOFFSET ( eppnt - > p_vaddr ) ;
unsigned long off = eppnt - > p_offset - ELF_PAGEOFFSET ( eppnt - > p_vaddr ) ;
addr = ELF_PAGESTART ( addr ) ;
size = ELF_PAGEALIGN ( size ) ;
2005-04-17 02:20:36 +04:00
2006-01-08 12:03:35 +03:00
/* mmap() will return -EINVAL if given a zero size, but a
* segment with zero filesize is perfectly valid */
2008-01-30 15:31:07 +03:00
if ( ! size )
return addr ;
/*
* total_size is the size of the ELF ( interpreter ) image .
* The _first_ mmap needs to know the full size , otherwise
* randomization might put this image into an overlapping
* position with the ELF binary image . ( since size < total_size )
* So we first map the ' big ' image - and unmap the remainder at
* the end . ( which unmap is needed for ELF images with holes . )
*/
if ( total_size ) {
total_size = ELF_PAGEALIGN ( total_size ) ;
2012-05-30 09:49:38 +04:00
map_addr = vm_mmap ( filep , addr , total_size , prot , type , off ) ;
2008-01-30 15:31:07 +03:00
if ( ! BAD_ADDR ( map_addr ) )
2012-05-30 09:49:38 +04:00
vm_munmap ( map_addr + size , total_size - size ) ;
2008-01-30 15:31:07 +03:00
} else
2012-05-30 09:49:38 +04:00
map_addr = vm_mmap ( filep , addr , size , prot , type , off ) ;
2008-01-30 15:31:07 +03:00
2005-04-17 02:20:36 +04:00
return ( map_addr ) ;
}
2008-01-30 15:31:07 +03:00
static unsigned long total_mapping_size ( struct elf_phdr * cmds , int nr )
{
int i , first_idx = - 1 , last_idx = - 1 ;
for ( i = 0 ; i < nr ; i + + ) {
if ( cmds [ i ] . p_type = = PT_LOAD ) {
last_idx = i ;
if ( first_idx = = - 1 )
first_idx = i ;
}
}
if ( first_idx = = - 1 )
return 0 ;
return cmds [ last_idx ] . p_vaddr + cmds [ last_idx ] . p_memsz -
ELF_PAGESTART ( cmds [ first_idx ] . p_vaddr ) ;
}
2005-04-17 02:20:36 +04:00
/* This is much more generalized than the library routine read function,
so we keep this separate . Technically the library read function
is only provided so that we can read a . out libraries that have
an ELF header */
2006-06-23 13:05:35 +04:00
static unsigned long load_elf_interp ( struct elfhdr * interp_elf_ex ,
2008-01-30 15:31:07 +03:00
struct file * interpreter , unsigned long * interp_map_addr ,
unsigned long no_base )
2005-04-17 02:20:36 +04:00
{
struct elf_phdr * elf_phdata ;
struct elf_phdr * eppnt ;
unsigned long load_addr = 0 ;
int load_addr_set = 0 ;
unsigned long last_bss = 0 , elf_bss = 0 ;
unsigned long error = ~ 0UL ;
2008-01-30 15:31:07 +03:00
unsigned long total_size ;
2005-04-17 02:20:36 +04:00
int retval , i , size ;
/* First of all, some simple consistency checks */
if ( interp_elf_ex - > e_type ! = ET_EXEC & &
interp_elf_ex - > e_type ! = ET_DYN )
goto out ;
if ( ! elf_check_arch ( interp_elf_ex ) )
goto out ;
if ( ! interpreter - > f_op | | ! interpreter - > f_op - > mmap )
goto out ;
/*
* If the size of this structure has changed , then punt , since
* we will be doing the wrong thing .
*/
if ( interp_elf_ex - > e_phentsize ! = sizeof ( struct elf_phdr ) )
goto out ;
if ( interp_elf_ex - > e_phnum < 1 | |
interp_elf_ex - > e_phnum > 65536U / sizeof ( struct elf_phdr ) )
goto out ;
/* Now read in all of the header information */
size = sizeof ( struct elf_phdr ) * interp_elf_ex - > e_phnum ;
if ( size > ELF_MIN_ALIGN )
goto out ;
2006-06-23 13:05:35 +04:00
elf_phdata = kmalloc ( size , GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! elf_phdata )
goto out ;
2006-06-23 13:05:35 +04:00
retval = kernel_read ( interpreter , interp_elf_ex - > e_phoff ,
2011-01-13 04:00:02 +03:00
( char * ) elf_phdata , size ) ;
2005-04-17 02:20:36 +04:00
error = - EIO ;
if ( retval ! = size ) {
if ( retval < 0 )
error = retval ;
goto out_close ;
}
2008-01-30 15:31:07 +03:00
total_size = total_mapping_size ( elf_phdata , interp_elf_ex - > e_phnum ) ;
if ( ! total_size ) {
error = - EINVAL ;
goto out_close ;
}
2005-04-17 02:20:36 +04:00
eppnt = elf_phdata ;
2006-06-23 13:05:35 +04:00
for ( i = 0 ; i < interp_elf_ex - > e_phnum ; i + + , eppnt + + ) {
if ( eppnt - > p_type = = PT_LOAD ) {
int elf_type = MAP_PRIVATE | MAP_DENYWRITE ;
int elf_prot = 0 ;
unsigned long vaddr = 0 ;
unsigned long k , map_addr ;
if ( eppnt - > p_flags & PF_R )
elf_prot = PROT_READ ;
if ( eppnt - > p_flags & PF_W )
elf_prot | = PROT_WRITE ;
if ( eppnt - > p_flags & PF_X )
elf_prot | = PROT_EXEC ;
vaddr = eppnt - > p_vaddr ;
if ( interp_elf_ex - > e_type = = ET_EXEC | | load_addr_set )
elf_type | = MAP_FIXED ;
2008-01-30 15:31:07 +03:00
else if ( no_base & & interp_elf_ex - > e_type = = ET_DYN )
load_addr = - vaddr ;
2006-06-23 13:05:35 +04:00
map_addr = elf_map ( interpreter , load_addr + vaddr ,
x86: PIE executable randomization, checkpatch fixes
#39: FILE: arch/ia64/ia32/binfmt_elf32.c:229:
+elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type, unsigned long unused)
WARNING: no space between function name and open parenthesis '('
#39: FILE: arch/ia64/ia32/binfmt_elf32.c:229:
+elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type, unsigned long unused)
WARNING: line over 80 characters
#67: FILE: arch/x86/kernel/sys_x86_64.c:80:
+ new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
ERROR: use tabs not spaces
#110: FILE: arch/x86/kernel/sys_x86_64.c:185:
+ ^I mm->cached_hole_size = 0;$
ERROR: use tabs not spaces
#111: FILE: arch/x86/kernel/sys_x86_64.c:186:
+ ^I^Imm->free_area_cache = mm->mmap_base;$
ERROR: use tabs not spaces
#112: FILE: arch/x86/kernel/sys_x86_64.c:187:
+ ^I}$
ERROR: use tabs not spaces
#141: FILE: arch/x86/kernel/sys_x86_64.c:216:
+ ^I^I/* remember the largest hole we saw so far */$
ERROR: use tabs not spaces
#142: FILE: arch/x86/kernel/sys_x86_64.c:217:
+ ^I^Iif (addr + mm->cached_hole_size < vma->vm_start)$
ERROR: use tabs not spaces
#143: FILE: arch/x86/kernel/sys_x86_64.c:218:
+ ^I^I mm->cached_hole_size = vma->vm_start - addr;$
ERROR: use tabs not spaces
#157: FILE: arch/x86/kernel/sys_x86_64.c:232:
+ ^Imm->free_area_cache = TASK_UNMAPPED_BASE;$
ERROR: need a space before the open parenthesis '('
#291: FILE: arch/x86/mm/mmap_64.c:101:
+ } else if(mmap_is_legacy()) {
WARNING: braces {} are not necessary for single statement blocks
#302: FILE: arch/x86/mm/mmap_64.c:112:
+ if (current->flags & PF_RANDOMIZE) {
+ mm->mmap_base += ((long)rnd) << PAGE_SHIFT;
+ }
WARNING: line over 80 characters
#314: FILE: fs/binfmt_elf.c:48:
+static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int, unsigned long);
WARNING: no space between function name and open parenthesis '('
#314: FILE: fs/binfmt_elf.c:48:
+static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int, unsigned long);
WARNING: line over 80 characters
#429: FILE: fs/binfmt_elf.c:438:
+ eppnt, elf_prot, elf_type, total_size);
ERROR: need space after that ',' (ctx:VxV)
#480: FILE: fs/binfmt_elf.c:939:
+ elf_prot, elf_flags,0);
^
total: 9 errors, 7 warnings, 461 lines checked
Your patch has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
Please run checkpatch prior to sending patches
Cc: "Luck, Tony" <tony.luck@intel.com>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Roland McGrath <roland@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-01-30 15:31:07 +03:00
eppnt , elf_prot , elf_type , total_size ) ;
2008-01-30 15:31:07 +03:00
total_size = 0 ;
if ( ! * interp_map_addr )
* interp_map_addr = map_addr ;
2006-06-23 13:05:35 +04:00
error = map_addr ;
if ( BAD_ADDR ( map_addr ) )
goto out_close ;
if ( ! load_addr_set & &
interp_elf_ex - > e_type = = ET_DYN ) {
load_addr = map_addr - ELF_PAGESTART ( vaddr ) ;
load_addr_set = 1 ;
}
/*
* Check to see if the section ' s size will overflow the
* allowed task size . Note that p_filesz must always be
* < = p_memsize so it ' s only necessary to check p_memsz .
*/
k = load_addr + eppnt - > p_vaddr ;
[PATCH] binfmt_elf: fix checks for bad address
Fix check for bad address; use macro instead of open-coding two checks.
Taken from RHEL4 kernel update.
From: Ernie Petrides <petrides@redhat.com>
For background, the BAD_ADDR() macro should return TRUE if the address is
TASK_SIZE, because that's the lowest address that is *not* valid for
user-space mappings. The macro was correct in binfmt_aout.c but was wrong
for the "equal to" case in binfmt_elf.c. There were two in-line validations
of user-space addresses in binfmt_elf.c, which have been appropriately
converted to use the corrected BAD_ADDR() macro in the patch you posted
yesterday. Note that the size checks against TASK_SIZE are okay as coded.
The additional changes that I propose are below. These are in the error
paths for bad ELF entry addresses once load_elf_binary() has already
committed to exec'ing the new image (following the tearing down of the
task's original address space).
The 1st hunk deals with the interp-side of the outer "if". There were two
problems here. The printk() should be removed because this path can be
triggered at will by a bogus interpreter image created and used by a
malicious user. Further, the error code should not be ENOEXEC, because that
causes the loop in search_binary_handler() to continue trying other exec
handlers (twice, in fact). But it's too late for this to work correctly,
because the user address space has already been torn down, and an exec()
failure cannot be returned to the user code because the code no longer
exists. The only recovery is to force a SIGSEGV, but it's best to terminate
the search loop immediately. I somewhat arbitrarily chose EINVAL as a
fallback error code, but any error returned by load_elf_interp() will
override that (but this value will never be seen by user-space).
The 2nd hunk deals with the non-interp-side of the outer "if". There were
two problems here as well. The SIGSEGV needs to be forced, because a prior
sigaction() syscall might have set the associated disposition to SIG_IGN.
And the ENOEXEC should be changed to EINVAL as described above.
Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Signed-off-by: Ernie Petrides <petrides@redhat.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-07-03 11:24:14 +04:00
if ( BAD_ADDR ( k ) | |
2006-06-23 13:05:35 +04:00
eppnt - > p_filesz > eppnt - > p_memsz | |
eppnt - > p_memsz > TASK_SIZE | |
TASK_SIZE - eppnt - > p_memsz < k ) {
error = - ENOMEM ;
goto out_close ;
}
/*
* Find the end of the file mapping for this phdr , and
* keep track of the largest address we see for this .
*/
k = load_addr + eppnt - > p_vaddr + eppnt - > p_filesz ;
if ( k > elf_bss )
elf_bss = k ;
/*
* Do the same thing for the memory mapping - between
* elf_bss and last_bss is the bss section .
*/
k = load_addr + eppnt - > p_memsz + eppnt - > p_vaddr ;
if ( k > last_bss )
last_bss = k ;
}
2005-04-17 02:20:36 +04:00
}
2009-09-09 06:49:40 +04:00
if ( last_bss > elf_bss ) {
/*
* Now fill out the bss section . First pad the last page up
* to the page boundary , and then perform a mmap to make sure
* that there are zero - mapped pages up to and including the
* last bss page .
*/
if ( padzero ( elf_bss ) ) {
error = - EFAULT ;
goto out_close ;
}
2005-04-17 02:20:36 +04:00
2009-09-09 06:49:40 +04:00
/* What we have mapped so far */
elf_bss = ELF_PAGESTART ( elf_bss + ELF_MIN_ALIGN - 1 ) ;
2005-04-17 02:20:36 +04:00
2009-09-09 06:49:40 +04:00
/* Map the last of the bss segment */
2012-04-21 02:35:40 +04:00
error = vm_brk ( elf_bss , last_bss - elf_bss ) ;
2005-04-17 02:20:36 +04:00
if ( BAD_ADDR ( error ) )
goto out_close ;
}
2008-01-30 15:31:07 +03:00
error = load_addr ;
2005-04-17 02:20:36 +04:00
out_close :
kfree ( elf_phdata ) ;
out :
return error ;
}
/*
* These are the functions used to load ELF style executables and shared
* libraries . There is no binary dependent code anywhere else .
*/
# define INTERPRETER_NONE 0
# define INTERPRETER_ELF 2
2006-03-25 18:29:09 +03:00
# ifndef STACK_RND_MASK
2007-03-17 00:38:35 +03:00
# define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
2006-03-25 18:29:09 +03:00
# endif
2005-04-17 02:20:36 +04:00
static unsigned long randomize_stack_top ( unsigned long stack_top )
{
unsigned int random_variable = 0 ;
2006-09-26 12:52:28 +04:00
if ( ( current - > flags & PF_RANDOMIZE ) & &
! ( current - > personality & ADDR_NO_RANDOMIZE ) ) {
2006-03-25 18:29:09 +03:00
random_variable = get_random_int ( ) & STACK_RND_MASK ;
random_variable < < = PAGE_SHIFT ;
}
2005-04-17 02:20:36 +04:00
# ifdef CONFIG_STACK_GROWSUP
2006-03-25 18:29:09 +03:00
return PAGE_ALIGN ( stack_top ) + random_variable ;
2005-04-17 02:20:36 +04:00
# else
2006-03-25 18:29:09 +03:00
return PAGE_ALIGN ( stack_top ) - random_variable ;
2005-04-17 02:20:36 +04:00
# endif
}
2006-06-23 13:05:35 +04:00
static int load_elf_binary ( struct linux_binprm * bprm , struct pt_regs * regs )
2005-04-17 02:20:36 +04:00
{
struct file * interpreter = NULL ; /* to shut gcc up */
unsigned long load_addr = 0 , load_bias = 0 ;
int load_addr_set = 0 ;
char * elf_interpreter = NULL ;
unsigned long error ;
2006-06-23 13:05:35 +04:00
struct elf_phdr * elf_ppnt , * elf_phdata ;
2005-04-17 02:20:36 +04:00
unsigned long elf_bss , elf_brk ;
int retval , i ;
unsigned int size ;
2008-01-30 15:31:07 +03:00
unsigned long elf_entry ;
unsigned long interp_load_addr = 0 ;
2005-04-17 02:20:36 +04:00
unsigned long start_code , end_code , start_data , end_data ;
2011-03-23 02:34:48 +03:00
unsigned long reloc_func_desc __maybe_unused = 0 ;
2006-12-07 07:40:16 +03:00
int executable_stack = EXSTACK_DEFAULT ;
2005-04-17 02:20:36 +04:00
unsigned long def_flags = 0 ;
struct {
struct elfhdr elf_ex ;
struct elfhdr interp_elf_ex ;
} * loc ;
loc = kmalloc ( sizeof ( * loc ) , GFP_KERNEL ) ;
if ( ! loc ) {
retval = - ENOMEM ;
goto out_ret ;
}
/* Get the exec-header */
2006-06-23 13:05:35 +04:00
loc - > elf_ex = * ( ( struct elfhdr * ) bprm - > buf ) ;
2005-04-17 02:20:36 +04:00
retval = - ENOEXEC ;
/* First of all, some simple consistency checks */
if ( memcmp ( loc - > elf_ex . e_ident , ELFMAG , SELFMAG ) ! = 0 )
goto out ;
if ( loc - > elf_ex . e_type ! = ET_EXEC & & loc - > elf_ex . e_type ! = ET_DYN )
goto out ;
if ( ! elf_check_arch ( & loc - > elf_ex ) )
goto out ;
2011-01-13 04:00:02 +03:00
if ( ! bprm - > file - > f_op | | ! bprm - > file - > f_op - > mmap )
2005-04-17 02:20:36 +04:00
goto out ;
/* Now read in all of the header information */
if ( loc - > elf_ex . e_phentsize ! = sizeof ( struct elf_phdr ) )
goto out ;
if ( loc - > elf_ex . e_phnum < 1 | |
loc - > elf_ex . e_phnum > 65536U / sizeof ( struct elf_phdr ) )
goto out ;
size = loc - > elf_ex . e_phnum * sizeof ( struct elf_phdr ) ;
retval = - ENOMEM ;
2006-06-23 13:05:35 +04:00
elf_phdata = kmalloc ( size , GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! elf_phdata )
goto out ;
2006-06-23 13:05:35 +04:00
retval = kernel_read ( bprm - > file , loc - > elf_ex . e_phoff ,
( char * ) elf_phdata , size ) ;
2005-04-17 02:20:36 +04:00
if ( retval ! = size ) {
if ( retval > = 0 )
retval = - EIO ;
goto out_free_ph ;
}
elf_ppnt = elf_phdata ;
elf_bss = 0 ;
elf_brk = 0 ;
start_code = ~ 0UL ;
end_code = 0 ;
start_data = 0 ;
end_data = 0 ;
for ( i = 0 ; i < loc - > elf_ex . e_phnum ; i + + ) {
if ( elf_ppnt - > p_type = = PT_INTERP ) {
/* This is the program interpreter used for
* shared libraries - for now assume that this
* is an a . out format binary
*/
retval = - ENOEXEC ;
if ( elf_ppnt - > p_filesz > PATH_MAX | |
elf_ppnt - > p_filesz < 2 )
2009-03-30 00:31:16 +04:00
goto out_free_ph ;
2005-04-17 02:20:36 +04:00
retval = - ENOMEM ;
2006-01-10 07:54:45 +03:00
elf_interpreter = kmalloc ( elf_ppnt - > p_filesz ,
2006-06-23 13:05:35 +04:00
GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! elf_interpreter )
2009-03-30 00:31:16 +04:00
goto out_free_ph ;
2005-04-17 02:20:36 +04:00
retval = kernel_read ( bprm - > file , elf_ppnt - > p_offset ,
2006-06-23 13:05:35 +04:00
elf_interpreter ,
elf_ppnt - > p_filesz ) ;
2005-04-17 02:20:36 +04:00
if ( retval ! = elf_ppnt - > p_filesz ) {
if ( retval > = 0 )
retval = - EIO ;
goto out_free_interp ;
}
/* make sure path is NULL terminated */
retval = - ENOEXEC ;
if ( elf_interpreter [ elf_ppnt - > p_filesz - 1 ] ! = ' \0 ' )
goto out_free_interp ;
interpreter = open_exec ( elf_interpreter ) ;
retval = PTR_ERR ( interpreter ) ;
if ( IS_ERR ( interpreter ) )
goto out_free_interp ;
2007-01-26 11:57:16 +03:00
/*
* If the binary is not readable then enforce
* mm - > dumpable = 0 regardless of the interpreter ' s
* permissions .
*/
2011-06-19 20:49:47 +04:00
would_dump ( bprm , interpreter ) ;
2007-01-26 11:57:16 +03:00
2006-06-23 13:05:35 +04:00
retval = kernel_read ( interpreter , 0 , bprm - > buf ,
BINPRM_BUF_SIZE ) ;
2005-04-17 02:20:36 +04:00
if ( retval ! = BINPRM_BUF_SIZE ) {
if ( retval > = 0 )
retval = - EIO ;
goto out_free_dentry ;
}
/* Get the exec headers */
2006-06-23 13:05:35 +04:00
loc - > interp_elf_ex = * ( ( struct elfhdr * ) bprm - > buf ) ;
2005-04-17 02:20:36 +04:00
break ;
}
elf_ppnt + + ;
}
elf_ppnt = elf_phdata ;
for ( i = 0 ; i < loc - > elf_ex . e_phnum ; i + + , elf_ppnt + + )
if ( elf_ppnt - > p_type = = PT_GNU_STACK ) {
if ( elf_ppnt - > p_flags & PF_X )
executable_stack = EXSTACK_ENABLE_X ;
else
executable_stack = EXSTACK_DISABLE_X ;
break ;
}
/* Some simple consistency checks for the interpreter */
if ( elf_interpreter ) {
retval = - ELIBBAD ;
2008-02-08 15:21:54 +03:00
/* Not an ELF interpreter */
if ( memcmp ( loc - > interp_elf_ex . e_ident , ELFMAG , SELFMAG ) ! = 0 )
2005-04-17 02:20:36 +04:00
goto out_free_dentry ;
/* Verify the interpreter has a valid arch */
2008-02-08 15:21:54 +03:00
if ( ! elf_check_arch ( & loc - > interp_elf_ex ) )
2005-04-17 02:20:36 +04:00
goto out_free_dentry ;
}
/* Flush all traces of the currently running executable */
retval = flush_old_exec ( bprm ) ;
if ( retval )
goto out_free_dentry ;
/* OK, This is the point of no return */
current - > mm - > def_flags = def_flags ;
/* Do this immediately, since STACK_TOP as used in setup_arg_pages
may depend on the personality . */
2008-10-16 17:39:57 +04:00
SET_PERSONALITY ( loc - > elf_ex ) ;
2005-04-17 02:20:36 +04:00
if ( elf_read_implies_exec ( loc - > elf_ex , executable_stack ) )
current - > personality | = READ_IMPLIES_EXEC ;
2006-06-23 13:05:35 +04:00
if ( ! ( current - > personality & ADDR_NO_RANDOMIZE ) & & randomize_va_space )
2005-04-17 02:20:36 +04:00
current - > flags | = PF_RANDOMIZE ;
Split 'flush_old_exec' into two functions
'flush_old_exec()' is the point of no return when doing an execve(), and
it is pretty badly misnamed. It doesn't just flush the old executable
environment, it also starts up the new one.
Which is very inconvenient for things like setting up the new
personality, because we want the new personality to affect the starting
of the new environment, but at the same time we do _not_ want the new
personality to take effect if flushing the old one fails.
As a result, the x86-64 '32-bit' personality is actually done using this
insane "I'm going to change the ABI, but I haven't done it yet" bit
(TIF_ABI_PENDING), with SET_PERSONALITY() not actually setting the
personality, but just the "pending" bit, so that "flush_thread()" can do
the actual personality magic.
This patch in no way changes any of that insanity, but it does split the
'flush_old_exec()' function up into a preparatory part that can fail
(still called flush_old_exec()), and a new part that will actually set
up the new exec environment (setup_new_exec()). All callers are changed
to trivially comply with the new world order.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2010-01-29 09:14:42 +03:00
setup_new_exec ( bprm ) ;
2005-04-17 02:20:36 +04:00
/* Do this so that we can load the interpreter, if need be. We will
change some of these later */
current - > mm - > free_area_cache = current - > mm - > mmap_base ;
2005-06-22 04:14:49 +04:00
current - > mm - > cached_hole_size = 0 ;
2005-04-17 02:20:36 +04:00
retval = setup_arg_pages ( bprm , randomize_stack_top ( STACK_TOP ) ,
executable_stack ) ;
if ( retval < 0 ) {
send_sig ( SIGKILL , current , 0 ) ;
goto out_free_dentry ;
}
current - > mm - > start_stack = bprm - > p ;
tree-wide: fix assorted typos all over the place
That is "success", "unknown", "through", "performance", "[re|un]mapping"
, "access", "default", "reasonable", "[con]currently", "temperature"
, "channel", "[un]used", "application", "example","hierarchy", "therefore"
, "[over|under]flow", "contiguous", "threshold", "enough" and others.
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2009-11-14 18:09:05 +03:00
/* Now we do a little grungy work by mmapping the ELF image into
2008-01-30 15:31:07 +03:00
the correct location in memory . */
2006-06-23 13:05:35 +04:00
for ( i = 0 , elf_ppnt = elf_phdata ;
i < loc - > elf_ex . e_phnum ; i + + , elf_ppnt + + ) {
2005-04-17 02:20:36 +04:00
int elf_prot = 0 , elf_flags ;
unsigned long k , vaddr ;
if ( elf_ppnt - > p_type ! = PT_LOAD )
continue ;
if ( unlikely ( elf_brk > elf_bss ) ) {
unsigned long nbyte ;
/* There was a PT_LOAD segment with p_memsz > p_filesz
before this one . Map anonymous pages , if needed ,
and clear the area . */
2011-01-13 04:00:02 +03:00
retval = set_brk ( elf_bss + load_bias ,
elf_brk + load_bias ) ;
2005-04-17 02:20:36 +04:00
if ( retval ) {
send_sig ( SIGKILL , current , 0 ) ;
goto out_free_dentry ;
}
nbyte = ELF_PAGEOFFSET ( elf_bss ) ;
if ( nbyte ) {
nbyte = ELF_MIN_ALIGN - nbyte ;
if ( nbyte > elf_brk - elf_bss )
nbyte = elf_brk - elf_bss ;
if ( clear_user ( ( void __user * ) elf_bss +
load_bias , nbyte ) ) {
/*
* This bss - zeroing can fail if the ELF
2006-06-23 13:05:35 +04:00
* file specifies odd protections . So
2005-04-17 02:20:36 +04:00
* we don ' t check the return value
*/
}
}
}
2006-06-23 13:05:35 +04:00
if ( elf_ppnt - > p_flags & PF_R )
elf_prot | = PROT_READ ;
if ( elf_ppnt - > p_flags & PF_W )
elf_prot | = PROT_WRITE ;
if ( elf_ppnt - > p_flags & PF_X )
elf_prot | = PROT_EXEC ;
2005-04-17 02:20:36 +04:00
2006-06-23 13:05:35 +04:00
elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE ;
2005-04-17 02:20:36 +04:00
vaddr = elf_ppnt - > p_vaddr ;
if ( loc - > elf_ex . e_type = = ET_EXEC | | load_addr_set ) {
elf_flags | = MAP_FIXED ;
} else if ( loc - > elf_ex . e_type = = ET_DYN ) {
2006-06-23 13:05:35 +04:00
/* Try and get dynamic programs out of the way of the
* default mmap base , as well as whatever program they
* might try to exec . This is because the brk will
* follow the loader , and is not movable . */
2012-01-11 03:10:21 +04:00
# ifdef CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE
2011-11-03 00:37:41 +04:00
/* Memory randomization might have been switched off
* in runtime via sysctl .
* If that is the case , retain the original non - zero
* load_bias value in order to establish proper
* non - randomized mappings .
*/
if ( current - > flags & PF_RANDOMIZE )
load_bias = 0 ;
else
load_bias = ELF_PAGESTART ( ELF_ET_DYN_BASE - vaddr ) ;
2008-01-30 15:31:07 +03:00
# else
2007-01-07 00:28:21 +03:00
load_bias = ELF_PAGESTART ( ELF_ET_DYN_BASE - vaddr ) ;
2008-01-30 15:31:07 +03:00
# endif
2005-04-17 02:20:36 +04:00
}
2006-06-23 13:05:35 +04:00
error = elf_map ( bprm - > file , load_bias + vaddr , elf_ppnt ,
x86: PIE executable randomization, checkpatch fixes
#39: FILE: arch/ia64/ia32/binfmt_elf32.c:229:
+elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type, unsigned long unused)
WARNING: no space between function name and open parenthesis '('
#39: FILE: arch/ia64/ia32/binfmt_elf32.c:229:
+elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type, unsigned long unused)
WARNING: line over 80 characters
#67: FILE: arch/x86/kernel/sys_x86_64.c:80:
+ new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
ERROR: use tabs not spaces
#110: FILE: arch/x86/kernel/sys_x86_64.c:185:
+ ^I mm->cached_hole_size = 0;$
ERROR: use tabs not spaces
#111: FILE: arch/x86/kernel/sys_x86_64.c:186:
+ ^I^Imm->free_area_cache = mm->mmap_base;$
ERROR: use tabs not spaces
#112: FILE: arch/x86/kernel/sys_x86_64.c:187:
+ ^I}$
ERROR: use tabs not spaces
#141: FILE: arch/x86/kernel/sys_x86_64.c:216:
+ ^I^I/* remember the largest hole we saw so far */$
ERROR: use tabs not spaces
#142: FILE: arch/x86/kernel/sys_x86_64.c:217:
+ ^I^Iif (addr + mm->cached_hole_size < vma->vm_start)$
ERROR: use tabs not spaces
#143: FILE: arch/x86/kernel/sys_x86_64.c:218:
+ ^I^I mm->cached_hole_size = vma->vm_start - addr;$
ERROR: use tabs not spaces
#157: FILE: arch/x86/kernel/sys_x86_64.c:232:
+ ^Imm->free_area_cache = TASK_UNMAPPED_BASE;$
ERROR: need a space before the open parenthesis '('
#291: FILE: arch/x86/mm/mmap_64.c:101:
+ } else if(mmap_is_legacy()) {
WARNING: braces {} are not necessary for single statement blocks
#302: FILE: arch/x86/mm/mmap_64.c:112:
+ if (current->flags & PF_RANDOMIZE) {
+ mm->mmap_base += ((long)rnd) << PAGE_SHIFT;
+ }
WARNING: line over 80 characters
#314: FILE: fs/binfmt_elf.c:48:
+static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int, unsigned long);
WARNING: no space between function name and open parenthesis '('
#314: FILE: fs/binfmt_elf.c:48:
+static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int, unsigned long);
WARNING: line over 80 characters
#429: FILE: fs/binfmt_elf.c:438:
+ eppnt, elf_prot, elf_type, total_size);
ERROR: need space after that ',' (ctx:VxV)
#480: FILE: fs/binfmt_elf.c:939:
+ elf_prot, elf_flags,0);
^
total: 9 errors, 7 warnings, 461 lines checked
Your patch has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
Please run checkpatch prior to sending patches
Cc: "Luck, Tony" <tony.luck@intel.com>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Roland McGrath <roland@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-01-30 15:31:07 +03:00
elf_prot , elf_flags , 0 ) ;
2005-04-17 02:20:36 +04:00
if ( BAD_ADDR ( error ) ) {
send_sig ( SIGKILL , current , 0 ) ;
2007-05-08 11:31:57 +04:00
retval = IS_ERR ( ( void * ) error ) ?
PTR_ERR ( ( void * ) error ) : - EINVAL ;
2005-04-17 02:20:36 +04:00
goto out_free_dentry ;
}
if ( ! load_addr_set ) {
load_addr_set = 1 ;
load_addr = ( elf_ppnt - > p_vaddr - elf_ppnt - > p_offset ) ;
if ( loc - > elf_ex . e_type = = ET_DYN ) {
load_bias + = error -
ELF_PAGESTART ( load_bias + vaddr ) ;
load_addr + = load_bias ;
reloc_func_desc = load_bias ;
}
}
k = elf_ppnt - > p_vaddr ;
2006-06-23 13:05:35 +04:00
if ( k < start_code )
start_code = k ;
if ( start_data < k )
start_data = k ;
2005-04-17 02:20:36 +04:00
/*
* Check to see if the section ' s size will overflow the
* allowed task size . Note that p_filesz must always be
* < = p_memsz so it is only necessary to check p_memsz .
*/
[PATCH] binfmt_elf: fix checks for bad address
Fix check for bad address; use macro instead of open-coding two checks.
Taken from RHEL4 kernel update.
From: Ernie Petrides <petrides@redhat.com>
For background, the BAD_ADDR() macro should return TRUE if the address is
TASK_SIZE, because that's the lowest address that is *not* valid for
user-space mappings. The macro was correct in binfmt_aout.c but was wrong
for the "equal to" case in binfmt_elf.c. There were two in-line validations
of user-space addresses in binfmt_elf.c, which have been appropriately
converted to use the corrected BAD_ADDR() macro in the patch you posted
yesterday. Note that the size checks against TASK_SIZE are okay as coded.
The additional changes that I propose are below. These are in the error
paths for bad ELF entry addresses once load_elf_binary() has already
committed to exec'ing the new image (following the tearing down of the
task's original address space).
The 1st hunk deals with the interp-side of the outer "if". There were two
problems here. The printk() should be removed because this path can be
triggered at will by a bogus interpreter image created and used by a
malicious user. Further, the error code should not be ENOEXEC, because that
causes the loop in search_binary_handler() to continue trying other exec
handlers (twice, in fact). But it's too late for this to work correctly,
because the user address space has already been torn down, and an exec()
failure cannot be returned to the user code because the code no longer
exists. The only recovery is to force a SIGSEGV, but it's best to terminate
the search loop immediately. I somewhat arbitrarily chose EINVAL as a
fallback error code, but any error returned by load_elf_interp() will
override that (but this value will never be seen by user-space).
The 2nd hunk deals with the non-interp-side of the outer "if". There were
two problems here as well. The SIGSEGV needs to be forced, because a prior
sigaction() syscall might have set the associated disposition to SIG_IGN.
And the ENOEXEC should be changed to EINVAL as described above.
Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Signed-off-by: Ernie Petrides <petrides@redhat.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-07-03 11:24:14 +04:00
if ( BAD_ADDR ( k ) | | elf_ppnt - > p_filesz > elf_ppnt - > p_memsz | |
2005-04-17 02:20:36 +04:00
elf_ppnt - > p_memsz > TASK_SIZE | |
TASK_SIZE - elf_ppnt - > p_memsz < k ) {
2006-06-23 13:05:35 +04:00
/* set_brk can never work. Avoid overflows. */
2005-04-17 02:20:36 +04:00
send_sig ( SIGKILL , current , 0 ) ;
2007-05-08 11:31:57 +04:00
retval = - EINVAL ;
2005-04-17 02:20:36 +04:00
goto out_free_dentry ;
}
k = elf_ppnt - > p_vaddr + elf_ppnt - > p_filesz ;
if ( k > elf_bss )
elf_bss = k ;
if ( ( elf_ppnt - > p_flags & PF_X ) & & end_code < k )
end_code = k ;
if ( end_data < k )
end_data = k ;
k = elf_ppnt - > p_vaddr + elf_ppnt - > p_memsz ;
if ( k > elf_brk )
elf_brk = k ;
}
loc - > elf_ex . e_entry + = load_bias ;
elf_bss + = load_bias ;
elf_brk + = load_bias ;
start_code + = load_bias ;
end_code + = load_bias ;
start_data + = load_bias ;
end_data + = load_bias ;
/* Calling set_brk effectively mmaps the pages that we need
* for the bss and break sections . We must do this before
* mapping in the interpreter , to make sure it doesn ' t wind
* up getting placed where the bss needs to go .
*/
retval = set_brk ( elf_bss , elf_brk ) ;
if ( retval ) {
send_sig ( SIGKILL , current , 0 ) ;
goto out_free_dentry ;
}
2005-10-11 19:29:08 +04:00
if ( likely ( elf_bss ! = elf_brk ) & & unlikely ( padzero ( elf_bss ) ) ) {
2005-04-17 02:20:36 +04:00
send_sig ( SIGSEGV , current , 0 ) ;
retval = - EFAULT ; /* Nobody gets to see this, but.. */
goto out_free_dentry ;
}
if ( elf_interpreter ) {
2008-02-08 15:21:54 +03:00
unsigned long uninitialized_var ( interp_map_addr ) ;
elf_entry = load_elf_interp ( & loc - > interp_elf_ex ,
interpreter ,
& interp_map_addr ,
load_bias ) ;
if ( ! IS_ERR ( ( void * ) elf_entry ) ) {
/*
* load_elf_interp ( ) returns relocation
* adjustment
*/
interp_load_addr = elf_entry ;
elf_entry + = loc - > interp_elf_ex . e_entry ;
2008-01-30 15:31:07 +03:00
}
2005-04-17 02:20:36 +04:00
if ( BAD_ADDR ( elf_entry ) ) {
force_sig ( SIGSEGV , current ) ;
[PATCH] binfmt_elf: fix checks for bad address
Fix check for bad address; use macro instead of open-coding two checks.
Taken from RHEL4 kernel update.
From: Ernie Petrides <petrides@redhat.com>
For background, the BAD_ADDR() macro should return TRUE if the address is
TASK_SIZE, because that's the lowest address that is *not* valid for
user-space mappings. The macro was correct in binfmt_aout.c but was wrong
for the "equal to" case in binfmt_elf.c. There were two in-line validations
of user-space addresses in binfmt_elf.c, which have been appropriately
converted to use the corrected BAD_ADDR() macro in the patch you posted
yesterday. Note that the size checks against TASK_SIZE are okay as coded.
The additional changes that I propose are below. These are in the error
paths for bad ELF entry addresses once load_elf_binary() has already
committed to exec'ing the new image (following the tearing down of the
task's original address space).
The 1st hunk deals with the interp-side of the outer "if". There were two
problems here. The printk() should be removed because this path can be
triggered at will by a bogus interpreter image created and used by a
malicious user. Further, the error code should not be ENOEXEC, because that
causes the loop in search_binary_handler() to continue trying other exec
handlers (twice, in fact). But it's too late for this to work correctly,
because the user address space has already been torn down, and an exec()
failure cannot be returned to the user code because the code no longer
exists. The only recovery is to force a SIGSEGV, but it's best to terminate
the search loop immediately. I somewhat arbitrarily chose EINVAL as a
fallback error code, but any error returned by load_elf_interp() will
override that (but this value will never be seen by user-space).
The 2nd hunk deals with the non-interp-side of the outer "if". There were
two problems here as well. The SIGSEGV needs to be forced, because a prior
sigaction() syscall might have set the associated disposition to SIG_IGN.
And the ENOEXEC should be changed to EINVAL as described above.
Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Signed-off-by: Ernie Petrides <petrides@redhat.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-07-03 11:24:14 +04:00
retval = IS_ERR ( ( void * ) elf_entry ) ?
( int ) elf_entry : - EINVAL ;
2005-04-17 02:20:36 +04:00
goto out_free_dentry ;
}
reloc_func_desc = interp_load_addr ;
allow_write_access ( interpreter ) ;
fput ( interpreter ) ;
kfree ( elf_interpreter ) ;
} else {
elf_entry = loc - > elf_ex . e_entry ;
2006-02-26 06:18:28 +03:00
if ( BAD_ADDR ( elf_entry ) ) {
[PATCH] binfmt_elf: fix checks for bad address
Fix check for bad address; use macro instead of open-coding two checks.
Taken from RHEL4 kernel update.
From: Ernie Petrides <petrides@redhat.com>
For background, the BAD_ADDR() macro should return TRUE if the address is
TASK_SIZE, because that's the lowest address that is *not* valid for
user-space mappings. The macro was correct in binfmt_aout.c but was wrong
for the "equal to" case in binfmt_elf.c. There were two in-line validations
of user-space addresses in binfmt_elf.c, which have been appropriately
converted to use the corrected BAD_ADDR() macro in the patch you posted
yesterday. Note that the size checks against TASK_SIZE are okay as coded.
The additional changes that I propose are below. These are in the error
paths for bad ELF entry addresses once load_elf_binary() has already
committed to exec'ing the new image (following the tearing down of the
task's original address space).
The 1st hunk deals with the interp-side of the outer "if". There were two
problems here. The printk() should be removed because this path can be
triggered at will by a bogus interpreter image created and used by a
malicious user. Further, the error code should not be ENOEXEC, because that
causes the loop in search_binary_handler() to continue trying other exec
handlers (twice, in fact). But it's too late for this to work correctly,
because the user address space has already been torn down, and an exec()
failure cannot be returned to the user code because the code no longer
exists. The only recovery is to force a SIGSEGV, but it's best to terminate
the search loop immediately. I somewhat arbitrarily chose EINVAL as a
fallback error code, but any error returned by load_elf_interp() will
override that (but this value will never be seen by user-space).
The 2nd hunk deals with the non-interp-side of the outer "if". There were
two problems here as well. The SIGSEGV needs to be forced, because a prior
sigaction() syscall might have set the associated disposition to SIG_IGN.
And the ENOEXEC should be changed to EINVAL as described above.
Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Signed-off-by: Ernie Petrides <petrides@redhat.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-07-03 11:24:14 +04:00
force_sig ( SIGSEGV , current ) ;
retval = - EINVAL ;
2006-02-26 06:18:28 +03:00
goto out_free_dentry ;
}
2005-04-17 02:20:36 +04:00
}
kfree ( elf_phdata ) ;
set_binfmt ( & elf_format ) ;
2005-04-17 02:24:35 +04:00
# ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
2008-12-25 15:38:35 +03:00
retval = arch_setup_additional_pages ( bprm , ! ! elf_interpreter ) ;
2005-04-17 02:24:35 +04:00
if ( retval < 0 ) {
send_sig ( SIGKILL , current , 0 ) ;
2005-04-29 02:17:19 +04:00
goto out ;
2005-04-17 02:24:35 +04:00
}
# endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
CRED: Make execve() take advantage of copy-on-write credentials
Make execve() take advantage of copy-on-write credentials, allowing it to set
up the credentials in advance, and then commit the whole lot after the point
of no return.
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
The credential bits from struct linux_binprm are, for the most part,
replaced with a single credentials pointer (bprm->cred). This means that
all the creds can be calculated in advance and then applied at the point
of no return with no possibility of failure.
I would like to replace bprm->cap_effective with:
cap_isclear(bprm->cap_effective)
but this seems impossible due to special behaviour for processes of pid 1
(they always retain their parent's capability masks where normally they'd
be changed - see cap_bprm_set_creds()).
The following sequence of events now happens:
(a) At the start of do_execve, the current task's cred_exec_mutex is
locked to prevent PTRACE_ATTACH from obsoleting the calculation of
creds that we make.
(a) prepare_exec_creds() is then called to make a copy of the current
task's credentials and prepare it. This copy is then assigned to
bprm->cred.
This renders security_bprm_alloc() and security_bprm_free()
unnecessary, and so they've been removed.
(b) The determination of unsafe execution is now performed immediately
after (a) rather than later on in the code. The result is stored in
bprm->unsafe for future reference.
(c) prepare_binprm() is called, possibly multiple times.
(i) This applies the result of set[ug]id binaries to the new creds
attached to bprm->cred. Personality bit clearance is recorded,
but now deferred on the basis that the exec procedure may yet
fail.
(ii) This then calls the new security_bprm_set_creds(). This should
calculate the new LSM and capability credentials into *bprm->cred.
This folds together security_bprm_set() and parts of
security_bprm_apply_creds() (these two have been removed).
Anything that might fail must be done at this point.
(iii) bprm->cred_prepared is set to 1.
bprm->cred_prepared is 0 on the first pass of the security
calculations, and 1 on all subsequent passes. This allows SELinux
in (ii) to base its calculations only on the initial script and
not on the interpreter.
(d) flush_old_exec() is called to commit the task to execution. This
performs the following steps with regard to credentials:
(i) Clear pdeath_signal and set dumpable on certain circumstances that
may not be covered by commit_creds().
(ii) Clear any bits in current->personality that were deferred from
(c.i).
(e) install_exec_creds() [compute_creds() as was] is called to install the
new credentials. This performs the following steps with regard to
credentials:
(i) Calls security_bprm_committing_creds() to apply any security
requirements, such as flushing unauthorised files in SELinux, that
must be done before the credentials are changed.
This is made up of bits of security_bprm_apply_creds() and
security_bprm_post_apply_creds(), both of which have been removed.
This function is not allowed to fail; anything that might fail
must have been done in (c.ii).
(ii) Calls commit_creds() to apply the new credentials in a single
assignment (more or less). Possibly pdeath_signal and dumpable
should be part of struct creds.
(iii) Unlocks the task's cred_replace_mutex, thus allowing
PTRACE_ATTACH to take place.
(iv) Clears The bprm->cred pointer as the credentials it was holding
are now immutable.
(v) Calls security_bprm_committed_creds() to apply any security
alterations that must be done after the creds have been changed.
SELinux uses this to flush signals and signal handlers.
(f) If an error occurs before (d.i), bprm_free() will call abort_creds()
to destroy the proposed new credentials and will then unlock
cred_replace_mutex. No changes to the credentials will have been
made.
(2) LSM interface.
A number of functions have been changed, added or removed:
(*) security_bprm_alloc(), ->bprm_alloc_security()
(*) security_bprm_free(), ->bprm_free_security()
Removed in favour of preparing new credentials and modifying those.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
(*) security_bprm_post_apply_creds(), ->bprm_post_apply_creds()
Removed; split between security_bprm_set_creds(),
security_bprm_committing_creds() and security_bprm_committed_creds().
(*) security_bprm_set(), ->bprm_set_security()
Removed; folded into security_bprm_set_creds().
(*) security_bprm_set_creds(), ->bprm_set_creds()
New. The new credentials in bprm->creds should be checked and set up
as appropriate. bprm->cred_prepared is 0 on the first call, 1 on the
second and subsequent calls.
(*) security_bprm_committing_creds(), ->bprm_committing_creds()
(*) security_bprm_committed_creds(), ->bprm_committed_creds()
New. Apply the security effects of the new credentials. This
includes closing unauthorised files in SELinux. This function may not
fail. When the former is called, the creds haven't yet been applied
to the process; when the latter is called, they have.
The former may access bprm->cred, the latter may not.
(3) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) The bprm_security_struct struct has been removed in favour of using
the credentials-under-construction approach.
(c) flush_unauthorized_files() now takes a cred pointer and passes it on
to inode_has_perm(), file_has_perm() and dentry_open().
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-14 02:39:24 +03:00
install_exec_creds ( bprm ) ;
2007-07-19 12:48:16 +04:00
retval = create_elf_tables ( bprm , & loc - > elf_ex ,
2006-06-23 13:05:35 +04:00
load_addr , interp_load_addr ) ;
2007-07-19 12:48:16 +04:00
if ( retval < 0 ) {
send_sig ( SIGKILL , current , 0 ) ;
goto out ;
}
2005-04-17 02:20:36 +04:00
/* N.B. passed_fileno might not be initialized? */
current - > mm - > end_code = end_code ;
current - > mm - > start_code = start_code ;
current - > mm - > start_data = start_data ;
current - > mm - > end_data = end_data ;
current - > mm - > start_stack = bprm - > p ;
2008-01-30 15:30:40 +03:00
# ifdef arch_randomize_brk
2011-04-15 02:22:09 +04:00
if ( ( current - > flags & PF_RANDOMIZE ) & & ( randomize_va_space > 1 ) ) {
2008-01-30 15:30:40 +03:00
current - > mm - > brk = current - > mm - > start_brk =
arch_randomize_brk ( current - > mm ) ;
2011-04-15 02:22:09 +04:00
# ifdef CONFIG_COMPAT_BRK
current - > brk_randomized = 1 ;
# endif
}
2008-01-30 15:30:40 +03:00
# endif
2005-04-17 02:20:36 +04:00
if ( current - > personality & MMAP_PAGE_ZERO ) {
/* Why this, you ask??? Well SVr4 maps page 0 as read-only,
and some applications " depend " upon this behavior .
Since we do not have the power to recompile these , we
2006-06-23 13:05:35 +04:00
emulate the SVr4 behavior . Sigh . */
2012-04-21 04:13:58 +04:00
error = vm_mmap ( NULL , 0 , PAGE_SIZE , PROT_READ | PROT_EXEC ,
2005-04-17 02:20:36 +04:00
MAP_FIXED | MAP_PRIVATE , 0 ) ;
}
# ifdef ELF_PLAT_INIT
/*
* The ABI may specify that certain registers be set up in special
* ways ( on i386 % edx is the address of a DT_FINI function , for
* example . In addition , it may also specify ( eg , PowerPC64 ELF )
* that the e_entry field is the address of the function descriptor
* for the startup routine , rather than the address of the startup
* routine itself . This macro performs whatever initialization to
* the regs structure is required as well as any relocations to the
* function descriptor entries when executing dynamically links apps .
*/
ELF_PLAT_INIT ( regs , reloc_func_desc ) ;
# endif
start_thread ( regs , elf_entry , bprm - > p ) ;
retval = 0 ;
out :
kfree ( loc ) ;
out_ret :
return retval ;
/* error cleanup */
out_free_dentry :
allow_write_access ( interpreter ) ;
if ( interpreter )
fput ( interpreter ) ;
out_free_interp :
2005-11-07 12:01:34 +03:00
kfree ( elf_interpreter ) ;
2005-04-17 02:20:36 +04:00
out_free_ph :
kfree ( elf_phdata ) ;
goto out ;
}
/* This is really simpleminded and specialized - we are loading an
a . out library that is given an ELF header . */
static int load_elf_library ( struct file * file )
{
struct elf_phdr * elf_phdata ;
struct elf_phdr * eppnt ;
unsigned long elf_bss , bss , len ;
int retval , error , i , j ;
struct elfhdr elf_ex ;
error = - ENOEXEC ;
2006-06-23 13:05:35 +04:00
retval = kernel_read ( file , 0 , ( char * ) & elf_ex , sizeof ( elf_ex ) ) ;
2005-04-17 02:20:36 +04:00
if ( retval ! = sizeof ( elf_ex ) )
goto out ;
if ( memcmp ( elf_ex . e_ident , ELFMAG , SELFMAG ) ! = 0 )
goto out ;
/* First of all, some simple consistency checks */
if ( elf_ex . e_type ! = ET_EXEC | | elf_ex . e_phnum > 2 | |
2006-06-23 13:05:35 +04:00
! elf_check_arch ( & elf_ex ) | | ! file - > f_op | | ! file - > f_op - > mmap )
2005-04-17 02:20:36 +04:00
goto out ;
/* Now read in all of the header information */
j = sizeof ( struct elf_phdr ) * elf_ex . e_phnum ;
/* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
error = - ENOMEM ;
elf_phdata = kmalloc ( j , GFP_KERNEL ) ;
if ( ! elf_phdata )
goto out ;
eppnt = elf_phdata ;
error = - ENOEXEC ;
retval = kernel_read ( file , elf_ex . e_phoff , ( char * ) eppnt , j ) ;
if ( retval ! = j )
goto out_free_ph ;
for ( j = 0 , i = 0 ; i < elf_ex . e_phnum ; i + + )
if ( ( eppnt + i ) - > p_type = = PT_LOAD )
j + + ;
if ( j ! = 1 )
goto out_free_ph ;
while ( eppnt - > p_type ! = PT_LOAD )
eppnt + + ;
/* Now use mmap to map the library into memory. */
2012-04-21 04:13:58 +04:00
error = vm_mmap ( file ,
2005-04-17 02:20:36 +04:00
ELF_PAGESTART ( eppnt - > p_vaddr ) ,
( eppnt - > p_filesz +
ELF_PAGEOFFSET ( eppnt - > p_vaddr ) ) ,
PROT_READ | PROT_WRITE | PROT_EXEC ,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE ,
( eppnt - > p_offset -
ELF_PAGEOFFSET ( eppnt - > p_vaddr ) ) ) ;
if ( error ! = ELF_PAGESTART ( eppnt - > p_vaddr ) )
goto out_free_ph ;
elf_bss = eppnt - > p_vaddr + eppnt - > p_filesz ;
if ( padzero ( elf_bss ) ) {
error = - EFAULT ;
goto out_free_ph ;
}
2006-06-23 13:05:35 +04:00
len = ELF_PAGESTART ( eppnt - > p_filesz + eppnt - > p_vaddr +
ELF_MIN_ALIGN - 1 ) ;
2005-04-17 02:20:36 +04:00
bss = eppnt - > p_memsz + eppnt - > p_vaddr ;
2012-04-21 02:35:40 +04:00
if ( bss > len )
vm_brk ( len , bss - len ) ;
2005-04-17 02:20:36 +04:00
error = 0 ;
out_free_ph :
kfree ( elf_phdata ) ;
out :
return error ;
}
2009-12-16 03:47:37 +03:00
# ifdef CONFIG_ELF_CORE
2005-04-17 02:20:36 +04:00
/*
* ELF core dumper
*
* Modelled on fs / exec . c : aout_core_dump ( )
* Jeremy Fitzhardinge < jeremy @ sw . oz . au >
*/
coredump: remove VM_ALWAYSDUMP flag
The motivation for this patchset was that I was looking at a way for a
qemu-kvm process, to exclude the guest memory from its core dump, which
can be quite large. There are already a number of filter flags in
/proc/<pid>/coredump_filter, however, these allow one to specify 'types'
of kernel memory, not specific address ranges (which is needed in this
case).
Since there are no more vma flags available, the first patch eliminates
the need for the 'VM_ALWAYSDUMP' flag. The flag is used internally by
the kernel to mark vdso and vsyscall pages. However, it is simple
enough to check if a vma covers a vdso or vsyscall page without the need
for this flag.
The second patch then replaces the 'VM_ALWAYSDUMP' flag with a new
'VM_NODUMP' flag, which can be set by userspace using new madvise flags:
'MADV_DONTDUMP', and unset via 'MADV_DODUMP'. The core dump filters
continue to work the same as before unless 'MADV_DONTDUMP' is set on the
region.
The qemu code which implements this features is at:
http://people.redhat.com/~jbaron/qemu-dump/qemu-dump.patch
In my testing the qemu core dump shrunk from 383MB -> 13MB with this
patch.
I also believe that the 'MADV_DONTDUMP' flag might be useful for
security sensitive apps, which might want to select which areas are
dumped.
This patch:
The VM_ALWAYSDUMP flag is currently used by the coredump code to
indicate that a vma is part of a vsyscall or vdso section. However, we
can determine if a vma is in one these sections by checking it against
the gate_vma and checking for a non-NULL return value from
arch_vma_name(). Thus, freeing a valuable vma bit.
Signed-off-by: Jason Baron <jbaron@redhat.com>
Acked-by: Roland McGrath <roland@hack.frob.com>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Avi Kivity <avi@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-03-24 02:02:51 +04: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 ;
/*
* arch_vma_name ( ) returns non - NULL for special architecture mappings ,
* such as vDSO sections .
*/
if ( arch_vma_name ( vma ) )
return true ;
return false ;
}
2005-04-17 02:20:36 +04:00
/*
2007-10-17 10:27:02 +04:00
* Decide what to dump of a segment , part , all or none .
2005-04-17 02:20:36 +04:00
*/
2007-10-17 10:27:02 +04:00
static unsigned long vma_dump_size ( struct vm_area_struct * vma ,
unsigned long mm_flags )
2005-04-17 02:20:36 +04:00
{
coredump_filter: add hugepage dumping
Presently hugepage's vma has a VM_RESERVED flag in order not to be
swapped. But a VM_RESERVED vma isn't core dumped because this flag is
often used for some kernel vmas (e.g. vmalloc, sound related).
Thus hugepages are never dumped and it can't be debugged easily. Many
developers want hugepages to be included into core-dump.
However, We can't read generic VM_RESERVED area because this area is often
IO mapping area. then these area reading may change device state. it is
definitly undesiable side-effect.
So adding a hugepage specific bit to the coredump filter is better. It
will be able to hugepage core dumping and doesn't cause any side-effect to
any i/o devices.
In additional, libhugetlb use hugetlb private mapping pages as anonymous
page. Then, hugepage private mapping pages should be core dumped by
default.
Then, /proc/[pid]/core_dump_filter has two new bits.
- bit 5 mean hugetlb private mapping pages are dumped or not. (default: yes)
- bit 6 mean hugetlb shared mapping pages are dumped or not. (default: no)
I tested by following method.
% ulimit -c unlimited
% ./crash_hugepage 50
% ./crash_hugepage 50 -p
% ls -lh
% gdb ./crash_hugepage core
%
% echo 0x43 > /proc/self/coredump_filter
% ./crash_hugepage 50
% ./crash_hugepage 50 -p
% ls -lh
% gdb ./crash_hugepage core
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include "hugetlbfs.h"
int main(int argc, char** argv){
char* p;
int ch;
int mmap_flags = MAP_SHARED;
int fd;
int nr_pages;
while((ch = getopt(argc, argv, "p")) != -1) {
switch (ch) {
case 'p':
mmap_flags &= ~MAP_SHARED;
mmap_flags |= MAP_PRIVATE;
break;
default:
/* nothing*/
break;
}
}
argc -= optind;
argv += optind;
if (argc == 0){
printf("need # of pages\n");
exit(1);
}
nr_pages = atoi(argv[0]);
if (nr_pages < 2) {
printf("nr_pages must >2\n");
exit(1);
}
fd = hugetlbfs_unlinked_fd();
p = mmap(NULL, nr_pages * gethugepagesize(),
PROT_READ|PROT_WRITE, mmap_flags, fd, 0);
sleep(2);
*(p + gethugepagesize()) = 1; /* COW */
sleep(2);
/* crash! */
*(int*)0 = 1;
return 0;
}
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Reviewed-by: Kawai Hidehiro <hidehiro.kawai.ez@hitachi.com>
Cc: Hugh Dickins <hugh@veritas.com>
Cc: William Irwin <wli@holomorphy.com>
Cc: Adam Litke <agl@us.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 07:27:08 +04:00
# define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
coredump: remove VM_ALWAYSDUMP flag
The motivation for this patchset was that I was looking at a way for a
qemu-kvm process, to exclude the guest memory from its core dump, which
can be quite large. There are already a number of filter flags in
/proc/<pid>/coredump_filter, however, these allow one to specify 'types'
of kernel memory, not specific address ranges (which is needed in this
case).
Since there are no more vma flags available, the first patch eliminates
the need for the 'VM_ALWAYSDUMP' flag. The flag is used internally by
the kernel to mark vdso and vsyscall pages. However, it is simple
enough to check if a vma covers a vdso or vsyscall page without the need
for this flag.
The second patch then replaces the 'VM_ALWAYSDUMP' flag with a new
'VM_NODUMP' flag, which can be set by userspace using new madvise flags:
'MADV_DONTDUMP', and unset via 'MADV_DODUMP'. The core dump filters
continue to work the same as before unless 'MADV_DONTDUMP' is set on the
region.
The qemu code which implements this features is at:
http://people.redhat.com/~jbaron/qemu-dump/qemu-dump.patch
In my testing the qemu core dump shrunk from 383MB -> 13MB with this
patch.
I also believe that the 'MADV_DONTDUMP' flag might be useful for
security sensitive apps, which might want to select which areas are
dumped.
This patch:
The VM_ALWAYSDUMP flag is currently used by the coredump code to
indicate that a vma is part of a vsyscall or vdso section. However, we
can determine if a vma is in one these sections by checking it against
the gate_vma and checking for a non-NULL return value from
arch_vma_name(). Thus, freeing a valuable vma bit.
Signed-off-by: Jason Baron <jbaron@redhat.com>
Acked-by: Roland McGrath <roland@hack.frob.com>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Avi Kivity <avi@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-03-24 02:02:51 +04:00
/* always dump the vdso and vsyscall sections */
if ( always_dump_vma ( vma ) )
2007-10-17 10:27:02 +04:00
goto whole ;
2007-01-26 11:56:48 +03:00
2012-03-24 02:02:51 +04:00
if ( vma - > vm_flags & VM_NODUMP )
return 0 ;
coredump_filter: add hugepage dumping
Presently hugepage's vma has a VM_RESERVED flag in order not to be
swapped. But a VM_RESERVED vma isn't core dumped because this flag is
often used for some kernel vmas (e.g. vmalloc, sound related).
Thus hugepages are never dumped and it can't be debugged easily. Many
developers want hugepages to be included into core-dump.
However, We can't read generic VM_RESERVED area because this area is often
IO mapping area. then these area reading may change device state. it is
definitly undesiable side-effect.
So adding a hugepage specific bit to the coredump filter is better. It
will be able to hugepage core dumping and doesn't cause any side-effect to
any i/o devices.
In additional, libhugetlb use hugetlb private mapping pages as anonymous
page. Then, hugepage private mapping pages should be core dumped by
default.
Then, /proc/[pid]/core_dump_filter has two new bits.
- bit 5 mean hugetlb private mapping pages are dumped or not. (default: yes)
- bit 6 mean hugetlb shared mapping pages are dumped or not. (default: no)
I tested by following method.
% ulimit -c unlimited
% ./crash_hugepage 50
% ./crash_hugepage 50 -p
% ls -lh
% gdb ./crash_hugepage core
%
% echo 0x43 > /proc/self/coredump_filter
% ./crash_hugepage 50
% ./crash_hugepage 50 -p
% ls -lh
% gdb ./crash_hugepage core
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include "hugetlbfs.h"
int main(int argc, char** argv){
char* p;
int ch;
int mmap_flags = MAP_SHARED;
int fd;
int nr_pages;
while((ch = getopt(argc, argv, "p")) != -1) {
switch (ch) {
case 'p':
mmap_flags &= ~MAP_SHARED;
mmap_flags |= MAP_PRIVATE;
break;
default:
/* nothing*/
break;
}
}
argc -= optind;
argv += optind;
if (argc == 0){
printf("need # of pages\n");
exit(1);
}
nr_pages = atoi(argv[0]);
if (nr_pages < 2) {
printf("nr_pages must >2\n");
exit(1);
}
fd = hugetlbfs_unlinked_fd();
p = mmap(NULL, nr_pages * gethugepagesize(),
PROT_READ|PROT_WRITE, mmap_flags, fd, 0);
sleep(2);
*(p + gethugepagesize()) = 1; /* COW */
sleep(2);
/* crash! */
*(int*)0 = 1;
return 0;
}
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Reviewed-by: Kawai Hidehiro <hidehiro.kawai.ez@hitachi.com>
Cc: Hugh Dickins <hugh@veritas.com>
Cc: William Irwin <wli@holomorphy.com>
Cc: Adam Litke <agl@us.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 07:27:08 +04:00
/* Hugetlb memory check */
if ( vma - > vm_flags & VM_HUGETLB ) {
if ( ( vma - > vm_flags & VM_SHARED ) & & FILTER ( HUGETLB_SHARED ) )
goto whole ;
if ( ! ( vma - > vm_flags & VM_SHARED ) & & FILTER ( HUGETLB_PRIVATE ) )
goto whole ;
}
2005-04-17 02:20:36 +04:00
/* Do not dump I/O mapped devices or special mappings */
if ( vma - > vm_flags & ( VM_IO | VM_RESERVED ) )
return 0 ;
2007-07-19 12:48:29 +04:00
/* By default, dump shared memory if mapped from an anonymous file. */
if ( vma - > vm_flags & VM_SHARED ) {
2007-10-17 10:27:02 +04:00
if ( vma - > vm_file - > f_path . dentry - > d_inode - > i_nlink = = 0 ?
FILTER ( ANON_SHARED ) : FILTER ( MAPPED_SHARED ) )
goto whole ;
return 0 ;
2007-07-19 12:48:29 +04:00
}
2005-04-17 02:20:36 +04:00
2007-10-17 10:27:02 +04:00
/* Dump segments that have been written to. */
if ( vma - > anon_vma & & FILTER ( ANON_PRIVATE ) )
goto whole ;
if ( vma - > vm_file = = NULL )
return 0 ;
2005-04-17 02:20:36 +04:00
2007-10-17 10:27:02 +04:00
if ( FILTER ( MAPPED_PRIVATE ) )
goto whole ;
/*
* If this looks like the beginning of a DSO or executable mapping ,
* check for an ELF header . If we find one , dump the first page to
* aid in determining what was mapped here .
*/
2009-02-07 04:34:07 +03:00
if ( FILTER ( ELF_HEADERS ) & &
vma - > vm_pgoff = = 0 & & ( vma - > vm_flags & VM_READ ) ) {
2007-10-17 10:27:02 +04:00
u32 __user * header = ( u32 __user * ) vma - > vm_start ;
u32 word ;
2009-02-07 04:34:07 +03:00
mm_segment_t fs = get_fs ( ) ;
2007-10-17 10:27:02 +04:00
/*
* Doing it this way gets the constant folded by GCC .
*/
union {
u32 cmp ;
char elfmag [ SELFMAG ] ;
} magic ;
BUILD_BUG_ON ( SELFMAG ! = sizeof word ) ;
magic . elfmag [ EI_MAG0 ] = ELFMAG0 ;
magic . elfmag [ EI_MAG1 ] = ELFMAG1 ;
magic . elfmag [ EI_MAG2 ] = ELFMAG2 ;
magic . elfmag [ EI_MAG3 ] = ELFMAG3 ;
2009-02-07 04:34:07 +03:00
/*
* Switch to the user " segment " for get_user ( ) ,
* then put back what elf_core_dump ( ) had in place .
*/
set_fs ( USER_DS ) ;
if ( unlikely ( get_user ( word , header ) ) )
word = 0 ;
set_fs ( fs ) ;
if ( word = = magic . cmp )
2007-10-17 10:27:02 +04:00
return PAGE_SIZE ;
}
# undef FILTER
return 0 ;
whole :
return vma - > vm_end - vma - > vm_start ;
2005-04-17 02:20:36 +04:00
}
/* An ELF note in memory */
struct memelfnote
{
const char * name ;
int type ;
unsigned int datasz ;
void * data ;
} ;
static int notesize ( struct memelfnote * en )
{
int sz ;
sz = sizeof ( struct elf_note ) ;
sz + = roundup ( strlen ( en - > name ) + 1 , 4 ) ;
sz + = roundup ( en - > datasz , 4 ) ;
return sz ;
}
2006-10-01 10:29:28 +04:00
# define DUMP_WRITE(addr, nr, foffset) \
do { if ( ! dump_write ( file , ( addr ) , ( nr ) ) ) return 0 ; * foffset + = ( nr ) ; } while ( 0 )
2005-04-17 02:20:36 +04:00
2006-10-01 10:29:28 +04:00
static int alignfile ( struct file * file , loff_t * foffset )
2005-04-17 02:20:36 +04:00
{
2006-10-13 20:42:07 +04:00
static const char buf [ 4 ] = { 0 , } ;
2006-10-01 10:29:28 +04:00
DUMP_WRITE ( buf , roundup ( * foffset , 4 ) - * foffset , foffset ) ;
return 1 ;
}
2005-04-17 02:20:36 +04:00
2006-10-01 10:29:28 +04:00
static int writenote ( struct memelfnote * men , struct file * file ,
loff_t * foffset )
{
struct elf_note en ;
2005-04-17 02:20:36 +04:00
en . n_namesz = strlen ( men - > name ) + 1 ;
en . n_descsz = men - > datasz ;
en . n_type = men - > type ;
2006-10-01 10:29:28 +04:00
DUMP_WRITE ( & en , sizeof ( en ) , foffset ) ;
DUMP_WRITE ( men - > name , en . n_namesz , foffset ) ;
if ( ! alignfile ( file , foffset ) )
return 0 ;
DUMP_WRITE ( men - > data , men - > datasz , foffset ) ;
if ( ! alignfile ( file , foffset ) )
return 0 ;
2005-04-17 02:20:36 +04:00
return 1 ;
}
# undef DUMP_WRITE
2008-01-30 15:31:44 +03:00
static void fill_elf_header ( struct elfhdr * elf , int segs ,
u16 machine , u32 flags , u8 osabi )
2005-04-17 02:20:36 +04:00
{
2008-04-29 12:01:18 +04:00
memset ( elf , 0 , sizeof ( * elf ) ) ;
2005-04-17 02:20:36 +04:00
memcpy ( elf - > e_ident , ELFMAG , SELFMAG ) ;
elf - > e_ident [ EI_CLASS ] = ELF_CLASS ;
elf - > e_ident [ EI_DATA ] = ELF_DATA ;
elf - > e_ident [ EI_VERSION ] = EV_CURRENT ;
elf - > e_ident [ EI_OSABI ] = ELF_OSABI ;
elf - > e_type = ET_CORE ;
2008-01-30 15:31:44 +03:00
elf - > e_machine = machine ;
2005-04-17 02:20:36 +04:00
elf - > e_version = EV_CURRENT ;
elf - > e_phoff = sizeof ( struct elfhdr ) ;
2008-01-30 15:31:44 +03:00
elf - > e_flags = flags ;
2005-04-17 02:20:36 +04:00
elf - > e_ehsize = sizeof ( struct elfhdr ) ;
elf - > e_phentsize = sizeof ( struct elf_phdr ) ;
elf - > e_phnum = segs ;
2008-04-29 12:01:18 +04:00
2005-04-17 02:20:36 +04:00
return ;
}
2006-09-26 10:32:04 +04:00
static void fill_elf_note_phdr ( struct elf_phdr * phdr , int sz , loff_t offset )
2005-04-17 02:20:36 +04:00
{
phdr - > p_type = PT_NOTE ;
phdr - > p_offset = offset ;
phdr - > p_vaddr = 0 ;
phdr - > p_paddr = 0 ;
phdr - > p_filesz = sz ;
phdr - > p_memsz = 0 ;
phdr - > p_flags = 0 ;
phdr - > p_align = 0 ;
return ;
}
static void fill_note ( struct memelfnote * note , const char * name , int type ,
unsigned int sz , void * data )
{
note - > name = name ;
note - > type = type ;
note - > datasz = sz ;
note - > data = data ;
return ;
}
/*
2006-06-23 13:05:35 +04:00
* fill up all the fields in prstatus from the given task struct , except
* registers which need to be filled up separately .
2005-04-17 02:20:36 +04:00
*/
static void fill_prstatus ( struct elf_prstatus * prstatus ,
2006-06-23 13:05:35 +04:00
struct task_struct * p , long signr )
2005-04-17 02:20:36 +04:00
{
prstatus - > pr_info . si_signo = prstatus - > pr_cursig = signr ;
prstatus - > pr_sigpend = p - > pending . signal . sig [ 0 ] ;
prstatus - > pr_sighold = p - > blocked . sig [ 0 ] ;
2009-06-18 03:27:38 +04:00
rcu_read_lock ( ) ;
prstatus - > pr_ppid = task_pid_vnr ( rcu_dereference ( p - > real_parent ) ) ;
rcu_read_unlock ( ) ;
2007-10-19 10:40:14 +04:00
prstatus - > pr_pid = task_pid_vnr ( p ) ;
prstatus - > pr_pgrp = task_pgrp_vnr ( p ) ;
prstatus - > pr_sid = task_session_vnr ( p ) ;
2005-04-17 02:20:36 +04:00
if ( thread_group_leader ( p ) ) {
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
struct task_cputime cputime ;
2005-04-17 02:20:36 +04:00
/*
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
* This is the record for the group leader . It shows the
* group - wide total , not its individual thread total .
2005-04-17 02:20:36 +04:00
*/
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
thread_group_cputime ( p , & cputime ) ;
cputime_to_timeval ( cputime . utime , & prstatus - > pr_utime ) ;
cputime_to_timeval ( cputime . stime , & prstatus - > pr_stime ) ;
2005-04-17 02:20:36 +04:00
} else {
cputime_to_timeval ( p - > utime , & prstatus - > pr_utime ) ;
cputime_to_timeval ( p - > stime , & prstatus - > pr_stime ) ;
}
cputime_to_timeval ( p - > signal - > cutime , & prstatus - > pr_cutime ) ;
cputime_to_timeval ( p - > signal - > cstime , & prstatus - > pr_cstime ) ;
}
static int fill_psinfo ( struct elf_prpsinfo * psinfo , struct task_struct * p ,
struct mm_struct * mm )
{
2008-11-14 02:39:19 +03:00
const struct cred * cred ;
2005-05-11 11:10:44 +04:00
unsigned int i , len ;
2005-04-17 02:20:36 +04:00
/* first copy the parameters from user space */
memset ( psinfo , 0 , sizeof ( struct elf_prpsinfo ) ) ;
len = mm - > arg_end - mm - > arg_start ;
if ( len > = ELF_PRARGSZ )
len = ELF_PRARGSZ - 1 ;
if ( copy_from_user ( & psinfo - > pr_psargs ,
( const char __user * ) mm - > arg_start , len ) )
return - EFAULT ;
for ( i = 0 ; i < len ; i + + )
if ( psinfo - > pr_psargs [ i ] = = 0 )
psinfo - > pr_psargs [ i ] = ' ' ;
psinfo - > pr_psargs [ len ] = 0 ;
2009-06-18 03:27:38 +04:00
rcu_read_lock ( ) ;
psinfo - > pr_ppid = task_pid_vnr ( rcu_dereference ( p - > real_parent ) ) ;
rcu_read_unlock ( ) ;
2007-10-19 10:40:14 +04:00
psinfo - > pr_pid = task_pid_vnr ( p ) ;
psinfo - > pr_pgrp = task_pgrp_vnr ( p ) ;
psinfo - > pr_sid = task_session_vnr ( p ) ;
2005-04-17 02:20:36 +04:00
i = p - > state ? ffz ( ~ p - > state ) + 1 : 0 ;
psinfo - > pr_state = i ;
2006-03-25 14:08:22 +03:00
psinfo - > pr_sname = ( i > 5 ) ? ' . ' : " RSDTZW " [ i ] ;
2005-04-17 02:20:36 +04:00
psinfo - > pr_zomb = psinfo - > pr_sname = = ' Z ' ;
psinfo - > pr_nice = task_nice ( p ) ;
psinfo - > pr_flag = p - > flags ;
2008-11-14 02:39:19 +03:00
rcu_read_lock ( ) ;
cred = __task_cred ( p ) ;
2012-02-08 06:36:10 +04:00
SET_UID ( psinfo - > pr_uid , from_kuid_munged ( cred - > user_ns , cred - > uid ) ) ;
SET_GID ( psinfo - > pr_gid , from_kgid_munged ( cred - > user_ns , cred - > gid ) ) ;
2008-11-14 02:39:19 +03:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
strncpy ( psinfo - > pr_fname , p - > comm , sizeof ( psinfo - > pr_fname ) ) ;
return 0 ;
}
2008-01-30 15:31:44 +03:00
static void fill_auxv_note ( struct memelfnote * note , struct mm_struct * mm )
{
elf_addr_t * auxv = ( elf_addr_t * ) mm - > saved_auxv ;
int i = 0 ;
do
i + = 2 ;
while ( auxv [ i - 2 ] ! = AT_NULL ) ;
fill_note ( note , " CORE " , NT_AUXV , i * sizeof ( elf_addr_t ) , auxv ) ;
}
2008-01-30 15:31:45 +03:00
# ifdef CORE_DUMP_USE_REGSET
# include <linux/regset.h>
struct elf_thread_core_info {
struct elf_thread_core_info * next ;
struct task_struct * task ;
struct elf_prstatus prstatus ;
struct memelfnote notes [ 0 ] ;
} ;
struct elf_note_info {
struct elf_thread_core_info * thread ;
struct memelfnote psinfo ;
struct memelfnote auxv ;
size_t size ;
int thread_notes ;
} ;
2008-03-05 01:28:30 +03:00
/*
* When a regset has a writeback hook , we call it on each thread before
* dumping user memory . On register window machines , this makes sure the
* user memory backing the register data is up to date before we read it .
*/
static void do_thread_regset_writeback ( struct task_struct * task ,
const struct user_regset * regset )
{
if ( regset - > writeback )
regset - > writeback ( task , regset , 1 ) ;
}
2012-02-15 01:34:52 +04:00
# ifndef PR_REG_SIZE
# define PR_REG_SIZE(S) sizeof(S)
# endif
# ifndef PRSTATUS_SIZE
# define PRSTATUS_SIZE(S) sizeof(S)
# endif
# ifndef PR_REG_PTR
# define PR_REG_PTR(S) (&((S)->pr_reg))
# endif
# ifndef SET_PR_FPVALID
# define SET_PR_FPVALID(S, V) ((S)->pr_fpvalid = (V))
# endif
2008-01-30 15:31:45 +03:00
static int fill_thread_core_info ( struct elf_thread_core_info * t ,
const struct user_regset_view * view ,
long signr , size_t * total )
{
unsigned int i ;
/*
* NT_PRSTATUS is the one special case , because the regset data
* goes into the pr_reg field inside the note contents , rather
* than being the whole note contents . We fill the reset in here .
* We assume that regset 0 is NT_PRSTATUS .
*/
fill_prstatus ( & t - > prstatus , t - > task , signr ) ;
( void ) view - > regsets [ 0 ] . get ( t - > task , & view - > regsets [ 0 ] ,
2012-02-15 01:34:52 +04:00
0 , PR_REG_SIZE ( t - > prstatus . pr_reg ) ,
PR_REG_PTR ( & t - > prstatus ) , NULL ) ;
2008-01-30 15:31:45 +03:00
fill_note ( & t - > notes [ 0 ] , " CORE " , NT_PRSTATUS ,
2012-02-15 01:34:52 +04:00
PRSTATUS_SIZE ( t - > prstatus ) , & t - > prstatus ) ;
2008-01-30 15:31:45 +03:00
* total + = notesize ( & t - > notes [ 0 ] ) ;
2008-03-05 01:28:30 +03:00
do_thread_regset_writeback ( t - > task , & view - > regsets [ 0 ] ) ;
2008-01-30 15:31:45 +03:00
/*
* Each other regset might generate a note too . For each regset
* that has no core_note_type or is inactive , we leave t - > notes [ i ]
* all zero and we ' ll know to skip writing it later .
*/
for ( i = 1 ; i < view - > n ; + + i ) {
const struct user_regset * regset = & view - > regsets [ i ] ;
2008-03-05 01:28:30 +03:00
do_thread_regset_writeback ( t - > task , regset ) ;
2012-03-02 22:43:48 +04:00
if ( regset - > core_note_type & & regset - > get & &
2008-01-30 15:31:45 +03:00
( ! regset - > active | | regset - > active ( t - > task , regset ) ) ) {
int ret ;
size_t size = regset - > n * regset - > size ;
void * data = kmalloc ( size , GFP_KERNEL ) ;
if ( unlikely ( ! data ) )
return 0 ;
ret = regset - > get ( t - > task , regset ,
0 , size , data , NULL ) ;
if ( unlikely ( ret ) )
kfree ( data ) ;
else {
if ( regset - > core_note_type ! = NT_PRFPREG )
fill_note ( & t - > notes [ i ] , " LINUX " ,
regset - > core_note_type ,
size , data ) ;
else {
2012-02-15 01:34:52 +04:00
SET_PR_FPVALID ( & t - > prstatus , 1 ) ;
2008-01-30 15:31:45 +03:00
fill_note ( & t - > notes [ i ] , " CORE " ,
NT_PRFPREG , size , data ) ;
}
* total + = notesize ( & t - > notes [ i ] ) ;
}
}
}
return 1 ;
}
static int fill_note_info ( struct elfhdr * elf , int phdrs ,
struct elf_note_info * info ,
long signr , struct pt_regs * regs )
{
struct task_struct * dump_task = current ;
const struct user_regset_view * view = task_user_regset_view ( dump_task ) ;
struct elf_thread_core_info * t ;
struct elf_prpsinfo * psinfo ;
2008-07-25 12:47:45 +04:00
struct core_thread * ct ;
2008-01-30 15:31:45 +03:00
unsigned int i ;
info - > size = 0 ;
info - > thread = NULL ;
psinfo = kmalloc ( sizeof ( * psinfo ) , GFP_KERNEL ) ;
if ( psinfo = = NULL )
return 0 ;
2009-07-01 09:06:26 +04:00
fill_note ( & info - > psinfo , " CORE " , NT_PRPSINFO , sizeof ( * psinfo ) , psinfo ) ;
2008-01-30 15:31:45 +03:00
/*
* Figure out how many notes we ' re going to need for each thread .
*/
info - > thread_notes = 0 ;
for ( i = 0 ; i < view - > n ; + + i )
if ( view - > regsets [ i ] . core_note_type ! = 0 )
+ + info - > thread_notes ;
/*
* Sanity check . We rely on regset 0 being in NT_PRSTATUS ,
* since it is our one special case .
*/
if ( unlikely ( info - > thread_notes = = 0 ) | |
unlikely ( view - > regsets [ 0 ] . core_note_type ! = NT_PRSTATUS ) ) {
WARN_ON ( 1 ) ;
return 0 ;
}
/*
* Initialize the ELF file header .
*/
fill_elf_header ( elf , phdrs ,
view - > e_machine , view - > e_flags , view - > ei_osabi ) ;
/*
* Allocate a structure for each thread .
*/
2008-07-25 12:47:45 +04:00
for ( ct = & dump_task - > mm - > core_state - > dumper ; ct ; ct = ct - > next ) {
t = kzalloc ( offsetof ( struct elf_thread_core_info ,
notes [ info - > thread_notes ] ) ,
GFP_KERNEL ) ;
if ( unlikely ( ! t ) )
return 0 ;
t - > task = ct - > task ;
if ( ct - > task = = dump_task | | ! info - > thread ) {
t - > next = info - > thread ;
info - > thread = t ;
} else {
/*
* Make sure to keep the original task at
* the head of the list .
*/
t - > next = info - > thread - > next ;
info - > thread - > next = t ;
2008-01-30 15:31:45 +03:00
}
2008-07-25 12:47:45 +04:00
}
2008-01-30 15:31:45 +03:00
/*
* Now fill in each thread ' s information .
*/
for ( t = info - > thread ; t ! = NULL ; t = t - > next )
if ( ! fill_thread_core_info ( t , view , signr , & info - > size ) )
return 0 ;
/*
* Fill in the two process - wide notes .
*/
fill_psinfo ( psinfo , dump_task - > group_leader , dump_task - > mm ) ;
info - > size + = notesize ( & info - > psinfo ) ;
fill_auxv_note ( & info - > auxv , current - > mm ) ;
info - > size + = notesize ( & info - > auxv ) ;
return 1 ;
}
static size_t get_note_info_size ( struct elf_note_info * info )
{
return info - > size ;
}
/*
* Write all the notes for each thread . When writing the first thread , the
* process - wide notes are interleaved after the first thread - specific note .
*/
static int write_note_info ( struct elf_note_info * info ,
struct file * file , loff_t * foffset )
{
bool first = 1 ;
struct elf_thread_core_info * t = info - > thread ;
do {
int i ;
if ( ! writenote ( & t - > notes [ 0 ] , file , foffset ) )
return 0 ;
if ( first & & ! writenote ( & info - > psinfo , file , foffset ) )
return 0 ;
if ( first & & ! writenote ( & info - > auxv , file , foffset ) )
return 0 ;
for ( i = 1 ; i < info - > thread_notes ; + + i )
if ( t - > notes [ i ] . data & &
! writenote ( & t - > notes [ i ] , file , foffset ) )
return 0 ;
first = 0 ;
t = t - > next ;
} while ( t ) ;
return 1 ;
}
static void free_note_info ( struct elf_note_info * info )
{
struct elf_thread_core_info * threads = info - > thread ;
while ( threads ) {
unsigned int i ;
struct elf_thread_core_info * t = threads ;
threads = t - > next ;
WARN_ON ( t - > notes [ 0 ] . data & & t - > notes [ 0 ] . data ! = & t - > prstatus ) ;
for ( i = 1 ; i < info - > thread_notes ; + + i )
kfree ( t - > notes [ i ] . data ) ;
kfree ( t ) ;
}
kfree ( info - > psinfo . data ) ;
}
# else
2005-04-17 02:20:36 +04:00
/* Here is the structure in which status of each thread is captured. */
struct elf_thread_status
{
struct list_head list ;
struct elf_prstatus prstatus ; /* NT_PRSTATUS */
elf_fpregset_t fpu ; /* NT_PRFPREG */
struct task_struct * thread ;
# ifdef ELF_CORE_COPY_XFPREGS
2007-10-17 10:25:39 +04:00
elf_fpxregset_t xfpu ; /* ELF_CORE_XFPREG_TYPE */
2005-04-17 02:20:36 +04:00
# endif
struct memelfnote notes [ 3 ] ;
int num_notes ;
} ;
/*
* In order to add the specific thread information for the elf file format ,
2006-06-23 13:05:35 +04:00
* we need to keep a linked list of every threads pr_status and then create
* a single section for them in the final core file .
2005-04-17 02:20:36 +04:00
*/
static int elf_dump_thread_status ( long signr , struct elf_thread_status * t )
{
int sz = 0 ;
struct task_struct * p = t - > thread ;
t - > num_notes = 0 ;
fill_prstatus ( & t - > prstatus , p , signr ) ;
elf_core_copy_task_regs ( p , & t - > prstatus . pr_reg ) ;
2006-06-23 13:05:35 +04:00
fill_note ( & t - > notes [ 0 ] , " CORE " , NT_PRSTATUS , sizeof ( t - > prstatus ) ,
& ( t - > prstatus ) ) ;
2005-04-17 02:20:36 +04:00
t - > num_notes + + ;
sz + = notesize ( & t - > notes [ 0 ] ) ;
2006-06-23 13:05:35 +04:00
if ( ( t - > prstatus . pr_fpvalid = elf_core_copy_task_fpregs ( p , NULL ,
& t - > fpu ) ) ) {
fill_note ( & t - > notes [ 1 ] , " CORE " , NT_PRFPREG , sizeof ( t - > fpu ) ,
& ( t - > fpu ) ) ;
2005-04-17 02:20:36 +04:00
t - > num_notes + + ;
sz + = notesize ( & t - > notes [ 1 ] ) ;
}
# ifdef ELF_CORE_COPY_XFPREGS
if ( elf_core_copy_task_xfpregs ( p , & t - > xfpu ) ) {
2007-10-17 10:25:39 +04:00
fill_note ( & t - > notes [ 2 ] , " LINUX " , ELF_CORE_XFPREG_TYPE ,
sizeof ( t - > xfpu ) , & t - > xfpu ) ;
2005-04-17 02:20:36 +04:00
t - > num_notes + + ;
sz + = notesize ( & t - > notes [ 2 ] ) ;
}
# endif
return sz ;
}
2008-01-30 15:31:44 +03:00
struct elf_note_info {
struct memelfnote * notes ;
struct elf_prstatus * prstatus ; /* NT_PRSTATUS */
struct elf_prpsinfo * psinfo ; /* NT_PRPSINFO */
struct list_head thread_list ;
elf_fpregset_t * fpu ;
# ifdef ELF_CORE_COPY_XFPREGS
elf_fpxregset_t * xfpu ;
# endif
int thread_status_size ;
int numnote ;
} ;
2009-09-24 02:57:05 +04:00
static int elf_note_info_init ( struct elf_note_info * info )
2008-01-30 15:31:44 +03:00
{
2009-09-24 02:57:05 +04:00
memset ( info , 0 , sizeof ( * info ) ) ;
2008-01-30 15:31:44 +03:00
INIT_LIST_HEAD ( & info - > thread_list ) ;
2009-09-24 02:57:05 +04:00
/* Allocate space for six ELF notes */
info - > notes = kmalloc ( 6 * sizeof ( struct memelfnote ) , GFP_KERNEL ) ;
2008-01-30 15:31:44 +03:00
if ( ! info - > notes )
return 0 ;
info - > psinfo = kmalloc ( sizeof ( * info - > psinfo ) , GFP_KERNEL ) ;
if ( ! info - > psinfo )
2009-09-24 02:57:05 +04:00
goto notes_free ;
2008-01-30 15:31:44 +03:00
info - > prstatus = kmalloc ( sizeof ( * info - > prstatus ) , GFP_KERNEL ) ;
if ( ! info - > prstatus )
2009-09-24 02:57:05 +04:00
goto psinfo_free ;
2008-01-30 15:31:44 +03:00
info - > fpu = kmalloc ( sizeof ( * info - > fpu ) , GFP_KERNEL ) ;
if ( ! info - > fpu )
2009-09-24 02:57:05 +04:00
goto prstatus_free ;
2008-01-30 15:31:44 +03:00
# ifdef ELF_CORE_COPY_XFPREGS
info - > xfpu = kmalloc ( sizeof ( * info - > xfpu ) , GFP_KERNEL ) ;
if ( ! info - > xfpu )
2009-09-24 02:57:05 +04:00
goto fpu_free ;
2008-01-30 15:31:44 +03:00
# endif
2009-09-24 02:57:05 +04:00
return 1 ;
# ifdef ELF_CORE_COPY_XFPREGS
fpu_free :
kfree ( info - > fpu ) ;
# endif
prstatus_free :
kfree ( info - > prstatus ) ;
psinfo_free :
kfree ( info - > psinfo ) ;
notes_free :
kfree ( info - > notes ) ;
return 0 ;
}
static int fill_note_info ( struct elfhdr * elf , int phdrs ,
struct elf_note_info * info ,
long signr , struct pt_regs * regs )
{
struct list_head * t ;
if ( ! elf_note_info_init ( info ) )
return 0 ;
2008-01-30 15:31:44 +03:00
if ( signr ) {
2008-07-25 12:47:45 +04:00
struct core_thread * ct ;
2008-04-29 12:01:18 +04:00
struct elf_thread_status * ets ;
2008-07-25 12:47:45 +04:00
for ( ct = current - > mm - > core_state - > dumper . next ;
ct ; ct = ct - > next ) {
ets = kzalloc ( sizeof ( * ets ) , GFP_KERNEL ) ;
if ( ! ets )
return 0 ;
ets - > thread = ct - > task ;
list_add ( & ets - > list , & info - > thread_list ) ;
}
2008-01-30 15:31:44 +03:00
list_for_each ( t , & info - > thread_list ) {
int sz ;
2008-04-29 12:01:18 +04:00
ets = list_entry ( t , struct elf_thread_status , list ) ;
sz = elf_dump_thread_status ( signr , ets ) ;
2008-01-30 15:31:44 +03:00
info - > thread_status_size + = sz ;
}
}
/* now collect the dump for the current */
memset ( info - > prstatus , 0 , sizeof ( * info - > prstatus ) ) ;
fill_prstatus ( info - > prstatus , current , signr ) ;
elf_core_copy_regs ( & info - > prstatus - > pr_reg , regs ) ;
/* Set up header */
fill_elf_header ( elf , phdrs , ELF_ARCH , ELF_CORE_EFLAGS , ELF_OSABI ) ;
/*
* Set up the notes in similar form to SVR4 core dumps made
* with info from their / proc .
*/
fill_note ( info - > notes + 0 , " CORE " , NT_PRSTATUS ,
sizeof ( * info - > prstatus ) , info - > prstatus ) ;
fill_psinfo ( info - > psinfo , current - > group_leader , current - > mm ) ;
fill_note ( info - > notes + 1 , " CORE " , NT_PRPSINFO ,
sizeof ( * info - > psinfo ) , info - > psinfo ) ;
info - > numnote = 2 ;
fill_auxv_note ( & info - > notes [ info - > numnote + + ] , current - > mm ) ;
/* Try to dump the FPU. */
info - > prstatus - > pr_fpvalid = elf_core_copy_task_fpregs ( current , regs ,
info - > fpu ) ;
if ( info - > prstatus - > pr_fpvalid )
fill_note ( info - > notes + info - > numnote + + ,
" CORE " , NT_PRFPREG , sizeof ( * info - > fpu ) , info - > fpu ) ;
# ifdef ELF_CORE_COPY_XFPREGS
if ( elf_core_copy_task_xfpregs ( current , info - > xfpu ) )
fill_note ( info - > notes + info - > numnote + + ,
" LINUX " , ELF_CORE_XFPREG_TYPE ,
sizeof ( * info - > xfpu ) , info - > xfpu ) ;
# endif
return 1 ;
}
static size_t get_note_info_size ( struct elf_note_info * info )
{
int sz = 0 ;
int i ;
for ( i = 0 ; i < info - > numnote ; i + + )
sz + = notesize ( info - > notes + i ) ;
sz + = info - > thread_status_size ;
return sz ;
}
static int write_note_info ( struct elf_note_info * info ,
struct file * file , loff_t * foffset )
{
int i ;
struct list_head * t ;
for ( i = 0 ; i < info - > numnote ; i + + )
if ( ! writenote ( info - > notes + i , file , foffset ) )
return 0 ;
/* write out the thread status notes section */
list_for_each ( t , & info - > thread_list ) {
struct elf_thread_status * tmp =
list_entry ( t , struct elf_thread_status , list ) ;
for ( i = 0 ; i < tmp - > num_notes ; i + + )
if ( ! writenote ( & tmp - > notes [ i ] , file , foffset ) )
return 0 ;
}
return 1 ;
}
static void free_note_info ( struct elf_note_info * info )
{
while ( ! list_empty ( & info - > thread_list ) ) {
struct list_head * tmp = info - > thread_list . next ;
list_del ( tmp ) ;
kfree ( list_entry ( tmp , struct elf_thread_status , list ) ) ;
}
kfree ( info - > prstatus ) ;
kfree ( info - > psinfo ) ;
kfree ( info - > notes ) ;
kfree ( info - > fpu ) ;
# ifdef ELF_CORE_COPY_XFPREGS
kfree ( info - > xfpu ) ;
# endif
}
2008-01-30 15:31:45 +03:00
# endif
2007-01-26 11:56:49 +03:00
static struct vm_area_struct * first_vma ( struct task_struct * tsk ,
struct vm_area_struct * gate_vma )
{
struct vm_area_struct * ret = tsk - > mm - > mmap ;
if ( ret )
return ret ;
return gate_vma ;
}
/*
* Helper function for iterating across a vma list . It ensures that the caller
* will visit ` gate_vma ' prior to terminating the search .
*/
static struct vm_area_struct * next_vma ( struct vm_area_struct * this_vma ,
struct vm_area_struct * gate_vma )
{
struct vm_area_struct * ret ;
ret = this_vma - > vm_next ;
if ( ret )
return ret ;
if ( this_vma = = gate_vma )
return NULL ;
return gate_vma ;
}
2010-03-06 00:44:10 +03:00
static void fill_extnum_info ( struct elfhdr * elf , struct elf_shdr * shdr4extnum ,
elf_addr_t e_shoff , int segs )
{
elf - > e_shoff = e_shoff ;
elf - > e_shentsize = sizeof ( * shdr4extnum ) ;
elf - > e_shnum = 1 ;
elf - > e_shstrndx = SHN_UNDEF ;
memset ( shdr4extnum , 0 , sizeof ( * shdr4extnum ) ) ;
shdr4extnum - > sh_type = SHT_NULL ;
shdr4extnum - > sh_size = elf - > e_shnum ;
shdr4extnum - > sh_link = elf - > e_shstrndx ;
shdr4extnum - > sh_info = segs ;
}
static size_t elf_core_vma_data_size ( struct vm_area_struct * gate_vma ,
unsigned long mm_flags )
{
struct vm_area_struct * vma ;
size_t size = 0 ;
for ( vma = first_vma ( current , gate_vma ) ; vma ! = NULL ;
vma = next_vma ( vma , gate_vma ) )
size + = vma_dump_size ( vma , mm_flags ) ;
return size ;
}
2005-04-17 02:20:36 +04:00
/*
* Actual dumper
*
* This is a two - pass process ; first we find the offsets of the bits ,
* and then they are actually written out . If we run out of core limit
* we just truncate .
*/
2009-12-18 02:27:16 +03:00
static int elf_core_dump ( struct coredump_params * cprm )
2005-04-17 02:20:36 +04:00
{
int has_dumped = 0 ;
mm_segment_t fs ;
int segs ;
size_t size = 0 ;
2007-01-26 11:56:49 +03:00
struct vm_area_struct * vma , * gate_vma ;
2005-04-17 02:20:36 +04:00
struct elfhdr * elf = NULL ;
2006-10-01 10:29:28 +04:00
loff_t offset = 0 , dataoff , foffset ;
2008-01-30 15:31:44 +03:00
struct elf_note_info info ;
2010-03-06 00:44:09 +03:00
struct elf_phdr * phdr4note = NULL ;
2010-03-06 00:44:10 +03:00
struct elf_shdr * shdr4extnum = NULL ;
Elf_Half e_phnum ;
elf_addr_t e_shoff ;
2005-04-17 02:20:36 +04:00
/*
* We no longer stop all VM operations .
*
2006-06-23 13:05:35 +04:00
* This is because those proceses that could possibly change map_count
* or the mmap / vma pages are now blocked in do_exit on current
* finishing this core dump .
2005-04-17 02:20:36 +04:00
*
* Only ptrace can touch these memory addresses , but it doesn ' t change
2006-06-23 13:05:35 +04:00
* the map_count or the pages allocated . So no possibility of crashing
2005-04-17 02:20:36 +04:00
* exists while dumping the mm - > vm_next areas to the core file .
*/
/* alloc memory for large data structures: too large to be on stack */
elf = kmalloc ( sizeof ( * elf ) , GFP_KERNEL ) ;
if ( ! elf )
2008-05-06 08:45:35 +04:00
goto out ;
2009-06-30 22:41:23 +04:00
/*
* The number of segs are recored into ELF header as 16 bit value .
* Please check DEFAULT_MAX_MAP_COUNT definition when you modify here .
*/
2005-04-17 02:20:36 +04:00
segs = current - > mm - > map_count ;
2010-03-06 00:44:07 +03:00
segs + = elf_core_extra_phdrs ( ) ;
2005-04-17 02:20:36 +04:00
2011-03-13 22:49:15 +03:00
gate_vma = get_gate_vma ( current - > mm ) ;
2007-01-26 11:56:49 +03:00
if ( gate_vma ! = NULL )
segs + + ;
2010-03-06 00:44:10 +03:00
/* for notes section */
segs + + ;
/* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
* this , kernel supports extended numbering . Have a look at
* include / linux / elf . h for further information . */
e_phnum = segs > PN_XNUM ? PN_XNUM : segs ;
2005-04-17 02:20:36 +04:00
/*
2008-01-30 15:31:44 +03:00
* Collect all the non - memory information about the process for the
* notes . This also sets up the file header .
2005-04-17 02:20:36 +04:00
*/
2010-03-06 00:44:10 +03:00
if ( ! fill_note_info ( elf , e_phnum , & info , cprm - > signr , cprm - > regs ) )
2008-01-30 15:31:44 +03:00
goto cleanup ;
2005-04-17 02:20:36 +04:00
2008-01-30 15:31:44 +03:00
has_dumped = 1 ;
current - > flags | = PF_DUMPCORE ;
2005-04-17 02:20:36 +04:00
fs = get_fs ( ) ;
set_fs ( KERNEL_DS ) ;
offset + = sizeof ( * elf ) ; /* Elf header */
2010-03-06 00:44:10 +03:00
offset + = segs * sizeof ( struct elf_phdr ) ; /* Program headers */
2006-10-13 20:42:07 +04:00
foffset = offset ;
2005-04-17 02:20:36 +04:00
/* Write notes phdr entry */
{
2008-01-30 15:31:44 +03:00
size_t sz = get_note_info_size ( & info ) ;
2005-04-17 02:20:36 +04:00
[POWERPC] spufs: Cleanup ELF coredump extra notes logic
To start with, arch_notes_size() etc. is a little too ambiguous a name for
my liking, so change the function names to be more explicit.
Calling through macros is ugly, especially with hidden parameters, so don't
do that, call the routines directly.
Use ARCH_HAVE_EXTRA_ELF_NOTES as the only flag, and based on it decide
whether we want the extern declarations or the empty versions.
Since we have empty routines, actually use them in the coredump code to
save a few #ifdefs.
We want to change the handling of foffset so that the write routine updates
foffset as it goes, instead of using file->f_pos (so that writing to a pipe
works). So pass foffset to the write routine, and for now just set it to
file->f_pos at the end of writing.
It should also be possible for the write routine to fail, so change it to
return int and treat a non-zero return as failure.
Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Paul Mackerras <paulus@samba.org>
2007-09-19 08:38:12 +04:00
sz + = elf_coredump_extra_notes_size ( ) ;
[POWERPC] coredump: Add SPU elf notes to coredump.
This patch adds SPU elf notes to the coredump. It creates a separate note
for each of /regs, /fpcr, /lslr, /decr, /decr_status, /mem, /signal1,
/signal1_type, /signal2, /signal2_type, /event_mask, /event_status,
/mbox_info, /ibox_info, /wbox_info, /dma_info, /proxydma_info, /object-id.
A new macro, ARCH_HAVE_EXTRA_NOTES, was created for architectures to
specify they have extra elf core notes.
A new macro, ELF_CORE_EXTRA_NOTES_SIZE, was created so the size of the
additional notes could be calculated and added to the notes phdr entry.
A new macro, ELF_CORE_WRITE_EXTRA_NOTES, was created so the new notes
would be written after the existing notes.
The SPU coredump code resides in spufs. Stub functions are provided in the
kernel which are hooked into the spufs code which does the actual work via
register_arch_coredump_calls().
A new set of __spufs_<file>_read/get() functions was provided to allow the
coredump code to read from the spufs files without having to lock the
SPU context for each file read from.
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Dwayne Grant McConnell <decimal@us.ibm.com>
Signed-off-by: Arnd Bergmann <arnd.bergmann@de.ibm.com>
2006-11-23 02:46:37 +03:00
2010-03-06 00:44:09 +03:00
phdr4note = kmalloc ( sizeof ( * phdr4note ) , GFP_KERNEL ) ;
if ( ! phdr4note )
2010-03-06 00:44:06 +03:00
goto end_coredump ;
2010-03-06 00:44:09 +03:00
fill_elf_note_phdr ( phdr4note , sz , offset ) ;
offset + = sz ;
2005-04-17 02:20:36 +04:00
}
dataoff = offset = roundup ( offset , ELF_EXEC_PAGESIZE ) ;
2010-03-06 00:44:12 +03:00
offset + = elf_core_vma_data_size ( gate_vma , cprm - > mm_flags ) ;
2010-03-06 00:44:10 +03:00
offset + = elf_core_extra_data_size ( ) ;
e_shoff = offset ;
if ( e_phnum = = PN_XNUM ) {
shdr4extnum = kmalloc ( sizeof ( * shdr4extnum ) , GFP_KERNEL ) ;
if ( ! shdr4extnum )
goto end_coredump ;
fill_extnum_info ( elf , shdr4extnum , e_shoff , segs ) ;
}
offset = dataoff ;
2010-03-06 00:44:09 +03:00
size + = sizeof ( * elf ) ;
if ( size > cprm - > limit | | ! dump_write ( cprm - > file , elf , sizeof ( * elf ) ) )
goto end_coredump ;
size + = sizeof ( * phdr4note ) ;
if ( size > cprm - > limit
| | ! dump_write ( cprm - > file , phdr4note , sizeof ( * phdr4note ) ) )
goto end_coredump ;
2005-04-17 02:20:36 +04:00
/* Write program headers for segments dump */
2007-01-26 11:56:49 +03:00
for ( vma = first_vma ( current , gate_vma ) ; vma ! = NULL ;
vma = next_vma ( vma , gate_vma ) ) {
2005-04-17 02:20:36 +04:00
struct elf_phdr phdr ;
phdr . p_type = PT_LOAD ;
phdr . p_offset = offset ;
phdr . p_vaddr = vma - > vm_start ;
phdr . p_paddr = 0 ;
2010-03-06 00:44:12 +03:00
phdr . p_filesz = vma_dump_size ( vma , cprm - > mm_flags ) ;
2007-10-17 10:27:02 +04:00
phdr . p_memsz = vma - > vm_end - vma - > vm_start ;
2005-04-17 02:20:36 +04:00
offset + = phdr . p_filesz ;
phdr . p_flags = vma - > vm_flags & VM_READ ? PF_R : 0 ;
2006-06-23 13:05:35 +04:00
if ( vma - > vm_flags & VM_WRITE )
phdr . p_flags | = PF_W ;
if ( vma - > vm_flags & VM_EXEC )
phdr . p_flags | = PF_X ;
2005-04-17 02:20:36 +04:00
phdr . p_align = ELF_EXEC_PAGESIZE ;
2010-03-06 00:44:06 +03:00
size + = sizeof ( phdr ) ;
if ( size > cprm - > limit
| | ! dump_write ( cprm - > file , & phdr , sizeof ( phdr ) ) )
goto end_coredump ;
2005-04-17 02:20:36 +04:00
}
2010-03-06 00:44:07 +03:00
if ( ! elf_core_write_extra_phdrs ( cprm - > file , offset , & size , cprm - > limit ) )
goto end_coredump ;
2005-04-17 02:20:36 +04:00
/* write out the notes section */
2009-12-18 02:27:16 +03:00
if ( ! write_note_info ( & info , cprm - > file , & foffset ) )
2008-01-30 15:31:44 +03:00
goto end_coredump ;
2005-04-17 02:20:36 +04:00
2009-12-18 02:27:16 +03:00
if ( elf_coredump_extra_notes_write ( cprm - > file , & foffset ) )
[POWERPC] spufs: Cleanup ELF coredump extra notes logic
To start with, arch_notes_size() etc. is a little too ambiguous a name for
my liking, so change the function names to be more explicit.
Calling through macros is ugly, especially with hidden parameters, so don't
do that, call the routines directly.
Use ARCH_HAVE_EXTRA_ELF_NOTES as the only flag, and based on it decide
whether we want the extern declarations or the empty versions.
Since we have empty routines, actually use them in the coredump code to
save a few #ifdefs.
We want to change the handling of foffset so that the write routine updates
foffset as it goes, instead of using file->f_pos (so that writing to a pipe
works). So pass foffset to the write routine, and for now just set it to
file->f_pos at the end of writing.
It should also be possible for the write routine to fail, so change it to
return int and treat a non-zero return as failure.
Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Paul Mackerras <paulus@samba.org>
2007-09-19 08:38:12 +04:00
goto end_coredump ;
[POWERPC] coredump: Add SPU elf notes to coredump.
This patch adds SPU elf notes to the coredump. It creates a separate note
for each of /regs, /fpcr, /lslr, /decr, /decr_status, /mem, /signal1,
/signal1_type, /signal2, /signal2_type, /event_mask, /event_status,
/mbox_info, /ibox_info, /wbox_info, /dma_info, /proxydma_info, /object-id.
A new macro, ARCH_HAVE_EXTRA_NOTES, was created for architectures to
specify they have extra elf core notes.
A new macro, ELF_CORE_EXTRA_NOTES_SIZE, was created so the size of the
additional notes could be calculated and added to the notes phdr entry.
A new macro, ELF_CORE_WRITE_EXTRA_NOTES, was created so the new notes
would be written after the existing notes.
The SPU coredump code resides in spufs. Stub functions are provided in the
kernel which are hooked into the spufs code which does the actual work via
register_arch_coredump_calls().
A new set of __spufs_<file>_read/get() functions was provided to allow the
coredump code to read from the spufs files without having to lock the
SPU context for each file read from.
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Dwayne Grant McConnell <decimal@us.ibm.com>
Signed-off-by: Arnd Bergmann <arnd.bergmann@de.ibm.com>
2006-11-23 02:46:37 +03:00
2006-10-01 10:29:28 +04:00
/* Align to page */
2009-12-18 02:27:16 +03:00
if ( ! dump_seek ( cprm - > file , dataoff - foffset ) )
2009-09-22 04:03:25 +04:00
goto end_coredump ;
2005-04-17 02:20:36 +04:00
2007-01-26 11:56:49 +03:00
for ( vma = first_vma ( current , gate_vma ) ; vma ! = NULL ;
vma = next_vma ( vma , gate_vma ) ) {
2005-04-17 02:20:36 +04:00
unsigned long addr ;
2007-10-17 10:27:02 +04:00
unsigned long end ;
2005-04-17 02:20:36 +04:00
2010-03-06 00:44:12 +03:00
end = vma - > vm_start + vma_dump_size ( vma , cprm - > mm_flags ) ;
2005-04-17 02:20:36 +04:00
2007-10-17 10:27:02 +04:00
for ( addr = vma - > vm_start ; addr < end ; addr + = PAGE_SIZE ) {
2006-06-23 13:05:35 +04:00
struct page * page ;
2009-09-22 04:03:25 +04:00
int stop ;
page = get_dump_page ( addr ) ;
if ( page ) {
void * kaddr = kmap ( page ) ;
2009-12-18 02:27:16 +03:00
stop = ( ( size + = PAGE_SIZE ) > cprm - > limit ) | |
! dump_write ( cprm - > file , kaddr ,
PAGE_SIZE ) ;
2009-09-22 04:03:25 +04:00
kunmap ( page ) ;
2005-04-17 02:20:36 +04:00
page_cache_release ( page ) ;
2009-09-22 04:03:25 +04:00
} else
2009-12-18 02:27:16 +03:00
stop = ! dump_seek ( cprm - > file , PAGE_SIZE ) ;
2009-09-22 04:03:25 +04:00
if ( stop )
goto end_coredump ;
2005-04-17 02:20:36 +04:00
}
}
2010-03-06 00:44:07 +03:00
if ( ! elf_core_write_extra_data ( cprm - > file , & size , cprm - > limit ) )
goto end_coredump ;
2005-04-17 02:20:36 +04:00
2010-03-06 00:44:10 +03:00
if ( e_phnum = = PN_XNUM ) {
size + = sizeof ( * shdr4extnum ) ;
if ( size > cprm - > limit
| | ! dump_write ( cprm - > file , shdr4extnum ,
sizeof ( * shdr4extnum ) ) )
goto end_coredump ;
}
2005-04-17 02:20:36 +04:00
end_coredump :
set_fs ( fs ) ;
cleanup :
2008-01-30 15:31:44 +03:00
free_note_info ( & info ) ;
2010-03-06 00:44:10 +03:00
kfree ( shdr4extnum ) ;
2010-03-06 00:44:09 +03:00
kfree ( phdr4note ) ;
2008-05-06 08:45:35 +04:00
kfree ( elf ) ;
out :
2005-04-17 02:20:36 +04:00
return has_dumped ;
}
2009-12-16 03:47:37 +03:00
# endif /* CONFIG_ELF_CORE */
2005-04-17 02:20:36 +04:00
static int __init init_elf_binfmt ( void )
{
2012-03-17 11:05:16 +04:00
register_binfmt ( & elf_format ) ;
return 0 ;
2005-04-17 02:20:36 +04:00
}
static void __exit exit_elf_binfmt ( void )
{
/* Remove the COFF and ELF loaders. */
unregister_binfmt ( & elf_format ) ;
}
core_initcall ( init_elf_binfmt ) ;
module_exit ( exit_elf_binfmt ) ;
MODULE_LICENSE ( " GPL " ) ;