exec.c 35 KB

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