exec.c 43 KB

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