exec.c 45 KB

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