exec.c 45 KB

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