exec.c 42 KB

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