exec.c 43 KB

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