exec.c 47 KB

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