exec.c 35 KB

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