exec.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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_state and changing tsk->mm.
  615. */
  616. down_read(&old_mm->mmap_sem);
  617. if (unlikely(old_mm->core_state)) {
  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. mm_update_next_owner(old_mm);
  629. arch_pick_mmap_layout(mm);
  630. if (old_mm) {
  631. up_read(&old_mm->mmap_sem);
  632. BUG_ON(active_mm != old_mm);
  633. mmput(old_mm);
  634. return 0;
  635. }
  636. mmdrop(active_mm);
  637. return 0;
  638. }
  639. /*
  640. * This function makes sure the current process has its own signal table,
  641. * so that flush_signal_handlers can later reset the handlers without
  642. * disturbing other processes. (Other processes might share the signal
  643. * table via the CLONE_SIGHAND option to clone().)
  644. */
  645. static int de_thread(struct task_struct *tsk)
  646. {
  647. struct signal_struct *sig = tsk->signal;
  648. struct sighand_struct *oldsighand = tsk->sighand;
  649. spinlock_t *lock = &oldsighand->siglock;
  650. struct task_struct *leader = NULL;
  651. int count;
  652. if (thread_group_empty(tsk))
  653. goto no_thread_group;
  654. /*
  655. * Kill all other threads in the thread group.
  656. */
  657. spin_lock_irq(lock);
  658. if (signal_group_exit(sig)) {
  659. /*
  660. * Another group action in progress, just
  661. * return so that the signal is processed.
  662. */
  663. spin_unlock_irq(lock);
  664. return -EAGAIN;
  665. }
  666. sig->group_exit_task = tsk;
  667. zap_other_threads(tsk);
  668. /* Account for the thread group leader hanging around: */
  669. count = thread_group_leader(tsk) ? 1 : 2;
  670. sig->notify_count = count;
  671. while (atomic_read(&sig->count) > count) {
  672. __set_current_state(TASK_UNINTERRUPTIBLE);
  673. spin_unlock_irq(lock);
  674. schedule();
  675. spin_lock_irq(lock);
  676. }
  677. spin_unlock_irq(lock);
  678. /*
  679. * At this point all other threads have exited, all we have to
  680. * do is to wait for the thread group leader to become inactive,
  681. * and to assume its PID:
  682. */
  683. if (!thread_group_leader(tsk)) {
  684. leader = tsk->group_leader;
  685. sig->notify_count = -1; /* for exit_notify() */
  686. for (;;) {
  687. write_lock_irq(&tasklist_lock);
  688. if (likely(leader->exit_state))
  689. break;
  690. __set_current_state(TASK_UNINTERRUPTIBLE);
  691. write_unlock_irq(&tasklist_lock);
  692. schedule();
  693. }
  694. if (unlikely(task_child_reaper(tsk) == leader))
  695. task_active_pid_ns(tsk)->child_reaper = tsk;
  696. /*
  697. * The only record we have of the real-time age of a
  698. * process, regardless of execs it's done, is start_time.
  699. * All the past CPU time is accumulated in signal_struct
  700. * from sister threads now dead. But in this non-leader
  701. * exec, nothing survives from the original leader thread,
  702. * whose birth marks the true age of this process now.
  703. * When we take on its identity by switching to its PID, we
  704. * also take its birthdate (always earlier than our own).
  705. */
  706. tsk->start_time = leader->start_time;
  707. BUG_ON(!same_thread_group(leader, tsk));
  708. BUG_ON(has_group_leader_pid(tsk));
  709. /*
  710. * An exec() starts a new thread group with the
  711. * TGID of the previous thread group. Rehash the
  712. * two threads with a switched PID, and release
  713. * the former thread group leader:
  714. */
  715. /* Become a process group leader with the old leader's pid.
  716. * The old leader becomes a thread of the this thread group.
  717. * Note: The old leader also uses this pid until release_task
  718. * is called. Odd but simple and correct.
  719. */
  720. detach_pid(tsk, PIDTYPE_PID);
  721. tsk->pid = leader->pid;
  722. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  723. transfer_pid(leader, tsk, PIDTYPE_PGID);
  724. transfer_pid(leader, tsk, PIDTYPE_SID);
  725. list_replace_rcu(&leader->tasks, &tsk->tasks);
  726. tsk->group_leader = tsk;
  727. leader->group_leader = tsk;
  728. tsk->exit_signal = SIGCHLD;
  729. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  730. leader->exit_state = EXIT_DEAD;
  731. write_unlock_irq(&tasklist_lock);
  732. }
  733. sig->group_exit_task = NULL;
  734. sig->notify_count = 0;
  735. no_thread_group:
  736. exit_itimers(sig);
  737. flush_itimer_signals();
  738. if (leader)
  739. release_task(leader);
  740. if (atomic_read(&oldsighand->count) != 1) {
  741. struct sighand_struct *newsighand;
  742. /*
  743. * This ->sighand is shared with the CLONE_SIGHAND
  744. * but not CLONE_THREAD task, switch to the new one.
  745. */
  746. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  747. if (!newsighand)
  748. return -ENOMEM;
  749. atomic_set(&newsighand->count, 1);
  750. memcpy(newsighand->action, oldsighand->action,
  751. sizeof(newsighand->action));
  752. write_lock_irq(&tasklist_lock);
  753. spin_lock(&oldsighand->siglock);
  754. rcu_assign_pointer(tsk->sighand, newsighand);
  755. spin_unlock(&oldsighand->siglock);
  756. write_unlock_irq(&tasklist_lock);
  757. __cleanup_sighand(oldsighand);
  758. }
  759. BUG_ON(!thread_group_leader(tsk));
  760. return 0;
  761. }
  762. /*
  763. * These functions flushes out all traces of the currently running executable
  764. * so that a new one can be started
  765. */
  766. static void flush_old_files(struct files_struct * files)
  767. {
  768. long j = -1;
  769. struct fdtable *fdt;
  770. spin_lock(&files->file_lock);
  771. for (;;) {
  772. unsigned long set, i;
  773. j++;
  774. i = j * __NFDBITS;
  775. fdt = files_fdtable(files);
  776. if (i >= fdt->max_fds)
  777. break;
  778. set = fdt->close_on_exec->fds_bits[j];
  779. if (!set)
  780. continue;
  781. fdt->close_on_exec->fds_bits[j] = 0;
  782. spin_unlock(&files->file_lock);
  783. for ( ; set ; i++,set >>= 1) {
  784. if (set & 1) {
  785. sys_close(i);
  786. }
  787. }
  788. spin_lock(&files->file_lock);
  789. }
  790. spin_unlock(&files->file_lock);
  791. }
  792. char *get_task_comm(char *buf, struct task_struct *tsk)
  793. {
  794. /* buf must be at least sizeof(tsk->comm) in size */
  795. task_lock(tsk);
  796. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  797. task_unlock(tsk);
  798. return buf;
  799. }
  800. void set_task_comm(struct task_struct *tsk, char *buf)
  801. {
  802. task_lock(tsk);
  803. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  804. task_unlock(tsk);
  805. }
  806. int flush_old_exec(struct linux_binprm * bprm)
  807. {
  808. char * name;
  809. int i, ch, retval;
  810. char tcomm[sizeof(current->comm)];
  811. /*
  812. * Make sure we have a private signal table and that
  813. * we are unassociated from the previous thread group.
  814. */
  815. retval = de_thread(current);
  816. if (retval)
  817. goto out;
  818. set_mm_exe_file(bprm->mm, bprm->file);
  819. /*
  820. * Release all of the old mmap stuff
  821. */
  822. retval = exec_mmap(bprm->mm);
  823. if (retval)
  824. goto out;
  825. bprm->mm = NULL; /* We're using it now */
  826. /* This is the point of no return */
  827. current->sas_ss_sp = current->sas_ss_size = 0;
  828. if (current->euid == current->uid && current->egid == current->gid)
  829. set_dumpable(current->mm, 1);
  830. else
  831. set_dumpable(current->mm, suid_dumpable);
  832. name = bprm->filename;
  833. /* Copies the binary name from after last slash */
  834. for (i=0; (ch = *(name++)) != '\0';) {
  835. if (ch == '/')
  836. i = 0; /* overwrite what we wrote */
  837. else
  838. if (i < (sizeof(tcomm) - 1))
  839. tcomm[i++] = ch;
  840. }
  841. tcomm[i] = '\0';
  842. set_task_comm(current, tcomm);
  843. current->flags &= ~PF_RANDOMIZE;
  844. flush_thread();
  845. /* Set the new mm task size. We have to do that late because it may
  846. * depend on TIF_32BIT which is only updated in flush_thread() on
  847. * some architectures like powerpc
  848. */
  849. current->mm->task_size = TASK_SIZE;
  850. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) {
  851. suid_keys(current);
  852. set_dumpable(current->mm, suid_dumpable);
  853. current->pdeath_signal = 0;
  854. } else if (file_permission(bprm->file, MAY_READ) ||
  855. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  856. suid_keys(current);
  857. set_dumpable(current->mm, suid_dumpable);
  858. }
  859. /* An exec changes our domain. We are no longer part of the thread
  860. group */
  861. current->self_exec_id++;
  862. flush_signal_handlers(current, 0);
  863. flush_old_files(current->files);
  864. return 0;
  865. out:
  866. return retval;
  867. }
  868. EXPORT_SYMBOL(flush_old_exec);
  869. /*
  870. * Fill the binprm structure from the inode.
  871. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  872. */
  873. int prepare_binprm(struct linux_binprm *bprm)
  874. {
  875. int mode;
  876. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  877. int retval;
  878. mode = inode->i_mode;
  879. if (bprm->file->f_op == NULL)
  880. return -EACCES;
  881. bprm->e_uid = current->euid;
  882. bprm->e_gid = current->egid;
  883. if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  884. /* Set-uid? */
  885. if (mode & S_ISUID) {
  886. current->personality &= ~PER_CLEAR_ON_SETID;
  887. bprm->e_uid = inode->i_uid;
  888. }
  889. /* Set-gid? */
  890. /*
  891. * If setgid is set but no group execute bit then this
  892. * is a candidate for mandatory locking, not a setgid
  893. * executable.
  894. */
  895. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  896. current->personality &= ~PER_CLEAR_ON_SETID;
  897. bprm->e_gid = inode->i_gid;
  898. }
  899. }
  900. /* fill in binprm security blob */
  901. retval = security_bprm_set(bprm);
  902. if (retval)
  903. return retval;
  904. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  905. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  906. }
  907. EXPORT_SYMBOL(prepare_binprm);
  908. static int unsafe_exec(struct task_struct *p)
  909. {
  910. int unsafe = 0;
  911. if (p->ptrace & PT_PTRACED) {
  912. if (p->ptrace & PT_PTRACE_CAP)
  913. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  914. else
  915. unsafe |= LSM_UNSAFE_PTRACE;
  916. }
  917. if (atomic_read(&p->fs->count) > 1 ||
  918. atomic_read(&p->files->count) > 1 ||
  919. atomic_read(&p->sighand->count) > 1)
  920. unsafe |= LSM_UNSAFE_SHARE;
  921. return unsafe;
  922. }
  923. void compute_creds(struct linux_binprm *bprm)
  924. {
  925. int unsafe;
  926. if (bprm->e_uid != current->uid) {
  927. suid_keys(current);
  928. current->pdeath_signal = 0;
  929. }
  930. exec_keys(current);
  931. task_lock(current);
  932. unsafe = unsafe_exec(current);
  933. security_bprm_apply_creds(bprm, unsafe);
  934. task_unlock(current);
  935. security_bprm_post_apply_creds(bprm);
  936. }
  937. EXPORT_SYMBOL(compute_creds);
  938. /*
  939. * Arguments are '\0' separated strings found at the location bprm->p
  940. * points to; chop off the first by relocating brpm->p to right after
  941. * the first '\0' encountered.
  942. */
  943. int remove_arg_zero(struct linux_binprm *bprm)
  944. {
  945. int ret = 0;
  946. unsigned long offset;
  947. char *kaddr;
  948. struct page *page;
  949. if (!bprm->argc)
  950. return 0;
  951. do {
  952. offset = bprm->p & ~PAGE_MASK;
  953. page = get_arg_page(bprm, bprm->p, 0);
  954. if (!page) {
  955. ret = -EFAULT;
  956. goto out;
  957. }
  958. kaddr = kmap_atomic(page, KM_USER0);
  959. for (; offset < PAGE_SIZE && kaddr[offset];
  960. offset++, bprm->p++)
  961. ;
  962. kunmap_atomic(kaddr, KM_USER0);
  963. put_arg_page(page);
  964. if (offset == PAGE_SIZE)
  965. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  966. } while (offset == PAGE_SIZE);
  967. bprm->p++;
  968. bprm->argc--;
  969. ret = 0;
  970. out:
  971. return ret;
  972. }
  973. EXPORT_SYMBOL(remove_arg_zero);
  974. /*
  975. * cycle the list of binary formats handler, until one recognizes the image
  976. */
  977. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  978. {
  979. int try,retval;
  980. struct linux_binfmt *fmt;
  981. #ifdef __alpha__
  982. /* handle /sbin/loader.. */
  983. {
  984. struct exec * eh = (struct exec *) bprm->buf;
  985. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  986. (eh->fh.f_flags & 0x3000) == 0x3000)
  987. {
  988. struct file * file;
  989. unsigned long loader;
  990. allow_write_access(bprm->file);
  991. fput(bprm->file);
  992. bprm->file = NULL;
  993. loader = bprm->vma->vm_end - sizeof(void *);
  994. file = open_exec("/sbin/loader");
  995. retval = PTR_ERR(file);
  996. if (IS_ERR(file))
  997. return retval;
  998. /* Remember if the application is TASO. */
  999. bprm->sh_bang = eh->ah.entry < 0x100000000UL;
  1000. bprm->file = file;
  1001. bprm->loader = loader;
  1002. retval = prepare_binprm(bprm);
  1003. if (retval<0)
  1004. return retval;
  1005. /* should call search_binary_handler recursively here,
  1006. but it does not matter */
  1007. }
  1008. }
  1009. #endif
  1010. retval = security_bprm_check(bprm);
  1011. if (retval)
  1012. return retval;
  1013. /* kernel module loader fixup */
  1014. /* so we don't try to load run modprobe in kernel space. */
  1015. set_fs(USER_DS);
  1016. retval = audit_bprm(bprm);
  1017. if (retval)
  1018. return retval;
  1019. retval = -ENOENT;
  1020. for (try=0; try<2; try++) {
  1021. read_lock(&binfmt_lock);
  1022. list_for_each_entry(fmt, &formats, lh) {
  1023. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1024. if (!fn)
  1025. continue;
  1026. if (!try_module_get(fmt->module))
  1027. continue;
  1028. read_unlock(&binfmt_lock);
  1029. retval = fn(bprm, regs);
  1030. if (retval >= 0) {
  1031. put_binfmt(fmt);
  1032. allow_write_access(bprm->file);
  1033. if (bprm->file)
  1034. fput(bprm->file);
  1035. bprm->file = NULL;
  1036. current->did_exec = 1;
  1037. proc_exec_connector(current);
  1038. return retval;
  1039. }
  1040. read_lock(&binfmt_lock);
  1041. put_binfmt(fmt);
  1042. if (retval != -ENOEXEC || bprm->mm == NULL)
  1043. break;
  1044. if (!bprm->file) {
  1045. read_unlock(&binfmt_lock);
  1046. return retval;
  1047. }
  1048. }
  1049. read_unlock(&binfmt_lock);
  1050. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1051. break;
  1052. #ifdef CONFIG_KMOD
  1053. }else{
  1054. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1055. if (printable(bprm->buf[0]) &&
  1056. printable(bprm->buf[1]) &&
  1057. printable(bprm->buf[2]) &&
  1058. printable(bprm->buf[3]))
  1059. break; /* -ENOEXEC */
  1060. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1061. #endif
  1062. }
  1063. }
  1064. return retval;
  1065. }
  1066. EXPORT_SYMBOL(search_binary_handler);
  1067. void free_bprm(struct linux_binprm *bprm)
  1068. {
  1069. free_arg_pages(bprm);
  1070. kfree(bprm);
  1071. }
  1072. /*
  1073. * sys_execve() executes a new program.
  1074. */
  1075. int do_execve(char * filename,
  1076. char __user *__user *argv,
  1077. char __user *__user *envp,
  1078. struct pt_regs * regs)
  1079. {
  1080. struct linux_binprm *bprm;
  1081. struct file *file;
  1082. struct files_struct *displaced;
  1083. int retval;
  1084. retval = unshare_files(&displaced);
  1085. if (retval)
  1086. goto out_ret;
  1087. retval = -ENOMEM;
  1088. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1089. if (!bprm)
  1090. goto out_files;
  1091. file = open_exec(filename);
  1092. retval = PTR_ERR(file);
  1093. if (IS_ERR(file))
  1094. goto out_kfree;
  1095. sched_exec();
  1096. bprm->file = file;
  1097. bprm->filename = filename;
  1098. bprm->interp = filename;
  1099. retval = bprm_mm_init(bprm);
  1100. if (retval)
  1101. goto out_file;
  1102. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1103. if ((retval = bprm->argc) < 0)
  1104. goto out_mm;
  1105. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1106. if ((retval = bprm->envc) < 0)
  1107. goto out_mm;
  1108. retval = security_bprm_alloc(bprm);
  1109. if (retval)
  1110. goto out;
  1111. retval = prepare_binprm(bprm);
  1112. if (retval < 0)
  1113. goto out;
  1114. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1115. if (retval < 0)
  1116. goto out;
  1117. bprm->exec = bprm->p;
  1118. retval = copy_strings(bprm->envc, envp, bprm);
  1119. if (retval < 0)
  1120. goto out;
  1121. retval = copy_strings(bprm->argc, argv, bprm);
  1122. if (retval < 0)
  1123. goto out;
  1124. current->flags &= ~PF_KTHREAD;
  1125. retval = search_binary_handler(bprm,regs);
  1126. if (retval >= 0) {
  1127. /* execve success */
  1128. security_bprm_free(bprm);
  1129. acct_update_integrals(current);
  1130. free_bprm(bprm);
  1131. if (displaced)
  1132. put_files_struct(displaced);
  1133. return retval;
  1134. }
  1135. out:
  1136. if (bprm->security)
  1137. security_bprm_free(bprm);
  1138. out_mm:
  1139. if (bprm->mm)
  1140. mmput (bprm->mm);
  1141. out_file:
  1142. if (bprm->file) {
  1143. allow_write_access(bprm->file);
  1144. fput(bprm->file);
  1145. }
  1146. out_kfree:
  1147. free_bprm(bprm);
  1148. out_files:
  1149. if (displaced)
  1150. reset_files_struct(displaced);
  1151. out_ret:
  1152. return retval;
  1153. }
  1154. int set_binfmt(struct linux_binfmt *new)
  1155. {
  1156. struct linux_binfmt *old = current->binfmt;
  1157. if (new) {
  1158. if (!try_module_get(new->module))
  1159. return -1;
  1160. }
  1161. current->binfmt = new;
  1162. if (old)
  1163. module_put(old->module);
  1164. return 0;
  1165. }
  1166. EXPORT_SYMBOL(set_binfmt);
  1167. /* format_corename will inspect the pattern parameter, and output a
  1168. * name into corename, which must have space for at least
  1169. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1170. */
  1171. static int format_corename(char *corename, const char *pattern, long signr)
  1172. {
  1173. const char *pat_ptr = pattern;
  1174. char *out_ptr = corename;
  1175. char *const out_end = corename + CORENAME_MAX_SIZE;
  1176. int rc;
  1177. int pid_in_pattern = 0;
  1178. int ispipe = 0;
  1179. if (*pattern == '|')
  1180. ispipe = 1;
  1181. /* Repeat as long as we have more pattern to process and more output
  1182. space */
  1183. while (*pat_ptr) {
  1184. if (*pat_ptr != '%') {
  1185. if (out_ptr == out_end)
  1186. goto out;
  1187. *out_ptr++ = *pat_ptr++;
  1188. } else {
  1189. switch (*++pat_ptr) {
  1190. case 0:
  1191. goto out;
  1192. /* Double percent, output one percent */
  1193. case '%':
  1194. if (out_ptr == out_end)
  1195. goto out;
  1196. *out_ptr++ = '%';
  1197. break;
  1198. /* pid */
  1199. case 'p':
  1200. pid_in_pattern = 1;
  1201. rc = snprintf(out_ptr, out_end - out_ptr,
  1202. "%d", task_tgid_vnr(current));
  1203. if (rc > out_end - out_ptr)
  1204. goto out;
  1205. out_ptr += rc;
  1206. break;
  1207. /* uid */
  1208. case 'u':
  1209. rc = snprintf(out_ptr, out_end - out_ptr,
  1210. "%d", current->uid);
  1211. if (rc > out_end - out_ptr)
  1212. goto out;
  1213. out_ptr += rc;
  1214. break;
  1215. /* gid */
  1216. case 'g':
  1217. rc = snprintf(out_ptr, out_end - out_ptr,
  1218. "%d", current->gid);
  1219. if (rc > out_end - out_ptr)
  1220. goto out;
  1221. out_ptr += rc;
  1222. break;
  1223. /* signal that caused the coredump */
  1224. case 's':
  1225. rc = snprintf(out_ptr, out_end - out_ptr,
  1226. "%ld", signr);
  1227. if (rc > out_end - out_ptr)
  1228. goto out;
  1229. out_ptr += rc;
  1230. break;
  1231. /* UNIX time of coredump */
  1232. case 't': {
  1233. struct timeval tv;
  1234. do_gettimeofday(&tv);
  1235. rc = snprintf(out_ptr, out_end - out_ptr,
  1236. "%lu", tv.tv_sec);
  1237. if (rc > out_end - out_ptr)
  1238. goto out;
  1239. out_ptr += rc;
  1240. break;
  1241. }
  1242. /* hostname */
  1243. case 'h':
  1244. down_read(&uts_sem);
  1245. rc = snprintf(out_ptr, out_end - out_ptr,
  1246. "%s", utsname()->nodename);
  1247. up_read(&uts_sem);
  1248. if (rc > out_end - out_ptr)
  1249. goto out;
  1250. out_ptr += rc;
  1251. break;
  1252. /* executable */
  1253. case 'e':
  1254. rc = snprintf(out_ptr, out_end - out_ptr,
  1255. "%s", current->comm);
  1256. if (rc > out_end - out_ptr)
  1257. goto out;
  1258. out_ptr += rc;
  1259. break;
  1260. /* core limit size */
  1261. case 'c':
  1262. rc = snprintf(out_ptr, out_end - out_ptr,
  1263. "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
  1264. if (rc > out_end - out_ptr)
  1265. goto out;
  1266. out_ptr += rc;
  1267. break;
  1268. default:
  1269. break;
  1270. }
  1271. ++pat_ptr;
  1272. }
  1273. }
  1274. /* Backward compatibility with core_uses_pid:
  1275. *
  1276. * If core_pattern does not include a %p (as is the default)
  1277. * and core_uses_pid is set, then .%pid will be appended to
  1278. * the filename. Do not do this for piped commands. */
  1279. if (!ispipe && !pid_in_pattern
  1280. && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
  1281. rc = snprintf(out_ptr, out_end - out_ptr,
  1282. ".%d", task_tgid_vnr(current));
  1283. if (rc > out_end - out_ptr)
  1284. goto out;
  1285. out_ptr += rc;
  1286. }
  1287. out:
  1288. *out_ptr = 0;
  1289. return ispipe;
  1290. }
  1291. static int zap_process(struct task_struct *start)
  1292. {
  1293. struct task_struct *t;
  1294. int nr = 0;
  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. sigaddset(&t->pending.signal, SIGKILL);
  1301. signal_wake_up(t, 1);
  1302. nr++;
  1303. }
  1304. } while_each_thread(start, t);
  1305. return nr;
  1306. }
  1307. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1308. struct core_state *core_state, int exit_code)
  1309. {
  1310. struct task_struct *g, *p;
  1311. unsigned long flags;
  1312. int nr = -EAGAIN;
  1313. spin_lock_irq(&tsk->sighand->siglock);
  1314. if (!signal_group_exit(tsk->signal)) {
  1315. mm->core_state = core_state;
  1316. tsk->signal->group_exit_code = exit_code;
  1317. nr = zap_process(tsk);
  1318. }
  1319. spin_unlock_irq(&tsk->sighand->siglock);
  1320. if (unlikely(nr < 0))
  1321. return nr;
  1322. if (atomic_read(&mm->mm_users) == nr + 1)
  1323. goto done;
  1324. /*
  1325. * We should find and kill all tasks which use this mm, and we should
  1326. * count them correctly into ->nr_threads. We don't take tasklist
  1327. * lock, but this is safe wrt:
  1328. *
  1329. * fork:
  1330. * None of sub-threads can fork after zap_process(leader). All
  1331. * processes which were created before this point should be
  1332. * visible to zap_threads() because copy_process() adds the new
  1333. * process to the tail of init_task.tasks list, and lock/unlock
  1334. * of ->siglock provides a memory barrier.
  1335. *
  1336. * do_exit:
  1337. * The caller holds mm->mmap_sem. This means that the task which
  1338. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1339. * its ->mm.
  1340. *
  1341. * de_thread:
  1342. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1343. * we must see either old or new leader, this does not matter.
  1344. * However, it can change p->sighand, so lock_task_sighand(p)
  1345. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1346. * it can't fail.
  1347. *
  1348. * Note also that "g" can be the old leader with ->mm == NULL
  1349. * and already unhashed and thus removed from ->thread_group.
  1350. * This is OK, __unhash_process()->list_del_rcu() does not
  1351. * clear the ->next pointer, we will find the new leader via
  1352. * next_thread().
  1353. */
  1354. rcu_read_lock();
  1355. for_each_process(g) {
  1356. if (g == tsk->group_leader)
  1357. continue;
  1358. if (g->flags & PF_KTHREAD)
  1359. continue;
  1360. p = g;
  1361. do {
  1362. if (p->mm) {
  1363. if (unlikely(p->mm == mm)) {
  1364. lock_task_sighand(p, &flags);
  1365. nr += zap_process(p);
  1366. unlock_task_sighand(p, &flags);
  1367. }
  1368. break;
  1369. }
  1370. } while_each_thread(g, p);
  1371. }
  1372. rcu_read_unlock();
  1373. done:
  1374. atomic_set(&core_state->nr_threads, nr);
  1375. return nr;
  1376. }
  1377. static int coredump_wait(int exit_code, struct core_state *core_state)
  1378. {
  1379. struct task_struct *tsk = current;
  1380. struct mm_struct *mm = tsk->mm;
  1381. struct completion *vfork_done;
  1382. int core_waiters;
  1383. init_completion(&core_state->startup);
  1384. core_state->dumper.task = tsk;
  1385. core_state->dumper.next = NULL;
  1386. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1387. up_write(&mm->mmap_sem);
  1388. if (unlikely(core_waiters < 0))
  1389. goto fail;
  1390. /*
  1391. * Make sure nobody is waiting for us to release the VM,
  1392. * otherwise we can deadlock when we wait on each other
  1393. */
  1394. vfork_done = tsk->vfork_done;
  1395. if (vfork_done) {
  1396. tsk->vfork_done = NULL;
  1397. complete(vfork_done);
  1398. }
  1399. if (core_waiters)
  1400. wait_for_completion(&core_state->startup);
  1401. fail:
  1402. return core_waiters;
  1403. }
  1404. static void coredump_finish(struct mm_struct *mm)
  1405. {
  1406. struct core_thread *curr, *next;
  1407. struct task_struct *task;
  1408. next = mm->core_state->dumper.next;
  1409. while ((curr = next) != NULL) {
  1410. next = curr->next;
  1411. task = curr->task;
  1412. /*
  1413. * see exit_mm(), curr->task must not see
  1414. * ->task == NULL before we read ->next.
  1415. */
  1416. smp_mb();
  1417. curr->task = NULL;
  1418. wake_up_process(task);
  1419. }
  1420. mm->core_state = NULL;
  1421. }
  1422. /*
  1423. * set_dumpable converts traditional three-value dumpable to two flags and
  1424. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1425. * these bits are not changed atomically. So get_dumpable can observe the
  1426. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1427. * return either old dumpable or new one by paying attention to the order of
  1428. * modifying the bits.
  1429. *
  1430. * dumpable | mm->flags (binary)
  1431. * old new | initial interim final
  1432. * ---------+-----------------------
  1433. * 0 1 | 00 01 01
  1434. * 0 2 | 00 10(*) 11
  1435. * 1 0 | 01 00 00
  1436. * 1 2 | 01 11 11
  1437. * 2 0 | 11 10(*) 00
  1438. * 2 1 | 11 11 01
  1439. *
  1440. * (*) get_dumpable regards interim value of 10 as 11.
  1441. */
  1442. void set_dumpable(struct mm_struct *mm, int value)
  1443. {
  1444. switch (value) {
  1445. case 0:
  1446. clear_bit(MMF_DUMPABLE, &mm->flags);
  1447. smp_wmb();
  1448. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1449. break;
  1450. case 1:
  1451. set_bit(MMF_DUMPABLE, &mm->flags);
  1452. smp_wmb();
  1453. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1454. break;
  1455. case 2:
  1456. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1457. smp_wmb();
  1458. set_bit(MMF_DUMPABLE, &mm->flags);
  1459. break;
  1460. }
  1461. }
  1462. int get_dumpable(struct mm_struct *mm)
  1463. {
  1464. int ret;
  1465. ret = mm->flags & 0x3;
  1466. return (ret >= 2) ? 2 : ret;
  1467. }
  1468. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1469. {
  1470. struct core_state core_state;
  1471. char corename[CORENAME_MAX_SIZE + 1];
  1472. struct mm_struct *mm = current->mm;
  1473. struct linux_binfmt * binfmt;
  1474. struct inode * inode;
  1475. struct file * file;
  1476. int retval = 0;
  1477. int fsuid = current->fsuid;
  1478. int flag = 0;
  1479. int ispipe = 0;
  1480. unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1481. char **helper_argv = NULL;
  1482. int helper_argc = 0;
  1483. char *delimit;
  1484. audit_core_dumps(signr);
  1485. binfmt = current->binfmt;
  1486. if (!binfmt || !binfmt->core_dump)
  1487. goto fail;
  1488. down_write(&mm->mmap_sem);
  1489. /*
  1490. * If another thread got here first, or we are not dumpable, bail out.
  1491. */
  1492. if (mm->core_state || !get_dumpable(mm)) {
  1493. up_write(&mm->mmap_sem);
  1494. goto fail;
  1495. }
  1496. /*
  1497. * We cannot trust fsuid as being the "true" uid of the
  1498. * process nor do we know its entire history. We only know it
  1499. * was tainted so we dump it as root in mode 2.
  1500. */
  1501. if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
  1502. flag = O_EXCL; /* Stop rewrite attacks */
  1503. current->fsuid = 0; /* Dump root private */
  1504. }
  1505. retval = coredump_wait(exit_code, &core_state);
  1506. if (retval < 0)
  1507. goto fail;
  1508. /*
  1509. * Clear any false indication of pending signals that might
  1510. * be seen by the filesystem code called to write the core file.
  1511. */
  1512. clear_thread_flag(TIF_SIGPENDING);
  1513. /*
  1514. * lock_kernel() because format_corename() is controlled by sysctl, which
  1515. * uses lock_kernel()
  1516. */
  1517. lock_kernel();
  1518. ispipe = format_corename(corename, core_pattern, signr);
  1519. unlock_kernel();
  1520. /*
  1521. * Don't bother to check the RLIMIT_CORE value if core_pattern points
  1522. * to a pipe. Since we're not writing directly to the filesystem
  1523. * RLIMIT_CORE doesn't really apply, as no actual core file will be
  1524. * created unless the pipe reader choses to write out the core file
  1525. * at which point file size limits and permissions will be imposed
  1526. * as it does with any other process
  1527. */
  1528. if ((!ispipe) && (core_limit < binfmt->min_coredump))
  1529. goto fail_unlock;
  1530. if (ispipe) {
  1531. helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
  1532. /* Terminate the string before the first option */
  1533. delimit = strchr(corename, ' ');
  1534. if (delimit)
  1535. *delimit = '\0';
  1536. delimit = strrchr(helper_argv[0], '/');
  1537. if (delimit)
  1538. delimit++;
  1539. else
  1540. delimit = helper_argv[0];
  1541. if (!strcmp(delimit, current->comm)) {
  1542. printk(KERN_NOTICE "Recursive core dump detected, "
  1543. "aborting\n");
  1544. goto fail_unlock;
  1545. }
  1546. core_limit = RLIM_INFINITY;
  1547. /* SIGPIPE can happen, but it's just never processed */
  1548. if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
  1549. &file)) {
  1550. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1551. corename);
  1552. goto fail_unlock;
  1553. }
  1554. } else
  1555. file = filp_open(corename,
  1556. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1557. 0600);
  1558. if (IS_ERR(file))
  1559. goto fail_unlock;
  1560. inode = file->f_path.dentry->d_inode;
  1561. if (inode->i_nlink > 1)
  1562. goto close_fail; /* multiple links - don't dump */
  1563. if (!ispipe && d_unhashed(file->f_path.dentry))
  1564. goto close_fail;
  1565. /* AK: actually i see no reason to not allow this for named pipes etc.,
  1566. but keep the previous behaviour for now. */
  1567. if (!ispipe && !S_ISREG(inode->i_mode))
  1568. goto close_fail;
  1569. /*
  1570. * Dont allow local users get cute and trick others to coredump
  1571. * into their pre-created files:
  1572. */
  1573. if (inode->i_uid != current->fsuid)
  1574. goto close_fail;
  1575. if (!file->f_op)
  1576. goto close_fail;
  1577. if (!file->f_op->write)
  1578. goto close_fail;
  1579. if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
  1580. goto close_fail;
  1581. retval = binfmt->core_dump(signr, regs, file, core_limit);
  1582. if (retval)
  1583. current->signal->group_exit_code |= 0x80;
  1584. close_fail:
  1585. filp_close(file, NULL);
  1586. fail_unlock:
  1587. if (helper_argv)
  1588. argv_free(helper_argv);
  1589. current->fsuid = fsuid;
  1590. coredump_finish(mm);
  1591. fail:
  1592. return retval;
  1593. }