exec.c 41 KB

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