exec.c 51 KB

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