slab: Generify kernel pointer validation
As suggested by Linus, introduce a kern_ptr_validate() helper that does some sanity checks to make sure a pointer is a valid kernel pointer. This is a preparational step for fixing SLUB kmem_ptr_validate(). Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Christoph Lameter <cl@linux-foundation.org> Cc: David Rientjes <rientjes@google.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Matt Mackall <mpm@selenic.com> Cc: Nick Piggin <npiggin@suse.de> Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
committed by
Linus Torvalds
parent
4dc86ae1f9
commit
fc1c183353
21
mm/util.c
21
mm/util.c
@@ -186,6 +186,27 @@ void kzfree(const void *p)
|
||||
}
|
||||
EXPORT_SYMBOL(kzfree);
|
||||
|
||||
int kern_ptr_validate(const void *ptr, unsigned long size)
|
||||
{
|
||||
unsigned long addr = (unsigned long)ptr;
|
||||
unsigned long min_addr = PAGE_OFFSET;
|
||||
unsigned long align_mask = sizeof(void *) - 1;
|
||||
|
||||
if (unlikely(addr < min_addr))
|
||||
goto out;
|
||||
if (unlikely(addr > (unsigned long)high_memory - size))
|
||||
goto out;
|
||||
if (unlikely(addr & align_mask))
|
||||
goto out;
|
||||
if (unlikely(!kern_addr_valid(addr)))
|
||||
goto out;
|
||||
if (unlikely(!kern_addr_valid(addr + size - 1)))
|
||||
goto out;
|
||||
return 1;
|
||||
out:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* strndup_user - duplicate an existing string from user space
|
||||
* @s: The string to duplicate
|
||||
|
Reference in New Issue
Block a user