exec.c 45 KB

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