exec.c 42 KB

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