exec.c 53 KB

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