exec.c 49 KB

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