exec.c 36 KB

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