exec.c 47 KB

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