David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12 | #include <linux/security.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 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; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 33 | int ret; |
| 34 | |
| 35 | ret = security_locked_down(LOCKDOWN_ACPI_TABLES); |
| 36 | if (ret) |
| 37 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 47 | /* make sure the buf is not allocated */ |
| 48 | kfree(buf); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 60 | (count > uncopied_bytes)) { |
| 61 | kfree(buf); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 62 | buf = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 63 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 64 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 92 | static int __init acpi_custom_method_init(void) |
| 93 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | cm_dentry = debugfs_create_file("custom_method", S_IWUSR, |
| 95 | acpi_debugfs_dir, NULL, &cm_fops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void __exit acpi_custom_method_exit(void) |
| 100 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 101 | debugfs_remove(cm_dentry); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | module_init(acpi_custom_method_init); |
| 105 | module_exit(acpi_custom_method_exit); |