Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "../mm/kasan/kasan.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static noinline void __init copy_user_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	char *kmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	char __user *usermem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	size_t size = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	int unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	kmem = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (!kmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 			    PROT_READ | PROT_WRITE | PROT_EXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	if (IS_ERR(usermem)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		pr_err("Failed to allocate user memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		kfree(kmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	pr_info("out-of-bounds in copy_from_user()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unused = copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	pr_info("out-of-bounds in copy_to_user()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	unused = copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	pr_info("out-of-bounds in __copy_from_user()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	unused = __copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	pr_info("out-of-bounds in __copy_to_user()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unused = __copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unused = __copy_from_user_inatomic(kmem, usermem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unused = __copy_to_user_inatomic(usermem, kmem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	pr_info("out-of-bounds in strncpy_from_user()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unused = strncpy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	vm_munmap((unsigned long)usermem, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	kfree(kmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static struct kasan_rcu_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) } *global_rcu_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static noinline void __init kasan_rcu_reclaim(struct rcu_head *rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct kasan_rcu_info *fp = container_of(rp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 						struct kasan_rcu_info, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	kfree(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	fp->i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static noinline void __init kasan_rcu_uaf(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct kasan_rcu_info *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	pr_info("use-after-free in kasan_rcu_reclaim\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		pr_err("Allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	global_rcu_ptr = rcu_dereference_protected(ptr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static noinline void __init kasan_workqueue_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	kfree(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static noinline void __init kasan_workqueue_uaf(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct workqueue_struct *workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct work_struct *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	workqueue = create_workqueue("kasan_wq_test");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (!workqueue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		pr_err("Allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	work = kmalloc(sizeof(struct work_struct), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!work) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		pr_err("Allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	INIT_WORK(work, kasan_workqueue_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	queue_work(workqueue, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	destroy_workqueue(workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	pr_info("use-after-free on workqueue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	((volatile struct work_struct *)work)->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int __init test_kasan_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * Temporarily enable multi-shot mode. Otherwise, KASAN would only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 * report the first detected bug and panic the kernel if panic_on_warn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	 * is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	bool multishot = kasan_save_enable_multi_shot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	copy_user_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	kasan_rcu_uaf();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	kasan_workqueue_uaf();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	kasan_restore_multi_shot(multishot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) module_init(test_kasan_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) MODULE_LICENSE("GPL");