exec.c 43 KB

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