exec.c 42 KB

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