exec.c 55 KB

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