pinctrl: axp209: Fix NULL pointer dereference after allocation
authorAnton Vasilyev <vasilyev@ispras.ru>
Mon, 6 Aug 2018 16:06:35 +0000 (19:06 +0300)
committerLinus Walleij <linus.walleij@linaro.org>
Fri, 10 Aug 2018 21:12:43 +0000 (23:12 +0200)
There is no check that allocation in axp20x_funcs_groups_from_mask
is successful.
The patch adds corresponding check and return values.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Anton Vasilyev <vasilyev@ispras.ru>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/pinctrl/pinctrl-axp209.c

index a52779f33ad4a466978132bb0e6906188cc4b8bd..afd0b533c40aa733ca20b0b9011b768bcd365b47 100644 (file)
@@ -316,7 +316,7 @@ static const struct pinctrl_ops axp20x_pctrl_ops = {
        .get_group_pins         = axp20x_group_pins,
 };
 
-static void axp20x_funcs_groups_from_mask(struct device *dev, unsigned int mask,
+static int axp20x_funcs_groups_from_mask(struct device *dev, unsigned int mask,
                                          unsigned int mask_len,
                                          struct axp20x_pinctrl_function *func,
                                          const struct pinctrl_pin_desc *pins)
@@ -331,18 +331,22 @@ static void axp20x_funcs_groups_from_mask(struct device *dev, unsigned int mask,
                func->groups = devm_kcalloc(dev,
                                            ngroups, sizeof(const char *),
                                            GFP_KERNEL);
+               if (!func->groups)
+                       return -ENOMEM;
                group = func->groups;
                for_each_set_bit(bit, &mask_cpy, mask_len) {
                        *group = pins[bit].name;
                        group++;
                }
        }
+
+       return 0;
 }
 
-static void axp20x_build_funcs_groups(struct platform_device *pdev)
+static int axp20x_build_funcs_groups(struct platform_device *pdev)
 {
        struct axp20x_pctl *pctl = platform_get_drvdata(pdev);
-       int i, pin, npins = pctl->desc->npins;
+       int i, ret, pin, npins = pctl->desc->npins;
 
        pctl->funcs[AXP20X_FUNC_GPIO_OUT].name = "gpio_out";
        pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval = AXP20X_MUX_GPIO_OUT;
@@ -366,13 +370,19 @@ static void axp20x_build_funcs_groups(struct platform_device *pdev)
                        pctl->funcs[i].groups[pin] = pctl->desc->pins[pin].name;
        }
 
-       axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->ldo_mask,
+       ret = axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->ldo_mask,
                                      npins, &pctl->funcs[AXP20X_FUNC_LDO],
                                      pctl->desc->pins);
+       if (ret)
+               return ret;
 
-       axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->adc_mask,
+       ret = axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->adc_mask,
                                      npins, &pctl->funcs[AXP20X_FUNC_ADC],
                                      pctl->desc->pins);
+       if (ret)
+               return ret;
+
+       return 0;
 }
 
 static const struct of_device_id axp20x_pctl_match[] = {
@@ -424,7 +434,11 @@ static int axp20x_pctl_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, pctl);
 
-       axp20x_build_funcs_groups(pdev);
+       ret = axp20x_build_funcs_groups(pdev);
+       if (ret) {
+               dev_err(&pdev->dev, "failed to build groups\n");
+               return ret;
+       }
 
        pctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctrl_desc), GFP_KERNEL);
        if (!pctrl_desc)