Skip to content

Commit 784bd0f

Browse files
keesAlex Shi
authored andcommitted
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 <keescook@chromium.org> Tested-by: Valdis Kletnieks <valdis.kletnieks@vt.edu> (cherry picked from commit 04385fc5e8fffed84425d909a783c0f0c587d847) Signed-off-by: Alex Shi <alex.shi@linaro.org>
1 parent 41e3ca9 commit 784bd0f

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

init/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,7 @@ choice
17191719

17201720
config SLAB
17211721
bool "SLAB"
1722+
select HAVE_HARDENED_USERCOPY_ALLOCATOR
17221723
help
17231724
The regular slab allocator that is established and known to work
17241725
well in all environments. It organizes cache hot objects in

mm/slab.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4228,6 +4228,36 @@ static int __init slab_proc_init(void)
42284228
module_init(slab_proc_init);
42294229
#endif
42304230

4231+
#ifdef CONFIG_HARDENED_USERCOPY
4232+
/*
4233+
* Rejects objects that are incorrectly sized.
4234+
*
4235+
* Returns NULL if check passes, otherwise const char * to name of cache
4236+
* to indicate an error.
4237+
*/
4238+
const char *__check_heap_object(const void *ptr, unsigned long n,
4239+
struct page *page)
4240+
{
4241+
struct kmem_cache *cachep;
4242+
unsigned int objnr;
4243+
unsigned long offset;
4244+
4245+
/* Find and validate object. */
4246+
cachep = page->slab_cache;
4247+
objnr = obj_to_index(cachep, page, (void *)ptr);
4248+
BUG_ON(objnr >= cachep->num);
4249+
4250+
/* Find offset within object. */
4251+
offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep);
4252+
4253+
/* Allow address range falling entirely within object size. */
4254+
if (offset <= cachep->object_size && n <= cachep->object_size - offset)
4255+
return NULL;
4256+
4257+
return cachep->name;
4258+
}
4259+
#endif /* CONFIG_HARDENED_USERCOPY */
4260+
42314261
/**
42324262
* ksize - get the actual amount of memory allocated for a given object
42334263
* @objp: Pointer to the object

0 commit comments

Comments
 (0)