exec.c 34 KB

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