exec.c 51 KB

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