Skip to content

Commit 2d891fb

Browse files
committed
usercopy: Allow strict enforcement of whitelists
This introduces CONFIG_HARDENED_USERCOPY_FALLBACK to control the behavior of hardened usercopy whitelist violations. By default, whitelist violations will continue to WARN() so that any bad or missing usercopy whitelists can be discovered without being too disruptive. If this config is disabled at build time or a system is booted with "slab_common.usercopy_fallback=0", usercopy whitelists will BUG() instead of WARN(). This is useful for admins that want to use usercopy whitelists immediately. Suggested-by: Matthew Garrett <mjg59@google.com> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent afcc90f commit 2d891fb

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎include/linux/slab.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ struct mem_cgroup;
135135
void __init kmem_cache_init(void);
136136
bool slab_is_available(void);
137137

138+
extern bool usercopy_fallback;
139+
138140
struct kmem_cache *kmem_cache_create(const char *name, size_t size,
139141
size_t align, slab_flags_t flags,
140142
void (*ctor)(void *));

‎mm/slab.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4426,7 +4426,8 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
44264426
* to be a temporary method to find any missing usercopy
44274427
* whitelists.
44284428
*/
4429-
if (offset <= cachep->object_size &&
4429+
if (usercopy_fallback &&
4430+
offset <= cachep->object_size &&
44304431
n <= cachep->object_size - offset) {
44314432
usercopy_warn("SLAB object", cachep->name, to_user, offset, n);
44324433
return;

‎mm/slab_common.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ LIST_HEAD(slab_caches);
3131
DEFINE_MUTEX(slab_mutex);
3232
struct kmem_cache *kmem_cache;
3333

34+
#ifdef CONFIG_HARDENED_USERCOPY
35+
bool usercopy_fallback __ro_after_init =
36+
IS_ENABLED(CONFIG_HARDENED_USERCOPY_FALLBACK);
37+
module_param(usercopy_fallback, bool, 0400);
38+
MODULE_PARM_DESC(usercopy_fallback,
39+
"WARN instead of reject usercopy whitelist violations");
40+
#endif
41+
3442
static LIST_HEAD(slab_caches_to_rcu_destroy);
3543
static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
3644
static DECLARE_WORK(slab_caches_to_rcu_destroy_work,

‎mm/slub.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,8 @@ void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
38593859
* whitelists.
38603860
*/
38613861
object_size = slab_ksize(s);
3862-
if (offset <= object_size && n <= object_size - offset) {
3862+
if (usercopy_fallback &&
3863+
offset <= object_size && n <= object_size - offset) {
38633864
usercopy_warn("SLUB object", s->name, to_user, offset, n);
38643865
return;
38653866
}

‎security/Kconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ config HARDENED_USERCOPY
152152
or are part of the kernel text. This kills entire classes
153153
of heap overflow exploits and similar kernel memory exposures.
154154

155+
config HARDENED_USERCOPY_FALLBACK
156+
bool "Allow usercopy whitelist violations to fallback to object size"
157+
depends on HARDENED_USERCOPY
158+
default y
159+
help
160+
This is a temporary option that allows missing usercopy whitelists
161+
to be discovered via a WARN() to the kernel log, instead of
162+
rejecting the copy, falling back to non-whitelisted hardened
163+
usercopy that checks the slab allocation size instead of the
164+
whitelist size. This option will be removed once it seems like
165+
all missing usercopy whitelists have been identified and fixed.
166+
Booting with "slab_common.usercopy_fallback=Y/N" can change
167+
this setting.
168+
155169
config HARDENED_USERCOPY_PAGESPAN
156170
bool "Refuse to copy allocations that span multiple pages"
157171
depends on HARDENED_USERCOPY

0 commit comments

Comments
 (0)