exec.c 34 KB

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