exec.c 45 KB

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