blob: d39a9b474727dc052fd4a6f5079314802615c8fb [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Thomas Renninger526b4af2011-05-26 12:26:24 +02002/*
Zhang Rui5baa1be2013-01-17 14:24:05 +01003 * custom_method.c - debugfs interface for customizing ACPI control method
Thomas Renninger526b4af2011-05-26 12:26:24 +02004 */
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>
Lv Zheng8b484632013-12-03 08:49:16 +080011#include <linux/acpi.h>
Matthew Garrettf474e142019-08-19 17:17:50 -070012#include <linux/security.h>
Thomas Renninger526b4af2011-05-26 12:26:24 +020013
14#include "internal.h"
15
Thomas Renninger526b4af2011-05-26 12:26:24 +020016MODULE_LICENSE("GPL");
17
18static struct dentry *cm_dentry;
19
20/* /sys/kernel/debug/acpi/custom_method */
21
Xiaofei Tan4dea6e82021-03-27 20:08:21 +080022static ssize_t cm_write(struct file *file, const char __user *user_buf,
Thomas Renninger526b4af2011-05-26 12:26:24 +020023 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;
Matthew Garrettf474e142019-08-19 17:17:50 -070031 int ret;
32
33 ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
34 if (ret)
35 return ret;
Thomas Renninger526b4af2011-05-26 12:26:24 +020036
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;
Mark Langsdorf1cfd8952021-04-27 13:54:33 -050045 /* make sure the buf is not allocated */
46 kfree(buf);
Thomas Renninger526b4af2011-05-26 12:26:24 +020047 buf = kzalloc(max_size, GFP_KERNEL);
48 if (!buf)
49 return -ENOMEM;
50 }
51
52 if (buf == NULL)
53 return -EINVAL;
54
55 if ((*ppos > max_size) ||
56 (*ppos + count > max_size) ||
57 (*ppos + count < count) ||
Wenwen Wang03d15712019-08-16 00:08:27 -050058 (count > uncopied_bytes)) {
59 kfree(buf);
Mark Langsdorfe483bb92021-04-23 10:28:17 -050060 buf = NULL;
Thomas Renninger526b4af2011-05-26 12:26:24 +020061 return -EINVAL;
Wenwen Wang03d15712019-08-16 00:08:27 -050062 }
Thomas Renninger526b4af2011-05-26 12:26:24 +020063
64 if (copy_from_user(buf + (*ppos), user_buf, count)) {
65 kfree(buf);
66 buf = NULL;
67 return -EFAULT;
68 }
69
70 uncopied_bytes -= count;
71 *ppos += count;
72
73 if (!uncopied_bytes) {
74 status = acpi_install_method(buf);
75 kfree(buf);
76 buf = NULL;
77 if (ACPI_FAILURE(status))
78 return -EINVAL;
Rusty Russell373d4d02013-01-21 17:17:39 +103079 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
Thomas Renninger526b4af2011-05-26 12:26:24 +020080 }
81
82 return count;
83}
84
85static const struct file_operations cm_fops = {
86 .write = cm_write,
87 .llseek = default_llseek,
88};
89
90static int __init acpi_custom_method_init(void)
91{
Thomas Renninger526b4af2011-05-26 12:26:24 +020092 cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
93 acpi_debugfs_dir, NULL, &cm_fops);
Thomas Renninger526b4af2011-05-26 12:26:24 +020094 return 0;
95}
96
97static void __exit acpi_custom_method_exit(void)
98{
zhong jiang7e79a692018-08-18 18:49:20 +080099 debugfs_remove(cm_dentry);
Joe Perches447a5642018-03-21 15:09:32 -0700100}
Thomas Renninger526b4af2011-05-26 12:26:24 +0200101
102module_init(acpi_custom_method_init);
103module_exit(acpi_custom_method_exit);