exec.c 43 KB

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