18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 2014 Samsung Electronics Co., Ltd.
58c2ecf20Sopenharmony_ci * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "kasan test: %s " fmt, __func__
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/mman.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/printk.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "../mm/kasan/kasan.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic noinline void __init copy_user_test(void)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	char *kmem;
238c2ecf20Sopenharmony_ci	char __user *usermem;
248c2ecf20Sopenharmony_ci	size_t size = 10;
258c2ecf20Sopenharmony_ci	int unused;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	kmem = kmalloc(size, GFP_KERNEL);
288c2ecf20Sopenharmony_ci	if (!kmem)
298c2ecf20Sopenharmony_ci		return;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
328c2ecf20Sopenharmony_ci			    PROT_READ | PROT_WRITE | PROT_EXEC,
338c2ecf20Sopenharmony_ci			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
348c2ecf20Sopenharmony_ci	if (IS_ERR(usermem)) {
358c2ecf20Sopenharmony_ci		pr_err("Failed to allocate user memory\n");
368c2ecf20Sopenharmony_ci		kfree(kmem);
378c2ecf20Sopenharmony_ci		return;
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in copy_from_user()\n");
418c2ecf20Sopenharmony_ci	unused = copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in copy_to_user()\n");
448c2ecf20Sopenharmony_ci	unused = copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in __copy_from_user()\n");
478c2ecf20Sopenharmony_ci	unused = __copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in __copy_to_user()\n");
508c2ecf20Sopenharmony_ci	unused = __copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
538c2ecf20Sopenharmony_ci	unused = __copy_from_user_inatomic(kmem, usermem, size + 1 + OOB_TAG_OFF);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
568c2ecf20Sopenharmony_ci	unused = __copy_to_user_inatomic(usermem, kmem, size + 1 + OOB_TAG_OFF);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	pr_info("out-of-bounds in strncpy_from_user()\n");
598c2ecf20Sopenharmony_ci	unused = strncpy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	vm_munmap((unsigned long)usermem, PAGE_SIZE);
628c2ecf20Sopenharmony_ci	kfree(kmem);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic struct kasan_rcu_info {
668c2ecf20Sopenharmony_ci	int i;
678c2ecf20Sopenharmony_ci	struct rcu_head rcu;
688c2ecf20Sopenharmony_ci} *global_rcu_ptr;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic noinline void __init kasan_rcu_reclaim(struct rcu_head *rp)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct kasan_rcu_info *fp = container_of(rp,
738c2ecf20Sopenharmony_ci						struct kasan_rcu_info, rcu);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	kfree(fp);
768c2ecf20Sopenharmony_ci	fp->i = 1;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic noinline void __init kasan_rcu_uaf(void)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct kasan_rcu_info *ptr;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	pr_info("use-after-free in kasan_rcu_reclaim\n");
848c2ecf20Sopenharmony_ci	ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL);
858c2ecf20Sopenharmony_ci	if (!ptr) {
868c2ecf20Sopenharmony_ci		pr_err("Allocation failed\n");
878c2ecf20Sopenharmony_ci		return;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	global_rcu_ptr = rcu_dereference_protected(ptr, NULL);
918c2ecf20Sopenharmony_ci	call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic int __init test_kasan_module_init(void)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	/*
988c2ecf20Sopenharmony_ci	 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
998c2ecf20Sopenharmony_ci	 * report for the first case.
1008c2ecf20Sopenharmony_ci	 */
1018c2ecf20Sopenharmony_ci	bool multishot = kasan_save_enable_multi_shot();
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	copy_user_test();
1048c2ecf20Sopenharmony_ci	kasan_rcu_uaf();
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	kasan_restore_multi_shot(multishot);
1078c2ecf20Sopenharmony_ci	return -EAGAIN;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cimodule_init(test_kasan_module_init);
1118c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
112