exec.c 46 KB

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