mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-30 03:27:44 -04:00
mm: SLAB hardened usercopy support
Under CONFIG_HARDENED_USERCOPY, this adds object size checking to the SLAB allocator to catch any copies that may span objects. Based on code from PaX and grsecurity. Signed-off-by: Kees Cook <[email protected]> Tested-by: Valdis Kletnieks <[email protected]>
This commit is contained in:
parent
97433ea4fd
commit
04385fc5e8
2 changed files with 31 additions and 0 deletions
30
mm/slab.c
30
mm/slab.c
|
|
@ -4477,6 +4477,36 @@ static int __init slab_proc_init(void)
|
|||
module_init(slab_proc_init);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_HARDENED_USERCOPY
|
||||
/*
|
||||
* Rejects objects that are incorrectly sized.
|
||||
*
|
||||
* Returns NULL if check passes, otherwise const char * to name of cache
|
||||
* to indicate an error.
|
||||
*/
|
||||
const char *__check_heap_object(const void *ptr, unsigned long n,
|
||||
struct page *page)
|
||||
{
|
||||
struct kmem_cache *cachep;
|
||||
unsigned int objnr;
|
||||
unsigned long offset;
|
||||
|
||||
/* Find and validate object. */
|
||||
cachep = page->slab_cache;
|
||||
objnr = obj_to_index(cachep, page, (void *)ptr);
|
||||
BUG_ON(objnr >= cachep->num);
|
||||
|
||||
/* Find offset within object. */
|
||||
offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep);
|
||||
|
||||
/* Allow address range falling entirely within object size. */
|
||||
if (offset <= cachep->object_size && n <= cachep->object_size - offset)
|
||||
return NULL;
|
||||
|
||||
return cachep->name;
|
||||
}
|
||||
#endif /* CONFIG_HARDENED_USERCOPY */
|
||||
|
||||
/**
|
||||
* ksize - get the actual amount of memory allocated for a given object
|
||||
* @objp: Pointer to the object
|
||||
|
|
|
|||
Loading…
Reference in a new issue