exec.c 43 KB

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