exec.c 43 KB

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