exec.c 43 KB

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