Re: [PATCH 2/2] test: check copy_to/from_user boundary validation

From: Joe Perches
Date: Tue Dec 03 2013 - 16:53:46 EST


On Tue, 2013-12-03 at 13:27 -0800, Kees Cook wrote:
> To help avoid an architecture failing to correctly check kernel/user
> boundaries when handling copy_to_user, copy_from_user, put_user, or
> get_user, perform some simple tests and fail to load if any of them
> behave unexpectedly.

> diff --git a/kernel/test_user_copy.c b/kernel/test_user_copy.c

> +#define failed(msg) { \
> + pr_warn(msg); \
> + ret = -EINVAL; \
> +}

That's an ugly macro.

Maybe use something like:

#define test(func, msg) \
({ \
int ret = func; \
if (ret) \
pr_warn("%s\n", msg); \
ret; \
})

> +static int __init test_user_copy_init(void)
> +{
> + int ret = 0;
> + char *kmem;
> + char __user *usermem;
> + unsigned long user_addr;
> + unsigned long value = 0x5A;
> +
> + kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
> + if (!kmem) {
> + pr_warn("Failed to allocate kernel memory\n");

Unnecessary as there's a generic alloc OOM
with a dump_stack()

[]

> + /* Legitimate usage: none of these should fail. */
> + if (copy_from_user(kmem, usermem, PAGE_SIZE))
> + failed("legitimate copy_from_user failed");

ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
"legitimate copy_from_user failed");

> + if (copy_to_user(usermem, kmem, PAGE_SIZE))
> + failed("legitimate copy_to_user failed");

ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
"legitimate copy_to_user failed");

etc.


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/