exec.c 41 KB

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