exec.c 39 KB

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