4 * Copyright (C) 1991, 1992 Linus Torvalds 8 * #!-checking implemented by tytso. 11 * Demand-loading implemented 01.12.91 - no need to read anything but 12 * the header into memory. The inode of the executable is put into 13 * "current->executable", and page faults do the actual loading. Clean. 15 * Once more I can proudly say that linux stood up to being changed: it 16 * was less than 2 hours work to get demand-loading completely implemented. 18 * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead, 19 * current->executable is only used by the procfs. This allows a dispatch 20 * table to check for several different types of binary formats. We keep 21 * trying until we recognize the file or we run out of supported binary 26 #include <linux/sched.h> 27 #include <linux/kernel.h> 29 #include <linux/mman.h> 30 #include <linux/a.out.h> 31 #include <linux/errno.h> 32 #include <linux/signal.h> 33 #include <linux/string.h> 34 #include <linux/stat.h> 35 #include <linux/fcntl.h> 36 #include <linux/ptrace.h> 37 #include <linux/user.h> 38 #include <linux/malloc.h> 39 #include <linux/binfmts.h> 40 #include <linux/personality.h> 42 #include <asm/system.h> 43 #include <asm/segment.h> 44 #include <asm/pgtable.h> 46 #include <linux/config.h> 48 #include <linux/kerneld.h> 51 asmlinkage
intsys_exit(int exit_code
); 52 asmlinkage
intsys_brk(unsigned long); 55 * Here are the actual binaries that will be accepted: 56 * add more with "register_binfmt()" if using modules... 58 * These are defined again for the 'real' modules if you are using a 59 * module definition for these routines. 62 static struct linux_binfmt
*formats
= (struct linux_binfmt
*) NULL
; 64 voidbinfmt_setup(void) 66 #ifdef CONFIG_BINFMT_ELF 70 #ifdef CONFIG_BINFMT_AOUT 73 /* This cannot be configured out of the kernel */ 77 intregister_binfmt(struct linux_binfmt
* fmt
) 79 struct linux_binfmt
** tmp
= &formats
; 96 intunregister_binfmt(struct linux_binfmt
* fmt
) 98 struct linux_binfmt
** tmp
= &formats
; 109 #endif/* CONFIG_MODULES */ 111 intopen_inode(struct inode
* inode
,int mode
) 114 struct file
*f
, **fpp
; 116 if(!inode
->i_op
|| !inode
->i_op
->default_file_ops
) 122 fpp
= current
->files
->fd
; 126 if(++fd
>= NR_OPEN
) { 134 f
->f_mode
= (mode
+1) & O_ACCMODE
; 138 f
->f_op
= inode
->i_op
->default_file_ops
; 140 error
= f
->f_op
->open(inode
,f
); 152 * Note that a shared library must be both readable and executable due to 155 * Also note that we take the address to load from from the file itself. 157 asmlinkage
intsys_uselib(const char* library
) 161 struct linux_binfmt
* fmt
; 163 fd
=sys_open(library
,0,0); 166 file
= current
->files
->fd
[fd
]; 168 if(file
&& file
->f_inode
&& file
->f_op
&& file
->f_op
->read
) { 169 for(fmt
= formats
; fmt
; fmt
= fmt
->next
) { 170 int(*fn
)(int) = fmt
->load_shlib
; 174 if(retval
!= -ENOEXEC
) 183 * count() counts the number of arguments/envelopes 185 * We also do some limited EFAULT checking: this isn't complete, but 186 * it does cover most cases. I'll have to do this correctly some day.. 188 static intcount(char** argv
) 193 if((tmp
= argv
) != NULL
) { 194 error
=verify_area(VERIFY_READ
, tmp
,sizeof(char*)); 197 while((p
=get_user(tmp
++)) != NULL
) { 199 error
=verify_area(VERIFY_READ
, p
,1); 208 * 'copy_string()' copies argument/envelope strings from user 209 * memory to free pages in kernel mem. These are in a format ready 210 * to be put directly into the top of new user memory. 212 * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies 213 * whether the string and the string array are from user or kernel segments: 215 * from_kmem argv * argv ** 216 * 0 user space user space 217 * 1 kernel space user space 218 * 2 kernel space kernel space 220 * We do this by playing games with the fs segment register. Since it 221 * is expensive to load a segment register, we try to avoid calling 222 * set_fs() unless we absolutely have to. 224 unsigned longcopy_strings(int argc
,char** argv
,unsigned long*page
, 225 unsigned long p
,int from_kmem
) 227 char*tmp
, *tmp1
, *pag
= NULL
; 229 unsigned long old_fs
, new_fs
; 232 return0;/* bullet-proofing */ 240 if(!(tmp1
= tmp
=get_user(argv
+argc
))) 241 panic("VFS: argc is wrong"); 244 while(get_user(tmp
++)); 246 if(p
< len
) {/* this shouldn't happen - 128kB */ 253 offset
= p
% PAGE_SIZE
; 256 if(!(pag
= (char*) page
[p
/PAGE_SIZE
]) && 257 !(pag
= (char*) page
[p
/PAGE_SIZE
] = 258 (unsigned long*)get_free_page(GFP_USER
))) 264 if(len
==0|| offset
==0) 265 *(pag
+ offset
) =get_user(tmp
); 267 int bytes_to_copy
= (len
> offset
) ? offset
: len
; 268 tmp
-= bytes_to_copy
; 270 offset
-= bytes_to_copy
; 271 len
-= bytes_to_copy
; 272 memcpy_fromfs(pag
+ offset
, tmp
, bytes_to_copy
+1); 281 unsigned longsetup_arg_pages(unsigned long p
,struct linux_binprm
* bprm
) 283 unsigned long stack_base
; 284 struct vm_area_struct
*mpnt
; 287 stack_base
= STACK_TOP
- MAX_ARG_PAGES
*PAGE_SIZE
; 291 bprm
->loader
+= stack_base
; 292 bprm
->exec
+= stack_base
; 294 mpnt
= (struct vm_area_struct
*)kmalloc(sizeof(*mpnt
), GFP_KERNEL
); 296 mpnt
->vm_mm
= current
->mm
; 297 mpnt
->vm_start
= PAGE_MASK
& (unsigned long) p
; 298 mpnt
->vm_end
= STACK_TOP
; 299 mpnt
->vm_page_prot
= PAGE_COPY
; 300 mpnt
->vm_flags
= VM_STACK_FLAGS
; 303 mpnt
->vm_inode
= NULL
; 305 insert_vm_struct(current
, mpnt
); 306 current
->mm
->total_vm
= (mpnt
->vm_end
- mpnt
->vm_start
) >> PAGE_SHIFT
; 309 for(i
=0; i
< MAX_ARG_PAGES
; i
++) { 312 put_dirty_page(current
,bprm
->page
[i
],stack_base
); 314 stack_base
+= PAGE_SIZE
; 320 * Read in the complete executable. This is used for "-N" files 321 * that aren't on a block boundary, and for files on filesystems 322 * without bmap support. 324 intread_exec(struct inode
*inode
,unsigned long offset
, 325 char* addr
,unsigned long count
,int to_kmem
) 328 int result
= -ENOEXEC
; 330 if(!inode
->i_op
|| !inode
->i_op
->default_file_ops
) 335 file
.f_inode
= inode
; 338 file
.f_op
= inode
->i_op
->default_file_ops
; 340 if(file
.f_op
->open(inode
,&file
)) 342 if(!file
.f_op
|| !file
.f_op
->read
) 344 if(file
.f_op
->lseek
) { 345 if(file
.f_op
->lseek(inode
,&file
,offset
,0) != offset
) 350 unsigned long old_fs
=get_fs(); 352 result
= file
.f_op
->read(inode
, &file
, addr
, count
); 355 result
=verify_area(VERIFY_WRITE
, addr
, count
); 358 result
= file
.f_op
->read(inode
, &file
, addr
, count
); 361 if(file
.f_op
->release
) 362 file
.f_op
->release(inode
,&file
); 367 static voidexec_mmap(void) 370 * The clear_page_tables done later on exec does the right thing 371 * to the page directory when shared, except for graceful abort 372 * (the oom is wrong there, too, IMHO) 374 if(current
->mm
->count
>1) { 375 struct mm_struct
*mm
=kmalloc(sizeof(*mm
), GFP_KERNEL
); 377 /* this is wrong, I think. */ 382 mm
->def_flags
=0;/* should future lockings be kept? */ 388 current
->mm
->count
--; 390 new_page_tables(current
); 393 exit_mmap(current
->mm
); 394 clear_page_tables(current
); 398 * This function flushes out all traces of the currently running executable so 399 * that a new one can be started 402 voidflush_old_exec(struct linux_binprm
* bprm
) 408 if(current
->euid
== current
->uid
&& current
->egid
== current
->gid
) 409 current
->dumpable
=1; 410 name
= bprm
->filename
; 411 for(i
=0; (ch
= *(name
++)) !='\0';) { 416 current
->comm
[i
++] = ch
; 418 current
->comm
[i
] ='\0'; 420 /* Release all of the old mmap stuff. */ 425 if(bprm
->e_uid
!= current
->euid
|| bprm
->e_gid
!= current
->egid
|| 426 permission(bprm
->inode
,MAY_READ
)) 427 current
->dumpable
=0; 429 for(i
=0; i
<32; i
++) { 430 current
->sig
->action
[i
].sa_mask
=0; 431 current
->sig
->action
[i
].sa_flags
=0; 432 if(current
->sig
->action
[i
].sa_handler
!= SIG_IGN
) 433 current
->sig
->action
[i
].sa_handler
= NULL
; 435 for(i
=0; i
<NR_OPEN
; i
++) 436 if(FD_ISSET(i
,¤t
->files
->close_on_exec
)) 438 FD_ZERO(¤t
->files
->close_on_exec
); 439 if(last_task_used_math
== current
) 440 last_task_used_math
= NULL
; 441 current
->used_math
=0; 445 * Fill the binprm structure from the inode. 446 * Check permissions, then read the first 512 bytes 448 intprepare_binprm(struct linux_binprm
*bprm
) 451 if(!S_ISREG(bprm
->inode
->i_mode
))/* must be regular file */ 453 if(IS_NOEXEC(bprm
->inode
))/* FS mustn't be mounted noexec */ 455 if(!bprm
->inode
->i_sb
) 457 i
= bprm
->inode
->i_mode
; 458 if(IS_NOSUID(bprm
->inode
) && 459 (((i
& S_ISUID
) && bprm
->inode
->i_uid
!= current
->euid
) 460 || ((i
& S_ISGID
) && !in_group_p(bprm
->inode
->i_gid
))) && !suser()) 462 /* make sure we don't let suid, sgid files be ptraced. */ 463 if(current
->flags
& PF_PTRACED
) { 464 bprm
->e_uid
= current
->euid
; 465 bprm
->e_gid
= current
->egid
; 467 bprm
->e_uid
= (i
& S_ISUID
) ? bprm
->inode
->i_uid
: current
->euid
; 469 * If setgid is set but no group execute bit then this 470 * is a candidate for mandatory locking, not a setgid 473 bprm
->e_gid
= ((i
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) ? 474 bprm
->inode
->i_gid
: current
->egid
; 476 if((retval
=permission(bprm
->inode
, MAY_EXEC
)) !=0) 478 if(!(bprm
->inode
->i_mode
&0111) &&fsuser()) 480 /* better not execute files which are being written to */ 481 if(bprm
->inode
->i_writecount
>0) 484 memset(bprm
->buf
,0,sizeof(bprm
->buf
)); 485 returnread_exec(bprm
->inode
,0,bprm
->buf
,128,1); 488 voidremove_arg_zero(struct linux_binprm
*bprm
) 491 unsigned long offset
; 493 offset
= bprm
->p
% PAGE_SIZE
; 494 page
= (char*)bprm
->page
[bprm
->p
/PAGE_SIZE
]; 495 while(bprm
->p
++,*(page
+offset
++)) 496 if(offset
==PAGE_SIZE
){ 498 page
= (char*)bprm
->page
[bprm
->p
/PAGE_SIZE
]; 505 * cycle the list of binary formats handler, until one recognizes the image 507 intsearch_binary_handler(struct linux_binprm
*bprm
,struct pt_regs
*regs
) 510 struct linux_binfmt
*fmt
; 512 /* handle /sbin/loader.. */ 514 struct exec
* eh
= (struct exec
*) bprm
->buf
; 516 if(!bprm
->loader
&& eh
->fh
.f_magic
==0x183&& 517 (eh
->fh
.f_flags
&0x3000) ==0x3000) 519 char* dynloader
[] = {"/sbin/loader"}; 522 remove_arg_zero(bprm
); 523 bprm
->p
=copy_strings(1, dynloader
, bprm
->page
, bprm
->p
,2); 525 bprm
->loader
= bprm
->p
; 526 retval
=open_namei(dynloader
[0],0,0, &bprm
->inode
, NULL
); 530 retval
=prepare_binprm(bprm
); 533 /* should call search_binary_handler recursively here, 534 but it does not matter */ 538 for(try=0;try<2;try++) { 539 for(fmt
= formats
; fmt
; fmt
= fmt
->next
) { 540 int(*fn
)(struct linux_binprm
*,struct pt_regs
*) = fmt
->load_binary
; 543 retval
=fn(bprm
, regs
); 548 current
->did_exec
=1; 551 if(retval
!= -ENOEXEC
) 553 if(bprm
->dont_iput
)/* We don't have the inode anymore*/ 556 if(retval
!= -ENOEXEC
) { 558 #ifdef CONFIG_KERNELD 560 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e)) 562 if(printable(bprm
->buf
[0]) && 563 printable(bprm
->buf
[1]) && 564 printable(bprm
->buf
[2]) && 565 printable(bprm
->buf
[3])) 567 sprintf(modname
,"binfmt-%hd", *(short*)(&bprm
->buf
)); 568 request_module(modname
); 577 * sys_execve() executes a new program. 579 intdo_execve(char* filename
,char** argv
,char** envp
,struct pt_regs
* regs
) 581 struct linux_binprm bprm
; 585 bprm
.p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void*); 586 for(i
=0; i
<MAX_ARG_PAGES
; i
++)/* clear page-table */ 588 retval
=open_namei(filename
,0,0, &bprm
.inode
, NULL
); 591 bprm
.filename
= filename
; 596 if((bprm
.argc
=count(argv
)) <0) 598 if((bprm
.envc
=count(envp
)) <0) 601 retval
=prepare_binprm(&bprm
); 604 bprm
.p
=copy_strings(1, &bprm
.filename
, bprm
.page
, bprm
.p
,2); 606 bprm
.p
=copy_strings(bprm
.envc
,envp
,bprm
.page
,bprm
.p
,0); 607 bprm
.p
=copy_strings(bprm
.argc
,argv
,bprm
.page
,bprm
.p
,0); 613 retval
=search_binary_handler(&bprm
,regs
); 618 /* Something went wrong, return the inode and free the argument pages*/ 621 for(i
=0; i
<MAX_ARG_PAGES
; i
++) 622 free_page(bprm
.page
[i
]);