828-v6.4-0001-of-Fix-modalias-string-generation.patch 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. From b19a4266c52de78496fe40f0b37580a3b762e67d Mon Sep 17 00:00:00 2001
  2. From: Miquel Raynal <[email protected]>
  3. Date: Tue, 4 Apr 2023 18:21:09 +0100
  4. Subject: [PATCH] of: Fix modalias string generation
  5. The helper generating an OF based modalias (of_device_get_modalias())
  6. works fine, but due to the use of snprintf() internally it needs a
  7. buffer one byte longer than what should be needed just for the entire
  8. string (excluding the '\0'). Most users of this helper are sysfs hooks
  9. providing the modalias string to users. They all provide a PAGE_SIZE
  10. buffer which is way above the number of bytes required to fit the
  11. modalias string and hence do not suffer from this issue.
  12. There is another user though, of_device_request_module(), which is only
  13. called by drivers/usb/common/ulpi.c. This request module function is
  14. faulty, but maybe because in most cases there is an alternative, ULPI
  15. driver users have not noticed it.
  16. In this function, of_device_get_modalias() is called twice. The first
  17. time without buffer just to get the number of bytes required by the
  18. modalias string (excluding the null byte), and a second time, after
  19. buffer allocation, to fill the buffer. The allocation asks for an
  20. additional byte, in order to store the trailing '\0'. However, the
  21. buffer *length* provided to of_device_get_modalias() excludes this extra
  22. byte. The internal use of snprintf() with a length that is exactly the
  23. number of bytes to be written has the effect of using the last available
  24. byte to store a '\0', which then smashes the last character of the
  25. modalias string.
  26. Provide the actual size of the buffer to of_device_get_modalias() to fix
  27. this issue.
  28. Note: the "str[size - 1] = '\0';" line is not really needed as snprintf
  29. will anyway end the string with a null byte, but there is a possibility
  30. that this function might be called on a struct device_node without
  31. compatible, in this case snprintf() would not be executed. So we keep it
  32. just to avoid possible unbounded strings.
  33. Cc: Stephen Boyd <[email protected]>
  34. Cc: Peter Chen <[email protected]>
  35. Fixes: 9c829c097f2f ("of: device: Support loading a module with OF based modalias")
  36. Signed-off-by: Miquel Raynal <[email protected]>
  37. Reviewed-by: Rob Herring <[email protected]>
  38. Signed-off-by: Srinivas Kandagatla <[email protected]>
  39. Link: https://lore.kernel.org/r/[email protected]
  40. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  41. ---
  42. drivers/of/device.c | 7 +++++--
  43. 1 file changed, 5 insertions(+), 2 deletions(-)
  44. --- a/drivers/of/device.c
  45. +++ b/drivers/of/device.c
  46. @@ -290,12 +290,15 @@ int of_device_request_module(struct devi
  47. if (size < 0)
  48. return size;
  49. - str = kmalloc(size + 1, GFP_KERNEL);
  50. + /* Reserve an additional byte for the trailing '\0' */
  51. + size++;
  52. +
  53. + str = kmalloc(size, GFP_KERNEL);
  54. if (!str)
  55. return -ENOMEM;
  56. of_device_get_modalias(dev, str, size);
  57. - str[size] = '\0';
  58. + str[size - 1] = '\0';
  59. ret = request_module(str);
  60. kfree(str);