exec.c 42 KB

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