exec.c 53 KB

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