exec.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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/fdtable.h>
  26. #include <linux/mm.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/swap.h>
  31. #include <linux/string.h>
  32. #include <linux/init.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/highmem.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/key.h>
  37. #include <linux/personality.h>
  38. #include <linux/binfmts.h>
  39. #include <linux/utsname.h>
  40. #include <linux/pid_namespace.h>
  41. #include <linux/module.h>
  42. #include <linux/namei.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/mount.h>
  45. #include <linux/security.h>
  46. #include <linux/ima.h>
  47. #include <linux/syscalls.h>
  48. #include <linux/tsacct_kern.h>
  49. #include <linux/cn_proc.h>
  50. #include <linux/audit.h>
  51. #include <linux/tracehook.h>
  52. #include <linux/kmod.h>
  53. #include <linux/fsnotify.h>
  54. #include <linux/fs_struct.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/mmu_context.h>
  57. #include <asm/tlb.h>
  58. #include "internal.h"
  59. int core_uses_pid;
  60. char core_pattern[CORENAME_MAX_SIZE] = "core";
  61. int suid_dumpable = 0;
  62. /* The maximal length of core_pattern is also specified in sysctl.c */
  63. static LIST_HEAD(formats);
  64. static DEFINE_RWLOCK(binfmt_lock);
  65. int __register_binfmt(struct linux_binfmt * fmt, int insert)
  66. {
  67. if (!fmt)
  68. return -EINVAL;
  69. write_lock(&binfmt_lock);
  70. insert ? list_add(&fmt->lh, &formats) :
  71. list_add_tail(&fmt->lh, &formats);
  72. write_unlock(&binfmt_lock);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(__register_binfmt);
  76. void unregister_binfmt(struct linux_binfmt * fmt)
  77. {
  78. write_lock(&binfmt_lock);
  79. list_del(&fmt->lh);
  80. write_unlock(&binfmt_lock);
  81. }
  82. EXPORT_SYMBOL(unregister_binfmt);
  83. static inline void put_binfmt(struct linux_binfmt * fmt)
  84. {
  85. module_put(fmt->module);
  86. }
  87. /*
  88. * Note that a shared library must be both readable and executable due to
  89. * security reasons.
  90. *
  91. * Also note that we take the address to load from from the file itself.
  92. */
  93. SYSCALL_DEFINE1(uselib, const char __user *, library)
  94. {
  95. struct file *file;
  96. char *tmp = getname(library);
  97. int error = PTR_ERR(tmp);
  98. if (IS_ERR(tmp))
  99. goto out;
  100. file = do_filp_open(AT_FDCWD, tmp,
  101. O_LARGEFILE | O_RDONLY | FMODE_EXEC, 0,
  102. MAY_READ | MAY_EXEC | MAY_OPEN);
  103. putname(tmp);
  104. error = PTR_ERR(file);
  105. if (IS_ERR(file))
  106. goto out;
  107. error = -EINVAL;
  108. if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
  109. goto exit;
  110. error = -EACCES;
  111. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  112. goto exit;
  113. fsnotify_open(file->f_path.dentry);
  114. error = -ENOEXEC;
  115. if(file->f_op) {
  116. struct linux_binfmt * fmt;
  117. read_lock(&binfmt_lock);
  118. list_for_each_entry(fmt, &formats, lh) {
  119. if (!fmt->load_shlib)
  120. continue;
  121. if (!try_module_get(fmt->module))
  122. continue;
  123. read_unlock(&binfmt_lock);
  124. error = fmt->load_shlib(file);
  125. read_lock(&binfmt_lock);
  126. put_binfmt(fmt);
  127. if (error != -ENOEXEC)
  128. break;
  129. }
  130. read_unlock(&binfmt_lock);
  131. }
  132. exit:
  133. fput(file);
  134. out:
  135. return error;
  136. }
  137. #ifdef CONFIG_MMU
  138. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  139. int write)
  140. {
  141. struct page *page;
  142. int ret;
  143. #ifdef CONFIG_STACK_GROWSUP
  144. if (write) {
  145. ret = expand_stack_downwards(bprm->vma, pos);
  146. if (ret < 0)
  147. return NULL;
  148. }
  149. #endif
  150. ret = get_user_pages(current, bprm->mm, pos,
  151. 1, write, 1, &page, NULL);
  152. if (ret <= 0)
  153. return NULL;
  154. if (write) {
  155. unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
  156. struct rlimit *rlim;
  157. /*
  158. * We've historically supported up to 32 pages (ARG_MAX)
  159. * of argument strings even with small stacks
  160. */
  161. if (size <= ARG_MAX)
  162. return page;
  163. /*
  164. * Limit to 1/4-th the stack size for the argv+env strings.
  165. * This ensures that:
  166. * - the remaining binfmt code will not run out of stack space,
  167. * - the program will have a reasonable amount of stack left
  168. * to work from.
  169. */
  170. rlim = current->signal->rlim;
  171. if (size > rlim[RLIMIT_STACK].rlim_cur / 4) {
  172. put_page(page);
  173. return NULL;
  174. }
  175. }
  176. return page;
  177. }
  178. static void put_arg_page(struct page *page)
  179. {
  180. put_page(page);
  181. }
  182. static void free_arg_page(struct linux_binprm *bprm, int i)
  183. {
  184. }
  185. static void free_arg_pages(struct linux_binprm *bprm)
  186. {
  187. }
  188. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  189. struct page *page)
  190. {
  191. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  192. }
  193. static int __bprm_mm_init(struct linux_binprm *bprm)
  194. {
  195. int err;
  196. struct vm_area_struct *vma = NULL;
  197. struct mm_struct *mm = bprm->mm;
  198. bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  199. if (!vma)
  200. return -ENOMEM;
  201. down_write(&mm->mmap_sem);
  202. vma->vm_mm = mm;
  203. /*
  204. * Place the stack at the largest stack address the architecture
  205. * supports. Later, we'll move this to an appropriate place. We don't
  206. * use STACK_TOP because that can depend on attributes which aren't
  207. * configured yet.
  208. */
  209. vma->vm_end = STACK_TOP_MAX;
  210. vma->vm_start = vma->vm_end - PAGE_SIZE;
  211. vma->vm_flags = VM_STACK_FLAGS;
  212. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  213. err = insert_vm_struct(mm, vma);
  214. if (err)
  215. goto err;
  216. mm->stack_vm = mm->total_vm = 1;
  217. up_write(&mm->mmap_sem);
  218. bprm->p = vma->vm_end - sizeof(void *);
  219. return 0;
  220. err:
  221. up_write(&mm->mmap_sem);
  222. bprm->vma = NULL;
  223. kmem_cache_free(vm_area_cachep, vma);
  224. return err;
  225. }
  226. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  227. {
  228. return len <= MAX_ARG_STRLEN;
  229. }
  230. #else
  231. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  232. int write)
  233. {
  234. struct page *page;
  235. page = bprm->page[pos / PAGE_SIZE];
  236. if (!page && write) {
  237. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  238. if (!page)
  239. return NULL;
  240. bprm->page[pos / PAGE_SIZE] = page;
  241. }
  242. return page;
  243. }
  244. static void put_arg_page(struct page *page)
  245. {
  246. }
  247. static void free_arg_page(struct linux_binprm *bprm, int i)
  248. {
  249. if (bprm->page[i]) {
  250. __free_page(bprm->page[i]);
  251. bprm->page[i] = NULL;
  252. }
  253. }
  254. static void free_arg_pages(struct linux_binprm *bprm)
  255. {
  256. int i;
  257. for (i = 0; i < MAX_ARG_PAGES; i++)
  258. free_arg_page(bprm, i);
  259. }
  260. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  261. struct page *page)
  262. {
  263. }
  264. static int __bprm_mm_init(struct linux_binprm *bprm)
  265. {
  266. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  267. return 0;
  268. }
  269. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  270. {
  271. return len <= bprm->p;
  272. }
  273. #endif /* CONFIG_MMU */
  274. /*
  275. * Create a new mm_struct and populate it with a temporary stack
  276. * vm_area_struct. We don't have enough context at this point to set the stack
  277. * flags, permissions, and offset, so we use temporary values. We'll update
  278. * them later in setup_arg_pages().
  279. */
  280. int bprm_mm_init(struct linux_binprm *bprm)
  281. {
  282. int err;
  283. struct mm_struct *mm = NULL;
  284. bprm->mm = mm = mm_alloc();
  285. err = -ENOMEM;
  286. if (!mm)
  287. goto err;
  288. err = init_new_context(current, mm);
  289. if (err)
  290. goto err;
  291. err = __bprm_mm_init(bprm);
  292. if (err)
  293. goto err;
  294. return 0;
  295. err:
  296. if (mm) {
  297. bprm->mm = NULL;
  298. mmdrop(mm);
  299. }
  300. return err;
  301. }
  302. /*
  303. * count() counts the number of strings in array ARGV.
  304. */
  305. static int count(char __user * __user * argv, int max)
  306. {
  307. int i = 0;
  308. if (argv != NULL) {
  309. for (;;) {
  310. char __user * p;
  311. if (get_user(p, argv))
  312. return -EFAULT;
  313. if (!p)
  314. break;
  315. argv++;
  316. if (i++ >= max)
  317. return -E2BIG;
  318. cond_resched();
  319. }
  320. }
  321. return i;
  322. }
  323. /*
  324. * 'copy_strings()' copies argument/environment strings from the old
  325. * processes's memory to the new process's stack. The call to get_user_pages()
  326. * ensures the destination page is created and not swapped out.
  327. */
  328. static int copy_strings(int argc, char __user * __user * argv,
  329. struct linux_binprm *bprm)
  330. {
  331. struct page *kmapped_page = NULL;
  332. char *kaddr = NULL;
  333. unsigned long kpos = 0;
  334. int ret;
  335. while (argc-- > 0) {
  336. char __user *str;
  337. int len;
  338. unsigned long pos;
  339. if (get_user(str, argv+argc) ||
  340. !(len = strnlen_user(str, MAX_ARG_STRLEN))) {
  341. ret = -EFAULT;
  342. goto out;
  343. }
  344. if (!valid_arg_len(bprm, len)) {
  345. ret = -E2BIG;
  346. goto out;
  347. }
  348. /* We're going to work our way backwords. */
  349. pos = bprm->p;
  350. str += len;
  351. bprm->p -= len;
  352. while (len > 0) {
  353. int offset, bytes_to_copy;
  354. offset = pos % PAGE_SIZE;
  355. if (offset == 0)
  356. offset = PAGE_SIZE;
  357. bytes_to_copy = offset;
  358. if (bytes_to_copy > len)
  359. bytes_to_copy = len;
  360. offset -= bytes_to_copy;
  361. pos -= bytes_to_copy;
  362. str -= bytes_to_copy;
  363. len -= bytes_to_copy;
  364. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  365. struct page *page;
  366. page = get_arg_page(bprm, pos, 1);
  367. if (!page) {
  368. ret = -E2BIG;
  369. goto out;
  370. }
  371. if (kmapped_page) {
  372. flush_kernel_dcache_page(kmapped_page);
  373. kunmap(kmapped_page);
  374. put_arg_page(kmapped_page);
  375. }
  376. kmapped_page = page;
  377. kaddr = kmap(kmapped_page);
  378. kpos = pos & PAGE_MASK;
  379. flush_arg_page(bprm, kpos, kmapped_page);
  380. }
  381. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  382. ret = -EFAULT;
  383. goto out;
  384. }
  385. }
  386. }
  387. ret = 0;
  388. out:
  389. if (kmapped_page) {
  390. flush_kernel_dcache_page(kmapped_page);
  391. kunmap(kmapped_page);
  392. put_arg_page(kmapped_page);
  393. }
  394. return ret;
  395. }
  396. /*
  397. * Like copy_strings, but get argv and its values from kernel memory.
  398. */
  399. int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
  400. {
  401. int r;
  402. mm_segment_t oldfs = get_fs();
  403. set_fs(KERNEL_DS);
  404. r = copy_strings(argc, (char __user * __user *)argv, bprm);
  405. set_fs(oldfs);
  406. return r;
  407. }
  408. EXPORT_SYMBOL(copy_strings_kernel);
  409. #ifdef CONFIG_MMU
  410. /*
  411. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  412. * the binfmt code determines where the new stack should reside, we shift it to
  413. * its final location. The process proceeds as follows:
  414. *
  415. * 1) Use shift to calculate the new vma endpoints.
  416. * 2) Extend vma to cover both the old and new ranges. This ensures the
  417. * arguments passed to subsequent functions are consistent.
  418. * 3) Move vma's page tables to the new range.
  419. * 4) Free up any cleared pgd range.
  420. * 5) Shrink the vma to cover only the new range.
  421. */
  422. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  423. {
  424. struct mm_struct *mm = vma->vm_mm;
  425. unsigned long old_start = vma->vm_start;
  426. unsigned long old_end = vma->vm_end;
  427. unsigned long length = old_end - old_start;
  428. unsigned long new_start = old_start - shift;
  429. unsigned long new_end = old_end - shift;
  430. struct mmu_gather *tlb;
  431. BUG_ON(new_start > new_end);
  432. /*
  433. * ensure there are no vmas between where we want to go
  434. * and where we are
  435. */
  436. if (vma != find_vma(mm, new_start))
  437. return -EFAULT;
  438. /*
  439. * cover the whole range: [new_start, old_end)
  440. */
  441. vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL);
  442. /*
  443. * move the page tables downwards, on failure we rely on
  444. * process cleanup to remove whatever mess we made.
  445. */
  446. if (length != move_page_tables(vma, old_start,
  447. vma, new_start, length))
  448. return -ENOMEM;
  449. lru_add_drain();
  450. tlb = tlb_gather_mmu(mm, 0);
  451. if (new_end > old_start) {
  452. /*
  453. * when the old and new regions overlap clear from new_end.
  454. */
  455. free_pgd_range(tlb, new_end, old_end, new_end,
  456. vma->vm_next ? vma->vm_next->vm_start : 0);
  457. } else {
  458. /*
  459. * otherwise, clean from old_start; this is done to not touch
  460. * the address space in [new_end, old_start) some architectures
  461. * have constraints on va-space that make this illegal (IA64) -
  462. * for the others its just a little faster.
  463. */
  464. free_pgd_range(tlb, old_start, old_end, new_end,
  465. vma->vm_next ? vma->vm_next->vm_start : 0);
  466. }
  467. tlb_finish_mmu(tlb, new_end, old_end);
  468. /*
  469. * shrink the vma to just the new range.
  470. */
  471. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  472. return 0;
  473. }
  474. #define EXTRA_STACK_VM_PAGES 20 /* random */
  475. /*
  476. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  477. * the stack is optionally relocated, and some extra space is added.
  478. */
  479. int setup_arg_pages(struct linux_binprm *bprm,
  480. unsigned long stack_top,
  481. int executable_stack)
  482. {
  483. unsigned long ret;
  484. unsigned long stack_shift;
  485. struct mm_struct *mm = current->mm;
  486. struct vm_area_struct *vma = bprm->vma;
  487. struct vm_area_struct *prev = NULL;
  488. unsigned long vm_flags;
  489. unsigned long stack_base;
  490. #ifdef CONFIG_STACK_GROWSUP
  491. /* Limit stack size to 1GB */
  492. stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
  493. if (stack_base > (1 << 30))
  494. stack_base = 1 << 30;
  495. /* Make sure we didn't let the argument array grow too large. */
  496. if (vma->vm_end - vma->vm_start > stack_base)
  497. return -ENOMEM;
  498. stack_base = PAGE_ALIGN(stack_top - stack_base);
  499. stack_shift = vma->vm_start - stack_base;
  500. mm->arg_start = bprm->p - stack_shift;
  501. bprm->p = vma->vm_end - stack_shift;
  502. #else
  503. stack_top = arch_align_stack(stack_top);
  504. stack_top = PAGE_ALIGN(stack_top);
  505. stack_shift = vma->vm_end - stack_top;
  506. bprm->p -= stack_shift;
  507. mm->arg_start = bprm->p;
  508. #endif
  509. if (bprm->loader)
  510. bprm->loader -= stack_shift;
  511. bprm->exec -= stack_shift;
  512. down_write(&mm->mmap_sem);
  513. vm_flags = VM_STACK_FLAGS;
  514. /*
  515. * Adjust stack execute permissions; explicitly enable for
  516. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  517. * (arch default) otherwise.
  518. */
  519. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  520. vm_flags |= VM_EXEC;
  521. else if (executable_stack == EXSTACK_DISABLE_X)
  522. vm_flags &= ~VM_EXEC;
  523. vm_flags |= mm->def_flags;
  524. ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
  525. vm_flags);
  526. if (ret)
  527. goto out_unlock;
  528. BUG_ON(prev != vma);
  529. /* Move stack pages down in memory. */
  530. if (stack_shift) {
  531. ret = shift_arg_pages(vma, stack_shift);
  532. if (ret) {
  533. up_write(&mm->mmap_sem);
  534. return ret;
  535. }
  536. }
  537. #ifdef CONFIG_STACK_GROWSUP
  538. stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  539. #else
  540. stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  541. #endif
  542. ret = expand_stack(vma, stack_base);
  543. if (ret)
  544. ret = -EFAULT;
  545. out_unlock:
  546. up_write(&mm->mmap_sem);
  547. return 0;
  548. }
  549. EXPORT_SYMBOL(setup_arg_pages);
  550. #endif /* CONFIG_MMU */
  551. struct file *open_exec(const char *name)
  552. {
  553. struct file *file;
  554. int err;
  555. file = do_filp_open(AT_FDCWD, name,
  556. O_LARGEFILE | O_RDONLY | FMODE_EXEC, 0,
  557. MAY_EXEC | MAY_OPEN);
  558. if (IS_ERR(file))
  559. goto out;
  560. err = -EACCES;
  561. if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
  562. goto exit;
  563. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  564. goto exit;
  565. fsnotify_open(file->f_path.dentry);
  566. err = deny_write_access(file);
  567. if (err)
  568. goto exit;
  569. out:
  570. return file;
  571. exit:
  572. fput(file);
  573. return ERR_PTR(err);
  574. }
  575. EXPORT_SYMBOL(open_exec);
  576. int kernel_read(struct file *file, unsigned long offset,
  577. char *addr, unsigned long count)
  578. {
  579. mm_segment_t old_fs;
  580. loff_t pos = offset;
  581. int result;
  582. old_fs = get_fs();
  583. set_fs(get_ds());
  584. /* The cast to a user pointer is valid due to the set_fs() */
  585. result = vfs_read(file, (void __user *)addr, count, &pos);
  586. set_fs(old_fs);
  587. return result;
  588. }
  589. EXPORT_SYMBOL(kernel_read);
  590. static int exec_mmap(struct mm_struct *mm)
  591. {
  592. struct task_struct *tsk;
  593. struct mm_struct * old_mm, *active_mm;
  594. /* Notify parent that we're no longer interested in the old VM */
  595. tsk = current;
  596. old_mm = current->mm;
  597. mm_release(tsk, old_mm);
  598. if (old_mm) {
  599. /*
  600. * Make sure that if there is a core dump in progress
  601. * for the old mm, we get out and die instead of going
  602. * through with the exec. We must hold mmap_sem around
  603. * checking core_state and changing tsk->mm.
  604. */
  605. down_read(&old_mm->mmap_sem);
  606. if (unlikely(old_mm->core_state)) {
  607. up_read(&old_mm->mmap_sem);
  608. return -EINTR;
  609. }
  610. }
  611. task_lock(tsk);
  612. active_mm = tsk->active_mm;
  613. tsk->mm = mm;
  614. tsk->active_mm = mm;
  615. activate_mm(active_mm, mm);
  616. task_unlock(tsk);
  617. arch_pick_mmap_layout(mm);
  618. if (old_mm) {
  619. up_read(&old_mm->mmap_sem);
  620. BUG_ON(active_mm != old_mm);
  621. mm_update_next_owner(old_mm);
  622. mmput(old_mm);
  623. return 0;
  624. }
  625. mmdrop(active_mm);
  626. return 0;
  627. }
  628. /*
  629. * This function makes sure the current process has its own signal table,
  630. * so that flush_signal_handlers can later reset the handlers without
  631. * disturbing other processes. (Other processes might share the signal
  632. * table via the CLONE_SIGHAND option to clone().)
  633. */
  634. static int de_thread(struct task_struct *tsk)
  635. {
  636. struct signal_struct *sig = tsk->signal;
  637. struct sighand_struct *oldsighand = tsk->sighand;
  638. spinlock_t *lock = &oldsighand->siglock;
  639. int count;
  640. if (thread_group_empty(tsk))
  641. goto no_thread_group;
  642. /*
  643. * Kill all other threads in the thread group.
  644. */
  645. spin_lock_irq(lock);
  646. if (signal_group_exit(sig)) {
  647. /*
  648. * Another group action in progress, just
  649. * return so that the signal is processed.
  650. */
  651. spin_unlock_irq(lock);
  652. return -EAGAIN;
  653. }
  654. sig->group_exit_task = tsk;
  655. zap_other_threads(tsk);
  656. /* Account for the thread group leader hanging around: */
  657. count = thread_group_leader(tsk) ? 1 : 2;
  658. sig->notify_count = count;
  659. while (atomic_read(&sig->count) > count) {
  660. __set_current_state(TASK_UNINTERRUPTIBLE);
  661. spin_unlock_irq(lock);
  662. schedule();
  663. spin_lock_irq(lock);
  664. }
  665. spin_unlock_irq(lock);
  666. /*
  667. * At this point all other threads have exited, all we have to
  668. * do is to wait for the thread group leader to become inactive,
  669. * and to assume its PID:
  670. */
  671. if (!thread_group_leader(tsk)) {
  672. struct task_struct *leader = tsk->group_leader;
  673. sig->notify_count = -1; /* for exit_notify() */
  674. for (;;) {
  675. write_lock_irq(&tasklist_lock);
  676. if (likely(leader->exit_state))
  677. break;
  678. __set_current_state(TASK_UNINTERRUPTIBLE);
  679. write_unlock_irq(&tasklist_lock);
  680. schedule();
  681. }
  682. /*
  683. * The only record we have of the real-time age of a
  684. * process, regardless of execs it's done, is start_time.
  685. * All the past CPU time is accumulated in signal_struct
  686. * from sister threads now dead. But in this non-leader
  687. * exec, nothing survives from the original leader thread,
  688. * whose birth marks the true age of this process now.
  689. * When we take on its identity by switching to its PID, we
  690. * also take its birthdate (always earlier than our own).
  691. */
  692. tsk->start_time = leader->start_time;
  693. BUG_ON(!same_thread_group(leader, tsk));
  694. BUG_ON(has_group_leader_pid(tsk));
  695. /*
  696. * An exec() starts a new thread group with the
  697. * TGID of the previous thread group. Rehash the
  698. * two threads with a switched PID, and release
  699. * the former thread group leader:
  700. */
  701. /* Become a process group leader with the old leader's pid.
  702. * The old leader becomes a thread of the this thread group.
  703. * Note: The old leader also uses this pid until release_task
  704. * is called. Odd but simple and correct.
  705. */
  706. detach_pid(tsk, PIDTYPE_PID);
  707. tsk->pid = leader->pid;
  708. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  709. transfer_pid(leader, tsk, PIDTYPE_PGID);
  710. transfer_pid(leader, tsk, PIDTYPE_SID);
  711. list_replace_rcu(&leader->tasks, &tsk->tasks);
  712. tsk->group_leader = tsk;
  713. leader->group_leader = tsk;
  714. tsk->exit_signal = SIGCHLD;
  715. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  716. leader->exit_state = EXIT_DEAD;
  717. write_unlock_irq(&tasklist_lock);
  718. release_task(leader);
  719. }
  720. sig->group_exit_task = NULL;
  721. sig->notify_count = 0;
  722. no_thread_group:
  723. exit_itimers(sig);
  724. flush_itimer_signals();
  725. if (atomic_read(&oldsighand->count) != 1) {
  726. struct sighand_struct *newsighand;
  727. /*
  728. * This ->sighand is shared with the CLONE_SIGHAND
  729. * but not CLONE_THREAD task, switch to the new one.
  730. */
  731. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  732. if (!newsighand)
  733. return -ENOMEM;
  734. atomic_set(&newsighand->count, 1);
  735. memcpy(newsighand->action, oldsighand->action,
  736. sizeof(newsighand->action));
  737. write_lock_irq(&tasklist_lock);
  738. spin_lock(&oldsighand->siglock);
  739. rcu_assign_pointer(tsk->sighand, newsighand);
  740. spin_unlock(&oldsighand->siglock);
  741. write_unlock_irq(&tasklist_lock);
  742. __cleanup_sighand(oldsighand);
  743. }
  744. BUG_ON(!thread_group_leader(tsk));
  745. return 0;
  746. }
  747. /*
  748. * These functions flushes out all traces of the currently running executable
  749. * so that a new one can be started
  750. */
  751. static void flush_old_files(struct files_struct * files)
  752. {
  753. long j = -1;
  754. struct fdtable *fdt;
  755. spin_lock(&files->file_lock);
  756. for (;;) {
  757. unsigned long set, i;
  758. j++;
  759. i = j * __NFDBITS;
  760. fdt = files_fdtable(files);
  761. if (i >= fdt->max_fds)
  762. break;
  763. set = fdt->close_on_exec->fds_bits[j];
  764. if (!set)
  765. continue;
  766. fdt->close_on_exec->fds_bits[j] = 0;
  767. spin_unlock(&files->file_lock);
  768. for ( ; set ; i++,set >>= 1) {
  769. if (set & 1) {
  770. sys_close(i);
  771. }
  772. }
  773. spin_lock(&files->file_lock);
  774. }
  775. spin_unlock(&files->file_lock);
  776. }
  777. char *get_task_comm(char *buf, struct task_struct *tsk)
  778. {
  779. /* buf must be at least sizeof(tsk->comm) in size */
  780. task_lock(tsk);
  781. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  782. task_unlock(tsk);
  783. return buf;
  784. }
  785. void set_task_comm(struct task_struct *tsk, char *buf)
  786. {
  787. task_lock(tsk);
  788. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  789. task_unlock(tsk);
  790. }
  791. int flush_old_exec(struct linux_binprm * bprm)
  792. {
  793. char * name;
  794. int i, ch, retval;
  795. char tcomm[sizeof(current->comm)];
  796. /*
  797. * Make sure we have a private signal table and that
  798. * we are unassociated from the previous thread group.
  799. */
  800. retval = de_thread(current);
  801. if (retval)
  802. goto out;
  803. set_mm_exe_file(bprm->mm, bprm->file);
  804. /*
  805. * Release all of the old mmap stuff
  806. */
  807. retval = exec_mmap(bprm->mm);
  808. if (retval)
  809. goto out;
  810. bprm->mm = NULL; /* We're using it now */
  811. /* This is the point of no return */
  812. current->sas_ss_sp = current->sas_ss_size = 0;
  813. if (current_euid() == current_uid() && current_egid() == current_gid())
  814. set_dumpable(current->mm, 1);
  815. else
  816. set_dumpable(current->mm, suid_dumpable);
  817. name = bprm->filename;
  818. /* Copies the binary name from after last slash */
  819. for (i=0; (ch = *(name++)) != '\0';) {
  820. if (ch == '/')
  821. i = 0; /* overwrite what we wrote */
  822. else
  823. if (i < (sizeof(tcomm) - 1))
  824. tcomm[i++] = ch;
  825. }
  826. tcomm[i] = '\0';
  827. set_task_comm(current, tcomm);
  828. current->flags &= ~PF_RANDOMIZE;
  829. flush_thread();
  830. /* Set the new mm task size. We have to do that late because it may
  831. * depend on TIF_32BIT which is only updated in flush_thread() on
  832. * some architectures like powerpc
  833. */
  834. current->mm->task_size = TASK_SIZE;
  835. /* install the new credentials */
  836. if (bprm->cred->uid != current_euid() ||
  837. bprm->cred->gid != current_egid()) {
  838. current->pdeath_signal = 0;
  839. } else if (file_permission(bprm->file, MAY_READ) ||
  840. bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP) {
  841. set_dumpable(current->mm, suid_dumpable);
  842. }
  843. current->personality &= ~bprm->per_clear;
  844. /* An exec changes our domain. We are no longer part of the thread
  845. group */
  846. current->self_exec_id++;
  847. flush_signal_handlers(current, 0);
  848. flush_old_files(current->files);
  849. return 0;
  850. out:
  851. return retval;
  852. }
  853. EXPORT_SYMBOL(flush_old_exec);
  854. /*
  855. * install the new credentials for this executable
  856. */
  857. void install_exec_creds(struct linux_binprm *bprm)
  858. {
  859. security_bprm_committing_creds(bprm);
  860. commit_creds(bprm->cred);
  861. bprm->cred = NULL;
  862. /* cred_exec_mutex must be held at least to this point to prevent
  863. * ptrace_attach() from altering our determination of the task's
  864. * credentials; any time after this it may be unlocked */
  865. security_bprm_committed_creds(bprm);
  866. }
  867. EXPORT_SYMBOL(install_exec_creds);
  868. /*
  869. * determine how safe it is to execute the proposed program
  870. * - the caller must hold current->cred_exec_mutex to protect against
  871. * PTRACE_ATTACH
  872. */
  873. int check_unsafe_exec(struct linux_binprm *bprm)
  874. {
  875. struct task_struct *p = current, *t;
  876. unsigned n_fs;
  877. int res = 0;
  878. bprm->unsafe = tracehook_unsafe_exec(p);
  879. n_fs = 1;
  880. write_lock(&p->fs->lock);
  881. rcu_read_lock();
  882. for (t = next_thread(p); t != p; t = next_thread(t)) {
  883. if (t->fs == p->fs)
  884. n_fs++;
  885. }
  886. rcu_read_unlock();
  887. if (p->fs->users > n_fs) {
  888. bprm->unsafe |= LSM_UNSAFE_SHARE;
  889. } else {
  890. res = -EAGAIN;
  891. if (!p->fs->in_exec) {
  892. p->fs->in_exec = 1;
  893. res = 1;
  894. }
  895. }
  896. write_unlock(&p->fs->lock);
  897. return res;
  898. }
  899. /*
  900. * Fill the binprm structure from the inode.
  901. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  902. *
  903. * This may be called multiple times for binary chains (scripts for example).
  904. */
  905. int prepare_binprm(struct linux_binprm *bprm)
  906. {
  907. umode_t mode;
  908. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  909. int retval;
  910. mode = inode->i_mode;
  911. if (bprm->file->f_op == NULL)
  912. return -EACCES;
  913. /* clear any previous set[ug]id data from a previous binary */
  914. bprm->cred->euid = current_euid();
  915. bprm->cred->egid = current_egid();
  916. if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  917. /* Set-uid? */
  918. if (mode & S_ISUID) {
  919. bprm->per_clear |= PER_CLEAR_ON_SETID;
  920. bprm->cred->euid = inode->i_uid;
  921. }
  922. /* Set-gid? */
  923. /*
  924. * If setgid is set but no group execute bit then this
  925. * is a candidate for mandatory locking, not a setgid
  926. * executable.
  927. */
  928. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  929. bprm->per_clear |= PER_CLEAR_ON_SETID;
  930. bprm->cred->egid = inode->i_gid;
  931. }
  932. }
  933. /* fill in binprm security blob */
  934. retval = security_bprm_set_creds(bprm);
  935. if (retval)
  936. return retval;
  937. bprm->cred_prepared = 1;
  938. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  939. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  940. }
  941. EXPORT_SYMBOL(prepare_binprm);
  942. /*
  943. * Arguments are '\0' separated strings found at the location bprm->p
  944. * points to; chop off the first by relocating brpm->p to right after
  945. * the first '\0' encountered.
  946. */
  947. int remove_arg_zero(struct linux_binprm *bprm)
  948. {
  949. int ret = 0;
  950. unsigned long offset;
  951. char *kaddr;
  952. struct page *page;
  953. if (!bprm->argc)
  954. return 0;
  955. do {
  956. offset = bprm->p & ~PAGE_MASK;
  957. page = get_arg_page(bprm, bprm->p, 0);
  958. if (!page) {
  959. ret = -EFAULT;
  960. goto out;
  961. }
  962. kaddr = kmap_atomic(page, KM_USER0);
  963. for (; offset < PAGE_SIZE && kaddr[offset];
  964. offset++, bprm->p++)
  965. ;
  966. kunmap_atomic(kaddr, KM_USER0);
  967. put_arg_page(page);
  968. if (offset == PAGE_SIZE)
  969. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  970. } while (offset == PAGE_SIZE);
  971. bprm->p++;
  972. bprm->argc--;
  973. ret = 0;
  974. out:
  975. return ret;
  976. }
  977. EXPORT_SYMBOL(remove_arg_zero);
  978. /*
  979. * cycle the list of binary formats handler, until one recognizes the image
  980. */
  981. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  982. {
  983. unsigned int depth = bprm->recursion_depth;
  984. int try,retval;
  985. struct linux_binfmt *fmt;
  986. retval = security_bprm_check(bprm);
  987. if (retval)
  988. return retval;
  989. retval = ima_bprm_check(bprm);
  990. if (retval)
  991. return retval;
  992. /* kernel module loader fixup */
  993. /* so we don't try to load run modprobe in kernel space. */
  994. set_fs(USER_DS);
  995. retval = audit_bprm(bprm);
  996. if (retval)
  997. return retval;
  998. retval = -ENOENT;
  999. for (try=0; try<2; try++) {
  1000. read_lock(&binfmt_lock);
  1001. list_for_each_entry(fmt, &formats, lh) {
  1002. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1003. if (!fn)
  1004. continue;
  1005. if (!try_module_get(fmt->module))
  1006. continue;
  1007. read_unlock(&binfmt_lock);
  1008. retval = fn(bprm, regs);
  1009. /*
  1010. * Restore the depth counter to its starting value
  1011. * in this call, so we don't have to rely on every
  1012. * load_binary function to restore it on return.
  1013. */
  1014. bprm->recursion_depth = depth;
  1015. if (retval >= 0) {
  1016. if (depth == 0)
  1017. tracehook_report_exec(fmt, bprm, regs);
  1018. put_binfmt(fmt);
  1019. allow_write_access(bprm->file);
  1020. if (bprm->file)
  1021. fput(bprm->file);
  1022. bprm->file = NULL;
  1023. current->did_exec = 1;
  1024. proc_exec_connector(current);
  1025. return retval;
  1026. }
  1027. read_lock(&binfmt_lock);
  1028. put_binfmt(fmt);
  1029. if (retval != -ENOEXEC || bprm->mm == NULL)
  1030. break;
  1031. if (!bprm->file) {
  1032. read_unlock(&binfmt_lock);
  1033. return retval;
  1034. }
  1035. }
  1036. read_unlock(&binfmt_lock);
  1037. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1038. break;
  1039. #ifdef CONFIG_MODULES
  1040. } else {
  1041. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1042. if (printable(bprm->buf[0]) &&
  1043. printable(bprm->buf[1]) &&
  1044. printable(bprm->buf[2]) &&
  1045. printable(bprm->buf[3]))
  1046. break; /* -ENOEXEC */
  1047. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1048. #endif
  1049. }
  1050. }
  1051. return retval;
  1052. }
  1053. EXPORT_SYMBOL(search_binary_handler);
  1054. void free_bprm(struct linux_binprm *bprm)
  1055. {
  1056. free_arg_pages(bprm);
  1057. if (bprm->cred)
  1058. abort_creds(bprm->cred);
  1059. kfree(bprm);
  1060. }
  1061. /*
  1062. * sys_execve() executes a new program.
  1063. */
  1064. int do_execve(char * filename,
  1065. char __user *__user *argv,
  1066. char __user *__user *envp,
  1067. struct pt_regs * regs)
  1068. {
  1069. struct linux_binprm *bprm;
  1070. struct file *file;
  1071. struct files_struct *displaced;
  1072. bool clear_in_exec;
  1073. int retval;
  1074. retval = unshare_files(&displaced);
  1075. if (retval)
  1076. goto out_ret;
  1077. retval = -ENOMEM;
  1078. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1079. if (!bprm)
  1080. goto out_files;
  1081. retval = mutex_lock_interruptible(&current->cred_exec_mutex);
  1082. if (retval < 0)
  1083. goto out_free;
  1084. current->in_execve = 1;
  1085. retval = -ENOMEM;
  1086. bprm->cred = prepare_exec_creds();
  1087. if (!bprm->cred)
  1088. goto out_unlock;
  1089. retval = check_unsafe_exec(bprm);
  1090. if (retval < 0)
  1091. goto out_unlock;
  1092. clear_in_exec = retval;
  1093. file = open_exec(filename);
  1094. retval = PTR_ERR(file);
  1095. if (IS_ERR(file))
  1096. goto out_unmark;
  1097. sched_exec();
  1098. bprm->file = file;
  1099. bprm->filename = filename;
  1100. bprm->interp = filename;
  1101. retval = bprm_mm_init(bprm);
  1102. if (retval)
  1103. goto out_file;
  1104. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1105. if ((retval = bprm->argc) < 0)
  1106. goto out;
  1107. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1108. if ((retval = bprm->envc) < 0)
  1109. goto out;
  1110. retval = prepare_binprm(bprm);
  1111. if (retval < 0)
  1112. goto out;
  1113. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1114. if (retval < 0)
  1115. goto out;
  1116. bprm->exec = bprm->p;
  1117. retval = copy_strings(bprm->envc, envp, bprm);
  1118. if (retval < 0)
  1119. goto out;
  1120. retval = copy_strings(bprm->argc, argv, bprm);
  1121. if (retval < 0)
  1122. goto out;
  1123. current->flags &= ~PF_KTHREAD;
  1124. retval = search_binary_handler(bprm,regs);
  1125. if (retval < 0)
  1126. goto out;
  1127. /* execve succeeded */
  1128. current->fs->in_exec = 0;
  1129. current->in_execve = 0;
  1130. mutex_unlock(&current->cred_exec_mutex);
  1131. acct_update_integrals(current);
  1132. free_bprm(bprm);
  1133. if (displaced)
  1134. put_files_struct(displaced);
  1135. return retval;
  1136. out:
  1137. if (bprm->mm)
  1138. mmput (bprm->mm);
  1139. out_file:
  1140. if (bprm->file) {
  1141. allow_write_access(bprm->file);
  1142. fput(bprm->file);
  1143. }
  1144. out_unmark:
  1145. if (clear_in_exec)
  1146. current->fs->in_exec = 0;
  1147. out_unlock:
  1148. current->in_execve = 0;
  1149. mutex_unlock(&current->cred_exec_mutex);
  1150. out_free:
  1151. free_bprm(bprm);
  1152. out_files:
  1153. if (displaced)
  1154. reset_files_struct(displaced);
  1155. out_ret:
  1156. return retval;
  1157. }
  1158. int set_binfmt(struct linux_binfmt *new)
  1159. {
  1160. struct linux_binfmt *old = current->binfmt;
  1161. if (new) {
  1162. if (!try_module_get(new->module))
  1163. return -1;
  1164. }
  1165. current->binfmt = new;
  1166. if (old)
  1167. module_put(old->module);
  1168. return 0;
  1169. }
  1170. EXPORT_SYMBOL(set_binfmt);
  1171. /* format_corename will inspect the pattern parameter, and output a
  1172. * name into corename, which must have space for at least
  1173. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1174. */
  1175. static int format_corename(char *corename, long signr)
  1176. {
  1177. const struct cred *cred = current_cred();
  1178. const char *pat_ptr = core_pattern;
  1179. int ispipe = (*pat_ptr == '|');
  1180. char *out_ptr = corename;
  1181. char *const out_end = corename + CORENAME_MAX_SIZE;
  1182. int rc;
  1183. int pid_in_pattern = 0;
  1184. /* Repeat as long as we have more pattern to process and more output
  1185. space */
  1186. while (*pat_ptr) {
  1187. if (*pat_ptr != '%') {
  1188. if (out_ptr == out_end)
  1189. goto out;
  1190. *out_ptr++ = *pat_ptr++;
  1191. } else {
  1192. switch (*++pat_ptr) {
  1193. case 0:
  1194. goto out;
  1195. /* Double percent, output one percent */
  1196. case '%':
  1197. if (out_ptr == out_end)
  1198. goto out;
  1199. *out_ptr++ = '%';
  1200. break;
  1201. /* pid */
  1202. case 'p':
  1203. pid_in_pattern = 1;
  1204. rc = snprintf(out_ptr, out_end - out_ptr,
  1205. "%d", task_tgid_vnr(current));
  1206. if (rc > out_end - out_ptr)
  1207. goto out;
  1208. out_ptr += rc;
  1209. break;
  1210. /* uid */
  1211. case 'u':
  1212. rc = snprintf(out_ptr, out_end - out_ptr,
  1213. "%d", cred->uid);
  1214. if (rc > out_end - out_ptr)
  1215. goto out;
  1216. out_ptr += rc;
  1217. break;
  1218. /* gid */
  1219. case 'g':
  1220. rc = snprintf(out_ptr, out_end - out_ptr,
  1221. "%d", cred->gid);
  1222. if (rc > out_end - out_ptr)
  1223. goto out;
  1224. out_ptr += rc;
  1225. break;
  1226. /* signal that caused the coredump */
  1227. case 's':
  1228. rc = snprintf(out_ptr, out_end - out_ptr,
  1229. "%ld", signr);
  1230. if (rc > out_end - out_ptr)
  1231. goto out;
  1232. out_ptr += rc;
  1233. break;
  1234. /* UNIX time of coredump */
  1235. case 't': {
  1236. struct timeval tv;
  1237. do_gettimeofday(&tv);
  1238. rc = snprintf(out_ptr, out_end - out_ptr,
  1239. "%lu", tv.tv_sec);
  1240. if (rc > out_end - out_ptr)
  1241. goto out;
  1242. out_ptr += rc;
  1243. break;
  1244. }
  1245. /* hostname */
  1246. case 'h':
  1247. down_read(&uts_sem);
  1248. rc = snprintf(out_ptr, out_end - out_ptr,
  1249. "%s", utsname()->nodename);
  1250. up_read(&uts_sem);
  1251. if (rc > out_end - out_ptr)
  1252. goto out;
  1253. out_ptr += rc;
  1254. break;
  1255. /* executable */
  1256. case 'e':
  1257. rc = snprintf(out_ptr, out_end - out_ptr,
  1258. "%s", current->comm);
  1259. if (rc > out_end - out_ptr)
  1260. goto out;
  1261. out_ptr += rc;
  1262. break;
  1263. /* core limit size */
  1264. case 'c':
  1265. rc = snprintf(out_ptr, out_end - out_ptr,
  1266. "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
  1267. if (rc > out_end - out_ptr)
  1268. goto out;
  1269. out_ptr += rc;
  1270. break;
  1271. default:
  1272. break;
  1273. }
  1274. ++pat_ptr;
  1275. }
  1276. }
  1277. /* Backward compatibility with core_uses_pid:
  1278. *
  1279. * If core_pattern does not include a %p (as is the default)
  1280. * and core_uses_pid is set, then .%pid will be appended to
  1281. * the filename. Do not do this for piped commands. */
  1282. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  1283. rc = snprintf(out_ptr, out_end - out_ptr,
  1284. ".%d", task_tgid_vnr(current));
  1285. if (rc > out_end - out_ptr)
  1286. goto out;
  1287. out_ptr += rc;
  1288. }
  1289. out:
  1290. *out_ptr = 0;
  1291. return ispipe;
  1292. }
  1293. static int zap_process(struct task_struct *start)
  1294. {
  1295. struct task_struct *t;
  1296. int nr = 0;
  1297. start->signal->flags = SIGNAL_GROUP_EXIT;
  1298. start->signal->group_stop_count = 0;
  1299. t = start;
  1300. do {
  1301. if (t != current && t->mm) {
  1302. sigaddset(&t->pending.signal, SIGKILL);
  1303. signal_wake_up(t, 1);
  1304. nr++;
  1305. }
  1306. } while_each_thread(start, t);
  1307. return nr;
  1308. }
  1309. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1310. struct core_state *core_state, int exit_code)
  1311. {
  1312. struct task_struct *g, *p;
  1313. unsigned long flags;
  1314. int nr = -EAGAIN;
  1315. spin_lock_irq(&tsk->sighand->siglock);
  1316. if (!signal_group_exit(tsk->signal)) {
  1317. mm->core_state = core_state;
  1318. tsk->signal->group_exit_code = exit_code;
  1319. nr = zap_process(tsk);
  1320. }
  1321. spin_unlock_irq(&tsk->sighand->siglock);
  1322. if (unlikely(nr < 0))
  1323. return nr;
  1324. if (atomic_read(&mm->mm_users) == nr + 1)
  1325. goto done;
  1326. /*
  1327. * We should find and kill all tasks which use this mm, and we should
  1328. * count them correctly into ->nr_threads. We don't take tasklist
  1329. * lock, but this is safe wrt:
  1330. *
  1331. * fork:
  1332. * None of sub-threads can fork after zap_process(leader). All
  1333. * processes which were created before this point should be
  1334. * visible to zap_threads() because copy_process() adds the new
  1335. * process to the tail of init_task.tasks list, and lock/unlock
  1336. * of ->siglock provides a memory barrier.
  1337. *
  1338. * do_exit:
  1339. * The caller holds mm->mmap_sem. This means that the task which
  1340. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1341. * its ->mm.
  1342. *
  1343. * de_thread:
  1344. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1345. * we must see either old or new leader, this does not matter.
  1346. * However, it can change p->sighand, so lock_task_sighand(p)
  1347. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1348. * it can't fail.
  1349. *
  1350. * Note also that "g" can be the old leader with ->mm == NULL
  1351. * and already unhashed and thus removed from ->thread_group.
  1352. * This is OK, __unhash_process()->list_del_rcu() does not
  1353. * clear the ->next pointer, we will find the new leader via
  1354. * next_thread().
  1355. */
  1356. rcu_read_lock();
  1357. for_each_process(g) {
  1358. if (g == tsk->group_leader)
  1359. continue;
  1360. if (g->flags & PF_KTHREAD)
  1361. continue;
  1362. p = g;
  1363. do {
  1364. if (p->mm) {
  1365. if (unlikely(p->mm == mm)) {
  1366. lock_task_sighand(p, &flags);
  1367. nr += zap_process(p);
  1368. unlock_task_sighand(p, &flags);
  1369. }
  1370. break;
  1371. }
  1372. } while_each_thread(g, p);
  1373. }
  1374. rcu_read_unlock();
  1375. done:
  1376. atomic_set(&core_state->nr_threads, nr);
  1377. return nr;
  1378. }
  1379. static int coredump_wait(int exit_code, struct core_state *core_state)
  1380. {
  1381. struct task_struct *tsk = current;
  1382. struct mm_struct *mm = tsk->mm;
  1383. struct completion *vfork_done;
  1384. int core_waiters;
  1385. init_completion(&core_state->startup);
  1386. core_state->dumper.task = tsk;
  1387. core_state->dumper.next = NULL;
  1388. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1389. up_write(&mm->mmap_sem);
  1390. if (unlikely(core_waiters < 0))
  1391. goto fail;
  1392. /*
  1393. * Make sure nobody is waiting for us to release the VM,
  1394. * otherwise we can deadlock when we wait on each other
  1395. */
  1396. vfork_done = tsk->vfork_done;
  1397. if (vfork_done) {
  1398. tsk->vfork_done = NULL;
  1399. complete(vfork_done);
  1400. }
  1401. if (core_waiters)
  1402. wait_for_completion(&core_state->startup);
  1403. fail:
  1404. return core_waiters;
  1405. }
  1406. static void coredump_finish(struct mm_struct *mm)
  1407. {
  1408. struct core_thread *curr, *next;
  1409. struct task_struct *task;
  1410. next = mm->core_state->dumper.next;
  1411. while ((curr = next) != NULL) {
  1412. next = curr->next;
  1413. task = curr->task;
  1414. /*
  1415. * see exit_mm(), curr->task must not see
  1416. * ->task == NULL before we read ->next.
  1417. */
  1418. smp_mb();
  1419. curr->task = NULL;
  1420. wake_up_process(task);
  1421. }
  1422. mm->core_state = NULL;
  1423. }
  1424. /*
  1425. * set_dumpable converts traditional three-value dumpable to two flags and
  1426. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1427. * these bits are not changed atomically. So get_dumpable can observe the
  1428. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1429. * return either old dumpable or new one by paying attention to the order of
  1430. * modifying the bits.
  1431. *
  1432. * dumpable | mm->flags (binary)
  1433. * old new | initial interim final
  1434. * ---------+-----------------------
  1435. * 0 1 | 00 01 01
  1436. * 0 2 | 00 10(*) 11
  1437. * 1 0 | 01 00 00
  1438. * 1 2 | 01 11 11
  1439. * 2 0 | 11 10(*) 00
  1440. * 2 1 | 11 11 01
  1441. *
  1442. * (*) get_dumpable regards interim value of 10 as 11.
  1443. */
  1444. void set_dumpable(struct mm_struct *mm, int value)
  1445. {
  1446. switch (value) {
  1447. case 0:
  1448. clear_bit(MMF_DUMPABLE, &mm->flags);
  1449. smp_wmb();
  1450. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1451. break;
  1452. case 1:
  1453. set_bit(MMF_DUMPABLE, &mm->flags);
  1454. smp_wmb();
  1455. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1456. break;
  1457. case 2:
  1458. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1459. smp_wmb();
  1460. set_bit(MMF_DUMPABLE, &mm->flags);
  1461. break;
  1462. }
  1463. }
  1464. int get_dumpable(struct mm_struct *mm)
  1465. {
  1466. int ret;
  1467. ret = mm->flags & 0x3;
  1468. return (ret >= 2) ? 2 : ret;
  1469. }
  1470. void do_coredump(long signr, int exit_code, struct pt_regs *regs)
  1471. {
  1472. struct core_state core_state;
  1473. char corename[CORENAME_MAX_SIZE + 1];
  1474. struct mm_struct *mm = current->mm;
  1475. struct linux_binfmt * binfmt;
  1476. struct inode * inode;
  1477. struct file * file;
  1478. const struct cred *old_cred;
  1479. struct cred *cred;
  1480. int retval = 0;
  1481. int flag = 0;
  1482. int ispipe = 0;
  1483. unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1484. char **helper_argv = NULL;
  1485. int helper_argc = 0;
  1486. char *delimit;
  1487. audit_core_dumps(signr);
  1488. binfmt = current->binfmt;
  1489. if (!binfmt || !binfmt->core_dump)
  1490. goto fail;
  1491. cred = prepare_creds();
  1492. if (!cred) {
  1493. retval = -ENOMEM;
  1494. goto fail;
  1495. }
  1496. down_write(&mm->mmap_sem);
  1497. /*
  1498. * If another thread got here first, or we are not dumpable, bail out.
  1499. */
  1500. if (mm->core_state || !get_dumpable(mm)) {
  1501. up_write(&mm->mmap_sem);
  1502. put_cred(cred);
  1503. goto fail;
  1504. }
  1505. /*
  1506. * We cannot trust fsuid as being the "true" uid of the
  1507. * process nor do we know its entire history. We only know it
  1508. * was tainted so we dump it as root in mode 2.
  1509. */
  1510. if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
  1511. flag = O_EXCL; /* Stop rewrite attacks */
  1512. cred->fsuid = 0; /* Dump root private */
  1513. }
  1514. retval = coredump_wait(exit_code, &core_state);
  1515. if (retval < 0) {
  1516. put_cred(cred);
  1517. goto fail;
  1518. }
  1519. old_cred = override_creds(cred);
  1520. /*
  1521. * Clear any false indication of pending signals that might
  1522. * be seen by the filesystem code called to write the core file.
  1523. */
  1524. clear_thread_flag(TIF_SIGPENDING);
  1525. /*
  1526. * lock_kernel() because format_corename() is controlled by sysctl, which
  1527. * uses lock_kernel()
  1528. */
  1529. lock_kernel();
  1530. ispipe = format_corename(corename, signr);
  1531. unlock_kernel();
  1532. /*
  1533. * Don't bother to check the RLIMIT_CORE value if core_pattern points
  1534. * to a pipe. Since we're not writing directly to the filesystem
  1535. * RLIMIT_CORE doesn't really apply, as no actual core file will be
  1536. * created unless the pipe reader choses to write out the core file
  1537. * at which point file size limits and permissions will be imposed
  1538. * as it does with any other process
  1539. */
  1540. if ((!ispipe) && (core_limit < binfmt->min_coredump))
  1541. goto fail_unlock;
  1542. if (ispipe) {
  1543. helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
  1544. if (!helper_argv) {
  1545. printk(KERN_WARNING "%s failed to allocate memory\n",
  1546. __func__);
  1547. goto fail_unlock;
  1548. }
  1549. /* Terminate the string before the first option */
  1550. delimit = strchr(corename, ' ');
  1551. if (delimit)
  1552. *delimit = '\0';
  1553. delimit = strrchr(helper_argv[0], '/');
  1554. if (delimit)
  1555. delimit++;
  1556. else
  1557. delimit = helper_argv[0];
  1558. if (!strcmp(delimit, current->comm)) {
  1559. printk(KERN_NOTICE "Recursive core dump detected, "
  1560. "aborting\n");
  1561. goto fail_unlock;
  1562. }
  1563. core_limit = RLIM_INFINITY;
  1564. /* SIGPIPE can happen, but it's just never processed */
  1565. if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
  1566. &file)) {
  1567. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1568. corename);
  1569. goto fail_unlock;
  1570. }
  1571. } else
  1572. file = filp_open(corename,
  1573. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1574. 0600);
  1575. if (IS_ERR(file))
  1576. goto fail_unlock;
  1577. inode = file->f_path.dentry->d_inode;
  1578. if (inode->i_nlink > 1)
  1579. goto close_fail; /* multiple links - don't dump */
  1580. if (!ispipe && d_unhashed(file->f_path.dentry))
  1581. goto close_fail;
  1582. /* AK: actually i see no reason to not allow this for named pipes etc.,
  1583. but keep the previous behaviour for now. */
  1584. if (!ispipe && !S_ISREG(inode->i_mode))
  1585. goto close_fail;
  1586. /*
  1587. * Dont allow local users get cute and trick others to coredump
  1588. * into their pre-created files:
  1589. */
  1590. if (inode->i_uid != current_fsuid())
  1591. goto close_fail;
  1592. if (!file->f_op)
  1593. goto close_fail;
  1594. if (!file->f_op->write)
  1595. goto close_fail;
  1596. if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
  1597. goto close_fail;
  1598. retval = binfmt->core_dump(signr, regs, file, core_limit);
  1599. if (retval)
  1600. current->signal->group_exit_code |= 0x80;
  1601. close_fail:
  1602. filp_close(file, NULL);
  1603. fail_unlock:
  1604. if (helper_argv)
  1605. argv_free(helper_argv);
  1606. revert_creds(old_cred);
  1607. put_cred(cred);
  1608. coredump_finish(mm);
  1609. fail:
  1610. return;
  1611. }