exec.c 46 KB

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