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