exec.c 42 KB

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