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