linux-stable/drivers/of
Miquel Raynal 161c8b6d85 of: Fix modalias string generation
[ Upstream commit b19a4266c5 ]

The helper generating an OF based modalias (of_device_get_modalias())
works fine, but due to the use of snprintf() internally it needs a
buffer one byte longer than what should be needed just for the entire
string (excluding the '\0'). Most users of this helper are sysfs hooks
providing the modalias string to users. They all provide a PAGE_SIZE
buffer which is way above the number of bytes required to fit the
modalias string and hence do not suffer from this issue.

There is another user though, of_device_request_module(), which is only
called by drivers/usb/common/ulpi.c. This request module function is
faulty, but maybe because in most cases there is an alternative, ULPI
driver users have not noticed it.

In this function, of_device_get_modalias() is called twice. The first
time without buffer just to get the number of bytes required by the
modalias string (excluding the null byte), and a second time, after
buffer allocation, to fill the buffer. The allocation asks for an
additional byte, in order to store the trailing '\0'. However, the
buffer *length* provided to of_device_get_modalias() excludes this extra
byte. The internal use of snprintf() with a length that is exactly the
number of bytes to be written has the effect of using the last available
byte to store a '\0', which then smashes the last character of the
modalias string.

Provide the actual size of the buffer to of_device_get_modalias() to fix
this issue.

Note: the "str[size - 1] = '\0';" line is not really needed as snprintf
will anyway end the string with a null byte, but there is a possibility
that this function might be called on a struct device_node without
compatible, in this case snprintf() would not be executed. So we keep it
just to avoid possible unbounded strings.

Cc: Stephen Boyd <sboyd@kernel.org>
Cc: Peter Chen <peter.chen@kernel.org>
Fixes: 9c829c097f ("of: device: Support loading a module with OF based modalias")
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20230404172148.82422-2-srinivas.kandagatla@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-05-11 23:11:21 +09:00
..
unittest-data of: overlay: rename overlay source files from .dts to .dtso 2022-10-26 09:02:28 -05:00
Kconfig
Makefile
address.c of/address: Return an error when no valid dma-ranges are found 2023-02-02 17:10:58 -06:00
base.c Devicetree updates for v6.1: 2022-10-10 13:13:51 -07:00
device.c of: Fix modalias string generation 2023-05-11 23:11:21 +09:00
dynamic.c
fdt.c Revert "mm: kmemleak: alloc gray object for reserved region with direct map" 2023-01-31 16:44:09 -08:00
fdt_address.c
irq.c of/irq: export of_msi_get_domain 2022-10-19 18:58:04 +05:30
kexec.c of/kexec: Fix reading 32-bit "linux,initrd-{start,end}" values 2022-12-06 14:50:36 -06:00
kobj.c
of_numa.c
of_private.h of: Fix "dma-ranges" handling for bus controllers 2022-09-30 14:35:43 -05:00
of_reserved_mem.c of: reserved_mem: Have kmemleak ignore dynamically allocated reserved mem 2023-02-09 15:56:51 -08:00
overlay.c of: overlay: fix null pointer dereferencing in find_dup_cset_node_entry() and find_dup_cset_prop() 2022-12-11 19:00:36 -06:00
pdt.c
platform.c of: Make OF framebuffer device names unique 2023-02-02 17:10:33 -06:00
property.c of: property: decrement node refcount in of_fwnode_get_reference_args() 2022-11-22 17:22:52 -06:00
resolver.c
unittest.c of: unittest: Convert to i2c's .probe_new() 2022-12-06 14:50:35 -06:00