exec.c 34 KB

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