exec.c 40 KB

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