exec.c 51 KB

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