exec.c 46 KB

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