exec.c 39 KB

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