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