exec.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/exec.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. /*
  8. * #!-checking implemented by tytso.
  9. */
  10. /*
  11. * Demand-loading implemented 01.12.91 - no need to read anything but
  12. * the header into memory. The inode of the executable is put into
  13. * "current->executable", and page faults do the actual loading. Clean.
  14. *
  15. * Once more I can proudly say that linux stood up to being changed: it
  16. * was less than 2 hours work to get demand-loading completely implemented.
  17. *
  18. * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead,
  19. * current->executable is only used by the procfs. This allows a dispatch
  20. * table to check for several different types of binary formats. We keep
  21. * trying until we recognize the file or we run out of supported binary
  22. * formats.
  23. */
  24. #include <linux/kernel_read_file.h>
  25. #include <linux/slab.h>
  26. #include <linux/file.h>
  27. #include <linux/fdtable.h>
  28. #include <linux/mm.h>
  29. #include <linux/stat.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/swap.h>
  32. #include <linux/string.h>
  33. #include <linux/init.h>
  34. #include <linux/sched/mm.h>
  35. #include <linux/sched/coredump.h>
  36. #include <linux/sched/signal.h>
  37. #include <linux/sched/numa_balancing.h>
  38. #include <linux/sched/task.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/perf_event.h>
  41. #include <linux/highmem.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/key.h>
  44. #include <linux/personality.h>
  45. #include <linux/binfmts.h>
  46. #include <linux/utsname.h>
  47. #include <linux/pid_namespace.h>
  48. #include <linux/module.h>
  49. #include <linux/namei.h>
  50. #include <linux/mount.h>
  51. #include <linux/security.h>
  52. #include <linux/syscalls.h>
  53. #include <linux/tsacct_kern.h>
  54. #include <linux/cn_proc.h>
  55. #include <linux/audit.h>
  56. #include <linux/kmod.h>
  57. #include <linux/fsnotify.h>
  58. #include <linux/fs_struct.h>
  59. #include <linux/oom.h>
  60. #include <linux/compat.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/io_uring.h>
  63. #include <linux/syscall_user_dispatch.h>
  64. #include <linux/coredump.h>
  65. #include <linux/uaccess.h>
  66. #include <asm/mmu_context.h>
  67. #include <asm/tlb.h>
  68. #include <trace/events/task.h>
  69. #include "internal.h"
  70. #include <trace/events/sched.h>
  71. static int bprm_creds_from_file(struct linux_binprm *bprm);
  72. int suid_dumpable = 0;
  73. static LIST_HEAD(formats);
  74. static DEFINE_RWLOCK(binfmt_lock);
  75. void __register_binfmt(struct linux_binfmt * fmt, int insert)
  76. {
  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 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. /*
  126. * may_open() has already checked for this, so it should be
  127. * impossible to trip now. But we need to be extra cautious
  128. * and check again at the very end too.
  129. */
  130. error = -EACCES;
  131. if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode) ||
  132. path_noexec(&file->f_path)))
  133. goto exit;
  134. fsnotify_open(file);
  135. error = -ENOEXEC;
  136. read_lock(&binfmt_lock);
  137. list_for_each_entry(fmt, &formats, lh) {
  138. if (!fmt->load_shlib)
  139. continue;
  140. if (!try_module_get(fmt->module))
  141. continue;
  142. read_unlock(&binfmt_lock);
  143. error = fmt->load_shlib(file);
  144. read_lock(&binfmt_lock);
  145. put_binfmt(fmt);
  146. if (error != -ENOEXEC)
  147. break;
  148. }
  149. read_unlock(&binfmt_lock);
  150. exit:
  151. fput(file);
  152. out:
  153. return error;
  154. }
  155. #endif /* #ifdef CONFIG_USELIB */
  156. #ifdef CONFIG_MMU
  157. /*
  158. * The nascent bprm->mm is not visible until exec_mmap() but it can
  159. * use a lot of memory, account these pages in current->mm temporary
  160. * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
  161. * change the counter back via acct_arg_size(0).
  162. */
  163. static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
  164. {
  165. struct mm_struct *mm = current->mm;
  166. long diff = (long)(pages - bprm->vma_pages);
  167. if (!mm || !diff)
  168. return;
  169. bprm->vma_pages = pages;
  170. add_mm_counter(mm, MM_ANONPAGES, diff);
  171. }
  172. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  173. int write)
  174. {
  175. struct page *page;
  176. struct vm_area_struct *vma = bprm->vma;
  177. struct mm_struct *mm = bprm->mm;
  178. int ret;
  179. /*
  180. * Avoid relying on expanding the stack down in GUP (which
  181. * does not work for STACK_GROWSUP anyway), and just do it
  182. * by hand ahead of time.
  183. */
  184. if (write && pos < vma->vm_start) {
  185. mmap_write_lock(mm);
  186. ret = expand_downwards(vma, pos);
  187. if (unlikely(ret < 0)) {
  188. mmap_write_unlock(mm);
  189. return NULL;
  190. }
  191. mmap_write_downgrade(mm);
  192. } else
  193. mmap_read_lock(mm);
  194. /*
  195. * We are doing an exec(). 'current' is the process
  196. * doing the exec and 'mm' is the new process's mm.
  197. */
  198. ret = get_user_pages_remote(mm, pos, 1,
  199. write ? FOLL_WRITE : 0,
  200. &page, NULL, NULL);
  201. mmap_read_unlock(mm);
  202. if (ret <= 0)
  203. return NULL;
  204. if (write)
  205. acct_arg_size(bprm, vma_pages(vma));
  206. return page;
  207. }
  208. static void put_arg_page(struct page *page)
  209. {
  210. put_page(page);
  211. }
  212. static void free_arg_pages(struct linux_binprm *bprm)
  213. {
  214. }
  215. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  216. struct page *page)
  217. {
  218. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  219. }
  220. static int __bprm_mm_init(struct linux_binprm *bprm)
  221. {
  222. int err;
  223. struct vm_area_struct *vma = NULL;
  224. struct mm_struct *mm = bprm->mm;
  225. bprm->vma = vma = vm_area_alloc(mm);
  226. if (!vma)
  227. return -ENOMEM;
  228. vma_set_anonymous(vma);
  229. if (mmap_write_lock_killable(mm)) {
  230. err = -EINTR;
  231. goto err_free;
  232. }
  233. /*
  234. * Place the stack at the largest stack address the architecture
  235. * supports. Later, we'll move this to an appropriate place. We don't
  236. * use STACK_TOP because that can depend on attributes which aren't
  237. * configured yet.
  238. */
  239. BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
  240. vma->vm_end = STACK_TOP_MAX;
  241. vma->vm_start = vma->vm_end - PAGE_SIZE;
  242. vm_flags_init(vma, VM_SOFTDIRTY | VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP);
  243. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  244. err = insert_vm_struct(mm, vma);
  245. if (err)
  246. goto err;
  247. mm->stack_vm = mm->total_vm = 1;
  248. mmap_write_unlock(mm);
  249. bprm->p = vma->vm_end - sizeof(void *);
  250. return 0;
  251. err:
  252. mmap_write_unlock(mm);
  253. err_free:
  254. bprm->vma = NULL;
  255. vm_area_free(vma);
  256. return err;
  257. }
  258. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  259. {
  260. return len <= MAX_ARG_STRLEN;
  261. }
  262. #else
  263. static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
  264. {
  265. }
  266. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  267. int write)
  268. {
  269. struct page *page;
  270. page = bprm->page[pos / PAGE_SIZE];
  271. if (!page && write) {
  272. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  273. if (!page)
  274. return NULL;
  275. bprm->page[pos / PAGE_SIZE] = page;
  276. }
  277. return page;
  278. }
  279. static void put_arg_page(struct page *page)
  280. {
  281. }
  282. static void free_arg_page(struct linux_binprm *bprm, int i)
  283. {
  284. if (bprm->page[i]) {
  285. __free_page(bprm->page[i]);
  286. bprm->page[i] = NULL;
  287. }
  288. }
  289. static void free_arg_pages(struct linux_binprm *bprm)
  290. {
  291. int i;
  292. for (i = 0; i < MAX_ARG_PAGES; i++)
  293. free_arg_page(bprm, i);
  294. }
  295. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  296. struct page *page)
  297. {
  298. }
  299. static int __bprm_mm_init(struct linux_binprm *bprm)
  300. {
  301. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  302. return 0;
  303. }
  304. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  305. {
  306. return len <= bprm->p;
  307. }
  308. #endif /* CONFIG_MMU */
  309. /*
  310. * Create a new mm_struct and populate it with a temporary stack
  311. * vm_area_struct. We don't have enough context at this point to set the stack
  312. * flags, permissions, and offset, so we use temporary values. We'll update
  313. * them later in setup_arg_pages().
  314. */
  315. static int bprm_mm_init(struct linux_binprm *bprm)
  316. {
  317. int err;
  318. struct mm_struct *mm = NULL;
  319. bprm->mm = mm = mm_alloc();
  320. err = -ENOMEM;
  321. if (!mm)
  322. goto err;
  323. /* Save current stack limit for all calculations made during exec. */
  324. task_lock(current->group_leader);
  325. bprm->rlim_stack = current->signal->rlim[RLIMIT_STACK];
  326. task_unlock(current->group_leader);
  327. err = __bprm_mm_init(bprm);
  328. if (err)
  329. goto err;
  330. return 0;
  331. err:
  332. if (mm) {
  333. bprm->mm = NULL;
  334. mmdrop(mm);
  335. }
  336. return err;
  337. }
  338. struct user_arg_ptr {
  339. #ifdef CONFIG_COMPAT
  340. bool is_compat;
  341. #endif
  342. union {
  343. const char __user *const __user *native;
  344. #ifdef CONFIG_COMPAT
  345. const compat_uptr_t __user *compat;
  346. #endif
  347. } ptr;
  348. };
  349. static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
  350. {
  351. const char __user *native;
  352. #ifdef CONFIG_COMPAT
  353. if (unlikely(argv.is_compat)) {
  354. compat_uptr_t compat;
  355. if (get_user(compat, argv.ptr.compat + nr))
  356. return ERR_PTR(-EFAULT);
  357. return compat_ptr(compat);
  358. }
  359. #endif
  360. if (get_user(native, argv.ptr.native + nr))
  361. return ERR_PTR(-EFAULT);
  362. return native;
  363. }
  364. /*
  365. * count() counts the number of strings in array ARGV.
  366. */
  367. static int count(struct user_arg_ptr argv, int max)
  368. {
  369. int i = 0;
  370. if (argv.ptr.native != NULL) {
  371. for (;;) {
  372. const char __user *p = get_user_arg_ptr(argv, i);
  373. if (!p)
  374. break;
  375. if (IS_ERR(p))
  376. return -EFAULT;
  377. if (i >= max)
  378. return -E2BIG;
  379. ++i;
  380. if (fatal_signal_pending(current))
  381. return -ERESTARTNOHAND;
  382. cond_resched();
  383. }
  384. }
  385. return i;
  386. }
  387. static int count_strings_kernel(const char *const *argv)
  388. {
  389. int i;
  390. if (!argv)
  391. return 0;
  392. for (i = 0; argv[i]; ++i) {
  393. if (i >= MAX_ARG_STRINGS)
  394. return -E2BIG;
  395. if (fatal_signal_pending(current))
  396. return -ERESTARTNOHAND;
  397. cond_resched();
  398. }
  399. return i;
  400. }
  401. static int bprm_stack_limits(struct linux_binprm *bprm)
  402. {
  403. unsigned long limit, ptr_size;
  404. /*
  405. * Limit to 1/4 of the max stack size or 3/4 of _STK_LIM
  406. * (whichever is smaller) for the argv+env strings.
  407. * This ensures that:
  408. * - the remaining binfmt code will not run out of stack space,
  409. * - the program will have a reasonable amount of stack left
  410. * to work from.
  411. */
  412. limit = _STK_LIM / 4 * 3;
  413. limit = min(limit, bprm->rlim_stack.rlim_cur / 4);
  414. /*
  415. * We've historically supported up to 32 pages (ARG_MAX)
  416. * of argument strings even with small stacks
  417. */
  418. limit = max_t(unsigned long, limit, ARG_MAX);
  419. /*
  420. * We must account for the size of all the argv and envp pointers to
  421. * the argv and envp strings, since they will also take up space in
  422. * the stack. They aren't stored until much later when we can't
  423. * signal to the parent that the child has run out of stack space.
  424. * Instead, calculate it here so it's possible to fail gracefully.
  425. *
  426. * In the case of argc = 0, make sure there is space for adding a
  427. * empty string (which will bump argc to 1), to ensure confused
  428. * userspace programs don't start processing from argv[1], thinking
  429. * argc can never be 0, to keep them from walking envp by accident.
  430. * See do_execveat_common().
  431. */
  432. ptr_size = (max(bprm->argc, 1) + bprm->envc) * sizeof(void *);
  433. if (limit <= ptr_size)
  434. return -E2BIG;
  435. limit -= ptr_size;
  436. bprm->argmin = bprm->p - limit;
  437. return 0;
  438. }
  439. /*
  440. * 'copy_strings()' copies argument/environment strings from the old
  441. * processes's memory to the new process's stack. The call to get_user_pages()
  442. * ensures the destination page is created and not swapped out.
  443. */
  444. static int copy_strings(int argc, struct user_arg_ptr argv,
  445. struct linux_binprm *bprm)
  446. {
  447. struct page *kmapped_page = NULL;
  448. char *kaddr = NULL;
  449. unsigned long kpos = 0;
  450. int ret;
  451. while (argc-- > 0) {
  452. const char __user *str;
  453. int len;
  454. unsigned long pos;
  455. ret = -EFAULT;
  456. str = get_user_arg_ptr(argv, argc);
  457. if (IS_ERR(str))
  458. goto out;
  459. len = strnlen_user(str, MAX_ARG_STRLEN);
  460. if (!len)
  461. goto out;
  462. ret = -E2BIG;
  463. if (!valid_arg_len(bprm, len))
  464. goto out;
  465. /* We're going to work our way backwards. */
  466. pos = bprm->p;
  467. str += len;
  468. bprm->p -= len;
  469. #ifdef CONFIG_MMU
  470. if (bprm->p < bprm->argmin)
  471. goto out;
  472. #endif
  473. while (len > 0) {
  474. int offset, bytes_to_copy;
  475. if (fatal_signal_pending(current)) {
  476. ret = -ERESTARTNOHAND;
  477. goto out;
  478. }
  479. cond_resched();
  480. offset = pos % PAGE_SIZE;
  481. if (offset == 0)
  482. offset = PAGE_SIZE;
  483. bytes_to_copy = offset;
  484. if (bytes_to_copy > len)
  485. bytes_to_copy = len;
  486. offset -= bytes_to_copy;
  487. pos -= bytes_to_copy;
  488. str -= bytes_to_copy;
  489. len -= bytes_to_copy;
  490. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  491. struct page *page;
  492. page = get_arg_page(bprm, pos, 1);
  493. if (!page) {
  494. ret = -E2BIG;
  495. goto out;
  496. }
  497. if (kmapped_page) {
  498. flush_dcache_page(kmapped_page);
  499. kunmap_local(kaddr);
  500. put_arg_page(kmapped_page);
  501. }
  502. kmapped_page = page;
  503. kaddr = kmap_local_page(kmapped_page);
  504. kpos = pos & PAGE_MASK;
  505. flush_arg_page(bprm, kpos, kmapped_page);
  506. }
  507. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  508. ret = -EFAULT;
  509. goto out;
  510. }
  511. }
  512. }
  513. ret = 0;
  514. out:
  515. if (kmapped_page) {
  516. flush_dcache_page(kmapped_page);
  517. kunmap_local(kaddr);
  518. put_arg_page(kmapped_page);
  519. }
  520. return ret;
  521. }
  522. /*
  523. * Copy and argument/environment string from the kernel to the processes stack.
  524. */
  525. int copy_string_kernel(const char *arg, struct linux_binprm *bprm)
  526. {
  527. int len = strnlen(arg, MAX_ARG_STRLEN) + 1 /* terminating NUL */;
  528. unsigned long pos = bprm->p;
  529. if (len == 0)
  530. return -EFAULT;
  531. if (!valid_arg_len(bprm, len))
  532. return -E2BIG;
  533. /* We're going to work our way backwards. */
  534. arg += len;
  535. bprm->p -= len;
  536. if (IS_ENABLED(CONFIG_MMU) && bprm->p < bprm->argmin)
  537. return -E2BIG;
  538. while (len > 0) {
  539. unsigned int bytes_to_copy = min_t(unsigned int, len,
  540. min_not_zero(offset_in_page(pos), PAGE_SIZE));
  541. struct page *page;
  542. pos -= bytes_to_copy;
  543. arg -= bytes_to_copy;
  544. len -= bytes_to_copy;
  545. page = get_arg_page(bprm, pos, 1);
  546. if (!page)
  547. return -E2BIG;
  548. flush_arg_page(bprm, pos & PAGE_MASK, page);
  549. memcpy_to_page(page, offset_in_page(pos), arg, bytes_to_copy);
  550. put_arg_page(page);
  551. }
  552. return 0;
  553. }
  554. EXPORT_SYMBOL(copy_string_kernel);
  555. static int copy_strings_kernel(int argc, const char *const *argv,
  556. struct linux_binprm *bprm)
  557. {
  558. while (argc-- > 0) {
  559. int ret = copy_string_kernel(argv[argc], bprm);
  560. if (ret < 0)
  561. return ret;
  562. if (fatal_signal_pending(current))
  563. return -ERESTARTNOHAND;
  564. cond_resched();
  565. }
  566. return 0;
  567. }
  568. #ifdef CONFIG_MMU
  569. /*
  570. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  571. * the binfmt code determines where the new stack should reside, we shift it to
  572. * its final location. The process proceeds as follows:
  573. *
  574. * 1) Use shift to calculate the new vma endpoints.
  575. * 2) Extend vma to cover both the old and new ranges. This ensures the
  576. * arguments passed to subsequent functions are consistent.
  577. * 3) Move vma's page tables to the new range.
  578. * 4) Free up any cleared pgd range.
  579. * 5) Shrink the vma to cover only the new range.
  580. */
  581. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  582. {
  583. struct mm_struct *mm = vma->vm_mm;
  584. unsigned long old_start = vma->vm_start;
  585. unsigned long old_end = vma->vm_end;
  586. unsigned long length = old_end - old_start;
  587. unsigned long new_start = old_start - shift;
  588. unsigned long new_end = old_end - shift;
  589. VMA_ITERATOR(vmi, mm, new_start);
  590. struct vm_area_struct *next;
  591. struct mmu_gather tlb;
  592. BUG_ON(new_start > new_end);
  593. /*
  594. * ensure there are no vmas between where we want to go
  595. * and where we are
  596. */
  597. if (vma != vma_next(&vmi))
  598. return -EFAULT;
  599. /*
  600. * cover the whole range: [new_start, old_end)
  601. */
  602. if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
  603. return -ENOMEM;
  604. /*
  605. * move the page tables downwards, on failure we rely on
  606. * process cleanup to remove whatever mess we made.
  607. */
  608. if (length != move_page_tables(vma, old_start,
  609. vma, new_start, length, false))
  610. return -ENOMEM;
  611. lru_add_drain();
  612. tlb_gather_mmu(&tlb, mm);
  613. next = vma_next(&vmi);
  614. if (new_end > old_start) {
  615. /*
  616. * when the old and new regions overlap clear from new_end.
  617. */
  618. free_pgd_range(&tlb, new_end, old_end, new_end,
  619. next ? next->vm_start : USER_PGTABLES_CEILING);
  620. } else {
  621. /*
  622. * otherwise, clean from old_start; this is done to not touch
  623. * the address space in [new_end, old_start) some architectures
  624. * have constraints on va-space that make this illegal (IA64) -
  625. * for the others its just a little faster.
  626. */
  627. free_pgd_range(&tlb, old_start, old_end, new_end,
  628. next ? next->vm_start : USER_PGTABLES_CEILING);
  629. }
  630. tlb_finish_mmu(&tlb);
  631. /*
  632. * Shrink the vma to just the new range. Always succeeds.
  633. */
  634. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  635. return 0;
  636. }
  637. /*
  638. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  639. * the stack is optionally relocated, and some extra space is added.
  640. */
  641. int setup_arg_pages(struct linux_binprm *bprm,
  642. unsigned long stack_top,
  643. int executable_stack)
  644. {
  645. unsigned long ret;
  646. unsigned long stack_shift;
  647. struct mm_struct *mm = current->mm;
  648. struct vm_area_struct *vma = bprm->vma;
  649. struct vm_area_struct *prev = NULL;
  650. unsigned long vm_flags;
  651. unsigned long stack_base;
  652. unsigned long stack_size;
  653. unsigned long stack_expand;
  654. unsigned long rlim_stack;
  655. struct mmu_gather tlb;
  656. #ifdef CONFIG_STACK_GROWSUP
  657. /* Limit stack size */
  658. stack_base = bprm->rlim_stack.rlim_max;
  659. stack_base = calc_max_stack_size(stack_base);
  660. /* Add space for stack randomization. */
  661. stack_base += (STACK_RND_MASK << PAGE_SHIFT);
  662. /* Make sure we didn't let the argument array grow too large. */
  663. if (vma->vm_end - vma->vm_start > stack_base)
  664. return -ENOMEM;
  665. stack_base = PAGE_ALIGN(stack_top - stack_base);
  666. stack_shift = vma->vm_start - stack_base;
  667. mm->arg_start = bprm->p - stack_shift;
  668. bprm->p = vma->vm_end - stack_shift;
  669. #else
  670. stack_top = arch_align_stack(stack_top);
  671. stack_top = PAGE_ALIGN(stack_top);
  672. if (unlikely(stack_top < mmap_min_addr) ||
  673. unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
  674. return -ENOMEM;
  675. stack_shift = vma->vm_end - stack_top;
  676. bprm->p -= stack_shift;
  677. mm->arg_start = bprm->p;
  678. #endif
  679. if (bprm->loader)
  680. bprm->loader -= stack_shift;
  681. bprm->exec -= stack_shift;
  682. if (mmap_write_lock_killable(mm))
  683. return -EINTR;
  684. vm_flags = VM_STACK_FLAGS;
  685. /*
  686. * Adjust stack execute permissions; explicitly enable for
  687. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  688. * (arch default) otherwise.
  689. */
  690. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  691. vm_flags |= VM_EXEC;
  692. else if (executable_stack == EXSTACK_DISABLE_X)
  693. vm_flags &= ~VM_EXEC;
  694. vm_flags |= mm->def_flags;
  695. vm_flags |= VM_STACK_INCOMPLETE_SETUP;
  696. tlb_gather_mmu(&tlb, mm);
  697. ret = mprotect_fixup(&tlb, vma, &prev, vma->vm_start, vma->vm_end,
  698. vm_flags);
  699. tlb_finish_mmu(&tlb);
  700. if (ret)
  701. goto out_unlock;
  702. BUG_ON(prev != vma);
  703. if (unlikely(vm_flags & VM_EXEC)) {
  704. pr_warn_once("process '%pD4' started with executable stack\n",
  705. bprm->file);
  706. }
  707. /* Move stack pages down in memory. */
  708. if (stack_shift) {
  709. ret = shift_arg_pages(vma, stack_shift);
  710. if (ret)
  711. goto out_unlock;
  712. }
  713. /* mprotect_fixup is overkill to remove the temporary stack flags */
  714. vm_flags_clear(vma, VM_STACK_INCOMPLETE_SETUP);
  715. stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
  716. stack_size = vma->vm_end - vma->vm_start;
  717. /*
  718. * Align this down to a page boundary as expand_stack
  719. * will align it up.
  720. */
  721. rlim_stack = bprm->rlim_stack.rlim_cur & PAGE_MASK;
  722. #ifdef CONFIG_STACK_GROWSUP
  723. if (stack_size + stack_expand > rlim_stack)
  724. stack_base = vma->vm_start + rlim_stack;
  725. else
  726. stack_base = vma->vm_end + stack_expand;
  727. #else
  728. if (stack_size + stack_expand > rlim_stack)
  729. stack_base = vma->vm_end - rlim_stack;
  730. else
  731. stack_base = vma->vm_start - stack_expand;
  732. #endif
  733. current->mm->start_stack = bprm->p;
  734. ret = expand_stack_locked(vma, stack_base);
  735. if (ret)
  736. ret = -EFAULT;
  737. out_unlock:
  738. mmap_write_unlock(mm);
  739. return ret;
  740. }
  741. EXPORT_SYMBOL(setup_arg_pages);
  742. #else
  743. /*
  744. * Transfer the program arguments and environment from the holding pages
  745. * onto the stack. The provided stack pointer is adjusted accordingly.
  746. */
  747. int transfer_args_to_stack(struct linux_binprm *bprm,
  748. unsigned long *sp_location)
  749. {
  750. unsigned long index, stop, sp;
  751. int ret = 0;
  752. stop = bprm->p >> PAGE_SHIFT;
  753. sp = *sp_location;
  754. for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
  755. unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0;
  756. char *src = kmap_local_page(bprm->page[index]) + offset;
  757. sp -= PAGE_SIZE - offset;
  758. if (copy_to_user((void *) sp, src, PAGE_SIZE - offset) != 0)
  759. ret = -EFAULT;
  760. kunmap_local(src);
  761. if (ret)
  762. goto out;
  763. }
  764. *sp_location = sp;
  765. out:
  766. return ret;
  767. }
  768. EXPORT_SYMBOL(transfer_args_to_stack);
  769. #endif /* CONFIG_MMU */
  770. static struct file *do_open_execat(int fd, struct filename *name, int flags)
  771. {
  772. struct file *file;
  773. int err;
  774. struct open_flags open_exec_flags = {
  775. .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
  776. .acc_mode = MAY_EXEC,
  777. .intent = LOOKUP_OPEN,
  778. .lookup_flags = LOOKUP_FOLLOW,
  779. };
  780. if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  781. return ERR_PTR(-EINVAL);
  782. if (flags & AT_SYMLINK_NOFOLLOW)
  783. open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW;
  784. if (flags & AT_EMPTY_PATH)
  785. open_exec_flags.lookup_flags |= LOOKUP_EMPTY;
  786. file = do_filp_open(fd, name, &open_exec_flags);
  787. if (IS_ERR(file))
  788. goto out;
  789. /*
  790. * may_open() has already checked for this, so it should be
  791. * impossible to trip now. But we need to be extra cautious
  792. * and check again at the very end too.
  793. */
  794. err = -EACCES;
  795. if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode) ||
  796. path_noexec(&file->f_path)))
  797. goto exit;
  798. err = deny_write_access(file);
  799. if (err)
  800. goto exit;
  801. if (name->name[0] != '\0')
  802. fsnotify_open(file);
  803. out:
  804. return file;
  805. exit:
  806. fput(file);
  807. return ERR_PTR(err);
  808. }
  809. struct file *open_exec(const char *name)
  810. {
  811. struct filename *filename = getname_kernel(name);
  812. struct file *f = ERR_CAST(filename);
  813. if (!IS_ERR(filename)) {
  814. f = do_open_execat(AT_FDCWD, filename, 0);
  815. putname(filename);
  816. }
  817. return f;
  818. }
  819. EXPORT_SYMBOL(open_exec);
  820. #if defined(CONFIG_BINFMT_FLAT) || defined(CONFIG_BINFMT_ELF_FDPIC)
  821. ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
  822. {
  823. ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
  824. if (res > 0)
  825. flush_icache_user_range(addr, addr + len);
  826. return res;
  827. }
  828. EXPORT_SYMBOL(read_code);
  829. #endif
  830. /*
  831. * Maps the mm_struct mm into the current task struct.
  832. * On success, this function returns with exec_update_lock
  833. * held for writing.
  834. */
  835. static int exec_mmap(struct mm_struct *mm)
  836. {
  837. struct task_struct *tsk;
  838. struct mm_struct *old_mm, *active_mm;
  839. int ret;
  840. /* Notify parent that we're no longer interested in the old VM */
  841. tsk = current;
  842. old_mm = current->mm;
  843. exec_mm_release(tsk, old_mm);
  844. if (old_mm)
  845. sync_mm_rss(old_mm);
  846. ret = down_write_killable(&tsk->signal->exec_update_lock);
  847. if (ret)
  848. return ret;
  849. if (old_mm) {
  850. /*
  851. * If there is a pending fatal signal perhaps a signal
  852. * whose default action is to create a coredump get
  853. * out and die instead of going through with the exec.
  854. */
  855. ret = mmap_read_lock_killable(old_mm);
  856. if (ret) {
  857. up_write(&tsk->signal->exec_update_lock);
  858. return ret;
  859. }
  860. }
  861. task_lock(tsk);
  862. membarrier_exec_mmap(mm);
  863. local_irq_disable();
  864. active_mm = tsk->active_mm;
  865. tsk->active_mm = mm;
  866. tsk->mm = mm;
  867. /*
  868. * This prevents preemption while active_mm is being loaded and
  869. * it and mm are being updated, which could cause problems for
  870. * lazy tlb mm refcounting when these are updated by context
  871. * switches. Not all architectures can handle irqs off over
  872. * activate_mm yet.
  873. */
  874. if (!IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
  875. local_irq_enable();
  876. activate_mm(active_mm, mm);
  877. if (IS_ENABLED(CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM))
  878. local_irq_enable();
  879. lru_gen_add_mm(mm);
  880. task_unlock(tsk);
  881. lru_gen_use_mm(mm);
  882. #ifdef CONFIG_KDP_CRED
  883. if (kdp_enable)
  884. uh_call(UH_APP_KDP, SET_CRED_PGD, (u64)current_cred(), (u64)mm->pgd, 0, 0);
  885. #endif
  886. if (old_mm) {
  887. mmap_read_unlock(old_mm);
  888. BUG_ON(active_mm != old_mm);
  889. setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
  890. mm_update_next_owner(old_mm);
  891. mmput(old_mm);
  892. return 0;
  893. }
  894. mmdrop(active_mm);
  895. return 0;
  896. }
  897. static int de_thread(struct task_struct *tsk)
  898. {
  899. struct signal_struct *sig = tsk->signal;
  900. struct sighand_struct *oldsighand = tsk->sighand;
  901. spinlock_t *lock = &oldsighand->siglock;
  902. if (thread_group_empty(tsk))
  903. goto no_thread_group;
  904. /*
  905. * Kill all other threads in the thread group.
  906. */
  907. spin_lock_irq(lock);
  908. if ((sig->flags & SIGNAL_GROUP_EXIT) || sig->group_exec_task) {
  909. /*
  910. * Another group action in progress, just
  911. * return so that the signal is processed.
  912. */
  913. spin_unlock_irq(lock);
  914. return -EAGAIN;
  915. }
  916. sig->group_exec_task = tsk;
  917. sig->notify_count = zap_other_threads(tsk);
  918. if (!thread_group_leader(tsk))
  919. sig->notify_count--;
  920. while (sig->notify_count) {
  921. __set_current_state(TASK_KILLABLE);
  922. spin_unlock_irq(lock);
  923. schedule();
  924. if (__fatal_signal_pending(tsk))
  925. goto killed;
  926. spin_lock_irq(lock);
  927. }
  928. spin_unlock_irq(lock);
  929. /*
  930. * At this point all other threads have exited, all we have to
  931. * do is to wait for the thread group leader to become inactive,
  932. * and to assume its PID:
  933. */
  934. if (!thread_group_leader(tsk)) {
  935. struct task_struct *leader = tsk->group_leader;
  936. for (;;) {
  937. cgroup_threadgroup_change_begin(tsk);
  938. write_lock_irq(&tasklist_lock);
  939. /*
  940. * Do this under tasklist_lock to ensure that
  941. * exit_notify() can't miss ->group_exec_task
  942. */
  943. sig->notify_count = -1;
  944. if (likely(leader->exit_state))
  945. break;
  946. __set_current_state(TASK_KILLABLE);
  947. write_unlock_irq(&tasklist_lock);
  948. cgroup_threadgroup_change_end(tsk);
  949. schedule();
  950. if (__fatal_signal_pending(tsk))
  951. goto killed;
  952. }
  953. /*
  954. * The only record we have of the real-time age of a
  955. * process, regardless of execs it's done, is start_time.
  956. * All the past CPU time is accumulated in signal_struct
  957. * from sister threads now dead. But in this non-leader
  958. * exec, nothing survives from the original leader thread,
  959. * whose birth marks the true age of this process now.
  960. * When we take on its identity by switching to its PID, we
  961. * also take its birthdate (always earlier than our own).
  962. */
  963. tsk->start_time = leader->start_time;
  964. tsk->start_boottime = leader->start_boottime;
  965. BUG_ON(!same_thread_group(leader, tsk));
  966. /*
  967. * An exec() starts a new thread group with the
  968. * TGID of the previous thread group. Rehash the
  969. * two threads with a switched PID, and release
  970. * the former thread group leader:
  971. */
  972. /* Become a process group leader with the old leader's pid.
  973. * The old leader becomes a thread of the this thread group.
  974. */
  975. exchange_tids(tsk, leader);
  976. transfer_pid(leader, tsk, PIDTYPE_TGID);
  977. transfer_pid(leader, tsk, PIDTYPE_PGID);
  978. transfer_pid(leader, tsk, PIDTYPE_SID);
  979. list_replace_rcu(&leader->tasks, &tsk->tasks);
  980. list_replace_init(&leader->sibling, &tsk->sibling);
  981. tsk->group_leader = tsk;
  982. leader->group_leader = tsk;
  983. tsk->exit_signal = SIGCHLD;
  984. leader->exit_signal = -1;
  985. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  986. leader->exit_state = EXIT_DEAD;
  987. /*
  988. * We are going to release_task()->ptrace_unlink() silently,
  989. * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
  990. * the tracer won't block again waiting for this thread.
  991. */
  992. if (unlikely(leader->ptrace))
  993. __wake_up_parent(leader, leader->parent);
  994. write_unlock_irq(&tasklist_lock);
  995. cgroup_threadgroup_change_end(tsk);
  996. release_task(leader);
  997. }
  998. sig->group_exec_task = NULL;
  999. sig->notify_count = 0;
  1000. no_thread_group:
  1001. /* we have changed execution domain */
  1002. tsk->exit_signal = SIGCHLD;
  1003. BUG_ON(!thread_group_leader(tsk));
  1004. return 0;
  1005. killed:
  1006. /* protects against exit_notify() and __exit_signal() */
  1007. read_lock(&tasklist_lock);
  1008. sig->group_exec_task = NULL;
  1009. sig->notify_count = 0;
  1010. read_unlock(&tasklist_lock);
  1011. return -EAGAIN;
  1012. }
  1013. /*
  1014. * This function makes sure the current process has its own signal table,
  1015. * so that flush_signal_handlers can later reset the handlers without
  1016. * disturbing other processes. (Other processes might share the signal
  1017. * table via the CLONE_SIGHAND option to clone().)
  1018. */
  1019. static int unshare_sighand(struct task_struct *me)
  1020. {
  1021. struct sighand_struct *oldsighand = me->sighand;
  1022. if (refcount_read(&oldsighand->count) != 1) {
  1023. struct sighand_struct *newsighand;
  1024. /*
  1025. * This ->sighand is shared with the CLONE_SIGHAND
  1026. * but not CLONE_THREAD task, switch to the new one.
  1027. */
  1028. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  1029. if (!newsighand)
  1030. return -ENOMEM;
  1031. refcount_set(&newsighand->count, 1);
  1032. write_lock_irq(&tasklist_lock);
  1033. spin_lock(&oldsighand->siglock);
  1034. memcpy(newsighand->action, oldsighand->action,
  1035. sizeof(newsighand->action));
  1036. rcu_assign_pointer(me->sighand, newsighand);
  1037. spin_unlock(&oldsighand->siglock);
  1038. write_unlock_irq(&tasklist_lock);
  1039. __cleanup_sighand(oldsighand);
  1040. }
  1041. return 0;
  1042. }
  1043. char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk)
  1044. {
  1045. task_lock(tsk);
  1046. /* Always NUL terminated and zero-padded */
  1047. strscpy_pad(buf, tsk->comm, buf_size);
  1048. task_unlock(tsk);
  1049. return buf;
  1050. }
  1051. EXPORT_SYMBOL_GPL(__get_task_comm);
  1052. /*
  1053. * These functions flushes out all traces of the currently running executable
  1054. * so that a new one can be started
  1055. */
  1056. void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
  1057. {
  1058. task_lock(tsk);
  1059. trace_task_rename(tsk, buf);
  1060. strscpy_pad(tsk->comm, buf, sizeof(tsk->comm));
  1061. task_unlock(tsk);
  1062. perf_event_comm(tsk, exec);
  1063. }
  1064. /*
  1065. * Calling this is the point of no return. None of the failures will be
  1066. * seen by userspace since either the process is already taking a fatal
  1067. * signal (via de_thread() or coredump), or will have SEGV raised
  1068. * (after exec_mmap()) by search_binary_handler (see below).
  1069. */
  1070. int begin_new_exec(struct linux_binprm * bprm)
  1071. {
  1072. struct task_struct *me = current;
  1073. int retval;
  1074. /* Once we are committed compute the creds */
  1075. retval = bprm_creds_from_file(bprm);
  1076. if (retval)
  1077. return retval;
  1078. /*
  1079. * Ensure all future errors are fatal.
  1080. */
  1081. bprm->point_of_no_return = true;
  1082. /*
  1083. * Make this the only thread in the thread group.
  1084. */
  1085. retval = de_thread(me);
  1086. if (retval)
  1087. goto out;
  1088. /*
  1089. * Cancel any io_uring activity across execve
  1090. */
  1091. io_uring_task_cancel();
  1092. /* Ensure the files table is not shared. */
  1093. retval = unshare_files();
  1094. if (retval)
  1095. goto out;
  1096. /*
  1097. * Must be called _before_ exec_mmap() as bprm->mm is
  1098. * not visible until then. This also enables the update
  1099. * to be lockless.
  1100. */
  1101. retval = set_mm_exe_file(bprm->mm, bprm->file);
  1102. if (retval)
  1103. goto out;
  1104. /* If the binary is not readable then enforce mm->dumpable=0 */
  1105. would_dump(bprm, bprm->file);
  1106. if (bprm->have_execfd)
  1107. would_dump(bprm, bprm->executable);
  1108. /*
  1109. * Release all of the old mmap stuff
  1110. */
  1111. acct_arg_size(bprm, 0);
  1112. retval = exec_mmap(bprm->mm);
  1113. if (retval)
  1114. goto out;
  1115. bprm->mm = NULL;
  1116. #ifdef CONFIG_POSIX_TIMERS
  1117. spin_lock_irq(&me->sighand->siglock);
  1118. posix_cpu_timers_exit(me);
  1119. spin_unlock_irq(&me->sighand->siglock);
  1120. exit_itimers(me);
  1121. flush_itimer_signals();
  1122. #endif
  1123. /*
  1124. * Make the signal table private.
  1125. */
  1126. retval = unshare_sighand(me);
  1127. if (retval)
  1128. goto out_unlock;
  1129. me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC |
  1130. PF_NOFREEZE | PF_NO_SETAFFINITY);
  1131. flush_thread();
  1132. me->personality &= ~bprm->per_clear;
  1133. clear_syscall_work_syscall_user_dispatch(me);
  1134. /*
  1135. * We have to apply CLOEXEC before we change whether the process is
  1136. * dumpable (in setup_new_exec) to avoid a race with a process in userspace
  1137. * trying to access the should-be-closed file descriptors of a process
  1138. * undergoing exec(2).
  1139. */
  1140. do_close_on_exec(me->files);
  1141. if (bprm->secureexec) {
  1142. /* Make sure parent cannot signal privileged process. */
  1143. me->pdeath_signal = 0;
  1144. /*
  1145. * For secureexec, reset the stack limit to sane default to
  1146. * avoid bad behavior from the prior rlimits. This has to
  1147. * happen before arch_pick_mmap_layout(), which examines
  1148. * RLIMIT_STACK, but after the point of no return to avoid
  1149. * needing to clean up the change on failure.
  1150. */
  1151. if (bprm->rlim_stack.rlim_cur > _STK_LIM)
  1152. bprm->rlim_stack.rlim_cur = _STK_LIM;
  1153. }
  1154. me->sas_ss_sp = me->sas_ss_size = 0;
  1155. /*
  1156. * Figure out dumpability. Note that this checking only of current
  1157. * is wrong, but userspace depends on it. This should be testing
  1158. * bprm->secureexec instead.
  1159. */
  1160. if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP ||
  1161. !(uid_eq(current_euid(), current_uid()) &&
  1162. gid_eq(current_egid(), current_gid())))
  1163. set_dumpable(current->mm, suid_dumpable);
  1164. else
  1165. set_dumpable(current->mm, SUID_DUMP_USER);
  1166. perf_event_exec();
  1167. __set_task_comm(me, kbasename(bprm->filename), true);
  1168. /* An exec changes our domain. We are no longer part of the thread
  1169. group */
  1170. WRITE_ONCE(me->self_exec_id, me->self_exec_id + 1);
  1171. flush_signal_handlers(me, 0);
  1172. retval = set_cred_ucounts(bprm->cred);
  1173. if (retval < 0)
  1174. goto out_unlock;
  1175. /*
  1176. * install the new credentials for this executable
  1177. */
  1178. security_bprm_committing_creds(bprm);
  1179. commit_creds(bprm->cred);
  1180. bprm->cred = NULL;
  1181. /*
  1182. * Disable monitoring for regular users
  1183. * when executing setuid binaries. Must
  1184. * wait until new credentials are committed
  1185. * by commit_creds() above
  1186. */
  1187. if (get_dumpable(me->mm) != SUID_DUMP_USER)
  1188. perf_event_exit_task(me);
  1189. /*
  1190. * cred_guard_mutex must be held at least to this point to prevent
  1191. * ptrace_attach() from altering our determination of the task's
  1192. * credentials; any time after this it may be unlocked.
  1193. */
  1194. security_bprm_committed_creds(bprm);
  1195. /* Pass the opened binary to the interpreter. */
  1196. if (bprm->have_execfd) {
  1197. retval = get_unused_fd_flags(0);
  1198. if (retval < 0)
  1199. goto out_unlock;
  1200. fd_install(retval, bprm->executable);
  1201. bprm->executable = NULL;
  1202. bprm->execfd = retval;
  1203. }
  1204. return 0;
  1205. out_unlock:
  1206. up_write(&me->signal->exec_update_lock);
  1207. out:
  1208. return retval;
  1209. }
  1210. EXPORT_SYMBOL(begin_new_exec);
  1211. void would_dump(struct linux_binprm *bprm, struct file *file)
  1212. {
  1213. struct inode *inode = file_inode(file);
  1214. struct user_namespace *mnt_userns = file_mnt_user_ns(file);
  1215. if (inode_permission(mnt_userns, inode, MAY_READ) < 0) {
  1216. struct user_namespace *old, *user_ns;
  1217. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  1218. /* Ensure mm->user_ns contains the executable */
  1219. user_ns = old = bprm->mm->user_ns;
  1220. while ((user_ns != &init_user_ns) &&
  1221. !privileged_wrt_inode_uidgid(user_ns, mnt_userns, inode))
  1222. user_ns = user_ns->parent;
  1223. if (old != user_ns) {
  1224. bprm->mm->user_ns = get_user_ns(user_ns);
  1225. put_user_ns(old);
  1226. }
  1227. }
  1228. }
  1229. EXPORT_SYMBOL(would_dump);
  1230. void setup_new_exec(struct linux_binprm * bprm)
  1231. {
  1232. /* Setup things that can depend upon the personality */
  1233. struct task_struct *me = current;
  1234. arch_pick_mmap_layout(me->mm, &bprm->rlim_stack);
  1235. arch_setup_new_exec();
  1236. /* Set the new mm task size. We have to do that late because it may
  1237. * depend on TIF_32BIT which is only updated in flush_thread() on
  1238. * some architectures like powerpc
  1239. */
  1240. me->mm->task_size = TASK_SIZE;
  1241. up_write(&me->signal->exec_update_lock);
  1242. mutex_unlock(&me->signal->cred_guard_mutex);
  1243. }
  1244. EXPORT_SYMBOL(setup_new_exec);
  1245. /* Runs immediately before start_thread() takes over. */
  1246. void finalize_exec(struct linux_binprm *bprm)
  1247. {
  1248. /* Store any stack rlimit changes before starting thread. */
  1249. task_lock(current->group_leader);
  1250. current->signal->rlim[RLIMIT_STACK] = bprm->rlim_stack;
  1251. task_unlock(current->group_leader);
  1252. }
  1253. EXPORT_SYMBOL(finalize_exec);
  1254. /*
  1255. * Prepare credentials and lock ->cred_guard_mutex.
  1256. * setup_new_exec() commits the new creds and drops the lock.
  1257. * Or, if exec fails before, free_bprm() should release ->cred
  1258. * and unlock.
  1259. */
  1260. static int prepare_bprm_creds(struct linux_binprm *bprm)
  1261. {
  1262. if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
  1263. return -ERESTARTNOINTR;
  1264. bprm->cred = prepare_exec_creds();
  1265. if (likely(bprm->cred))
  1266. return 0;
  1267. mutex_unlock(&current->signal->cred_guard_mutex);
  1268. return -ENOMEM;
  1269. }
  1270. static void free_bprm(struct linux_binprm *bprm)
  1271. {
  1272. if (bprm->mm) {
  1273. acct_arg_size(bprm, 0);
  1274. mmput(bprm->mm);
  1275. }
  1276. free_arg_pages(bprm);
  1277. if (bprm->cred) {
  1278. mutex_unlock(&current->signal->cred_guard_mutex);
  1279. abort_creds(bprm->cred);
  1280. }
  1281. if (bprm->file) {
  1282. allow_write_access(bprm->file);
  1283. fput(bprm->file);
  1284. }
  1285. if (bprm->executable)
  1286. fput(bprm->executable);
  1287. /* If a binfmt changed the interp, free it. */
  1288. if (bprm->interp != bprm->filename)
  1289. kfree(bprm->interp);
  1290. kfree(bprm->fdpath);
  1291. kfree(bprm);
  1292. }
  1293. static struct linux_binprm *alloc_bprm(int fd, struct filename *filename)
  1294. {
  1295. struct linux_binprm *bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1296. int retval = -ENOMEM;
  1297. if (!bprm)
  1298. goto out;
  1299. if (fd == AT_FDCWD || filename->name[0] == '/') {
  1300. bprm->filename = filename->name;
  1301. } else {
  1302. if (filename->name[0] == '\0')
  1303. bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd);
  1304. else
  1305. bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s",
  1306. fd, filename->name);
  1307. if (!bprm->fdpath)
  1308. goto out_free;
  1309. bprm->filename = bprm->fdpath;
  1310. }
  1311. bprm->interp = bprm->filename;
  1312. retval = bprm_mm_init(bprm);
  1313. if (retval)
  1314. goto out_free;
  1315. return bprm;
  1316. out_free:
  1317. free_bprm(bprm);
  1318. out:
  1319. return ERR_PTR(retval);
  1320. }
  1321. int bprm_change_interp(const char *interp, struct linux_binprm *bprm)
  1322. {
  1323. /* If a binfmt changed the interp, free it first. */
  1324. if (bprm->interp != bprm->filename)
  1325. kfree(bprm->interp);
  1326. bprm->interp = kstrdup(interp, GFP_KERNEL);
  1327. if (!bprm->interp)
  1328. return -ENOMEM;
  1329. return 0;
  1330. }
  1331. EXPORT_SYMBOL(bprm_change_interp);
  1332. /*
  1333. * determine how safe it is to execute the proposed program
  1334. * - the caller must hold ->cred_guard_mutex to protect against
  1335. * PTRACE_ATTACH or seccomp thread-sync
  1336. */
  1337. static void check_unsafe_exec(struct linux_binprm *bprm)
  1338. {
  1339. struct task_struct *p = current, *t;
  1340. unsigned n_fs;
  1341. if (p->ptrace)
  1342. bprm->unsafe |= LSM_UNSAFE_PTRACE;
  1343. /*
  1344. * This isn't strictly necessary, but it makes it harder for LSMs to
  1345. * mess up.
  1346. */
  1347. if (task_no_new_privs(current))
  1348. bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
  1349. t = p;
  1350. n_fs = 1;
  1351. spin_lock(&p->fs->lock);
  1352. rcu_read_lock();
  1353. while_each_thread(p, t) {
  1354. if (t->fs == p->fs)
  1355. n_fs++;
  1356. }
  1357. rcu_read_unlock();
  1358. if (p->fs->users > n_fs)
  1359. bprm->unsafe |= LSM_UNSAFE_SHARE;
  1360. else
  1361. p->fs->in_exec = 1;
  1362. spin_unlock(&p->fs->lock);
  1363. }
  1364. static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file)
  1365. {
  1366. /* Handle suid and sgid on files */
  1367. struct user_namespace *mnt_userns;
  1368. struct inode *inode = file_inode(file);
  1369. unsigned int mode;
  1370. kuid_t uid;
  1371. kgid_t gid;
  1372. if (!mnt_may_suid(file->f_path.mnt))
  1373. return;
  1374. if (task_no_new_privs(current))
  1375. return;
  1376. mode = READ_ONCE(inode->i_mode);
  1377. if (!(mode & (S_ISUID|S_ISGID)))
  1378. return;
  1379. mnt_userns = file_mnt_user_ns(file);
  1380. /* Be careful if suid/sgid is set */
  1381. inode_lock(inode);
  1382. /* reload atomically mode/uid/gid now that lock held */
  1383. mode = inode->i_mode;
  1384. uid = i_uid_into_mnt(mnt_userns, inode);
  1385. gid = i_gid_into_mnt(mnt_userns, inode);
  1386. inode_unlock(inode);
  1387. /* We ignore suid/sgid if there are no mappings for them in the ns */
  1388. if (!kuid_has_mapping(bprm->cred->user_ns, uid) ||
  1389. !kgid_has_mapping(bprm->cred->user_ns, gid))
  1390. return;
  1391. if (mode & S_ISUID) {
  1392. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1393. bprm->cred->euid = uid;
  1394. }
  1395. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1396. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1397. bprm->cred->egid = gid;
  1398. }
  1399. }
  1400. /*
  1401. * Compute brpm->cred based upon the final binary.
  1402. */
  1403. static int bprm_creds_from_file(struct linux_binprm *bprm)
  1404. {
  1405. /* Compute creds based on which file? */
  1406. struct file *file = bprm->execfd_creds ? bprm->executable : bprm->file;
  1407. bprm_fill_uid(bprm, file);
  1408. return security_bprm_creds_from_file(bprm, file);
  1409. }
  1410. /*
  1411. * Fill the binprm structure from the inode.
  1412. * Read the first BINPRM_BUF_SIZE bytes
  1413. *
  1414. * This may be called multiple times for binary chains (scripts for example).
  1415. */
  1416. static int prepare_binprm(struct linux_binprm *bprm)
  1417. {
  1418. loff_t pos = 0;
  1419. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  1420. return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
  1421. }
  1422. /*
  1423. * Arguments are '\0' separated strings found at the location bprm->p
  1424. * points to; chop off the first by relocating brpm->p to right after
  1425. * the first '\0' encountered.
  1426. */
  1427. int remove_arg_zero(struct linux_binprm *bprm)
  1428. {
  1429. int ret = 0;
  1430. unsigned long offset;
  1431. char *kaddr;
  1432. struct page *page;
  1433. if (!bprm->argc)
  1434. return 0;
  1435. do {
  1436. offset = bprm->p & ~PAGE_MASK;
  1437. page = get_arg_page(bprm, bprm->p, 0);
  1438. if (!page) {
  1439. ret = -EFAULT;
  1440. goto out;
  1441. }
  1442. kaddr = kmap_local_page(page);
  1443. for (; offset < PAGE_SIZE && kaddr[offset];
  1444. offset++, bprm->p++)
  1445. ;
  1446. kunmap_local(kaddr);
  1447. put_arg_page(page);
  1448. } while (offset == PAGE_SIZE);
  1449. bprm->p++;
  1450. bprm->argc--;
  1451. ret = 0;
  1452. out:
  1453. return ret;
  1454. }
  1455. EXPORT_SYMBOL(remove_arg_zero);
  1456. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1457. /*
  1458. * cycle the list of binary formats handler, until one recognizes the image
  1459. */
  1460. static int search_binary_handler(struct linux_binprm *bprm)
  1461. {
  1462. bool need_retry = IS_ENABLED(CONFIG_MODULES);
  1463. struct linux_binfmt *fmt;
  1464. int retval;
  1465. retval = prepare_binprm(bprm);
  1466. if (retval < 0)
  1467. return retval;
  1468. retval = security_bprm_check(bprm);
  1469. if (retval)
  1470. return retval;
  1471. retval = -ENOENT;
  1472. retry:
  1473. read_lock(&binfmt_lock);
  1474. list_for_each_entry(fmt, &formats, lh) {
  1475. if (!try_module_get(fmt->module))
  1476. continue;
  1477. read_unlock(&binfmt_lock);
  1478. retval = fmt->load_binary(bprm);
  1479. read_lock(&binfmt_lock);
  1480. put_binfmt(fmt);
  1481. if (bprm->point_of_no_return || (retval != -ENOEXEC)) {
  1482. read_unlock(&binfmt_lock);
  1483. return retval;
  1484. }
  1485. }
  1486. read_unlock(&binfmt_lock);
  1487. if (need_retry) {
  1488. if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
  1489. printable(bprm->buf[2]) && printable(bprm->buf[3]))
  1490. return retval;
  1491. if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0)
  1492. return retval;
  1493. need_retry = false;
  1494. goto retry;
  1495. }
  1496. return retval;
  1497. }
  1498. static int exec_binprm(struct linux_binprm *bprm)
  1499. {
  1500. pid_t old_pid, old_vpid;
  1501. int ret, depth;
  1502. /* Need to fetch pid before load_binary changes it */
  1503. old_pid = current->pid;
  1504. rcu_read_lock();
  1505. old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
  1506. rcu_read_unlock();
  1507. /* This allows 4 levels of binfmt rewrites before failing hard. */
  1508. for (depth = 0;; depth++) {
  1509. struct file *exec;
  1510. if (depth > 5)
  1511. return -ELOOP;
  1512. ret = search_binary_handler(bprm);
  1513. if (ret < 0)
  1514. return ret;
  1515. if (!bprm->interpreter)
  1516. break;
  1517. exec = bprm->file;
  1518. bprm->file = bprm->interpreter;
  1519. bprm->interpreter = NULL;
  1520. allow_write_access(exec);
  1521. if (unlikely(bprm->have_execfd)) {
  1522. if (bprm->executable) {
  1523. fput(exec);
  1524. return -ENOEXEC;
  1525. }
  1526. bprm->executable = exec;
  1527. } else
  1528. fput(exec);
  1529. }
  1530. audit_bprm(bprm);
  1531. trace_sched_process_exec(current, old_pid, bprm);
  1532. ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
  1533. proc_exec_connector(current);
  1534. return 0;
  1535. }
  1536. /*
  1537. * sys_execve() executes a new program.
  1538. */
  1539. static int bprm_execve(struct linux_binprm *bprm,
  1540. int fd, struct filename *filename, int flags)
  1541. {
  1542. struct file *file;
  1543. int retval;
  1544. retval = prepare_bprm_creds(bprm);
  1545. if (retval)
  1546. return retval;
  1547. check_unsafe_exec(bprm);
  1548. current->in_execve = 1;
  1549. file = do_open_execat(fd, filename, flags);
  1550. retval = PTR_ERR(file);
  1551. if (IS_ERR(file))
  1552. goto out_unmark;
  1553. sched_exec();
  1554. bprm->file = file;
  1555. /*
  1556. * Record that a name derived from an O_CLOEXEC fd will be
  1557. * inaccessible after exec. This allows the code in exec to
  1558. * choose to fail when the executable is not mmaped into the
  1559. * interpreter and an open file descriptor is not passed to
  1560. * the interpreter. This makes for a better user experience
  1561. * than having the interpreter start and then immediately fail
  1562. * when it finds the executable is inaccessible.
  1563. */
  1564. if (bprm->fdpath && get_close_on_exec(fd))
  1565. bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
  1566. /* Set the unchanging part of bprm->cred */
  1567. retval = security_bprm_creds_for_exec(bprm);
  1568. if (retval)
  1569. goto out;
  1570. retval = exec_binprm(bprm);
  1571. if (retval < 0)
  1572. goto out;
  1573. /* execve succeeded */
  1574. current->fs->in_exec = 0;
  1575. current->in_execve = 0;
  1576. rseq_execve(current);
  1577. acct_update_integrals(current);
  1578. task_numa_free(current, false);
  1579. return retval;
  1580. out:
  1581. /*
  1582. * If past the point of no return ensure the code never
  1583. * returns to the userspace process. Use an existing fatal
  1584. * signal if present otherwise terminate the process with
  1585. * SIGSEGV.
  1586. */
  1587. if (bprm->point_of_no_return && !fatal_signal_pending(current))
  1588. force_fatal_sig(SIGSEGV);
  1589. out_unmark:
  1590. current->fs->in_exec = 0;
  1591. current->in_execve = 0;
  1592. return retval;
  1593. }
  1594. static int do_execveat_common(int fd, struct filename *filename,
  1595. struct user_arg_ptr argv,
  1596. struct user_arg_ptr envp,
  1597. int flags)
  1598. {
  1599. struct linux_binprm *bprm;
  1600. int retval;
  1601. if (IS_ERR(filename))
  1602. return PTR_ERR(filename);
  1603. /*
  1604. * We move the actual failure in case of RLIMIT_NPROC excess from
  1605. * set*uid() to execve() because too many poorly written programs
  1606. * don't check setuid() return code. Here we additionally recheck
  1607. * whether NPROC limit is still exceeded.
  1608. */
  1609. if ((current->flags & PF_NPROC_EXCEEDED) &&
  1610. is_rlimit_overlimit(current_ucounts(), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) {
  1611. retval = -EAGAIN;
  1612. goto out_ret;
  1613. }
  1614. /* We're below the limit (still or again), so we don't want to make
  1615. * further execve() calls fail. */
  1616. current->flags &= ~PF_NPROC_EXCEEDED;
  1617. bprm = alloc_bprm(fd, filename);
  1618. if (IS_ERR(bprm)) {
  1619. retval = PTR_ERR(bprm);
  1620. goto out_ret;
  1621. }
  1622. retval = count(argv, MAX_ARG_STRINGS);
  1623. if (retval == 0)
  1624. pr_warn_once("process '%s' launched '%s' with NULL argv: empty string added\n",
  1625. current->comm, bprm->filename);
  1626. if (retval < 0)
  1627. goto out_free;
  1628. bprm->argc = retval;
  1629. retval = count(envp, MAX_ARG_STRINGS);
  1630. if (retval < 0)
  1631. goto out_free;
  1632. bprm->envc = retval;
  1633. retval = bprm_stack_limits(bprm);
  1634. if (retval < 0)
  1635. goto out_free;
  1636. retval = copy_string_kernel(bprm->filename, bprm);
  1637. if (retval < 0)
  1638. goto out_free;
  1639. bprm->exec = bprm->p;
  1640. retval = copy_strings(bprm->envc, envp, bprm);
  1641. if (retval < 0)
  1642. goto out_free;
  1643. retval = copy_strings(bprm->argc, argv, bprm);
  1644. if (retval < 0)
  1645. goto out_free;
  1646. /*
  1647. * When argv is empty, add an empty string ("") as argv[0] to
  1648. * ensure confused userspace programs that start processing
  1649. * from argv[1] won't end up walking envp. See also
  1650. * bprm_stack_limits().
  1651. */
  1652. if (bprm->argc == 0) {
  1653. retval = copy_string_kernel("", bprm);
  1654. if (retval < 0)
  1655. goto out_free;
  1656. bprm->argc = 1;
  1657. }
  1658. retval = bprm_execve(bprm, fd, filename, flags);
  1659. out_free:
  1660. free_bprm(bprm);
  1661. out_ret:
  1662. putname(filename);
  1663. return retval;
  1664. }
  1665. int kernel_execve(const char *kernel_filename,
  1666. const char *const *argv, const char *const *envp)
  1667. {
  1668. struct filename *filename;
  1669. struct linux_binprm *bprm;
  1670. int fd = AT_FDCWD;
  1671. int retval;
  1672. /* It is non-sense for kernel threads to call execve */
  1673. if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
  1674. return -EINVAL;
  1675. filename = getname_kernel(kernel_filename);
  1676. if (IS_ERR(filename))
  1677. return PTR_ERR(filename);
  1678. bprm = alloc_bprm(fd, filename);
  1679. if (IS_ERR(bprm)) {
  1680. retval = PTR_ERR(bprm);
  1681. goto out_ret;
  1682. }
  1683. retval = count_strings_kernel(argv);
  1684. if (WARN_ON_ONCE(retval == 0))
  1685. retval = -EINVAL;
  1686. if (retval < 0)
  1687. goto out_free;
  1688. bprm->argc = retval;
  1689. retval = count_strings_kernel(envp);
  1690. if (retval < 0)
  1691. goto out_free;
  1692. bprm->envc = retval;
  1693. retval = bprm_stack_limits(bprm);
  1694. if (retval < 0)
  1695. goto out_free;
  1696. retval = copy_string_kernel(bprm->filename, bprm);
  1697. if (retval < 0)
  1698. goto out_free;
  1699. bprm->exec = bprm->p;
  1700. retval = copy_strings_kernel(bprm->envc, envp, bprm);
  1701. if (retval < 0)
  1702. goto out_free;
  1703. retval = copy_strings_kernel(bprm->argc, argv, bprm);
  1704. if (retval < 0)
  1705. goto out_free;
  1706. retval = bprm_execve(bprm, fd, filename, 0);
  1707. out_free:
  1708. free_bprm(bprm);
  1709. out_ret:
  1710. putname(filename);
  1711. return retval;
  1712. }
  1713. static int do_execve(struct filename *filename,
  1714. const char __user *const __user *__argv,
  1715. const char __user *const __user *__envp)
  1716. {
  1717. struct user_arg_ptr argv = { .ptr.native = __argv };
  1718. struct user_arg_ptr envp = { .ptr.native = __envp };
  1719. return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
  1720. }
  1721. static int do_execveat(int fd, struct filename *filename,
  1722. const char __user *const __user *__argv,
  1723. const char __user *const __user *__envp,
  1724. int flags)
  1725. {
  1726. struct user_arg_ptr argv = { .ptr.native = __argv };
  1727. struct user_arg_ptr envp = { .ptr.native = __envp };
  1728. return do_execveat_common(fd, filename, argv, envp, flags);
  1729. }
  1730. #ifdef CONFIG_COMPAT
  1731. static int compat_do_execve(struct filename *filename,
  1732. const compat_uptr_t __user *__argv,
  1733. const compat_uptr_t __user *__envp)
  1734. {
  1735. struct user_arg_ptr argv = {
  1736. .is_compat = true,
  1737. .ptr.compat = __argv,
  1738. };
  1739. struct user_arg_ptr envp = {
  1740. .is_compat = true,
  1741. .ptr.compat = __envp,
  1742. };
  1743. return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
  1744. }
  1745. static int compat_do_execveat(int fd, struct filename *filename,
  1746. const compat_uptr_t __user *__argv,
  1747. const compat_uptr_t __user *__envp,
  1748. int flags)
  1749. {
  1750. struct user_arg_ptr argv = {
  1751. .is_compat = true,
  1752. .ptr.compat = __argv,
  1753. };
  1754. struct user_arg_ptr envp = {
  1755. .is_compat = true,
  1756. .ptr.compat = __envp,
  1757. };
  1758. return do_execveat_common(fd, filename, argv, envp, flags);
  1759. }
  1760. #endif
  1761. void set_binfmt(struct linux_binfmt *new)
  1762. {
  1763. struct mm_struct *mm = current->mm;
  1764. if (mm->binfmt)
  1765. module_put(mm->binfmt->module);
  1766. mm->binfmt = new;
  1767. if (new)
  1768. __module_get(new->module);
  1769. }
  1770. EXPORT_SYMBOL(set_binfmt);
  1771. /*
  1772. * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
  1773. */
  1774. void set_dumpable(struct mm_struct *mm, int value)
  1775. {
  1776. if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
  1777. return;
  1778. set_mask_bits(&mm->flags, MMF_DUMPABLE_MASK, value);
  1779. }
  1780. SYSCALL_DEFINE3(execve,
  1781. const char __user *, filename,
  1782. const char __user *const __user *, argv,
  1783. const char __user *const __user *, envp)
  1784. {
  1785. #ifdef CONFIG_KDP_CRED
  1786. struct filename *path = getname(filename);
  1787. int error = PTR_ERR(path);
  1788. if (IS_ERR(path))
  1789. return error;
  1790. if (kdp_enable) {
  1791. uh_call(UH_APP_KDP, MARK_PPT, (u64)path->name, (u64)current, 0, 0);
  1792. if (current->cred->uid.val == 0 || current->cred->gid.val == 0 ||
  1793. current->cred->euid.val == 0 || current->cred->egid.val == 0 ||
  1794. current->cred->suid.val == 0 || current->cred->sgid.val == 0) {
  1795. if (kdp_restrict_fork(path)) {
  1796. pr_warn("RKP_KDP Restricted making process. PID = %d(%s) PPID = %d(%s)\n",
  1797. current->pid, current->comm,
  1798. current->parent->pid, current->parent->comm);
  1799. putname(path);
  1800. return -EACCES;
  1801. }
  1802. }
  1803. }
  1804. putname(path);
  1805. #endif
  1806. return do_execve(getname(filename), argv, envp);
  1807. }
  1808. SYSCALL_DEFINE5(execveat,
  1809. int, fd, const char __user *, filename,
  1810. const char __user *const __user *, argv,
  1811. const char __user *const __user *, envp,
  1812. int, flags)
  1813. {
  1814. return do_execveat(fd,
  1815. getname_uflags(filename, flags),
  1816. argv, envp, flags);
  1817. }
  1818. #ifdef CONFIG_COMPAT
  1819. COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename,
  1820. const compat_uptr_t __user *, argv,
  1821. const compat_uptr_t __user *, envp)
  1822. {
  1823. return compat_do_execve(getname(filename), argv, envp);
  1824. }
  1825. COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
  1826. const char __user *, filename,
  1827. const compat_uptr_t __user *, argv,
  1828. const compat_uptr_t __user *, envp,
  1829. int, flags)
  1830. {
  1831. return compat_do_execveat(fd,
  1832. getname_uflags(filename, flags),
  1833. argv, envp, flags);
  1834. }
  1835. #endif
  1836. #ifdef CONFIG_SYSCTL
  1837. static int proc_dointvec_minmax_coredump(struct ctl_table *table, int write,
  1838. void *buffer, size_t *lenp, loff_t *ppos)
  1839. {
  1840. int error = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  1841. if (!error)
  1842. validate_coredump_safety();
  1843. return error;
  1844. }
  1845. static struct ctl_table fs_exec_sysctls[] = {
  1846. {
  1847. .procname = "suid_dumpable",
  1848. .data = &suid_dumpable,
  1849. .maxlen = sizeof(int),
  1850. .mode = 0644,
  1851. .proc_handler = proc_dointvec_minmax_coredump,
  1852. .extra1 = SYSCTL_ZERO,
  1853. .extra2 = SYSCTL_TWO,
  1854. },
  1855. { }
  1856. };
  1857. static int __init init_fs_exec_sysctls(void)
  1858. {
  1859. register_sysctl_init("fs", fs_exec_sysctls);
  1860. return 0;
  1861. }
  1862. fs_initcall(init_fs_exec_sysctls);
  1863. #endif /* CONFIG_SYSCTL */