exec.c 35 KB

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