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