exec.c 54 KB

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