exec.c 42 KB

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