exec.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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/perf_counter.h>
  35. #include <linux/highmem.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/key.h>
  38. #include <linux/personality.h>
  39. #include <linux/binfmts.h>
  40. #include <linux/utsname.h>
  41. #include <linux/pid_namespace.h>
  42. #include <linux/module.h>
  43. #include <linux/namei.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/mount.h>
  46. #include <linux/security.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 <asm/uaccess.h>
  54. #include <asm/mmu_context.h>
  55. #include <asm/tlb.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. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) {
  858. suid_keys(current);
  859. set_dumpable(current->mm, suid_dumpable);
  860. current->pdeath_signal = 0;
  861. } else if (file_permission(bprm->file, MAY_READ) ||
  862. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  863. suid_keys(current);
  864. set_dumpable(current->mm, suid_dumpable);
  865. }
  866. /*
  867. * Flush performance counters when crossing a
  868. * security domain:
  869. */
  870. if (!get_dumpable(current->mm))
  871. perf_counter_exit_task(current);
  872. /* An exec changes our domain. We are no longer part of the thread
  873. group */
  874. current->self_exec_id++;
  875. flush_signal_handlers(current, 0);
  876. flush_old_files(current->files);
  877. return 0;
  878. out:
  879. return retval;
  880. }
  881. EXPORT_SYMBOL(flush_old_exec);
  882. /*
  883. * Fill the binprm structure from the inode.
  884. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  885. */
  886. int prepare_binprm(struct linux_binprm *bprm)
  887. {
  888. int mode;
  889. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  890. int retval;
  891. mode = inode->i_mode;
  892. if (bprm->file->f_op == NULL)
  893. return -EACCES;
  894. bprm->e_uid = current->euid;
  895. bprm->e_gid = current->egid;
  896. if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  897. /* Set-uid? */
  898. if (mode & S_ISUID) {
  899. current->personality &= ~PER_CLEAR_ON_SETID;
  900. bprm->e_uid = inode->i_uid;
  901. }
  902. /* Set-gid? */
  903. /*
  904. * If setgid is set but no group execute bit then this
  905. * is a candidate for mandatory locking, not a setgid
  906. * executable.
  907. */
  908. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  909. current->personality &= ~PER_CLEAR_ON_SETID;
  910. bprm->e_gid = inode->i_gid;
  911. }
  912. }
  913. /* fill in binprm security blob */
  914. retval = security_bprm_set(bprm);
  915. if (retval)
  916. return retval;
  917. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  918. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  919. }
  920. EXPORT_SYMBOL(prepare_binprm);
  921. static int unsafe_exec(struct task_struct *p)
  922. {
  923. int unsafe = tracehook_unsafe_exec(p);
  924. if (atomic_read(&p->fs->count) > 1 ||
  925. atomic_read(&p->files->count) > 1 ||
  926. atomic_read(&p->sighand->count) > 1)
  927. unsafe |= LSM_UNSAFE_SHARE;
  928. return unsafe;
  929. }
  930. void compute_creds(struct linux_binprm *bprm)
  931. {
  932. int unsafe;
  933. if (bprm->e_uid != current->uid) {
  934. suid_keys(current);
  935. current->pdeath_signal = 0;
  936. }
  937. exec_keys(current);
  938. task_lock(current);
  939. unsafe = unsafe_exec(current);
  940. security_bprm_apply_creds(bprm, unsafe);
  941. task_unlock(current);
  942. security_bprm_post_apply_creds(bprm);
  943. }
  944. EXPORT_SYMBOL(compute_creds);
  945. /*
  946. * Arguments are '\0' separated strings found at the location bprm->p
  947. * points to; chop off the first by relocating brpm->p to right after
  948. * the first '\0' encountered.
  949. */
  950. int remove_arg_zero(struct linux_binprm *bprm)
  951. {
  952. int ret = 0;
  953. unsigned long offset;
  954. char *kaddr;
  955. struct page *page;
  956. if (!bprm->argc)
  957. return 0;
  958. do {
  959. offset = bprm->p & ~PAGE_MASK;
  960. page = get_arg_page(bprm, bprm->p, 0);
  961. if (!page) {
  962. ret = -EFAULT;
  963. goto out;
  964. }
  965. kaddr = kmap_atomic(page, KM_USER0);
  966. for (; offset < PAGE_SIZE && kaddr[offset];
  967. offset++, bprm->p++)
  968. ;
  969. kunmap_atomic(kaddr, KM_USER0);
  970. put_arg_page(page);
  971. if (offset == PAGE_SIZE)
  972. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  973. } while (offset == PAGE_SIZE);
  974. bprm->p++;
  975. bprm->argc--;
  976. ret = 0;
  977. out:
  978. return ret;
  979. }
  980. EXPORT_SYMBOL(remove_arg_zero);
  981. /*
  982. * cycle the list of binary formats handler, until one recognizes the image
  983. */
  984. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  985. {
  986. unsigned int depth = bprm->recursion_depth;
  987. int try,retval;
  988. struct linux_binfmt *fmt;
  989. #ifdef __alpha__
  990. /* handle /sbin/loader.. */
  991. {
  992. struct exec * eh = (struct exec *) bprm->buf;
  993. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  994. (eh->fh.f_flags & 0x3000) == 0x3000)
  995. {
  996. struct file * file;
  997. unsigned long loader;
  998. allow_write_access(bprm->file);
  999. fput(bprm->file);
  1000. bprm->file = NULL;
  1001. loader = bprm->vma->vm_end - sizeof(void *);
  1002. file = open_exec("/sbin/loader");
  1003. retval = PTR_ERR(file);
  1004. if (IS_ERR(file))
  1005. return retval;
  1006. /* Remember if the application is TASO. */
  1007. bprm->taso = eh->ah.entry < 0x100000000UL;
  1008. bprm->file = file;
  1009. bprm->loader = loader;
  1010. retval = prepare_binprm(bprm);
  1011. if (retval<0)
  1012. return retval;
  1013. /* should call search_binary_handler recursively here,
  1014. but it does not matter */
  1015. }
  1016. }
  1017. #endif
  1018. retval = security_bprm_check(bprm);
  1019. if (retval)
  1020. return retval;
  1021. /* kernel module loader fixup */
  1022. /* so we don't try to load run modprobe in kernel space. */
  1023. set_fs(USER_DS);
  1024. retval = audit_bprm(bprm);
  1025. if (retval)
  1026. return retval;
  1027. retval = -ENOENT;
  1028. for (try=0; try<2; try++) {
  1029. read_lock(&binfmt_lock);
  1030. list_for_each_entry(fmt, &formats, lh) {
  1031. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1032. if (!fn)
  1033. continue;
  1034. if (!try_module_get(fmt->module))
  1035. continue;
  1036. read_unlock(&binfmt_lock);
  1037. retval = fn(bprm, regs);
  1038. /*
  1039. * Restore the depth counter to its starting value
  1040. * in this call, so we don't have to rely on every
  1041. * load_binary function to restore it on return.
  1042. */
  1043. bprm->recursion_depth = depth;
  1044. if (retval >= 0) {
  1045. if (depth == 0)
  1046. tracehook_report_exec(fmt, bprm, regs);
  1047. put_binfmt(fmt);
  1048. allow_write_access(bprm->file);
  1049. if (bprm->file)
  1050. fput(bprm->file);
  1051. bprm->file = NULL;
  1052. current->did_exec = 1;
  1053. proc_exec_connector(current);
  1054. return retval;
  1055. }
  1056. read_lock(&binfmt_lock);
  1057. put_binfmt(fmt);
  1058. if (retval != -ENOEXEC || bprm->mm == NULL)
  1059. break;
  1060. if (!bprm->file) {
  1061. read_unlock(&binfmt_lock);
  1062. return retval;
  1063. }
  1064. }
  1065. read_unlock(&binfmt_lock);
  1066. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1067. break;
  1068. #ifdef CONFIG_MODULES
  1069. } else {
  1070. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1071. if (printable(bprm->buf[0]) &&
  1072. printable(bprm->buf[1]) &&
  1073. printable(bprm->buf[2]) &&
  1074. printable(bprm->buf[3]))
  1075. break; /* -ENOEXEC */
  1076. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1077. #endif
  1078. }
  1079. }
  1080. return retval;
  1081. }
  1082. EXPORT_SYMBOL(search_binary_handler);
  1083. void free_bprm(struct linux_binprm *bprm)
  1084. {
  1085. free_arg_pages(bprm);
  1086. kfree(bprm);
  1087. }
  1088. /*
  1089. * sys_execve() executes a new program.
  1090. */
  1091. int do_execve(char * filename,
  1092. char __user *__user *argv,
  1093. char __user *__user *envp,
  1094. struct pt_regs * regs)
  1095. {
  1096. struct linux_binprm *bprm;
  1097. struct file *file;
  1098. struct files_struct *displaced;
  1099. int retval;
  1100. retval = unshare_files(&displaced);
  1101. if (retval)
  1102. goto out_ret;
  1103. retval = -ENOMEM;
  1104. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1105. if (!bprm)
  1106. goto out_files;
  1107. file = open_exec(filename);
  1108. retval = PTR_ERR(file);
  1109. if (IS_ERR(file))
  1110. goto out_kfree;
  1111. sched_exec();
  1112. bprm->file = file;
  1113. bprm->filename = filename;
  1114. bprm->interp = filename;
  1115. retval = bprm_mm_init(bprm);
  1116. if (retval)
  1117. goto out_file;
  1118. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1119. if ((retval = bprm->argc) < 0)
  1120. goto out_mm;
  1121. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1122. if ((retval = bprm->envc) < 0)
  1123. goto out_mm;
  1124. retval = security_bprm_alloc(bprm);
  1125. if (retval)
  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. /* execve success */
  1144. security_bprm_free(bprm);
  1145. acct_update_integrals(current);
  1146. free_bprm(bprm);
  1147. if (displaced)
  1148. put_files_struct(displaced);
  1149. return retval;
  1150. }
  1151. out:
  1152. if (bprm->security)
  1153. security_bprm_free(bprm);
  1154. out_mm:
  1155. if (bprm->mm)
  1156. mmput (bprm->mm);
  1157. out_file:
  1158. if (bprm->file) {
  1159. allow_write_access(bprm->file);
  1160. fput(bprm->file);
  1161. }
  1162. out_kfree:
  1163. free_bprm(bprm);
  1164. out_files:
  1165. if (displaced)
  1166. reset_files_struct(displaced);
  1167. out_ret:
  1168. return retval;
  1169. }
  1170. int set_binfmt(struct linux_binfmt *new)
  1171. {
  1172. struct linux_binfmt *old = current->binfmt;
  1173. if (new) {
  1174. if (!try_module_get(new->module))
  1175. return -1;
  1176. }
  1177. current->binfmt = new;
  1178. if (old)
  1179. module_put(old->module);
  1180. return 0;
  1181. }
  1182. EXPORT_SYMBOL(set_binfmt);
  1183. /* format_corename will inspect the pattern parameter, and output a
  1184. * name into corename, which must have space for at least
  1185. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1186. */
  1187. static int format_corename(char *corename, long signr)
  1188. {
  1189. const char *pat_ptr = core_pattern;
  1190. int ispipe = (*pat_ptr == '|');
  1191. char *out_ptr = corename;
  1192. char *const out_end = corename + CORENAME_MAX_SIZE;
  1193. int rc;
  1194. int pid_in_pattern = 0;
  1195. /* Repeat as long as we have more pattern to process and more output
  1196. space */
  1197. while (*pat_ptr) {
  1198. if (*pat_ptr != '%') {
  1199. if (out_ptr == out_end)
  1200. goto out;
  1201. *out_ptr++ = *pat_ptr++;
  1202. } else {
  1203. switch (*++pat_ptr) {
  1204. case 0:
  1205. goto out;
  1206. /* Double percent, output one percent */
  1207. case '%':
  1208. if (out_ptr == out_end)
  1209. goto out;
  1210. *out_ptr++ = '%';
  1211. break;
  1212. /* pid */
  1213. case 'p':
  1214. pid_in_pattern = 1;
  1215. rc = snprintf(out_ptr, out_end - out_ptr,
  1216. "%d", task_tgid_vnr(current));
  1217. if (rc > out_end - out_ptr)
  1218. goto out;
  1219. out_ptr += rc;
  1220. break;
  1221. /* uid */
  1222. case 'u':
  1223. rc = snprintf(out_ptr, out_end - out_ptr,
  1224. "%d", current->uid);
  1225. if (rc > out_end - out_ptr)
  1226. goto out;
  1227. out_ptr += rc;
  1228. break;
  1229. /* gid */
  1230. case 'g':
  1231. rc = snprintf(out_ptr, out_end - out_ptr,
  1232. "%d", current->gid);
  1233. if (rc > out_end - out_ptr)
  1234. goto out;
  1235. out_ptr += rc;
  1236. break;
  1237. /* signal that caused the coredump */
  1238. case 's':
  1239. rc = snprintf(out_ptr, out_end - out_ptr,
  1240. "%ld", signr);
  1241. if (rc > out_end - out_ptr)
  1242. goto out;
  1243. out_ptr += rc;
  1244. break;
  1245. /* UNIX time of coredump */
  1246. case 't': {
  1247. struct timeval tv;
  1248. do_gettimeofday(&tv);
  1249. rc = snprintf(out_ptr, out_end - out_ptr,
  1250. "%lu", tv.tv_sec);
  1251. if (rc > out_end - out_ptr)
  1252. goto out;
  1253. out_ptr += rc;
  1254. break;
  1255. }
  1256. /* hostname */
  1257. case 'h':
  1258. down_read(&uts_sem);
  1259. rc = snprintf(out_ptr, out_end - out_ptr,
  1260. "%s", utsname()->nodename);
  1261. up_read(&uts_sem);
  1262. if (rc > out_end - out_ptr)
  1263. goto out;
  1264. out_ptr += rc;
  1265. break;
  1266. /* executable */
  1267. case 'e':
  1268. rc = snprintf(out_ptr, out_end - out_ptr,
  1269. "%s", current->comm);
  1270. if (rc > out_end - out_ptr)
  1271. goto out;
  1272. out_ptr += rc;
  1273. break;
  1274. /* core limit size */
  1275. case 'c':
  1276. rc = snprintf(out_ptr, out_end - out_ptr,
  1277. "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
  1278. if (rc > out_end - out_ptr)
  1279. goto out;
  1280. out_ptr += rc;
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. ++pat_ptr;
  1286. }
  1287. }
  1288. /* Backward compatibility with core_uses_pid:
  1289. *
  1290. * If core_pattern does not include a %p (as is the default)
  1291. * and core_uses_pid is set, then .%pid will be appended to
  1292. * the filename. Do not do this for piped commands. */
  1293. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  1294. rc = snprintf(out_ptr, out_end - out_ptr,
  1295. ".%d", task_tgid_vnr(current));
  1296. if (rc > out_end - out_ptr)
  1297. goto out;
  1298. out_ptr += rc;
  1299. }
  1300. out:
  1301. *out_ptr = 0;
  1302. return ispipe;
  1303. }
  1304. static int zap_process(struct task_struct *start)
  1305. {
  1306. struct task_struct *t;
  1307. int nr = 0;
  1308. start->signal->flags = SIGNAL_GROUP_EXIT;
  1309. start->signal->group_stop_count = 0;
  1310. t = start;
  1311. do {
  1312. if (t != current && t->mm) {
  1313. sigaddset(&t->pending.signal, SIGKILL);
  1314. signal_wake_up(t, 1);
  1315. nr++;
  1316. }
  1317. } while_each_thread(start, t);
  1318. return nr;
  1319. }
  1320. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1321. struct core_state *core_state, int exit_code)
  1322. {
  1323. struct task_struct *g, *p;
  1324. unsigned long flags;
  1325. int nr = -EAGAIN;
  1326. spin_lock_irq(&tsk->sighand->siglock);
  1327. if (!signal_group_exit(tsk->signal)) {
  1328. mm->core_state = core_state;
  1329. tsk->signal->group_exit_code = exit_code;
  1330. nr = zap_process(tsk);
  1331. }
  1332. spin_unlock_irq(&tsk->sighand->siglock);
  1333. if (unlikely(nr < 0))
  1334. return nr;
  1335. if (atomic_read(&mm->mm_users) == nr + 1)
  1336. goto done;
  1337. /*
  1338. * We should find and kill all tasks which use this mm, and we should
  1339. * count them correctly into ->nr_threads. We don't take tasklist
  1340. * lock, but this is safe wrt:
  1341. *
  1342. * fork:
  1343. * None of sub-threads can fork after zap_process(leader). All
  1344. * processes which were created before this point should be
  1345. * visible to zap_threads() because copy_process() adds the new
  1346. * process to the tail of init_task.tasks list, and lock/unlock
  1347. * of ->siglock provides a memory barrier.
  1348. *
  1349. * do_exit:
  1350. * The caller holds mm->mmap_sem. This means that the task which
  1351. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1352. * its ->mm.
  1353. *
  1354. * de_thread:
  1355. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1356. * we must see either old or new leader, this does not matter.
  1357. * However, it can change p->sighand, so lock_task_sighand(p)
  1358. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1359. * it can't fail.
  1360. *
  1361. * Note also that "g" can be the old leader with ->mm == NULL
  1362. * and already unhashed and thus removed from ->thread_group.
  1363. * This is OK, __unhash_process()->list_del_rcu() does not
  1364. * clear the ->next pointer, we will find the new leader via
  1365. * next_thread().
  1366. */
  1367. rcu_read_lock();
  1368. for_each_process(g) {
  1369. if (g == tsk->group_leader)
  1370. continue;
  1371. if (g->flags & PF_KTHREAD)
  1372. continue;
  1373. p = g;
  1374. do {
  1375. if (p->mm) {
  1376. if (unlikely(p->mm == mm)) {
  1377. lock_task_sighand(p, &flags);
  1378. nr += zap_process(p);
  1379. unlock_task_sighand(p, &flags);
  1380. }
  1381. break;
  1382. }
  1383. } while_each_thread(g, p);
  1384. }
  1385. rcu_read_unlock();
  1386. done:
  1387. atomic_set(&core_state->nr_threads, nr);
  1388. return nr;
  1389. }
  1390. static int coredump_wait(int exit_code, struct core_state *core_state)
  1391. {
  1392. struct task_struct *tsk = current;
  1393. struct mm_struct *mm = tsk->mm;
  1394. struct completion *vfork_done;
  1395. int core_waiters;
  1396. init_completion(&core_state->startup);
  1397. core_state->dumper.task = tsk;
  1398. core_state->dumper.next = NULL;
  1399. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1400. up_write(&mm->mmap_sem);
  1401. if (unlikely(core_waiters < 0))
  1402. goto fail;
  1403. /*
  1404. * Make sure nobody is waiting for us to release the VM,
  1405. * otherwise we can deadlock when we wait on each other
  1406. */
  1407. vfork_done = tsk->vfork_done;
  1408. if (vfork_done) {
  1409. tsk->vfork_done = NULL;
  1410. complete(vfork_done);
  1411. }
  1412. if (core_waiters)
  1413. wait_for_completion(&core_state->startup);
  1414. fail:
  1415. return core_waiters;
  1416. }
  1417. static void coredump_finish(struct mm_struct *mm)
  1418. {
  1419. struct core_thread *curr, *next;
  1420. struct task_struct *task;
  1421. next = mm->core_state->dumper.next;
  1422. while ((curr = next) != NULL) {
  1423. next = curr->next;
  1424. task = curr->task;
  1425. /*
  1426. * see exit_mm(), curr->task must not see
  1427. * ->task == NULL before we read ->next.
  1428. */
  1429. smp_mb();
  1430. curr->task = NULL;
  1431. wake_up_process(task);
  1432. }
  1433. mm->core_state = NULL;
  1434. }
  1435. /*
  1436. * set_dumpable converts traditional three-value dumpable to two flags and
  1437. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1438. * these bits are not changed atomically. So get_dumpable can observe the
  1439. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1440. * return either old dumpable or new one by paying attention to the order of
  1441. * modifying the bits.
  1442. *
  1443. * dumpable | mm->flags (binary)
  1444. * old new | initial interim final
  1445. * ---------+-----------------------
  1446. * 0 1 | 00 01 01
  1447. * 0 2 | 00 10(*) 11
  1448. * 1 0 | 01 00 00
  1449. * 1 2 | 01 11 11
  1450. * 2 0 | 11 10(*) 00
  1451. * 2 1 | 11 11 01
  1452. *
  1453. * (*) get_dumpable regards interim value of 10 as 11.
  1454. */
  1455. void set_dumpable(struct mm_struct *mm, int value)
  1456. {
  1457. switch (value) {
  1458. case 0:
  1459. clear_bit(MMF_DUMPABLE, &mm->flags);
  1460. smp_wmb();
  1461. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1462. break;
  1463. case 1:
  1464. set_bit(MMF_DUMPABLE, &mm->flags);
  1465. smp_wmb();
  1466. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1467. break;
  1468. case 2:
  1469. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1470. smp_wmb();
  1471. set_bit(MMF_DUMPABLE, &mm->flags);
  1472. break;
  1473. }
  1474. }
  1475. int get_dumpable(struct mm_struct *mm)
  1476. {
  1477. int ret;
  1478. ret = mm->flags & 0x3;
  1479. return (ret >= 2) ? 2 : ret;
  1480. }
  1481. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1482. {
  1483. struct core_state core_state;
  1484. char corename[CORENAME_MAX_SIZE + 1];
  1485. struct mm_struct *mm = current->mm;
  1486. struct linux_binfmt * binfmt;
  1487. struct inode * inode;
  1488. struct file * file;
  1489. int retval = 0;
  1490. int fsuid = current->fsuid;
  1491. int flag = 0;
  1492. int ispipe = 0;
  1493. unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1494. char **helper_argv = NULL;
  1495. int helper_argc = 0;
  1496. char *delimit;
  1497. audit_core_dumps(signr);
  1498. binfmt = current->binfmt;
  1499. if (!binfmt || !binfmt->core_dump)
  1500. goto fail;
  1501. down_write(&mm->mmap_sem);
  1502. /*
  1503. * If another thread got here first, or we are not dumpable, bail out.
  1504. */
  1505. if (mm->core_state || !get_dumpable(mm)) {
  1506. up_write(&mm->mmap_sem);
  1507. goto fail;
  1508. }
  1509. /*
  1510. * We cannot trust fsuid as being the "true" uid of the
  1511. * process nor do we know its entire history. We only know it
  1512. * was tainted so we dump it as root in mode 2.
  1513. */
  1514. if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
  1515. flag = O_EXCL; /* Stop rewrite attacks */
  1516. current->fsuid = 0; /* Dump root private */
  1517. }
  1518. retval = coredump_wait(exit_code, &core_state);
  1519. if (retval < 0)
  1520. goto fail;
  1521. /*
  1522. * Clear any false indication of pending signals that might
  1523. * be seen by the filesystem code called to write the core file.
  1524. */
  1525. clear_thread_flag(TIF_SIGPENDING);
  1526. /*
  1527. * lock_kernel() because format_corename() is controlled by sysctl, which
  1528. * uses lock_kernel()
  1529. */
  1530. lock_kernel();
  1531. ispipe = format_corename(corename, signr);
  1532. unlock_kernel();
  1533. /*
  1534. * Don't bother to check the RLIMIT_CORE value if core_pattern points
  1535. * to a pipe. Since we're not writing directly to the filesystem
  1536. * RLIMIT_CORE doesn't really apply, as no actual core file will be
  1537. * created unless the pipe reader choses to write out the core file
  1538. * at which point file size limits and permissions will be imposed
  1539. * as it does with any other process
  1540. */
  1541. if ((!ispipe) && (core_limit < binfmt->min_coredump))
  1542. goto fail_unlock;
  1543. if (ispipe) {
  1544. helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
  1545. /* Terminate the string before the first option */
  1546. delimit = strchr(corename, ' ');
  1547. if (delimit)
  1548. *delimit = '\0';
  1549. delimit = strrchr(helper_argv[0], '/');
  1550. if (delimit)
  1551. delimit++;
  1552. else
  1553. delimit = helper_argv[0];
  1554. if (!strcmp(delimit, current->comm)) {
  1555. printk(KERN_NOTICE "Recursive core dump detected, "
  1556. "aborting\n");
  1557. goto fail_unlock;
  1558. }
  1559. core_limit = RLIM_INFINITY;
  1560. /* SIGPIPE can happen, but it's just never processed */
  1561. if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
  1562. &file)) {
  1563. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1564. corename);
  1565. goto fail_unlock;
  1566. }
  1567. } else
  1568. file = filp_open(corename,
  1569. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1570. 0600);
  1571. if (IS_ERR(file))
  1572. goto fail_unlock;
  1573. inode = file->f_path.dentry->d_inode;
  1574. if (inode->i_nlink > 1)
  1575. goto close_fail; /* multiple links - don't dump */
  1576. if (!ispipe && d_unhashed(file->f_path.dentry))
  1577. goto close_fail;
  1578. /* AK: actually i see no reason to not allow this for named pipes etc.,
  1579. but keep the previous behaviour for now. */
  1580. if (!ispipe && !S_ISREG(inode->i_mode))
  1581. goto close_fail;
  1582. /*
  1583. * Dont allow local users get cute and trick others to coredump
  1584. * into their pre-created files:
  1585. */
  1586. if (inode->i_uid != current->fsuid)
  1587. goto close_fail;
  1588. if (!file->f_op)
  1589. goto close_fail;
  1590. if (!file->f_op->write)
  1591. goto close_fail;
  1592. if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
  1593. goto close_fail;
  1594. retval = binfmt->core_dump(signr, regs, file, core_limit);
  1595. if (retval)
  1596. current->signal->group_exit_code |= 0x80;
  1597. close_fail:
  1598. filp_close(file, NULL);
  1599. fail_unlock:
  1600. if (helper_argv)
  1601. argv_free(helper_argv);
  1602. current->fsuid = fsuid;
  1603. coredump_finish(mm);
  1604. fail:
  1605. return retval;
  1606. }