exec.c 41 KB

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