exec.c 55 KB

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