exec.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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/smp_lock.h>
  30. #include <linux/swap.h>
  31. #include <linux/string.h>
  32. #include <linux/init.h>
  33. #include <linux/pagemap.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/proc_fs.h>
  44. #include <linux/mount.h>
  45. #include <linux/security.h>
  46. #include <linux/ima.h>
  47. #include <linux/syscalls.h>
  48. #include <linux/tsacct_kern.h>
  49. #include <linux/cn_proc.h>
  50. #include <linux/audit.h>
  51. #include <linux/tracehook.h>
  52. #include <linux/kmod.h>
  53. #include <linux/fsnotify.h>
  54. #include <asm/uaccess.h>
  55. #include <asm/mmu_context.h>
  56. #include <asm/tlb.h>
  57. #include "internal.h"
  58. int core_uses_pid;
  59. char core_pattern[CORENAME_MAX_SIZE] = "core";
  60. int suid_dumpable = 0;
  61. /* The maximal length of core_pattern is also specified in sysctl.c */
  62. static LIST_HEAD(formats);
  63. static DEFINE_RWLOCK(binfmt_lock);
  64. int register_binfmt(struct linux_binfmt * fmt)
  65. {
  66. if (!fmt)
  67. return -EINVAL;
  68. write_lock(&binfmt_lock);
  69. list_add(&fmt->lh, &formats);
  70. write_unlock(&binfmt_lock);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(register_binfmt);
  74. void unregister_binfmt(struct linux_binfmt * fmt)
  75. {
  76. write_lock(&binfmt_lock);
  77. list_del(&fmt->lh);
  78. write_unlock(&binfmt_lock);
  79. }
  80. EXPORT_SYMBOL(unregister_binfmt);
  81. static inline void put_binfmt(struct linux_binfmt * fmt)
  82. {
  83. module_put(fmt->module);
  84. }
  85. /*
  86. * Note that a shared library must be both readable and executable due to
  87. * security reasons.
  88. *
  89. * Also note that we take the address to load from from the file itself.
  90. */
  91. SYSCALL_DEFINE1(uselib, const char __user *, library)
  92. {
  93. struct file *file;
  94. struct nameidata nd;
  95. char *tmp = getname(library);
  96. int error = PTR_ERR(tmp);
  97. if (!IS_ERR(tmp)) {
  98. error = path_lookup_open(AT_FDCWD, tmp,
  99. LOOKUP_FOLLOW, &nd,
  100. FMODE_READ|FMODE_EXEC);
  101. putname(tmp);
  102. }
  103. if (error)
  104. goto out;
  105. error = -EINVAL;
  106. if (!S_ISREG(nd.path.dentry->d_inode->i_mode))
  107. goto exit;
  108. error = -EACCES;
  109. if (nd.path.mnt->mnt_flags & MNT_NOEXEC)
  110. goto exit;
  111. error = inode_permission(nd.path.dentry->d_inode,
  112. MAY_READ | MAY_EXEC | MAY_OPEN);
  113. if (error)
  114. goto exit;
  115. error = ima_path_check(&nd.path, MAY_READ | MAY_EXEC | MAY_OPEN);
  116. if (error)
  117. goto exit;
  118. file = nameidata_to_filp(&nd, O_RDONLY|O_LARGEFILE);
  119. error = PTR_ERR(file);
  120. if (IS_ERR(file))
  121. goto out;
  122. fsnotify_open(file->f_path.dentry);
  123. error = -ENOEXEC;
  124. if(file->f_op) {
  125. struct linux_binfmt * fmt;
  126. read_lock(&binfmt_lock);
  127. list_for_each_entry(fmt, &formats, lh) {
  128. if (!fmt->load_shlib)
  129. continue;
  130. if (!try_module_get(fmt->module))
  131. continue;
  132. read_unlock(&binfmt_lock);
  133. error = fmt->load_shlib(file);
  134. read_lock(&binfmt_lock);
  135. put_binfmt(fmt);
  136. if (error != -ENOEXEC)
  137. break;
  138. }
  139. read_unlock(&binfmt_lock);
  140. }
  141. fput(file);
  142. out:
  143. return error;
  144. exit:
  145. release_open_intent(&nd);
  146. path_put(&nd.path);
  147. goto out;
  148. }
  149. #ifdef CONFIG_MMU
  150. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  151. int write)
  152. {
  153. struct page *page;
  154. int ret;
  155. #ifdef CONFIG_STACK_GROWSUP
  156. if (write) {
  157. ret = expand_stack_downwards(bprm->vma, pos);
  158. if (ret < 0)
  159. return NULL;
  160. }
  161. #endif
  162. ret = get_user_pages(current, bprm->mm, pos,
  163. 1, write, 1, &page, NULL);
  164. if (ret <= 0)
  165. return NULL;
  166. if (write) {
  167. unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
  168. struct rlimit *rlim;
  169. /*
  170. * We've historically supported up to 32 pages (ARG_MAX)
  171. * of argument strings even with small stacks
  172. */
  173. if (size <= ARG_MAX)
  174. return page;
  175. /*
  176. * Limit to 1/4-th the stack size for the argv+env strings.
  177. * This ensures that:
  178. * - the remaining binfmt code will not run out of stack space,
  179. * - the program will have a reasonable amount of stack left
  180. * to work from.
  181. */
  182. rlim = current->signal->rlim;
  183. if (size > rlim[RLIMIT_STACK].rlim_cur / 4) {
  184. put_page(page);
  185. return NULL;
  186. }
  187. }
  188. return page;
  189. }
  190. static void put_arg_page(struct page *page)
  191. {
  192. put_page(page);
  193. }
  194. static void free_arg_page(struct linux_binprm *bprm, int i)
  195. {
  196. }
  197. static void free_arg_pages(struct linux_binprm *bprm)
  198. {
  199. }
  200. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  201. struct page *page)
  202. {
  203. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  204. }
  205. static int __bprm_mm_init(struct linux_binprm *bprm)
  206. {
  207. int err;
  208. struct vm_area_struct *vma = NULL;
  209. struct mm_struct *mm = bprm->mm;
  210. bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  211. if (!vma)
  212. return -ENOMEM;
  213. down_write(&mm->mmap_sem);
  214. vma->vm_mm = mm;
  215. /*
  216. * Place the stack at the largest stack address the architecture
  217. * supports. Later, we'll move this to an appropriate place. We don't
  218. * use STACK_TOP because that can depend on attributes which aren't
  219. * configured yet.
  220. */
  221. vma->vm_end = STACK_TOP_MAX;
  222. vma->vm_start = vma->vm_end - PAGE_SIZE;
  223. vma->vm_flags = VM_STACK_FLAGS;
  224. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  225. err = insert_vm_struct(mm, vma);
  226. if (err)
  227. goto err;
  228. mm->stack_vm = mm->total_vm = 1;
  229. up_write(&mm->mmap_sem);
  230. bprm->p = vma->vm_end - sizeof(void *);
  231. return 0;
  232. err:
  233. up_write(&mm->mmap_sem);
  234. bprm->vma = NULL;
  235. kmem_cache_free(vm_area_cachep, vma);
  236. return err;
  237. }
  238. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  239. {
  240. return len <= MAX_ARG_STRLEN;
  241. }
  242. #else
  243. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  244. int write)
  245. {
  246. struct page *page;
  247. page = bprm->page[pos / PAGE_SIZE];
  248. if (!page && write) {
  249. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  250. if (!page)
  251. return NULL;
  252. bprm->page[pos / PAGE_SIZE] = page;
  253. }
  254. return page;
  255. }
  256. static void put_arg_page(struct page *page)
  257. {
  258. }
  259. static void free_arg_page(struct linux_binprm *bprm, int i)
  260. {
  261. if (bprm->page[i]) {
  262. __free_page(bprm->page[i]);
  263. bprm->page[i] = NULL;
  264. }
  265. }
  266. static void free_arg_pages(struct linux_binprm *bprm)
  267. {
  268. int i;
  269. for (i = 0; i < MAX_ARG_PAGES; i++)
  270. free_arg_page(bprm, i);
  271. }
  272. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  273. struct page *page)
  274. {
  275. }
  276. static int __bprm_mm_init(struct linux_binprm *bprm)
  277. {
  278. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  279. return 0;
  280. }
  281. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  282. {
  283. return len <= bprm->p;
  284. }
  285. #endif /* CONFIG_MMU */
  286. /*
  287. * Create a new mm_struct and populate it with a temporary stack
  288. * vm_area_struct. We don't have enough context at this point to set the stack
  289. * flags, permissions, and offset, so we use temporary values. We'll update
  290. * them later in setup_arg_pages().
  291. */
  292. int bprm_mm_init(struct linux_binprm *bprm)
  293. {
  294. int err;
  295. struct mm_struct *mm = NULL;
  296. bprm->mm = mm = mm_alloc();
  297. err = -ENOMEM;
  298. if (!mm)
  299. goto err;
  300. err = init_new_context(current, mm);
  301. if (err)
  302. goto err;
  303. err = __bprm_mm_init(bprm);
  304. if (err)
  305. goto err;
  306. return 0;
  307. err:
  308. if (mm) {
  309. bprm->mm = NULL;
  310. mmdrop(mm);
  311. }
  312. return err;
  313. }
  314. /*
  315. * count() counts the number of strings in array ARGV.
  316. */
  317. static int count(char __user * __user * argv, int max)
  318. {
  319. int i = 0;
  320. if (argv != NULL) {
  321. for (;;) {
  322. char __user * p;
  323. if (get_user(p, argv))
  324. return -EFAULT;
  325. if (!p)
  326. break;
  327. argv++;
  328. if (i++ >= max)
  329. return -E2BIG;
  330. cond_resched();
  331. }
  332. }
  333. return i;
  334. }
  335. /*
  336. * 'copy_strings()' copies argument/environment strings from the old
  337. * processes's memory to the new process's stack. The call to get_user_pages()
  338. * ensures the destination page is created and not swapped out.
  339. */
  340. static int copy_strings(int argc, char __user * __user * argv,
  341. struct linux_binprm *bprm)
  342. {
  343. struct page *kmapped_page = NULL;
  344. char *kaddr = NULL;
  345. unsigned long kpos = 0;
  346. int ret;
  347. while (argc-- > 0) {
  348. char __user *str;
  349. int len;
  350. unsigned long pos;
  351. if (get_user(str, argv+argc) ||
  352. !(len = strnlen_user(str, MAX_ARG_STRLEN))) {
  353. ret = -EFAULT;
  354. goto out;
  355. }
  356. if (!valid_arg_len(bprm, len)) {
  357. ret = -E2BIG;
  358. goto out;
  359. }
  360. /* We're going to work our way backwords. */
  361. pos = bprm->p;
  362. str += len;
  363. bprm->p -= len;
  364. while (len > 0) {
  365. int offset, bytes_to_copy;
  366. offset = pos % PAGE_SIZE;
  367. if (offset == 0)
  368. offset = PAGE_SIZE;
  369. bytes_to_copy = offset;
  370. if (bytes_to_copy > len)
  371. bytes_to_copy = len;
  372. offset -= bytes_to_copy;
  373. pos -= bytes_to_copy;
  374. str -= bytes_to_copy;
  375. len -= bytes_to_copy;
  376. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  377. struct page *page;
  378. page = get_arg_page(bprm, pos, 1);
  379. if (!page) {
  380. ret = -E2BIG;
  381. goto out;
  382. }
  383. if (kmapped_page) {
  384. flush_kernel_dcache_page(kmapped_page);
  385. kunmap(kmapped_page);
  386. put_arg_page(kmapped_page);
  387. }
  388. kmapped_page = page;
  389. kaddr = kmap(kmapped_page);
  390. kpos = pos & PAGE_MASK;
  391. flush_arg_page(bprm, kpos, kmapped_page);
  392. }
  393. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  394. ret = -EFAULT;
  395. goto out;
  396. }
  397. }
  398. }
  399. ret = 0;
  400. out:
  401. if (kmapped_page) {
  402. flush_kernel_dcache_page(kmapped_page);
  403. kunmap(kmapped_page);
  404. put_arg_page(kmapped_page);
  405. }
  406. return ret;
  407. }
  408. /*
  409. * Like copy_strings, but get argv and its values from kernel memory.
  410. */
  411. int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
  412. {
  413. int r;
  414. mm_segment_t oldfs = get_fs();
  415. set_fs(KERNEL_DS);
  416. r = copy_strings(argc, (char __user * __user *)argv, bprm);
  417. set_fs(oldfs);
  418. return r;
  419. }
  420. EXPORT_SYMBOL(copy_strings_kernel);
  421. #ifdef CONFIG_MMU
  422. /*
  423. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  424. * the binfmt code determines where the new stack should reside, we shift it to
  425. * its final location. The process proceeds as follows:
  426. *
  427. * 1) Use shift to calculate the new vma endpoints.
  428. * 2) Extend vma to cover both the old and new ranges. This ensures the
  429. * arguments passed to subsequent functions are consistent.
  430. * 3) Move vma's page tables to the new range.
  431. * 4) Free up any cleared pgd range.
  432. * 5) Shrink the vma to cover only the new range.
  433. */
  434. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  435. {
  436. struct mm_struct *mm = vma->vm_mm;
  437. unsigned long old_start = vma->vm_start;
  438. unsigned long old_end = vma->vm_end;
  439. unsigned long length = old_end - old_start;
  440. unsigned long new_start = old_start - shift;
  441. unsigned long new_end = old_end - shift;
  442. struct mmu_gather *tlb;
  443. BUG_ON(new_start > new_end);
  444. /*
  445. * ensure there are no vmas between where we want to go
  446. * and where we are
  447. */
  448. if (vma != find_vma(mm, new_start))
  449. return -EFAULT;
  450. /*
  451. * cover the whole range: [new_start, old_end)
  452. */
  453. vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL);
  454. /*
  455. * move the page tables downwards, on failure we rely on
  456. * process cleanup to remove whatever mess we made.
  457. */
  458. if (length != move_page_tables(vma, old_start,
  459. vma, new_start, length))
  460. return -ENOMEM;
  461. lru_add_drain();
  462. tlb = tlb_gather_mmu(mm, 0);
  463. if (new_end > old_start) {
  464. /*
  465. * when the old and new regions overlap clear from new_end.
  466. */
  467. free_pgd_range(tlb, new_end, old_end, new_end,
  468. vma->vm_next ? vma->vm_next->vm_start : 0);
  469. } else {
  470. /*
  471. * otherwise, clean from old_start; this is done to not touch
  472. * the address space in [new_end, old_start) some architectures
  473. * have constraints on va-space that make this illegal (IA64) -
  474. * for the others its just a little faster.
  475. */
  476. free_pgd_range(tlb, old_start, old_end, new_end,
  477. vma->vm_next ? vma->vm_next->vm_start : 0);
  478. }
  479. tlb_finish_mmu(tlb, new_end, old_end);
  480. /*
  481. * shrink the vma to just the new range.
  482. */
  483. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  484. return 0;
  485. }
  486. #define EXTRA_STACK_VM_PAGES 20 /* random */
  487. /*
  488. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  489. * the stack is optionally relocated, and some extra space is added.
  490. */
  491. int setup_arg_pages(struct linux_binprm *bprm,
  492. unsigned long stack_top,
  493. int executable_stack)
  494. {
  495. unsigned long ret;
  496. unsigned long stack_shift;
  497. struct mm_struct *mm = current->mm;
  498. struct vm_area_struct *vma = bprm->vma;
  499. struct vm_area_struct *prev = NULL;
  500. unsigned long vm_flags;
  501. unsigned long stack_base;
  502. #ifdef CONFIG_STACK_GROWSUP
  503. /* Limit stack size to 1GB */
  504. stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
  505. if (stack_base > (1 << 30))
  506. stack_base = 1 << 30;
  507. /* Make sure we didn't let the argument array grow too large. */
  508. if (vma->vm_end - vma->vm_start > stack_base)
  509. return -ENOMEM;
  510. stack_base = PAGE_ALIGN(stack_top - stack_base);
  511. stack_shift = vma->vm_start - stack_base;
  512. mm->arg_start = bprm->p - stack_shift;
  513. bprm->p = vma->vm_end - stack_shift;
  514. #else
  515. stack_top = arch_align_stack(stack_top);
  516. stack_top = PAGE_ALIGN(stack_top);
  517. stack_shift = vma->vm_end - stack_top;
  518. bprm->p -= stack_shift;
  519. mm->arg_start = bprm->p;
  520. #endif
  521. if (bprm->loader)
  522. bprm->loader -= stack_shift;
  523. bprm->exec -= stack_shift;
  524. down_write(&mm->mmap_sem);
  525. vm_flags = VM_STACK_FLAGS;
  526. /*
  527. * Adjust stack execute permissions; explicitly enable for
  528. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  529. * (arch default) otherwise.
  530. */
  531. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  532. vm_flags |= VM_EXEC;
  533. else if (executable_stack == EXSTACK_DISABLE_X)
  534. vm_flags &= ~VM_EXEC;
  535. vm_flags |= mm->def_flags;
  536. ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
  537. vm_flags);
  538. if (ret)
  539. goto out_unlock;
  540. BUG_ON(prev != vma);
  541. /* Move stack pages down in memory. */
  542. if (stack_shift) {
  543. ret = shift_arg_pages(vma, stack_shift);
  544. if (ret) {
  545. up_write(&mm->mmap_sem);
  546. return ret;
  547. }
  548. }
  549. #ifdef CONFIG_STACK_GROWSUP
  550. stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  551. #else
  552. stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  553. #endif
  554. ret = expand_stack(vma, stack_base);
  555. if (ret)
  556. ret = -EFAULT;
  557. out_unlock:
  558. up_write(&mm->mmap_sem);
  559. return 0;
  560. }
  561. EXPORT_SYMBOL(setup_arg_pages);
  562. #endif /* CONFIG_MMU */
  563. struct file *open_exec(const char *name)
  564. {
  565. struct nameidata nd;
  566. struct file *file;
  567. int err;
  568. err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd,
  569. FMODE_READ|FMODE_EXEC);
  570. if (err)
  571. goto out;
  572. err = -EACCES;
  573. if (!S_ISREG(nd.path.dentry->d_inode->i_mode))
  574. goto out_path_put;
  575. if (nd.path.mnt->mnt_flags & MNT_NOEXEC)
  576. goto out_path_put;
  577. err = inode_permission(nd.path.dentry->d_inode, MAY_EXEC | MAY_OPEN);
  578. if (err)
  579. goto out_path_put;
  580. err = ima_path_check(&nd.path, MAY_EXEC | MAY_OPEN);
  581. if (err)
  582. goto out_path_put;
  583. file = nameidata_to_filp(&nd, O_RDONLY|O_LARGEFILE);
  584. if (IS_ERR(file))
  585. return file;
  586. fsnotify_open(file->f_path.dentry);
  587. err = deny_write_access(file);
  588. if (err) {
  589. fput(file);
  590. goto out;
  591. }
  592. return file;
  593. out_path_put:
  594. release_open_intent(&nd);
  595. path_put(&nd.path);
  596. out:
  597. return ERR_PTR(err);
  598. }
  599. EXPORT_SYMBOL(open_exec);
  600. int kernel_read(struct file *file, unsigned long offset,
  601. char *addr, unsigned long count)
  602. {
  603. mm_segment_t old_fs;
  604. loff_t pos = offset;
  605. int result;
  606. old_fs = get_fs();
  607. set_fs(get_ds());
  608. /* The cast to a user pointer is valid due to the set_fs() */
  609. result = vfs_read(file, (void __user *)addr, count, &pos);
  610. set_fs(old_fs);
  611. return result;
  612. }
  613. EXPORT_SYMBOL(kernel_read);
  614. static int exec_mmap(struct mm_struct *mm)
  615. {
  616. struct task_struct *tsk;
  617. struct mm_struct * old_mm, *active_mm;
  618. /* Notify parent that we're no longer interested in the old VM */
  619. tsk = current;
  620. old_mm = current->mm;
  621. mm_release(tsk, old_mm);
  622. if (old_mm) {
  623. /*
  624. * Make sure that if there is a core dump in progress
  625. * for the old mm, we get out and die instead of going
  626. * through with the exec. We must hold mmap_sem around
  627. * checking core_state and changing tsk->mm.
  628. */
  629. down_read(&old_mm->mmap_sem);
  630. if (unlikely(old_mm->core_state)) {
  631. up_read(&old_mm->mmap_sem);
  632. return -EINTR;
  633. }
  634. }
  635. task_lock(tsk);
  636. active_mm = tsk->active_mm;
  637. tsk->mm = mm;
  638. tsk->active_mm = mm;
  639. activate_mm(active_mm, mm);
  640. task_unlock(tsk);
  641. arch_pick_mmap_layout(mm);
  642. if (old_mm) {
  643. up_read(&old_mm->mmap_sem);
  644. BUG_ON(active_mm != old_mm);
  645. mm_update_next_owner(old_mm);
  646. mmput(old_mm);
  647. return 0;
  648. }
  649. mmdrop(active_mm);
  650. return 0;
  651. }
  652. /*
  653. * This function makes sure the current process has its own signal table,
  654. * so that flush_signal_handlers can later reset the handlers without
  655. * disturbing other processes. (Other processes might share the signal
  656. * table via the CLONE_SIGHAND option to clone().)
  657. */
  658. static int de_thread(struct task_struct *tsk)
  659. {
  660. struct signal_struct *sig = tsk->signal;
  661. struct sighand_struct *oldsighand = tsk->sighand;
  662. spinlock_t *lock = &oldsighand->siglock;
  663. int count;
  664. if (thread_group_empty(tsk))
  665. goto no_thread_group;
  666. /*
  667. * Kill all other threads in the thread group.
  668. */
  669. spin_lock_irq(lock);
  670. if (signal_group_exit(sig)) {
  671. /*
  672. * Another group action in progress, just
  673. * return so that the signal is processed.
  674. */
  675. spin_unlock_irq(lock);
  676. return -EAGAIN;
  677. }
  678. sig->group_exit_task = tsk;
  679. zap_other_threads(tsk);
  680. /* Account for the thread group leader hanging around: */
  681. count = thread_group_leader(tsk) ? 1 : 2;
  682. sig->notify_count = count;
  683. while (atomic_read(&sig->count) > count) {
  684. __set_current_state(TASK_UNINTERRUPTIBLE);
  685. spin_unlock_irq(lock);
  686. schedule();
  687. spin_lock_irq(lock);
  688. }
  689. spin_unlock_irq(lock);
  690. /*
  691. * At this point all other threads have exited, all we have to
  692. * do is to wait for the thread group leader to become inactive,
  693. * and to assume its PID:
  694. */
  695. if (!thread_group_leader(tsk)) {
  696. struct task_struct *leader = tsk->group_leader;
  697. sig->notify_count = -1; /* for exit_notify() */
  698. for (;;) {
  699. write_lock_irq(&tasklist_lock);
  700. if (likely(leader->exit_state))
  701. break;
  702. __set_current_state(TASK_UNINTERRUPTIBLE);
  703. write_unlock_irq(&tasklist_lock);
  704. schedule();
  705. }
  706. /*
  707. * The only record we have of the real-time age of a
  708. * process, regardless of execs it's done, is start_time.
  709. * All the past CPU time is accumulated in signal_struct
  710. * from sister threads now dead. But in this non-leader
  711. * exec, nothing survives from the original leader thread,
  712. * whose birth marks the true age of this process now.
  713. * When we take on its identity by switching to its PID, we
  714. * also take its birthdate (always earlier than our own).
  715. */
  716. tsk->start_time = leader->start_time;
  717. BUG_ON(!same_thread_group(leader, tsk));
  718. BUG_ON(has_group_leader_pid(tsk));
  719. /*
  720. * An exec() starts a new thread group with the
  721. * TGID of the previous thread group. Rehash the
  722. * two threads with a switched PID, and release
  723. * the former thread group leader:
  724. */
  725. /* Become a process group leader with the old leader's pid.
  726. * The old leader becomes a thread of the this thread group.
  727. * Note: The old leader also uses this pid until release_task
  728. * is called. Odd but simple and correct.
  729. */
  730. detach_pid(tsk, PIDTYPE_PID);
  731. tsk->pid = leader->pid;
  732. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  733. transfer_pid(leader, tsk, PIDTYPE_PGID);
  734. transfer_pid(leader, tsk, PIDTYPE_SID);
  735. list_replace_rcu(&leader->tasks, &tsk->tasks);
  736. tsk->group_leader = tsk;
  737. leader->group_leader = tsk;
  738. tsk->exit_signal = SIGCHLD;
  739. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  740. leader->exit_state = EXIT_DEAD;
  741. write_unlock_irq(&tasklist_lock);
  742. release_task(leader);
  743. }
  744. sig->group_exit_task = NULL;
  745. sig->notify_count = 0;
  746. no_thread_group:
  747. exit_itimers(sig);
  748. flush_itimer_signals();
  749. if (atomic_read(&oldsighand->count) != 1) {
  750. struct sighand_struct *newsighand;
  751. /*
  752. * This ->sighand is shared with the CLONE_SIGHAND
  753. * but not CLONE_THREAD task, switch to the new one.
  754. */
  755. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  756. if (!newsighand)
  757. return -ENOMEM;
  758. atomic_set(&newsighand->count, 1);
  759. memcpy(newsighand->action, oldsighand->action,
  760. sizeof(newsighand->action));
  761. write_lock_irq(&tasklist_lock);
  762. spin_lock(&oldsighand->siglock);
  763. rcu_assign_pointer(tsk->sighand, newsighand);
  764. spin_unlock(&oldsighand->siglock);
  765. write_unlock_irq(&tasklist_lock);
  766. __cleanup_sighand(oldsighand);
  767. }
  768. BUG_ON(!thread_group_leader(tsk));
  769. return 0;
  770. }
  771. /*
  772. * These functions flushes out all traces of the currently running executable
  773. * so that a new one can be started
  774. */
  775. static void flush_old_files(struct files_struct * files)
  776. {
  777. long j = -1;
  778. struct fdtable *fdt;
  779. spin_lock(&files->file_lock);
  780. for (;;) {
  781. unsigned long set, i;
  782. j++;
  783. i = j * __NFDBITS;
  784. fdt = files_fdtable(files);
  785. if (i >= fdt->max_fds)
  786. break;
  787. set = fdt->close_on_exec->fds_bits[j];
  788. if (!set)
  789. continue;
  790. fdt->close_on_exec->fds_bits[j] = 0;
  791. spin_unlock(&files->file_lock);
  792. for ( ; set ; i++,set >>= 1) {
  793. if (set & 1) {
  794. sys_close(i);
  795. }
  796. }
  797. spin_lock(&files->file_lock);
  798. }
  799. spin_unlock(&files->file_lock);
  800. }
  801. char *get_task_comm(char *buf, struct task_struct *tsk)
  802. {
  803. /* buf must be at least sizeof(tsk->comm) in size */
  804. task_lock(tsk);
  805. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  806. task_unlock(tsk);
  807. return buf;
  808. }
  809. void set_task_comm(struct task_struct *tsk, char *buf)
  810. {
  811. task_lock(tsk);
  812. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  813. task_unlock(tsk);
  814. }
  815. int flush_old_exec(struct linux_binprm * bprm)
  816. {
  817. char * name;
  818. int i, ch, retval;
  819. char tcomm[sizeof(current->comm)];
  820. /*
  821. * Make sure we have a private signal table and that
  822. * we are unassociated from the previous thread group.
  823. */
  824. retval = de_thread(current);
  825. if (retval)
  826. goto out;
  827. set_mm_exe_file(bprm->mm, bprm->file);
  828. /*
  829. * Release all of the old mmap stuff
  830. */
  831. retval = exec_mmap(bprm->mm);
  832. if (retval)
  833. goto out;
  834. bprm->mm = NULL; /* We're using it now */
  835. /* This is the point of no return */
  836. current->sas_ss_sp = current->sas_ss_size = 0;
  837. if (current_euid() == current_uid() && current_egid() == current_gid())
  838. set_dumpable(current->mm, 1);
  839. else
  840. set_dumpable(current->mm, suid_dumpable);
  841. name = bprm->filename;
  842. /* Copies the binary name from after last slash */
  843. for (i=0; (ch = *(name++)) != '\0';) {
  844. if (ch == '/')
  845. i = 0; /* overwrite what we wrote */
  846. else
  847. if (i < (sizeof(tcomm) - 1))
  848. tcomm[i++] = ch;
  849. }
  850. tcomm[i] = '\0';
  851. set_task_comm(current, tcomm);
  852. current->flags &= ~PF_RANDOMIZE;
  853. flush_thread();
  854. /* Set the new mm task size. We have to do that late because it may
  855. * depend on TIF_32BIT which is only updated in flush_thread() on
  856. * some architectures like powerpc
  857. */
  858. current->mm->task_size = TASK_SIZE;
  859. /* install the new credentials */
  860. if (bprm->cred->uid != current_euid() ||
  861. bprm->cred->gid != current_egid()) {
  862. current->pdeath_signal = 0;
  863. } else if (file_permission(bprm->file, MAY_READ) ||
  864. bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP) {
  865. set_dumpable(current->mm, suid_dumpable);
  866. }
  867. current->personality &= ~bprm->per_clear;
  868. /* An exec changes our domain. We are no longer part of the thread
  869. group */
  870. current->self_exec_id++;
  871. flush_signal_handlers(current, 0);
  872. flush_old_files(current->files);
  873. return 0;
  874. out:
  875. return retval;
  876. }
  877. EXPORT_SYMBOL(flush_old_exec);
  878. /*
  879. * install the new credentials for this executable
  880. */
  881. void install_exec_creds(struct linux_binprm *bprm)
  882. {
  883. security_bprm_committing_creds(bprm);
  884. commit_creds(bprm->cred);
  885. bprm->cred = NULL;
  886. /* cred_exec_mutex must be held at least to this point to prevent
  887. * ptrace_attach() from altering our determination of the task's
  888. * credentials; any time after this it may be unlocked */
  889. security_bprm_committed_creds(bprm);
  890. }
  891. EXPORT_SYMBOL(install_exec_creds);
  892. /*
  893. * determine how safe it is to execute the proposed program
  894. * - the caller must hold current->cred_exec_mutex to protect against
  895. * PTRACE_ATTACH
  896. */
  897. void check_unsafe_exec(struct linux_binprm *bprm, struct files_struct *files)
  898. {
  899. struct task_struct *p = current, *t;
  900. unsigned long flags;
  901. unsigned n_fs, n_files, n_sighand;
  902. bprm->unsafe = tracehook_unsafe_exec(p);
  903. n_fs = 1;
  904. n_files = 1;
  905. n_sighand = 1;
  906. lock_task_sighand(p, &flags);
  907. for (t = next_thread(p); t != p; t = next_thread(t)) {
  908. if (t->fs == p->fs)
  909. n_fs++;
  910. if (t->files == files)
  911. n_files++;
  912. n_sighand++;
  913. }
  914. if (atomic_read(&p->fs->count) > n_fs ||
  915. atomic_read(&p->files->count) > n_files ||
  916. atomic_read(&p->sighand->count) > n_sighand)
  917. bprm->unsafe |= LSM_UNSAFE_SHARE;
  918. unlock_task_sighand(p, &flags);
  919. }
  920. /*
  921. * Fill the binprm structure from the inode.
  922. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  923. *
  924. * This may be called multiple times for binary chains (scripts for example).
  925. */
  926. int prepare_binprm(struct linux_binprm *bprm)
  927. {
  928. umode_t mode;
  929. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  930. int retval;
  931. mode = inode->i_mode;
  932. if (bprm->file->f_op == NULL)
  933. return -EACCES;
  934. /* clear any previous set[ug]id data from a previous binary */
  935. bprm->cred->euid = current_euid();
  936. bprm->cred->egid = current_egid();
  937. if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  938. /* Set-uid? */
  939. if (mode & S_ISUID) {
  940. bprm->per_clear |= PER_CLEAR_ON_SETID;
  941. bprm->cred->euid = inode->i_uid;
  942. }
  943. /* Set-gid? */
  944. /*
  945. * If setgid is set but no group execute bit then this
  946. * is a candidate for mandatory locking, not a setgid
  947. * executable.
  948. */
  949. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  950. bprm->per_clear |= PER_CLEAR_ON_SETID;
  951. bprm->cred->egid = inode->i_gid;
  952. }
  953. }
  954. /* fill in binprm security blob */
  955. retval = security_bprm_set_creds(bprm);
  956. if (retval)
  957. return retval;
  958. bprm->cred_prepared = 1;
  959. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  960. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  961. }
  962. EXPORT_SYMBOL(prepare_binprm);
  963. /*
  964. * Arguments are '\0' separated strings found at the location bprm->p
  965. * points to; chop off the first by relocating brpm->p to right after
  966. * the first '\0' encountered.
  967. */
  968. int remove_arg_zero(struct linux_binprm *bprm)
  969. {
  970. int ret = 0;
  971. unsigned long offset;
  972. char *kaddr;
  973. struct page *page;
  974. if (!bprm->argc)
  975. return 0;
  976. do {
  977. offset = bprm->p & ~PAGE_MASK;
  978. page = get_arg_page(bprm, bprm->p, 0);
  979. if (!page) {
  980. ret = -EFAULT;
  981. goto out;
  982. }
  983. kaddr = kmap_atomic(page, KM_USER0);
  984. for (; offset < PAGE_SIZE && kaddr[offset];
  985. offset++, bprm->p++)
  986. ;
  987. kunmap_atomic(kaddr, KM_USER0);
  988. put_arg_page(page);
  989. if (offset == PAGE_SIZE)
  990. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  991. } while (offset == PAGE_SIZE);
  992. bprm->p++;
  993. bprm->argc--;
  994. ret = 0;
  995. out:
  996. return ret;
  997. }
  998. EXPORT_SYMBOL(remove_arg_zero);
  999. /*
  1000. * cycle the list of binary formats handler, until one recognizes the image
  1001. */
  1002. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  1003. {
  1004. unsigned int depth = bprm->recursion_depth;
  1005. int try,retval;
  1006. struct linux_binfmt *fmt;
  1007. retval = security_bprm_check(bprm);
  1008. if (retval)
  1009. return retval;
  1010. retval = ima_bprm_check(bprm);
  1011. if (retval)
  1012. return retval;
  1013. /* kernel module loader fixup */
  1014. /* so we don't try to load run modprobe in kernel space. */
  1015. set_fs(USER_DS);
  1016. retval = audit_bprm(bprm);
  1017. if (retval)
  1018. return retval;
  1019. retval = -ENOENT;
  1020. for (try=0; try<2; try++) {
  1021. read_lock(&binfmt_lock);
  1022. list_for_each_entry(fmt, &formats, lh) {
  1023. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1024. if (!fn)
  1025. continue;
  1026. if (!try_module_get(fmt->module))
  1027. continue;
  1028. read_unlock(&binfmt_lock);
  1029. retval = fn(bprm, regs);
  1030. /*
  1031. * Restore the depth counter to its starting value
  1032. * in this call, so we don't have to rely on every
  1033. * load_binary function to restore it on return.
  1034. */
  1035. bprm->recursion_depth = depth;
  1036. if (retval >= 0) {
  1037. if (depth == 0)
  1038. tracehook_report_exec(fmt, bprm, regs);
  1039. put_binfmt(fmt);
  1040. allow_write_access(bprm->file);
  1041. if (bprm->file)
  1042. fput(bprm->file);
  1043. bprm->file = NULL;
  1044. current->did_exec = 1;
  1045. proc_exec_connector(current);
  1046. return retval;
  1047. }
  1048. read_lock(&binfmt_lock);
  1049. put_binfmt(fmt);
  1050. if (retval != -ENOEXEC || bprm->mm == NULL)
  1051. break;
  1052. if (!bprm->file) {
  1053. read_unlock(&binfmt_lock);
  1054. return retval;
  1055. }
  1056. }
  1057. read_unlock(&binfmt_lock);
  1058. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1059. break;
  1060. #ifdef CONFIG_MODULES
  1061. } else {
  1062. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1063. if (printable(bprm->buf[0]) &&
  1064. printable(bprm->buf[1]) &&
  1065. printable(bprm->buf[2]) &&
  1066. printable(bprm->buf[3]))
  1067. break; /* -ENOEXEC */
  1068. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1069. #endif
  1070. }
  1071. }
  1072. return retval;
  1073. }
  1074. EXPORT_SYMBOL(search_binary_handler);
  1075. void free_bprm(struct linux_binprm *bprm)
  1076. {
  1077. free_arg_pages(bprm);
  1078. if (bprm->cred)
  1079. abort_creds(bprm->cred);
  1080. kfree(bprm);
  1081. }
  1082. /*
  1083. * sys_execve() executes a new program.
  1084. */
  1085. int do_execve(char * filename,
  1086. char __user *__user *argv,
  1087. char __user *__user *envp,
  1088. struct pt_regs * regs)
  1089. {
  1090. struct linux_binprm *bprm;
  1091. struct file *file;
  1092. struct files_struct *displaced;
  1093. int retval;
  1094. retval = unshare_files(&displaced);
  1095. if (retval)
  1096. goto out_ret;
  1097. retval = -ENOMEM;
  1098. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1099. if (!bprm)
  1100. goto out_files;
  1101. retval = mutex_lock_interruptible(&current->cred_exec_mutex);
  1102. if (retval < 0)
  1103. goto out_free;
  1104. current->in_execve = 1;
  1105. retval = -ENOMEM;
  1106. bprm->cred = prepare_exec_creds();
  1107. if (!bprm->cred)
  1108. goto out_unlock;
  1109. check_unsafe_exec(bprm, displaced);
  1110. file = open_exec(filename);
  1111. retval = PTR_ERR(file);
  1112. if (IS_ERR(file))
  1113. goto out_unlock;
  1114. sched_exec();
  1115. bprm->file = file;
  1116. bprm->filename = filename;
  1117. bprm->interp = filename;
  1118. retval = bprm_mm_init(bprm);
  1119. if (retval)
  1120. goto out_file;
  1121. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1122. if ((retval = bprm->argc) < 0)
  1123. goto out;
  1124. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1125. if ((retval = bprm->envc) < 0)
  1126. goto out;
  1127. retval = prepare_binprm(bprm);
  1128. if (retval < 0)
  1129. goto out;
  1130. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1131. if (retval < 0)
  1132. goto out;
  1133. bprm->exec = bprm->p;
  1134. retval = copy_strings(bprm->envc, envp, bprm);
  1135. if (retval < 0)
  1136. goto out;
  1137. retval = copy_strings(bprm->argc, argv, bprm);
  1138. if (retval < 0)
  1139. goto out;
  1140. current->flags &= ~PF_KTHREAD;
  1141. retval = search_binary_handler(bprm,regs);
  1142. if (retval < 0)
  1143. goto out;
  1144. /* execve succeeded */
  1145. current->in_execve = 0;
  1146. mutex_unlock(&current->cred_exec_mutex);
  1147. acct_update_integrals(current);
  1148. free_bprm(bprm);
  1149. if (displaced)
  1150. put_files_struct(displaced);
  1151. return retval;
  1152. out:
  1153. if (bprm->mm)
  1154. mmput (bprm->mm);
  1155. out_file:
  1156. if (bprm->file) {
  1157. allow_write_access(bprm->file);
  1158. fput(bprm->file);
  1159. }
  1160. out_unlock:
  1161. current->in_execve = 0;
  1162. mutex_unlock(&current->cred_exec_mutex);
  1163. out_free:
  1164. free_bprm(bprm);
  1165. out_files:
  1166. if (displaced)
  1167. reset_files_struct(displaced);
  1168. out_ret:
  1169. return retval;
  1170. }
  1171. int set_binfmt(struct linux_binfmt *new)
  1172. {
  1173. struct linux_binfmt *old = current->binfmt;
  1174. if (new) {
  1175. if (!try_module_get(new->module))
  1176. return -1;
  1177. }
  1178. current->binfmt = new;
  1179. if (old)
  1180. module_put(old->module);
  1181. return 0;
  1182. }
  1183. EXPORT_SYMBOL(set_binfmt);
  1184. /* format_corename will inspect the pattern parameter, and output a
  1185. * name into corename, which must have space for at least
  1186. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1187. */
  1188. static int format_corename(char *corename, long signr)
  1189. {
  1190. const struct cred *cred = current_cred();
  1191. const char *pat_ptr = core_pattern;
  1192. int ispipe = (*pat_ptr == '|');
  1193. char *out_ptr = corename;
  1194. char *const out_end = corename + CORENAME_MAX_SIZE;
  1195. int rc;
  1196. int pid_in_pattern = 0;
  1197. /* Repeat as long as we have more pattern to process and more output
  1198. space */
  1199. while (*pat_ptr) {
  1200. if (*pat_ptr != '%') {
  1201. if (out_ptr == out_end)
  1202. goto out;
  1203. *out_ptr++ = *pat_ptr++;
  1204. } else {
  1205. switch (*++pat_ptr) {
  1206. case 0:
  1207. goto out;
  1208. /* Double percent, output one percent */
  1209. case '%':
  1210. if (out_ptr == out_end)
  1211. goto out;
  1212. *out_ptr++ = '%';
  1213. break;
  1214. /* pid */
  1215. case 'p':
  1216. pid_in_pattern = 1;
  1217. rc = snprintf(out_ptr, out_end - out_ptr,
  1218. "%d", task_tgid_vnr(current));
  1219. if (rc > out_end - out_ptr)
  1220. goto out;
  1221. out_ptr += rc;
  1222. break;
  1223. /* uid */
  1224. case 'u':
  1225. rc = snprintf(out_ptr, out_end - out_ptr,
  1226. "%d", cred->uid);
  1227. if (rc > out_end - out_ptr)
  1228. goto out;
  1229. out_ptr += rc;
  1230. break;
  1231. /* gid */
  1232. case 'g':
  1233. rc = snprintf(out_ptr, out_end - out_ptr,
  1234. "%d", cred->gid);
  1235. if (rc > out_end - out_ptr)
  1236. goto out;
  1237. out_ptr += rc;
  1238. break;
  1239. /* signal that caused the coredump */
  1240. case 's':
  1241. rc = snprintf(out_ptr, out_end - out_ptr,
  1242. "%ld", signr);
  1243. if (rc > out_end - out_ptr)
  1244. goto out;
  1245. out_ptr += rc;
  1246. break;
  1247. /* UNIX time of coredump */
  1248. case 't': {
  1249. struct timeval tv;
  1250. do_gettimeofday(&tv);
  1251. rc = snprintf(out_ptr, out_end - out_ptr,
  1252. "%lu", tv.tv_sec);
  1253. if (rc > out_end - out_ptr)
  1254. goto out;
  1255. out_ptr += rc;
  1256. break;
  1257. }
  1258. /* hostname */
  1259. case 'h':
  1260. down_read(&uts_sem);
  1261. rc = snprintf(out_ptr, out_end - out_ptr,
  1262. "%s", utsname()->nodename);
  1263. up_read(&uts_sem);
  1264. if (rc > out_end - out_ptr)
  1265. goto out;
  1266. out_ptr += rc;
  1267. break;
  1268. /* executable */
  1269. case 'e':
  1270. rc = snprintf(out_ptr, out_end - out_ptr,
  1271. "%s", current->comm);
  1272. if (rc > out_end - out_ptr)
  1273. goto out;
  1274. out_ptr += rc;
  1275. break;
  1276. /* core limit size */
  1277. case 'c':
  1278. rc = snprintf(out_ptr, out_end - out_ptr,
  1279. "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
  1280. if (rc > out_end - out_ptr)
  1281. goto out;
  1282. out_ptr += rc;
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. ++pat_ptr;
  1288. }
  1289. }
  1290. /* Backward compatibility with core_uses_pid:
  1291. *
  1292. * If core_pattern does not include a %p (as is the default)
  1293. * and core_uses_pid is set, then .%pid will be appended to
  1294. * the filename. Do not do this for piped commands. */
  1295. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  1296. rc = snprintf(out_ptr, out_end - out_ptr,
  1297. ".%d", task_tgid_vnr(current));
  1298. if (rc > out_end - out_ptr)
  1299. goto out;
  1300. out_ptr += rc;
  1301. }
  1302. out:
  1303. *out_ptr = 0;
  1304. return ispipe;
  1305. }
  1306. static int zap_process(struct task_struct *start)
  1307. {
  1308. struct task_struct *t;
  1309. int nr = 0;
  1310. start->signal->flags = SIGNAL_GROUP_EXIT;
  1311. start->signal->group_stop_count = 0;
  1312. t = start;
  1313. do {
  1314. if (t != current && t->mm) {
  1315. sigaddset(&t->pending.signal, SIGKILL);
  1316. signal_wake_up(t, 1);
  1317. nr++;
  1318. }
  1319. } while_each_thread(start, t);
  1320. return nr;
  1321. }
  1322. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1323. struct core_state *core_state, int exit_code)
  1324. {
  1325. struct task_struct *g, *p;
  1326. unsigned long flags;
  1327. int nr = -EAGAIN;
  1328. spin_lock_irq(&tsk->sighand->siglock);
  1329. if (!signal_group_exit(tsk->signal)) {
  1330. mm->core_state = core_state;
  1331. tsk->signal->group_exit_code = exit_code;
  1332. nr = zap_process(tsk);
  1333. }
  1334. spin_unlock_irq(&tsk->sighand->siglock);
  1335. if (unlikely(nr < 0))
  1336. return nr;
  1337. if (atomic_read(&mm->mm_users) == nr + 1)
  1338. goto done;
  1339. /*
  1340. * We should find and kill all tasks which use this mm, and we should
  1341. * count them correctly into ->nr_threads. We don't take tasklist
  1342. * lock, but this is safe wrt:
  1343. *
  1344. * fork:
  1345. * None of sub-threads can fork after zap_process(leader). All
  1346. * processes which were created before this point should be
  1347. * visible to zap_threads() because copy_process() adds the new
  1348. * process to the tail of init_task.tasks list, and lock/unlock
  1349. * of ->siglock provides a memory barrier.
  1350. *
  1351. * do_exit:
  1352. * The caller holds mm->mmap_sem. This means that the task which
  1353. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1354. * its ->mm.
  1355. *
  1356. * de_thread:
  1357. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1358. * we must see either old or new leader, this does not matter.
  1359. * However, it can change p->sighand, so lock_task_sighand(p)
  1360. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1361. * it can't fail.
  1362. *
  1363. * Note also that "g" can be the old leader with ->mm == NULL
  1364. * and already unhashed and thus removed from ->thread_group.
  1365. * This is OK, __unhash_process()->list_del_rcu() does not
  1366. * clear the ->next pointer, we will find the new leader via
  1367. * next_thread().
  1368. */
  1369. rcu_read_lock();
  1370. for_each_process(g) {
  1371. if (g == tsk->group_leader)
  1372. continue;
  1373. if (g->flags & PF_KTHREAD)
  1374. continue;
  1375. p = g;
  1376. do {
  1377. if (p->mm) {
  1378. if (unlikely(p->mm == mm)) {
  1379. lock_task_sighand(p, &flags);
  1380. nr += zap_process(p);
  1381. unlock_task_sighand(p, &flags);
  1382. }
  1383. break;
  1384. }
  1385. } while_each_thread(g, p);
  1386. }
  1387. rcu_read_unlock();
  1388. done:
  1389. atomic_set(&core_state->nr_threads, nr);
  1390. return nr;
  1391. }
  1392. static int coredump_wait(int exit_code, struct core_state *core_state)
  1393. {
  1394. struct task_struct *tsk = current;
  1395. struct mm_struct *mm = tsk->mm;
  1396. struct completion *vfork_done;
  1397. int core_waiters;
  1398. init_completion(&core_state->startup);
  1399. core_state->dumper.task = tsk;
  1400. core_state->dumper.next = NULL;
  1401. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1402. up_write(&mm->mmap_sem);
  1403. if (unlikely(core_waiters < 0))
  1404. goto fail;
  1405. /*
  1406. * Make sure nobody is waiting for us to release the VM,
  1407. * otherwise we can deadlock when we wait on each other
  1408. */
  1409. vfork_done = tsk->vfork_done;
  1410. if (vfork_done) {
  1411. tsk->vfork_done = NULL;
  1412. complete(vfork_done);
  1413. }
  1414. if (core_waiters)
  1415. wait_for_completion(&core_state->startup);
  1416. fail:
  1417. return core_waiters;
  1418. }
  1419. static void coredump_finish(struct mm_struct *mm)
  1420. {
  1421. struct core_thread *curr, *next;
  1422. struct task_struct *task;
  1423. next = mm->core_state->dumper.next;
  1424. while ((curr = next) != NULL) {
  1425. next = curr->next;
  1426. task = curr->task;
  1427. /*
  1428. * see exit_mm(), curr->task must not see
  1429. * ->task == NULL before we read ->next.
  1430. */
  1431. smp_mb();
  1432. curr->task = NULL;
  1433. wake_up_process(task);
  1434. }
  1435. mm->core_state = NULL;
  1436. }
  1437. /*
  1438. * set_dumpable converts traditional three-value dumpable to two flags and
  1439. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1440. * these bits are not changed atomically. So get_dumpable can observe the
  1441. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1442. * return either old dumpable or new one by paying attention to the order of
  1443. * modifying the bits.
  1444. *
  1445. * dumpable | mm->flags (binary)
  1446. * old new | initial interim final
  1447. * ---------+-----------------------
  1448. * 0 1 | 00 01 01
  1449. * 0 2 | 00 10(*) 11
  1450. * 1 0 | 01 00 00
  1451. * 1 2 | 01 11 11
  1452. * 2 0 | 11 10(*) 00
  1453. * 2 1 | 11 11 01
  1454. *
  1455. * (*) get_dumpable regards interim value of 10 as 11.
  1456. */
  1457. void set_dumpable(struct mm_struct *mm, int value)
  1458. {
  1459. switch (value) {
  1460. case 0:
  1461. clear_bit(MMF_DUMPABLE, &mm->flags);
  1462. smp_wmb();
  1463. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1464. break;
  1465. case 1:
  1466. set_bit(MMF_DUMPABLE, &mm->flags);
  1467. smp_wmb();
  1468. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1469. break;
  1470. case 2:
  1471. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1472. smp_wmb();
  1473. set_bit(MMF_DUMPABLE, &mm->flags);
  1474. break;
  1475. }
  1476. }
  1477. int get_dumpable(struct mm_struct *mm)
  1478. {
  1479. int ret;
  1480. ret = mm->flags & 0x3;
  1481. return (ret >= 2) ? 2 : ret;
  1482. }
  1483. void do_coredump(long signr, int exit_code, struct pt_regs *regs)
  1484. {
  1485. struct core_state core_state;
  1486. char corename[CORENAME_MAX_SIZE + 1];
  1487. struct mm_struct *mm = current->mm;
  1488. struct linux_binfmt * binfmt;
  1489. struct inode * inode;
  1490. struct file * file;
  1491. const struct cred *old_cred;
  1492. struct cred *cred;
  1493. int retval = 0;
  1494. int flag = 0;
  1495. int ispipe = 0;
  1496. unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1497. char **helper_argv = NULL;
  1498. int helper_argc = 0;
  1499. char *delimit;
  1500. audit_core_dumps(signr);
  1501. binfmt = current->binfmt;
  1502. if (!binfmt || !binfmt->core_dump)
  1503. goto fail;
  1504. cred = prepare_creds();
  1505. if (!cred) {
  1506. retval = -ENOMEM;
  1507. goto fail;
  1508. }
  1509. down_write(&mm->mmap_sem);
  1510. /*
  1511. * If another thread got here first, or we are not dumpable, bail out.
  1512. */
  1513. if (mm->core_state || !get_dumpable(mm)) {
  1514. up_write(&mm->mmap_sem);
  1515. put_cred(cred);
  1516. goto fail;
  1517. }
  1518. /*
  1519. * We cannot trust fsuid as being the "true" uid of the
  1520. * process nor do we know its entire history. We only know it
  1521. * was tainted so we dump it as root in mode 2.
  1522. */
  1523. if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
  1524. flag = O_EXCL; /* Stop rewrite attacks */
  1525. cred->fsuid = 0; /* Dump root private */
  1526. }
  1527. retval = coredump_wait(exit_code, &core_state);
  1528. if (retval < 0) {
  1529. put_cred(cred);
  1530. goto fail;
  1531. }
  1532. old_cred = override_creds(cred);
  1533. /*
  1534. * Clear any false indication of pending signals that might
  1535. * be seen by the filesystem code called to write the core file.
  1536. */
  1537. clear_thread_flag(TIF_SIGPENDING);
  1538. /*
  1539. * lock_kernel() because format_corename() is controlled by sysctl, which
  1540. * uses lock_kernel()
  1541. */
  1542. lock_kernel();
  1543. ispipe = format_corename(corename, signr);
  1544. unlock_kernel();
  1545. /*
  1546. * Don't bother to check the RLIMIT_CORE value if core_pattern points
  1547. * to a pipe. Since we're not writing directly to the filesystem
  1548. * RLIMIT_CORE doesn't really apply, as no actual core file will be
  1549. * created unless the pipe reader choses to write out the core file
  1550. * at which point file size limits and permissions will be imposed
  1551. * as it does with any other process
  1552. */
  1553. if ((!ispipe) && (core_limit < binfmt->min_coredump))
  1554. goto fail_unlock;
  1555. if (ispipe) {
  1556. helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
  1557. if (!helper_argv) {
  1558. printk(KERN_WARNING "%s failed to allocate memory\n",
  1559. __func__);
  1560. goto fail_unlock;
  1561. }
  1562. /* Terminate the string before the first option */
  1563. delimit = strchr(corename, ' ');
  1564. if (delimit)
  1565. *delimit = '\0';
  1566. delimit = strrchr(helper_argv[0], '/');
  1567. if (delimit)
  1568. delimit++;
  1569. else
  1570. delimit = helper_argv[0];
  1571. if (!strcmp(delimit, current->comm)) {
  1572. printk(KERN_NOTICE "Recursive core dump detected, "
  1573. "aborting\n");
  1574. goto fail_unlock;
  1575. }
  1576. core_limit = RLIM_INFINITY;
  1577. /* SIGPIPE can happen, but it's just never processed */
  1578. if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
  1579. &file)) {
  1580. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1581. corename);
  1582. goto fail_unlock;
  1583. }
  1584. } else
  1585. file = filp_open(corename,
  1586. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1587. 0600);
  1588. if (IS_ERR(file))
  1589. goto fail_unlock;
  1590. inode = file->f_path.dentry->d_inode;
  1591. if (inode->i_nlink > 1)
  1592. goto close_fail; /* multiple links - don't dump */
  1593. if (!ispipe && d_unhashed(file->f_path.dentry))
  1594. goto close_fail;
  1595. /* AK: actually i see no reason to not allow this for named pipes etc.,
  1596. but keep the previous behaviour for now. */
  1597. if (!ispipe && !S_ISREG(inode->i_mode))
  1598. goto close_fail;
  1599. /*
  1600. * Dont allow local users get cute and trick others to coredump
  1601. * into their pre-created files:
  1602. */
  1603. if (inode->i_uid != current_fsuid())
  1604. goto close_fail;
  1605. if (!file->f_op)
  1606. goto close_fail;
  1607. if (!file->f_op->write)
  1608. goto close_fail;
  1609. if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
  1610. goto close_fail;
  1611. retval = binfmt->core_dump(signr, regs, file, core_limit);
  1612. if (retval)
  1613. current->signal->group_exit_code |= 0x80;
  1614. close_fail:
  1615. filp_close(file, NULL);
  1616. fail_unlock:
  1617. if (helper_argv)
  1618. argv_free(helper_argv);
  1619. revert_creds(old_cred);
  1620. put_cred(cred);
  1621. coredump_finish(mm);
  1622. fail:
  1623. return;
  1624. }