exec.c 41 KB

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