aboutsummaryrefslogtreecommitdiffstats
path: root/fs/exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/exec.c')
-rw-r--r--fs/exec.c178
1 files changed, 146 insertions, 32 deletions
diff --git a/fs/exec.c b/fs/exec.c
index 5e62d26a4fec..ea5f748906a8 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -42,7 +42,6 @@
42#include <linux/pid_namespace.h> 42#include <linux/pid_namespace.h>
43#include <linux/module.h> 43#include <linux/module.h>
44#include <linux/namei.h> 44#include <linux/namei.h>
45#include <linux/proc_fs.h>
46#include <linux/mount.h> 45#include <linux/mount.h>
47#include <linux/security.h> 46#include <linux/security.h>
48#include <linux/syscalls.h> 47#include <linux/syscalls.h>
@@ -55,6 +54,7 @@
55#include <linux/fs_struct.h> 54#include <linux/fs_struct.h>
56#include <linux/pipe_fs_i.h> 55#include <linux/pipe_fs_i.h>
57#include <linux/oom.h> 56#include <linux/oom.h>
57#include <linux/compat.h>
58 58
59#include <asm/uaccess.h> 59#include <asm/uaccess.h>
60#include <asm/mmu_context.h> 60#include <asm/mmu_context.h>
@@ -166,8 +166,13 @@ out:
166} 166}
167 167
168#ifdef CONFIG_MMU 168#ifdef CONFIG_MMU
169 169/*
170void acct_arg_size(struct linux_binprm *bprm, unsigned long pages) 170 * The nascent bprm->mm is not visible until exec_mmap() but it can
171 * use a lot of memory, account these pages in current->mm temporary
172 * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
173 * change the counter back via acct_arg_size(0).
174 */
175static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
171{ 176{
172 struct mm_struct *mm = current->mm; 177 struct mm_struct *mm = current->mm;
173 long diff = (long)(pages - bprm->vma_pages); 178 long diff = (long)(pages - bprm->vma_pages);
@@ -186,7 +191,7 @@ void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
186#endif 191#endif
187} 192}
188 193
189struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, 194static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
190 int write) 195 int write)
191{ 196{
192 struct page *page; 197 struct page *page;
@@ -194,7 +199,7 @@ struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
194 199
195#ifdef CONFIG_STACK_GROWSUP 200#ifdef CONFIG_STACK_GROWSUP
196 if (write) { 201 if (write) {
197 ret = expand_stack_downwards(bprm->vma, pos); 202 ret = expand_downwards(bprm->vma, pos);
198 if (ret < 0) 203 if (ret < 0)
199 return NULL; 204 return NULL;
200 } 205 }
@@ -305,11 +310,11 @@ static bool valid_arg_len(struct linux_binprm *bprm, long len)
305 310
306#else 311#else
307 312
308void acct_arg_size(struct linux_binprm *bprm, unsigned long pages) 313static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
309{ 314{
310} 315}
311 316
312struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, 317static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
313 int write) 318 int write)
314{ 319{
315 struct page *page; 320 struct page *page;
@@ -398,22 +403,56 @@ err:
398 return err; 403 return err;
399} 404}
400 405
406struct user_arg_ptr {
407#ifdef CONFIG_COMPAT
408 bool is_compat;
409#endif
410 union {
411 const char __user *const __user *native;
412#ifdef CONFIG_COMPAT
413 compat_uptr_t __user *compat;
414#endif
415 } ptr;
416};
417
418static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
419{
420 const char __user *native;
421
422#ifdef CONFIG_COMPAT
423 if (unlikely(argv.is_compat)) {
424 compat_uptr_t compat;
425
426 if (get_user(compat, argv.ptr.compat + nr))
427 return ERR_PTR(-EFAULT);
428
429 return compat_ptr(compat);
430 }
431#endif
432
433 if (get_user(native, argv.ptr.native + nr))
434 return ERR_PTR(-EFAULT);
435
436 return native;
437}
438
401/* 439/*
402 * count() counts the number of strings in array ARGV. 440 * count() counts the number of strings in array ARGV.
403 */ 441 */
404static int count(const char __user * const __user * argv, int max) 442static int count(struct user_arg_ptr argv, int max)
405{ 443{
406 int i = 0; 444 int i = 0;
407 445
408 if (argv != NULL) { 446 if (argv.ptr.native != NULL) {
409 for (;;) { 447 for (;;) {
410 const char __user * p; 448 const char __user *p = get_user_arg_ptr(argv, i);
411 449
412 if (get_user(p, argv))
413 return -EFAULT;
414 if (!p) 450 if (!p)
415 break; 451 break;
416 argv++; 452
453 if (IS_ERR(p))
454 return -EFAULT;
455
417 if (i++ >= max) 456 if (i++ >= max)
418 return -E2BIG; 457 return -E2BIG;
419 458
@@ -430,7 +469,7 @@ static int count(const char __user * const __user * argv, int max)
430 * processes's memory to the new process's stack. The call to get_user_pages() 469 * processes's memory to the new process's stack. The call to get_user_pages()
431 * ensures the destination page is created and not swapped out. 470 * ensures the destination page is created and not swapped out.
432 */ 471 */
433static int copy_strings(int argc, const char __user *const __user *argv, 472static int copy_strings(int argc, struct user_arg_ptr argv,
434 struct linux_binprm *bprm) 473 struct linux_binprm *bprm)
435{ 474{
436 struct page *kmapped_page = NULL; 475 struct page *kmapped_page = NULL;
@@ -443,16 +482,18 @@ static int copy_strings(int argc, const char __user *const __user *argv,
443 int len; 482 int len;
444 unsigned long pos; 483 unsigned long pos;
445 484
446 if (get_user(str, argv+argc) || 485 ret = -EFAULT;
447 !(len = strnlen_user(str, MAX_ARG_STRLEN))) { 486 str = get_user_arg_ptr(argv, argc);
448 ret = -EFAULT; 487 if (IS_ERR(str))
449 goto out; 488 goto out;
450 }
451 489
452 if (!valid_arg_len(bprm, len)) { 490 len = strnlen_user(str, MAX_ARG_STRLEN);
453 ret = -E2BIG; 491 if (!len)
492 goto out;
493
494 ret = -E2BIG;
495 if (!valid_arg_len(bprm, len))
454 goto out; 496 goto out;
455 }
456 497
457 /* We're going to work our way backwords. */ 498 /* We're going to work our way backwords. */
458 pos = bprm->p; 499 pos = bprm->p;
@@ -519,14 +560,19 @@ out:
519/* 560/*
520 * Like copy_strings, but get argv and its values from kernel memory. 561 * Like copy_strings, but get argv and its values from kernel memory.
521 */ 562 */
522int copy_strings_kernel(int argc, const char *const *argv, 563int copy_strings_kernel(int argc, const char *const *__argv,
523 struct linux_binprm *bprm) 564 struct linux_binprm *bprm)
524{ 565{
525 int r; 566 int r;
526 mm_segment_t oldfs = get_fs(); 567 mm_segment_t oldfs = get_fs();
568 struct user_arg_ptr argv = {
569 .ptr.native = (const char __user *const __user *)__argv,
570 };
571
527 set_fs(KERNEL_DS); 572 set_fs(KERNEL_DS);
528 r = copy_strings(argc, (const char __user *const __user *)argv, bprm); 573 r = copy_strings(argc, argv, bprm);
529 set_fs(oldfs); 574 set_fs(oldfs);
575
530 return r; 576 return r;
531} 577}
532EXPORT_SYMBOL(copy_strings_kernel); 578EXPORT_SYMBOL(copy_strings_kernel);
@@ -553,7 +599,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
553 unsigned long length = old_end - old_start; 599 unsigned long length = old_end - old_start;
554 unsigned long new_start = old_start - shift; 600 unsigned long new_start = old_start - shift;
555 unsigned long new_end = old_end - shift; 601 unsigned long new_end = old_end - shift;
556 struct mmu_gather *tlb; 602 struct mmu_gather tlb;
557 603
558 BUG_ON(new_start > new_end); 604 BUG_ON(new_start > new_end);
559 605
@@ -579,12 +625,12 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
579 return -ENOMEM; 625 return -ENOMEM;
580 626
581 lru_add_drain(); 627 lru_add_drain();
582 tlb = tlb_gather_mmu(mm, 0); 628 tlb_gather_mmu(&tlb, mm, 0);
583 if (new_end > old_start) { 629 if (new_end > old_start) {
584 /* 630 /*
585 * when the old and new regions overlap clear from new_end. 631 * when the old and new regions overlap clear from new_end.
586 */ 632 */
587 free_pgd_range(tlb, new_end, old_end, new_end, 633 free_pgd_range(&tlb, new_end, old_end, new_end,
588 vma->vm_next ? vma->vm_next->vm_start : 0); 634 vma->vm_next ? vma->vm_next->vm_start : 0);
589 } else { 635 } else {
590 /* 636 /*
@@ -593,10 +639,10 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
593 * have constraints on va-space that make this illegal (IA64) - 639 * have constraints on va-space that make this illegal (IA64) -
594 * for the others its just a little faster. 640 * for the others its just a little faster.
595 */ 641 */
596 free_pgd_range(tlb, old_start, old_end, new_end, 642 free_pgd_range(&tlb, old_start, old_end, new_end,
597 vma->vm_next ? vma->vm_next->vm_start : 0); 643 vma->vm_next ? vma->vm_next->vm_start : 0);
598 } 644 }
599 tlb_finish_mmu(tlb, new_end, old_end); 645 tlb_finish_mmu(&tlb, new_end, old_end);
600 646
601 /* 647 /*
602 * Shrink the vma to just the new range. Always succeeds. 648 * Shrink the vma to just the new range. Always succeeds.
@@ -1004,6 +1050,7 @@ char *get_task_comm(char *buf, struct task_struct *tsk)
1004 task_unlock(tsk); 1050 task_unlock(tsk);
1005 return buf; 1051 return buf;
1006} 1052}
1053EXPORT_SYMBOL_GPL(get_task_comm);
1007 1054
1008void set_task_comm(struct task_struct *tsk, char *buf) 1055void set_task_comm(struct task_struct *tsk, char *buf)
1009{ 1056{
@@ -1379,10 +1426,10 @@ EXPORT_SYMBOL(search_binary_handler);
1379/* 1426/*
1380 * sys_execve() executes a new program. 1427 * sys_execve() executes a new program.
1381 */ 1428 */
1382int do_execve(const char * filename, 1429static int do_execve_common(const char *filename,
1383 const char __user *const __user *argv, 1430 struct user_arg_ptr argv,
1384 const char __user *const __user *envp, 1431 struct user_arg_ptr envp,
1385 struct pt_regs * regs) 1432 struct pt_regs *regs)
1386{ 1433{
1387 struct linux_binprm *bprm; 1434 struct linux_binprm *bprm;
1388 struct file *file; 1435 struct file *file;
@@ -1489,6 +1536,34 @@ out_ret:
1489 return retval; 1536 return retval;
1490} 1537}
1491 1538
1539int do_execve(const char *filename,
1540 const char __user *const __user *__argv,
1541 const char __user *const __user *__envp,
1542 struct pt_regs *regs)
1543{
1544 struct user_arg_ptr argv = { .ptr.native = __argv };
1545 struct user_arg_ptr envp = { .ptr.native = __envp };
1546 return do_execve_common(filename, argv, envp, regs);
1547}
1548
1549#ifdef CONFIG_COMPAT
1550int compat_do_execve(char *filename,
1551 compat_uptr_t __user *__argv,
1552 compat_uptr_t __user *__envp,
1553 struct pt_regs *regs)
1554{
1555 struct user_arg_ptr argv = {
1556 .is_compat = true,
1557 .ptr.compat = __argv,
1558 };
1559 struct user_arg_ptr envp = {
1560 .is_compat = true,
1561 .ptr.compat = __envp,
1562 };
1563 return do_execve_common(filename, argv, envp, regs);
1564}
1565#endif
1566
1492void set_binfmt(struct linux_binfmt *new) 1567void set_binfmt(struct linux_binfmt *new)
1493{ 1568{
1494 struct mm_struct *mm = current->mm; 1569 struct mm_struct *mm = current->mm;
@@ -1548,6 +1623,41 @@ expand_fail:
1548 return ret; 1623 return ret;
1549} 1624}
1550 1625
1626static int cn_print_exe_file(struct core_name *cn)
1627{
1628 struct file *exe_file;
1629 char *pathbuf, *path, *p;
1630 int ret;
1631
1632 exe_file = get_mm_exe_file(current->mm);
1633 if (!exe_file)
1634 return cn_printf(cn, "(unknown)");
1635
1636 pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
1637 if (!pathbuf) {
1638 ret = -ENOMEM;
1639 goto put_exe_file;
1640 }
1641
1642 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
1643 if (IS_ERR(path)) {
1644 ret = PTR_ERR(path);
1645 goto free_buf;
1646 }
1647
1648 for (p = path; *p; p++)
1649 if (*p == '/')
1650 *p = '!';
1651
1652 ret = cn_printf(cn, "%s", path);
1653
1654free_buf:
1655 kfree(pathbuf);
1656put_exe_file:
1657 fput(exe_file);
1658 return ret;
1659}
1660
1551/* format_corename will inspect the pattern parameter, and output a 1661/* format_corename will inspect the pattern parameter, and output a
1552 * name into corename, which must have space for at least 1662 * name into corename, which must have space for at least
1553 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator. 1663 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
@@ -1619,6 +1729,9 @@ static int format_corename(struct core_name *cn, long signr)
1619 case 'e': 1729 case 'e':
1620 err = cn_printf(cn, "%s", current->comm); 1730 err = cn_printf(cn, "%s", current->comm);
1621 break; 1731 break;
1732 case 'E':
1733 err = cn_print_exe_file(cn);
1734 break;
1622 /* core limit size */ 1735 /* core limit size */
1623 case 'c': 1736 case 'c':
1624 err = cn_printf(cn, "%lu", 1737 err = cn_printf(cn, "%lu",
@@ -1659,6 +1772,7 @@ static int zap_process(struct task_struct *start, int exit_code)
1659 1772
1660 t = start; 1773 t = start;
1661 do { 1774 do {
1775 task_clear_group_stop_pending(t);
1662 if (t != current && t->mm) { 1776 if (t != current && t->mm) {
1663 sigaddset(&t->pending.signal, SIGKILL); 1777 sigaddset(&t->pending.signal, SIGKILL);
1664 signal_wake_up(t, 1); 1778 signal_wake_up(t, 1);