exec.c 49 KB

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