net: mdio-ipq4019: fix possible invalid pointer dereference

Message ID 20221115045028.182441-1-tanghui20@huawei.com
State New
Headers
Series net: mdio-ipq4019: fix possible invalid pointer dereference |

Commit Message

Hui Tang Nov. 15, 2022, 4:50 a.m. UTC
  priv->eth_ldo_rdy is saved the return value of devm_ioremap_resource(),
which !IS_ERR() should be used to check.

Fixes: 23a890d493e3 ("net: mdio: Add the reset function for IPQ MDIO driver")
Signed-off-by: Hui Tang <tanghui20@huawei.com>
---
 drivers/net/mdio/mdio-ipq4019.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Andrew Lunn Nov. 15, 2022, 1:36 p.m. UTC | #1
On Tue, Nov 15, 2022 at 12:50:28PM +0800, Hui Tang wrote:
> priv->eth_ldo_rdy is saved the return value of devm_ioremap_resource(),
> which !IS_ERR() should be used to check.
> 
> Fixes: 23a890d493e3 ("net: mdio: Add the reset function for IPQ MDIO driver")
> Signed-off-by: Hui Tang <tanghui20@huawei.com>
> ---
>  drivers/net/mdio/mdio-ipq4019.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/mdio/mdio-ipq4019.c b/drivers/net/mdio/mdio-ipq4019.c
> index 4eba5a91075c..d7a1f7c56f97 100644
> --- a/drivers/net/mdio/mdio-ipq4019.c
> +++ b/drivers/net/mdio/mdio-ipq4019.c
> @@ -188,7 +188,7 @@ static int ipq_mdio_reset(struct mii_bus *bus)
>  	/* To indicate CMN_PLL that ethernet_ldo has been ready if platform resource 1
>  	 * is specified in the device tree.
>  	 */
> -	if (priv->eth_ldo_rdy) {
> +	if (!IS_ERR(priv->eth_ldo_rdy)) {
>  		val = readl(priv->eth_ldo_rdy);
>  		val |= BIT(0);
>  		writel(val, priv->eth_ldo_rdy);

There is a general pattern in the kernel that optional things are set
to NULL if the resource does not exist. Often there is a
get_foo_optional() which will return a NULL point if the things does
not exist, the thing if it does exist, or an error code if a real
error happened.

So please follow this patterns. Check the return value in
ipq4019_mdio_probe(). Looking at __devm_ioremap_resource() i _think_
it returns -ENOMEM if the resource does not exist? So maybe any other
error is a real error, and should be reported, and -ENOMEM should
result in eth_ldo_rdy set to NULL?

       Andrew
  
Hui Tang Nov. 17, 2022, 8:50 a.m. UTC | #2
On 2022/11/15 21:36, Andrew Lunn wrote:
> On Tue, Nov 15, 2022 at 12:50:28PM +0800, Hui Tang wrote:
>> priv->eth_ldo_rdy is saved the return value of devm_ioremap_resource(),
>> which !IS_ERR() should be used to check.
>>
>> Fixes: 23a890d493e3 ("net: mdio: Add the reset function for IPQ MDIO driver")
>> Signed-off-by: Hui Tang <tanghui20@huawei.com>
>> ---
>>  drivers/net/mdio/mdio-ipq4019.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/mdio/mdio-ipq4019.c b/drivers/net/mdio/mdio-ipq4019.c
>> index 4eba5a91075c..d7a1f7c56f97 100644
>> --- a/drivers/net/mdio/mdio-ipq4019.c
>> +++ b/drivers/net/mdio/mdio-ipq4019.c
>> @@ -188,7 +188,7 @@ static int ipq_mdio_reset(struct mii_bus *bus)
>>  	/* To indicate CMN_PLL that ethernet_ldo has been ready if platform resource 1
>>  	 * is specified in the device tree.
>>  	 */
>> -	if (priv->eth_ldo_rdy) {
>> +	if (!IS_ERR(priv->eth_ldo_rdy)) {
>>  		val = readl(priv->eth_ldo_rdy);
>>  		val |= BIT(0);
>>  		writel(val, priv->eth_ldo_rdy);
>
> There is a general pattern in the kernel that optional things are set
> to NULL if the resource does not exist. Often there is a
> get_foo_optional() which will return a NULL point if the things does
> not exist, the thing if it does exist, or an error code if a real
> error happened.
>
> So please follow this patterns. Check the return value in
> ipq4019_mdio_probe(). Looking at __devm_ioremap_resource() i _think_
> it returns -ENOMEM if the resource does not exist? So maybe any other
> error is a real error, and should be reported, and -ENOMEM should
> result in eth_ldo_rdy set to NULL?

Thanks, I will resend it according to the style you said.
  

Patch

diff --git a/drivers/net/mdio/mdio-ipq4019.c b/drivers/net/mdio/mdio-ipq4019.c
index 4eba5a91075c..d7a1f7c56f97 100644
--- a/drivers/net/mdio/mdio-ipq4019.c
+++ b/drivers/net/mdio/mdio-ipq4019.c
@@ -188,7 +188,7 @@  static int ipq_mdio_reset(struct mii_bus *bus)
 	/* To indicate CMN_PLL that ethernet_ldo has been ready if platform resource 1
 	 * is specified in the device tree.
 	 */
-	if (priv->eth_ldo_rdy) {
+	if (!IS_ERR(priv->eth_ldo_rdy)) {
 		val = readl(priv->eth_ldo_rdy);
 		val |= BIT(0);
 		writel(val, priv->eth_ldo_rdy);