exec.c 40 KB

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