exec.c 41 KB

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