exec.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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. /*
  544. * child_reaper ignores SIGKILL, change it now.
  545. * Reparenting needs write_lock on tasklist_lock,
  546. * so it is safe to do it under read_lock.
  547. */
  548. if (unlikely(current->group_leader == child_reaper))
  549. child_reaper = current;
  550. zap_other_threads(current);
  551. read_unlock(&tasklist_lock);
  552. /*
  553. * Account for the thread group leader hanging around:
  554. */
  555. count = 1;
  556. if (!thread_group_leader(current)) {
  557. count = 2;
  558. /*
  559. * The SIGALRM timer survives the exec, but needs to point
  560. * at us as the new group leader now. We have a race with
  561. * a timer firing now getting the old leader, so we need to
  562. * synchronize with any firing (by calling del_timer_sync)
  563. * before we can safely let the old group leader die.
  564. */
  565. sig->tsk = current;
  566. spin_unlock_irq(lock);
  567. if (hrtimer_cancel(&sig->real_timer))
  568. hrtimer_restart(&sig->real_timer);
  569. spin_lock_irq(lock);
  570. }
  571. while (atomic_read(&sig->count) > count) {
  572. sig->group_exit_task = current;
  573. sig->notify_count = count;
  574. __set_current_state(TASK_UNINTERRUPTIBLE);
  575. spin_unlock_irq(lock);
  576. schedule();
  577. spin_lock_irq(lock);
  578. }
  579. sig->group_exit_task = NULL;
  580. sig->notify_count = 0;
  581. spin_unlock_irq(lock);
  582. /*
  583. * At this point all other threads have exited, all we have to
  584. * do is to wait for the thread group leader to become inactive,
  585. * and to assume its PID:
  586. */
  587. if (!thread_group_leader(current)) {
  588. struct task_struct *parent;
  589. struct dentry *proc_dentry1, *proc_dentry2;
  590. unsigned long ptrace;
  591. /*
  592. * Wait for the thread group leader to be a zombie.
  593. * It should already be zombie at this point, most
  594. * of the time.
  595. */
  596. leader = current->group_leader;
  597. while (leader->exit_state != EXIT_ZOMBIE)
  598. yield();
  599. spin_lock(&leader->proc_lock);
  600. spin_lock(&current->proc_lock);
  601. proc_dentry1 = proc_pid_unhash(current);
  602. proc_dentry2 = proc_pid_unhash(leader);
  603. write_lock_irq(&tasklist_lock);
  604. BUG_ON(leader->tgid != current->tgid);
  605. BUG_ON(current->pid == current->tgid);
  606. /*
  607. * An exec() starts a new thread group with the
  608. * TGID of the previous thread group. Rehash the
  609. * two threads with a switched PID, and release
  610. * the former thread group leader:
  611. */
  612. ptrace = leader->ptrace;
  613. parent = leader->parent;
  614. if (unlikely(ptrace) && unlikely(parent == current)) {
  615. /*
  616. * Joker was ptracing his own group leader,
  617. * and now he wants to be his own parent!
  618. * We can't have that.
  619. */
  620. ptrace = 0;
  621. }
  622. ptrace_unlink(current);
  623. ptrace_unlink(leader);
  624. remove_parent(current);
  625. remove_parent(leader);
  626. /* Become a process group leader with the old leader's pid.
  627. * Note: The old leader also uses thispid until release_task
  628. * is called. Odd but simple and correct.
  629. */
  630. detach_pid(current, PIDTYPE_PID);
  631. current->pid = leader->pid;
  632. attach_pid(current, PIDTYPE_PID, current->pid);
  633. attach_pid(current, PIDTYPE_PGID, current->signal->pgrp);
  634. attach_pid(current, PIDTYPE_SID, current->signal->session);
  635. list_add_tail(&current->tasks, &init_task.tasks);
  636. current->parent = current->real_parent = leader->real_parent;
  637. leader->parent = leader->real_parent = child_reaper;
  638. current->group_leader = current;
  639. leader->group_leader = leader;
  640. add_parent(current, current->parent);
  641. add_parent(leader, leader->parent);
  642. if (ptrace) {
  643. current->ptrace = ptrace;
  644. __ptrace_link(current, parent);
  645. }
  646. current->exit_signal = SIGCHLD;
  647. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  648. leader->exit_state = EXIT_DEAD;
  649. write_unlock_irq(&tasklist_lock);
  650. spin_unlock(&leader->proc_lock);
  651. spin_unlock(&current->proc_lock);
  652. proc_pid_flush(proc_dentry1);
  653. proc_pid_flush(proc_dentry2);
  654. }
  655. /*
  656. * There may be one thread left which is just exiting,
  657. * but it's safe to stop telling the group to kill themselves.
  658. */
  659. sig->flags = 0;
  660. no_thread_group:
  661. exit_itimers(sig);
  662. if (leader)
  663. release_task(leader);
  664. BUG_ON(atomic_read(&sig->count) != 1);
  665. if (atomic_read(&oldsighand->count) == 1) {
  666. /*
  667. * Now that we nuked the rest of the thread group,
  668. * it turns out we are not sharing sighand any more either.
  669. * So we can just keep it.
  670. */
  671. kmem_cache_free(sighand_cachep, newsighand);
  672. } else {
  673. /*
  674. * Move our state over to newsighand and switch it in.
  675. */
  676. spin_lock_init(&newsighand->siglock);
  677. atomic_set(&newsighand->count, 1);
  678. memcpy(newsighand->action, oldsighand->action,
  679. sizeof(newsighand->action));
  680. write_lock_irq(&tasklist_lock);
  681. spin_lock(&oldsighand->siglock);
  682. spin_lock(&newsighand->siglock);
  683. rcu_assign_pointer(current->sighand, newsighand);
  684. recalc_sigpending();
  685. spin_unlock(&newsighand->siglock);
  686. spin_unlock(&oldsighand->siglock);
  687. write_unlock_irq(&tasklist_lock);
  688. if (atomic_dec_and_test(&oldsighand->count))
  689. sighand_free(oldsighand);
  690. }
  691. BUG_ON(!thread_group_leader(current));
  692. return 0;
  693. }
  694. /*
  695. * These functions flushes out all traces of the currently running executable
  696. * so that a new one can be started
  697. */
  698. static void flush_old_files(struct files_struct * files)
  699. {
  700. long j = -1;
  701. struct fdtable *fdt;
  702. spin_lock(&files->file_lock);
  703. for (;;) {
  704. unsigned long set, i;
  705. j++;
  706. i = j * __NFDBITS;
  707. fdt = files_fdtable(files);
  708. if (i >= fdt->max_fds || i >= fdt->max_fdset)
  709. break;
  710. set = fdt->close_on_exec->fds_bits[j];
  711. if (!set)
  712. continue;
  713. fdt->close_on_exec->fds_bits[j] = 0;
  714. spin_unlock(&files->file_lock);
  715. for ( ; set ; i++,set >>= 1) {
  716. if (set & 1) {
  717. sys_close(i);
  718. }
  719. }
  720. spin_lock(&files->file_lock);
  721. }
  722. spin_unlock(&files->file_lock);
  723. }
  724. void get_task_comm(char *buf, struct task_struct *tsk)
  725. {
  726. /* buf must be at least sizeof(tsk->comm) in size */
  727. task_lock(tsk);
  728. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  729. task_unlock(tsk);
  730. }
  731. void set_task_comm(struct task_struct *tsk, char *buf)
  732. {
  733. task_lock(tsk);
  734. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  735. task_unlock(tsk);
  736. }
  737. int flush_old_exec(struct linux_binprm * bprm)
  738. {
  739. char * name;
  740. int i, ch, retval;
  741. struct files_struct *files;
  742. char tcomm[sizeof(current->comm)];
  743. /*
  744. * Make sure we have a private signal table and that
  745. * we are unassociated from the previous thread group.
  746. */
  747. retval = de_thread(current);
  748. if (retval)
  749. goto out;
  750. /*
  751. * Make sure we have private file handles. Ask the
  752. * fork helper to do the work for us and the exit
  753. * helper to do the cleanup of the old one.
  754. */
  755. files = current->files; /* refcounted so safe to hold */
  756. retval = unshare_files();
  757. if (retval)
  758. goto out;
  759. /*
  760. * Release all of the old mmap stuff
  761. */
  762. retval = exec_mmap(bprm->mm);
  763. if (retval)
  764. goto mmap_failed;
  765. bprm->mm = NULL; /* We're using it now */
  766. /* This is the point of no return */
  767. steal_locks(files);
  768. put_files_struct(files);
  769. current->sas_ss_sp = current->sas_ss_size = 0;
  770. if (current->euid == current->uid && current->egid == current->gid)
  771. current->mm->dumpable = 1;
  772. else
  773. current->mm->dumpable = suid_dumpable;
  774. name = bprm->filename;
  775. /* Copies the binary name from after last slash */
  776. for (i=0; (ch = *(name++)) != '\0';) {
  777. if (ch == '/')
  778. i = 0; /* overwrite what we wrote */
  779. else
  780. if (i < (sizeof(tcomm) - 1))
  781. tcomm[i++] = ch;
  782. }
  783. tcomm[i] = '\0';
  784. set_task_comm(current, tcomm);
  785. current->flags &= ~PF_RANDOMIZE;
  786. flush_thread();
  787. /* Set the new mm task size. We have to do that late because it may
  788. * depend on TIF_32BIT which is only updated in flush_thread() on
  789. * some architectures like powerpc
  790. */
  791. current->mm->task_size = TASK_SIZE;
  792. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
  793. file_permission(bprm->file, MAY_READ) ||
  794. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  795. suid_keys(current);
  796. current->mm->dumpable = suid_dumpable;
  797. }
  798. /* An exec changes our domain. We are no longer part of the thread
  799. group */
  800. current->self_exec_id++;
  801. flush_signal_handlers(current, 0);
  802. flush_old_files(current->files);
  803. return 0;
  804. mmap_failed:
  805. put_files_struct(current->files);
  806. current->files = files;
  807. out:
  808. return retval;
  809. }
  810. EXPORT_SYMBOL(flush_old_exec);
  811. /*
  812. * Fill the binprm structure from the inode.
  813. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  814. */
  815. int prepare_binprm(struct linux_binprm *bprm)
  816. {
  817. int mode;
  818. struct inode * inode = bprm->file->f_dentry->d_inode;
  819. int retval;
  820. mode = inode->i_mode;
  821. /*
  822. * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
  823. * generic_permission lets a non-executable through
  824. */
  825. if (!(mode & 0111)) /* with at least _one_ execute bit set */
  826. return -EACCES;
  827. if (bprm->file->f_op == NULL)
  828. return -EACCES;
  829. bprm->e_uid = current->euid;
  830. bprm->e_gid = current->egid;
  831. if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
  832. /* Set-uid? */
  833. if (mode & S_ISUID) {
  834. current->personality &= ~PER_CLEAR_ON_SETID;
  835. bprm->e_uid = inode->i_uid;
  836. }
  837. /* Set-gid? */
  838. /*
  839. * If setgid is set but no group execute bit then this
  840. * is a candidate for mandatory locking, not a setgid
  841. * executable.
  842. */
  843. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  844. current->personality &= ~PER_CLEAR_ON_SETID;
  845. bprm->e_gid = inode->i_gid;
  846. }
  847. }
  848. /* fill in binprm security blob */
  849. retval = security_bprm_set(bprm);
  850. if (retval)
  851. return retval;
  852. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  853. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  854. }
  855. EXPORT_SYMBOL(prepare_binprm);
  856. static int unsafe_exec(struct task_struct *p)
  857. {
  858. int unsafe = 0;
  859. if (p->ptrace & PT_PTRACED) {
  860. if (p->ptrace & PT_PTRACE_CAP)
  861. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  862. else
  863. unsafe |= LSM_UNSAFE_PTRACE;
  864. }
  865. if (atomic_read(&p->fs->count) > 1 ||
  866. atomic_read(&p->files->count) > 1 ||
  867. atomic_read(&p->sighand->count) > 1)
  868. unsafe |= LSM_UNSAFE_SHARE;
  869. return unsafe;
  870. }
  871. void compute_creds(struct linux_binprm *bprm)
  872. {
  873. int unsafe;
  874. if (bprm->e_uid != current->uid)
  875. suid_keys(current);
  876. exec_keys(current);
  877. task_lock(current);
  878. unsafe = unsafe_exec(current);
  879. security_bprm_apply_creds(bprm, unsafe);
  880. task_unlock(current);
  881. security_bprm_post_apply_creds(bprm);
  882. }
  883. EXPORT_SYMBOL(compute_creds);
  884. void remove_arg_zero(struct linux_binprm *bprm)
  885. {
  886. if (bprm->argc) {
  887. unsigned long offset;
  888. char * kaddr;
  889. struct page *page;
  890. offset = bprm->p % PAGE_SIZE;
  891. goto inside;
  892. while (bprm->p++, *(kaddr+offset++)) {
  893. if (offset != PAGE_SIZE)
  894. continue;
  895. offset = 0;
  896. kunmap_atomic(kaddr, KM_USER0);
  897. inside:
  898. page = bprm->page[bprm->p/PAGE_SIZE];
  899. kaddr = kmap_atomic(page, KM_USER0);
  900. }
  901. kunmap_atomic(kaddr, KM_USER0);
  902. bprm->argc--;
  903. }
  904. }
  905. EXPORT_SYMBOL(remove_arg_zero);
  906. /*
  907. * cycle the list of binary formats handler, until one recognizes the image
  908. */
  909. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  910. {
  911. int try,retval;
  912. struct linux_binfmt *fmt;
  913. #ifdef __alpha__
  914. /* handle /sbin/loader.. */
  915. {
  916. struct exec * eh = (struct exec *) bprm->buf;
  917. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  918. (eh->fh.f_flags & 0x3000) == 0x3000)
  919. {
  920. struct file * file;
  921. unsigned long loader;
  922. allow_write_access(bprm->file);
  923. fput(bprm->file);
  924. bprm->file = NULL;
  925. loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  926. file = open_exec("/sbin/loader");
  927. retval = PTR_ERR(file);
  928. if (IS_ERR(file))
  929. return retval;
  930. /* Remember if the application is TASO. */
  931. bprm->sh_bang = eh->ah.entry < 0x100000000UL;
  932. bprm->file = file;
  933. bprm->loader = loader;
  934. retval = prepare_binprm(bprm);
  935. if (retval<0)
  936. return retval;
  937. /* should call search_binary_handler recursively here,
  938. but it does not matter */
  939. }
  940. }
  941. #endif
  942. retval = security_bprm_check(bprm);
  943. if (retval)
  944. return retval;
  945. /* kernel module loader fixup */
  946. /* so we don't try to load run modprobe in kernel space. */
  947. set_fs(USER_DS);
  948. retval = -ENOENT;
  949. for (try=0; try<2; try++) {
  950. read_lock(&binfmt_lock);
  951. for (fmt = formats ; fmt ; fmt = fmt->next) {
  952. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  953. if (!fn)
  954. continue;
  955. if (!try_module_get(fmt->module))
  956. continue;
  957. read_unlock(&binfmt_lock);
  958. retval = fn(bprm, regs);
  959. if (retval >= 0) {
  960. put_binfmt(fmt);
  961. allow_write_access(bprm->file);
  962. if (bprm->file)
  963. fput(bprm->file);
  964. bprm->file = NULL;
  965. current->did_exec = 1;
  966. proc_exec_connector(current);
  967. return retval;
  968. }
  969. read_lock(&binfmt_lock);
  970. put_binfmt(fmt);
  971. if (retval != -ENOEXEC || bprm->mm == NULL)
  972. break;
  973. if (!bprm->file) {
  974. read_unlock(&binfmt_lock);
  975. return retval;
  976. }
  977. }
  978. read_unlock(&binfmt_lock);
  979. if (retval != -ENOEXEC || bprm->mm == NULL) {
  980. break;
  981. #ifdef CONFIG_KMOD
  982. }else{
  983. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  984. if (printable(bprm->buf[0]) &&
  985. printable(bprm->buf[1]) &&
  986. printable(bprm->buf[2]) &&
  987. printable(bprm->buf[3]))
  988. break; /* -ENOEXEC */
  989. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  990. #endif
  991. }
  992. }
  993. return retval;
  994. }
  995. EXPORT_SYMBOL(search_binary_handler);
  996. /*
  997. * sys_execve() executes a new program.
  998. */
  999. int do_execve(char * filename,
  1000. char __user *__user *argv,
  1001. char __user *__user *envp,
  1002. struct pt_regs * regs)
  1003. {
  1004. struct linux_binprm *bprm;
  1005. struct file *file;
  1006. int retval;
  1007. int i;
  1008. retval = -ENOMEM;
  1009. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1010. if (!bprm)
  1011. goto out_ret;
  1012. file = open_exec(filename);
  1013. retval = PTR_ERR(file);
  1014. if (IS_ERR(file))
  1015. goto out_kfree;
  1016. sched_exec();
  1017. bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  1018. bprm->file = file;
  1019. bprm->filename = filename;
  1020. bprm->interp = filename;
  1021. bprm->mm = mm_alloc();
  1022. retval = -ENOMEM;
  1023. if (!bprm->mm)
  1024. goto out_file;
  1025. retval = init_new_context(current, bprm->mm);
  1026. if (retval < 0)
  1027. goto out_mm;
  1028. bprm->argc = count(argv, bprm->p / sizeof(void *));
  1029. if ((retval = bprm->argc) < 0)
  1030. goto out_mm;
  1031. bprm->envc = count(envp, bprm->p / sizeof(void *));
  1032. if ((retval = bprm->envc) < 0)
  1033. goto out_mm;
  1034. retval = security_bprm_alloc(bprm);
  1035. if (retval)
  1036. goto out;
  1037. retval = prepare_binprm(bprm);
  1038. if (retval < 0)
  1039. goto out;
  1040. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1041. if (retval < 0)
  1042. goto out;
  1043. bprm->exec = bprm->p;
  1044. retval = copy_strings(bprm->envc, envp, bprm);
  1045. if (retval < 0)
  1046. goto out;
  1047. retval = copy_strings(bprm->argc, argv, bprm);
  1048. if (retval < 0)
  1049. goto out;
  1050. retval = search_binary_handler(bprm,regs);
  1051. if (retval >= 0) {
  1052. free_arg_pages(bprm);
  1053. /* execve success */
  1054. security_bprm_free(bprm);
  1055. acct_update_integrals(current);
  1056. kfree(bprm);
  1057. return retval;
  1058. }
  1059. out:
  1060. /* Something went wrong, return the inode and free the argument pages*/
  1061. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  1062. struct page * page = bprm->page[i];
  1063. if (page)
  1064. __free_page(page);
  1065. }
  1066. if (bprm->security)
  1067. security_bprm_free(bprm);
  1068. out_mm:
  1069. if (bprm->mm)
  1070. mmdrop(bprm->mm);
  1071. out_file:
  1072. if (bprm->file) {
  1073. allow_write_access(bprm->file);
  1074. fput(bprm->file);
  1075. }
  1076. out_kfree:
  1077. kfree(bprm);
  1078. out_ret:
  1079. return retval;
  1080. }
  1081. int set_binfmt(struct linux_binfmt *new)
  1082. {
  1083. struct linux_binfmt *old = current->binfmt;
  1084. if (new) {
  1085. if (!try_module_get(new->module))
  1086. return -1;
  1087. }
  1088. current->binfmt = new;
  1089. if (old)
  1090. module_put(old->module);
  1091. return 0;
  1092. }
  1093. EXPORT_SYMBOL(set_binfmt);
  1094. #define CORENAME_MAX_SIZE 64
  1095. /* format_corename will inspect the pattern parameter, and output a
  1096. * name into corename, which must have space for at least
  1097. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1098. */
  1099. static void format_corename(char *corename, const char *pattern, long signr)
  1100. {
  1101. const char *pat_ptr = pattern;
  1102. char *out_ptr = corename;
  1103. char *const out_end = corename + CORENAME_MAX_SIZE;
  1104. int rc;
  1105. int pid_in_pattern = 0;
  1106. /* Repeat as long as we have more pattern to process and more output
  1107. space */
  1108. while (*pat_ptr) {
  1109. if (*pat_ptr != '%') {
  1110. if (out_ptr == out_end)
  1111. goto out;
  1112. *out_ptr++ = *pat_ptr++;
  1113. } else {
  1114. switch (*++pat_ptr) {
  1115. case 0:
  1116. goto out;
  1117. /* Double percent, output one percent */
  1118. case '%':
  1119. if (out_ptr == out_end)
  1120. goto out;
  1121. *out_ptr++ = '%';
  1122. break;
  1123. /* pid */
  1124. case 'p':
  1125. pid_in_pattern = 1;
  1126. rc = snprintf(out_ptr, out_end - out_ptr,
  1127. "%d", current->tgid);
  1128. if (rc > out_end - out_ptr)
  1129. goto out;
  1130. out_ptr += rc;
  1131. break;
  1132. /* uid */
  1133. case 'u':
  1134. rc = snprintf(out_ptr, out_end - out_ptr,
  1135. "%d", current->uid);
  1136. if (rc > out_end - out_ptr)
  1137. goto out;
  1138. out_ptr += rc;
  1139. break;
  1140. /* gid */
  1141. case 'g':
  1142. rc = snprintf(out_ptr, out_end - out_ptr,
  1143. "%d", current->gid);
  1144. if (rc > out_end - out_ptr)
  1145. goto out;
  1146. out_ptr += rc;
  1147. break;
  1148. /* signal that caused the coredump */
  1149. case 's':
  1150. rc = snprintf(out_ptr, out_end - out_ptr,
  1151. "%ld", signr);
  1152. if (rc > out_end - out_ptr)
  1153. goto out;
  1154. out_ptr += rc;
  1155. break;
  1156. /* UNIX time of coredump */
  1157. case 't': {
  1158. struct timeval tv;
  1159. do_gettimeofday(&tv);
  1160. rc = snprintf(out_ptr, out_end - out_ptr,
  1161. "%lu", tv.tv_sec);
  1162. if (rc > out_end - out_ptr)
  1163. goto out;
  1164. out_ptr += rc;
  1165. break;
  1166. }
  1167. /* hostname */
  1168. case 'h':
  1169. down_read(&uts_sem);
  1170. rc = snprintf(out_ptr, out_end - out_ptr,
  1171. "%s", system_utsname.nodename);
  1172. up_read(&uts_sem);
  1173. if (rc > out_end - out_ptr)
  1174. goto out;
  1175. out_ptr += rc;
  1176. break;
  1177. /* executable */
  1178. case 'e':
  1179. rc = snprintf(out_ptr, out_end - out_ptr,
  1180. "%s", current->comm);
  1181. if (rc > out_end - out_ptr)
  1182. goto out;
  1183. out_ptr += rc;
  1184. break;
  1185. default:
  1186. break;
  1187. }
  1188. ++pat_ptr;
  1189. }
  1190. }
  1191. /* Backward compatibility with core_uses_pid:
  1192. *
  1193. * If core_pattern does not include a %p (as is the default)
  1194. * and core_uses_pid is set, then .%pid will be appended to
  1195. * the filename */
  1196. if (!pid_in_pattern
  1197. && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
  1198. rc = snprintf(out_ptr, out_end - out_ptr,
  1199. ".%d", current->tgid);
  1200. if (rc > out_end - out_ptr)
  1201. goto out;
  1202. out_ptr += rc;
  1203. }
  1204. out:
  1205. *out_ptr = 0;
  1206. }
  1207. static void zap_threads (struct mm_struct *mm)
  1208. {
  1209. struct task_struct *g, *p;
  1210. struct task_struct *tsk = current;
  1211. struct completion *vfork_done = tsk->vfork_done;
  1212. int traced = 0;
  1213. /*
  1214. * Make sure nobody is waiting for us to release the VM,
  1215. * otherwise we can deadlock when we wait on each other
  1216. */
  1217. if (vfork_done) {
  1218. tsk->vfork_done = NULL;
  1219. complete(vfork_done);
  1220. }
  1221. read_lock(&tasklist_lock);
  1222. do_each_thread(g,p)
  1223. if (mm == p->mm && p != tsk) {
  1224. force_sig_specific(SIGKILL, p);
  1225. mm->core_waiters++;
  1226. if (unlikely(p->ptrace) &&
  1227. unlikely(p->parent->mm == mm))
  1228. traced = 1;
  1229. }
  1230. while_each_thread(g,p);
  1231. read_unlock(&tasklist_lock);
  1232. if (unlikely(traced)) {
  1233. /*
  1234. * We are zapping a thread and the thread it ptraces.
  1235. * If the tracee went into a ptrace stop for exit tracing,
  1236. * we could deadlock since the tracer is waiting for this
  1237. * coredump to finish. Detach them so they can both die.
  1238. */
  1239. write_lock_irq(&tasklist_lock);
  1240. do_each_thread(g,p) {
  1241. if (mm == p->mm && p != tsk &&
  1242. p->ptrace && p->parent->mm == mm) {
  1243. __ptrace_detach(p, 0);
  1244. }
  1245. } while_each_thread(g,p);
  1246. write_unlock_irq(&tasklist_lock);
  1247. }
  1248. }
  1249. static void coredump_wait(struct mm_struct *mm)
  1250. {
  1251. DECLARE_COMPLETION(startup_done);
  1252. int core_waiters;
  1253. mm->core_startup_done = &startup_done;
  1254. zap_threads(mm);
  1255. core_waiters = mm->core_waiters;
  1256. up_write(&mm->mmap_sem);
  1257. if (core_waiters)
  1258. wait_for_completion(&startup_done);
  1259. BUG_ON(mm->core_waiters);
  1260. }
  1261. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1262. {
  1263. char corename[CORENAME_MAX_SIZE + 1];
  1264. struct mm_struct *mm = current->mm;
  1265. struct linux_binfmt * binfmt;
  1266. struct inode * inode;
  1267. struct file * file;
  1268. int retval = 0;
  1269. int fsuid = current->fsuid;
  1270. int flag = 0;
  1271. binfmt = current->binfmt;
  1272. if (!binfmt || !binfmt->core_dump)
  1273. goto fail;
  1274. down_write(&mm->mmap_sem);
  1275. if (!mm->dumpable) {
  1276. up_write(&mm->mmap_sem);
  1277. goto fail;
  1278. }
  1279. /*
  1280. * We cannot trust fsuid as being the "true" uid of the
  1281. * process nor do we know its entire history. We only know it
  1282. * was tainted so we dump it as root in mode 2.
  1283. */
  1284. if (mm->dumpable == 2) { /* Setuid core dump mode */
  1285. flag = O_EXCL; /* Stop rewrite attacks */
  1286. current->fsuid = 0; /* Dump root private */
  1287. }
  1288. mm->dumpable = 0;
  1289. retval = -EAGAIN;
  1290. spin_lock_irq(&current->sighand->siglock);
  1291. if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
  1292. current->signal->flags = SIGNAL_GROUP_EXIT;
  1293. current->signal->group_exit_code = exit_code;
  1294. current->signal->group_stop_count = 0;
  1295. retval = 0;
  1296. }
  1297. spin_unlock_irq(&current->sighand->siglock);
  1298. if (retval) {
  1299. up_write(&mm->mmap_sem);
  1300. goto fail;
  1301. }
  1302. init_completion(&mm->core_done);
  1303. coredump_wait(mm);
  1304. /*
  1305. * Clear any false indication of pending signals that might
  1306. * be seen by the filesystem code called to write the core file.
  1307. */
  1308. clear_thread_flag(TIF_SIGPENDING);
  1309. if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
  1310. goto fail_unlock;
  1311. /*
  1312. * lock_kernel() because format_corename() is controlled by sysctl, which
  1313. * uses lock_kernel()
  1314. */
  1315. lock_kernel();
  1316. format_corename(corename, core_pattern, signr);
  1317. unlock_kernel();
  1318. file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
  1319. if (IS_ERR(file))
  1320. goto fail_unlock;
  1321. inode = file->f_dentry->d_inode;
  1322. if (inode->i_nlink > 1)
  1323. goto close_fail; /* multiple links - don't dump */
  1324. if (d_unhashed(file->f_dentry))
  1325. goto close_fail;
  1326. if (!S_ISREG(inode->i_mode))
  1327. goto close_fail;
  1328. if (!file->f_op)
  1329. goto close_fail;
  1330. if (!file->f_op->write)
  1331. goto close_fail;
  1332. if (do_truncate(file->f_dentry, 0, 0, file) != 0)
  1333. goto close_fail;
  1334. retval = binfmt->core_dump(signr, regs, file);
  1335. if (retval)
  1336. current->signal->group_exit_code |= 0x80;
  1337. close_fail:
  1338. filp_close(file, NULL);
  1339. fail_unlock:
  1340. current->fsuid = fsuid;
  1341. complete_all(&mm->core_done);
  1342. fail:
  1343. return retval;
  1344. }