exec.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  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. int core_uses_pid;
  62. char core_pattern[CORENAME_MAX_SIZE] = "core";
  63. unsigned int core_pipe_limit;
  64. int suid_dumpable = 0;
  65. struct core_name {
  66. char *corename;
  67. int used, size;
  68. };
  69. static atomic_t call_count = ATOMIC_INIT(1);
  70. /* The maximal length of core_pattern is also specified in sysctl.c */
  71. static LIST_HEAD(formats);
  72. static DEFINE_RWLOCK(binfmt_lock);
  73. int __register_binfmt(struct linux_binfmt * fmt, int insert)
  74. {
  75. if (!fmt)
  76. return -EINVAL;
  77. write_lock(&binfmt_lock);
  78. insert ? list_add(&fmt->lh, &formats) :
  79. list_add_tail(&fmt->lh, &formats);
  80. write_unlock(&binfmt_lock);
  81. return 0;
  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(tsk, 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. mm_update_next_owner(old_mm);
  719. mmput(old_mm);
  720. return 0;
  721. }
  722. mmdrop(active_mm);
  723. return 0;
  724. }
  725. /*
  726. * This function makes sure the current process has its own signal table,
  727. * so that flush_signal_handlers can later reset the handlers without
  728. * disturbing other processes. (Other processes might share the signal
  729. * table via the CLONE_SIGHAND option to clone().)
  730. */
  731. static int de_thread(struct task_struct *tsk)
  732. {
  733. struct signal_struct *sig = tsk->signal;
  734. struct sighand_struct *oldsighand = tsk->sighand;
  735. spinlock_t *lock = &oldsighand->siglock;
  736. if (thread_group_empty(tsk))
  737. goto no_thread_group;
  738. /*
  739. * Kill all other threads in the thread group.
  740. */
  741. spin_lock_irq(lock);
  742. if (signal_group_exit(sig)) {
  743. /*
  744. * Another group action in progress, just
  745. * return so that the signal is processed.
  746. */
  747. spin_unlock_irq(lock);
  748. return -EAGAIN;
  749. }
  750. sig->group_exit_task = tsk;
  751. sig->notify_count = zap_other_threads(tsk);
  752. if (!thread_group_leader(tsk))
  753. sig->notify_count--;
  754. while (sig->notify_count) {
  755. __set_current_state(TASK_UNINTERRUPTIBLE);
  756. spin_unlock_irq(lock);
  757. schedule();
  758. spin_lock_irq(lock);
  759. }
  760. spin_unlock_irq(lock);
  761. /*
  762. * At this point all other threads have exited, all we have to
  763. * do is to wait for the thread group leader to become inactive,
  764. * and to assume its PID:
  765. */
  766. if (!thread_group_leader(tsk)) {
  767. struct task_struct *leader = tsk->group_leader;
  768. sig->notify_count = -1; /* for exit_notify() */
  769. for (;;) {
  770. write_lock_irq(&tasklist_lock);
  771. if (likely(leader->exit_state))
  772. break;
  773. __set_current_state(TASK_UNINTERRUPTIBLE);
  774. write_unlock_irq(&tasklist_lock);
  775. schedule();
  776. }
  777. /*
  778. * The only record we have of the real-time age of a
  779. * process, regardless of execs it's done, is start_time.
  780. * All the past CPU time is accumulated in signal_struct
  781. * from sister threads now dead. But in this non-leader
  782. * exec, nothing survives from the original leader thread,
  783. * whose birth marks the true age of this process now.
  784. * When we take on its identity by switching to its PID, we
  785. * also take its birthdate (always earlier than our own).
  786. */
  787. tsk->start_time = leader->start_time;
  788. BUG_ON(!same_thread_group(leader, tsk));
  789. BUG_ON(has_group_leader_pid(tsk));
  790. /*
  791. * An exec() starts a new thread group with the
  792. * TGID of the previous thread group. Rehash the
  793. * two threads with a switched PID, and release
  794. * the former thread group leader:
  795. */
  796. /* Become a process group leader with the old leader's pid.
  797. * The old leader becomes a thread of the this thread group.
  798. * Note: The old leader also uses this pid until release_task
  799. * is called. Odd but simple and correct.
  800. */
  801. detach_pid(tsk, PIDTYPE_PID);
  802. tsk->pid = leader->pid;
  803. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  804. transfer_pid(leader, tsk, PIDTYPE_PGID);
  805. transfer_pid(leader, tsk, PIDTYPE_SID);
  806. list_replace_rcu(&leader->tasks, &tsk->tasks);
  807. list_replace_init(&leader->sibling, &tsk->sibling);
  808. tsk->group_leader = tsk;
  809. leader->group_leader = tsk;
  810. tsk->exit_signal = SIGCHLD;
  811. leader->exit_signal = -1;
  812. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  813. leader->exit_state = EXIT_DEAD;
  814. /*
  815. * We are going to release_task()->ptrace_unlink() silently,
  816. * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
  817. * the tracer wont't block again waiting for this thread.
  818. */
  819. if (unlikely(leader->ptrace))
  820. __wake_up_parent(leader, leader->parent);
  821. write_unlock_irq(&tasklist_lock);
  822. release_task(leader);
  823. }
  824. sig->group_exit_task = NULL;
  825. sig->notify_count = 0;
  826. no_thread_group:
  827. if (current->mm)
  828. setmax_mm_hiwater_rss(&sig->maxrss, current->mm);
  829. exit_itimers(sig);
  830. flush_itimer_signals();
  831. if (atomic_read(&oldsighand->count) != 1) {
  832. struct sighand_struct *newsighand;
  833. /*
  834. * This ->sighand is shared with the CLONE_SIGHAND
  835. * but not CLONE_THREAD task, switch to the new one.
  836. */
  837. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  838. if (!newsighand)
  839. return -ENOMEM;
  840. atomic_set(&newsighand->count, 1);
  841. memcpy(newsighand->action, oldsighand->action,
  842. sizeof(newsighand->action));
  843. write_lock_irq(&tasklist_lock);
  844. spin_lock(&oldsighand->siglock);
  845. rcu_assign_pointer(tsk->sighand, newsighand);
  846. spin_unlock(&oldsighand->siglock);
  847. write_unlock_irq(&tasklist_lock);
  848. __cleanup_sighand(oldsighand);
  849. }
  850. BUG_ON(!thread_group_leader(tsk));
  851. return 0;
  852. }
  853. /*
  854. * These functions flushes out all traces of the currently running executable
  855. * so that a new one can be started
  856. */
  857. static void flush_old_files(struct files_struct * files)
  858. {
  859. long j = -1;
  860. struct fdtable *fdt;
  861. spin_lock(&files->file_lock);
  862. for (;;) {
  863. unsigned long set, i;
  864. j++;
  865. i = j * __NFDBITS;
  866. fdt = files_fdtable(files);
  867. if (i >= fdt->max_fds)
  868. break;
  869. set = fdt->close_on_exec->fds_bits[j];
  870. if (!set)
  871. continue;
  872. fdt->close_on_exec->fds_bits[j] = 0;
  873. spin_unlock(&files->file_lock);
  874. for ( ; set ; i++,set >>= 1) {
  875. if (set & 1) {
  876. sys_close(i);
  877. }
  878. }
  879. spin_lock(&files->file_lock);
  880. }
  881. spin_unlock(&files->file_lock);
  882. }
  883. char *get_task_comm(char *buf, struct task_struct *tsk)
  884. {
  885. /* buf must be at least sizeof(tsk->comm) in size */
  886. task_lock(tsk);
  887. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  888. task_unlock(tsk);
  889. return buf;
  890. }
  891. EXPORT_SYMBOL_GPL(get_task_comm);
  892. void set_task_comm(struct task_struct *tsk, char *buf)
  893. {
  894. task_lock(tsk);
  895. trace_task_rename(tsk, buf);
  896. /*
  897. * Threads may access current->comm without holding
  898. * the task lock, so write the string carefully.
  899. * Readers without a lock may see incomplete new
  900. * names but are safe from non-terminating string reads.
  901. */
  902. memset(tsk->comm, 0, TASK_COMM_LEN);
  903. wmb();
  904. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  905. task_unlock(tsk);
  906. perf_event_comm(tsk);
  907. }
  908. int flush_old_exec(struct linux_binprm * bprm)
  909. {
  910. int retval;
  911. /*
  912. * Make sure we have a private signal table and that
  913. * we are unassociated from the previous thread group.
  914. */
  915. retval = de_thread(current);
  916. if (retval)
  917. goto out;
  918. set_mm_exe_file(bprm->mm, bprm->file);
  919. /*
  920. * Release all of the old mmap stuff
  921. */
  922. acct_arg_size(bprm, 0);
  923. retval = exec_mmap(bprm->mm);
  924. if (retval)
  925. goto out;
  926. bprm->mm = NULL; /* We're using it now */
  927. set_fs(USER_DS);
  928. current->flags &= ~(PF_RANDOMIZE | PF_KTHREAD);
  929. flush_thread();
  930. current->personality &= ~bprm->per_clear;
  931. return 0;
  932. out:
  933. return retval;
  934. }
  935. EXPORT_SYMBOL(flush_old_exec);
  936. void would_dump(struct linux_binprm *bprm, struct file *file)
  937. {
  938. if (inode_permission(file->f_path.dentry->d_inode, MAY_READ) < 0)
  939. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  940. }
  941. EXPORT_SYMBOL(would_dump);
  942. void setup_new_exec(struct linux_binprm * bprm)
  943. {
  944. int i, ch;
  945. const char *name;
  946. char tcomm[sizeof(current->comm)];
  947. arch_pick_mmap_layout(current->mm);
  948. /* This is the point of no return */
  949. current->sas_ss_sp = current->sas_ss_size = 0;
  950. if (current_euid() == current_uid() && current_egid() == current_gid())
  951. set_dumpable(current->mm, 1);
  952. else
  953. set_dumpable(current->mm, suid_dumpable);
  954. name = bprm->filename;
  955. /* Copies the binary name from after last slash */
  956. for (i=0; (ch = *(name++)) != '\0';) {
  957. if (ch == '/')
  958. i = 0; /* overwrite what we wrote */
  959. else
  960. if (i < (sizeof(tcomm) - 1))
  961. tcomm[i++] = ch;
  962. }
  963. tcomm[i] = '\0';
  964. set_task_comm(current, tcomm);
  965. /* Set the new mm task size. We have to do that late because it may
  966. * depend on TIF_32BIT which is only updated in flush_thread() on
  967. * some architectures like powerpc
  968. */
  969. current->mm->task_size = TASK_SIZE;
  970. /* install the new credentials */
  971. if (bprm->cred->uid != current_euid() ||
  972. bprm->cred->gid != current_egid()) {
  973. current->pdeath_signal = 0;
  974. } else {
  975. would_dump(bprm, bprm->file);
  976. if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
  977. set_dumpable(current->mm, suid_dumpable);
  978. }
  979. /*
  980. * Flush performance counters when crossing a
  981. * security domain:
  982. */
  983. if (!get_dumpable(current->mm))
  984. perf_event_exit_task(current);
  985. /* An exec changes our domain. We are no longer part of the thread
  986. group */
  987. current->self_exec_id++;
  988. flush_signal_handlers(current, 0);
  989. flush_old_files(current->files);
  990. }
  991. EXPORT_SYMBOL(setup_new_exec);
  992. /*
  993. * Prepare credentials and lock ->cred_guard_mutex.
  994. * install_exec_creds() commits the new creds and drops the lock.
  995. * Or, if exec fails before, free_bprm() should release ->cred and
  996. * and unlock.
  997. */
  998. int prepare_bprm_creds(struct linux_binprm *bprm)
  999. {
  1000. if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
  1001. return -ERESTARTNOINTR;
  1002. bprm->cred = prepare_exec_creds();
  1003. if (likely(bprm->cred))
  1004. return 0;
  1005. mutex_unlock(&current->signal->cred_guard_mutex);
  1006. return -ENOMEM;
  1007. }
  1008. void free_bprm(struct linux_binprm *bprm)
  1009. {
  1010. free_arg_pages(bprm);
  1011. if (bprm->cred) {
  1012. mutex_unlock(&current->signal->cred_guard_mutex);
  1013. abort_creds(bprm->cred);
  1014. }
  1015. kfree(bprm);
  1016. }
  1017. /*
  1018. * install the new credentials for this executable
  1019. */
  1020. void install_exec_creds(struct linux_binprm *bprm)
  1021. {
  1022. security_bprm_committing_creds(bprm);
  1023. commit_creds(bprm->cred);
  1024. bprm->cred = NULL;
  1025. /*
  1026. * cred_guard_mutex must be held at least to this point to prevent
  1027. * ptrace_attach() from altering our determination of the task's
  1028. * credentials; any time after this it may be unlocked.
  1029. */
  1030. security_bprm_committed_creds(bprm);
  1031. mutex_unlock(&current->signal->cred_guard_mutex);
  1032. }
  1033. EXPORT_SYMBOL(install_exec_creds);
  1034. /*
  1035. * determine how safe it is to execute the proposed program
  1036. * - the caller must hold ->cred_guard_mutex to protect against
  1037. * PTRACE_ATTACH
  1038. */
  1039. static int check_unsafe_exec(struct linux_binprm *bprm)
  1040. {
  1041. struct task_struct *p = current, *t;
  1042. unsigned n_fs;
  1043. int res = 0;
  1044. if (p->ptrace) {
  1045. if (p->ptrace & PT_PTRACE_CAP)
  1046. bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
  1047. else
  1048. bprm->unsafe |= LSM_UNSAFE_PTRACE;
  1049. }
  1050. n_fs = 1;
  1051. spin_lock(&p->fs->lock);
  1052. rcu_read_lock();
  1053. for (t = next_thread(p); t != p; t = next_thread(t)) {
  1054. if (t->fs == p->fs)
  1055. n_fs++;
  1056. }
  1057. rcu_read_unlock();
  1058. if (p->fs->users > n_fs) {
  1059. bprm->unsafe |= LSM_UNSAFE_SHARE;
  1060. } else {
  1061. res = -EAGAIN;
  1062. if (!p->fs->in_exec) {
  1063. p->fs->in_exec = 1;
  1064. res = 1;
  1065. }
  1066. }
  1067. spin_unlock(&p->fs->lock);
  1068. return res;
  1069. }
  1070. /*
  1071. * Fill the binprm structure from the inode.
  1072. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  1073. *
  1074. * This may be called multiple times for binary chains (scripts for example).
  1075. */
  1076. int prepare_binprm(struct linux_binprm *bprm)
  1077. {
  1078. umode_t mode;
  1079. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  1080. int retval;
  1081. mode = inode->i_mode;
  1082. if (bprm->file->f_op == NULL)
  1083. return -EACCES;
  1084. /* clear any previous set[ug]id data from a previous binary */
  1085. bprm->cred->euid = current_euid();
  1086. bprm->cred->egid = current_egid();
  1087. if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  1088. /* Set-uid? */
  1089. if (mode & S_ISUID) {
  1090. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1091. bprm->cred->euid = inode->i_uid;
  1092. }
  1093. /* Set-gid? */
  1094. /*
  1095. * If setgid is set but no group execute bit then this
  1096. * is a candidate for mandatory locking, not a setgid
  1097. * executable.
  1098. */
  1099. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1100. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1101. bprm->cred->egid = inode->i_gid;
  1102. }
  1103. }
  1104. /* fill in binprm security blob */
  1105. retval = security_bprm_set_creds(bprm);
  1106. if (retval)
  1107. return retval;
  1108. bprm->cred_prepared = 1;
  1109. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  1110. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  1111. }
  1112. EXPORT_SYMBOL(prepare_binprm);
  1113. /*
  1114. * Arguments are '\0' separated strings found at the location bprm->p
  1115. * points to; chop off the first by relocating brpm->p to right after
  1116. * the first '\0' encountered.
  1117. */
  1118. int remove_arg_zero(struct linux_binprm *bprm)
  1119. {
  1120. int ret = 0;
  1121. unsigned long offset;
  1122. char *kaddr;
  1123. struct page *page;
  1124. if (!bprm->argc)
  1125. return 0;
  1126. do {
  1127. offset = bprm->p & ~PAGE_MASK;
  1128. page = get_arg_page(bprm, bprm->p, 0);
  1129. if (!page) {
  1130. ret = -EFAULT;
  1131. goto out;
  1132. }
  1133. kaddr = kmap_atomic(page, KM_USER0);
  1134. for (; offset < PAGE_SIZE && kaddr[offset];
  1135. offset++, bprm->p++)
  1136. ;
  1137. kunmap_atomic(kaddr, KM_USER0);
  1138. put_arg_page(page);
  1139. if (offset == PAGE_SIZE)
  1140. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  1141. } while (offset == PAGE_SIZE);
  1142. bprm->p++;
  1143. bprm->argc--;
  1144. ret = 0;
  1145. out:
  1146. return ret;
  1147. }
  1148. EXPORT_SYMBOL(remove_arg_zero);
  1149. /*
  1150. * cycle the list of binary formats handler, until one recognizes the image
  1151. */
  1152. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  1153. {
  1154. unsigned int depth = bprm->recursion_depth;
  1155. int try,retval;
  1156. struct linux_binfmt *fmt;
  1157. pid_t old_pid;
  1158. retval = security_bprm_check(bprm);
  1159. if (retval)
  1160. return retval;
  1161. retval = audit_bprm(bprm);
  1162. if (retval)
  1163. return retval;
  1164. /* Need to fetch pid before load_binary changes it */
  1165. rcu_read_lock();
  1166. old_pid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
  1167. rcu_read_unlock();
  1168. retval = -ENOENT;
  1169. for (try=0; try<2; try++) {
  1170. read_lock(&binfmt_lock);
  1171. list_for_each_entry(fmt, &formats, lh) {
  1172. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1173. if (!fn)
  1174. continue;
  1175. if (!try_module_get(fmt->module))
  1176. continue;
  1177. read_unlock(&binfmt_lock);
  1178. retval = fn(bprm, regs);
  1179. /*
  1180. * Restore the depth counter to its starting value
  1181. * in this call, so we don't have to rely on every
  1182. * load_binary function to restore it on return.
  1183. */
  1184. bprm->recursion_depth = depth;
  1185. if (retval >= 0) {
  1186. if (depth == 0)
  1187. ptrace_event(PTRACE_EVENT_EXEC,
  1188. old_pid);
  1189. put_binfmt(fmt);
  1190. allow_write_access(bprm->file);
  1191. if (bprm->file)
  1192. fput(bprm->file);
  1193. bprm->file = NULL;
  1194. current->did_exec = 1;
  1195. proc_exec_connector(current);
  1196. return retval;
  1197. }
  1198. read_lock(&binfmt_lock);
  1199. put_binfmt(fmt);
  1200. if (retval != -ENOEXEC || bprm->mm == NULL)
  1201. break;
  1202. if (!bprm->file) {
  1203. read_unlock(&binfmt_lock);
  1204. return retval;
  1205. }
  1206. }
  1207. read_unlock(&binfmt_lock);
  1208. #ifdef CONFIG_MODULES
  1209. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1210. break;
  1211. } else {
  1212. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1213. if (printable(bprm->buf[0]) &&
  1214. printable(bprm->buf[1]) &&
  1215. printable(bprm->buf[2]) &&
  1216. printable(bprm->buf[3]))
  1217. break; /* -ENOEXEC */
  1218. if (try)
  1219. break; /* -ENOEXEC */
  1220. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1221. }
  1222. #else
  1223. break;
  1224. #endif
  1225. }
  1226. return retval;
  1227. }
  1228. EXPORT_SYMBOL(search_binary_handler);
  1229. /*
  1230. * sys_execve() executes a new program.
  1231. */
  1232. static int do_execve_common(const char *filename,
  1233. struct user_arg_ptr argv,
  1234. struct user_arg_ptr envp,
  1235. struct pt_regs *regs)
  1236. {
  1237. struct linux_binprm *bprm;
  1238. struct file *file;
  1239. struct files_struct *displaced;
  1240. bool clear_in_exec;
  1241. int retval;
  1242. const struct cred *cred = current_cred();
  1243. /*
  1244. * We move the actual failure in case of RLIMIT_NPROC excess from
  1245. * set*uid() to execve() because too many poorly written programs
  1246. * don't check setuid() return code. Here we additionally recheck
  1247. * whether NPROC limit is still exceeded.
  1248. */
  1249. if ((current->flags & PF_NPROC_EXCEEDED) &&
  1250. atomic_read(&cred->user->processes) > rlimit(RLIMIT_NPROC)) {
  1251. retval = -EAGAIN;
  1252. goto out_ret;
  1253. }
  1254. /* We're below the limit (still or again), so we don't want to make
  1255. * further execve() calls fail. */
  1256. current->flags &= ~PF_NPROC_EXCEEDED;
  1257. retval = unshare_files(&displaced);
  1258. if (retval)
  1259. goto out_ret;
  1260. retval = -ENOMEM;
  1261. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1262. if (!bprm)
  1263. goto out_files;
  1264. retval = prepare_bprm_creds(bprm);
  1265. if (retval)
  1266. goto out_free;
  1267. retval = check_unsafe_exec(bprm);
  1268. if (retval < 0)
  1269. goto out_free;
  1270. clear_in_exec = retval;
  1271. current->in_execve = 1;
  1272. file = open_exec(filename);
  1273. retval = PTR_ERR(file);
  1274. if (IS_ERR(file))
  1275. goto out_unmark;
  1276. sched_exec();
  1277. bprm->file = file;
  1278. bprm->filename = filename;
  1279. bprm->interp = filename;
  1280. retval = bprm_mm_init(bprm);
  1281. if (retval)
  1282. goto out_file;
  1283. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1284. if ((retval = bprm->argc) < 0)
  1285. goto out;
  1286. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1287. if ((retval = bprm->envc) < 0)
  1288. goto out;
  1289. retval = prepare_binprm(bprm);
  1290. if (retval < 0)
  1291. goto out;
  1292. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1293. if (retval < 0)
  1294. goto out;
  1295. bprm->exec = bprm->p;
  1296. retval = copy_strings(bprm->envc, envp, bprm);
  1297. if (retval < 0)
  1298. goto out;
  1299. retval = copy_strings(bprm->argc, argv, bprm);
  1300. if (retval < 0)
  1301. goto out;
  1302. retval = search_binary_handler(bprm,regs);
  1303. if (retval < 0)
  1304. goto out;
  1305. /* execve succeeded */
  1306. current->fs->in_exec = 0;
  1307. current->in_execve = 0;
  1308. acct_update_integrals(current);
  1309. free_bprm(bprm);
  1310. if (displaced)
  1311. put_files_struct(displaced);
  1312. return retval;
  1313. out:
  1314. if (bprm->mm) {
  1315. acct_arg_size(bprm, 0);
  1316. mmput(bprm->mm);
  1317. }
  1318. out_file:
  1319. if (bprm->file) {
  1320. allow_write_access(bprm->file);
  1321. fput(bprm->file);
  1322. }
  1323. out_unmark:
  1324. if (clear_in_exec)
  1325. current->fs->in_exec = 0;
  1326. current->in_execve = 0;
  1327. out_free:
  1328. free_bprm(bprm);
  1329. out_files:
  1330. if (displaced)
  1331. reset_files_struct(displaced);
  1332. out_ret:
  1333. return retval;
  1334. }
  1335. int do_execve(const char *filename,
  1336. const char __user *const __user *__argv,
  1337. const char __user *const __user *__envp,
  1338. struct pt_regs *regs)
  1339. {
  1340. struct user_arg_ptr argv = { .ptr.native = __argv };
  1341. struct user_arg_ptr envp = { .ptr.native = __envp };
  1342. return do_execve_common(filename, argv, envp, regs);
  1343. }
  1344. #ifdef CONFIG_COMPAT
  1345. int compat_do_execve(char *filename,
  1346. compat_uptr_t __user *__argv,
  1347. compat_uptr_t __user *__envp,
  1348. struct pt_regs *regs)
  1349. {
  1350. struct user_arg_ptr argv = {
  1351. .is_compat = true,
  1352. .ptr.compat = __argv,
  1353. };
  1354. struct user_arg_ptr envp = {
  1355. .is_compat = true,
  1356. .ptr.compat = __envp,
  1357. };
  1358. return do_execve_common(filename, argv, envp, regs);
  1359. }
  1360. #endif
  1361. void set_binfmt(struct linux_binfmt *new)
  1362. {
  1363. struct mm_struct *mm = current->mm;
  1364. if (mm->binfmt)
  1365. module_put(mm->binfmt->module);
  1366. mm->binfmt = new;
  1367. if (new)
  1368. __module_get(new->module);
  1369. }
  1370. EXPORT_SYMBOL(set_binfmt);
  1371. static int expand_corename(struct core_name *cn)
  1372. {
  1373. char *old_corename = cn->corename;
  1374. cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
  1375. cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
  1376. if (!cn->corename) {
  1377. kfree(old_corename);
  1378. return -ENOMEM;
  1379. }
  1380. return 0;
  1381. }
  1382. static int cn_printf(struct core_name *cn, const char *fmt, ...)
  1383. {
  1384. char *cur;
  1385. int need;
  1386. int ret;
  1387. va_list arg;
  1388. va_start(arg, fmt);
  1389. need = vsnprintf(NULL, 0, fmt, arg);
  1390. va_end(arg);
  1391. if (likely(need < cn->size - cn->used - 1))
  1392. goto out_printf;
  1393. ret = expand_corename(cn);
  1394. if (ret)
  1395. goto expand_fail;
  1396. out_printf:
  1397. cur = cn->corename + cn->used;
  1398. va_start(arg, fmt);
  1399. vsnprintf(cur, need + 1, fmt, arg);
  1400. va_end(arg);
  1401. cn->used += need;
  1402. return 0;
  1403. expand_fail:
  1404. return ret;
  1405. }
  1406. static void cn_escape(char *str)
  1407. {
  1408. for (; *str; str++)
  1409. if (*str == '/')
  1410. *str = '!';
  1411. }
  1412. static int cn_print_exe_file(struct core_name *cn)
  1413. {
  1414. struct file *exe_file;
  1415. char *pathbuf, *path;
  1416. int ret;
  1417. exe_file = get_mm_exe_file(current->mm);
  1418. if (!exe_file) {
  1419. char *commstart = cn->corename + cn->used;
  1420. ret = cn_printf(cn, "%s (path unknown)", current->comm);
  1421. cn_escape(commstart);
  1422. return ret;
  1423. }
  1424. pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
  1425. if (!pathbuf) {
  1426. ret = -ENOMEM;
  1427. goto put_exe_file;
  1428. }
  1429. path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
  1430. if (IS_ERR(path)) {
  1431. ret = PTR_ERR(path);
  1432. goto free_buf;
  1433. }
  1434. cn_escape(path);
  1435. ret = cn_printf(cn, "%s", path);
  1436. free_buf:
  1437. kfree(pathbuf);
  1438. put_exe_file:
  1439. fput(exe_file);
  1440. return ret;
  1441. }
  1442. /* format_corename will inspect the pattern parameter, and output a
  1443. * name into corename, which must have space for at least
  1444. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1445. */
  1446. static int format_corename(struct core_name *cn, long signr)
  1447. {
  1448. const struct cred *cred = current_cred();
  1449. const char *pat_ptr = core_pattern;
  1450. int ispipe = (*pat_ptr == '|');
  1451. int pid_in_pattern = 0;
  1452. int err = 0;
  1453. cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
  1454. cn->corename = kmalloc(cn->size, GFP_KERNEL);
  1455. cn->used = 0;
  1456. if (!cn->corename)
  1457. return -ENOMEM;
  1458. /* Repeat as long as we have more pattern to process and more output
  1459. space */
  1460. while (*pat_ptr) {
  1461. if (*pat_ptr != '%') {
  1462. if (*pat_ptr == 0)
  1463. goto out;
  1464. err = cn_printf(cn, "%c", *pat_ptr++);
  1465. } else {
  1466. switch (*++pat_ptr) {
  1467. /* single % at the end, drop that */
  1468. case 0:
  1469. goto out;
  1470. /* Double percent, output one percent */
  1471. case '%':
  1472. err = cn_printf(cn, "%c", '%');
  1473. break;
  1474. /* pid */
  1475. case 'p':
  1476. pid_in_pattern = 1;
  1477. err = cn_printf(cn, "%d",
  1478. task_tgid_vnr(current));
  1479. break;
  1480. /* uid */
  1481. case 'u':
  1482. err = cn_printf(cn, "%d", cred->uid);
  1483. break;
  1484. /* gid */
  1485. case 'g':
  1486. err = cn_printf(cn, "%d", cred->gid);
  1487. break;
  1488. /* signal that caused the coredump */
  1489. case 's':
  1490. err = cn_printf(cn, "%ld", signr);
  1491. break;
  1492. /* UNIX time of coredump */
  1493. case 't': {
  1494. struct timeval tv;
  1495. do_gettimeofday(&tv);
  1496. err = cn_printf(cn, "%lu", tv.tv_sec);
  1497. break;
  1498. }
  1499. /* hostname */
  1500. case 'h': {
  1501. char *namestart = cn->corename + cn->used;
  1502. down_read(&uts_sem);
  1503. err = cn_printf(cn, "%s",
  1504. utsname()->nodename);
  1505. up_read(&uts_sem);
  1506. cn_escape(namestart);
  1507. break;
  1508. }
  1509. /* executable */
  1510. case 'e': {
  1511. char *commstart = cn->corename + cn->used;
  1512. err = cn_printf(cn, "%s", current->comm);
  1513. cn_escape(commstart);
  1514. break;
  1515. }
  1516. case 'E':
  1517. err = cn_print_exe_file(cn);
  1518. break;
  1519. /* core limit size */
  1520. case 'c':
  1521. err = cn_printf(cn, "%lu",
  1522. rlimit(RLIMIT_CORE));
  1523. break;
  1524. default:
  1525. break;
  1526. }
  1527. ++pat_ptr;
  1528. }
  1529. if (err)
  1530. return err;
  1531. }
  1532. /* Backward compatibility with core_uses_pid:
  1533. *
  1534. * If core_pattern does not include a %p (as is the default)
  1535. * and core_uses_pid is set, then .%pid will be appended to
  1536. * the filename. Do not do this for piped commands. */
  1537. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  1538. err = cn_printf(cn, ".%d", task_tgid_vnr(current));
  1539. if (err)
  1540. return err;
  1541. }
  1542. out:
  1543. return ispipe;
  1544. }
  1545. static int zap_process(struct task_struct *start, int exit_code)
  1546. {
  1547. struct task_struct *t;
  1548. int nr = 0;
  1549. start->signal->flags = SIGNAL_GROUP_EXIT;
  1550. start->signal->group_exit_code = exit_code;
  1551. start->signal->group_stop_count = 0;
  1552. t = start;
  1553. do {
  1554. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  1555. if (t != current && t->mm) {
  1556. sigaddset(&t->pending.signal, SIGKILL);
  1557. signal_wake_up(t, 1);
  1558. nr++;
  1559. }
  1560. } while_each_thread(start, t);
  1561. return nr;
  1562. }
  1563. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1564. struct core_state *core_state, int exit_code)
  1565. {
  1566. struct task_struct *g, *p;
  1567. unsigned long flags;
  1568. int nr = -EAGAIN;
  1569. spin_lock_irq(&tsk->sighand->siglock);
  1570. if (!signal_group_exit(tsk->signal)) {
  1571. mm->core_state = core_state;
  1572. nr = zap_process(tsk, exit_code);
  1573. }
  1574. spin_unlock_irq(&tsk->sighand->siglock);
  1575. if (unlikely(nr < 0))
  1576. return nr;
  1577. if (atomic_read(&mm->mm_users) == nr + 1)
  1578. goto done;
  1579. /*
  1580. * We should find and kill all tasks which use this mm, and we should
  1581. * count them correctly into ->nr_threads. We don't take tasklist
  1582. * lock, but this is safe wrt:
  1583. *
  1584. * fork:
  1585. * None of sub-threads can fork after zap_process(leader). All
  1586. * processes which were created before this point should be
  1587. * visible to zap_threads() because copy_process() adds the new
  1588. * process to the tail of init_task.tasks list, and lock/unlock
  1589. * of ->siglock provides a memory barrier.
  1590. *
  1591. * do_exit:
  1592. * The caller holds mm->mmap_sem. This means that the task which
  1593. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1594. * its ->mm.
  1595. *
  1596. * de_thread:
  1597. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1598. * we must see either old or new leader, this does not matter.
  1599. * However, it can change p->sighand, so lock_task_sighand(p)
  1600. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1601. * it can't fail.
  1602. *
  1603. * Note also that "g" can be the old leader with ->mm == NULL
  1604. * and already unhashed and thus removed from ->thread_group.
  1605. * This is OK, __unhash_process()->list_del_rcu() does not
  1606. * clear the ->next pointer, we will find the new leader via
  1607. * next_thread().
  1608. */
  1609. rcu_read_lock();
  1610. for_each_process(g) {
  1611. if (g == tsk->group_leader)
  1612. continue;
  1613. if (g->flags & PF_KTHREAD)
  1614. continue;
  1615. p = g;
  1616. do {
  1617. if (p->mm) {
  1618. if (unlikely(p->mm == mm)) {
  1619. lock_task_sighand(p, &flags);
  1620. nr += zap_process(p, exit_code);
  1621. unlock_task_sighand(p, &flags);
  1622. }
  1623. break;
  1624. }
  1625. } while_each_thread(g, p);
  1626. }
  1627. rcu_read_unlock();
  1628. done:
  1629. atomic_set(&core_state->nr_threads, nr);
  1630. return nr;
  1631. }
  1632. static int coredump_wait(int exit_code, struct core_state *core_state)
  1633. {
  1634. struct task_struct *tsk = current;
  1635. struct mm_struct *mm = tsk->mm;
  1636. struct completion *vfork_done;
  1637. int core_waiters = -EBUSY;
  1638. init_completion(&core_state->startup);
  1639. core_state->dumper.task = tsk;
  1640. core_state->dumper.next = NULL;
  1641. down_write(&mm->mmap_sem);
  1642. if (!mm->core_state)
  1643. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1644. up_write(&mm->mmap_sem);
  1645. if (unlikely(core_waiters < 0))
  1646. goto fail;
  1647. /*
  1648. * Make sure nobody is waiting for us to release the VM,
  1649. * otherwise we can deadlock when we wait on each other
  1650. */
  1651. vfork_done = tsk->vfork_done;
  1652. if (vfork_done) {
  1653. tsk->vfork_done = NULL;
  1654. complete(vfork_done);
  1655. }
  1656. if (core_waiters)
  1657. wait_for_completion(&core_state->startup);
  1658. fail:
  1659. return core_waiters;
  1660. }
  1661. static void coredump_finish(struct mm_struct *mm)
  1662. {
  1663. struct core_thread *curr, *next;
  1664. struct task_struct *task;
  1665. next = mm->core_state->dumper.next;
  1666. while ((curr = next) != NULL) {
  1667. next = curr->next;
  1668. task = curr->task;
  1669. /*
  1670. * see exit_mm(), curr->task must not see
  1671. * ->task == NULL before we read ->next.
  1672. */
  1673. smp_mb();
  1674. curr->task = NULL;
  1675. wake_up_process(task);
  1676. }
  1677. mm->core_state = NULL;
  1678. }
  1679. /*
  1680. * set_dumpable converts traditional three-value dumpable to two flags and
  1681. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1682. * these bits are not changed atomically. So get_dumpable can observe the
  1683. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1684. * return either old dumpable or new one by paying attention to the order of
  1685. * modifying the bits.
  1686. *
  1687. * dumpable | mm->flags (binary)
  1688. * old new | initial interim final
  1689. * ---------+-----------------------
  1690. * 0 1 | 00 01 01
  1691. * 0 2 | 00 10(*) 11
  1692. * 1 0 | 01 00 00
  1693. * 1 2 | 01 11 11
  1694. * 2 0 | 11 10(*) 00
  1695. * 2 1 | 11 11 01
  1696. *
  1697. * (*) get_dumpable regards interim value of 10 as 11.
  1698. */
  1699. void set_dumpable(struct mm_struct *mm, int value)
  1700. {
  1701. switch (value) {
  1702. case 0:
  1703. clear_bit(MMF_DUMPABLE, &mm->flags);
  1704. smp_wmb();
  1705. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1706. break;
  1707. case 1:
  1708. set_bit(MMF_DUMPABLE, &mm->flags);
  1709. smp_wmb();
  1710. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1711. break;
  1712. case 2:
  1713. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1714. smp_wmb();
  1715. set_bit(MMF_DUMPABLE, &mm->flags);
  1716. break;
  1717. }
  1718. }
  1719. static int __get_dumpable(unsigned long mm_flags)
  1720. {
  1721. int ret;
  1722. ret = mm_flags & MMF_DUMPABLE_MASK;
  1723. return (ret >= 2) ? 2 : ret;
  1724. }
  1725. int get_dumpable(struct mm_struct *mm)
  1726. {
  1727. return __get_dumpable(mm->flags);
  1728. }
  1729. static void wait_for_dump_helpers(struct file *file)
  1730. {
  1731. struct pipe_inode_info *pipe;
  1732. pipe = file->f_path.dentry->d_inode->i_pipe;
  1733. pipe_lock(pipe);
  1734. pipe->readers++;
  1735. pipe->writers--;
  1736. while ((pipe->readers > 1) && (!signal_pending(current))) {
  1737. wake_up_interruptible_sync(&pipe->wait);
  1738. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  1739. pipe_wait(pipe);
  1740. }
  1741. pipe->readers--;
  1742. pipe->writers++;
  1743. pipe_unlock(pipe);
  1744. }
  1745. /*
  1746. * umh_pipe_setup
  1747. * helper function to customize the process used
  1748. * to collect the core in userspace. Specifically
  1749. * it sets up a pipe and installs it as fd 0 (stdin)
  1750. * for the process. Returns 0 on success, or
  1751. * PTR_ERR on failure.
  1752. * Note that it also sets the core limit to 1. This
  1753. * is a special value that we use to trap recursive
  1754. * core dumps
  1755. */
  1756. static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
  1757. {
  1758. struct file *rp, *wp;
  1759. struct fdtable *fdt;
  1760. struct coredump_params *cp = (struct coredump_params *)info->data;
  1761. struct files_struct *cf = current->files;
  1762. wp = create_write_pipe(0);
  1763. if (IS_ERR(wp))
  1764. return PTR_ERR(wp);
  1765. rp = create_read_pipe(wp, 0);
  1766. if (IS_ERR(rp)) {
  1767. free_write_pipe(wp);
  1768. return PTR_ERR(rp);
  1769. }
  1770. cp->file = wp;
  1771. sys_close(0);
  1772. fd_install(0, rp);
  1773. spin_lock(&cf->file_lock);
  1774. fdt = files_fdtable(cf);
  1775. FD_SET(0, fdt->open_fds);
  1776. FD_CLR(0, fdt->close_on_exec);
  1777. spin_unlock(&cf->file_lock);
  1778. /* and disallow core files too */
  1779. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
  1780. return 0;
  1781. }
  1782. void do_coredump(long signr, int exit_code, struct pt_regs *regs)
  1783. {
  1784. struct core_state core_state;
  1785. struct core_name cn;
  1786. struct mm_struct *mm = current->mm;
  1787. struct linux_binfmt * binfmt;
  1788. const struct cred *old_cred;
  1789. struct cred *cred;
  1790. int retval = 0;
  1791. int flag = 0;
  1792. int ispipe;
  1793. static atomic_t core_dump_count = ATOMIC_INIT(0);
  1794. struct coredump_params cprm = {
  1795. .signr = signr,
  1796. .regs = regs,
  1797. .limit = rlimit(RLIMIT_CORE),
  1798. /*
  1799. * We must use the same mm->flags while dumping core to avoid
  1800. * inconsistency of bit flags, since this flag is not protected
  1801. * by any locks.
  1802. */
  1803. .mm_flags = mm->flags,
  1804. };
  1805. audit_core_dumps(signr);
  1806. binfmt = mm->binfmt;
  1807. if (!binfmt || !binfmt->core_dump)
  1808. goto fail;
  1809. if (!__get_dumpable(cprm.mm_flags))
  1810. goto fail;
  1811. cred = prepare_creds();
  1812. if (!cred)
  1813. goto fail;
  1814. /*
  1815. * We cannot trust fsuid as being the "true" uid of the
  1816. * process nor do we know its entire history. We only know it
  1817. * was tainted so we dump it as root in mode 2.
  1818. */
  1819. if (__get_dumpable(cprm.mm_flags) == 2) {
  1820. /* Setuid core dump mode */
  1821. flag = O_EXCL; /* Stop rewrite attacks */
  1822. cred->fsuid = 0; /* Dump root private */
  1823. }
  1824. retval = coredump_wait(exit_code, &core_state);
  1825. if (retval < 0)
  1826. goto fail_creds;
  1827. old_cred = override_creds(cred);
  1828. /*
  1829. * Clear any false indication of pending signals that might
  1830. * be seen by the filesystem code called to write the core file.
  1831. */
  1832. clear_thread_flag(TIF_SIGPENDING);
  1833. ispipe = format_corename(&cn, signr);
  1834. if (ispipe) {
  1835. int dump_count;
  1836. char **helper_argv;
  1837. if (ispipe < 0) {
  1838. printk(KERN_WARNING "format_corename failed\n");
  1839. printk(KERN_WARNING "Aborting core\n");
  1840. goto fail_corename;
  1841. }
  1842. if (cprm.limit == 1) {
  1843. /*
  1844. * Normally core limits are irrelevant to pipes, since
  1845. * we're not writing to the file system, but we use
  1846. * cprm.limit of 1 here as a speacial value. Any
  1847. * non-1 limit gets set to RLIM_INFINITY below, but
  1848. * a limit of 0 skips the dump. This is a consistent
  1849. * way to catch recursive crashes. We can still crash
  1850. * if the core_pattern binary sets RLIM_CORE = !1
  1851. * but it runs as root, and can do lots of stupid things
  1852. * Note that we use task_tgid_vnr here to grab the pid
  1853. * of the process group leader. That way we get the
  1854. * right pid if a thread in a multi-threaded
  1855. * core_pattern process dies.
  1856. */
  1857. printk(KERN_WARNING
  1858. "Process %d(%s) has RLIMIT_CORE set to 1\n",
  1859. task_tgid_vnr(current), current->comm);
  1860. printk(KERN_WARNING "Aborting core\n");
  1861. goto fail_unlock;
  1862. }
  1863. cprm.limit = RLIM_INFINITY;
  1864. dump_count = atomic_inc_return(&core_dump_count);
  1865. if (core_pipe_limit && (core_pipe_limit < dump_count)) {
  1866. printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
  1867. task_tgid_vnr(current), current->comm);
  1868. printk(KERN_WARNING "Skipping core dump\n");
  1869. goto fail_dropcount;
  1870. }
  1871. helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
  1872. if (!helper_argv) {
  1873. printk(KERN_WARNING "%s failed to allocate memory\n",
  1874. __func__);
  1875. goto fail_dropcount;
  1876. }
  1877. retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
  1878. NULL, UMH_WAIT_EXEC, umh_pipe_setup,
  1879. NULL, &cprm);
  1880. argv_free(helper_argv);
  1881. if (retval) {
  1882. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1883. cn.corename);
  1884. goto close_fail;
  1885. }
  1886. } else {
  1887. struct inode *inode;
  1888. if (cprm.limit < binfmt->min_coredump)
  1889. goto fail_unlock;
  1890. cprm.file = filp_open(cn.corename,
  1891. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1892. 0600);
  1893. if (IS_ERR(cprm.file))
  1894. goto fail_unlock;
  1895. inode = cprm.file->f_path.dentry->d_inode;
  1896. if (inode->i_nlink > 1)
  1897. goto close_fail;
  1898. if (d_unhashed(cprm.file->f_path.dentry))
  1899. goto close_fail;
  1900. /*
  1901. * AK: actually i see no reason to not allow this for named
  1902. * pipes etc, but keep the previous behaviour for now.
  1903. */
  1904. if (!S_ISREG(inode->i_mode))
  1905. goto close_fail;
  1906. /*
  1907. * Dont allow local users get cute and trick others to coredump
  1908. * into their pre-created files.
  1909. */
  1910. if (inode->i_uid != current_fsuid())
  1911. goto close_fail;
  1912. if (!cprm.file->f_op || !cprm.file->f_op->write)
  1913. goto close_fail;
  1914. if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
  1915. goto close_fail;
  1916. }
  1917. retval = binfmt->core_dump(&cprm);
  1918. if (retval)
  1919. current->signal->group_exit_code |= 0x80;
  1920. if (ispipe && core_pipe_limit)
  1921. wait_for_dump_helpers(cprm.file);
  1922. close_fail:
  1923. if (cprm.file)
  1924. filp_close(cprm.file, NULL);
  1925. fail_dropcount:
  1926. if (ispipe)
  1927. atomic_dec(&core_dump_count);
  1928. fail_unlock:
  1929. kfree(cn.corename);
  1930. fail_corename:
  1931. coredump_finish(mm);
  1932. revert_creds(old_cred);
  1933. fail_creds:
  1934. put_cred(cred);
  1935. fail:
  1936. return;
  1937. }
  1938. /*
  1939. * Core dumping helper functions. These are the only things you should
  1940. * do on a core-file: use only these functions to write out all the
  1941. * necessary info.
  1942. */
  1943. int dump_write(struct file *file, const void *addr, int nr)
  1944. {
  1945. return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1946. }
  1947. EXPORT_SYMBOL(dump_write);
  1948. int dump_seek(struct file *file, loff_t off)
  1949. {
  1950. int ret = 1;
  1951. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1952. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1953. return 0;
  1954. } else {
  1955. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1956. if (!buf)
  1957. return 0;
  1958. while (off > 0) {
  1959. unsigned long n = off;
  1960. if (n > PAGE_SIZE)
  1961. n = PAGE_SIZE;
  1962. if (!dump_write(file, buf, n)) {
  1963. ret = 0;
  1964. break;
  1965. }
  1966. off -= n;
  1967. }
  1968. free_page((unsigned long)buf);
  1969. }
  1970. return ret;
  1971. }
  1972. EXPORT_SYMBOL(dump_seek);