exec.c 43 KB

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