• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * custom_method.c - debugfs interface for customizing ACPI control method
4  */
5 
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/uaccess.h>
10 #include <linux/debugfs.h>
11 #include <linux/acpi.h>
12 #include <linux/security.h>
13 
14 #include "internal.h"
15 
16 #define _COMPONENT		ACPI_SYSTEM_COMPONENT
17 ACPI_MODULE_NAME("custom_method");
18 MODULE_LICENSE("GPL");
19 
20 static struct dentry *cm_dentry;
21 
22 /* /sys/kernel/debug/acpi/custom_method */
23 
cm_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)24 static ssize_t cm_write(struct file *file, const char __user * user_buf,
25 			size_t count, loff_t *ppos)
26 {
27 	static char *buf;
28 	static u32 max_size;
29 	static u32 uncopied_bytes;
30 
31 	struct acpi_table_header table;
32 	acpi_status status;
33 	int ret;
34 
35 	ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
36 	if (ret)
37 		return ret;
38 
39 	if (!(*ppos)) {
40 		/* parse the table header to get the table length */
41 		if (count <= sizeof(struct acpi_table_header))
42 			return -EINVAL;
43 		if (copy_from_user(&table, user_buf,
44 				   sizeof(struct acpi_table_header)))
45 			return -EFAULT;
46 		uncopied_bytes = max_size = table.length;
47 		/* make sure the buf is not allocated */
48 		kfree(buf);
49 		buf = kzalloc(max_size, GFP_KERNEL);
50 		if (!buf)
51 			return -ENOMEM;
52 	}
53 
54 	if (buf == NULL)
55 		return -EINVAL;
56 
57 	if ((*ppos > max_size) ||
58 	    (*ppos + count > max_size) ||
59 	    (*ppos + count < count) ||
60 	    (count > uncopied_bytes)) {
61 		kfree(buf);
62 		buf = NULL;
63 		return -EINVAL;
64 	}
65 
66 	if (copy_from_user(buf + (*ppos), user_buf, count)) {
67 		kfree(buf);
68 		buf = NULL;
69 		return -EFAULT;
70 	}
71 
72 	uncopied_bytes -= count;
73 	*ppos += count;
74 
75 	if (!uncopied_bytes) {
76 		status = acpi_install_method(buf);
77 		kfree(buf);
78 		buf = NULL;
79 		if (ACPI_FAILURE(status))
80 			return -EINVAL;
81 		add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
82 	}
83 
84 	return count;
85 }
86 
87 static const struct file_operations cm_fops = {
88 	.write = cm_write,
89 	.llseek = default_llseek,
90 };
91 
acpi_custom_method_init(void)92 static int __init acpi_custom_method_init(void)
93 {
94 	cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
95 					acpi_debugfs_dir, NULL, &cm_fops);
96 	return 0;
97 }
98 
acpi_custom_method_exit(void)99 static void __exit acpi_custom_method_exit(void)
100 {
101 	debugfs_remove(cm_dentry);
102 }
103 
104 module_init(acpi_custom_method_init);
105 module_exit(acpi_custom_method_exit);
106