exec.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. /*
  2. * linux/fs/exec.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * #!-checking implemented by tytso.
  8. */
  9. /*
  10. * Demand-loading implemented 01.12.91 - no need to read anything but
  11. * the header into memory. The inode of the executable is put into
  12. * "current->executable", and page faults do the actual loading. Clean.
  13. *
  14. * Once more I can proudly say that linux stood up to being changed: it
  15. * was less than 2 hours work to get demand-loading completely implemented.
  16. *
  17. * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead,
  18. * current->executable is only used by the procfs. This allows a dispatch
  19. * table to check for several different types of binary formats. We keep
  20. * trying until we recognize the file or we run out of supported binary
  21. * formats.
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/file.h>
  25. #include <linux/fdtable.h>
  26. #include <linux/mm.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/swap.h>
  30. #include <linux/string.h>
  31. #include <linux/init.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/perf_event.h>
  34. #include <linux/highmem.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/key.h>
  37. #include <linux/personality.h>
  38. #include <linux/binfmts.h>
  39. #include <linux/utsname.h>
  40. #include <linux/pid_namespace.h>
  41. #include <linux/module.h>
  42. #include <linux/namei.h>
  43. #include <linux/mount.h>
  44. #include <linux/security.h>
  45. #include <linux/syscalls.h>
  46. #include <linux/tsacct_kern.h>
  47. #include <linux/cn_proc.h>
  48. #include <linux/audit.h>
  49. #include <linux/tracehook.h>
  50. #include <linux/kmod.h>
  51. #include <linux/fsnotify.h>
  52. #include <linux/fs_struct.h>
  53. #include <linux/pipe_fs_i.h>
  54. #include <linux/oom.h>
  55. #include <linux/compat.h>
  56. #include <asm/uaccess.h>
  57. #include <asm/mmu_context.h>
  58. #include <asm/tlb.h>
  59. #include <trace/events/task.h>
  60. #include "internal.h"
  61. #include "coredump.h"
  62. #include <trace/events/sched.h>
  63. int suid_dumpable = 0;
  64. static LIST_HEAD(formats);
  65. static DEFINE_RWLOCK(binfmt_lock);
  66. void __register_binfmt(struct linux_binfmt * fmt, int insert)
  67. {
  68. BUG_ON(!fmt);
  69. write_lock(&binfmt_lock);
  70. insert ? list_add(&fmt->lh, &formats) :
  71. list_add_tail(&fmt->lh, &formats);
  72. write_unlock(&binfmt_lock);
  73. }
  74. EXPORT_SYMBOL(__register_binfmt);
  75. void unregister_binfmt(struct linux_binfmt * fmt)
  76. {
  77. write_lock(&binfmt_lock);
  78. list_del(&fmt->lh);
  79. write_unlock(&binfmt_lock);
  80. }
  81. EXPORT_SYMBOL(unregister_binfmt);
  82. static inline void put_binfmt(struct linux_binfmt * fmt)
  83. {
  84. module_put(fmt->module);
  85. }
  86. /*
  87. * Note that a shared library must be both readable and executable due to
  88. * security reasons.
  89. *
  90. * Also note that we take the address to load from from the file itself.
  91. */
  92. SYSCALL_DEFINE1(uselib, const char __user *, library)
  93. {
  94. struct file *file;
  95. struct filename *tmp = getname(library);
  96. int error = PTR_ERR(tmp);
  97. static const struct open_flags uselib_flags = {
  98. .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
  99. .acc_mode = MAY_READ | MAY_EXEC | MAY_OPEN,
  100. .intent = LOOKUP_OPEN
  101. };
  102. if (IS_ERR(tmp))
  103. goto out;
  104. file = do_filp_open(AT_FDCWD, tmp, &uselib_flags, LOOKUP_FOLLOW);
  105. putname(tmp);
  106. error = PTR_ERR(file);
  107. if (IS_ERR(file))
  108. goto out;
  109. error = -EINVAL;
  110. if (!S_ISREG(file_inode(file)->i_mode))
  111. goto exit;
  112. error = -EACCES;
  113. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  114. goto exit;
  115. fsnotify_open(file);
  116. error = -ENOEXEC;
  117. if(file->f_op) {
  118. struct linux_binfmt * fmt;
  119. read_lock(&binfmt_lock);
  120. list_for_each_entry(fmt, &formats, lh) {
  121. if (!fmt->load_shlib)
  122. continue;
  123. if (!try_module_get(fmt->module))
  124. continue;
  125. read_unlock(&binfmt_lock);
  126. error = fmt->load_shlib(file);
  127. read_lock(&binfmt_lock);
  128. put_binfmt(fmt);
  129. if (error != -ENOEXEC)
  130. break;
  131. }
  132. read_unlock(&binfmt_lock);
  133. }
  134. exit:
  135. fput(file);
  136. out:
  137. return error;
  138. }
  139. #ifdef CONFIG_MMU
  140. /*
  141. * The nascent bprm->mm is not visible until exec_mmap() but it can
  142. * use a lot of memory, account these pages in current->mm temporary
  143. * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
  144. * change the counter back via acct_arg_size(0).
  145. */
  146. static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
  147. {
  148. struct mm_struct *mm = current->mm;
  149. long diff = (long)(pages - bprm->vma_pages);
  150. if (!mm || !diff)
  151. return;
  152. bprm->vma_pages = pages;
  153. add_mm_counter(mm, MM_ANONPAGES, diff);
  154. }
  155. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  156. int write)
  157. {
  158. struct page *page;
  159. int ret;
  160. #ifdef CONFIG_STACK_GROWSUP
  161. if (write) {
  162. ret = expand_downwards(bprm->vma, pos);
  163. if (ret < 0)
  164. return NULL;
  165. }
  166. #endif
  167. ret = get_user_pages(current, bprm->mm, pos,
  168. 1, write, 1, &page, NULL);
  169. if (ret <= 0)
  170. return NULL;
  171. if (write) {
  172. unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
  173. struct rlimit *rlim;
  174. acct_arg_size(bprm, size / PAGE_SIZE);
  175. /*
  176. * We've historically supported up to 32 pages (ARG_MAX)
  177. * of argument strings even with small stacks
  178. */
  179. if (size <= ARG_MAX)
  180. return page;
  181. /*
  182. * Limit to 1/4-th the stack size for the argv+env strings.
  183. * This ensures that:
  184. * - the remaining binfmt code will not run out of stack space,
  185. * - the program will have a reasonable amount of stack left
  186. * to work from.
  187. */
  188. rlim = current->signal->rlim;
  189. if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
  190. put_page(page);
  191. return NULL;
  192. }
  193. }
  194. return page;
  195. }
  196. static void put_arg_page(struct page *page)
  197. {
  198. put_page(page);
  199. }
  200. static void free_arg_page(struct linux_binprm *bprm, int i)
  201. {
  202. }
  203. static void free_arg_pages(struct linux_binprm *bprm)
  204. {
  205. }
  206. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  207. struct page *page)
  208. {
  209. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  210. }
  211. static int __bprm_mm_init(struct linux_binprm *bprm)
  212. {
  213. int err;
  214. struct vm_area_struct *vma = NULL;
  215. struct mm_struct *mm = bprm->mm;
  216. bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  217. if (!vma)
  218. return -ENOMEM;
  219. down_write(&mm->mmap_sem);
  220. vma->vm_mm = mm;
  221. /*
  222. * Place the stack at the largest stack address the architecture
  223. * supports. Later, we'll move this to an appropriate place. We don't
  224. * use STACK_TOP because that can depend on attributes which aren't
  225. * configured yet.
  226. */
  227. BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
  228. vma->vm_end = STACK_TOP_MAX;
  229. vma->vm_start = vma->vm_end - PAGE_SIZE;
  230. vma->vm_flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
  231. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  232. INIT_LIST_HEAD(&vma->anon_vma_chain);
  233. err = insert_vm_struct(mm, vma);
  234. if (err)
  235. goto err;
  236. mm->stack_vm = mm->total_vm = 1;
  237. up_write(&mm->mmap_sem);
  238. bprm->p = vma->vm_end - sizeof(void *);
  239. return 0;
  240. err:
  241. up_write(&mm->mmap_sem);
  242. bprm->vma = NULL;
  243. kmem_cache_free(vm_area_cachep, vma);
  244. return err;
  245. }
  246. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  247. {
  248. return len <= MAX_ARG_STRLEN;
  249. }
  250. #else
  251. static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
  252. {
  253. }
  254. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  255. int write)
  256. {
  257. struct page *page;
  258. page = bprm->page[pos / PAGE_SIZE];
  259. if (!page && write) {
  260. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  261. if (!page)
  262. return NULL;
  263. bprm->page[pos / PAGE_SIZE] = page;
  264. }
  265. return page;
  266. }
  267. static void put_arg_page(struct page *page)
  268. {
  269. }
  270. static void free_arg_page(struct linux_binprm *bprm, int i)
  271. {
  272. if (bprm->page[i]) {
  273. __free_page(bprm->page[i]);
  274. bprm->page[i] = NULL;
  275. }
  276. }
  277. static void free_arg_pages(struct linux_binprm *bprm)
  278. {
  279. int i;
  280. for (i = 0; i < MAX_ARG_PAGES; i++)
  281. free_arg_page(bprm, i);
  282. }
  283. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  284. struct page *page)
  285. {
  286. }
  287. static int __bprm_mm_init(struct linux_binprm *bprm)
  288. {
  289. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  290. return 0;
  291. }
  292. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  293. {
  294. return len <= bprm->p;
  295. }
  296. #endif /* CONFIG_MMU */
  297. /*
  298. * Create a new mm_struct and populate it with a temporary stack
  299. * vm_area_struct. We don't have enough context at this point to set the stack
  300. * flags, permissions, and offset, so we use temporary values. We'll update
  301. * them later in setup_arg_pages().
  302. */
  303. static int bprm_mm_init(struct linux_binprm *bprm)
  304. {
  305. int err;
  306. struct mm_struct *mm = NULL;
  307. bprm->mm = mm = mm_alloc();
  308. err = -ENOMEM;
  309. if (!mm)
  310. goto err;
  311. err = init_new_context(current, mm);
  312. if (err)
  313. goto err;
  314. err = __bprm_mm_init(bprm);
  315. if (err)
  316. goto err;
  317. return 0;
  318. err:
  319. if (mm) {
  320. bprm->mm = NULL;
  321. mmdrop(mm);
  322. }
  323. return err;
  324. }
  325. struct user_arg_ptr {
  326. #ifdef CONFIG_COMPAT
  327. bool is_compat;
  328. #endif
  329. union {
  330. const char __user *const __user *native;
  331. #ifdef CONFIG_COMPAT
  332. const compat_uptr_t __user *compat;
  333. #endif
  334. } ptr;
  335. };
  336. static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
  337. {
  338. const char __user *native;
  339. #ifdef CONFIG_COMPAT
  340. if (unlikely(argv.is_compat)) {
  341. compat_uptr_t compat;
  342. if (get_user(compat, argv.ptr.compat + nr))
  343. return ERR_PTR(-EFAULT);
  344. return compat_ptr(compat);
  345. }
  346. #endif
  347. if (get_user(native, argv.ptr.native + nr))
  348. return ERR_PTR(-EFAULT);
  349. return native;
  350. }
  351. /*
  352. * count() counts the number of strings in array ARGV.
  353. */
  354. static int count(struct user_arg_ptr argv, int max)
  355. {
  356. int i = 0;
  357. if (argv.ptr.native != NULL) {
  358. for (;;) {
  359. const char __user *p = get_user_arg_ptr(argv, i);
  360. if (!p)
  361. break;
  362. if (IS_ERR(p))
  363. return -EFAULT;
  364. if (i >= max)
  365. return -E2BIG;
  366. ++i;
  367. if (fatal_signal_pending(current))
  368. return -ERESTARTNOHAND;
  369. cond_resched();
  370. }
  371. }
  372. return i;
  373. }
  374. /*
  375. * 'copy_strings()' copies argument/environment strings from the old
  376. * processes's memory to the new process's stack. The call to get_user_pages()
  377. * ensures the destination page is created and not swapped out.
  378. */
  379. static int copy_strings(int argc, struct user_arg_ptr argv,
  380. struct linux_binprm *bprm)
  381. {
  382. struct page *kmapped_page = NULL;
  383. char *kaddr = NULL;
  384. unsigned long kpos = 0;
  385. int ret;
  386. while (argc-- > 0) {
  387. const char __user *str;
  388. int len;
  389. unsigned long pos;
  390. ret = -EFAULT;
  391. str = get_user_arg_ptr(argv, argc);
  392. if (IS_ERR(str))
  393. goto out;
  394. len = strnlen_user(str, MAX_ARG_STRLEN);
  395. if (!len)
  396. goto out;
  397. ret = -E2BIG;
  398. if (!valid_arg_len(bprm, len))
  399. goto out;
  400. /* We're going to work our way backwords. */
  401. pos = bprm->p;
  402. str += len;
  403. bprm->p -= len;
  404. while (len > 0) {
  405. int offset, bytes_to_copy;
  406. if (fatal_signal_pending(current)) {
  407. ret = -ERESTARTNOHAND;
  408. goto out;
  409. }
  410. cond_resched();
  411. offset = pos % PAGE_SIZE;
  412. if (offset == 0)
  413. offset = PAGE_SIZE;
  414. bytes_to_copy = offset;
  415. if (bytes_to_copy > len)
  416. bytes_to_copy = len;
  417. offset -= bytes_to_copy;
  418. pos -= bytes_to_copy;
  419. str -= bytes_to_copy;
  420. len -= bytes_to_copy;
  421. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  422. struct page *page;
  423. page = get_arg_page(bprm, pos, 1);
  424. if (!page) {
  425. ret = -E2BIG;
  426. goto out;
  427. }
  428. if (kmapped_page) {
  429. flush_kernel_dcache_page(kmapped_page);
  430. kunmap(kmapped_page);
  431. put_arg_page(kmapped_page);
  432. }
  433. kmapped_page = page;
  434. kaddr = kmap(kmapped_page);
  435. kpos = pos & PAGE_MASK;
  436. flush_arg_page(bprm, kpos, kmapped_page);
  437. }
  438. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  439. ret = -EFAULT;
  440. goto out;
  441. }
  442. }
  443. }
  444. ret = 0;
  445. out:
  446. if (kmapped_page) {
  447. flush_kernel_dcache_page(kmapped_page);
  448. kunmap(kmapped_page);
  449. put_arg_page(kmapped_page);
  450. }
  451. return ret;
  452. }
  453. /*
  454. * Like copy_strings, but get argv and its values from kernel memory.
  455. */
  456. int copy_strings_kernel(int argc, const char *const *__argv,
  457. struct linux_binprm *bprm)
  458. {
  459. int r;
  460. mm_segment_t oldfs = get_fs();
  461. struct user_arg_ptr argv = {
  462. .ptr.native = (const char __user *const __user *)__argv,
  463. };
  464. set_fs(KERNEL_DS);
  465. r = copy_strings(argc, argv, bprm);
  466. set_fs(oldfs);
  467. return r;
  468. }
  469. EXPORT_SYMBOL(copy_strings_kernel);
  470. #ifdef CONFIG_MMU
  471. /*
  472. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  473. * the binfmt code determines where the new stack should reside, we shift it to
  474. * its final location. The process proceeds as follows:
  475. *
  476. * 1) Use shift to calculate the new vma endpoints.
  477. * 2) Extend vma to cover both the old and new ranges. This ensures the
  478. * arguments passed to subsequent functions are consistent.
  479. * 3) Move vma's page tables to the new range.
  480. * 4) Free up any cleared pgd range.
  481. * 5) Shrink the vma to cover only the new range.
  482. */
  483. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  484. {
  485. struct mm_struct *mm = vma->vm_mm;
  486. unsigned long old_start = vma->vm_start;
  487. unsigned long old_end = vma->vm_end;
  488. unsigned long length = old_end - old_start;
  489. unsigned long new_start = old_start - shift;
  490. unsigned long new_end = old_end - shift;
  491. struct mmu_gather tlb;
  492. BUG_ON(new_start > new_end);
  493. /*
  494. * ensure there are no vmas between where we want to go
  495. * and where we are
  496. */
  497. if (vma != find_vma(mm, new_start))
  498. return -EFAULT;
  499. /*
  500. * cover the whole range: [new_start, old_end)
  501. */
  502. if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
  503. return -ENOMEM;
  504. /*
  505. * move the page tables downwards, on failure we rely on
  506. * process cleanup to remove whatever mess we made.
  507. */
  508. if (length != move_page_tables(vma, old_start,
  509. vma, new_start, length, false))
  510. return -ENOMEM;
  511. lru_add_drain();
  512. tlb_gather_mmu(&tlb, mm, 0);
  513. if (new_end > old_start) {
  514. /*
  515. * when the old and new regions overlap clear from new_end.
  516. */
  517. free_pgd_range(&tlb, new_end, old_end, new_end,
  518. vma->vm_next ? vma->vm_next->vm_start : 0);
  519. } else {
  520. /*
  521. * otherwise, clean from old_start; this is done to not touch
  522. * the address space in [new_end, old_start) some architectures
  523. * have constraints on va-space that make this illegal (IA64) -
  524. * for the others its just a little faster.
  525. */
  526. free_pgd_range(&tlb, old_start, old_end, new_end,
  527. vma->vm_next ? vma->vm_next->vm_start : 0);
  528. }
  529. tlb_finish_mmu(&tlb, new_end, old_end);
  530. /*
  531. * Shrink the vma to just the new range. Always succeeds.
  532. */
  533. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  534. return 0;
  535. }
  536. /*
  537. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  538. * the stack is optionally relocated, and some extra space is added.
  539. */
  540. int setup_arg_pages(struct linux_binprm *bprm,
  541. unsigned long stack_top,
  542. int executable_stack)
  543. {
  544. unsigned long ret;
  545. unsigned long stack_shift;
  546. struct mm_struct *mm = current->mm;
  547. struct vm_area_struct *vma = bprm->vma;
  548. struct vm_area_struct *prev = NULL;
  549. unsigned long vm_flags;
  550. unsigned long stack_base;
  551. unsigned long stack_size;
  552. unsigned long stack_expand;
  553. unsigned long rlim_stack;
  554. #ifdef CONFIG_STACK_GROWSUP
  555. /* Limit stack size to 1GB */
  556. stack_base = rlimit_max(RLIMIT_STACK);
  557. if (stack_base > (1 << 30))
  558. stack_base = 1 << 30;
  559. /* Make sure we didn't let the argument array grow too large. */
  560. if (vma->vm_end - vma->vm_start > stack_base)
  561. return -ENOMEM;
  562. stack_base = PAGE_ALIGN(stack_top - stack_base);
  563. stack_shift = vma->vm_start - stack_base;
  564. mm->arg_start = bprm->p - stack_shift;
  565. bprm->p = vma->vm_end - stack_shift;
  566. #else
  567. stack_top = arch_align_stack(stack_top);
  568. stack_top = PAGE_ALIGN(stack_top);
  569. if (unlikely(stack_top < mmap_min_addr) ||
  570. unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
  571. return -ENOMEM;
  572. stack_shift = vma->vm_end - stack_top;
  573. bprm->p -= stack_shift;
  574. mm->arg_start = bprm->p;
  575. #endif
  576. if (bprm->loader)
  577. bprm->loader -= stack_shift;
  578. bprm->exec -= stack_shift;
  579. down_write(&mm->mmap_sem);
  580. vm_flags = VM_STACK_FLAGS;
  581. /*
  582. * Adjust stack execute permissions; explicitly enable for
  583. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  584. * (arch default) otherwise.
  585. */
  586. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  587. vm_flags |= VM_EXEC;
  588. else if (executable_stack == EXSTACK_DISABLE_X)
  589. vm_flags &= ~VM_EXEC;
  590. vm_flags |= mm->def_flags;
  591. vm_flags |= VM_STACK_INCOMPLETE_SETUP;
  592. ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
  593. vm_flags);
  594. if (ret)
  595. goto out_unlock;
  596. BUG_ON(prev != vma);
  597. /* Move stack pages down in memory. */
  598. if (stack_shift) {
  599. ret = shift_arg_pages(vma, stack_shift);
  600. if (ret)
  601. goto out_unlock;
  602. }
  603. /* mprotect_fixup is overkill to remove the temporary stack flags */
  604. vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
  605. stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
  606. stack_size = vma->vm_end - vma->vm_start;
  607. /*
  608. * Align this down to a page boundary as expand_stack
  609. * will align it up.
  610. */
  611. rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
  612. #ifdef CONFIG_STACK_GROWSUP
  613. if (stack_size + stack_expand > rlim_stack)
  614. stack_base = vma->vm_start + rlim_stack;
  615. else
  616. stack_base = vma->vm_end + stack_expand;
  617. #else
  618. if (stack_size + stack_expand > rlim_stack)
  619. stack_base = vma->vm_end - rlim_stack;
  620. else
  621. stack_base = vma->vm_start - stack_expand;
  622. #endif
  623. current->mm->start_stack = bprm->p;
  624. ret = expand_stack(vma, stack_base);
  625. if (ret)
  626. ret = -EFAULT;
  627. out_unlock:
  628. up_write(&mm->mmap_sem);
  629. return ret;
  630. }
  631. EXPORT_SYMBOL(setup_arg_pages);
  632. #endif /* CONFIG_MMU */
  633. struct file *open_exec(const char *name)
  634. {
  635. struct file *file;
  636. int err;
  637. struct filename tmp = { .name = name };
  638. static const struct open_flags open_exec_flags = {
  639. .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
  640. .acc_mode = MAY_EXEC | MAY_OPEN,
  641. .intent = LOOKUP_OPEN
  642. };
  643. file = do_filp_open(AT_FDCWD, &tmp, &open_exec_flags, LOOKUP_FOLLOW);
  644. if (IS_ERR(file))
  645. goto out;
  646. err = -EACCES;
  647. if (!S_ISREG(file_inode(file)->i_mode))
  648. goto exit;
  649. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  650. goto exit;
  651. fsnotify_open(file);
  652. err = deny_write_access(file);
  653. if (err)
  654. goto exit;
  655. out:
  656. return file;
  657. exit:
  658. fput(file);
  659. return ERR_PTR(err);
  660. }
  661. EXPORT_SYMBOL(open_exec);
  662. int kernel_read(struct file *file, loff_t offset,
  663. char *addr, unsigned long count)
  664. {
  665. mm_segment_t old_fs;
  666. loff_t pos = offset;
  667. int result;
  668. old_fs = get_fs();
  669. set_fs(get_ds());
  670. /* The cast to a user pointer is valid due to the set_fs() */
  671. result = vfs_read(file, (void __user *)addr, count, &pos);
  672. set_fs(old_fs);
  673. return result;
  674. }
  675. EXPORT_SYMBOL(kernel_read);
  676. static int exec_mmap(struct mm_struct *mm)
  677. {
  678. struct task_struct *tsk;
  679. struct mm_struct * old_mm, *active_mm;
  680. /* Notify parent that we're no longer interested in the old VM */
  681. tsk = current;
  682. old_mm = current->mm;
  683. mm_release(tsk, old_mm);
  684. if (old_mm) {
  685. sync_mm_rss(old_mm);
  686. /*
  687. * Make sure that if there is a core dump in progress
  688. * for the old mm, we get out and die instead of going
  689. * through with the exec. We must hold mmap_sem around
  690. * checking core_state and changing tsk->mm.
  691. */
  692. down_read(&old_mm->mmap_sem);
  693. if (unlikely(old_mm->core_state)) {
  694. up_read(&old_mm->mmap_sem);
  695. return -EINTR;
  696. }
  697. }
  698. task_lock(tsk);
  699. active_mm = tsk->active_mm;
  700. tsk->mm = mm;
  701. tsk->active_mm = mm;
  702. activate_mm(active_mm, mm);
  703. task_unlock(tsk);
  704. arch_pick_mmap_layout(mm);
  705. if (old_mm) {
  706. up_read(&old_mm->mmap_sem);
  707. BUG_ON(active_mm != old_mm);
  708. setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
  709. mm_update_next_owner(old_mm);
  710. mmput(old_mm);
  711. return 0;
  712. }
  713. mmdrop(active_mm);
  714. return 0;
  715. }
  716. /*
  717. * This function makes sure the current process has its own signal table,
  718. * so that flush_signal_handlers can later reset the handlers without
  719. * disturbing other processes. (Other processes might share the signal
  720. * table via the CLONE_SIGHAND option to clone().)
  721. */
  722. static int de_thread(struct task_struct *tsk)
  723. {
  724. struct signal_struct *sig = tsk->signal;
  725. struct sighand_struct *oldsighand = tsk->sighand;
  726. spinlock_t *lock = &oldsighand->siglock;
  727. if (thread_group_empty(tsk))
  728. goto no_thread_group;
  729. /*
  730. * Kill all other threads in the thread group.
  731. */
  732. spin_lock_irq(lock);
  733. if (signal_group_exit(sig)) {
  734. /*
  735. * Another group action in progress, just
  736. * return so that the signal is processed.
  737. */
  738. spin_unlock_irq(lock);
  739. return -EAGAIN;
  740. }
  741. sig->group_exit_task = tsk;
  742. sig->notify_count = zap_other_threads(tsk);
  743. if (!thread_group_leader(tsk))
  744. sig->notify_count--;
  745. while (sig->notify_count) {
  746. __set_current_state(TASK_KILLABLE);
  747. spin_unlock_irq(lock);
  748. schedule();
  749. if (unlikely(__fatal_signal_pending(tsk)))
  750. goto killed;
  751. spin_lock_irq(lock);
  752. }
  753. spin_unlock_irq(lock);
  754. /*
  755. * At this point all other threads have exited, all we have to
  756. * do is to wait for the thread group leader to become inactive,
  757. * and to assume its PID:
  758. */
  759. if (!thread_group_leader(tsk)) {
  760. struct task_struct *leader = tsk->group_leader;
  761. sig->notify_count = -1; /* for exit_notify() */
  762. for (;;) {
  763. write_lock_irq(&tasklist_lock);
  764. if (likely(leader->exit_state))
  765. break;
  766. __set_current_state(TASK_KILLABLE);
  767. write_unlock_irq(&tasklist_lock);
  768. schedule();
  769. if (unlikely(__fatal_signal_pending(tsk)))
  770. goto killed;
  771. }
  772. /*
  773. * The only record we have of the real-time age of a
  774. * process, regardless of execs it's done, is start_time.
  775. * All the past CPU time is accumulated in signal_struct
  776. * from sister threads now dead. But in this non-leader
  777. * exec, nothing survives from the original leader thread,
  778. * whose birth marks the true age of this process now.
  779. * When we take on its identity by switching to its PID, we
  780. * also take its birthdate (always earlier than our own).
  781. */
  782. tsk->start_time = leader->start_time;
  783. BUG_ON(!same_thread_group(leader, tsk));
  784. BUG_ON(has_group_leader_pid(tsk));
  785. /*
  786. * An exec() starts a new thread group with the
  787. * TGID of the previous thread group. Rehash the
  788. * two threads with a switched PID, and release
  789. * the former thread group leader:
  790. */
  791. /* Become a process group leader with the old leader's pid.
  792. * The old leader becomes a thread of the this thread group.
  793. * Note: The old leader also uses this pid until release_task
  794. * is called. Odd but simple and correct.
  795. */
  796. detach_pid(tsk, PIDTYPE_PID);
  797. tsk->pid = leader->pid;
  798. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  799. transfer_pid(leader, tsk, PIDTYPE_PGID);
  800. transfer_pid(leader, tsk, PIDTYPE_SID);
  801. list_replace_rcu(&leader->tasks, &tsk->tasks);
  802. list_replace_init(&leader->sibling, &tsk->sibling);
  803. tsk->group_leader = tsk;
  804. leader->group_leader = tsk;
  805. tsk->exit_signal = SIGCHLD;
  806. leader->exit_signal = -1;
  807. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  808. leader->exit_state = EXIT_DEAD;
  809. /*
  810. * We are going to release_task()->ptrace_unlink() silently,
  811. * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
  812. * the tracer wont't block again waiting for this thread.
  813. */
  814. if (unlikely(leader->ptrace))
  815. __wake_up_parent(leader, leader->parent);
  816. write_unlock_irq(&tasklist_lock);
  817. release_task(leader);
  818. }
  819. sig->group_exit_task = NULL;
  820. sig->notify_count = 0;
  821. no_thread_group:
  822. /* we have changed execution domain */
  823. tsk->exit_signal = SIGCHLD;
  824. exit_itimers(sig);
  825. flush_itimer_signals();
  826. if (atomic_read(&oldsighand->count) != 1) {
  827. struct sighand_struct *newsighand;
  828. /*
  829. * This ->sighand is shared with the CLONE_SIGHAND
  830. * but not CLONE_THREAD task, switch to the new one.
  831. */
  832. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  833. if (!newsighand)
  834. return -ENOMEM;
  835. atomic_set(&newsighand->count, 1);
  836. memcpy(newsighand->action, oldsighand->action,
  837. sizeof(newsighand->action));
  838. write_lock_irq(&tasklist_lock);
  839. spin_lock(&oldsighand->siglock);
  840. rcu_assign_pointer(tsk->sighand, newsighand);
  841. spin_unlock(&oldsighand->siglock);
  842. write_unlock_irq(&tasklist_lock);
  843. __cleanup_sighand(oldsighand);
  844. }
  845. BUG_ON(!thread_group_leader(tsk));
  846. return 0;
  847. killed:
  848. /* protects against exit_notify() and __exit_signal() */
  849. read_lock(&tasklist_lock);
  850. sig->group_exit_task = NULL;
  851. sig->notify_count = 0;
  852. read_unlock(&tasklist_lock);
  853. return -EAGAIN;
  854. }
  855. char *get_task_comm(char *buf, struct task_struct *tsk)
  856. {
  857. /* buf must be at least sizeof(tsk->comm) in size */
  858. task_lock(tsk);
  859. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  860. task_unlock(tsk);
  861. return buf;
  862. }
  863. EXPORT_SYMBOL_GPL(get_task_comm);
  864. /*
  865. * These functions flushes out all traces of the currently running executable
  866. * so that a new one can be started
  867. */
  868. void set_task_comm(struct task_struct *tsk, char *buf)
  869. {
  870. task_lock(tsk);
  871. trace_task_rename(tsk, buf);
  872. /*
  873. * Threads may access current->comm without holding
  874. * the task lock, so write the string carefully.
  875. * Readers without a lock may see incomplete new
  876. * names but are safe from non-terminating string reads.
  877. */
  878. memset(tsk->comm, 0, TASK_COMM_LEN);
  879. wmb();
  880. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  881. task_unlock(tsk);
  882. perf_event_comm(tsk);
  883. }
  884. static void filename_to_taskname(char *tcomm, const char *fn, unsigned int len)
  885. {
  886. int i, ch;
  887. /* Copies the binary name from after last slash */
  888. for (i = 0; (ch = *(fn++)) != '\0';) {
  889. if (ch == '/')
  890. i = 0; /* overwrite what we wrote */
  891. else
  892. if (i < len - 1)
  893. tcomm[i++] = ch;
  894. }
  895. tcomm[i] = '\0';
  896. }
  897. int flush_old_exec(struct linux_binprm * bprm)
  898. {
  899. int retval;
  900. /*
  901. * Make sure we have a private signal table and that
  902. * we are unassociated from the previous thread group.
  903. */
  904. retval = de_thread(current);
  905. if (retval)
  906. goto out;
  907. set_mm_exe_file(bprm->mm, bprm->file);
  908. filename_to_taskname(bprm->tcomm, bprm->filename, sizeof(bprm->tcomm));
  909. /*
  910. * Release all of the old mmap stuff
  911. */
  912. acct_arg_size(bprm, 0);
  913. retval = exec_mmap(bprm->mm);
  914. if (retval)
  915. goto out;
  916. bprm->mm = NULL; /* We're using it now */
  917. set_fs(USER_DS);
  918. current->flags &=
  919. ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD | PF_NOFREEZE);
  920. flush_thread();
  921. current->personality &= ~bprm->per_clear;
  922. return 0;
  923. out:
  924. return retval;
  925. }
  926. EXPORT_SYMBOL(flush_old_exec);
  927. void would_dump(struct linux_binprm *bprm, struct file *file)
  928. {
  929. if (inode_permission(file_inode(file), MAY_READ) < 0)
  930. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  931. }
  932. EXPORT_SYMBOL(would_dump);
  933. void setup_new_exec(struct linux_binprm * bprm)
  934. {
  935. arch_pick_mmap_layout(current->mm);
  936. /* This is the point of no return */
  937. current->sas_ss_sp = current->sas_ss_size = 0;
  938. if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
  939. set_dumpable(current->mm, SUID_DUMP_USER);
  940. else
  941. set_dumpable(current->mm, suid_dumpable);
  942. set_task_comm(current, bprm->tcomm);
  943. /* Set the new mm task size. We have to do that late because it may
  944. * depend on TIF_32BIT which is only updated in flush_thread() on
  945. * some architectures like powerpc
  946. */
  947. current->mm->task_size = TASK_SIZE;
  948. /* install the new credentials */
  949. if (!uid_eq(bprm->cred->uid, current_euid()) ||
  950. !gid_eq(bprm->cred->gid, current_egid())) {
  951. current->pdeath_signal = 0;
  952. } else {
  953. would_dump(bprm, bprm->file);
  954. if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
  955. set_dumpable(current->mm, suid_dumpable);
  956. }
  957. /*
  958. * Flush performance counters when crossing a
  959. * security domain:
  960. */
  961. if (!get_dumpable(current->mm))
  962. perf_event_exit_task(current);
  963. /* An exec changes our domain. We are no longer part of the thread
  964. group */
  965. current->self_exec_id++;
  966. flush_signal_handlers(current, 0);
  967. do_close_on_exec(current->files);
  968. }
  969. EXPORT_SYMBOL(setup_new_exec);
  970. /*
  971. * Prepare credentials and lock ->cred_guard_mutex.
  972. * install_exec_creds() commits the new creds and drops the lock.
  973. * Or, if exec fails before, free_bprm() should release ->cred and
  974. * and unlock.
  975. */
  976. int prepare_bprm_creds(struct linux_binprm *bprm)
  977. {
  978. if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
  979. return -ERESTARTNOINTR;
  980. bprm->cred = prepare_exec_creds();
  981. if (likely(bprm->cred))
  982. return 0;
  983. mutex_unlock(&current->signal->cred_guard_mutex);
  984. return -ENOMEM;
  985. }
  986. void free_bprm(struct linux_binprm *bprm)
  987. {
  988. free_arg_pages(bprm);
  989. if (bprm->cred) {
  990. mutex_unlock(&current->signal->cred_guard_mutex);
  991. abort_creds(bprm->cred);
  992. }
  993. /* If a binfmt changed the interp, free it. */
  994. if (bprm->interp != bprm->filename)
  995. kfree(bprm->interp);
  996. kfree(bprm);
  997. }
  998. int bprm_change_interp(char *interp, struct linux_binprm *bprm)
  999. {
  1000. /* If a binfmt changed the interp, free it first. */
  1001. if (bprm->interp != bprm->filename)
  1002. kfree(bprm->interp);
  1003. bprm->interp = kstrdup(interp, GFP_KERNEL);
  1004. if (!bprm->interp)
  1005. return -ENOMEM;
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(bprm_change_interp);
  1009. /*
  1010. * install the new credentials for this executable
  1011. */
  1012. void install_exec_creds(struct linux_binprm *bprm)
  1013. {
  1014. security_bprm_committing_creds(bprm);
  1015. commit_creds(bprm->cred);
  1016. bprm->cred = NULL;
  1017. /*
  1018. * cred_guard_mutex must be held at least to this point to prevent
  1019. * ptrace_attach() from altering our determination of the task's
  1020. * credentials; any time after this it may be unlocked.
  1021. */
  1022. security_bprm_committed_creds(bprm);
  1023. mutex_unlock(&current->signal->cred_guard_mutex);
  1024. }
  1025. EXPORT_SYMBOL(install_exec_creds);
  1026. /*
  1027. * determine how safe it is to execute the proposed program
  1028. * - the caller must hold ->cred_guard_mutex to protect against
  1029. * PTRACE_ATTACH
  1030. */
  1031. static int check_unsafe_exec(struct linux_binprm *bprm)
  1032. {
  1033. struct task_struct *p = current, *t;
  1034. unsigned n_fs;
  1035. int res = 0;
  1036. if (p->ptrace) {
  1037. if (p->ptrace & PT_PTRACE_CAP)
  1038. bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
  1039. else
  1040. bprm->unsafe |= LSM_UNSAFE_PTRACE;
  1041. }
  1042. /*
  1043. * This isn't strictly necessary, but it makes it harder for LSMs to
  1044. * mess up.
  1045. */
  1046. if (current->no_new_privs)
  1047. bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
  1048. n_fs = 1;
  1049. spin_lock(&p->fs->lock);
  1050. rcu_read_lock();
  1051. for (t = next_thread(p); t != p; t = next_thread(t)) {
  1052. if (t->fs == p->fs)
  1053. n_fs++;
  1054. }
  1055. rcu_read_unlock();
  1056. if (p->fs->users > n_fs) {
  1057. bprm->unsafe |= LSM_UNSAFE_SHARE;
  1058. } else {
  1059. res = -EAGAIN;
  1060. if (!p->fs->in_exec) {
  1061. p->fs->in_exec = 1;
  1062. res = 1;
  1063. }
  1064. }
  1065. spin_unlock(&p->fs->lock);
  1066. return res;
  1067. }
  1068. /*
  1069. * Fill the binprm structure from the inode.
  1070. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  1071. *
  1072. * This may be called multiple times for binary chains (scripts for example).
  1073. */
  1074. int prepare_binprm(struct linux_binprm *bprm)
  1075. {
  1076. umode_t mode;
  1077. struct inode * inode = file_inode(bprm->file);
  1078. int retval;
  1079. mode = inode->i_mode;
  1080. if (bprm->file->f_op == NULL)
  1081. return -EACCES;
  1082. /* clear any previous set[ug]id data from a previous binary */
  1083. bprm->cred->euid = current_euid();
  1084. bprm->cred->egid = current_egid();
  1085. if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
  1086. !current->no_new_privs &&
  1087. kuid_has_mapping(bprm->cred->user_ns, inode->i_uid) &&
  1088. kgid_has_mapping(bprm->cred->user_ns, inode->i_gid)) {
  1089. /* Set-uid? */
  1090. if (mode & S_ISUID) {
  1091. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1092. bprm->cred->euid = inode->i_uid;
  1093. }
  1094. /* Set-gid? */
  1095. /*
  1096. * If setgid is set but no group execute bit then this
  1097. * is a candidate for mandatory locking, not a setgid
  1098. * executable.
  1099. */
  1100. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1101. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1102. bprm->cred->egid = inode->i_gid;
  1103. }
  1104. }
  1105. /* fill in binprm security blob */
  1106. retval = security_bprm_set_creds(bprm);
  1107. if (retval)
  1108. return retval;
  1109. bprm->cred_prepared = 1;
  1110. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  1111. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  1112. }
  1113. EXPORT_SYMBOL(prepare_binprm);
  1114. /*
  1115. * Arguments are '\0' separated strings found at the location bprm->p
  1116. * points to; chop off the first by relocating brpm->p to right after
  1117. * the first '\0' encountered.
  1118. */
  1119. int remove_arg_zero(struct linux_binprm *bprm)
  1120. {
  1121. int ret = 0;
  1122. unsigned long offset;
  1123. char *kaddr;
  1124. struct page *page;
  1125. if (!bprm->argc)
  1126. return 0;
  1127. do {
  1128. offset = bprm->p & ~PAGE_MASK;
  1129. page = get_arg_page(bprm, bprm->p, 0);
  1130. if (!page) {
  1131. ret = -EFAULT;
  1132. goto out;
  1133. }
  1134. kaddr = kmap_atomic(page);
  1135. for (; offset < PAGE_SIZE && kaddr[offset];
  1136. offset++, bprm->p++)
  1137. ;
  1138. kunmap_atomic(kaddr);
  1139. put_arg_page(page);
  1140. if (offset == PAGE_SIZE)
  1141. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  1142. } while (offset == PAGE_SIZE);
  1143. bprm->p++;
  1144. bprm->argc--;
  1145. ret = 0;
  1146. out:
  1147. return ret;
  1148. }
  1149. EXPORT_SYMBOL(remove_arg_zero);
  1150. /*
  1151. * cycle the list of binary formats handler, until one recognizes the image
  1152. */
  1153. int search_binary_handler(struct linux_binprm *bprm)
  1154. {
  1155. unsigned int depth = bprm->recursion_depth;
  1156. int try,retval;
  1157. struct linux_binfmt *fmt;
  1158. pid_t old_pid, old_vpid;
  1159. /* This allows 4 levels of binfmt rewrites before failing hard. */
  1160. if (depth > 5)
  1161. return -ELOOP;
  1162. retval = security_bprm_check(bprm);
  1163. if (retval)
  1164. return retval;
  1165. retval = audit_bprm(bprm);
  1166. if (retval)
  1167. return retval;
  1168. /* Need to fetch pid before load_binary changes it */
  1169. old_pid = current->pid;
  1170. rcu_read_lock();
  1171. old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
  1172. rcu_read_unlock();
  1173. retval = -ENOENT;
  1174. for (try=0; try<2; try++) {
  1175. read_lock(&binfmt_lock);
  1176. list_for_each_entry(fmt, &formats, lh) {
  1177. int (*fn)(struct linux_binprm *) = fmt->load_binary;
  1178. if (!fn)
  1179. continue;
  1180. if (!try_module_get(fmt->module))
  1181. continue;
  1182. read_unlock(&binfmt_lock);
  1183. bprm->recursion_depth = depth + 1;
  1184. retval = fn(bprm);
  1185. bprm->recursion_depth = depth;
  1186. if (retval >= 0) {
  1187. if (depth == 0) {
  1188. trace_sched_process_exec(current, old_pid, bprm);
  1189. ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
  1190. }
  1191. put_binfmt(fmt);
  1192. allow_write_access(bprm->file);
  1193. if (bprm->file)
  1194. fput(bprm->file);
  1195. bprm->file = NULL;
  1196. current->did_exec = 1;
  1197. proc_exec_connector(current);
  1198. return retval;
  1199. }
  1200. read_lock(&binfmt_lock);
  1201. put_binfmt(fmt);
  1202. if (retval != -ENOEXEC || bprm->mm == NULL)
  1203. break;
  1204. if (!bprm->file) {
  1205. read_unlock(&binfmt_lock);
  1206. return retval;
  1207. }
  1208. }
  1209. read_unlock(&binfmt_lock);
  1210. #ifdef CONFIG_MODULES
  1211. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1212. break;
  1213. } else {
  1214. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1215. if (printable(bprm->buf[0]) &&
  1216. printable(bprm->buf[1]) &&
  1217. printable(bprm->buf[2]) &&
  1218. printable(bprm->buf[3]))
  1219. break; /* -ENOEXEC */
  1220. if (try)
  1221. break; /* -ENOEXEC */
  1222. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1223. }
  1224. #else
  1225. break;
  1226. #endif
  1227. }
  1228. return retval;
  1229. }
  1230. EXPORT_SYMBOL(search_binary_handler);
  1231. /*
  1232. * sys_execve() executes a new program.
  1233. */
  1234. static int do_execve_common(const char *filename,
  1235. struct user_arg_ptr argv,
  1236. struct user_arg_ptr envp)
  1237. {
  1238. struct linux_binprm *bprm;
  1239. struct file *file;
  1240. struct files_struct *displaced;
  1241. bool clear_in_exec;
  1242. int retval;
  1243. const struct cred *cred = current_cred();
  1244. /*
  1245. * We move the actual failure in case of RLIMIT_NPROC excess from
  1246. * set*uid() to execve() because too many poorly written programs
  1247. * don't check setuid() return code. Here we additionally recheck
  1248. * whether NPROC limit is still exceeded.
  1249. */
  1250. if ((current->flags & PF_NPROC_EXCEEDED) &&
  1251. atomic_read(&cred->user->processes) > rlimit(RLIMIT_NPROC)) {
  1252. retval = -EAGAIN;
  1253. goto out_ret;
  1254. }
  1255. /* We're below the limit (still or again), so we don't want to make
  1256. * further execve() calls fail. */
  1257. current->flags &= ~PF_NPROC_EXCEEDED;
  1258. retval = unshare_files(&displaced);
  1259. if (retval)
  1260. goto out_ret;
  1261. retval = -ENOMEM;
  1262. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1263. if (!bprm)
  1264. goto out_files;
  1265. retval = prepare_bprm_creds(bprm);
  1266. if (retval)
  1267. goto out_free;
  1268. retval = check_unsafe_exec(bprm);
  1269. if (retval < 0)
  1270. goto out_free;
  1271. clear_in_exec = retval;
  1272. current->in_execve = 1;
  1273. file = open_exec(filename);
  1274. retval = PTR_ERR(file);
  1275. if (IS_ERR(file))
  1276. goto out_unmark;
  1277. sched_exec();
  1278. bprm->file = file;
  1279. bprm->filename = filename;
  1280. bprm->interp = filename;
  1281. retval = bprm_mm_init(bprm);
  1282. if (retval)
  1283. goto out_file;
  1284. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1285. if ((retval = bprm->argc) < 0)
  1286. goto out;
  1287. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1288. if ((retval = bprm->envc) < 0)
  1289. goto out;
  1290. retval = prepare_binprm(bprm);
  1291. if (retval < 0)
  1292. goto out;
  1293. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1294. if (retval < 0)
  1295. goto out;
  1296. bprm->exec = bprm->p;
  1297. retval = copy_strings(bprm->envc, envp, bprm);
  1298. if (retval < 0)
  1299. goto out;
  1300. retval = copy_strings(bprm->argc, argv, bprm);
  1301. if (retval < 0)
  1302. goto out;
  1303. retval = search_binary_handler(bprm);
  1304. if (retval < 0)
  1305. goto out;
  1306. /* execve succeeded */
  1307. current->fs->in_exec = 0;
  1308. current->in_execve = 0;
  1309. acct_update_integrals(current);
  1310. free_bprm(bprm);
  1311. if (displaced)
  1312. put_files_struct(displaced);
  1313. return retval;
  1314. out:
  1315. if (bprm->mm) {
  1316. acct_arg_size(bprm, 0);
  1317. mmput(bprm->mm);
  1318. }
  1319. out_file:
  1320. if (bprm->file) {
  1321. allow_write_access(bprm->file);
  1322. fput(bprm->file);
  1323. }
  1324. out_unmark:
  1325. if (clear_in_exec)
  1326. current->fs->in_exec = 0;
  1327. current->in_execve = 0;
  1328. out_free:
  1329. free_bprm(bprm);
  1330. out_files:
  1331. if (displaced)
  1332. reset_files_struct(displaced);
  1333. out_ret:
  1334. return retval;
  1335. }
  1336. int do_execve(const char *filename,
  1337. const char __user *const __user *__argv,
  1338. const char __user *const __user *__envp)
  1339. {
  1340. struct user_arg_ptr argv = { .ptr.native = __argv };
  1341. struct user_arg_ptr envp = { .ptr.native = __envp };
  1342. return do_execve_common(filename, argv, envp);
  1343. }
  1344. #ifdef CONFIG_COMPAT
  1345. static int compat_do_execve(const char *filename,
  1346. const compat_uptr_t __user *__argv,
  1347. const compat_uptr_t __user *__envp)
  1348. {
  1349. struct user_arg_ptr argv = {
  1350. .is_compat = true,
  1351. .ptr.compat = __argv,
  1352. };
  1353. struct user_arg_ptr envp = {
  1354. .is_compat = true,
  1355. .ptr.compat = __envp,
  1356. };
  1357. return do_execve_common(filename, argv, envp);
  1358. }
  1359. #endif
  1360. void set_binfmt(struct linux_binfmt *new)
  1361. {
  1362. struct mm_struct *mm = current->mm;
  1363. if (mm->binfmt)
  1364. module_put(mm->binfmt->module);
  1365. mm->binfmt = new;
  1366. if (new)
  1367. __module_get(new->module);
  1368. }
  1369. EXPORT_SYMBOL(set_binfmt);
  1370. /*
  1371. * set_dumpable converts traditional three-value dumpable to two flags and
  1372. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1373. * these bits are not changed atomically. So get_dumpable can observe the
  1374. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1375. * return either old dumpable or new one by paying attention to the order of
  1376. * modifying the bits.
  1377. *
  1378. * dumpable | mm->flags (binary)
  1379. * old new | initial interim final
  1380. * ---------+-----------------------
  1381. * 0 1 | 00 01 01
  1382. * 0 2 | 00 10(*) 11
  1383. * 1 0 | 01 00 00
  1384. * 1 2 | 01 11 11
  1385. * 2 0 | 11 10(*) 00
  1386. * 2 1 | 11 11 01
  1387. *
  1388. * (*) get_dumpable regards interim value of 10 as 11.
  1389. */
  1390. void set_dumpable(struct mm_struct *mm, int value)
  1391. {
  1392. switch (value) {
  1393. case SUID_DUMP_DISABLE:
  1394. clear_bit(MMF_DUMPABLE, &mm->flags);
  1395. smp_wmb();
  1396. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1397. break;
  1398. case SUID_DUMP_USER:
  1399. set_bit(MMF_DUMPABLE, &mm->flags);
  1400. smp_wmb();
  1401. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1402. break;
  1403. case SUID_DUMP_ROOT:
  1404. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1405. smp_wmb();
  1406. set_bit(MMF_DUMPABLE, &mm->flags);
  1407. break;
  1408. }
  1409. }
  1410. int __get_dumpable(unsigned long mm_flags)
  1411. {
  1412. int ret;
  1413. ret = mm_flags & MMF_DUMPABLE_MASK;
  1414. return (ret > SUID_DUMP_USER) ? SUID_DUMP_ROOT : ret;
  1415. }
  1416. int get_dumpable(struct mm_struct *mm)
  1417. {
  1418. return __get_dumpable(mm->flags);
  1419. }
  1420. SYSCALL_DEFINE3(execve,
  1421. const char __user *, filename,
  1422. const char __user *const __user *, argv,
  1423. const char __user *const __user *, envp)
  1424. {
  1425. struct filename *path = getname(filename);
  1426. int error = PTR_ERR(path);
  1427. if (!IS_ERR(path)) {
  1428. error = do_execve(path->name, argv, envp);
  1429. putname(path);
  1430. }
  1431. return error;
  1432. }
  1433. #ifdef CONFIG_COMPAT
  1434. asmlinkage long compat_sys_execve(const char __user * filename,
  1435. const compat_uptr_t __user * argv,
  1436. const compat_uptr_t __user * envp)
  1437. {
  1438. struct filename *path = getname(filename);
  1439. int error = PTR_ERR(path);
  1440. if (!IS_ERR(path)) {
  1441. error = compat_do_execve(path->name, argv, envp);
  1442. putname(path);
  1443. }
  1444. return error;
  1445. }
  1446. #endif