exec.c 44 KB

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