exec.c 55 KB

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