exec.c 55 KB

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