Rather than incurring a division or requesting too many random bytes for
the given range, use the prandom_u32_max() function, which only takes
the minimum required bytes from the RNG and avoids divisions. This was
done mechanically with this coccinelle script:
@basic@
expression E;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u64;
@@
(
- ((T)get_random_u32() % (E))
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ((E) - 1))
+ prandom_u32_max(E * XXX_MAKE_SURE_E_IS_POW2)
|
- ((u64)(E) * get_random_u32() >> 32)
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ~PAGE_MASK)
+ prandom_u32_max(PAGE_SIZE)
)
@multi_line@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
identifier RAND;
expression E;
@@
- RAND = get_random_u32();
... when != RAND
- RAND %= (E);
+ RAND = prandom_u32_max(E);
// Find a potential literal
@literal_mask@
expression LITERAL;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
position p;
@@
((T)get_random_u32()@p & (LITERAL))
// Add one to the literal.
@script:python add_one@
literal << literal_mask.LITERAL;
RESULT;
@@
value = None
if literal.startswith('0x'):
value = int(literal, 16)
elif literal[0] in '123456789':
value = int(literal, 10)
if value is None:
print("I don't know how to handle %s" % (literal))
cocci.include_match(False)
elif value == 2**32 - 1 or value == 2**31 - 1 or value == 2**24 - 1 or value == 2**16 - 1 or value == 2**8 - 1:
print("Skipping 0x%x for cleanup elsewhere" % (value))
cocci.include_match(False)
elif value & (value + 1) != 0:
print("Skipping 0x%x because it's not a power of two minus one" % (value))
cocci.include_match(False)
elif literal.startswith('0x'):
coccinelle.RESULT = cocci.make_expr("0x%x" % (value + 1))
else:
coccinelle.RESULT = cocci.make_expr("%d" % (value + 1))
// Replace the literal mask with the calculated result.
@plus_one@
expression literal_mask.LITERAL;
position literal_mask.p;
expression add_one.RESULT;
identifier FUNC;
@@
- (FUNC()@p & (LITERAL))
+ prandom_u32_max(RESULT)
@collapse_ret@
type T;
identifier VAR;
expression E;
@@
{
- T VAR;
- VAR = (E);
- return VAR;
+ return E;
}
@drop_var@
type T;
identifier VAR;
@@
{
- T VAR;
... when != VAR
}
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: KP Singh <kpsingh@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz> # for ext4 and sbitmap
Reviewed-by: Christoph Böhmwalder <christoph.boehmwalder@linbit.com> # for drbd
Acked-by: Jakub Kicinski <kuba@kernel.org>
Acked-by: Heiko Carstens <hca@linux.ibm.com> # for s390
Acked-by: Ulf Hansson <ulf.hansson@linaro.org> # for mmc
Acked-by: Darrick J. Wong <djwong@kernel.org> # for xfs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
123 lines
3.0 KiB
C
123 lines
3.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright (c) 2022 Helge Deller <deller@gmx.de>
|
|
*
|
|
* based on arch/s390/kernel/vdso.c which is
|
|
* Copyright IBM Corp. 2008
|
|
* Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/elf.h>
|
|
#include <linux/timekeeper_internal.h>
|
|
#include <linux/compat.h>
|
|
#include <linux/nsproxy.h>
|
|
#include <linux/time_namespace.h>
|
|
#include <linux/random.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
#include <asm/page.h>
|
|
#include <asm/sections.h>
|
|
#include <asm/vdso.h>
|
|
#include <asm/cacheflush.h>
|
|
|
|
extern char vdso32_start, vdso32_end;
|
|
extern char vdso64_start, vdso64_end;
|
|
|
|
static int vdso_mremap(const struct vm_special_mapping *sm,
|
|
struct vm_area_struct *vma)
|
|
{
|
|
current->mm->context.vdso_base = vma->vm_start;
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_64BIT
|
|
static struct vm_special_mapping vdso64_mapping = {
|
|
.name = "[vdso]",
|
|
.mremap = vdso_mremap,
|
|
};
|
|
#endif
|
|
|
|
static struct vm_special_mapping vdso32_mapping = {
|
|
.name = "[vdso]",
|
|
.mremap = vdso_mremap,
|
|
};
|
|
|
|
/*
|
|
* 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 executable_stack)
|
|
{
|
|
|
|
unsigned long vdso_text_start, vdso_text_len, map_base;
|
|
struct vm_special_mapping *vdso_mapping;
|
|
struct mm_struct *mm = current->mm;
|
|
struct vm_area_struct *vma;
|
|
int rc;
|
|
|
|
if (mmap_write_lock_killable(mm))
|
|
return -EINTR;
|
|
|
|
#ifdef CONFIG_64BIT
|
|
if (!is_compat_task()) {
|
|
vdso_text_len = &vdso64_end - &vdso64_start;
|
|
vdso_mapping = &vdso64_mapping;
|
|
} else
|
|
#endif
|
|
{
|
|
vdso_text_len = &vdso32_end - &vdso32_start;
|
|
vdso_mapping = &vdso32_mapping;
|
|
}
|
|
|
|
map_base = mm->mmap_base;
|
|
if (current->flags & PF_RANDOMIZE)
|
|
map_base -= prandom_u32_max(0x20) * PAGE_SIZE;
|
|
|
|
vdso_text_start = get_unmapped_area(NULL, map_base, vdso_text_len, 0, 0);
|
|
|
|
/* VM_MAYWRITE for COW so gdb can set breakpoints */
|
|
vma = _install_special_mapping(mm, vdso_text_start, vdso_text_len,
|
|
VM_READ|VM_EXEC|
|
|
VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
|
|
vdso_mapping);
|
|
if (IS_ERR(vma)) {
|
|
do_munmap(mm, vdso_text_start, PAGE_SIZE, NULL);
|
|
rc = PTR_ERR(vma);
|
|
} else {
|
|
current->mm->context.vdso_base = vdso_text_start;
|
|
rc = 0;
|
|
}
|
|
|
|
mmap_write_unlock(mm);
|
|
return rc;
|
|
}
|
|
|
|
static struct page ** __init vdso_setup_pages(void *start, void *end)
|
|
{
|
|
int pages = (end - start) >> PAGE_SHIFT;
|
|
struct page **pagelist;
|
|
int i;
|
|
|
|
pagelist = kcalloc(pages + 1, sizeof(struct page *), GFP_KERNEL);
|
|
if (!pagelist)
|
|
panic("%s: Cannot allocate page list for VDSO", __func__);
|
|
for (i = 0; i < pages; i++)
|
|
pagelist[i] = virt_to_page(start + i * PAGE_SIZE);
|
|
return pagelist;
|
|
}
|
|
|
|
static int __init vdso_init(void)
|
|
{
|
|
#ifdef CONFIG_64BIT
|
|
vdso64_mapping.pages = vdso_setup_pages(&vdso64_start, &vdso64_end);
|
|
#endif
|
|
if (IS_ENABLED(CONFIG_COMPAT) || !IS_ENABLED(CONFIG_64BIT))
|
|
vdso32_mapping.pages = vdso_setup_pages(&vdso32_start, &vdso32_end);
|
|
return 0;
|
|
}
|
|
arch_initcall(vdso_init);
|