custom_method.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * custom_method.c - debugfs interface for customizing ACPI control method
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/acpi.h>
  11. #include <linux/security.h>
  12. #include "internal.h"
  13. MODULE_LICENSE("GPL");
  14. static struct dentry *cm_dentry;
  15. /* /sys/kernel/debug/acpi/custom_method */
  16. static ssize_t cm_write(struct file *file, const char __user *user_buf,
  17. size_t count, loff_t *ppos)
  18. {
  19. static char *buf;
  20. static u32 max_size;
  21. static u32 uncopied_bytes;
  22. struct acpi_table_header table;
  23. acpi_status status;
  24. int ret;
  25. ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
  26. if (ret)
  27. return ret;
  28. if (!(*ppos)) {
  29. /* parse the table header to get the table length */
  30. if (count <= sizeof(struct acpi_table_header))
  31. return -EINVAL;
  32. if (copy_from_user(&table, user_buf,
  33. sizeof(struct acpi_table_header)))
  34. return -EFAULT;
  35. uncopied_bytes = max_size = table.length;
  36. /* make sure the buf is not allocated */
  37. kfree(buf);
  38. buf = kzalloc(max_size, GFP_KERNEL);
  39. if (!buf)
  40. return -ENOMEM;
  41. }
  42. if (buf == NULL)
  43. return -EINVAL;
  44. if ((*ppos > max_size) ||
  45. (*ppos + count > max_size) ||
  46. (*ppos + count < count) ||
  47. (count > uncopied_bytes)) {
  48. kfree(buf);
  49. buf = NULL;
  50. return -EINVAL;
  51. }
  52. if (copy_from_user(buf + (*ppos), user_buf, count)) {
  53. kfree(buf);
  54. buf = NULL;
  55. return -EFAULT;
  56. }
  57. uncopied_bytes -= count;
  58. *ppos += count;
  59. if (!uncopied_bytes) {
  60. status = acpi_install_method(buf);
  61. kfree(buf);
  62. buf = NULL;
  63. if (ACPI_FAILURE(status))
  64. return -EINVAL;
  65. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
  66. }
  67. return count;
  68. }
  69. static const struct file_operations cm_fops = {
  70. .write = cm_write,
  71. .llseek = default_llseek,
  72. };
  73. static int __init acpi_custom_method_init(void)
  74. {
  75. cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
  76. acpi_debugfs_dir, NULL, &cm_fops);
  77. return 0;
  78. }
  79. static void __exit acpi_custom_method_exit(void)
  80. {
  81. debugfs_remove(cm_dentry);
  82. }
  83. module_init(acpi_custom_method_init);
  84. module_exit(acpi_custom_method_exit);