exec.c 47 KB

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