exec.c 44 KB

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