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