exec.c 39 KB

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