exec.c 49 KB

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