From d29e6c2a62cfa53e131e4e669d7fa0492c200b04 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Wed, 23 Aug 2017 18:04:04 -0700 Subject: [PATCH] of/device: Prevent buffer overflow in of_device_modalias() commit 08ab58d9de3eb8498ae0585001d0975e46217a39 upstream. As of_device_get_modalias() returns the number of bytes that would have been written to the target string, regardless of how much did fit in the buffer, it's possible that the returned index points beyond the buffer passed to of_device_modalias() - causing memory beyond the buffer to be null terminated. Fixes: 0634c2958927 ("of: Add function for generating a DT modalias with a newline") Cc: Rob Herring Signed-off-by: Bjorn Andersson Signed-off-by: Rob Herring Signed-off-by: Greg Kroah-Hartman --- drivers/of/device.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/of/device.c b/drivers/of/device.c index e0a28ea341fe9..eabfa36383cee 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -274,6 +274,8 @@ ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len) ssize_t sl = of_device_get_modalias(dev, str, len - 2); if (sl < 0) return sl; + if (sl > len - 2) + return -ENOMEM; str[sl++] = '\n'; str[sl] = 0; -- 2.47.3