exec.c 41 KB

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