exec.c 41 KB

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