exec.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. #else
  648. /*
  649. * Transfer the program arguments and environment from the holding pages
  650. * onto the stack. The provided stack pointer is adjusted accordingly.
  651. */
  652. int transfer_args_to_stack(struct linux_binprm *bprm,
  653. unsigned long *sp_location)
  654. {
  655. unsigned long index, stop, sp;
  656. int ret = 0;
  657. stop = bprm->p >> PAGE_SHIFT;
  658. sp = *sp_location;
  659. for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
  660. unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0;
  661. char *src = kmap(bprm->page[index]) + offset;
  662. sp -= PAGE_SIZE - offset;
  663. if (copy_to_user((void *) sp, src, PAGE_SIZE - offset) != 0)
  664. ret = -EFAULT;
  665. kunmap(bprm->page[index]);
  666. if (ret)
  667. goto out;
  668. }
  669. *sp_location = sp;
  670. out:
  671. return ret;
  672. }
  673. EXPORT_SYMBOL(transfer_args_to_stack);
  674. #endif /* CONFIG_MMU */
  675. static struct file *do_open_execat(int fd, struct filename *name, int flags)
  676. {
  677. struct file *file;
  678. int err;
  679. struct open_flags open_exec_flags = {
  680. .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
  681. .acc_mode = MAY_EXEC,
  682. .intent = LOOKUP_OPEN,
  683. .lookup_flags = LOOKUP_FOLLOW,
  684. };
  685. if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  686. return ERR_PTR(-EINVAL);
  687. if (flags & AT_SYMLINK_NOFOLLOW)
  688. open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW;
  689. if (flags & AT_EMPTY_PATH)
  690. open_exec_flags.lookup_flags |= LOOKUP_EMPTY;
  691. file = do_filp_open(fd, name, &open_exec_flags);
  692. if (IS_ERR(file))
  693. goto out;
  694. err = -EACCES;
  695. if (!S_ISREG(file_inode(file)->i_mode))
  696. goto exit;
  697. if (path_noexec(&file->f_path))
  698. goto exit;
  699. err = deny_write_access(file);
  700. if (err)
  701. goto exit;
  702. if (name->name[0] != '\0')
  703. fsnotify_open(file);
  704. out:
  705. return file;
  706. exit:
  707. fput(file);
  708. return ERR_PTR(err);
  709. }
  710. struct file *open_exec(const char *name)
  711. {
  712. struct filename *filename = getname_kernel(name);
  713. struct file *f = ERR_CAST(filename);
  714. if (!IS_ERR(filename)) {
  715. f = do_open_execat(AT_FDCWD, filename, 0);
  716. putname(filename);
  717. }
  718. return f;
  719. }
  720. EXPORT_SYMBOL(open_exec);
  721. int kernel_read(struct file *file, loff_t offset,
  722. char *addr, unsigned long count)
  723. {
  724. mm_segment_t old_fs;
  725. loff_t pos = offset;
  726. int result;
  727. old_fs = get_fs();
  728. set_fs(get_ds());
  729. /* The cast to a user pointer is valid due to the set_fs() */
  730. result = vfs_read(file, (void __user *)addr, count, &pos);
  731. set_fs(old_fs);
  732. return result;
  733. }
  734. EXPORT_SYMBOL(kernel_read);
  735. int kernel_read_file(struct file *file, void **buf, loff_t *size,
  736. loff_t max_size, enum kernel_read_file_id id)
  737. {
  738. loff_t i_size, pos;
  739. ssize_t bytes = 0;
  740. int ret;
  741. if (!S_ISREG(file_inode(file)->i_mode) || max_size < 0)
  742. return -EINVAL;
  743. ret = security_kernel_read_file(file, id);
  744. if (ret)
  745. return ret;
  746. ret = deny_write_access(file);
  747. if (ret)
  748. return ret;
  749. i_size = i_size_read(file_inode(file));
  750. if (max_size > 0 && i_size > max_size) {
  751. ret = -EFBIG;
  752. goto out;
  753. }
  754. if (i_size <= 0) {
  755. ret = -EINVAL;
  756. goto out;
  757. }
  758. if (id != READING_FIRMWARE_PREALLOC_BUFFER)
  759. *buf = vmalloc(i_size);
  760. if (!*buf) {
  761. ret = -ENOMEM;
  762. goto out;
  763. }
  764. pos = 0;
  765. while (pos < i_size) {
  766. bytes = kernel_read(file, pos, (char *)(*buf) + pos,
  767. i_size - pos);
  768. if (bytes < 0) {
  769. ret = bytes;
  770. goto out;
  771. }
  772. if (bytes == 0)
  773. break;
  774. pos += bytes;
  775. }
  776. if (pos != i_size) {
  777. ret = -EIO;
  778. goto out_free;
  779. }
  780. ret = security_kernel_post_read_file(file, *buf, i_size, id);
  781. if (!ret)
  782. *size = pos;
  783. out_free:
  784. if (ret < 0) {
  785. if (id != READING_FIRMWARE_PREALLOC_BUFFER) {
  786. vfree(*buf);
  787. *buf = NULL;
  788. }
  789. }
  790. out:
  791. allow_write_access(file);
  792. return ret;
  793. }
  794. EXPORT_SYMBOL_GPL(kernel_read_file);
  795. int kernel_read_file_from_path(char *path, void **buf, loff_t *size,
  796. loff_t max_size, enum kernel_read_file_id id)
  797. {
  798. struct file *file;
  799. int ret;
  800. if (!path || !*path)
  801. return -EINVAL;
  802. file = filp_open(path, O_RDONLY, 0);
  803. if (IS_ERR(file))
  804. return PTR_ERR(file);
  805. ret = kernel_read_file(file, buf, size, max_size, id);
  806. fput(file);
  807. return ret;
  808. }
  809. EXPORT_SYMBOL_GPL(kernel_read_file_from_path);
  810. int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
  811. enum kernel_read_file_id id)
  812. {
  813. struct fd f = fdget(fd);
  814. int ret = -EBADF;
  815. if (!f.file)
  816. goto out;
  817. ret = kernel_read_file(f.file, buf, size, max_size, id);
  818. out:
  819. fdput(f);
  820. return ret;
  821. }
  822. EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);
  823. ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
  824. {
  825. ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
  826. if (res > 0)
  827. flush_icache_range(addr, addr + len);
  828. return res;
  829. }
  830. EXPORT_SYMBOL(read_code);
  831. static int exec_mmap(struct mm_struct *mm)
  832. {
  833. struct task_struct *tsk;
  834. struct mm_struct *old_mm, *active_mm;
  835. /* Notify parent that we're no longer interested in the old VM */
  836. tsk = current;
  837. old_mm = current->mm;
  838. mm_release(tsk, old_mm);
  839. if (old_mm) {
  840. sync_mm_rss(old_mm);
  841. /*
  842. * Make sure that if there is a core dump in progress
  843. * for the old mm, we get out and die instead of going
  844. * through with the exec. We must hold mmap_sem around
  845. * checking core_state and changing tsk->mm.
  846. */
  847. down_read(&old_mm->mmap_sem);
  848. if (unlikely(old_mm->core_state)) {
  849. up_read(&old_mm->mmap_sem);
  850. return -EINTR;
  851. }
  852. }
  853. task_lock(tsk);
  854. active_mm = tsk->active_mm;
  855. tsk->mm = mm;
  856. tsk->active_mm = mm;
  857. activate_mm(active_mm, mm);
  858. tsk->mm->vmacache_seqnum = 0;
  859. vmacache_flush(tsk);
  860. task_unlock(tsk);
  861. if (old_mm) {
  862. up_read(&old_mm->mmap_sem);
  863. BUG_ON(active_mm != old_mm);
  864. setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
  865. mm_update_next_owner(old_mm);
  866. mmput(old_mm);
  867. return 0;
  868. }
  869. mmdrop(active_mm);
  870. return 0;
  871. }
  872. /*
  873. * This function makes sure the current process has its own signal table,
  874. * so that flush_signal_handlers can later reset the handlers without
  875. * disturbing other processes. (Other processes might share the signal
  876. * table via the CLONE_SIGHAND option to clone().)
  877. */
  878. static int de_thread(struct task_struct *tsk)
  879. {
  880. struct signal_struct *sig = tsk->signal;
  881. struct sighand_struct *oldsighand = tsk->sighand;
  882. spinlock_t *lock = &oldsighand->siglock;
  883. if (thread_group_empty(tsk))
  884. goto no_thread_group;
  885. /*
  886. * Kill all other threads in the thread group.
  887. */
  888. spin_lock_irq(lock);
  889. if (signal_group_exit(sig)) {
  890. /*
  891. * Another group action in progress, just
  892. * return so that the signal is processed.
  893. */
  894. spin_unlock_irq(lock);
  895. return -EAGAIN;
  896. }
  897. sig->group_exit_task = tsk;
  898. sig->notify_count = zap_other_threads(tsk);
  899. if (!thread_group_leader(tsk))
  900. sig->notify_count--;
  901. while (sig->notify_count) {
  902. __set_current_state(TASK_KILLABLE);
  903. spin_unlock_irq(lock);
  904. schedule();
  905. if (unlikely(__fatal_signal_pending(tsk)))
  906. goto killed;
  907. spin_lock_irq(lock);
  908. }
  909. spin_unlock_irq(lock);
  910. /*
  911. * At this point all other threads have exited, all we have to
  912. * do is to wait for the thread group leader to become inactive,
  913. * and to assume its PID:
  914. */
  915. if (!thread_group_leader(tsk)) {
  916. struct task_struct *leader = tsk->group_leader;
  917. for (;;) {
  918. threadgroup_change_begin(tsk);
  919. write_lock_irq(&tasklist_lock);
  920. /*
  921. * Do this under tasklist_lock to ensure that
  922. * exit_notify() can't miss ->group_exit_task
  923. */
  924. sig->notify_count = -1;
  925. if (likely(leader->exit_state))
  926. break;
  927. __set_current_state(TASK_KILLABLE);
  928. write_unlock_irq(&tasklist_lock);
  929. threadgroup_change_end(tsk);
  930. schedule();
  931. if (unlikely(__fatal_signal_pending(tsk)))
  932. goto killed;
  933. }
  934. /*
  935. * The only record we have of the real-time age of a
  936. * process, regardless of execs it's done, is start_time.
  937. * All the past CPU time is accumulated in signal_struct
  938. * from sister threads now dead. But in this non-leader
  939. * exec, nothing survives from the original leader thread,
  940. * whose birth marks the true age of this process now.
  941. * When we take on its identity by switching to its PID, we
  942. * also take its birthdate (always earlier than our own).
  943. */
  944. tsk->start_time = leader->start_time;
  945. tsk->real_start_time = leader->real_start_time;
  946. BUG_ON(!same_thread_group(leader, tsk));
  947. BUG_ON(has_group_leader_pid(tsk));
  948. /*
  949. * An exec() starts a new thread group with the
  950. * TGID of the previous thread group. Rehash the
  951. * two threads with a switched PID, and release
  952. * the former thread group leader:
  953. */
  954. /* Become a process group leader with the old leader's pid.
  955. * The old leader becomes a thread of the this thread group.
  956. * Note: The old leader also uses this pid until release_task
  957. * is called. Odd but simple and correct.
  958. */
  959. tsk->pid = leader->pid;
  960. change_pid(tsk, PIDTYPE_PID, task_pid(leader));
  961. transfer_pid(leader, tsk, PIDTYPE_PGID);
  962. transfer_pid(leader, tsk, PIDTYPE_SID);
  963. list_replace_rcu(&leader->tasks, &tsk->tasks);
  964. list_replace_init(&leader->sibling, &tsk->sibling);
  965. tsk->group_leader = tsk;
  966. leader->group_leader = tsk;
  967. tsk->exit_signal = SIGCHLD;
  968. leader->exit_signal = -1;
  969. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  970. leader->exit_state = EXIT_DEAD;
  971. /*
  972. * We are going to release_task()->ptrace_unlink() silently,
  973. * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
  974. * the tracer wont't block again waiting for this thread.
  975. */
  976. if (unlikely(leader->ptrace))
  977. __wake_up_parent(leader, leader->parent);
  978. write_unlock_irq(&tasklist_lock);
  979. threadgroup_change_end(tsk);
  980. release_task(leader);
  981. }
  982. sig->group_exit_task = NULL;
  983. sig->notify_count = 0;
  984. no_thread_group:
  985. /* we have changed execution domain */
  986. tsk->exit_signal = SIGCHLD;
  987. exit_itimers(sig);
  988. flush_itimer_signals();
  989. if (atomic_read(&oldsighand->count) != 1) {
  990. struct sighand_struct *newsighand;
  991. /*
  992. * This ->sighand is shared with the CLONE_SIGHAND
  993. * but not CLONE_THREAD task, switch to the new one.
  994. */
  995. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  996. if (!newsighand)
  997. return -ENOMEM;
  998. atomic_set(&newsighand->count, 1);
  999. memcpy(newsighand->action, oldsighand->action,
  1000. sizeof(newsighand->action));
  1001. write_lock_irq(&tasklist_lock);
  1002. spin_lock(&oldsighand->siglock);
  1003. rcu_assign_pointer(tsk->sighand, newsighand);
  1004. spin_unlock(&oldsighand->siglock);
  1005. write_unlock_irq(&tasklist_lock);
  1006. __cleanup_sighand(oldsighand);
  1007. }
  1008. BUG_ON(!thread_group_leader(tsk));
  1009. return 0;
  1010. killed:
  1011. /* protects against exit_notify() and __exit_signal() */
  1012. read_lock(&tasklist_lock);
  1013. sig->group_exit_task = NULL;
  1014. sig->notify_count = 0;
  1015. read_unlock(&tasklist_lock);
  1016. return -EAGAIN;
  1017. }
  1018. char *get_task_comm(char *buf, struct task_struct *tsk)
  1019. {
  1020. /* buf must be at least sizeof(tsk->comm) in size */
  1021. task_lock(tsk);
  1022. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  1023. task_unlock(tsk);
  1024. return buf;
  1025. }
  1026. EXPORT_SYMBOL_GPL(get_task_comm);
  1027. /*
  1028. * These functions flushes out all traces of the currently running executable
  1029. * so that a new one can be started
  1030. */
  1031. void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
  1032. {
  1033. task_lock(tsk);
  1034. trace_task_rename(tsk, buf);
  1035. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  1036. task_unlock(tsk);
  1037. perf_event_comm(tsk, exec);
  1038. }
  1039. int flush_old_exec(struct linux_binprm * bprm)
  1040. {
  1041. int retval;
  1042. /*
  1043. * Make sure we have a private signal table and that
  1044. * we are unassociated from the previous thread group.
  1045. */
  1046. retval = de_thread(current);
  1047. if (retval)
  1048. goto out;
  1049. /*
  1050. * Must be called _before_ exec_mmap() as bprm->mm is
  1051. * not visibile until then. This also enables the update
  1052. * to be lockless.
  1053. */
  1054. set_mm_exe_file(bprm->mm, bprm->file);
  1055. /*
  1056. * Release all of the old mmap stuff
  1057. */
  1058. acct_arg_size(bprm, 0);
  1059. retval = exec_mmap(bprm->mm);
  1060. if (retval)
  1061. goto out;
  1062. bprm->mm = NULL; /* We're using it now */
  1063. set_fs(USER_DS);
  1064. current->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD |
  1065. PF_NOFREEZE | PF_NO_SETAFFINITY);
  1066. flush_thread();
  1067. current->personality &= ~bprm->per_clear;
  1068. return 0;
  1069. out:
  1070. return retval;
  1071. }
  1072. EXPORT_SYMBOL(flush_old_exec);
  1073. void would_dump(struct linux_binprm *bprm, struct file *file)
  1074. {
  1075. if (inode_permission(file_inode(file), MAY_READ) < 0)
  1076. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  1077. }
  1078. EXPORT_SYMBOL(would_dump);
  1079. void setup_new_exec(struct linux_binprm * bprm)
  1080. {
  1081. arch_pick_mmap_layout(current->mm);
  1082. /* This is the point of no return */
  1083. current->sas_ss_sp = current->sas_ss_size = 0;
  1084. if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
  1085. set_dumpable(current->mm, SUID_DUMP_USER);
  1086. else
  1087. set_dumpable(current->mm, suid_dumpable);
  1088. perf_event_exec();
  1089. __set_task_comm(current, kbasename(bprm->filename), true);
  1090. /* Set the new mm task size. We have to do that late because it may
  1091. * depend on TIF_32BIT which is only updated in flush_thread() on
  1092. * some architectures like powerpc
  1093. */
  1094. current->mm->task_size = TASK_SIZE;
  1095. /* install the new credentials */
  1096. if (!uid_eq(bprm->cred->uid, current_euid()) ||
  1097. !gid_eq(bprm->cred->gid, current_egid())) {
  1098. current->pdeath_signal = 0;
  1099. } else {
  1100. would_dump(bprm, bprm->file);
  1101. if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
  1102. set_dumpable(current->mm, suid_dumpable);
  1103. }
  1104. /* An exec changes our domain. We are no longer part of the thread
  1105. group */
  1106. current->self_exec_id++;
  1107. flush_signal_handlers(current, 0);
  1108. do_close_on_exec(current->files);
  1109. }
  1110. EXPORT_SYMBOL(setup_new_exec);
  1111. /*
  1112. * Prepare credentials and lock ->cred_guard_mutex.
  1113. * install_exec_creds() commits the new creds and drops the lock.
  1114. * Or, if exec fails before, free_bprm() should release ->cred and
  1115. * and unlock.
  1116. */
  1117. int prepare_bprm_creds(struct linux_binprm *bprm)
  1118. {
  1119. if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
  1120. return -ERESTARTNOINTR;
  1121. bprm->cred = prepare_exec_creds();
  1122. if (likely(bprm->cred))
  1123. return 0;
  1124. mutex_unlock(&current->signal->cred_guard_mutex);
  1125. return -ENOMEM;
  1126. }
  1127. static void free_bprm(struct linux_binprm *bprm)
  1128. {
  1129. free_arg_pages(bprm);
  1130. if (bprm->cred) {
  1131. mutex_unlock(&current->signal->cred_guard_mutex);
  1132. abort_creds(bprm->cred);
  1133. }
  1134. if (bprm->file) {
  1135. allow_write_access(bprm->file);
  1136. fput(bprm->file);
  1137. }
  1138. /* If a binfmt changed the interp, free it. */
  1139. if (bprm->interp != bprm->filename)
  1140. kfree(bprm->interp);
  1141. kfree(bprm);
  1142. }
  1143. int bprm_change_interp(char *interp, struct linux_binprm *bprm)
  1144. {
  1145. /* If a binfmt changed the interp, free it first. */
  1146. if (bprm->interp != bprm->filename)
  1147. kfree(bprm->interp);
  1148. bprm->interp = kstrdup(interp, GFP_KERNEL);
  1149. if (!bprm->interp)
  1150. return -ENOMEM;
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL(bprm_change_interp);
  1154. /*
  1155. * install the new credentials for this executable
  1156. */
  1157. void install_exec_creds(struct linux_binprm *bprm)
  1158. {
  1159. security_bprm_committing_creds(bprm);
  1160. commit_creds(bprm->cred);
  1161. bprm->cred = NULL;
  1162. /*
  1163. * Disable monitoring for regular users
  1164. * when executing setuid binaries. Must
  1165. * wait until new credentials are committed
  1166. * by commit_creds() above
  1167. */
  1168. if (get_dumpable(current->mm) != SUID_DUMP_USER)
  1169. perf_event_exit_task(current);
  1170. /*
  1171. * cred_guard_mutex must be held at least to this point to prevent
  1172. * ptrace_attach() from altering our determination of the task's
  1173. * credentials; any time after this it may be unlocked.
  1174. */
  1175. security_bprm_committed_creds(bprm);
  1176. mutex_unlock(&current->signal->cred_guard_mutex);
  1177. }
  1178. EXPORT_SYMBOL(install_exec_creds);
  1179. /*
  1180. * determine how safe it is to execute the proposed program
  1181. * - the caller must hold ->cred_guard_mutex to protect against
  1182. * PTRACE_ATTACH or seccomp thread-sync
  1183. */
  1184. static void check_unsafe_exec(struct linux_binprm *bprm)
  1185. {
  1186. struct task_struct *p = current, *t;
  1187. unsigned n_fs;
  1188. if (p->ptrace) {
  1189. if (p->ptrace & PT_PTRACE_CAP)
  1190. bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
  1191. else
  1192. bprm->unsafe |= LSM_UNSAFE_PTRACE;
  1193. }
  1194. /*
  1195. * This isn't strictly necessary, but it makes it harder for LSMs to
  1196. * mess up.
  1197. */
  1198. if (task_no_new_privs(current))
  1199. bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
  1200. t = p;
  1201. n_fs = 1;
  1202. spin_lock(&p->fs->lock);
  1203. rcu_read_lock();
  1204. while_each_thread(p, t) {
  1205. if (t->fs == p->fs)
  1206. n_fs++;
  1207. }
  1208. rcu_read_unlock();
  1209. if (p->fs->users > n_fs)
  1210. bprm->unsafe |= LSM_UNSAFE_SHARE;
  1211. else
  1212. p->fs->in_exec = 1;
  1213. spin_unlock(&p->fs->lock);
  1214. }
  1215. static void bprm_fill_uid(struct linux_binprm *bprm)
  1216. {
  1217. struct inode *inode;
  1218. unsigned int mode;
  1219. kuid_t uid;
  1220. kgid_t gid;
  1221. /*
  1222. * Since this can be called multiple times (via prepare_binprm),
  1223. * we must clear any previous work done when setting set[ug]id
  1224. * bits from any earlier bprm->file uses (for example when run
  1225. * first for a setuid script then again for its interpreter).
  1226. */
  1227. bprm->cred->euid = current_euid();
  1228. bprm->cred->egid = current_egid();
  1229. if (!mnt_may_suid(bprm->file->f_path.mnt))
  1230. return;
  1231. if (task_no_new_privs(current))
  1232. return;
  1233. inode = file_inode(bprm->file);
  1234. mode = READ_ONCE(inode->i_mode);
  1235. if (!(mode & (S_ISUID|S_ISGID)))
  1236. return;
  1237. /* Be careful if suid/sgid is set */
  1238. inode_lock(inode);
  1239. /* reload atomically mode/uid/gid now that lock held */
  1240. mode = inode->i_mode;
  1241. uid = inode->i_uid;
  1242. gid = inode->i_gid;
  1243. inode_unlock(inode);
  1244. /* We ignore suid/sgid if there are no mappings for them in the ns */
  1245. if (!kuid_has_mapping(bprm->cred->user_ns, uid) ||
  1246. !kgid_has_mapping(bprm->cred->user_ns, gid))
  1247. return;
  1248. if (mode & S_ISUID) {
  1249. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1250. bprm->cred->euid = uid;
  1251. }
  1252. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1253. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1254. bprm->cred->egid = gid;
  1255. }
  1256. }
  1257. /*
  1258. * Fill the binprm structure from the inode.
  1259. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  1260. *
  1261. * This may be called multiple times for binary chains (scripts for example).
  1262. */
  1263. int prepare_binprm(struct linux_binprm *bprm)
  1264. {
  1265. int retval;
  1266. bprm_fill_uid(bprm);
  1267. /* fill in binprm security blob */
  1268. retval = security_bprm_set_creds(bprm);
  1269. if (retval)
  1270. return retval;
  1271. bprm->cred_prepared = 1;
  1272. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  1273. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  1274. }
  1275. EXPORT_SYMBOL(prepare_binprm);
  1276. /*
  1277. * Arguments are '\0' separated strings found at the location bprm->p
  1278. * points to; chop off the first by relocating brpm->p to right after
  1279. * the first '\0' encountered.
  1280. */
  1281. int remove_arg_zero(struct linux_binprm *bprm)
  1282. {
  1283. int ret = 0;
  1284. unsigned long offset;
  1285. char *kaddr;
  1286. struct page *page;
  1287. if (!bprm->argc)
  1288. return 0;
  1289. do {
  1290. offset = bprm->p & ~PAGE_MASK;
  1291. page = get_arg_page(bprm, bprm->p, 0);
  1292. if (!page) {
  1293. ret = -EFAULT;
  1294. goto out;
  1295. }
  1296. kaddr = kmap_atomic(page);
  1297. for (; offset < PAGE_SIZE && kaddr[offset];
  1298. offset++, bprm->p++)
  1299. ;
  1300. kunmap_atomic(kaddr);
  1301. put_arg_page(page);
  1302. } while (offset == PAGE_SIZE);
  1303. bprm->p++;
  1304. bprm->argc--;
  1305. ret = 0;
  1306. out:
  1307. return ret;
  1308. }
  1309. EXPORT_SYMBOL(remove_arg_zero);
  1310. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1311. /*
  1312. * cycle the list of binary formats handler, until one recognizes the image
  1313. */
  1314. int search_binary_handler(struct linux_binprm *bprm)
  1315. {
  1316. bool need_retry = IS_ENABLED(CONFIG_MODULES);
  1317. struct linux_binfmt *fmt;
  1318. int retval;
  1319. /* This allows 4 levels of binfmt rewrites before failing hard. */
  1320. if (bprm->recursion_depth > 5)
  1321. return -ELOOP;
  1322. retval = security_bprm_check(bprm);
  1323. if (retval)
  1324. return retval;
  1325. retval = -ENOENT;
  1326. retry:
  1327. read_lock(&binfmt_lock);
  1328. list_for_each_entry(fmt, &formats, lh) {
  1329. if (!try_module_get(fmt->module))
  1330. continue;
  1331. read_unlock(&binfmt_lock);
  1332. bprm->recursion_depth++;
  1333. retval = fmt->load_binary(bprm);
  1334. read_lock(&binfmt_lock);
  1335. put_binfmt(fmt);
  1336. bprm->recursion_depth--;
  1337. if (retval < 0 && !bprm->mm) {
  1338. /* we got to flush_old_exec() and failed after it */
  1339. read_unlock(&binfmt_lock);
  1340. force_sigsegv(SIGSEGV, current);
  1341. return retval;
  1342. }
  1343. if (retval != -ENOEXEC || !bprm->file) {
  1344. read_unlock(&binfmt_lock);
  1345. return retval;
  1346. }
  1347. }
  1348. read_unlock(&binfmt_lock);
  1349. if (need_retry) {
  1350. if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
  1351. printable(bprm->buf[2]) && printable(bprm->buf[3]))
  1352. return retval;
  1353. if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0)
  1354. return retval;
  1355. need_retry = false;
  1356. goto retry;
  1357. }
  1358. return retval;
  1359. }
  1360. EXPORT_SYMBOL(search_binary_handler);
  1361. static int exec_binprm(struct linux_binprm *bprm)
  1362. {
  1363. pid_t old_pid, old_vpid;
  1364. int ret;
  1365. /* Need to fetch pid before load_binary changes it */
  1366. old_pid = current->pid;
  1367. rcu_read_lock();
  1368. old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
  1369. rcu_read_unlock();
  1370. ret = search_binary_handler(bprm);
  1371. if (ret >= 0) {
  1372. audit_bprm(bprm);
  1373. trace_sched_process_exec(current, old_pid, bprm);
  1374. ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
  1375. proc_exec_connector(current);
  1376. }
  1377. return ret;
  1378. }
  1379. /*
  1380. * sys_execve() executes a new program.
  1381. */
  1382. static int do_execveat_common(int fd, struct filename *filename,
  1383. struct user_arg_ptr argv,
  1384. struct user_arg_ptr envp,
  1385. int flags)
  1386. {
  1387. char *pathbuf = NULL;
  1388. struct linux_binprm *bprm;
  1389. struct file *file;
  1390. struct files_struct *displaced;
  1391. int retval;
  1392. if (IS_ERR(filename))
  1393. return PTR_ERR(filename);
  1394. /*
  1395. * We move the actual failure in case of RLIMIT_NPROC excess from
  1396. * set*uid() to execve() because too many poorly written programs
  1397. * don't check setuid() return code. Here we additionally recheck
  1398. * whether NPROC limit is still exceeded.
  1399. */
  1400. if ((current->flags & PF_NPROC_EXCEEDED) &&
  1401. atomic_read(&current_user()->processes) > rlimit(RLIMIT_NPROC)) {
  1402. retval = -EAGAIN;
  1403. goto out_ret;
  1404. }
  1405. /* We're below the limit (still or again), so we don't want to make
  1406. * further execve() calls fail. */
  1407. current->flags &= ~PF_NPROC_EXCEEDED;
  1408. retval = unshare_files(&displaced);
  1409. if (retval)
  1410. goto out_ret;
  1411. retval = -ENOMEM;
  1412. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1413. if (!bprm)
  1414. goto out_files;
  1415. retval = prepare_bprm_creds(bprm);
  1416. if (retval)
  1417. goto out_free;
  1418. check_unsafe_exec(bprm);
  1419. current->in_execve = 1;
  1420. file = do_open_execat(fd, filename, flags);
  1421. retval = PTR_ERR(file);
  1422. if (IS_ERR(file))
  1423. goto out_unmark;
  1424. sched_exec();
  1425. bprm->file = file;
  1426. if (fd == AT_FDCWD || filename->name[0] == '/') {
  1427. bprm->filename = filename->name;
  1428. } else {
  1429. if (filename->name[0] == '\0')
  1430. pathbuf = kasprintf(GFP_TEMPORARY, "/dev/fd/%d", fd);
  1431. else
  1432. pathbuf = kasprintf(GFP_TEMPORARY, "/dev/fd/%d/%s",
  1433. fd, filename->name);
  1434. if (!pathbuf) {
  1435. retval = -ENOMEM;
  1436. goto out_unmark;
  1437. }
  1438. /*
  1439. * Record that a name derived from an O_CLOEXEC fd will be
  1440. * inaccessible after exec. Relies on having exclusive access to
  1441. * current->files (due to unshare_files above).
  1442. */
  1443. if (close_on_exec(fd, rcu_dereference_raw(current->files->fdt)))
  1444. bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
  1445. bprm->filename = pathbuf;
  1446. }
  1447. bprm->interp = bprm->filename;
  1448. retval = bprm_mm_init(bprm);
  1449. if (retval)
  1450. goto out_unmark;
  1451. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1452. if ((retval = bprm->argc) < 0)
  1453. goto out;
  1454. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1455. if ((retval = bprm->envc) < 0)
  1456. goto out;
  1457. retval = prepare_binprm(bprm);
  1458. if (retval < 0)
  1459. goto out;
  1460. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1461. if (retval < 0)
  1462. goto out;
  1463. bprm->exec = bprm->p;
  1464. retval = copy_strings(bprm->envc, envp, bprm);
  1465. if (retval < 0)
  1466. goto out;
  1467. retval = copy_strings(bprm->argc, argv, bprm);
  1468. if (retval < 0)
  1469. goto out;
  1470. retval = exec_binprm(bprm);
  1471. if (retval < 0)
  1472. goto out;
  1473. /* execve succeeded */
  1474. current->fs->in_exec = 0;
  1475. current->in_execve = 0;
  1476. acct_update_integrals(current);
  1477. task_numa_free(current);
  1478. free_bprm(bprm);
  1479. kfree(pathbuf);
  1480. putname(filename);
  1481. if (displaced)
  1482. put_files_struct(displaced);
  1483. return retval;
  1484. out:
  1485. if (bprm->mm) {
  1486. acct_arg_size(bprm, 0);
  1487. mmput(bprm->mm);
  1488. }
  1489. out_unmark:
  1490. current->fs->in_exec = 0;
  1491. current->in_execve = 0;
  1492. out_free:
  1493. free_bprm(bprm);
  1494. kfree(pathbuf);
  1495. out_files:
  1496. if (displaced)
  1497. reset_files_struct(displaced);
  1498. out_ret:
  1499. putname(filename);
  1500. return retval;
  1501. }
  1502. int do_execve(struct filename *filename,
  1503. const char __user *const __user *__argv,
  1504. const char __user *const __user *__envp)
  1505. {
  1506. struct user_arg_ptr argv = { .ptr.native = __argv };
  1507. struct user_arg_ptr envp = { .ptr.native = __envp };
  1508. return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
  1509. }
  1510. int do_execveat(int fd, struct filename *filename,
  1511. const char __user *const __user *__argv,
  1512. const char __user *const __user *__envp,
  1513. int flags)
  1514. {
  1515. struct user_arg_ptr argv = { .ptr.native = __argv };
  1516. struct user_arg_ptr envp = { .ptr.native = __envp };
  1517. return do_execveat_common(fd, filename, argv, envp, flags);
  1518. }
  1519. #ifdef CONFIG_COMPAT
  1520. static int compat_do_execve(struct filename *filename,
  1521. const compat_uptr_t __user *__argv,
  1522. const compat_uptr_t __user *__envp)
  1523. {
  1524. struct user_arg_ptr argv = {
  1525. .is_compat = true,
  1526. .ptr.compat = __argv,
  1527. };
  1528. struct user_arg_ptr envp = {
  1529. .is_compat = true,
  1530. .ptr.compat = __envp,
  1531. };
  1532. return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
  1533. }
  1534. static int compat_do_execveat(int fd, struct filename *filename,
  1535. const compat_uptr_t __user *__argv,
  1536. const compat_uptr_t __user *__envp,
  1537. int flags)
  1538. {
  1539. struct user_arg_ptr argv = {
  1540. .is_compat = true,
  1541. .ptr.compat = __argv,
  1542. };
  1543. struct user_arg_ptr envp = {
  1544. .is_compat = true,
  1545. .ptr.compat = __envp,
  1546. };
  1547. return do_execveat_common(fd, filename, argv, envp, flags);
  1548. }
  1549. #endif
  1550. void set_binfmt(struct linux_binfmt *new)
  1551. {
  1552. struct mm_struct *mm = current->mm;
  1553. if (mm->binfmt)
  1554. module_put(mm->binfmt->module);
  1555. mm->binfmt = new;
  1556. if (new)
  1557. __module_get(new->module);
  1558. }
  1559. EXPORT_SYMBOL(set_binfmt);
  1560. /*
  1561. * set_dumpable stores three-value SUID_DUMP_* into mm->flags.
  1562. */
  1563. void set_dumpable(struct mm_struct *mm, int value)
  1564. {
  1565. unsigned long old, new;
  1566. if (WARN_ON((unsigned)value > SUID_DUMP_ROOT))
  1567. return;
  1568. do {
  1569. old = ACCESS_ONCE(mm->flags);
  1570. new = (old & ~MMF_DUMPABLE_MASK) | value;
  1571. } while (cmpxchg(&mm->flags, old, new) != old);
  1572. }
  1573. SYSCALL_DEFINE3(execve,
  1574. const char __user *, filename,
  1575. const char __user *const __user *, argv,
  1576. const char __user *const __user *, envp)
  1577. {
  1578. return do_execve(getname(filename), argv, envp);
  1579. }
  1580. SYSCALL_DEFINE5(execveat,
  1581. int, fd, const char __user *, filename,
  1582. const char __user *const __user *, argv,
  1583. const char __user *const __user *, envp,
  1584. int, flags)
  1585. {
  1586. int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
  1587. return do_execveat(fd,
  1588. getname_flags(filename, lookup_flags, NULL),
  1589. argv, envp, flags);
  1590. }
  1591. #ifdef CONFIG_COMPAT
  1592. COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename,
  1593. const compat_uptr_t __user *, argv,
  1594. const compat_uptr_t __user *, envp)
  1595. {
  1596. return compat_do_execve(getname(filename), argv, envp);
  1597. }
  1598. COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
  1599. const char __user *, filename,
  1600. const compat_uptr_t __user *, argv,
  1601. const compat_uptr_t __user *, envp,
  1602. int, flags)
  1603. {
  1604. int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
  1605. return compat_do_execveat(fd,
  1606. getname_flags(filename, lookup_flags, NULL),
  1607. argv, envp, flags);
  1608. }
  1609. #endif