exec.c 53 KB

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