exec.c 53 KB

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