Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 2 | /* |
Zhang Rui | 5baa1be | 2013-01-17 14:24:05 +0100 | [diff] [blame] | 3 | * custom_method.c - debugfs interface for customizing ACPI control method |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 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> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 11 | #include <linux/acpi.h> |
Matthew Garrett | f474e14 | 2019-08-19 17:17:50 -0700 | [diff] [blame] | 12 | #include <linux/security.h> |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 13 | |
| 14 | #include "internal.h" |
| 15 | |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 16 | MODULE_LICENSE("GPL"); |
| 17 | |
| 18 | static struct dentry *cm_dentry; |
| 19 | |
| 20 | /* /sys/kernel/debug/acpi/custom_method */ |
| 21 | |
Xiaofei Tan | 4dea6e8 | 2021-03-27 20:08:21 +0800 | [diff] [blame] | 22 | static ssize_t cm_write(struct file *file, const char __user *user_buf, |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 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; |
Matthew Garrett | f474e14 | 2019-08-19 17:17:50 -0700 | [diff] [blame] | 31 | int ret; |
| 32 | |
| 33 | ret = security_locked_down(LOCKDOWN_ACPI_TABLES); |
| 34 | if (ret) |
| 35 | return ret; |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 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; |
Mark Langsdorf | 1cfd895 | 2021-04-27 13:54:33 -0500 | [diff] [blame] | 45 | /* make sure the buf is not allocated */ |
| 46 | kfree(buf); |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 47 | 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 Wang | 03d1571 | 2019-08-16 00:08:27 -0500 | [diff] [blame] | 58 | (count > uncopied_bytes)) { |
| 59 | kfree(buf); |
Mark Langsdorf | e483bb9 | 2021-04-23 10:28:17 -0500 | [diff] [blame] | 60 | buf = NULL; |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 61 | return -EINVAL; |
Wenwen Wang | 03d1571 | 2019-08-16 00:08:27 -0500 | [diff] [blame] | 62 | } |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 63 | |
| 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 Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 79 | add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE); |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return count; |
| 83 | } |
| 84 | |
| 85 | static const struct file_operations cm_fops = { |
| 86 | .write = cm_write, |
| 87 | .llseek = default_llseek, |
| 88 | }; |
| 89 | |
| 90 | static int __init acpi_custom_method_init(void) |
| 91 | { |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 92 | cm_dentry = debugfs_create_file("custom_method", S_IWUSR, |
| 93 | acpi_debugfs_dir, NULL, &cm_fops); |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static void __exit acpi_custom_method_exit(void) |
| 98 | { |
zhong jiang | 7e79a69 | 2018-08-18 18:49:20 +0800 | [diff] [blame] | 99 | debugfs_remove(cm_dentry); |
Joe Perches | 447a564 | 2018-03-21 15:09:32 -0700 | [diff] [blame] | 100 | } |
Thomas Renninger | 526b4af | 2011-05-26 12:26:24 +0200 | [diff] [blame] | 101 | |
| 102 | module_init(acpi_custom_method_init); |
| 103 | module_exit(acpi_custom_method_exit); |