exec.c 39 KB

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