exec.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  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. sync_mm_rss(old_mm);
  691. mm_release(tsk, old_mm);
  692. if (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. /*
  852. * These functions flushes out all traces of the currently running executable
  853. * so that a new one can be started
  854. */
  855. static void flush_old_files(struct files_struct * files)
  856. {
  857. long j = -1;
  858. struct fdtable *fdt;
  859. spin_lock(&files->file_lock);
  860. for (;;) {
  861. unsigned long set, i;
  862. j++;
  863. i = j * __NFDBITS;
  864. fdt = files_fdtable(files);
  865. if (i >= fdt->max_fds)
  866. break;
  867. set = fdt->close_on_exec[j];
  868. if (!set)
  869. continue;
  870. fdt->close_on_exec[j] = 0;
  871. spin_unlock(&files->file_lock);
  872. for ( ; set ; i++,set >>= 1) {
  873. if (set & 1) {
  874. sys_close(i);
  875. }
  876. }
  877. spin_lock(&files->file_lock);
  878. }
  879. spin_unlock(&files->file_lock);
  880. }
  881. char *get_task_comm(char *buf, struct task_struct *tsk)
  882. {
  883. /* buf must be at least sizeof(tsk->comm) in size */
  884. task_lock(tsk);
  885. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  886. task_unlock(tsk);
  887. return buf;
  888. }
  889. EXPORT_SYMBOL_GPL(get_task_comm);
  890. void set_task_comm(struct task_struct *tsk, char *buf)
  891. {
  892. task_lock(tsk);
  893. trace_task_rename(tsk, buf);
  894. /*
  895. * Threads may access current->comm without holding
  896. * the task lock, so write the string carefully.
  897. * Readers without a lock may see incomplete new
  898. * names but are safe from non-terminating string reads.
  899. */
  900. memset(tsk->comm, 0, TASK_COMM_LEN);
  901. wmb();
  902. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  903. task_unlock(tsk);
  904. perf_event_comm(tsk);
  905. }
  906. static void filename_to_taskname(char *tcomm, const char *fn, unsigned int len)
  907. {
  908. int i, ch;
  909. /* Copies the binary name from after last slash */
  910. for (i = 0; (ch = *(fn++)) != '\0';) {
  911. if (ch == '/')
  912. i = 0; /* overwrite what we wrote */
  913. else
  914. if (i < len - 1)
  915. tcomm[i++] = ch;
  916. }
  917. tcomm[i] = '\0';
  918. }
  919. int flush_old_exec(struct linux_binprm * bprm)
  920. {
  921. int retval;
  922. /*
  923. * Make sure we have a private signal table and that
  924. * we are unassociated from the previous thread group.
  925. */
  926. retval = de_thread(current);
  927. if (retval)
  928. goto out;
  929. set_mm_exe_file(bprm->mm, bprm->file);
  930. filename_to_taskname(bprm->tcomm, bprm->filename, sizeof(bprm->tcomm));
  931. /*
  932. * Release all of the old mmap stuff
  933. */
  934. acct_arg_size(bprm, 0);
  935. retval = exec_mmap(bprm->mm);
  936. if (retval)
  937. goto out;
  938. bprm->mm = NULL; /* We're using it now */
  939. set_fs(USER_DS);
  940. current->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD);
  941. flush_thread();
  942. current->personality &= ~bprm->per_clear;
  943. return 0;
  944. out:
  945. return retval;
  946. }
  947. EXPORT_SYMBOL(flush_old_exec);
  948. void would_dump(struct linux_binprm *bprm, struct file *file)
  949. {
  950. if (inode_permission(file->f_path.dentry->d_inode, MAY_READ) < 0)
  951. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  952. }
  953. EXPORT_SYMBOL(would_dump);
  954. void setup_new_exec(struct linux_binprm * bprm)
  955. {
  956. arch_pick_mmap_layout(current->mm);
  957. /* This is the point of no return */
  958. current->sas_ss_sp = current->sas_ss_size = 0;
  959. if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
  960. set_dumpable(current->mm, 1);
  961. else
  962. set_dumpable(current->mm, suid_dumpable);
  963. set_task_comm(current, bprm->tcomm);
  964. /* Set the new mm task size. We have to do that late because it may
  965. * depend on TIF_32BIT which is only updated in flush_thread() on
  966. * some architectures like powerpc
  967. */
  968. current->mm->task_size = TASK_SIZE;
  969. /* install the new credentials */
  970. if (!uid_eq(bprm->cred->uid, current_euid()) ||
  971. !gid_eq(bprm->cred->gid, current_egid())) {
  972. current->pdeath_signal = 0;
  973. } else {
  974. would_dump(bprm, bprm->file);
  975. if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
  976. set_dumpable(current->mm, suid_dumpable);
  977. }
  978. /*
  979. * Flush performance counters when crossing a
  980. * security domain:
  981. */
  982. if (!get_dumpable(current->mm))
  983. perf_event_exit_task(current);
  984. /* An exec changes our domain. We are no longer part of the thread
  985. group */
  986. current->self_exec_id++;
  987. flush_signal_handlers(current, 0);
  988. flush_old_files(current->files);
  989. }
  990. EXPORT_SYMBOL(setup_new_exec);
  991. /*
  992. * Prepare credentials and lock ->cred_guard_mutex.
  993. * install_exec_creds() commits the new creds and drops the lock.
  994. * Or, if exec fails before, free_bprm() should release ->cred and
  995. * and unlock.
  996. */
  997. int prepare_bprm_creds(struct linux_binprm *bprm)
  998. {
  999. if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
  1000. return -ERESTARTNOINTR;
  1001. bprm->cred = prepare_exec_creds();
  1002. if (likely(bprm->cred))
  1003. return 0;
  1004. mutex_unlock(&current->signal->cred_guard_mutex);
  1005. return -ENOMEM;
  1006. }
  1007. void free_bprm(struct linux_binprm *bprm)
  1008. {
  1009. free_arg_pages(bprm);
  1010. if (bprm->cred) {
  1011. mutex_unlock(&current->signal->cred_guard_mutex);
  1012. abort_creds(bprm->cred);
  1013. }
  1014. kfree(bprm);
  1015. }
  1016. /*
  1017. * install the new credentials for this executable
  1018. */
  1019. void install_exec_creds(struct linux_binprm *bprm)
  1020. {
  1021. security_bprm_committing_creds(bprm);
  1022. commit_creds(bprm->cred);
  1023. bprm->cred = NULL;
  1024. /*
  1025. * cred_guard_mutex must be held at least to this point to prevent
  1026. * ptrace_attach() from altering our determination of the task's
  1027. * credentials; any time after this it may be unlocked.
  1028. */
  1029. security_bprm_committed_creds(bprm);
  1030. mutex_unlock(&current->signal->cred_guard_mutex);
  1031. }
  1032. EXPORT_SYMBOL(install_exec_creds);
  1033. /*
  1034. * determine how safe it is to execute the proposed program
  1035. * - the caller must hold ->cred_guard_mutex to protect against
  1036. * PTRACE_ATTACH
  1037. */
  1038. static int check_unsafe_exec(struct linux_binprm *bprm)
  1039. {
  1040. struct task_struct *p = current, *t;
  1041. unsigned n_fs;
  1042. int res = 0;
  1043. if (p->ptrace) {
  1044. if (p->ptrace & PT_PTRACE_CAP)
  1045. bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
  1046. else
  1047. bprm->unsafe |= LSM_UNSAFE_PTRACE;
  1048. }
  1049. /*
  1050. * This isn't strictly necessary, but it makes it harder for LSMs to
  1051. * mess up.
  1052. */
  1053. if (current->no_new_privs)
  1054. bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
  1055. n_fs = 1;
  1056. spin_lock(&p->fs->lock);
  1057. rcu_read_lock();
  1058. for (t = next_thread(p); t != p; t = next_thread(t)) {
  1059. if (t->fs == p->fs)
  1060. n_fs++;
  1061. }
  1062. rcu_read_unlock();
  1063. if (p->fs->users > n_fs) {
  1064. bprm->unsafe |= LSM_UNSAFE_SHARE;
  1065. } else {
  1066. res = -EAGAIN;
  1067. if (!p->fs->in_exec) {
  1068. p->fs->in_exec = 1;
  1069. res = 1;
  1070. }
  1071. }
  1072. spin_unlock(&p->fs->lock);
  1073. return res;
  1074. }
  1075. /*
  1076. * Fill the binprm structure from the inode.
  1077. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  1078. *
  1079. * This may be called multiple times for binary chains (scripts for example).
  1080. */
  1081. int prepare_binprm(struct linux_binprm *bprm)
  1082. {
  1083. umode_t mode;
  1084. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  1085. int retval;
  1086. mode = inode->i_mode;
  1087. if (bprm->file->f_op == NULL)
  1088. return -EACCES;
  1089. /* clear any previous set[ug]id data from a previous binary */
  1090. bprm->cred->euid = current_euid();
  1091. bprm->cred->egid = current_egid();
  1092. if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
  1093. !current->no_new_privs) {
  1094. /* Set-uid? */
  1095. if (mode & S_ISUID) {
  1096. if (!kuid_has_mapping(bprm->cred->user_ns, inode->i_uid))
  1097. return -EPERM;
  1098. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1099. bprm->cred->euid = inode->i_uid;
  1100. }
  1101. /* Set-gid? */
  1102. /*
  1103. * If setgid is set but no group execute bit then this
  1104. * is a candidate for mandatory locking, not a setgid
  1105. * executable.
  1106. */
  1107. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1108. if (!kgid_has_mapping(bprm->cred->user_ns, inode->i_gid))
  1109. return -EPERM;
  1110. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1111. bprm->cred->egid = inode->i_gid;
  1112. }
  1113. }
  1114. /* fill in binprm security blob */
  1115. retval = security_bprm_set_creds(bprm);
  1116. if (retval)
  1117. return retval;
  1118. bprm->cred_prepared = 1;
  1119. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  1120. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  1121. }
  1122. EXPORT_SYMBOL(prepare_binprm);
  1123. /*
  1124. * Arguments are '\0' separated strings found at the location bprm->p
  1125. * points to; chop off the first by relocating brpm->p to right after
  1126. * the first '\0' encountered.
  1127. */
  1128. int remove_arg_zero(struct linux_binprm *bprm)
  1129. {
  1130. int ret = 0;
  1131. unsigned long offset;
  1132. char *kaddr;
  1133. struct page *page;
  1134. if (!bprm->argc)
  1135. return 0;
  1136. do {
  1137. offset = bprm->p & ~PAGE_MASK;
  1138. page = get_arg_page(bprm, bprm->p, 0);
  1139. if (!page) {
  1140. ret = -EFAULT;
  1141. goto out;
  1142. }
  1143. kaddr = kmap_atomic(page);
  1144. for (; offset < PAGE_SIZE && kaddr[offset];
  1145. offset++, bprm->p++)
  1146. ;
  1147. kunmap_atomic(kaddr);
  1148. put_arg_page(page);
  1149. if (offset == PAGE_SIZE)
  1150. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  1151. } while (offset == PAGE_SIZE);
  1152. bprm->p++;
  1153. bprm->argc--;
  1154. ret = 0;
  1155. out:
  1156. return ret;
  1157. }
  1158. EXPORT_SYMBOL(remove_arg_zero);
  1159. /*
  1160. * cycle the list of binary formats handler, until one recognizes the image
  1161. */
  1162. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  1163. {
  1164. unsigned int depth = bprm->recursion_depth;
  1165. int try,retval;
  1166. struct linux_binfmt *fmt;
  1167. pid_t old_pid, old_vpid;
  1168. retval = security_bprm_check(bprm);
  1169. if (retval)
  1170. return retval;
  1171. retval = audit_bprm(bprm);
  1172. if (retval)
  1173. return retval;
  1174. /* Need to fetch pid before load_binary changes it */
  1175. old_pid = current->pid;
  1176. rcu_read_lock();
  1177. old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
  1178. rcu_read_unlock();
  1179. retval = -ENOENT;
  1180. for (try=0; try<2; try++) {
  1181. read_lock(&binfmt_lock);
  1182. list_for_each_entry(fmt, &formats, lh) {
  1183. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1184. if (!fn)
  1185. continue;
  1186. if (!try_module_get(fmt->module))
  1187. continue;
  1188. read_unlock(&binfmt_lock);
  1189. retval = fn(bprm, regs);
  1190. /*
  1191. * Restore the depth counter to its starting value
  1192. * in this call, so we don't have to rely on every
  1193. * load_binary function to restore it on return.
  1194. */
  1195. bprm->recursion_depth = depth;
  1196. if (retval >= 0) {
  1197. if (depth == 0) {
  1198. trace_sched_process_exec(current, old_pid, bprm);
  1199. ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
  1200. }
  1201. put_binfmt(fmt);
  1202. allow_write_access(bprm->file);
  1203. if (bprm->file)
  1204. fput(bprm->file);
  1205. bprm->file = NULL;
  1206. current->did_exec = 1;
  1207. proc_exec_connector(current);
  1208. return retval;
  1209. }
  1210. read_lock(&binfmt_lock);
  1211. put_binfmt(fmt);
  1212. if (retval != -ENOEXEC || bprm->mm == NULL)
  1213. break;
  1214. if (!bprm->file) {
  1215. read_unlock(&binfmt_lock);
  1216. return retval;
  1217. }
  1218. }
  1219. read_unlock(&binfmt_lock);
  1220. #ifdef CONFIG_MODULES
  1221. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1222. break;
  1223. } else {
  1224. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1225. if (printable(bprm->buf[0]) &&
  1226. printable(bprm->buf[1]) &&
  1227. printable(bprm->buf[2]) &&
  1228. printable(bprm->buf[3]))
  1229. break; /* -ENOEXEC */
  1230. if (try)
  1231. break; /* -ENOEXEC */
  1232. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1233. }
  1234. #else
  1235. break;
  1236. #endif
  1237. }
  1238. return retval;
  1239. }
  1240. EXPORT_SYMBOL(search_binary_handler);
  1241. /*
  1242. * sys_execve() executes a new program.
  1243. */
  1244. static int do_execve_common(const char *filename,
  1245. struct user_arg_ptr argv,
  1246. struct user_arg_ptr envp,
  1247. struct pt_regs *regs)
  1248. {
  1249. struct linux_binprm *bprm;
  1250. struct file *file;
  1251. struct files_struct *displaced;
  1252. bool clear_in_exec;
  1253. int retval;
  1254. const struct cred *cred = current_cred();
  1255. /*
  1256. * We move the actual failure in case of RLIMIT_NPROC excess from
  1257. * set*uid() to execve() because too many poorly written programs
  1258. * don't check setuid() return code. Here we additionally recheck
  1259. * whether NPROC limit is still exceeded.
  1260. */
  1261. if ((current->flags & PF_NPROC_EXCEEDED) &&
  1262. atomic_read(&cred->user->processes) > rlimit(RLIMIT_NPROC)) {
  1263. retval = -EAGAIN;
  1264. goto out_ret;
  1265. }
  1266. /* We're below the limit (still or again), so we don't want to make
  1267. * further execve() calls fail. */
  1268. current->flags &= ~PF_NPROC_EXCEEDED;
  1269. retval = unshare_files(&displaced);
  1270. if (retval)
  1271. goto out_ret;
  1272. retval = -ENOMEM;
  1273. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1274. if (!bprm)
  1275. goto out_files;
  1276. retval = prepare_bprm_creds(bprm);
  1277. if (retval)
  1278. goto out_free;
  1279. retval = check_unsafe_exec(bprm);
  1280. if (retval < 0)
  1281. goto out_free;
  1282. clear_in_exec = retval;
  1283. current->in_execve = 1;
  1284. file = open_exec(filename);
  1285. retval = PTR_ERR(file);
  1286. if (IS_ERR(file))
  1287. goto out_unmark;
  1288. sched_exec();
  1289. bprm->file = file;
  1290. bprm->filename = filename;
  1291. bprm->interp = filename;
  1292. retval = bprm_mm_init(bprm);
  1293. if (retval)
  1294. goto out_file;
  1295. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1296. if ((retval = bprm->argc) < 0)
  1297. goto out;
  1298. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1299. if ((retval = bprm->envc) < 0)
  1300. goto out;
  1301. retval = prepare_binprm(bprm);
  1302. if (retval < 0)
  1303. goto out;
  1304. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1305. if (retval < 0)
  1306. goto out;
  1307. bprm->exec = bprm->p;
  1308. retval = copy_strings(bprm->envc, envp, bprm);
  1309. if (retval < 0)
  1310. goto out;
  1311. retval = copy_strings(bprm->argc, argv, bprm);
  1312. if (retval < 0)
  1313. goto out;
  1314. retval = search_binary_handler(bprm,regs);
  1315. if (retval < 0)
  1316. goto out;
  1317. /* execve succeeded */
  1318. current->fs->in_exec = 0;
  1319. current->in_execve = 0;
  1320. acct_update_integrals(current);
  1321. free_bprm(bprm);
  1322. if (displaced)
  1323. put_files_struct(displaced);
  1324. return retval;
  1325. out:
  1326. if (bprm->mm) {
  1327. acct_arg_size(bprm, 0);
  1328. mmput(bprm->mm);
  1329. }
  1330. out_file:
  1331. if (bprm->file) {
  1332. allow_write_access(bprm->file);
  1333. fput(bprm->file);
  1334. }
  1335. out_unmark:
  1336. if (clear_in_exec)
  1337. current->fs->in_exec = 0;
  1338. current->in_execve = 0;
  1339. out_free:
  1340. free_bprm(bprm);
  1341. out_files:
  1342. if (displaced)
  1343. reset_files_struct(displaced);
  1344. out_ret:
  1345. return retval;
  1346. }
  1347. int do_execve(const char *filename,
  1348. const char __user *const __user *__argv,
  1349. const char __user *const __user *__envp,
  1350. struct pt_regs *regs)
  1351. {
  1352. struct user_arg_ptr argv = { .ptr.native = __argv };
  1353. struct user_arg_ptr envp = { .ptr.native = __envp };
  1354. return do_execve_common(filename, argv, envp, regs);
  1355. }
  1356. #ifdef CONFIG_COMPAT
  1357. int compat_do_execve(char *filename,
  1358. compat_uptr_t __user *__argv,
  1359. compat_uptr_t __user *__envp,
  1360. struct pt_regs *regs)
  1361. {
  1362. struct user_arg_ptr argv = {
  1363. .is_compat = true,
  1364. .ptr.compat = __argv,
  1365. };
  1366. struct user_arg_ptr envp = {
  1367. .is_compat = true,
  1368. .ptr.compat = __envp,
  1369. };
  1370. return do_execve_common(filename, argv, envp, regs);
  1371. }
  1372. #endif
  1373. void set_binfmt(struct linux_binfmt *new)
  1374. {
  1375. struct mm_struct *mm = current->mm;
  1376. if (mm->binfmt)
  1377. module_put(mm->binfmt->module);
  1378. mm->binfmt = new;
  1379. if (new)
  1380. __module_get(new->module);
  1381. }
  1382. EXPORT_SYMBOL(set_binfmt);
  1383. static int expand_corename(struct core_name *cn)
  1384. {
  1385. char *old_corename = cn->corename;
  1386. cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
  1387. cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
  1388. if (!cn->corename) {
  1389. kfree(old_corename);
  1390. return -ENOMEM;
  1391. }
  1392. return 0;
  1393. }
  1394. static int cn_printf(struct core_name *cn, const char *fmt, ...)
  1395. {
  1396. char *cur;
  1397. int need;
  1398. int ret;
  1399. va_list arg;
  1400. va_start(arg, fmt);
  1401. need = vsnprintf(NULL, 0, fmt, arg);
  1402. va_end(arg);
  1403. if (likely(need < cn->size - cn->used - 1))
  1404. goto out_printf;
  1405. ret = expand_corename(cn);
  1406. if (ret)
  1407. goto expand_fail;
  1408. out_printf:
  1409. cur = cn->corename + cn->used;
  1410. va_start(arg, fmt);
  1411. vsnprintf(cur, need + 1, fmt, arg);
  1412. va_end(arg);
  1413. cn->used += need;
  1414. return 0;
  1415. expand_fail:
  1416. return ret;
  1417. }
  1418. static void cn_escape(char *str)
  1419. {
  1420. for (; *str; str++)
  1421. if (*str == '/')
  1422. *str = '!';
  1423. }
  1424. static int cn_print_exe_file(struct core_name *cn)
  1425. {
  1426. struct file *exe_file;
  1427. char *pathbuf, *path;
  1428. int ret;
  1429. exe_file = get_mm_exe_file(current->mm);
  1430. if (!exe_file) {
  1431. char *commstart = cn->corename + cn->used;
  1432. ret = cn_printf(cn, "%s (path unknown)", current->comm);
  1433. cn_escape(commstart);
  1434. return ret;
  1435. }
  1436. pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
  1437. if (!pathbuf) {
  1438. ret = -ENOMEM;
  1439. goto put_exe_file;
  1440. }
  1441. path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
  1442. if (IS_ERR(path)) {
  1443. ret = PTR_ERR(path);
  1444. goto free_buf;
  1445. }
  1446. cn_escape(path);
  1447. ret = cn_printf(cn, "%s", path);
  1448. free_buf:
  1449. kfree(pathbuf);
  1450. put_exe_file:
  1451. fput(exe_file);
  1452. return ret;
  1453. }
  1454. /* format_corename will inspect the pattern parameter, and output a
  1455. * name into corename, which must have space for at least
  1456. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1457. */
  1458. static int format_corename(struct core_name *cn, long signr)
  1459. {
  1460. const struct cred *cred = current_cred();
  1461. const char *pat_ptr = core_pattern;
  1462. int ispipe = (*pat_ptr == '|');
  1463. int pid_in_pattern = 0;
  1464. int err = 0;
  1465. cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
  1466. cn->corename = kmalloc(cn->size, GFP_KERNEL);
  1467. cn->used = 0;
  1468. if (!cn->corename)
  1469. return -ENOMEM;
  1470. /* Repeat as long as we have more pattern to process and more output
  1471. space */
  1472. while (*pat_ptr) {
  1473. if (*pat_ptr != '%') {
  1474. if (*pat_ptr == 0)
  1475. goto out;
  1476. err = cn_printf(cn, "%c", *pat_ptr++);
  1477. } else {
  1478. switch (*++pat_ptr) {
  1479. /* single % at the end, drop that */
  1480. case 0:
  1481. goto out;
  1482. /* Double percent, output one percent */
  1483. case '%':
  1484. err = cn_printf(cn, "%c", '%');
  1485. break;
  1486. /* pid */
  1487. case 'p':
  1488. pid_in_pattern = 1;
  1489. err = cn_printf(cn, "%d",
  1490. task_tgid_vnr(current));
  1491. break;
  1492. /* uid */
  1493. case 'u':
  1494. err = cn_printf(cn, "%d", cred->uid);
  1495. break;
  1496. /* gid */
  1497. case 'g':
  1498. err = cn_printf(cn, "%d", cred->gid);
  1499. break;
  1500. /* signal that caused the coredump */
  1501. case 's':
  1502. err = cn_printf(cn, "%ld", signr);
  1503. break;
  1504. /* UNIX time of coredump */
  1505. case 't': {
  1506. struct timeval tv;
  1507. do_gettimeofday(&tv);
  1508. err = cn_printf(cn, "%lu", tv.tv_sec);
  1509. break;
  1510. }
  1511. /* hostname */
  1512. case 'h': {
  1513. char *namestart = cn->corename + cn->used;
  1514. down_read(&uts_sem);
  1515. err = cn_printf(cn, "%s",
  1516. utsname()->nodename);
  1517. up_read(&uts_sem);
  1518. cn_escape(namestart);
  1519. break;
  1520. }
  1521. /* executable */
  1522. case 'e': {
  1523. char *commstart = cn->corename + cn->used;
  1524. err = cn_printf(cn, "%s", current->comm);
  1525. cn_escape(commstart);
  1526. break;
  1527. }
  1528. case 'E':
  1529. err = cn_print_exe_file(cn);
  1530. break;
  1531. /* core limit size */
  1532. case 'c':
  1533. err = cn_printf(cn, "%lu",
  1534. rlimit(RLIMIT_CORE));
  1535. break;
  1536. default:
  1537. break;
  1538. }
  1539. ++pat_ptr;
  1540. }
  1541. if (err)
  1542. return err;
  1543. }
  1544. /* Backward compatibility with core_uses_pid:
  1545. *
  1546. * If core_pattern does not include a %p (as is the default)
  1547. * and core_uses_pid is set, then .%pid will be appended to
  1548. * the filename. Do not do this for piped commands. */
  1549. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  1550. err = cn_printf(cn, ".%d", task_tgid_vnr(current));
  1551. if (err)
  1552. return err;
  1553. }
  1554. out:
  1555. return ispipe;
  1556. }
  1557. static int zap_process(struct task_struct *start, int exit_code)
  1558. {
  1559. struct task_struct *t;
  1560. int nr = 0;
  1561. start->signal->flags = SIGNAL_GROUP_EXIT;
  1562. start->signal->group_exit_code = exit_code;
  1563. start->signal->group_stop_count = 0;
  1564. t = start;
  1565. do {
  1566. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  1567. if (t != current && t->mm) {
  1568. sigaddset(&t->pending.signal, SIGKILL);
  1569. signal_wake_up(t, 1);
  1570. nr++;
  1571. }
  1572. } while_each_thread(start, t);
  1573. return nr;
  1574. }
  1575. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1576. struct core_state *core_state, int exit_code)
  1577. {
  1578. struct task_struct *g, *p;
  1579. unsigned long flags;
  1580. int nr = -EAGAIN;
  1581. spin_lock_irq(&tsk->sighand->siglock);
  1582. if (!signal_group_exit(tsk->signal)) {
  1583. mm->core_state = core_state;
  1584. nr = zap_process(tsk, exit_code);
  1585. }
  1586. spin_unlock_irq(&tsk->sighand->siglock);
  1587. if (unlikely(nr < 0))
  1588. return nr;
  1589. if (atomic_read(&mm->mm_users) == nr + 1)
  1590. goto done;
  1591. /*
  1592. * We should find and kill all tasks which use this mm, and we should
  1593. * count them correctly into ->nr_threads. We don't take tasklist
  1594. * lock, but this is safe wrt:
  1595. *
  1596. * fork:
  1597. * None of sub-threads can fork after zap_process(leader). All
  1598. * processes which were created before this point should be
  1599. * visible to zap_threads() because copy_process() adds the new
  1600. * process to the tail of init_task.tasks list, and lock/unlock
  1601. * of ->siglock provides a memory barrier.
  1602. *
  1603. * do_exit:
  1604. * The caller holds mm->mmap_sem. This means that the task which
  1605. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1606. * its ->mm.
  1607. *
  1608. * de_thread:
  1609. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1610. * we must see either old or new leader, this does not matter.
  1611. * However, it can change p->sighand, so lock_task_sighand(p)
  1612. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1613. * it can't fail.
  1614. *
  1615. * Note also that "g" can be the old leader with ->mm == NULL
  1616. * and already unhashed and thus removed from ->thread_group.
  1617. * This is OK, __unhash_process()->list_del_rcu() does not
  1618. * clear the ->next pointer, we will find the new leader via
  1619. * next_thread().
  1620. */
  1621. rcu_read_lock();
  1622. for_each_process(g) {
  1623. if (g == tsk->group_leader)
  1624. continue;
  1625. if (g->flags & PF_KTHREAD)
  1626. continue;
  1627. p = g;
  1628. do {
  1629. if (p->mm) {
  1630. if (unlikely(p->mm == mm)) {
  1631. lock_task_sighand(p, &flags);
  1632. nr += zap_process(p, exit_code);
  1633. unlock_task_sighand(p, &flags);
  1634. }
  1635. break;
  1636. }
  1637. } while_each_thread(g, p);
  1638. }
  1639. rcu_read_unlock();
  1640. done:
  1641. atomic_set(&core_state->nr_threads, nr);
  1642. return nr;
  1643. }
  1644. static int coredump_wait(int exit_code, struct core_state *core_state)
  1645. {
  1646. struct task_struct *tsk = current;
  1647. struct mm_struct *mm = tsk->mm;
  1648. int core_waiters = -EBUSY;
  1649. init_completion(&core_state->startup);
  1650. core_state->dumper.task = tsk;
  1651. core_state->dumper.next = NULL;
  1652. down_write(&mm->mmap_sem);
  1653. if (!mm->core_state)
  1654. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1655. up_write(&mm->mmap_sem);
  1656. if (core_waiters > 0) {
  1657. struct core_thread *ptr;
  1658. wait_for_completion(&core_state->startup);
  1659. /*
  1660. * Wait for all the threads to become inactive, so that
  1661. * all the thread context (extended register state, like
  1662. * fpu etc) gets copied to the memory.
  1663. */
  1664. ptr = core_state->dumper.next;
  1665. while (ptr != NULL) {
  1666. wait_task_inactive(ptr->task, 0);
  1667. ptr = ptr->next;
  1668. }
  1669. }
  1670. return core_waiters;
  1671. }
  1672. static void coredump_finish(struct mm_struct *mm)
  1673. {
  1674. struct core_thread *curr, *next;
  1675. struct task_struct *task;
  1676. next = mm->core_state->dumper.next;
  1677. while ((curr = next) != NULL) {
  1678. next = curr->next;
  1679. task = curr->task;
  1680. /*
  1681. * see exit_mm(), curr->task must not see
  1682. * ->task == NULL before we read ->next.
  1683. */
  1684. smp_mb();
  1685. curr->task = NULL;
  1686. wake_up_process(task);
  1687. }
  1688. mm->core_state = NULL;
  1689. }
  1690. /*
  1691. * set_dumpable converts traditional three-value dumpable to two flags and
  1692. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1693. * these bits are not changed atomically. So get_dumpable can observe the
  1694. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1695. * return either old dumpable or new one by paying attention to the order of
  1696. * modifying the bits.
  1697. *
  1698. * dumpable | mm->flags (binary)
  1699. * old new | initial interim final
  1700. * ---------+-----------------------
  1701. * 0 1 | 00 01 01
  1702. * 0 2 | 00 10(*) 11
  1703. * 1 0 | 01 00 00
  1704. * 1 2 | 01 11 11
  1705. * 2 0 | 11 10(*) 00
  1706. * 2 1 | 11 11 01
  1707. *
  1708. * (*) get_dumpable regards interim value of 10 as 11.
  1709. */
  1710. void set_dumpable(struct mm_struct *mm, int value)
  1711. {
  1712. switch (value) {
  1713. case 0:
  1714. clear_bit(MMF_DUMPABLE, &mm->flags);
  1715. smp_wmb();
  1716. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1717. break;
  1718. case 1:
  1719. set_bit(MMF_DUMPABLE, &mm->flags);
  1720. smp_wmb();
  1721. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1722. break;
  1723. case 2:
  1724. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1725. smp_wmb();
  1726. set_bit(MMF_DUMPABLE, &mm->flags);
  1727. break;
  1728. }
  1729. }
  1730. static int __get_dumpable(unsigned long mm_flags)
  1731. {
  1732. int ret;
  1733. ret = mm_flags & MMF_DUMPABLE_MASK;
  1734. return (ret >= 2) ? 2 : ret;
  1735. }
  1736. int get_dumpable(struct mm_struct *mm)
  1737. {
  1738. return __get_dumpable(mm->flags);
  1739. }
  1740. static void wait_for_dump_helpers(struct file *file)
  1741. {
  1742. struct pipe_inode_info *pipe;
  1743. pipe = file->f_path.dentry->d_inode->i_pipe;
  1744. pipe_lock(pipe);
  1745. pipe->readers++;
  1746. pipe->writers--;
  1747. while ((pipe->readers > 1) && (!signal_pending(current))) {
  1748. wake_up_interruptible_sync(&pipe->wait);
  1749. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  1750. pipe_wait(pipe);
  1751. }
  1752. pipe->readers--;
  1753. pipe->writers++;
  1754. pipe_unlock(pipe);
  1755. }
  1756. /*
  1757. * umh_pipe_setup
  1758. * helper function to customize the process used
  1759. * to collect the core in userspace. Specifically
  1760. * it sets up a pipe and installs it as fd 0 (stdin)
  1761. * for the process. Returns 0 on success, or
  1762. * PTR_ERR on failure.
  1763. * Note that it also sets the core limit to 1. This
  1764. * is a special value that we use to trap recursive
  1765. * core dumps
  1766. */
  1767. static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
  1768. {
  1769. struct file *rp, *wp;
  1770. struct fdtable *fdt;
  1771. struct coredump_params *cp = (struct coredump_params *)info->data;
  1772. struct files_struct *cf = current->files;
  1773. wp = create_write_pipe(0);
  1774. if (IS_ERR(wp))
  1775. return PTR_ERR(wp);
  1776. rp = create_read_pipe(wp, 0);
  1777. if (IS_ERR(rp)) {
  1778. free_write_pipe(wp);
  1779. return PTR_ERR(rp);
  1780. }
  1781. cp->file = wp;
  1782. sys_close(0);
  1783. fd_install(0, rp);
  1784. spin_lock(&cf->file_lock);
  1785. fdt = files_fdtable(cf);
  1786. __set_open_fd(0, fdt);
  1787. __clear_close_on_exec(0, fdt);
  1788. spin_unlock(&cf->file_lock);
  1789. /* and disallow core files too */
  1790. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
  1791. return 0;
  1792. }
  1793. void do_coredump(long signr, int exit_code, struct pt_regs *regs)
  1794. {
  1795. struct core_state core_state;
  1796. struct core_name cn;
  1797. struct mm_struct *mm = current->mm;
  1798. struct linux_binfmt * binfmt;
  1799. const struct cred *old_cred;
  1800. struct cred *cred;
  1801. int retval = 0;
  1802. int flag = 0;
  1803. int ispipe;
  1804. static atomic_t core_dump_count = ATOMIC_INIT(0);
  1805. struct coredump_params cprm = {
  1806. .signr = signr,
  1807. .regs = regs,
  1808. .limit = rlimit(RLIMIT_CORE),
  1809. /*
  1810. * We must use the same mm->flags while dumping core to avoid
  1811. * inconsistency of bit flags, since this flag is not protected
  1812. * by any locks.
  1813. */
  1814. .mm_flags = mm->flags,
  1815. };
  1816. audit_core_dumps(signr);
  1817. binfmt = mm->binfmt;
  1818. if (!binfmt || !binfmt->core_dump)
  1819. goto fail;
  1820. if (!__get_dumpable(cprm.mm_flags))
  1821. goto fail;
  1822. cred = prepare_creds();
  1823. if (!cred)
  1824. goto fail;
  1825. /*
  1826. * We cannot trust fsuid as being the "true" uid of the
  1827. * process nor do we know its entire history. We only know it
  1828. * was tainted so we dump it as root in mode 2.
  1829. */
  1830. if (__get_dumpable(cprm.mm_flags) == 2) {
  1831. /* Setuid core dump mode */
  1832. flag = O_EXCL; /* Stop rewrite attacks */
  1833. cred->fsuid = GLOBAL_ROOT_UID; /* Dump root private */
  1834. }
  1835. retval = coredump_wait(exit_code, &core_state);
  1836. if (retval < 0)
  1837. goto fail_creds;
  1838. old_cred = override_creds(cred);
  1839. /*
  1840. * Clear any false indication of pending signals that might
  1841. * be seen by the filesystem code called to write the core file.
  1842. */
  1843. clear_thread_flag(TIF_SIGPENDING);
  1844. ispipe = format_corename(&cn, signr);
  1845. if (ispipe) {
  1846. int dump_count;
  1847. char **helper_argv;
  1848. if (ispipe < 0) {
  1849. printk(KERN_WARNING "format_corename failed\n");
  1850. printk(KERN_WARNING "Aborting core\n");
  1851. goto fail_corename;
  1852. }
  1853. if (cprm.limit == 1) {
  1854. /*
  1855. * Normally core limits are irrelevant to pipes, since
  1856. * we're not writing to the file system, but we use
  1857. * cprm.limit of 1 here as a speacial value. Any
  1858. * non-1 limit gets set to RLIM_INFINITY below, but
  1859. * a limit of 0 skips the dump. This is a consistent
  1860. * way to catch recursive crashes. We can still crash
  1861. * if the core_pattern binary sets RLIM_CORE = !1
  1862. * but it runs as root, and can do lots of stupid things
  1863. * Note that we use task_tgid_vnr here to grab the pid
  1864. * of the process group leader. That way we get the
  1865. * right pid if a thread in a multi-threaded
  1866. * core_pattern process dies.
  1867. */
  1868. printk(KERN_WARNING
  1869. "Process %d(%s) has RLIMIT_CORE set to 1\n",
  1870. task_tgid_vnr(current), current->comm);
  1871. printk(KERN_WARNING "Aborting core\n");
  1872. goto fail_unlock;
  1873. }
  1874. cprm.limit = RLIM_INFINITY;
  1875. dump_count = atomic_inc_return(&core_dump_count);
  1876. if (core_pipe_limit && (core_pipe_limit < dump_count)) {
  1877. printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
  1878. task_tgid_vnr(current), current->comm);
  1879. printk(KERN_WARNING "Skipping core dump\n");
  1880. goto fail_dropcount;
  1881. }
  1882. helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
  1883. if (!helper_argv) {
  1884. printk(KERN_WARNING "%s failed to allocate memory\n",
  1885. __func__);
  1886. goto fail_dropcount;
  1887. }
  1888. retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
  1889. NULL, UMH_WAIT_EXEC, umh_pipe_setup,
  1890. NULL, &cprm);
  1891. argv_free(helper_argv);
  1892. if (retval) {
  1893. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1894. cn.corename);
  1895. goto close_fail;
  1896. }
  1897. } else {
  1898. struct inode *inode;
  1899. if (cprm.limit < binfmt->min_coredump)
  1900. goto fail_unlock;
  1901. cprm.file = filp_open(cn.corename,
  1902. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1903. 0600);
  1904. if (IS_ERR(cprm.file))
  1905. goto fail_unlock;
  1906. inode = cprm.file->f_path.dentry->d_inode;
  1907. if (inode->i_nlink > 1)
  1908. goto close_fail;
  1909. if (d_unhashed(cprm.file->f_path.dentry))
  1910. goto close_fail;
  1911. /*
  1912. * AK: actually i see no reason to not allow this for named
  1913. * pipes etc, but keep the previous behaviour for now.
  1914. */
  1915. if (!S_ISREG(inode->i_mode))
  1916. goto close_fail;
  1917. /*
  1918. * Dont allow local users get cute and trick others to coredump
  1919. * into their pre-created files.
  1920. */
  1921. if (!uid_eq(inode->i_uid, current_fsuid()))
  1922. goto close_fail;
  1923. if (!cprm.file->f_op || !cprm.file->f_op->write)
  1924. goto close_fail;
  1925. if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
  1926. goto close_fail;
  1927. }
  1928. retval = binfmt->core_dump(&cprm);
  1929. if (retval)
  1930. current->signal->group_exit_code |= 0x80;
  1931. if (ispipe && core_pipe_limit)
  1932. wait_for_dump_helpers(cprm.file);
  1933. close_fail:
  1934. if (cprm.file)
  1935. filp_close(cprm.file, NULL);
  1936. fail_dropcount:
  1937. if (ispipe)
  1938. atomic_dec(&core_dump_count);
  1939. fail_unlock:
  1940. kfree(cn.corename);
  1941. fail_corename:
  1942. coredump_finish(mm);
  1943. revert_creds(old_cred);
  1944. fail_creds:
  1945. put_cred(cred);
  1946. fail:
  1947. return;
  1948. }
  1949. /*
  1950. * Core dumping helper functions. These are the only things you should
  1951. * do on a core-file: use only these functions to write out all the
  1952. * necessary info.
  1953. */
  1954. int dump_write(struct file *file, const void *addr, int nr)
  1955. {
  1956. return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1957. }
  1958. EXPORT_SYMBOL(dump_write);
  1959. int dump_seek(struct file *file, loff_t off)
  1960. {
  1961. int ret = 1;
  1962. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1963. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1964. return 0;
  1965. } else {
  1966. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1967. if (!buf)
  1968. return 0;
  1969. while (off > 0) {
  1970. unsigned long n = off;
  1971. if (n > PAGE_SIZE)
  1972. n = PAGE_SIZE;
  1973. if (!dump_write(file, buf, n)) {
  1974. ret = 0;
  1975. break;
  1976. }
  1977. off -= n;
  1978. }
  1979. free_page((unsigned long)buf);
  1980. }
  1981. return ret;
  1982. }
  1983. EXPORT_SYMBOL(dump_seek);