exec.c 41 KB

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