exec.c 40 KB

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