Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/exec.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * #!-checking implemented by tytso. |
| 9 | */ |
| 10 | /* |
| 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. |
| 14 | * |
| 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. |
| 17 | * |
| 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 |
| 22 | * formats. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/config.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/file.h> |
| 28 | #include <linux/mman.h> |
| 29 | #include <linux/a.out.h> |
| 30 | #include <linux/stat.h> |
| 31 | #include <linux/fcntl.h> |
| 32 | #include <linux/smp_lock.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/pagemap.h> |
| 35 | #include <linux/highmem.h> |
| 36 | #include <linux/spinlock.h> |
| 37 | #include <linux/key.h> |
| 38 | #include <linux/personality.h> |
| 39 | #include <linux/binfmts.h> |
| 40 | #include <linux/swap.h> |
| 41 | #include <linux/utsname.h> |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/namei.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/ptrace.h> |
| 46 | #include <linux/mount.h> |
| 47 | #include <linux/security.h> |
| 48 | #include <linux/syscalls.h> |
| 49 | #include <linux/rmap.h> |
| 50 | #include <linux/acct.h> |
| 51 | |
| 52 | #include <asm/uaccess.h> |
| 53 | #include <asm/mmu_context.h> |
| 54 | |
| 55 | #ifdef CONFIG_KMOD |
| 56 | #include <linux/kmod.h> |
| 57 | #endif |
| 58 | |
| 59 | int core_uses_pid; |
| 60 | char core_pattern[65] = "core"; |
| 61 | /* The maximal length of core_pattern is also specified in sysctl.c */ |
| 62 | |
| 63 | static struct linux_binfmt *formats; |
| 64 | static DEFINE_RWLOCK(binfmt_lock); |
| 65 | |
| 66 | int register_binfmt(struct linux_binfmt * fmt) |
| 67 | { |
| 68 | struct linux_binfmt ** tmp = &formats; |
| 69 | |
| 70 | if (!fmt) |
| 71 | return -EINVAL; |
| 72 | if (fmt->next) |
| 73 | return -EBUSY; |
| 74 | write_lock(&binfmt_lock); |
| 75 | while (*tmp) { |
| 76 | if (fmt == *tmp) { |
| 77 | write_unlock(&binfmt_lock); |
| 78 | return -EBUSY; |
| 79 | } |
| 80 | tmp = &(*tmp)->next; |
| 81 | } |
| 82 | fmt->next = formats; |
| 83 | formats = fmt; |
| 84 | write_unlock(&binfmt_lock); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | EXPORT_SYMBOL(register_binfmt); |
| 89 | |
| 90 | int unregister_binfmt(struct linux_binfmt * fmt) |
| 91 | { |
| 92 | struct linux_binfmt ** tmp = &formats; |
| 93 | |
| 94 | write_lock(&binfmt_lock); |
| 95 | while (*tmp) { |
| 96 | if (fmt == *tmp) { |
| 97 | *tmp = fmt->next; |
| 98 | write_unlock(&binfmt_lock); |
| 99 | return 0; |
| 100 | } |
| 101 | tmp = &(*tmp)->next; |
| 102 | } |
| 103 | write_unlock(&binfmt_lock); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | EXPORT_SYMBOL(unregister_binfmt); |
| 108 | |
| 109 | static inline void put_binfmt(struct linux_binfmt * fmt) |
| 110 | { |
| 111 | module_put(fmt->module); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Note that a shared library must be both readable and executable due to |
| 116 | * security reasons. |
| 117 | * |
| 118 | * Also note that we take the address to load from from the file itself. |
| 119 | */ |
| 120 | asmlinkage long sys_uselib(const char __user * library) |
| 121 | { |
| 122 | struct file * file; |
| 123 | struct nameidata nd; |
| 124 | int error; |
| 125 | |
| 126 | nd.intent.open.flags = FMODE_READ; |
| 127 | error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd); |
| 128 | if (error) |
| 129 | goto out; |
| 130 | |
| 131 | error = -EINVAL; |
| 132 | if (!S_ISREG(nd.dentry->d_inode->i_mode)) |
| 133 | goto exit; |
| 134 | |
| 135 | error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd); |
| 136 | if (error) |
| 137 | goto exit; |
| 138 | |
| 139 | file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); |
| 140 | error = PTR_ERR(file); |
| 141 | if (IS_ERR(file)) |
| 142 | goto out; |
| 143 | |
| 144 | error = -ENOEXEC; |
| 145 | if(file->f_op) { |
| 146 | struct linux_binfmt * fmt; |
| 147 | |
| 148 | read_lock(&binfmt_lock); |
| 149 | for (fmt = formats ; fmt ; fmt = fmt->next) { |
| 150 | if (!fmt->load_shlib) |
| 151 | continue; |
| 152 | if (!try_module_get(fmt->module)) |
| 153 | continue; |
| 154 | read_unlock(&binfmt_lock); |
| 155 | error = fmt->load_shlib(file); |
| 156 | read_lock(&binfmt_lock); |
| 157 | put_binfmt(fmt); |
| 158 | if (error != -ENOEXEC) |
| 159 | break; |
| 160 | } |
| 161 | read_unlock(&binfmt_lock); |
| 162 | } |
| 163 | fput(file); |
| 164 | out: |
| 165 | return error; |
| 166 | exit: |
| 167 | path_release(&nd); |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * count() counts the number of strings in array ARGV. |
| 173 | */ |
| 174 | static int count(char __user * __user * argv, int max) |
| 175 | { |
| 176 | int i = 0; |
| 177 | |
| 178 | if (argv != NULL) { |
| 179 | for (;;) { |
| 180 | char __user * p; |
| 181 | |
| 182 | if (get_user(p, argv)) |
| 183 | return -EFAULT; |
| 184 | if (!p) |
| 185 | break; |
| 186 | argv++; |
| 187 | if(++i > max) |
| 188 | return -E2BIG; |
| 189 | cond_resched(); |
| 190 | } |
| 191 | } |
| 192 | return i; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * 'copy_strings()' copies argument/environment strings from user |
| 197 | * memory to free pages in kernel mem. These are in a format ready |
| 198 | * to be put directly into the top of new user memory. |
| 199 | */ |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 200 | static int copy_strings(int argc, char __user * __user * argv, |
| 201 | struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | struct page *kmapped_page = NULL; |
| 204 | char *kaddr = NULL; |
| 205 | int ret; |
| 206 | |
| 207 | while (argc-- > 0) { |
| 208 | char __user *str; |
| 209 | int len; |
| 210 | unsigned long pos; |
| 211 | |
| 212 | if (get_user(str, argv+argc) || |
| 213 | !(len = strnlen_user(str, bprm->p))) { |
| 214 | ret = -EFAULT; |
| 215 | goto out; |
| 216 | } |
| 217 | |
| 218 | if (bprm->p < len) { |
| 219 | ret = -E2BIG; |
| 220 | goto out; |
| 221 | } |
| 222 | |
| 223 | bprm->p -= len; |
| 224 | /* XXX: add architecture specific overflow check here. */ |
| 225 | pos = bprm->p; |
| 226 | |
| 227 | while (len > 0) { |
| 228 | int i, new, err; |
| 229 | int offset, bytes_to_copy; |
| 230 | struct page *page; |
| 231 | |
| 232 | offset = pos % PAGE_SIZE; |
| 233 | i = pos/PAGE_SIZE; |
| 234 | page = bprm->page[i]; |
| 235 | new = 0; |
| 236 | if (!page) { |
| 237 | page = alloc_page(GFP_HIGHUSER); |
| 238 | bprm->page[i] = page; |
| 239 | if (!page) { |
| 240 | ret = -ENOMEM; |
| 241 | goto out; |
| 242 | } |
| 243 | new = 1; |
| 244 | } |
| 245 | |
| 246 | if (page != kmapped_page) { |
| 247 | if (kmapped_page) |
| 248 | kunmap(kmapped_page); |
| 249 | kmapped_page = page; |
| 250 | kaddr = kmap(kmapped_page); |
| 251 | } |
| 252 | if (new && offset) |
| 253 | memset(kaddr, 0, offset); |
| 254 | bytes_to_copy = PAGE_SIZE - offset; |
| 255 | if (bytes_to_copy > len) { |
| 256 | bytes_to_copy = len; |
| 257 | if (new) |
| 258 | memset(kaddr+offset+len, 0, |
| 259 | PAGE_SIZE-offset-len); |
| 260 | } |
| 261 | err = copy_from_user(kaddr+offset, str, bytes_to_copy); |
| 262 | if (err) { |
| 263 | ret = -EFAULT; |
| 264 | goto out; |
| 265 | } |
| 266 | |
| 267 | pos += bytes_to_copy; |
| 268 | str += bytes_to_copy; |
| 269 | len -= bytes_to_copy; |
| 270 | } |
| 271 | } |
| 272 | ret = 0; |
| 273 | out: |
| 274 | if (kmapped_page) |
| 275 | kunmap(kmapped_page); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Like copy_strings, but get argv and its values from kernel memory. |
| 281 | */ |
| 282 | int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm) |
| 283 | { |
| 284 | int r; |
| 285 | mm_segment_t oldfs = get_fs(); |
| 286 | set_fs(KERNEL_DS); |
| 287 | r = copy_strings(argc, (char __user * __user *)argv, bprm); |
| 288 | set_fs(oldfs); |
| 289 | return r; |
| 290 | } |
| 291 | |
| 292 | EXPORT_SYMBOL(copy_strings_kernel); |
| 293 | |
| 294 | #ifdef CONFIG_MMU |
| 295 | /* |
| 296 | * This routine is used to map in a page into an address space: needed by |
| 297 | * execve() for the initial stack and environment pages. |
| 298 | * |
| 299 | * vma->vm_mm->mmap_sem is held for writing. |
| 300 | */ |
| 301 | void install_arg_page(struct vm_area_struct *vma, |
| 302 | struct page *page, unsigned long address) |
| 303 | { |
| 304 | struct mm_struct *mm = vma->vm_mm; |
| 305 | pgd_t * pgd; |
| 306 | pud_t * pud; |
| 307 | pmd_t * pmd; |
| 308 | pte_t * pte; |
| 309 | |
| 310 | if (unlikely(anon_vma_prepare(vma))) |
| 311 | goto out_sig; |
| 312 | |
| 313 | flush_dcache_page(page); |
| 314 | pgd = pgd_offset(mm, address); |
| 315 | |
| 316 | spin_lock(&mm->page_table_lock); |
| 317 | pud = pud_alloc(mm, pgd, address); |
| 318 | if (!pud) |
| 319 | goto out; |
| 320 | pmd = pmd_alloc(mm, pud, address); |
| 321 | if (!pmd) |
| 322 | goto out; |
| 323 | pte = pte_alloc_map(mm, pmd, address); |
| 324 | if (!pte) |
| 325 | goto out; |
| 326 | if (!pte_none(*pte)) { |
| 327 | pte_unmap(pte); |
| 328 | goto out; |
| 329 | } |
| 330 | inc_mm_counter(mm, rss); |
| 331 | lru_cache_add_active(page); |
| 332 | set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte( |
| 333 | page, vma->vm_page_prot)))); |
| 334 | page_add_anon_rmap(page, vma, address); |
| 335 | pte_unmap(pte); |
| 336 | spin_unlock(&mm->page_table_lock); |
| 337 | |
| 338 | /* no need for flush_tlb */ |
| 339 | return; |
| 340 | out: |
| 341 | spin_unlock(&mm->page_table_lock); |
| 342 | out_sig: |
| 343 | __free_page(page); |
| 344 | force_sig(SIGKILL, current); |
| 345 | } |
| 346 | |
| 347 | #define EXTRA_STACK_VM_PAGES 20 /* random */ |
| 348 | |
| 349 | int setup_arg_pages(struct linux_binprm *bprm, |
| 350 | unsigned long stack_top, |
| 351 | int executable_stack) |
| 352 | { |
| 353 | unsigned long stack_base; |
| 354 | struct vm_area_struct *mpnt; |
| 355 | struct mm_struct *mm = current->mm; |
| 356 | int i, ret; |
| 357 | long arg_size; |
| 358 | |
| 359 | #ifdef CONFIG_STACK_GROWSUP |
| 360 | /* Move the argument and environment strings to the bottom of the |
| 361 | * stack space. |
| 362 | */ |
| 363 | int offset, j; |
| 364 | char *to, *from; |
| 365 | |
| 366 | /* Start by shifting all the pages down */ |
| 367 | i = 0; |
| 368 | for (j = 0; j < MAX_ARG_PAGES; j++) { |
| 369 | struct page *page = bprm->page[j]; |
| 370 | if (!page) |
| 371 | continue; |
| 372 | bprm->page[i++] = page; |
| 373 | } |
| 374 | |
| 375 | /* Now move them within their pages */ |
| 376 | offset = bprm->p % PAGE_SIZE; |
| 377 | to = kmap(bprm->page[0]); |
| 378 | for (j = 1; j < i; j++) { |
| 379 | memmove(to, to + offset, PAGE_SIZE - offset); |
| 380 | from = kmap(bprm->page[j]); |
| 381 | memcpy(to + PAGE_SIZE - offset, from, offset); |
| 382 | kunmap(bprm->page[j - 1]); |
| 383 | to = from; |
| 384 | } |
| 385 | memmove(to, to + offset, PAGE_SIZE - offset); |
| 386 | kunmap(bprm->page[j - 1]); |
| 387 | |
| 388 | /* Limit stack size to 1GB */ |
| 389 | stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max; |
| 390 | if (stack_base > (1 << 30)) |
| 391 | stack_base = 1 << 30; |
| 392 | stack_base = PAGE_ALIGN(stack_top - stack_base); |
| 393 | |
| 394 | /* Adjust bprm->p to point to the end of the strings. */ |
| 395 | bprm->p = stack_base + PAGE_SIZE * i - offset; |
| 396 | |
| 397 | mm->arg_start = stack_base; |
| 398 | arg_size = i << PAGE_SHIFT; |
| 399 | |
| 400 | /* zero pages that were copied above */ |
| 401 | while (i < MAX_ARG_PAGES) |
| 402 | bprm->page[i++] = NULL; |
| 403 | #else |
| 404 | stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE); |
| 405 | stack_base = PAGE_ALIGN(stack_base); |
| 406 | bprm->p += stack_base; |
| 407 | mm->arg_start = bprm->p; |
| 408 | arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start); |
| 409 | #endif |
| 410 | |
| 411 | arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE; |
| 412 | |
| 413 | if (bprm->loader) |
| 414 | bprm->loader += stack_base; |
| 415 | bprm->exec += stack_base; |
| 416 | |
| 417 | mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); |
| 418 | if (!mpnt) |
| 419 | return -ENOMEM; |
| 420 | |
| 421 | if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) { |
| 422 | kmem_cache_free(vm_area_cachep, mpnt); |
| 423 | return -ENOMEM; |
| 424 | } |
| 425 | |
| 426 | memset(mpnt, 0, sizeof(*mpnt)); |
| 427 | |
| 428 | down_write(&mm->mmap_sem); |
| 429 | { |
| 430 | mpnt->vm_mm = mm; |
| 431 | #ifdef CONFIG_STACK_GROWSUP |
| 432 | mpnt->vm_start = stack_base; |
| 433 | mpnt->vm_end = stack_base + arg_size; |
| 434 | #else |
| 435 | mpnt->vm_end = stack_top; |
| 436 | mpnt->vm_start = mpnt->vm_end - arg_size; |
| 437 | #endif |
| 438 | /* Adjust stack execute permissions; explicitly enable |
| 439 | * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X |
| 440 | * and leave alone (arch default) otherwise. */ |
| 441 | if (unlikely(executable_stack == EXSTACK_ENABLE_X)) |
| 442 | mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC; |
| 443 | else if (executable_stack == EXSTACK_DISABLE_X) |
| 444 | mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC; |
| 445 | else |
| 446 | mpnt->vm_flags = VM_STACK_FLAGS; |
| 447 | mpnt->vm_flags |= mm->def_flags; |
| 448 | mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7]; |
| 449 | if ((ret = insert_vm_struct(mm, mpnt))) { |
| 450 | up_write(&mm->mmap_sem); |
| 451 | kmem_cache_free(vm_area_cachep, mpnt); |
| 452 | return ret; |
| 453 | } |
| 454 | mm->stack_vm = mm->total_vm = vma_pages(mpnt); |
| 455 | } |
| 456 | |
| 457 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
| 458 | struct page *page = bprm->page[i]; |
| 459 | if (page) { |
| 460 | bprm->page[i] = NULL; |
| 461 | install_arg_page(mpnt, page, stack_base); |
| 462 | } |
| 463 | stack_base += PAGE_SIZE; |
| 464 | } |
| 465 | up_write(&mm->mmap_sem); |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | EXPORT_SYMBOL(setup_arg_pages); |
| 471 | |
| 472 | #define free_arg_pages(bprm) do { } while (0) |
| 473 | |
| 474 | #else |
| 475 | |
| 476 | static inline void free_arg_pages(struct linux_binprm *bprm) |
| 477 | { |
| 478 | int i; |
| 479 | |
| 480 | for (i = 0; i < MAX_ARG_PAGES; i++) { |
| 481 | if (bprm->page[i]) |
| 482 | __free_page(bprm->page[i]); |
| 483 | bprm->page[i] = NULL; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | #endif /* CONFIG_MMU */ |
| 488 | |
| 489 | struct file *open_exec(const char *name) |
| 490 | { |
| 491 | struct nameidata nd; |
| 492 | int err; |
| 493 | struct file *file; |
| 494 | |
| 495 | nd.intent.open.flags = FMODE_READ; |
| 496 | err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd); |
| 497 | file = ERR_PTR(err); |
| 498 | |
| 499 | if (!err) { |
| 500 | struct inode *inode = nd.dentry->d_inode; |
| 501 | file = ERR_PTR(-EACCES); |
| 502 | if (!(nd.mnt->mnt_flags & MNT_NOEXEC) && |
| 503 | S_ISREG(inode->i_mode)) { |
| 504 | int err = permission(inode, MAY_EXEC, &nd); |
| 505 | if (!err && !(inode->i_mode & 0111)) |
| 506 | err = -EACCES; |
| 507 | file = ERR_PTR(err); |
| 508 | if (!err) { |
| 509 | file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); |
| 510 | if (!IS_ERR(file)) { |
| 511 | err = deny_write_access(file); |
| 512 | if (err) { |
| 513 | fput(file); |
| 514 | file = ERR_PTR(err); |
| 515 | } |
| 516 | } |
| 517 | out: |
| 518 | return file; |
| 519 | } |
| 520 | } |
| 521 | path_release(&nd); |
| 522 | } |
| 523 | goto out; |
| 524 | } |
| 525 | |
| 526 | EXPORT_SYMBOL(open_exec); |
| 527 | |
| 528 | int kernel_read(struct file *file, unsigned long offset, |
| 529 | char *addr, unsigned long count) |
| 530 | { |
| 531 | mm_segment_t old_fs; |
| 532 | loff_t pos = offset; |
| 533 | int result; |
| 534 | |
| 535 | old_fs = get_fs(); |
| 536 | set_fs(get_ds()); |
| 537 | /* The cast to a user pointer is valid due to the set_fs() */ |
| 538 | result = vfs_read(file, (void __user *)addr, count, &pos); |
| 539 | set_fs(old_fs); |
| 540 | return result; |
| 541 | } |
| 542 | |
| 543 | EXPORT_SYMBOL(kernel_read); |
| 544 | |
| 545 | static int exec_mmap(struct mm_struct *mm) |
| 546 | { |
| 547 | struct task_struct *tsk; |
| 548 | struct mm_struct * old_mm, *active_mm; |
| 549 | |
| 550 | /* Notify parent that we're no longer interested in the old VM */ |
| 551 | tsk = current; |
| 552 | old_mm = current->mm; |
| 553 | mm_release(tsk, old_mm); |
| 554 | |
| 555 | if (old_mm) { |
| 556 | /* |
| 557 | * Make sure that if there is a core dump in progress |
| 558 | * for the old mm, we get out and die instead of going |
| 559 | * through with the exec. We must hold mmap_sem around |
| 560 | * checking core_waiters and changing tsk->mm. The |
| 561 | * core-inducing thread will increment core_waiters for |
| 562 | * each thread whose ->mm == old_mm. |
| 563 | */ |
| 564 | down_read(&old_mm->mmap_sem); |
| 565 | if (unlikely(old_mm->core_waiters)) { |
| 566 | up_read(&old_mm->mmap_sem); |
| 567 | return -EINTR; |
| 568 | } |
| 569 | } |
| 570 | task_lock(tsk); |
| 571 | active_mm = tsk->active_mm; |
| 572 | tsk->mm = mm; |
| 573 | tsk->active_mm = mm; |
| 574 | activate_mm(active_mm, mm); |
| 575 | task_unlock(tsk); |
| 576 | arch_pick_mmap_layout(mm); |
| 577 | if (old_mm) { |
| 578 | up_read(&old_mm->mmap_sem); |
| 579 | if (active_mm != old_mm) BUG(); |
| 580 | mmput(old_mm); |
| 581 | return 0; |
| 582 | } |
| 583 | mmdrop(active_mm); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * This function makes sure the current process has its own signal table, |
| 589 | * so that flush_signal_handlers can later reset the handlers without |
| 590 | * disturbing other processes. (Other processes might share the signal |
| 591 | * table via the CLONE_SIGHAND option to clone().) |
| 592 | */ |
| 593 | static inline int de_thread(struct task_struct *tsk) |
| 594 | { |
| 595 | struct signal_struct *sig = tsk->signal; |
| 596 | struct sighand_struct *newsighand, *oldsighand = tsk->sighand; |
| 597 | spinlock_t *lock = &oldsighand->siglock; |
| 598 | int count; |
| 599 | |
| 600 | /* |
| 601 | * If we don't share sighandlers, then we aren't sharing anything |
| 602 | * and we can just re-use it all. |
| 603 | */ |
| 604 | if (atomic_read(&oldsighand->count) <= 1) { |
| 605 | BUG_ON(atomic_read(&sig->count) != 1); |
| 606 | exit_itimers(sig); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); |
| 611 | if (!newsighand) |
| 612 | return -ENOMEM; |
| 613 | |
| 614 | if (thread_group_empty(current)) |
| 615 | goto no_thread_group; |
| 616 | |
| 617 | /* |
| 618 | * Kill all other threads in the thread group. |
| 619 | * We must hold tasklist_lock to call zap_other_threads. |
| 620 | */ |
| 621 | read_lock(&tasklist_lock); |
| 622 | spin_lock_irq(lock); |
| 623 | if (sig->flags & SIGNAL_GROUP_EXIT) { |
| 624 | /* |
| 625 | * Another group action in progress, just |
| 626 | * return so that the signal is processed. |
| 627 | */ |
| 628 | spin_unlock_irq(lock); |
| 629 | read_unlock(&tasklist_lock); |
| 630 | kmem_cache_free(sighand_cachep, newsighand); |
| 631 | return -EAGAIN; |
| 632 | } |
| 633 | zap_other_threads(current); |
| 634 | read_unlock(&tasklist_lock); |
| 635 | |
| 636 | /* |
| 637 | * Account for the thread group leader hanging around: |
| 638 | */ |
| 639 | count = 2; |
| 640 | if (thread_group_leader(current)) |
| 641 | count = 1; |
| 642 | while (atomic_read(&sig->count) > count) { |
| 643 | sig->group_exit_task = current; |
| 644 | sig->notify_count = count; |
| 645 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 646 | spin_unlock_irq(lock); |
| 647 | schedule(); |
| 648 | spin_lock_irq(lock); |
| 649 | } |
| 650 | sig->group_exit_task = NULL; |
| 651 | sig->notify_count = 0; |
Linus Torvalds | c2a0f59 | 2005-06-18 13:06:22 -0700 | [diff] [blame^] | 652 | sig->real_timer.data = (unsigned long)current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | spin_unlock_irq(lock); |
| 654 | |
| 655 | /* |
| 656 | * At this point all other threads have exited, all we have to |
| 657 | * do is to wait for the thread group leader to become inactive, |
| 658 | * and to assume its PID: |
| 659 | */ |
| 660 | if (!thread_group_leader(current)) { |
| 661 | struct task_struct *leader = current->group_leader, *parent; |
| 662 | struct dentry *proc_dentry1, *proc_dentry2; |
| 663 | unsigned long exit_state, ptrace; |
| 664 | |
| 665 | /* |
| 666 | * Wait for the thread group leader to be a zombie. |
| 667 | * It should already be zombie at this point, most |
| 668 | * of the time. |
| 669 | */ |
| 670 | while (leader->exit_state != EXIT_ZOMBIE) |
| 671 | yield(); |
| 672 | |
| 673 | spin_lock(&leader->proc_lock); |
| 674 | spin_lock(¤t->proc_lock); |
| 675 | proc_dentry1 = proc_pid_unhash(current); |
| 676 | proc_dentry2 = proc_pid_unhash(leader); |
| 677 | write_lock_irq(&tasklist_lock); |
| 678 | |
Linus Torvalds | c2a0f59 | 2005-06-18 13:06:22 -0700 | [diff] [blame^] | 679 | BUG_ON(leader->tgid != current->tgid); |
| 680 | BUG_ON(current->pid == current->tgid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | /* |
| 682 | * An exec() starts a new thread group with the |
| 683 | * TGID of the previous thread group. Rehash the |
| 684 | * two threads with a switched PID, and release |
| 685 | * the former thread group leader: |
| 686 | */ |
| 687 | ptrace = leader->ptrace; |
| 688 | parent = leader->parent; |
| 689 | if (unlikely(ptrace) && unlikely(parent == current)) { |
| 690 | /* |
| 691 | * Joker was ptracing his own group leader, |
| 692 | * and now he wants to be his own parent! |
| 693 | * We can't have that. |
| 694 | */ |
| 695 | ptrace = 0; |
| 696 | } |
| 697 | |
| 698 | ptrace_unlink(current); |
| 699 | ptrace_unlink(leader); |
| 700 | remove_parent(current); |
| 701 | remove_parent(leader); |
| 702 | |
| 703 | switch_exec_pids(leader, current); |
| 704 | |
| 705 | current->parent = current->real_parent = leader->real_parent; |
| 706 | leader->parent = leader->real_parent = child_reaper; |
| 707 | current->group_leader = current; |
| 708 | leader->group_leader = leader; |
| 709 | |
| 710 | add_parent(current, current->parent); |
| 711 | add_parent(leader, leader->parent); |
| 712 | if (ptrace) { |
| 713 | current->ptrace = ptrace; |
| 714 | __ptrace_link(current, parent); |
| 715 | } |
| 716 | |
| 717 | list_del(¤t->tasks); |
| 718 | list_add_tail(¤t->tasks, &init_task.tasks); |
| 719 | current->exit_signal = SIGCHLD; |
| 720 | exit_state = leader->exit_state; |
| 721 | |
| 722 | write_unlock_irq(&tasklist_lock); |
| 723 | spin_unlock(&leader->proc_lock); |
| 724 | spin_unlock(¤t->proc_lock); |
| 725 | proc_pid_flush(proc_dentry1); |
| 726 | proc_pid_flush(proc_dentry2); |
| 727 | |
Linus Torvalds | c2a0f59 | 2005-06-18 13:06:22 -0700 | [diff] [blame^] | 728 | BUG_ON(exit_state != EXIT_ZOMBIE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | release_task(leader); |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Now there are really no other threads at all, |
| 734 | * so it's safe to stop telling them to kill themselves. |
| 735 | */ |
| 736 | sig->flags = 0; |
| 737 | |
| 738 | no_thread_group: |
| 739 | BUG_ON(atomic_read(&sig->count) != 1); |
| 740 | exit_itimers(sig); |
| 741 | |
| 742 | if (atomic_read(&oldsighand->count) == 1) { |
| 743 | /* |
| 744 | * Now that we nuked the rest of the thread group, |
| 745 | * it turns out we are not sharing sighand any more either. |
| 746 | * So we can just keep it. |
| 747 | */ |
| 748 | kmem_cache_free(sighand_cachep, newsighand); |
| 749 | } else { |
| 750 | /* |
| 751 | * Move our state over to newsighand and switch it in. |
| 752 | */ |
| 753 | spin_lock_init(&newsighand->siglock); |
| 754 | atomic_set(&newsighand->count, 1); |
| 755 | memcpy(newsighand->action, oldsighand->action, |
| 756 | sizeof(newsighand->action)); |
| 757 | |
| 758 | write_lock_irq(&tasklist_lock); |
| 759 | spin_lock(&oldsighand->siglock); |
| 760 | spin_lock(&newsighand->siglock); |
| 761 | |
| 762 | current->sighand = newsighand; |
| 763 | recalc_sigpending(); |
| 764 | |
| 765 | spin_unlock(&newsighand->siglock); |
| 766 | spin_unlock(&oldsighand->siglock); |
| 767 | write_unlock_irq(&tasklist_lock); |
| 768 | |
| 769 | if (atomic_dec_and_test(&oldsighand->count)) |
| 770 | kmem_cache_free(sighand_cachep, oldsighand); |
| 771 | } |
| 772 | |
Linus Torvalds | c2a0f59 | 2005-06-18 13:06:22 -0700 | [diff] [blame^] | 773 | BUG_ON(!thread_group_empty(current)); |
| 774 | BUG_ON(!thread_group_leader(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * These functions flushes out all traces of the currently running executable |
| 780 | * so that a new one can be started |
| 781 | */ |
| 782 | |
| 783 | static inline void flush_old_files(struct files_struct * files) |
| 784 | { |
| 785 | long j = -1; |
| 786 | |
| 787 | spin_lock(&files->file_lock); |
| 788 | for (;;) { |
| 789 | unsigned long set, i; |
| 790 | |
| 791 | j++; |
| 792 | i = j * __NFDBITS; |
| 793 | if (i >= files->max_fds || i >= files->max_fdset) |
| 794 | break; |
| 795 | set = files->close_on_exec->fds_bits[j]; |
| 796 | if (!set) |
| 797 | continue; |
| 798 | files->close_on_exec->fds_bits[j] = 0; |
| 799 | spin_unlock(&files->file_lock); |
| 800 | for ( ; set ; i++,set >>= 1) { |
| 801 | if (set & 1) { |
| 802 | sys_close(i); |
| 803 | } |
| 804 | } |
| 805 | spin_lock(&files->file_lock); |
| 806 | |
| 807 | } |
| 808 | spin_unlock(&files->file_lock); |
| 809 | } |
| 810 | |
| 811 | void get_task_comm(char *buf, struct task_struct *tsk) |
| 812 | { |
| 813 | /* buf must be at least sizeof(tsk->comm) in size */ |
| 814 | task_lock(tsk); |
| 815 | strncpy(buf, tsk->comm, sizeof(tsk->comm)); |
| 816 | task_unlock(tsk); |
| 817 | } |
| 818 | |
| 819 | void set_task_comm(struct task_struct *tsk, char *buf) |
| 820 | { |
| 821 | task_lock(tsk); |
| 822 | strlcpy(tsk->comm, buf, sizeof(tsk->comm)); |
| 823 | task_unlock(tsk); |
| 824 | } |
| 825 | |
| 826 | int flush_old_exec(struct linux_binprm * bprm) |
| 827 | { |
| 828 | char * name; |
| 829 | int i, ch, retval; |
| 830 | struct files_struct *files; |
| 831 | char tcomm[sizeof(current->comm)]; |
| 832 | |
| 833 | /* |
| 834 | * Make sure we have a private signal table and that |
| 835 | * we are unassociated from the previous thread group. |
| 836 | */ |
| 837 | retval = de_thread(current); |
| 838 | if (retval) |
| 839 | goto out; |
| 840 | |
| 841 | /* |
| 842 | * Make sure we have private file handles. Ask the |
| 843 | * fork helper to do the work for us and the exit |
| 844 | * helper to do the cleanup of the old one. |
| 845 | */ |
| 846 | files = current->files; /* refcounted so safe to hold */ |
| 847 | retval = unshare_files(); |
| 848 | if (retval) |
| 849 | goto out; |
| 850 | /* |
| 851 | * Release all of the old mmap stuff |
| 852 | */ |
| 853 | retval = exec_mmap(bprm->mm); |
| 854 | if (retval) |
| 855 | goto mmap_failed; |
| 856 | |
| 857 | bprm->mm = NULL; /* We're using it now */ |
| 858 | |
| 859 | /* This is the point of no return */ |
| 860 | steal_locks(files); |
| 861 | put_files_struct(files); |
| 862 | |
| 863 | current->sas_ss_sp = current->sas_ss_size = 0; |
| 864 | |
| 865 | if (current->euid == current->uid && current->egid == current->gid) |
| 866 | current->mm->dumpable = 1; |
| 867 | name = bprm->filename; |
Paolo 'Blaisorblade' Giarrusso | 3677209 | 2005-05-05 16:16:12 -0700 | [diff] [blame] | 868 | |
| 869 | /* Copies the binary name from after last slash */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | for (i=0; (ch = *(name++)) != '\0';) { |
| 871 | if (ch == '/') |
Paolo 'Blaisorblade' Giarrusso | 3677209 | 2005-05-05 16:16:12 -0700 | [diff] [blame] | 872 | i = 0; /* overwrite what we wrote */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | else |
| 874 | if (i < (sizeof(tcomm) - 1)) |
| 875 | tcomm[i++] = ch; |
| 876 | } |
| 877 | tcomm[i] = '\0'; |
| 878 | set_task_comm(current, tcomm); |
| 879 | |
| 880 | current->flags &= ~PF_RANDOMIZE; |
| 881 | flush_thread(); |
| 882 | |
| 883 | if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || |
| 884 | permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) || |
| 885 | (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) { |
| 886 | suid_keys(current); |
| 887 | current->mm->dumpable = 0; |
| 888 | } |
| 889 | |
| 890 | /* An exec changes our domain. We are no longer part of the thread |
| 891 | group */ |
| 892 | |
| 893 | current->self_exec_id++; |
| 894 | |
| 895 | flush_signal_handlers(current, 0); |
| 896 | flush_old_files(current->files); |
| 897 | |
| 898 | return 0; |
| 899 | |
| 900 | mmap_failed: |
| 901 | put_files_struct(current->files); |
| 902 | current->files = files; |
| 903 | out: |
| 904 | return retval; |
| 905 | } |
| 906 | |
| 907 | EXPORT_SYMBOL(flush_old_exec); |
| 908 | |
| 909 | /* |
| 910 | * Fill the binprm structure from the inode. |
| 911 | * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes |
| 912 | */ |
| 913 | int prepare_binprm(struct linux_binprm *bprm) |
| 914 | { |
| 915 | int mode; |
| 916 | struct inode * inode = bprm->file->f_dentry->d_inode; |
| 917 | int retval; |
| 918 | |
| 919 | mode = inode->i_mode; |
| 920 | /* |
| 921 | * Check execute perms again - if the caller has CAP_DAC_OVERRIDE, |
| 922 | * generic_permission lets a non-executable through |
| 923 | */ |
| 924 | if (!(mode & 0111)) /* with at least _one_ execute bit set */ |
| 925 | return -EACCES; |
| 926 | if (bprm->file->f_op == NULL) |
| 927 | return -EACCES; |
| 928 | |
| 929 | bprm->e_uid = current->euid; |
| 930 | bprm->e_gid = current->egid; |
| 931 | |
| 932 | if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) { |
| 933 | /* Set-uid? */ |
| 934 | if (mode & S_ISUID) { |
| 935 | current->personality &= ~PER_CLEAR_ON_SETID; |
| 936 | bprm->e_uid = inode->i_uid; |
| 937 | } |
| 938 | |
| 939 | /* Set-gid? */ |
| 940 | /* |
| 941 | * If setgid is set but no group execute bit then this |
| 942 | * is a candidate for mandatory locking, not a setgid |
| 943 | * executable. |
| 944 | */ |
| 945 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
| 946 | current->personality &= ~PER_CLEAR_ON_SETID; |
| 947 | bprm->e_gid = inode->i_gid; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* fill in binprm security blob */ |
| 952 | retval = security_bprm_set(bprm); |
| 953 | if (retval) |
| 954 | return retval; |
| 955 | |
| 956 | memset(bprm->buf,0,BINPRM_BUF_SIZE); |
| 957 | return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE); |
| 958 | } |
| 959 | |
| 960 | EXPORT_SYMBOL(prepare_binprm); |
| 961 | |
| 962 | static inline int unsafe_exec(struct task_struct *p) |
| 963 | { |
| 964 | int unsafe = 0; |
| 965 | if (p->ptrace & PT_PTRACED) { |
| 966 | if (p->ptrace & PT_PTRACE_CAP) |
| 967 | unsafe |= LSM_UNSAFE_PTRACE_CAP; |
| 968 | else |
| 969 | unsafe |= LSM_UNSAFE_PTRACE; |
| 970 | } |
| 971 | if (atomic_read(&p->fs->count) > 1 || |
| 972 | atomic_read(&p->files->count) > 1 || |
| 973 | atomic_read(&p->sighand->count) > 1) |
| 974 | unsafe |= LSM_UNSAFE_SHARE; |
| 975 | |
| 976 | return unsafe; |
| 977 | } |
| 978 | |
| 979 | void compute_creds(struct linux_binprm *bprm) |
| 980 | { |
| 981 | int unsafe; |
| 982 | |
| 983 | if (bprm->e_uid != current->uid) |
| 984 | suid_keys(current); |
| 985 | exec_keys(current); |
| 986 | |
| 987 | task_lock(current); |
| 988 | unsafe = unsafe_exec(current); |
| 989 | security_bprm_apply_creds(bprm, unsafe); |
| 990 | task_unlock(current); |
| 991 | security_bprm_post_apply_creds(bprm); |
| 992 | } |
| 993 | |
| 994 | EXPORT_SYMBOL(compute_creds); |
| 995 | |
| 996 | void remove_arg_zero(struct linux_binprm *bprm) |
| 997 | { |
| 998 | if (bprm->argc) { |
| 999 | unsigned long offset; |
| 1000 | char * kaddr; |
| 1001 | struct page *page; |
| 1002 | |
| 1003 | offset = bprm->p % PAGE_SIZE; |
| 1004 | goto inside; |
| 1005 | |
| 1006 | while (bprm->p++, *(kaddr+offset++)) { |
| 1007 | if (offset != PAGE_SIZE) |
| 1008 | continue; |
| 1009 | offset = 0; |
| 1010 | kunmap_atomic(kaddr, KM_USER0); |
| 1011 | inside: |
| 1012 | page = bprm->page[bprm->p/PAGE_SIZE]; |
| 1013 | kaddr = kmap_atomic(page, KM_USER0); |
| 1014 | } |
| 1015 | kunmap_atomic(kaddr, KM_USER0); |
| 1016 | bprm->argc--; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | EXPORT_SYMBOL(remove_arg_zero); |
| 1021 | |
| 1022 | /* |
| 1023 | * cycle the list of binary formats handler, until one recognizes the image |
| 1024 | */ |
| 1025 | int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs) |
| 1026 | { |
| 1027 | int try,retval; |
| 1028 | struct linux_binfmt *fmt; |
| 1029 | #ifdef __alpha__ |
| 1030 | /* handle /sbin/loader.. */ |
| 1031 | { |
| 1032 | struct exec * eh = (struct exec *) bprm->buf; |
| 1033 | |
| 1034 | if (!bprm->loader && eh->fh.f_magic == 0x183 && |
| 1035 | (eh->fh.f_flags & 0x3000) == 0x3000) |
| 1036 | { |
| 1037 | struct file * file; |
| 1038 | unsigned long loader; |
| 1039 | |
| 1040 | allow_write_access(bprm->file); |
| 1041 | fput(bprm->file); |
| 1042 | bprm->file = NULL; |
| 1043 | |
| 1044 | loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *); |
| 1045 | |
| 1046 | file = open_exec("/sbin/loader"); |
| 1047 | retval = PTR_ERR(file); |
| 1048 | if (IS_ERR(file)) |
| 1049 | return retval; |
| 1050 | |
| 1051 | /* Remember if the application is TASO. */ |
| 1052 | bprm->sh_bang = eh->ah.entry < 0x100000000UL; |
| 1053 | |
| 1054 | bprm->file = file; |
| 1055 | bprm->loader = loader; |
| 1056 | retval = prepare_binprm(bprm); |
| 1057 | if (retval<0) |
| 1058 | return retval; |
| 1059 | /* should call search_binary_handler recursively here, |
| 1060 | but it does not matter */ |
| 1061 | } |
| 1062 | } |
| 1063 | #endif |
| 1064 | retval = security_bprm_check(bprm); |
| 1065 | if (retval) |
| 1066 | return retval; |
| 1067 | |
| 1068 | /* kernel module loader fixup */ |
| 1069 | /* so we don't try to load run modprobe in kernel space. */ |
| 1070 | set_fs(USER_DS); |
| 1071 | retval = -ENOENT; |
| 1072 | for (try=0; try<2; try++) { |
| 1073 | read_lock(&binfmt_lock); |
| 1074 | for (fmt = formats ; fmt ; fmt = fmt->next) { |
| 1075 | int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary; |
| 1076 | if (!fn) |
| 1077 | continue; |
| 1078 | if (!try_module_get(fmt->module)) |
| 1079 | continue; |
| 1080 | read_unlock(&binfmt_lock); |
| 1081 | retval = fn(bprm, regs); |
| 1082 | if (retval >= 0) { |
| 1083 | put_binfmt(fmt); |
| 1084 | allow_write_access(bprm->file); |
| 1085 | if (bprm->file) |
| 1086 | fput(bprm->file); |
| 1087 | bprm->file = NULL; |
| 1088 | current->did_exec = 1; |
| 1089 | return retval; |
| 1090 | } |
| 1091 | read_lock(&binfmt_lock); |
| 1092 | put_binfmt(fmt); |
| 1093 | if (retval != -ENOEXEC || bprm->mm == NULL) |
| 1094 | break; |
| 1095 | if (!bprm->file) { |
| 1096 | read_unlock(&binfmt_lock); |
| 1097 | return retval; |
| 1098 | } |
| 1099 | } |
| 1100 | read_unlock(&binfmt_lock); |
| 1101 | if (retval != -ENOEXEC || bprm->mm == NULL) { |
| 1102 | break; |
| 1103 | #ifdef CONFIG_KMOD |
| 1104 | }else{ |
| 1105 | #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e)) |
| 1106 | if (printable(bprm->buf[0]) && |
| 1107 | printable(bprm->buf[1]) && |
| 1108 | printable(bprm->buf[2]) && |
| 1109 | printable(bprm->buf[3])) |
| 1110 | break; /* -ENOEXEC */ |
| 1111 | request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2])); |
| 1112 | #endif |
| 1113 | } |
| 1114 | } |
| 1115 | return retval; |
| 1116 | } |
| 1117 | |
| 1118 | EXPORT_SYMBOL(search_binary_handler); |
| 1119 | |
| 1120 | /* |
| 1121 | * sys_execve() executes a new program. |
| 1122 | */ |
| 1123 | int do_execve(char * filename, |
| 1124 | char __user *__user *argv, |
| 1125 | char __user *__user *envp, |
| 1126 | struct pt_regs * regs) |
| 1127 | { |
| 1128 | struct linux_binprm *bprm; |
| 1129 | struct file *file; |
| 1130 | int retval; |
| 1131 | int i; |
| 1132 | |
| 1133 | retval = -ENOMEM; |
| 1134 | bprm = kmalloc(sizeof(*bprm), GFP_KERNEL); |
| 1135 | if (!bprm) |
| 1136 | goto out_ret; |
| 1137 | memset(bprm, 0, sizeof(*bprm)); |
| 1138 | |
| 1139 | file = open_exec(filename); |
| 1140 | retval = PTR_ERR(file); |
| 1141 | if (IS_ERR(file)) |
| 1142 | goto out_kfree; |
| 1143 | |
| 1144 | sched_exec(); |
| 1145 | |
| 1146 | bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *); |
| 1147 | |
| 1148 | bprm->file = file; |
| 1149 | bprm->filename = filename; |
| 1150 | bprm->interp = filename; |
| 1151 | bprm->mm = mm_alloc(); |
| 1152 | retval = -ENOMEM; |
| 1153 | if (!bprm->mm) |
| 1154 | goto out_file; |
| 1155 | |
| 1156 | retval = init_new_context(current, bprm->mm); |
| 1157 | if (retval < 0) |
| 1158 | goto out_mm; |
| 1159 | |
| 1160 | bprm->argc = count(argv, bprm->p / sizeof(void *)); |
| 1161 | if ((retval = bprm->argc) < 0) |
| 1162 | goto out_mm; |
| 1163 | |
| 1164 | bprm->envc = count(envp, bprm->p / sizeof(void *)); |
| 1165 | if ((retval = bprm->envc) < 0) |
| 1166 | goto out_mm; |
| 1167 | |
| 1168 | retval = security_bprm_alloc(bprm); |
| 1169 | if (retval) |
| 1170 | goto out; |
| 1171 | |
| 1172 | retval = prepare_binprm(bprm); |
| 1173 | if (retval < 0) |
| 1174 | goto out; |
| 1175 | |
| 1176 | retval = copy_strings_kernel(1, &bprm->filename, bprm); |
| 1177 | if (retval < 0) |
| 1178 | goto out; |
| 1179 | |
| 1180 | bprm->exec = bprm->p; |
| 1181 | retval = copy_strings(bprm->envc, envp, bprm); |
| 1182 | if (retval < 0) |
| 1183 | goto out; |
| 1184 | |
| 1185 | retval = copy_strings(bprm->argc, argv, bprm); |
| 1186 | if (retval < 0) |
| 1187 | goto out; |
| 1188 | |
| 1189 | retval = search_binary_handler(bprm,regs); |
| 1190 | if (retval >= 0) { |
| 1191 | free_arg_pages(bprm); |
| 1192 | |
| 1193 | /* execve success */ |
| 1194 | security_bprm_free(bprm); |
| 1195 | acct_update_integrals(current); |
| 1196 | update_mem_hiwater(current); |
| 1197 | kfree(bprm); |
| 1198 | return retval; |
| 1199 | } |
| 1200 | |
| 1201 | out: |
| 1202 | /* Something went wrong, return the inode and free the argument pages*/ |
| 1203 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
| 1204 | struct page * page = bprm->page[i]; |
| 1205 | if (page) |
| 1206 | __free_page(page); |
| 1207 | } |
| 1208 | |
| 1209 | if (bprm->security) |
| 1210 | security_bprm_free(bprm); |
| 1211 | |
| 1212 | out_mm: |
| 1213 | if (bprm->mm) |
| 1214 | mmdrop(bprm->mm); |
| 1215 | |
| 1216 | out_file: |
| 1217 | if (bprm->file) { |
| 1218 | allow_write_access(bprm->file); |
| 1219 | fput(bprm->file); |
| 1220 | } |
| 1221 | |
| 1222 | out_kfree: |
| 1223 | kfree(bprm); |
| 1224 | |
| 1225 | out_ret: |
| 1226 | return retval; |
| 1227 | } |
| 1228 | |
| 1229 | int set_binfmt(struct linux_binfmt *new) |
| 1230 | { |
| 1231 | struct linux_binfmt *old = current->binfmt; |
| 1232 | |
| 1233 | if (new) { |
| 1234 | if (!try_module_get(new->module)) |
| 1235 | return -1; |
| 1236 | } |
| 1237 | current->binfmt = new; |
| 1238 | if (old) |
| 1239 | module_put(old->module); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | EXPORT_SYMBOL(set_binfmt); |
| 1244 | |
| 1245 | #define CORENAME_MAX_SIZE 64 |
| 1246 | |
| 1247 | /* format_corename will inspect the pattern parameter, and output a |
| 1248 | * name into corename, which must have space for at least |
| 1249 | * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator. |
| 1250 | */ |
| 1251 | static void format_corename(char *corename, const char *pattern, long signr) |
| 1252 | { |
| 1253 | const char *pat_ptr = pattern; |
| 1254 | char *out_ptr = corename; |
| 1255 | char *const out_end = corename + CORENAME_MAX_SIZE; |
| 1256 | int rc; |
| 1257 | int pid_in_pattern = 0; |
| 1258 | |
| 1259 | /* Repeat as long as we have more pattern to process and more output |
| 1260 | space */ |
| 1261 | while (*pat_ptr) { |
| 1262 | if (*pat_ptr != '%') { |
| 1263 | if (out_ptr == out_end) |
| 1264 | goto out; |
| 1265 | *out_ptr++ = *pat_ptr++; |
| 1266 | } else { |
| 1267 | switch (*++pat_ptr) { |
| 1268 | case 0: |
| 1269 | goto out; |
| 1270 | /* Double percent, output one percent */ |
| 1271 | case '%': |
| 1272 | if (out_ptr == out_end) |
| 1273 | goto out; |
| 1274 | *out_ptr++ = '%'; |
| 1275 | break; |
| 1276 | /* pid */ |
| 1277 | case 'p': |
| 1278 | pid_in_pattern = 1; |
| 1279 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1280 | "%d", current->tgid); |
| 1281 | if (rc > out_end - out_ptr) |
| 1282 | goto out; |
| 1283 | out_ptr += rc; |
| 1284 | break; |
| 1285 | /* uid */ |
| 1286 | case 'u': |
| 1287 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1288 | "%d", current->uid); |
| 1289 | if (rc > out_end - out_ptr) |
| 1290 | goto out; |
| 1291 | out_ptr += rc; |
| 1292 | break; |
| 1293 | /* gid */ |
| 1294 | case 'g': |
| 1295 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1296 | "%d", current->gid); |
| 1297 | if (rc > out_end - out_ptr) |
| 1298 | goto out; |
| 1299 | out_ptr += rc; |
| 1300 | break; |
| 1301 | /* signal that caused the coredump */ |
| 1302 | case 's': |
| 1303 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1304 | "%ld", signr); |
| 1305 | if (rc > out_end - out_ptr) |
| 1306 | goto out; |
| 1307 | out_ptr += rc; |
| 1308 | break; |
| 1309 | /* UNIX time of coredump */ |
| 1310 | case 't': { |
| 1311 | struct timeval tv; |
| 1312 | do_gettimeofday(&tv); |
| 1313 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1314 | "%lu", tv.tv_sec); |
| 1315 | if (rc > out_end - out_ptr) |
| 1316 | goto out; |
| 1317 | out_ptr += rc; |
| 1318 | break; |
| 1319 | } |
| 1320 | /* hostname */ |
| 1321 | case 'h': |
| 1322 | down_read(&uts_sem); |
| 1323 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1324 | "%s", system_utsname.nodename); |
| 1325 | up_read(&uts_sem); |
| 1326 | if (rc > out_end - out_ptr) |
| 1327 | goto out; |
| 1328 | out_ptr += rc; |
| 1329 | break; |
| 1330 | /* executable */ |
| 1331 | case 'e': |
| 1332 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1333 | "%s", current->comm); |
| 1334 | if (rc > out_end - out_ptr) |
| 1335 | goto out; |
| 1336 | out_ptr += rc; |
| 1337 | break; |
| 1338 | default: |
| 1339 | break; |
| 1340 | } |
| 1341 | ++pat_ptr; |
| 1342 | } |
| 1343 | } |
| 1344 | /* Backward compatibility with core_uses_pid: |
| 1345 | * |
| 1346 | * If core_pattern does not include a %p (as is the default) |
| 1347 | * and core_uses_pid is set, then .%pid will be appended to |
| 1348 | * the filename */ |
| 1349 | if (!pid_in_pattern |
| 1350 | && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) { |
| 1351 | rc = snprintf(out_ptr, out_end - out_ptr, |
| 1352 | ".%d", current->tgid); |
| 1353 | if (rc > out_end - out_ptr) |
| 1354 | goto out; |
| 1355 | out_ptr += rc; |
| 1356 | } |
| 1357 | out: |
| 1358 | *out_ptr = 0; |
| 1359 | } |
| 1360 | |
| 1361 | static void zap_threads (struct mm_struct *mm) |
| 1362 | { |
| 1363 | struct task_struct *g, *p; |
| 1364 | struct task_struct *tsk = current; |
| 1365 | struct completion *vfork_done = tsk->vfork_done; |
| 1366 | int traced = 0; |
| 1367 | |
| 1368 | /* |
| 1369 | * Make sure nobody is waiting for us to release the VM, |
| 1370 | * otherwise we can deadlock when we wait on each other |
| 1371 | */ |
| 1372 | if (vfork_done) { |
| 1373 | tsk->vfork_done = NULL; |
| 1374 | complete(vfork_done); |
| 1375 | } |
| 1376 | |
| 1377 | read_lock(&tasklist_lock); |
| 1378 | do_each_thread(g,p) |
| 1379 | if (mm == p->mm && p != tsk) { |
| 1380 | force_sig_specific(SIGKILL, p); |
| 1381 | mm->core_waiters++; |
| 1382 | if (unlikely(p->ptrace) && |
| 1383 | unlikely(p->parent->mm == mm)) |
| 1384 | traced = 1; |
| 1385 | } |
| 1386 | while_each_thread(g,p); |
| 1387 | |
| 1388 | read_unlock(&tasklist_lock); |
| 1389 | |
| 1390 | if (unlikely(traced)) { |
| 1391 | /* |
| 1392 | * We are zapping a thread and the thread it ptraces. |
| 1393 | * If the tracee went into a ptrace stop for exit tracing, |
| 1394 | * we could deadlock since the tracer is waiting for this |
| 1395 | * coredump to finish. Detach them so they can both die. |
| 1396 | */ |
| 1397 | write_lock_irq(&tasklist_lock); |
| 1398 | do_each_thread(g,p) { |
| 1399 | if (mm == p->mm && p != tsk && |
| 1400 | p->ptrace && p->parent->mm == mm) { |
| 1401 | __ptrace_unlink(p); |
| 1402 | } |
| 1403 | } while_each_thread(g,p); |
| 1404 | write_unlock_irq(&tasklist_lock); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | static void coredump_wait(struct mm_struct *mm) |
| 1409 | { |
| 1410 | DECLARE_COMPLETION(startup_done); |
| 1411 | |
| 1412 | mm->core_waiters++; /* let other threads block */ |
| 1413 | mm->core_startup_done = &startup_done; |
| 1414 | |
| 1415 | /* give other threads a chance to run: */ |
| 1416 | yield(); |
| 1417 | |
| 1418 | zap_threads(mm); |
| 1419 | if (--mm->core_waiters) { |
| 1420 | up_write(&mm->mmap_sem); |
| 1421 | wait_for_completion(&startup_done); |
| 1422 | } else |
| 1423 | up_write(&mm->mmap_sem); |
| 1424 | BUG_ON(mm->core_waiters); |
| 1425 | } |
| 1426 | |
| 1427 | int do_coredump(long signr, int exit_code, struct pt_regs * regs) |
| 1428 | { |
| 1429 | char corename[CORENAME_MAX_SIZE + 1]; |
| 1430 | struct mm_struct *mm = current->mm; |
| 1431 | struct linux_binfmt * binfmt; |
| 1432 | struct inode * inode; |
| 1433 | struct file * file; |
| 1434 | int retval = 0; |
| 1435 | |
| 1436 | binfmt = current->binfmt; |
| 1437 | if (!binfmt || !binfmt->core_dump) |
| 1438 | goto fail; |
| 1439 | down_write(&mm->mmap_sem); |
| 1440 | if (!mm->dumpable) { |
| 1441 | up_write(&mm->mmap_sem); |
| 1442 | goto fail; |
| 1443 | } |
| 1444 | mm->dumpable = 0; |
| 1445 | init_completion(&mm->core_done); |
| 1446 | spin_lock_irq(¤t->sighand->siglock); |
| 1447 | current->signal->flags = SIGNAL_GROUP_EXIT; |
| 1448 | current->signal->group_exit_code = exit_code; |
| 1449 | spin_unlock_irq(¤t->sighand->siglock); |
| 1450 | coredump_wait(mm); |
| 1451 | |
| 1452 | /* |
| 1453 | * Clear any false indication of pending signals that might |
| 1454 | * be seen by the filesystem code called to write the core file. |
| 1455 | */ |
| 1456 | current->signal->group_stop_count = 0; |
| 1457 | clear_thread_flag(TIF_SIGPENDING); |
| 1458 | |
| 1459 | if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump) |
| 1460 | goto fail_unlock; |
| 1461 | |
| 1462 | /* |
| 1463 | * lock_kernel() because format_corename() is controlled by sysctl, which |
| 1464 | * uses lock_kernel() |
| 1465 | */ |
| 1466 | lock_kernel(); |
| 1467 | format_corename(corename, core_pattern, signr); |
| 1468 | unlock_kernel(); |
| 1469 | file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600); |
| 1470 | if (IS_ERR(file)) |
| 1471 | goto fail_unlock; |
| 1472 | inode = file->f_dentry->d_inode; |
| 1473 | if (inode->i_nlink > 1) |
| 1474 | goto close_fail; /* multiple links - don't dump */ |
| 1475 | if (d_unhashed(file->f_dentry)) |
| 1476 | goto close_fail; |
| 1477 | |
| 1478 | if (!S_ISREG(inode->i_mode)) |
| 1479 | goto close_fail; |
| 1480 | if (!file->f_op) |
| 1481 | goto close_fail; |
| 1482 | if (!file->f_op->write) |
| 1483 | goto close_fail; |
| 1484 | if (do_truncate(file->f_dentry, 0) != 0) |
| 1485 | goto close_fail; |
| 1486 | |
| 1487 | retval = binfmt->core_dump(signr, regs, file); |
| 1488 | |
| 1489 | if (retval) |
| 1490 | current->signal->group_exit_code |= 0x80; |
| 1491 | close_fail: |
| 1492 | filp_close(file, NULL); |
| 1493 | fail_unlock: |
| 1494 | complete_all(&mm->core_done); |
| 1495 | fail: |
| 1496 | return retval; |
| 1497 | } |