exec.c 52 KB

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