exec.c 40 KB

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