exec.c 53 KB

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