exec.c 41 KB

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