019-3-nvmem-Add-flag-to-export-NVMEM-to-root-only.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. From 811b0d6538b9f26f3eb0f90fe4e6118f2480ec6f Mon Sep 17 00:00:00 2001
  2. From: Andrew Lunn <andrew@lunn.ch>
  3. Date: Fri, 26 Feb 2016 20:59:18 +0100
  4. Subject: nvmem: Add flag to export NVMEM to root only
  5. Legacy AT24, AT25 EEPROMs are exported in sys so that only root can
  6. read the contents. The EEPROMs may contain sensitive information. Add
  7. a flag so the provide can indicate that NVMEM should also restrict
  8. access to root only.
  9. Signed-off-by: Andrew Lunn <andrew@lunn.ch>
  10. Acked-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  11. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  12. ---
  13. drivers/nvmem/core.c | 57 ++++++++++++++++++++++++++++++++++++++++--
  14. include/linux/nvmem-provider.h | 1 +
  15. 2 files changed, 56 insertions(+), 2 deletions(-)
  16. --- a/drivers/nvmem/core.c
  17. +++ b/drivers/nvmem/core.c
  18. @@ -161,6 +161,53 @@ static const struct attribute_group *nvm
  19. NULL,
  20. };
  21. +/* default read/write permissions, root only */
  22. +static struct bin_attribute bin_attr_rw_root_nvmem = {
  23. + .attr = {
  24. + .name = "nvmem",
  25. + .mode = S_IWUSR | S_IRUSR,
  26. + },
  27. + .read = bin_attr_nvmem_read,
  28. + .write = bin_attr_nvmem_write,
  29. +};
  30. +
  31. +static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
  32. + &bin_attr_rw_root_nvmem,
  33. + NULL,
  34. +};
  35. +
  36. +static const struct attribute_group nvmem_bin_rw_root_group = {
  37. + .bin_attrs = nvmem_bin_rw_root_attributes,
  38. +};
  39. +
  40. +static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
  41. + &nvmem_bin_rw_root_group,
  42. + NULL,
  43. +};
  44. +
  45. +/* read only permission, root only */
  46. +static struct bin_attribute bin_attr_ro_root_nvmem = {
  47. + .attr = {
  48. + .name = "nvmem",
  49. + .mode = S_IRUSR,
  50. + },
  51. + .read = bin_attr_nvmem_read,
  52. +};
  53. +
  54. +static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
  55. + &bin_attr_ro_root_nvmem,
  56. + NULL,
  57. +};
  58. +
  59. +static const struct attribute_group nvmem_bin_ro_root_group = {
  60. + .bin_attrs = nvmem_bin_ro_root_attributes,
  61. +};
  62. +
  63. +static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
  64. + &nvmem_bin_ro_root_group,
  65. + NULL,
  66. +};
  67. +
  68. static void nvmem_release(struct device *dev)
  69. {
  70. struct nvmem_device *nvmem = to_nvmem_device(dev);
  71. @@ -355,8 +402,14 @@ struct nvmem_device *nvmem_register(cons
  72. nvmem->read_only = of_property_read_bool(np, "read-only") |
  73. config->read_only;
  74. - nvmem->dev.groups = nvmem->read_only ? nvmem_ro_dev_groups :
  75. - nvmem_rw_dev_groups;
  76. + if (config->root_only)
  77. + nvmem->dev.groups = nvmem->read_only ?
  78. + nvmem_ro_root_dev_groups :
  79. + nvmem_rw_root_dev_groups;
  80. + else
  81. + nvmem->dev.groups = nvmem->read_only ?
  82. + nvmem_ro_dev_groups :
  83. + nvmem_rw_dev_groups;
  84. device_initialize(&nvmem->dev);
  85. --- a/include/linux/nvmem-provider.h
  86. +++ b/include/linux/nvmem-provider.h
  87. @@ -23,6 +23,7 @@ struct nvmem_config {
  88. const struct nvmem_cell_info *cells;
  89. int ncells;
  90. bool read_only;
  91. + bool root_only;
  92. };
  93. #if IS_ENABLED(CONFIG_NVMEM)