exec.c 35 KB

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