exec.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  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/mman.h>
  26. #include <linux/a.out.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/string.h>
  31. #include <linux/init.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/highmem.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/key.h>
  36. #include <linux/personality.h>
  37. #include <linux/binfmts.h>
  38. #include <linux/swap.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/ptrace.h>
  45. #include <linux/mount.h>
  46. #include <linux/security.h>
  47. #include <linux/syscalls.h>
  48. #include <linux/rmap.h>
  49. #include <linux/tsacct_kern.h>
  50. #include <linux/cn_proc.h>
  51. #include <linux/audit.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/mmu_context.h>
  54. #include <asm/tlb.h>
  55. #ifdef CONFIG_KMOD
  56. #include <linux/kmod.h>
  57. #endif
  58. int core_uses_pid;
  59. char core_pattern[CORENAME_MAX_SIZE] = "core";
  60. int suid_dumpable = 0;
  61. EXPORT_SYMBOL(suid_dumpable);
  62. /* The maximal length of core_pattern is also specified in sysctl.c */
  63. static LIST_HEAD(formats);
  64. static DEFINE_RWLOCK(binfmt_lock);
  65. int register_binfmt(struct linux_binfmt * fmt)
  66. {
  67. if (!fmt)
  68. return -EINVAL;
  69. write_lock(&binfmt_lock);
  70. list_add(&fmt->lh, &formats);
  71. write_unlock(&binfmt_lock);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(register_binfmt);
  75. void unregister_binfmt(struct linux_binfmt * fmt)
  76. {
  77. write_lock(&binfmt_lock);
  78. list_del(&fmt->lh);
  79. write_unlock(&binfmt_lock);
  80. }
  81. EXPORT_SYMBOL(unregister_binfmt);
  82. static inline void put_binfmt(struct linux_binfmt * fmt)
  83. {
  84. module_put(fmt->module);
  85. }
  86. /*
  87. * Note that a shared library must be both readable and executable due to
  88. * security reasons.
  89. *
  90. * Also note that we take the address to load from from the file itself.
  91. */
  92. asmlinkage long sys_uselib(const char __user * library)
  93. {
  94. struct file * file;
  95. struct nameidata nd;
  96. int error;
  97. error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
  98. if (error)
  99. goto out;
  100. error = -EINVAL;
  101. if (!S_ISREG(nd.dentry->d_inode->i_mode))
  102. goto exit;
  103. error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
  104. if (error)
  105. goto exit;
  106. file = nameidata_to_filp(&nd, O_RDONLY);
  107. error = PTR_ERR(file);
  108. if (IS_ERR(file))
  109. goto out;
  110. error = -ENOEXEC;
  111. if(file->f_op) {
  112. struct linux_binfmt * fmt;
  113. read_lock(&binfmt_lock);
  114. list_for_each_entry(fmt, &formats, lh) {
  115. if (!fmt->load_shlib)
  116. continue;
  117. if (!try_module_get(fmt->module))
  118. continue;
  119. read_unlock(&binfmt_lock);
  120. error = fmt->load_shlib(file);
  121. read_lock(&binfmt_lock);
  122. put_binfmt(fmt);
  123. if (error != -ENOEXEC)
  124. break;
  125. }
  126. read_unlock(&binfmt_lock);
  127. }
  128. fput(file);
  129. out:
  130. return error;
  131. exit:
  132. release_open_intent(&nd);
  133. path_release(&nd);
  134. goto out;
  135. }
  136. #ifdef CONFIG_MMU
  137. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  138. int write)
  139. {
  140. struct page *page;
  141. int ret;
  142. #ifdef CONFIG_STACK_GROWSUP
  143. if (write) {
  144. ret = expand_stack_downwards(bprm->vma, pos);
  145. if (ret < 0)
  146. return NULL;
  147. }
  148. #endif
  149. ret = get_user_pages(current, bprm->mm, pos,
  150. 1, write, 1, &page, NULL);
  151. if (ret <= 0)
  152. return NULL;
  153. if (write) {
  154. struct rlimit *rlim = current->signal->rlim;
  155. unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
  156. /*
  157. * Limit to 1/4-th the stack size for the argv+env strings.
  158. * This ensures that:
  159. * - the remaining binfmt code will not run out of stack space,
  160. * - the program will have a reasonable amount of stack left
  161. * to work from.
  162. */
  163. if (size > rlim[RLIMIT_STACK].rlim_cur / 4) {
  164. put_page(page);
  165. return NULL;
  166. }
  167. }
  168. return page;
  169. }
  170. static void put_arg_page(struct page *page)
  171. {
  172. put_page(page);
  173. }
  174. static void free_arg_page(struct linux_binprm *bprm, int i)
  175. {
  176. }
  177. static void free_arg_pages(struct linux_binprm *bprm)
  178. {
  179. }
  180. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  181. struct page *page)
  182. {
  183. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  184. }
  185. static int __bprm_mm_init(struct linux_binprm *bprm)
  186. {
  187. int err = -ENOMEM;
  188. struct vm_area_struct *vma = NULL;
  189. struct mm_struct *mm = bprm->mm;
  190. bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  191. if (!vma)
  192. goto err;
  193. down_write(&mm->mmap_sem);
  194. vma->vm_mm = mm;
  195. /*
  196. * Place the stack at the largest stack address the architecture
  197. * supports. Later, we'll move this to an appropriate place. We don't
  198. * use STACK_TOP because that can depend on attributes which aren't
  199. * configured yet.
  200. */
  201. vma->vm_end = STACK_TOP_MAX;
  202. vma->vm_start = vma->vm_end - PAGE_SIZE;
  203. vma->vm_flags = VM_STACK_FLAGS;
  204. vma->vm_page_prot = protection_map[vma->vm_flags & 0x7];
  205. err = insert_vm_struct(mm, vma);
  206. if (err) {
  207. up_write(&mm->mmap_sem);
  208. goto err;
  209. }
  210. mm->stack_vm = mm->total_vm = 1;
  211. up_write(&mm->mmap_sem);
  212. bprm->p = vma->vm_end - sizeof(void *);
  213. return 0;
  214. err:
  215. if (vma) {
  216. bprm->vma = NULL;
  217. kmem_cache_free(vm_area_cachep, vma);
  218. }
  219. return err;
  220. }
  221. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  222. {
  223. return len <= MAX_ARG_STRLEN;
  224. }
  225. #else
  226. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  227. int write)
  228. {
  229. struct page *page;
  230. page = bprm->page[pos / PAGE_SIZE];
  231. if (!page && write) {
  232. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  233. if (!page)
  234. return NULL;
  235. bprm->page[pos / PAGE_SIZE] = page;
  236. }
  237. return page;
  238. }
  239. static void put_arg_page(struct page *page)
  240. {
  241. }
  242. static void free_arg_page(struct linux_binprm *bprm, int i)
  243. {
  244. if (bprm->page[i]) {
  245. __free_page(bprm->page[i]);
  246. bprm->page[i] = NULL;
  247. }
  248. }
  249. static void free_arg_pages(struct linux_binprm *bprm)
  250. {
  251. int i;
  252. for (i = 0; i < MAX_ARG_PAGES; i++)
  253. free_arg_page(bprm, i);
  254. }
  255. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  256. struct page *page)
  257. {
  258. }
  259. static int __bprm_mm_init(struct linux_binprm *bprm)
  260. {
  261. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  262. return 0;
  263. }
  264. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  265. {
  266. return len <= bprm->p;
  267. }
  268. #endif /* CONFIG_MMU */
  269. /*
  270. * Create a new mm_struct and populate it with a temporary stack
  271. * vm_area_struct. We don't have enough context at this point to set the stack
  272. * flags, permissions, and offset, so we use temporary values. We'll update
  273. * them later in setup_arg_pages().
  274. */
  275. int bprm_mm_init(struct linux_binprm *bprm)
  276. {
  277. int err;
  278. struct mm_struct *mm = NULL;
  279. bprm->mm = mm = mm_alloc();
  280. err = -ENOMEM;
  281. if (!mm)
  282. goto err;
  283. err = init_new_context(current, mm);
  284. if (err)
  285. goto err;
  286. err = __bprm_mm_init(bprm);
  287. if (err)
  288. goto err;
  289. return 0;
  290. err:
  291. if (mm) {
  292. bprm->mm = NULL;
  293. mmdrop(mm);
  294. }
  295. return err;
  296. }
  297. /*
  298. * count() counts the number of strings in array ARGV.
  299. */
  300. static int count(char __user * __user * argv, int max)
  301. {
  302. int i = 0;
  303. if (argv != NULL) {
  304. for (;;) {
  305. char __user * p;
  306. if (get_user(p, argv))
  307. return -EFAULT;
  308. if (!p)
  309. break;
  310. argv++;
  311. if(++i > max)
  312. return -E2BIG;
  313. cond_resched();
  314. }
  315. }
  316. return i;
  317. }
  318. /*
  319. * 'copy_strings()' copies argument/environment strings from the old
  320. * processes's memory to the new process's stack. The call to get_user_pages()
  321. * ensures the destination page is created and not swapped out.
  322. */
  323. static int copy_strings(int argc, char __user * __user * argv,
  324. struct linux_binprm *bprm)
  325. {
  326. struct page *kmapped_page = NULL;
  327. char *kaddr = NULL;
  328. unsigned long kpos = 0;
  329. int ret;
  330. while (argc-- > 0) {
  331. char __user *str;
  332. int len;
  333. unsigned long pos;
  334. if (get_user(str, argv+argc) ||
  335. !(len = strnlen_user(str, MAX_ARG_STRLEN))) {
  336. ret = -EFAULT;
  337. goto out;
  338. }
  339. if (!valid_arg_len(bprm, len)) {
  340. ret = -E2BIG;
  341. goto out;
  342. }
  343. /* We're going to work our way backwords. */
  344. pos = bprm->p;
  345. str += len;
  346. bprm->p -= len;
  347. while (len > 0) {
  348. int offset, bytes_to_copy;
  349. offset = pos % PAGE_SIZE;
  350. if (offset == 0)
  351. offset = PAGE_SIZE;
  352. bytes_to_copy = offset;
  353. if (bytes_to_copy > len)
  354. bytes_to_copy = len;
  355. offset -= bytes_to_copy;
  356. pos -= bytes_to_copy;
  357. str -= bytes_to_copy;
  358. len -= bytes_to_copy;
  359. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  360. struct page *page;
  361. page = get_arg_page(bprm, pos, 1);
  362. if (!page) {
  363. ret = -E2BIG;
  364. goto out;
  365. }
  366. if (kmapped_page) {
  367. flush_kernel_dcache_page(kmapped_page);
  368. kunmap(kmapped_page);
  369. put_arg_page(kmapped_page);
  370. }
  371. kmapped_page = page;
  372. kaddr = kmap(kmapped_page);
  373. kpos = pos & PAGE_MASK;
  374. flush_arg_page(bprm, kpos, kmapped_page);
  375. }
  376. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  377. ret = -EFAULT;
  378. goto out;
  379. }
  380. }
  381. }
  382. ret = 0;
  383. out:
  384. if (kmapped_page) {
  385. flush_kernel_dcache_page(kmapped_page);
  386. kunmap(kmapped_page);
  387. put_arg_page(kmapped_page);
  388. }
  389. return ret;
  390. }
  391. /*
  392. * Like copy_strings, but get argv and its values from kernel memory.
  393. */
  394. int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
  395. {
  396. int r;
  397. mm_segment_t oldfs = get_fs();
  398. set_fs(KERNEL_DS);
  399. r = copy_strings(argc, (char __user * __user *)argv, bprm);
  400. set_fs(oldfs);
  401. return r;
  402. }
  403. EXPORT_SYMBOL(copy_strings_kernel);
  404. #ifdef CONFIG_MMU
  405. /*
  406. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  407. * the binfmt code determines where the new stack should reside, we shift it to
  408. * its final location. The process proceeds as follows:
  409. *
  410. * 1) Use shift to calculate the new vma endpoints.
  411. * 2) Extend vma to cover both the old and new ranges. This ensures the
  412. * arguments passed to subsequent functions are consistent.
  413. * 3) Move vma's page tables to the new range.
  414. * 4) Free up any cleared pgd range.
  415. * 5) Shrink the vma to cover only the new range.
  416. */
  417. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  418. {
  419. struct mm_struct *mm = vma->vm_mm;
  420. unsigned long old_start = vma->vm_start;
  421. unsigned long old_end = vma->vm_end;
  422. unsigned long length = old_end - old_start;
  423. unsigned long new_start = old_start - shift;
  424. unsigned long new_end = old_end - shift;
  425. struct mmu_gather *tlb;
  426. BUG_ON(new_start > new_end);
  427. /*
  428. * ensure there are no vmas between where we want to go
  429. * and where we are
  430. */
  431. if (vma != find_vma(mm, new_start))
  432. return -EFAULT;
  433. /*
  434. * cover the whole range: [new_start, old_end)
  435. */
  436. vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL);
  437. /*
  438. * move the page tables downwards, on failure we rely on
  439. * process cleanup to remove whatever mess we made.
  440. */
  441. if (length != move_page_tables(vma, old_start,
  442. vma, new_start, length))
  443. return -ENOMEM;
  444. lru_add_drain();
  445. tlb = tlb_gather_mmu(mm, 0);
  446. if (new_end > old_start) {
  447. /*
  448. * when the old and new regions overlap clear from new_end.
  449. */
  450. free_pgd_range(&tlb, new_end, old_end, new_end,
  451. vma->vm_next ? vma->vm_next->vm_start : 0);
  452. } else {
  453. /*
  454. * otherwise, clean from old_start; this is done to not touch
  455. * the address space in [new_end, old_start) some architectures
  456. * have constraints on va-space that make this illegal (IA64) -
  457. * for the others its just a little faster.
  458. */
  459. free_pgd_range(&tlb, old_start, old_end, new_end,
  460. vma->vm_next ? vma->vm_next->vm_start : 0);
  461. }
  462. tlb_finish_mmu(tlb, new_end, old_end);
  463. /*
  464. * shrink the vma to just the new range.
  465. */
  466. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  467. return 0;
  468. }
  469. #define EXTRA_STACK_VM_PAGES 20 /* random */
  470. /*
  471. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  472. * the stack is optionally relocated, and some extra space is added.
  473. */
  474. int setup_arg_pages(struct linux_binprm *bprm,
  475. unsigned long stack_top,
  476. int executable_stack)
  477. {
  478. unsigned long ret;
  479. unsigned long stack_shift;
  480. struct mm_struct *mm = current->mm;
  481. struct vm_area_struct *vma = bprm->vma;
  482. struct vm_area_struct *prev = NULL;
  483. unsigned long vm_flags;
  484. unsigned long stack_base;
  485. #ifdef CONFIG_STACK_GROWSUP
  486. /* Limit stack size to 1GB */
  487. stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
  488. if (stack_base > (1 << 30))
  489. stack_base = 1 << 30;
  490. /* Make sure we didn't let the argument array grow too large. */
  491. if (vma->vm_end - vma->vm_start > stack_base)
  492. return -ENOMEM;
  493. stack_base = PAGE_ALIGN(stack_top - stack_base);
  494. stack_shift = vma->vm_start - stack_base;
  495. mm->arg_start = bprm->p - stack_shift;
  496. bprm->p = vma->vm_end - stack_shift;
  497. #else
  498. stack_top = arch_align_stack(stack_top);
  499. stack_top = PAGE_ALIGN(stack_top);
  500. stack_shift = vma->vm_end - stack_top;
  501. bprm->p -= stack_shift;
  502. mm->arg_start = bprm->p;
  503. #endif
  504. if (bprm->loader)
  505. bprm->loader -= stack_shift;
  506. bprm->exec -= stack_shift;
  507. down_write(&mm->mmap_sem);
  508. vm_flags = vma->vm_flags;
  509. /*
  510. * Adjust stack execute permissions; explicitly enable for
  511. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  512. * (arch default) otherwise.
  513. */
  514. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  515. vm_flags |= VM_EXEC;
  516. else if (executable_stack == EXSTACK_DISABLE_X)
  517. vm_flags &= ~VM_EXEC;
  518. vm_flags |= mm->def_flags;
  519. ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
  520. vm_flags);
  521. if (ret)
  522. goto out_unlock;
  523. BUG_ON(prev != vma);
  524. /* Move stack pages down in memory. */
  525. if (stack_shift) {
  526. ret = shift_arg_pages(vma, stack_shift);
  527. if (ret) {
  528. up_write(&mm->mmap_sem);
  529. return ret;
  530. }
  531. }
  532. #ifdef CONFIG_STACK_GROWSUP
  533. stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  534. #else
  535. stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  536. #endif
  537. ret = expand_stack(vma, stack_base);
  538. if (ret)
  539. ret = -EFAULT;
  540. out_unlock:
  541. up_write(&mm->mmap_sem);
  542. return 0;
  543. }
  544. EXPORT_SYMBOL(setup_arg_pages);
  545. #endif /* CONFIG_MMU */
  546. struct file *open_exec(const char *name)
  547. {
  548. struct nameidata nd;
  549. int err;
  550. struct file *file;
  551. err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
  552. file = ERR_PTR(err);
  553. if (!err) {
  554. struct inode *inode = nd.dentry->d_inode;
  555. file = ERR_PTR(-EACCES);
  556. if (S_ISREG(inode->i_mode)) {
  557. int err = vfs_permission(&nd, MAY_EXEC);
  558. file = ERR_PTR(err);
  559. if (!err) {
  560. file = nameidata_to_filp(&nd, O_RDONLY);
  561. if (!IS_ERR(file)) {
  562. err = deny_write_access(file);
  563. if (err) {
  564. fput(file);
  565. file = ERR_PTR(err);
  566. }
  567. }
  568. out:
  569. return file;
  570. }
  571. }
  572. release_open_intent(&nd);
  573. path_release(&nd);
  574. }
  575. goto out;
  576. }
  577. EXPORT_SYMBOL(open_exec);
  578. int kernel_read(struct file *file, unsigned long offset,
  579. char *addr, unsigned long count)
  580. {
  581. mm_segment_t old_fs;
  582. loff_t pos = offset;
  583. int result;
  584. old_fs = get_fs();
  585. set_fs(get_ds());
  586. /* The cast to a user pointer is valid due to the set_fs() */
  587. result = vfs_read(file, (void __user *)addr, count, &pos);
  588. set_fs(old_fs);
  589. return result;
  590. }
  591. EXPORT_SYMBOL(kernel_read);
  592. static int exec_mmap(struct mm_struct *mm)
  593. {
  594. struct task_struct *tsk;
  595. struct mm_struct * old_mm, *active_mm;
  596. /* Notify parent that we're no longer interested in the old VM */
  597. tsk = current;
  598. old_mm = current->mm;
  599. mm_release(tsk, old_mm);
  600. if (old_mm) {
  601. /*
  602. * Make sure that if there is a core dump in progress
  603. * for the old mm, we get out and die instead of going
  604. * through with the exec. We must hold mmap_sem around
  605. * checking core_waiters and changing tsk->mm. The
  606. * core-inducing thread will increment core_waiters for
  607. * each thread whose ->mm == old_mm.
  608. */
  609. down_read(&old_mm->mmap_sem);
  610. if (unlikely(old_mm->core_waiters)) {
  611. up_read(&old_mm->mmap_sem);
  612. return -EINTR;
  613. }
  614. }
  615. task_lock(tsk);
  616. active_mm = tsk->active_mm;
  617. tsk->mm = mm;
  618. tsk->active_mm = mm;
  619. activate_mm(active_mm, mm);
  620. task_unlock(tsk);
  621. arch_pick_mmap_layout(mm);
  622. if (old_mm) {
  623. up_read(&old_mm->mmap_sem);
  624. BUG_ON(active_mm != old_mm);
  625. mmput(old_mm);
  626. return 0;
  627. }
  628. mmdrop(active_mm);
  629. return 0;
  630. }
  631. /*
  632. * This function makes sure the current process has its own signal table,
  633. * so that flush_signal_handlers can later reset the handlers without
  634. * disturbing other processes. (Other processes might share the signal
  635. * table via the CLONE_SIGHAND option to clone().)
  636. */
  637. static int de_thread(struct task_struct *tsk)
  638. {
  639. struct signal_struct *sig = tsk->signal;
  640. struct sighand_struct *oldsighand = tsk->sighand;
  641. spinlock_t *lock = &oldsighand->siglock;
  642. struct task_struct *leader = NULL;
  643. int count;
  644. if (thread_group_empty(tsk))
  645. goto no_thread_group;
  646. /*
  647. * Kill all other threads in the thread group.
  648. * We must hold tasklist_lock to call zap_other_threads.
  649. */
  650. read_lock(&tasklist_lock);
  651. spin_lock_irq(lock);
  652. if (sig->flags & SIGNAL_GROUP_EXIT) {
  653. /*
  654. * Another group action in progress, just
  655. * return so that the signal is processed.
  656. */
  657. spin_unlock_irq(lock);
  658. read_unlock(&tasklist_lock);
  659. return -EAGAIN;
  660. }
  661. /*
  662. * child_reaper ignores SIGKILL, change it now.
  663. * Reparenting needs write_lock on tasklist_lock,
  664. * so it is safe to do it under read_lock.
  665. */
  666. if (unlikely(tsk->group_leader == child_reaper(tsk)))
  667. tsk->nsproxy->pid_ns->child_reaper = tsk;
  668. zap_other_threads(tsk);
  669. read_unlock(&tasklist_lock);
  670. /*
  671. * Account for the thread group leader hanging around:
  672. */
  673. count = 1;
  674. if (!thread_group_leader(tsk)) {
  675. count = 2;
  676. /*
  677. * The SIGALRM timer survives the exec, but needs to point
  678. * at us as the new group leader now. We have a race with
  679. * a timer firing now getting the old leader, so we need to
  680. * synchronize with any firing (by calling del_timer_sync)
  681. * before we can safely let the old group leader die.
  682. */
  683. sig->tsk = tsk;
  684. spin_unlock_irq(lock);
  685. if (hrtimer_cancel(&sig->real_timer))
  686. hrtimer_restart(&sig->real_timer);
  687. spin_lock_irq(lock);
  688. }
  689. sig->notify_count = count;
  690. sig->group_exit_task = tsk;
  691. while (atomic_read(&sig->count) > count) {
  692. __set_current_state(TASK_UNINTERRUPTIBLE);
  693. spin_unlock_irq(lock);
  694. schedule();
  695. spin_lock_irq(lock);
  696. }
  697. spin_unlock_irq(lock);
  698. /*
  699. * At this point all other threads have exited, all we have to
  700. * do is to wait for the thread group leader to become inactive,
  701. * and to assume its PID:
  702. */
  703. if (!thread_group_leader(tsk)) {
  704. leader = tsk->group_leader;
  705. sig->notify_count = -1;
  706. for (;;) {
  707. write_lock_irq(&tasklist_lock);
  708. if (likely(leader->exit_state))
  709. break;
  710. __set_current_state(TASK_UNINTERRUPTIBLE);
  711. write_unlock_irq(&tasklist_lock);
  712. schedule();
  713. }
  714. /*
  715. * The only record we have of the real-time age of a
  716. * process, regardless of execs it's done, is start_time.
  717. * All the past CPU time is accumulated in signal_struct
  718. * from sister threads now dead. But in this non-leader
  719. * exec, nothing survives from the original leader thread,
  720. * whose birth marks the true age of this process now.
  721. * When we take on its identity by switching to its PID, we
  722. * also take its birthdate (always earlier than our own).
  723. */
  724. tsk->start_time = leader->start_time;
  725. BUG_ON(leader->tgid != tsk->tgid);
  726. BUG_ON(tsk->pid == tsk->tgid);
  727. /*
  728. * An exec() starts a new thread group with the
  729. * TGID of the previous thread group. Rehash the
  730. * two threads with a switched PID, and release
  731. * the former thread group leader:
  732. */
  733. /* Become a process group leader with the old leader's pid.
  734. * The old leader becomes a thread of the this thread group.
  735. * Note: The old leader also uses this pid until release_task
  736. * is called. Odd but simple and correct.
  737. */
  738. detach_pid(tsk, PIDTYPE_PID);
  739. tsk->pid = leader->pid;
  740. attach_pid(tsk, PIDTYPE_PID, find_pid(tsk->pid));
  741. transfer_pid(leader, tsk, PIDTYPE_PGID);
  742. transfer_pid(leader, tsk, PIDTYPE_SID);
  743. list_replace_rcu(&leader->tasks, &tsk->tasks);
  744. tsk->group_leader = tsk;
  745. leader->group_leader = tsk;
  746. tsk->exit_signal = SIGCHLD;
  747. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  748. leader->exit_state = EXIT_DEAD;
  749. write_unlock_irq(&tasklist_lock);
  750. }
  751. sig->group_exit_task = NULL;
  752. sig->notify_count = 0;
  753. /*
  754. * There may be one thread left which is just exiting,
  755. * but it's safe to stop telling the group to kill themselves.
  756. */
  757. sig->flags = 0;
  758. no_thread_group:
  759. exit_itimers(sig);
  760. if (leader)
  761. release_task(leader);
  762. if (atomic_read(&oldsighand->count) != 1) {
  763. struct sighand_struct *newsighand;
  764. /*
  765. * This ->sighand is shared with the CLONE_SIGHAND
  766. * but not CLONE_THREAD task, switch to the new one.
  767. */
  768. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  769. if (!newsighand)
  770. return -ENOMEM;
  771. atomic_set(&newsighand->count, 1);
  772. memcpy(newsighand->action, oldsighand->action,
  773. sizeof(newsighand->action));
  774. write_lock_irq(&tasklist_lock);
  775. spin_lock(&oldsighand->siglock);
  776. rcu_assign_pointer(tsk->sighand, newsighand);
  777. spin_unlock(&oldsighand->siglock);
  778. write_unlock_irq(&tasklist_lock);
  779. __cleanup_sighand(oldsighand);
  780. }
  781. BUG_ON(!thread_group_leader(tsk));
  782. return 0;
  783. }
  784. /*
  785. * These functions flushes out all traces of the currently running executable
  786. * so that a new one can be started
  787. */
  788. static void flush_old_files(struct files_struct * files)
  789. {
  790. long j = -1;
  791. struct fdtable *fdt;
  792. spin_lock(&files->file_lock);
  793. for (;;) {
  794. unsigned long set, i;
  795. j++;
  796. i = j * __NFDBITS;
  797. fdt = files_fdtable(files);
  798. if (i >= fdt->max_fds)
  799. break;
  800. set = fdt->close_on_exec->fds_bits[j];
  801. if (!set)
  802. continue;
  803. fdt->close_on_exec->fds_bits[j] = 0;
  804. spin_unlock(&files->file_lock);
  805. for ( ; set ; i++,set >>= 1) {
  806. if (set & 1) {
  807. sys_close(i);
  808. }
  809. }
  810. spin_lock(&files->file_lock);
  811. }
  812. spin_unlock(&files->file_lock);
  813. }
  814. void get_task_comm(char *buf, struct task_struct *tsk)
  815. {
  816. /* buf must be at least sizeof(tsk->comm) in size */
  817. task_lock(tsk);
  818. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  819. task_unlock(tsk);
  820. }
  821. void set_task_comm(struct task_struct *tsk, char *buf)
  822. {
  823. task_lock(tsk);
  824. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  825. task_unlock(tsk);
  826. }
  827. int flush_old_exec(struct linux_binprm * bprm)
  828. {
  829. char * name;
  830. int i, ch, retval;
  831. struct files_struct *files;
  832. char tcomm[sizeof(current->comm)];
  833. /*
  834. * Make sure we have a private signal table and that
  835. * we are unassociated from the previous thread group.
  836. */
  837. retval = de_thread(current);
  838. if (retval)
  839. goto out;
  840. /*
  841. * Make sure we have private file handles. Ask the
  842. * fork helper to do the work for us and the exit
  843. * helper to do the cleanup of the old one.
  844. */
  845. files = current->files; /* refcounted so safe to hold */
  846. retval = unshare_files();
  847. if (retval)
  848. goto out;
  849. /*
  850. * Release all of the old mmap stuff
  851. */
  852. retval = exec_mmap(bprm->mm);
  853. if (retval)
  854. goto mmap_failed;
  855. bprm->mm = NULL; /* We're using it now */
  856. /* This is the point of no return */
  857. put_files_struct(files);
  858. current->sas_ss_sp = current->sas_ss_size = 0;
  859. if (current->euid == current->uid && current->egid == current->gid)
  860. set_dumpable(current->mm, 1);
  861. else
  862. set_dumpable(current->mm, suid_dumpable);
  863. name = bprm->filename;
  864. /* Copies the binary name from after last slash */
  865. for (i=0; (ch = *(name++)) != '\0';) {
  866. if (ch == '/')
  867. i = 0; /* overwrite what we wrote */
  868. else
  869. if (i < (sizeof(tcomm) - 1))
  870. tcomm[i++] = ch;
  871. }
  872. tcomm[i] = '\0';
  873. set_task_comm(current, tcomm);
  874. current->flags &= ~PF_RANDOMIZE;
  875. flush_thread();
  876. /* Set the new mm task size. We have to do that late because it may
  877. * depend on TIF_32BIT which is only updated in flush_thread() on
  878. * some architectures like powerpc
  879. */
  880. current->mm->task_size = TASK_SIZE;
  881. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) {
  882. suid_keys(current);
  883. set_dumpable(current->mm, suid_dumpable);
  884. current->pdeath_signal = 0;
  885. } else if (file_permission(bprm->file, MAY_READ) ||
  886. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  887. suid_keys(current);
  888. set_dumpable(current->mm, suid_dumpable);
  889. }
  890. /* An exec changes our domain. We are no longer part of the thread
  891. group */
  892. current->self_exec_id++;
  893. flush_signal_handlers(current, 0);
  894. flush_old_files(current->files);
  895. return 0;
  896. mmap_failed:
  897. reset_files_struct(current, files);
  898. out:
  899. return retval;
  900. }
  901. EXPORT_SYMBOL(flush_old_exec);
  902. /*
  903. * Fill the binprm structure from the inode.
  904. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  905. */
  906. int prepare_binprm(struct linux_binprm *bprm)
  907. {
  908. int mode;
  909. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  910. int retval;
  911. mode = inode->i_mode;
  912. if (bprm->file->f_op == NULL)
  913. return -EACCES;
  914. bprm->e_uid = current->euid;
  915. bprm->e_gid = current->egid;
  916. if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  917. /* Set-uid? */
  918. if (mode & S_ISUID) {
  919. current->personality &= ~PER_CLEAR_ON_SETID;
  920. bprm->e_uid = inode->i_uid;
  921. }
  922. /* Set-gid? */
  923. /*
  924. * If setgid is set but no group execute bit then this
  925. * is a candidate for mandatory locking, not a setgid
  926. * executable.
  927. */
  928. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  929. current->personality &= ~PER_CLEAR_ON_SETID;
  930. bprm->e_gid = inode->i_gid;
  931. }
  932. }
  933. /* fill in binprm security blob */
  934. retval = security_bprm_set(bprm);
  935. if (retval)
  936. return retval;
  937. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  938. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  939. }
  940. EXPORT_SYMBOL(prepare_binprm);
  941. static int unsafe_exec(struct task_struct *p)
  942. {
  943. int unsafe = 0;
  944. if (p->ptrace & PT_PTRACED) {
  945. if (p->ptrace & PT_PTRACE_CAP)
  946. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  947. else
  948. unsafe |= LSM_UNSAFE_PTRACE;
  949. }
  950. if (atomic_read(&p->fs->count) > 1 ||
  951. atomic_read(&p->files->count) > 1 ||
  952. atomic_read(&p->sighand->count) > 1)
  953. unsafe |= LSM_UNSAFE_SHARE;
  954. return unsafe;
  955. }
  956. void compute_creds(struct linux_binprm *bprm)
  957. {
  958. int unsafe;
  959. if (bprm->e_uid != current->uid) {
  960. suid_keys(current);
  961. current->pdeath_signal = 0;
  962. }
  963. exec_keys(current);
  964. task_lock(current);
  965. unsafe = unsafe_exec(current);
  966. security_bprm_apply_creds(bprm, unsafe);
  967. task_unlock(current);
  968. security_bprm_post_apply_creds(bprm);
  969. }
  970. EXPORT_SYMBOL(compute_creds);
  971. /*
  972. * Arguments are '\0' separated strings found at the location bprm->p
  973. * points to; chop off the first by relocating brpm->p to right after
  974. * the first '\0' encountered.
  975. */
  976. int remove_arg_zero(struct linux_binprm *bprm)
  977. {
  978. int ret = 0;
  979. unsigned long offset;
  980. char *kaddr;
  981. struct page *page;
  982. if (!bprm->argc)
  983. return 0;
  984. do {
  985. offset = bprm->p & ~PAGE_MASK;
  986. page = get_arg_page(bprm, bprm->p, 0);
  987. if (!page) {
  988. ret = -EFAULT;
  989. goto out;
  990. }
  991. kaddr = kmap_atomic(page, KM_USER0);
  992. for (; offset < PAGE_SIZE && kaddr[offset];
  993. offset++, bprm->p++)
  994. ;
  995. kunmap_atomic(kaddr, KM_USER0);
  996. put_arg_page(page);
  997. if (offset == PAGE_SIZE)
  998. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  999. } while (offset == PAGE_SIZE);
  1000. bprm->p++;
  1001. bprm->argc--;
  1002. ret = 0;
  1003. out:
  1004. return ret;
  1005. }
  1006. EXPORT_SYMBOL(remove_arg_zero);
  1007. /*
  1008. * cycle the list of binary formats handler, until one recognizes the image
  1009. */
  1010. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  1011. {
  1012. int try,retval;
  1013. struct linux_binfmt *fmt;
  1014. #ifdef __alpha__
  1015. /* handle /sbin/loader.. */
  1016. {
  1017. struct exec * eh = (struct exec *) bprm->buf;
  1018. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  1019. (eh->fh.f_flags & 0x3000) == 0x3000)
  1020. {
  1021. struct file * file;
  1022. unsigned long loader;
  1023. allow_write_access(bprm->file);
  1024. fput(bprm->file);
  1025. bprm->file = NULL;
  1026. loader = bprm->vma->vm_end - sizeof(void *);
  1027. file = open_exec("/sbin/loader");
  1028. retval = PTR_ERR(file);
  1029. if (IS_ERR(file))
  1030. return retval;
  1031. /* Remember if the application is TASO. */
  1032. bprm->sh_bang = eh->ah.entry < 0x100000000UL;
  1033. bprm->file = file;
  1034. bprm->loader = loader;
  1035. retval = prepare_binprm(bprm);
  1036. if (retval<0)
  1037. return retval;
  1038. /* should call search_binary_handler recursively here,
  1039. but it does not matter */
  1040. }
  1041. }
  1042. #endif
  1043. retval = security_bprm_check(bprm);
  1044. if (retval)
  1045. return retval;
  1046. /* kernel module loader fixup */
  1047. /* so we don't try to load run modprobe in kernel space. */
  1048. set_fs(USER_DS);
  1049. retval = audit_bprm(bprm);
  1050. if (retval)
  1051. return retval;
  1052. retval = -ENOENT;
  1053. for (try=0; try<2; try++) {
  1054. read_lock(&binfmt_lock);
  1055. list_for_each_entry(fmt, &formats, lh) {
  1056. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1057. if (!fn)
  1058. continue;
  1059. if (!try_module_get(fmt->module))
  1060. continue;
  1061. read_unlock(&binfmt_lock);
  1062. retval = fn(bprm, regs);
  1063. if (retval >= 0) {
  1064. put_binfmt(fmt);
  1065. allow_write_access(bprm->file);
  1066. if (bprm->file)
  1067. fput(bprm->file);
  1068. bprm->file = NULL;
  1069. current->did_exec = 1;
  1070. proc_exec_connector(current);
  1071. return retval;
  1072. }
  1073. read_lock(&binfmt_lock);
  1074. put_binfmt(fmt);
  1075. if (retval != -ENOEXEC || bprm->mm == NULL)
  1076. break;
  1077. if (!bprm->file) {
  1078. read_unlock(&binfmt_lock);
  1079. return retval;
  1080. }
  1081. }
  1082. read_unlock(&binfmt_lock);
  1083. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1084. break;
  1085. #ifdef CONFIG_KMOD
  1086. }else{
  1087. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1088. if (printable(bprm->buf[0]) &&
  1089. printable(bprm->buf[1]) &&
  1090. printable(bprm->buf[2]) &&
  1091. printable(bprm->buf[3]))
  1092. break; /* -ENOEXEC */
  1093. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1094. #endif
  1095. }
  1096. }
  1097. return retval;
  1098. }
  1099. EXPORT_SYMBOL(search_binary_handler);
  1100. /*
  1101. * sys_execve() executes a new program.
  1102. */
  1103. int do_execve(char * filename,
  1104. char __user *__user *argv,
  1105. char __user *__user *envp,
  1106. struct pt_regs * regs)
  1107. {
  1108. struct linux_binprm *bprm;
  1109. struct file *file;
  1110. unsigned long env_p;
  1111. int retval;
  1112. retval = -ENOMEM;
  1113. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1114. if (!bprm)
  1115. goto out_ret;
  1116. file = open_exec(filename);
  1117. retval = PTR_ERR(file);
  1118. if (IS_ERR(file))
  1119. goto out_kfree;
  1120. sched_exec();
  1121. bprm->file = file;
  1122. bprm->filename = filename;
  1123. bprm->interp = filename;
  1124. retval = bprm_mm_init(bprm);
  1125. if (retval)
  1126. goto out_file;
  1127. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1128. if ((retval = bprm->argc) < 0)
  1129. goto out_mm;
  1130. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1131. if ((retval = bprm->envc) < 0)
  1132. goto out_mm;
  1133. retval = security_bprm_alloc(bprm);
  1134. if (retval)
  1135. goto out;
  1136. retval = prepare_binprm(bprm);
  1137. if (retval < 0)
  1138. goto out;
  1139. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1140. if (retval < 0)
  1141. goto out;
  1142. bprm->exec = bprm->p;
  1143. retval = copy_strings(bprm->envc, envp, bprm);
  1144. if (retval < 0)
  1145. goto out;
  1146. env_p = bprm->p;
  1147. retval = copy_strings(bprm->argc, argv, bprm);
  1148. if (retval < 0)
  1149. goto out;
  1150. bprm->argv_len = env_p - bprm->p;
  1151. retval = search_binary_handler(bprm,regs);
  1152. if (retval >= 0) {
  1153. /* execve success */
  1154. free_arg_pages(bprm);
  1155. security_bprm_free(bprm);
  1156. acct_update_integrals(current);
  1157. kfree(bprm);
  1158. return retval;
  1159. }
  1160. out:
  1161. free_arg_pages(bprm);
  1162. if (bprm->security)
  1163. security_bprm_free(bprm);
  1164. out_mm:
  1165. if (bprm->mm)
  1166. mmput (bprm->mm);
  1167. out_file:
  1168. if (bprm->file) {
  1169. allow_write_access(bprm->file);
  1170. fput(bprm->file);
  1171. }
  1172. out_kfree:
  1173. kfree(bprm);
  1174. out_ret:
  1175. return retval;
  1176. }
  1177. int set_binfmt(struct linux_binfmt *new)
  1178. {
  1179. struct linux_binfmt *old = current->binfmt;
  1180. if (new) {
  1181. if (!try_module_get(new->module))
  1182. return -1;
  1183. }
  1184. current->binfmt = new;
  1185. if (old)
  1186. module_put(old->module);
  1187. return 0;
  1188. }
  1189. EXPORT_SYMBOL(set_binfmt);
  1190. /* format_corename will inspect the pattern parameter, and output a
  1191. * name into corename, which must have space for at least
  1192. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1193. */
  1194. static int format_corename(char *corename, const char *pattern, long signr)
  1195. {
  1196. const char *pat_ptr = pattern;
  1197. char *out_ptr = corename;
  1198. char *const out_end = corename + CORENAME_MAX_SIZE;
  1199. int rc;
  1200. int pid_in_pattern = 0;
  1201. int ispipe = 0;
  1202. if (*pattern == '|')
  1203. ispipe = 1;
  1204. /* Repeat as long as we have more pattern to process and more output
  1205. space */
  1206. while (*pat_ptr) {
  1207. if (*pat_ptr != '%') {
  1208. if (out_ptr == out_end)
  1209. goto out;
  1210. *out_ptr++ = *pat_ptr++;
  1211. } else {
  1212. switch (*++pat_ptr) {
  1213. case 0:
  1214. goto out;
  1215. /* Double percent, output one percent */
  1216. case '%':
  1217. if (out_ptr == out_end)
  1218. goto out;
  1219. *out_ptr++ = '%';
  1220. break;
  1221. /* pid */
  1222. case 'p':
  1223. pid_in_pattern = 1;
  1224. rc = snprintf(out_ptr, out_end - out_ptr,
  1225. "%d", current->tgid);
  1226. if (rc > out_end - out_ptr)
  1227. goto out;
  1228. out_ptr += rc;
  1229. break;
  1230. /* uid */
  1231. case 'u':
  1232. rc = snprintf(out_ptr, out_end - out_ptr,
  1233. "%d", current->uid);
  1234. if (rc > out_end - out_ptr)
  1235. goto out;
  1236. out_ptr += rc;
  1237. break;
  1238. /* gid */
  1239. case 'g':
  1240. rc = snprintf(out_ptr, out_end - out_ptr,
  1241. "%d", current->gid);
  1242. if (rc > out_end - out_ptr)
  1243. goto out;
  1244. out_ptr += rc;
  1245. break;
  1246. /* signal that caused the coredump */
  1247. case 's':
  1248. rc = snprintf(out_ptr, out_end - out_ptr,
  1249. "%ld", signr);
  1250. if (rc > out_end - out_ptr)
  1251. goto out;
  1252. out_ptr += rc;
  1253. break;
  1254. /* UNIX time of coredump */
  1255. case 't': {
  1256. struct timeval tv;
  1257. do_gettimeofday(&tv);
  1258. rc = snprintf(out_ptr, out_end - out_ptr,
  1259. "%lu", tv.tv_sec);
  1260. if (rc > out_end - out_ptr)
  1261. goto out;
  1262. out_ptr += rc;
  1263. break;
  1264. }
  1265. /* hostname */
  1266. case 'h':
  1267. down_read(&uts_sem);
  1268. rc = snprintf(out_ptr, out_end - out_ptr,
  1269. "%s", utsname()->nodename);
  1270. up_read(&uts_sem);
  1271. if (rc > out_end - out_ptr)
  1272. goto out;
  1273. out_ptr += rc;
  1274. break;
  1275. /* executable */
  1276. case 'e':
  1277. rc = snprintf(out_ptr, out_end - out_ptr,
  1278. "%s", current->comm);
  1279. if (rc > out_end - out_ptr)
  1280. goto out;
  1281. out_ptr += rc;
  1282. break;
  1283. /* core limit size */
  1284. case 'c':
  1285. rc = snprintf(out_ptr, out_end - out_ptr,
  1286. "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
  1287. if (rc > out_end - out_ptr)
  1288. goto out;
  1289. out_ptr += rc;
  1290. break;
  1291. default:
  1292. break;
  1293. }
  1294. ++pat_ptr;
  1295. }
  1296. }
  1297. /* Backward compatibility with core_uses_pid:
  1298. *
  1299. * If core_pattern does not include a %p (as is the default)
  1300. * and core_uses_pid is set, then .%pid will be appended to
  1301. * the filename. Do not do this for piped commands. */
  1302. if (!ispipe && !pid_in_pattern
  1303. && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
  1304. rc = snprintf(out_ptr, out_end - out_ptr,
  1305. ".%d", current->tgid);
  1306. if (rc > out_end - out_ptr)
  1307. goto out;
  1308. out_ptr += rc;
  1309. }
  1310. out:
  1311. *out_ptr = 0;
  1312. return ispipe;
  1313. }
  1314. static void zap_process(struct task_struct *start)
  1315. {
  1316. struct task_struct *t;
  1317. start->signal->flags = SIGNAL_GROUP_EXIT;
  1318. start->signal->group_stop_count = 0;
  1319. t = start;
  1320. do {
  1321. if (t != current && t->mm) {
  1322. t->mm->core_waiters++;
  1323. sigaddset(&t->pending.signal, SIGKILL);
  1324. signal_wake_up(t, 1);
  1325. }
  1326. } while ((t = next_thread(t)) != start);
  1327. }
  1328. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1329. int exit_code)
  1330. {
  1331. struct task_struct *g, *p;
  1332. unsigned long flags;
  1333. int err = -EAGAIN;
  1334. spin_lock_irq(&tsk->sighand->siglock);
  1335. if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT)) {
  1336. tsk->signal->group_exit_code = exit_code;
  1337. zap_process(tsk);
  1338. err = 0;
  1339. }
  1340. spin_unlock_irq(&tsk->sighand->siglock);
  1341. if (err)
  1342. return err;
  1343. if (atomic_read(&mm->mm_users) == mm->core_waiters + 1)
  1344. goto done;
  1345. rcu_read_lock();
  1346. for_each_process(g) {
  1347. if (g == tsk->group_leader)
  1348. continue;
  1349. p = g;
  1350. do {
  1351. if (p->mm) {
  1352. if (p->mm == mm) {
  1353. /*
  1354. * p->sighand can't disappear, but
  1355. * may be changed by de_thread()
  1356. */
  1357. lock_task_sighand(p, &flags);
  1358. zap_process(p);
  1359. unlock_task_sighand(p, &flags);
  1360. }
  1361. break;
  1362. }
  1363. } while ((p = next_thread(p)) != g);
  1364. }
  1365. rcu_read_unlock();
  1366. done:
  1367. return mm->core_waiters;
  1368. }
  1369. static int coredump_wait(int exit_code)
  1370. {
  1371. struct task_struct *tsk = current;
  1372. struct mm_struct *mm = tsk->mm;
  1373. struct completion startup_done;
  1374. struct completion *vfork_done;
  1375. int core_waiters;
  1376. init_completion(&mm->core_done);
  1377. init_completion(&startup_done);
  1378. mm->core_startup_done = &startup_done;
  1379. core_waiters = zap_threads(tsk, mm, exit_code);
  1380. up_write(&mm->mmap_sem);
  1381. if (unlikely(core_waiters < 0))
  1382. goto fail;
  1383. /*
  1384. * Make sure nobody is waiting for us to release the VM,
  1385. * otherwise we can deadlock when we wait on each other
  1386. */
  1387. vfork_done = tsk->vfork_done;
  1388. if (vfork_done) {
  1389. tsk->vfork_done = NULL;
  1390. complete(vfork_done);
  1391. }
  1392. if (core_waiters)
  1393. wait_for_completion(&startup_done);
  1394. fail:
  1395. BUG_ON(mm->core_waiters);
  1396. return core_waiters;
  1397. }
  1398. /*
  1399. * set_dumpable converts traditional three-value dumpable to two flags and
  1400. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1401. * these bits are not changed atomically. So get_dumpable can observe the
  1402. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1403. * return either old dumpable or new one by paying attention to the order of
  1404. * modifying the bits.
  1405. *
  1406. * dumpable | mm->flags (binary)
  1407. * old new | initial interim final
  1408. * ---------+-----------------------
  1409. * 0 1 | 00 01 01
  1410. * 0 2 | 00 10(*) 11
  1411. * 1 0 | 01 00 00
  1412. * 1 2 | 01 11 11
  1413. * 2 0 | 11 10(*) 00
  1414. * 2 1 | 11 11 01
  1415. *
  1416. * (*) get_dumpable regards interim value of 10 as 11.
  1417. */
  1418. void set_dumpable(struct mm_struct *mm, int value)
  1419. {
  1420. switch (value) {
  1421. case 0:
  1422. clear_bit(MMF_DUMPABLE, &mm->flags);
  1423. smp_wmb();
  1424. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1425. break;
  1426. case 1:
  1427. set_bit(MMF_DUMPABLE, &mm->flags);
  1428. smp_wmb();
  1429. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1430. break;
  1431. case 2:
  1432. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1433. smp_wmb();
  1434. set_bit(MMF_DUMPABLE, &mm->flags);
  1435. break;
  1436. }
  1437. }
  1438. EXPORT_SYMBOL_GPL(set_dumpable);
  1439. int get_dumpable(struct mm_struct *mm)
  1440. {
  1441. int ret;
  1442. ret = mm->flags & 0x3;
  1443. return (ret >= 2) ? 2 : ret;
  1444. }
  1445. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1446. {
  1447. char corename[CORENAME_MAX_SIZE + 1];
  1448. struct mm_struct *mm = current->mm;
  1449. struct linux_binfmt * binfmt;
  1450. struct inode * inode;
  1451. struct file * file;
  1452. int retval = 0;
  1453. int fsuid = current->fsuid;
  1454. int flag = 0;
  1455. int ispipe = 0;
  1456. unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1457. char **helper_argv = NULL;
  1458. int helper_argc = 0;
  1459. char *delimit;
  1460. audit_core_dumps(signr);
  1461. binfmt = current->binfmt;
  1462. if (!binfmt || !binfmt->core_dump)
  1463. goto fail;
  1464. down_write(&mm->mmap_sem);
  1465. if (!get_dumpable(mm)) {
  1466. up_write(&mm->mmap_sem);
  1467. goto fail;
  1468. }
  1469. /*
  1470. * We cannot trust fsuid as being the "true" uid of the
  1471. * process nor do we know its entire history. We only know it
  1472. * was tainted so we dump it as root in mode 2.
  1473. */
  1474. if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
  1475. flag = O_EXCL; /* Stop rewrite attacks */
  1476. current->fsuid = 0; /* Dump root private */
  1477. }
  1478. set_dumpable(mm, 0);
  1479. retval = coredump_wait(exit_code);
  1480. if (retval < 0)
  1481. goto fail;
  1482. /*
  1483. * Clear any false indication of pending signals that might
  1484. * be seen by the filesystem code called to write the core file.
  1485. */
  1486. clear_thread_flag(TIF_SIGPENDING);
  1487. /*
  1488. * lock_kernel() because format_corename() is controlled by sysctl, which
  1489. * uses lock_kernel()
  1490. */
  1491. lock_kernel();
  1492. ispipe = format_corename(corename, core_pattern, signr);
  1493. unlock_kernel();
  1494. /*
  1495. * Don't bother to check the RLIMIT_CORE value if core_pattern points
  1496. * to a pipe. Since we're not writing directly to the filesystem
  1497. * RLIMIT_CORE doesn't really apply, as no actual core file will be
  1498. * created unless the pipe reader choses to write out the core file
  1499. * at which point file size limits and permissions will be imposed
  1500. * as it does with any other process
  1501. */
  1502. if ((!ispipe) && (core_limit < binfmt->min_coredump))
  1503. goto fail_unlock;
  1504. if (ispipe) {
  1505. helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
  1506. /* Terminate the string before the first option */
  1507. delimit = strchr(corename, ' ');
  1508. if (delimit)
  1509. *delimit = '\0';
  1510. delimit = strrchr(helper_argv[0], '/');
  1511. if (delimit)
  1512. delimit++;
  1513. else
  1514. delimit = helper_argv[0];
  1515. if (!strcmp(delimit, current->comm)) {
  1516. printk(KERN_NOTICE "Recursive core dump detected, "
  1517. "aborting\n");
  1518. goto fail_unlock;
  1519. }
  1520. core_limit = RLIM_INFINITY;
  1521. /* SIGPIPE can happen, but it's just never processed */
  1522. if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
  1523. &file)) {
  1524. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1525. corename);
  1526. goto fail_unlock;
  1527. }
  1528. } else
  1529. file = filp_open(corename,
  1530. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1531. 0600);
  1532. if (IS_ERR(file))
  1533. goto fail_unlock;
  1534. inode = file->f_path.dentry->d_inode;
  1535. if (inode->i_nlink > 1)
  1536. goto close_fail; /* multiple links - don't dump */
  1537. if (!ispipe && d_unhashed(file->f_path.dentry))
  1538. goto close_fail;
  1539. /* AK: actually i see no reason to not allow this for named pipes etc.,
  1540. but keep the previous behaviour for now. */
  1541. if (!ispipe && !S_ISREG(inode->i_mode))
  1542. goto close_fail;
  1543. if (!file->f_op)
  1544. goto close_fail;
  1545. if (!file->f_op->write)
  1546. goto close_fail;
  1547. if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
  1548. goto close_fail;
  1549. retval = binfmt->core_dump(signr, regs, file, core_limit);
  1550. if (retval)
  1551. current->signal->group_exit_code |= 0x80;
  1552. close_fail:
  1553. filp_close(file, NULL);
  1554. fail_unlock:
  1555. if (helper_argv)
  1556. argv_free(helper_argv);
  1557. current->fsuid = fsuid;
  1558. complete_all(&mm->core_done);
  1559. fail:
  1560. return retval;
  1561. }