* [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform
@ 2021-04-14 15:12 Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 2/3] mmc: sdhci-s3c: correct kerneldoc of sdhci_s3c_drv_data Krzysztof Kozlowski
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2021-04-14 15:12 UTC (permalink / raw)
To: Ben Dooks, Jaehoon Chung, Adrian Hunter, Ulf Hansson, linux-mmc,
linux-kernel
Cc: Marek Szyprowski, Sylwester Nawrocki, Krzysztof Kozlowski
The driver can be matched by legacy platform way or OF-device matching.
In the first case, of_match_node() can return NULL, which immediately
would be dereferenced to get the match data.
Addresses-Coverity: Dereference null return value
Fixes: cd1b00eb24b0 ("mmc: sdhci-s3c: Add device tree support")
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
---
Not tested on HW, but the code should be equivalent (plus safer).
drivers/mmc/host/sdhci-s3c.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
diff --git a/drivers/mmc/host/sdhci-s3c.c b/drivers/mmc/host/sdhci-s3c.c
index f48a788a9d3d..8e1dca625620 100644
--- a/drivers/mmc/host/sdhci-s3c.c
+++ b/drivers/mmc/host/sdhci-s3c.c
@@ -20,6 +20,7 @@
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/of.h>
+#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
@@ -461,19 +462,12 @@ static int sdhci_s3c_parse_dt(struct device *dev,
}
#endif
-#ifdef CONFIG_OF
-static const struct of_device_id sdhci_s3c_dt_match[];
-#endif
-
static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
struct platform_device *pdev)
{
#ifdef CONFIG_OF
- if (pdev->dev.of_node) {
- const struct of_device_id *match;
- match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
- return (struct sdhci_s3c_drv_data *)match->data;
- }
+ if (pdev->dev.of_node)
+ return (struct sdhci_s3c_drv_data *)of_device_get_match_data(&pdev->dev);
#endif
return (struct sdhci_s3c_drv_data *)
platform_get_device_id(pdev)->driver_data;
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/3] mmc: sdhci-s3c: correct kerneldoc of sdhci_s3c_drv_data
2021-04-14 15:12 [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
@ 2021-04-14 15:12 ` Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 3/3] mmc: sdhci-s3c: constify uses of driver/match data Krzysztof Kozlowski
2021-04-14 15:25 ` [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
2 siblings, 0 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2021-04-14 15:12 UTC (permalink / raw)
To: Ben Dooks, Jaehoon Chung, Adrian Hunter, Ulf Hansson, linux-mmc,
linux-kernel
Cc: Marek Szyprowski, Sylwester Nawrocki, Krzysztof Kozlowski
Correct the name of sdhci_s3c_drv_data structure in kerneldoc:
drivers/mmc/host/sdhci-s3c.c:143: warning:
expecting prototype for struct sdhci_s3c_driver_data. Prototype was for struct sdhci_s3c_drv_data instead
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
---
drivers/mmc/host/sdhci-s3c.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/mmc/host/sdhci-s3c.c b/drivers/mmc/host/sdhci-s3c.c
index 8e1dca625620..a07a8f011741 100644
--- a/drivers/mmc/host/sdhci-s3c.c
+++ b/drivers/mmc/host/sdhci-s3c.c
@@ -130,7 +130,7 @@ struct sdhci_s3c {
};
/**
- * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
+ * struct sdhci_s3c_drv_data - S3C SDHCI platform specific driver data
* @sdhci_quirks: sdhci host specific quirks.
* @no_divider: no or non-standard internal clock divider.
*
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/3] mmc: sdhci-s3c: constify uses of driver/match data
2021-04-14 15:12 [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 2/3] mmc: sdhci-s3c: correct kerneldoc of sdhci_s3c_drv_data Krzysztof Kozlowski
@ 2021-04-14 15:12 ` Krzysztof Kozlowski
2021-04-14 15:25 ` [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
2 siblings, 0 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2021-04-14 15:12 UTC (permalink / raw)
To: Ben Dooks, Jaehoon Chung, Adrian Hunter, Ulf Hansson, linux-mmc,
linux-kernel
Cc: Marek Szyprowski, Sylwester Nawrocki, Krzysztof Kozlowski
The driver data (struct sdhci_s3c_drv_data) stored in of_device_id
table is allocated as const and used only in const-way. Skip
unnecessary const-away casts and convert all users to work with pointer
to const. This is both more logical and safer.
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
---
drivers/mmc/host/sdhci-s3c.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/drivers/mmc/host/sdhci-s3c.c b/drivers/mmc/host/sdhci-s3c.c
index a07a8f011741..862f033d235d 100644
--- a/drivers/mmc/host/sdhci-s3c.c
+++ b/drivers/mmc/host/sdhci-s3c.c
@@ -462,21 +462,21 @@ static int sdhci_s3c_parse_dt(struct device *dev,
}
#endif
-static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
+static inline const struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
struct platform_device *pdev)
{
#ifdef CONFIG_OF
if (pdev->dev.of_node)
- return (struct sdhci_s3c_drv_data *)of_device_get_match_data(&pdev->dev);
+ return of_device_get_match_data(&pdev->dev);
#endif
- return (struct sdhci_s3c_drv_data *)
+ return (const struct sdhci_s3c_drv_data *)
platform_get_device_id(pdev)->driver_data;
}
static int sdhci_s3c_probe(struct platform_device *pdev)
{
struct s3c_sdhci_platdata *pdata;
- struct sdhci_s3c_drv_data *drv_data;
+ const struct sdhci_s3c_drv_data *drv_data;
struct device *dev = &pdev->dev;
struct sdhci_host *host;
struct sdhci_s3c *sc;
@@ -761,7 +761,7 @@ static const struct platform_device_id sdhci_s3c_driver_ids[] = {
MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
#ifdef CONFIG_OF
-static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
+static const struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
.no_divider = true,
};
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform
2021-04-14 15:12 [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 2/3] mmc: sdhci-s3c: correct kerneldoc of sdhci_s3c_drv_data Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 3/3] mmc: sdhci-s3c: constify uses of driver/match data Krzysztof Kozlowski
@ 2021-04-14 15:25 ` Krzysztof Kozlowski
2021-04-14 16:49 ` Sylwester Nawrocki
2 siblings, 1 reply; 6+ messages in thread
From: Krzysztof Kozlowski @ 2021-04-14 15:25 UTC (permalink / raw)
To: Ben Dooks, Jaehoon Chung, Adrian Hunter, Ulf Hansson, linux-mmc,
linux-kernel
Cc: Marek Szyprowski, Sylwester Nawrocki
On 14/04/2021 17:12, Krzysztof Kozlowski wrote:
> The driver can be matched by legacy platform way or OF-device matching.
> In the first case, of_match_node() can return NULL, which immediately
> would be dereferenced to get the match data.
>
> Addresses-Coverity: Dereference null return value
> Fixes: cd1b00eb24b0 ("mmc: sdhci-s3c: Add device tree support")
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
> ---
>
> Not tested on HW, but the code should be equivalent (plus safer).
>
> drivers/mmc/host/sdhci-s3c.c | 12 +++---------
> 1 file changed, 3 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/mmc/host/sdhci-s3c.c b/drivers/mmc/host/sdhci-s3c.c
> index f48a788a9d3d..8e1dca625620 100644
> --- a/drivers/mmc/host/sdhci-s3c.c
> +++ b/drivers/mmc/host/sdhci-s3c.c
> @@ -20,6 +20,7 @@
> #include <linux/gpio.h>
> #include <linux/module.h>
> #include <linux/of.h>
> +#include <linux/of_device.h>
> #include <linux/of_gpio.h>
> #include <linux/pm.h>
> #include <linux/pm_runtime.h>
> @@ -461,19 +462,12 @@ static int sdhci_s3c_parse_dt(struct device *dev,
> }
> #endif
>
> -#ifdef CONFIG_OF
> -static const struct of_device_id sdhci_s3c_dt_match[];
> -#endif
> -
> static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
> struct platform_device *pdev)
> {
> #ifdef CONFIG_OF
> - if (pdev->dev.of_node) {
> - const struct of_device_id *match;
> - match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
Now I have second thoughts whether NULL pointer can actually happen. If
device is matched via platform/board files, maybe the pdev->dev.of_node
will be NULL thus skipping this branch?
Could there be a case where device is matched via platform_device_id()
(which has different name than compatible!) and (pdev->dev.of_node) is
still assigned? Maybe in case of out of tree DTS?
Anyway, the patch makes the code simpler/smaller, so I still think it is
reasonable. Just the severity of issue is questionable...
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform
2021-04-14 15:25 ` [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
@ 2021-04-14 16:49 ` Sylwester Nawrocki
2021-04-15 8:41 ` Krzysztof Kozlowski
0 siblings, 1 reply; 6+ messages in thread
From: Sylwester Nawrocki @ 2021-04-14 16:49 UTC (permalink / raw)
To: Krzysztof Kozlowski, Ben Dooks, Jaehoon Chung, Adrian Hunter,
Ulf Hansson, linux-mmc, linux-kernel
Cc: Marek Szyprowski
On 14.04.2021 17:25, Krzysztof Kozlowski wrote:
> On 14/04/2021 17:12, Krzysztof Kozlowski wrote:
>> The driver can be matched by legacy platform way or OF-device matching.
>> In the first case, of_match_node() can return NULL, which immediately
>> would be dereferenced to get the match data.
>>
>> Addresses-Coverity: Dereference null return value
>> Fixes: cd1b00eb24b0 ("mmc: sdhci-s3c: Add device tree support")
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
>> -#ifdef CONFIG_OF
>> -static const struct of_device_id sdhci_s3c_dt_match[];
>> -#endif
>> -
>> static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
>> struct platform_device *pdev)
>> {
>> #ifdef CONFIG_OF
>> - if (pdev->dev.of_node) {
>> - const struct of_device_id *match;
>> - match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
>
> Now I have second thoughts whether NULL pointer can actually happen. If
> device is matched via platform/board files, maybe the pdev->dev.of_node
> will be NULL thus skipping this branch?
>
> Could there be a case where device is matched via platform_device_id()
> (which has different name than compatible!) and (pdev->dev.of_node) is
> still assigned? Maybe in case of out of tree DTS?
That seems unlikely, the platform device would need to be initialized
via board file and then its of_node assigned somehow. It doesn't make
much sense to me. We either use board file or dtb to instantiate devices.
> Anyway, the patch makes the code simpler/smaller, so I still think it is
> reasonable. Just the severity of issue is questionable...
Yes, the patch looks good. Similar cleanups are already done in most of
the s3c/s5p/exynos drivers.
--
Thanks,
Sylwester
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform
2021-04-14 16:49 ` Sylwester Nawrocki
@ 2021-04-15 8:41 ` Krzysztof Kozlowski
0 siblings, 0 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2021-04-15 8:41 UTC (permalink / raw)
To: Sylwester Nawrocki, Ben Dooks, Jaehoon Chung, Adrian Hunter,
Ulf Hansson, linux-mmc, linux-kernel
Cc: Marek Szyprowski
On 14/04/2021 18:49, Sylwester Nawrocki wrote:
> On 14.04.2021 17:25, Krzysztof Kozlowski wrote:
>> On 14/04/2021 17:12, Krzysztof Kozlowski wrote:
>>> The driver can be matched by legacy platform way or OF-device matching.
>>> In the first case, of_match_node() can return NULL, which immediately
>>> would be dereferenced to get the match data.
>>>
>>> Addresses-Coverity: Dereference null return value
>>> Fixes: cd1b00eb24b0 ("mmc: sdhci-s3c: Add device tree support")
>>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
>
>>> -#ifdef CONFIG_OF
>>> -static const struct of_device_id sdhci_s3c_dt_match[];
>>> -#endif
>>> -
>>> static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
>>> struct platform_device *pdev)
>>> {
>>> #ifdef CONFIG_OF
>>> - if (pdev->dev.of_node) {
>>> - const struct of_device_id *match;
>>> - match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
>>
>> Now I have second thoughts whether NULL pointer can actually happen. If
>> device is matched via platform/board files, maybe the pdev->dev.of_node
>> will be NULL thus skipping this branch?
>>
>> Could there be a case where device is matched via platform_device_id()
>> (which has different name than compatible!) and (pdev->dev.of_node) is
>> still assigned? Maybe in case of out of tree DTS?
>
> That seems unlikely, the platform device would need to be initialized
> via board file and then its of_node assigned somehow. It doesn't make
> much sense to me. We either use board file or dtb to instantiate devices.
>
>> Anyway, the patch makes the code simpler/smaller, so I still think it is
>> reasonable. Just the severity of issue is questionable...
>
> Yes, the patch looks good. Similar cleanups are already done in most of
> the s3c/s5p/exynos drivers.
Thanks Sylwester, I'll resend with different description.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-04-15 8:41 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14 15:12 [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 2/3] mmc: sdhci-s3c: correct kerneldoc of sdhci_s3c_drv_data Krzysztof Kozlowski
2021-04-14 15:12 ` [PATCH 3/3] mmc: sdhci-s3c: constify uses of driver/match data Krzysztof Kozlowski
2021-04-14 15:25 ` [PATCH 1/3] mmc: sdhci-s3c: fix possible NULL pointer dereference when probed via platform Krzysztof Kozlowski
2021-04-14 16:49 ` Sylwester Nawrocki
2021-04-15 8:41 ` Krzysztof Kozlowski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).