exec.c 53 KB

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