exec.c 43 KB

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