exec.c 35 KB

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