2017-11-24 22:00:32 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-12-25 20:38:36 +08:00
|
|
|
/*
|
|
|
|
* vdso setup for s390
|
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2008
|
|
|
|
* Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
|
|
|
|
*/
|
|
|
|
|
2017-02-10 04:20:23 +08:00
|
|
|
#include <linux/init.h>
|
2008-12-25 20:38:36 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/unistd.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/user.h>
|
|
|
|
#include <linux/elf.h>
|
|
|
|
#include <linux/security.h>
|
2018-10-31 06:09:49 +08:00
|
|
|
#include <linux/memblock.h>
|
2009-06-12 16:26:25 +08:00
|
|
|
#include <linux/compat.h>
|
2020-04-28 15:52:23 +08:00
|
|
|
#include <linux/binfmts.h>
|
|
|
|
#include <vdso/datapage.h>
|
2010-02-27 05:37:43 +08:00
|
|
|
#include <asm/asm-offsets.h>
|
2008-12-25 20:38:36 +08:00
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/mmu.h>
|
|
|
|
#include <asm/mmu_context.h>
|
|
|
|
#include <asm/sections.h>
|
|
|
|
#include <asm/vdso.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/facility.h>
|
2020-11-16 15:06:41 +08:00
|
|
|
#include <asm/timex.h>
|
2008-12-25 20:38:36 +08:00
|
|
|
|
|
|
|
extern char vdso64_start, vdso64_end;
|
|
|
|
static void *vdso64_kbase = &vdso64_start;
|
|
|
|
static unsigned int vdso64_pages;
|
|
|
|
static struct page **vdso64_pagelist;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Should the kernel map a VDSO page into processes and pass its
|
|
|
|
* address down to glibc upon exec()?
|
|
|
|
*/
|
|
|
|
unsigned int __read_mostly vdso_enabled = 1;
|
|
|
|
|
2018-07-22 21:41:09 +08:00
|
|
|
static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
|
2017-05-15 16:23:38 +08:00
|
|
|
struct vm_area_struct *vma, struct vm_fault *vmf)
|
|
|
|
{
|
|
|
|
struct page **vdso_pagelist;
|
|
|
|
unsigned long vdso_pages;
|
|
|
|
|
|
|
|
vdso_pagelist = vdso64_pagelist;
|
|
|
|
vdso_pages = vdso64_pages;
|
|
|
|
|
|
|
|
if (vmf->pgoff >= vdso_pages)
|
|
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
|
|
|
|
vmf->page = vdso_pagelist[vmf->pgoff];
|
|
|
|
get_page(vmf->page);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vdso_mremap(const struct vm_special_mapping *sm,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
current->mm->context.vdso_base = vma->vm_start;
|
2020-12-15 11:08:25 +08:00
|
|
|
|
2017-05-15 16:23:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct vm_special_mapping vdso_mapping = {
|
|
|
|
.name = "[vdso]",
|
|
|
|
.fault = vdso_fault,
|
|
|
|
.mremap = vdso_mremap,
|
|
|
|
};
|
|
|
|
|
2019-08-19 23:41:17 +08:00
|
|
|
static int __init vdso_setup(char *str)
|
2008-12-25 20:38:36 +08:00
|
|
|
{
|
2019-08-19 23:41:17 +08:00
|
|
|
bool enabled;
|
|
|
|
|
|
|
|
if (!kstrtobool(str, &enabled))
|
|
|
|
vdso_enabled = enabled;
|
|
|
|
return 1;
|
2008-12-25 20:38:36 +08:00
|
|
|
}
|
|
|
|
__setup("vdso=", vdso_setup);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The vdso data page
|
|
|
|
*/
|
|
|
|
static union {
|
2021-01-25 02:44:18 +08:00
|
|
|
struct vdso_data data[CS_BASES];
|
2008-12-25 20:38:36 +08:00
|
|
|
u8 page[PAGE_SIZE];
|
2009-09-21 06:14:15 +08:00
|
|
|
} vdso_data_store __page_aligned_data;
|
2021-01-25 02:44:18 +08:00
|
|
|
struct vdso_data *vdso_data = vdso_data_store.data;
|
2008-12-31 22:11:42 +08:00
|
|
|
|
2021-01-25 02:51:34 +08:00
|
|
|
int vdso_getcpu_init(void)
|
2020-11-16 15:06:41 +08:00
|
|
|
{
|
|
|
|
set_tod_programmable_field(smp_processor_id());
|
2021-01-25 02:51:34 +08:00
|
|
|
return 0;
|
2020-11-16 15:06:41 +08:00
|
|
|
}
|
2021-01-25 02:51:34 +08:00
|
|
|
early_initcall(vdso_getcpu_init); /* Must be called before SMP init */
|
2020-11-16 15:06:41 +08:00
|
|
|
|
2008-12-25 20:38:36 +08:00
|
|
|
/*
|
|
|
|
* This is called from binfmt_elf, we create the special vma for the
|
|
|
|
* vDSO and insert it into the mm struct tree
|
|
|
|
*/
|
|
|
|
int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
|
|
|
|
{
|
|
|
|
struct mm_struct *mm = current->mm;
|
2017-05-15 16:23:38 +08:00
|
|
|
struct vm_area_struct *vma;
|
2008-12-25 20:38:36 +08:00
|
|
|
unsigned long vdso_pages;
|
|
|
|
unsigned long vdso_base;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!vdso_enabled)
|
|
|
|
return 0;
|
|
|
|
|
2019-11-18 20:59:25 +08:00
|
|
|
if (is_compat_task())
|
|
|
|
return 0;
|
|
|
|
|
2008-12-25 20:38:36 +08:00
|
|
|
vdso_pages = vdso64_pages;
|
|
|
|
/*
|
|
|
|
* pick a base address for the vDSO in process space. We try to put
|
|
|
|
* it at vdso_base which is the "natural" base for it, but we might
|
|
|
|
* fail and end up putting it elsewhere.
|
|
|
|
*/
|
2020-06-09 12:33:25 +08:00
|
|
|
if (mmap_write_lock_killable(mm))
|
2016-05-24 07:25:54 +08:00
|
|
|
return -EINTR;
|
2011-01-12 16:55:24 +08:00
|
|
|
vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
|
2008-12-25 20:38:36 +08:00
|
|
|
if (IS_ERR_VALUE(vdso_base)) {
|
|
|
|
rc = vdso_base;
|
|
|
|
goto out_up;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* our vma flags don't have VM_WRITE so by default, the process
|
|
|
|
* isn't allowed to write those pages.
|
|
|
|
* gdb can break that with ptrace interface, and thus trigger COW
|
|
|
|
* on those pages but it's then your responsibility to never do that
|
|
|
|
* on the "data" page of the vDSO or you'll stop getting kernel
|
|
|
|
* updates and your nice userland gettimeofday will be totally dead.
|
|
|
|
* It's fine to use that for setting breakpoints in the vDSO code
|
coredump: remove VM_ALWAYSDUMP flag
The motivation for this patchset was that I was looking at a way for a
qemu-kvm process, to exclude the guest memory from its core dump, which
can be quite large. There are already a number of filter flags in
/proc/<pid>/coredump_filter, however, these allow one to specify 'types'
of kernel memory, not specific address ranges (which is needed in this
case).
Since there are no more vma flags available, the first patch eliminates
the need for the 'VM_ALWAYSDUMP' flag. The flag is used internally by
the kernel to mark vdso and vsyscall pages. However, it is simple
enough to check if a vma covers a vdso or vsyscall page without the need
for this flag.
The second patch then replaces the 'VM_ALWAYSDUMP' flag with a new
'VM_NODUMP' flag, which can be set by userspace using new madvise flags:
'MADV_DONTDUMP', and unset via 'MADV_DODUMP'. The core dump filters
continue to work the same as before unless 'MADV_DONTDUMP' is set on the
region.
The qemu code which implements this features is at:
http://people.redhat.com/~jbaron/qemu-dump/qemu-dump.patch
In my testing the qemu core dump shrunk from 383MB -> 13MB with this
patch.
I also believe that the 'MADV_DONTDUMP' flag might be useful for
security sensitive apps, which might want to select which areas are
dumped.
This patch:
The VM_ALWAYSDUMP flag is currently used by the coredump code to
indicate that a vma is part of a vsyscall or vdso section. However, we
can determine if a vma is in one these sections by checking it against
the gate_vma and checking for a non-NULL return value from
arch_vma_name(). Thus, freeing a valuable vma bit.
Signed-off-by: Jason Baron <jbaron@redhat.com>
Acked-by: Roland McGrath <roland@hack.frob.com>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Avi Kivity <avi@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-03-24 06:02:51 +08:00
|
|
|
* pages though.
|
2008-12-25 20:38:36 +08:00
|
|
|
*/
|
2017-05-15 16:23:38 +08:00
|
|
|
vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
|
|
|
|
VM_READ|VM_EXEC|
|
|
|
|
VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
|
|
|
|
&vdso_mapping);
|
|
|
|
if (IS_ERR(vma)) {
|
|
|
|
rc = PTR_ERR(vma);
|
|
|
|
goto out_up;
|
|
|
|
}
|
|
|
|
|
|
|
|
current->mm->context.vdso_base = vdso_base;
|
|
|
|
rc = 0;
|
|
|
|
|
2008-12-25 20:38:36 +08:00
|
|
|
out_up:
|
2020-06-09 12:33:25 +08:00
|
|
|
mmap_write_unlock(mm);
|
2008-12-25 20:38:36 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init vdso_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Calculate the size of the 64 bit vDSO */
|
2021-01-25 03:04:08 +08:00
|
|
|
vdso64_pages = ((&vdso64_end - &vdso64_start) >> PAGE_SHIFT) + 1;
|
2008-12-25 20:38:36 +08:00
|
|
|
|
|
|
|
/* Make sure pages are in the correct state */
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
vdso64_pagelist = kcalloc(vdso64_pages + 1, sizeof(struct page *),
|
2008-12-25 20:38:36 +08:00
|
|
|
GFP_KERNEL);
|
2021-01-25 03:08:40 +08:00
|
|
|
if (!vdso64_pagelist) {
|
|
|
|
vdso_enabled = 0;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2008-12-25 20:38:36 +08:00
|
|
|
for (i = 0; i < vdso64_pages - 1; i++) {
|
|
|
|
struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
|
|
|
|
get_page(pg);
|
|
|
|
vdso64_pagelist[i] = pg;
|
|
|
|
}
|
|
|
|
vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
|
|
|
|
vdso64_pagelist[vdso64_pages] = NULL;
|
|
|
|
|
|
|
|
get_page(virt_to_page(vdso_data));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-25 02:51:34 +08:00
|
|
|
arch_initcall(vdso_init);
|