exec.c 35 KB

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