exec.c 46 KB

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