exec.c 43 KB

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