blob: e7716c7b9be76d0d676353c8ba2df46c400dc100 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
David Brazdil0f672f62019-12-10 10:32:29 +000012#include <linux/security.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013
14#include "internal.h"
15
16#define _COMPONENT ACPI_SYSTEM_COMPONENT
17ACPI_MODULE_NAME("custom_method");
18MODULE_LICENSE("GPL");
19
20static struct dentry *cm_dentry;
21
22/* /sys/kernel/debug/acpi/custom_method */
23
24static 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;
David Brazdil0f672f62019-12-10 10:32:29 +000033 int ret;
34
35 ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
36 if (ret)
37 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038
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;
Olivier Deprez0e641232021-09-23 10:07:05 +020047 /* make sure the buf is not allocated */
48 kfree(buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 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) ||
David Brazdil0f672f62019-12-10 10:32:29 +000060 (count > uncopied_bytes)) {
61 kfree(buf);
Olivier Deprez0e641232021-09-23 10:07:05 +020062 buf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +000064 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065
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
87static const struct file_operations cm_fops = {
88 .write = cm_write,
89 .llseek = default_llseek,
90};
91
92static int __init acpi_custom_method_init(void)
93{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
95 acpi_debugfs_dir, NULL, &cm_fops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 return 0;
97}
98
99static void __exit acpi_custom_method_exit(void)
100{
David Brazdil0f672f62019-12-10 10:32:29 +0000101 debugfs_remove(cm_dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102}
103
104module_init(acpi_custom_method_init);
105module_exit(acpi_custom_method_exit);