2014-06-11 06:07:54

by Dan Carpenter

[permalink] [raw]
Subject: [patch] mfd: vexpress: use after free in vexpress_syscfg_regmap_init()

We should return NULL if regmap_init() fails instead of continuing.

Signed-off-by: Dan Carpenter <[email protected]>

diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
index 73068e5..2c0ddb2 100644
--- a/drivers/misc/vexpress-syscfg.c
+++ b/drivers/misc/vexpress-syscfg.c
@@ -231,10 +231,12 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
func->regmap = regmap_init(dev, NULL, func,
&vexpress_syscfg_regmap_config);

- if (IS_ERR(func->regmap))
+ if (IS_ERR(func->regmap)) {
kfree(func);
- else
- list_add(&func->list, &syscfg->funcs);
+ return NULL;
+ }
+
+ list_add(&func->list, &syscfg->funcs);

return func->regmap;
}


2014-06-11 09:22:31

by Pawel Moll

[permalink] [raw]
Subject: Re: [patch] mfd: vexpress: use after free in vexpress_syscfg_regmap_init()

On Wed, 2014-06-11 at 07:07 +0100, Dan Carpenter wrote:
> We should return NULL if regmap_init() fails instead of continuing.
>
> Signed-off-by: Dan Carpenter <[email protected]>
>
> diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
> index 73068e5..2c0ddb2 100644
> --- a/drivers/misc/vexpress-syscfg.c
> +++ b/drivers/misc/vexpress-syscfg.c
> @@ -231,10 +231,12 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> func->regmap = regmap_init(dev, NULL, func,
> &vexpress_syscfg_regmap_config);
>
> - if (IS_ERR(func->regmap))
> + if (IS_ERR(func->regmap)) {
> kfree(func);
> - else
> - list_add(&func->list, &syscfg->funcs);
> + return NULL;
> + }
> +
> + list_add(&func->list, &syscfg->funcs);
>
> return func->regmap;
> }

Not really, no. What made you think so?

vexpress_config_bridge_ops.regmap_init should return an ERR_PTR in case
of troubles, not a NULL. See devm_regmap_init_vexpress_config() in
drivers/bus/vexpress-config.c:

regmap = bridge->ops->regmap_init(dev, bridge->context);
if (IS_ERR(regmap)) {

Pawel

2014-06-11 10:08:16

by Dan Carpenter

[permalink] [raw]
Subject: Re: [patch] mfd: vexpress: use after free in vexpress_syscfg_regmap_init()

On Wed, Jun 11, 2014 at 10:22:22AM +0100, Pawel Moll wrote:
> On Wed, 2014-06-11 at 07:07 +0100, Dan Carpenter wrote:
> > We should return NULL if regmap_init() fails instead of continuing.
> >
> > Signed-off-by: Dan Carpenter <[email protected]>
> >
> > diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
> > index 73068e5..2c0ddb2 100644
> > --- a/drivers/misc/vexpress-syscfg.c
> > +++ b/drivers/misc/vexpress-syscfg.c
> > @@ -231,10 +231,12 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> > func->regmap = regmap_init(dev, NULL, func,
> > &vexpress_syscfg_regmap_config);
> >
> > - if (IS_ERR(func->regmap))
> > + if (IS_ERR(func->regmap)) {
> > kfree(func);
> > - else
> > - list_add(&func->list, &syscfg->funcs);
> > + return NULL;
> > + }
> > +
> > + list_add(&func->list, &syscfg->funcs);
> >
> > return func->regmap;
> > }
>
> Not really, no. What made you think so?
>
> vexpress_config_bridge_ops.regmap_init should return an ERR_PTR in case
> of troubles, not a NULL. See devm_regmap_init_vexpress_config() in
> drivers/bus/vexpress-config.c:
>
> regmap = bridge->ops->regmap_init(dev, bridge->context);
> if (IS_ERR(regmap)) {

Ah... That was sloppy of me. There is a similar bug earlier and I'll
fix that as well.

regards,
dan carpenter

2014-06-11 10:17:57

by Dan Carpenter

[permalink] [raw]
Subject: [patch v2] mfd: vexpress: fix error handling vexpress_syscfg_regmap_init()

This function should be returning an ERR_PTR() on failure instead of
NULL. Also there is a use after free bug if regmap_init() fails because
we free "func" and then dereference doing the return.

Signed-off-by: Dan Carpenter <[email protected]>

diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
index 73068e5..3250fc1 100644
--- a/drivers/misc/vexpress-syscfg.c
+++ b/drivers/misc/vexpress-syscfg.c
@@ -199,7 +199,7 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
GFP_KERNEL);
if (!func)
- return NULL;
+ return ERR_PTR(-ENOMEM);

func->syscfg = syscfg;
func->num_templates = num;
@@ -231,10 +231,14 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
func->regmap = regmap_init(dev, NULL, func,
&vexpress_syscfg_regmap_config);

- if (IS_ERR(func->regmap))
+ if (IS_ERR(func->regmap)) {
+ void *err = func->regmap;
+
kfree(func);
- else
- list_add(&func->list, &syscfg->funcs);
+ return err;
+ }
+
+ list_add(&func->list, &syscfg->funcs);

return func->regmap;
}

2014-06-11 10:33:30

by Pawel Moll

[permalink] [raw]
Subject: Re: [patch v2] mfd: vexpress: fix error handling vexpress_syscfg_regmap_init()

On Wed, 2014-06-11 at 11:17 +0100, Dan Carpenter wrote:
> This function should be returning an ERR_PTR() on failure instead of
> NULL. Also there is a use after free bug if regmap_init() fails because
> we free "func" and then dereference doing the return.
>
> Signed-off-by: Dan Carpenter <[email protected]>
>
> diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
> index 73068e5..3250fc1 100644
> --- a/drivers/misc/vexpress-syscfg.c
> +++ b/drivers/misc/vexpress-syscfg.c
> @@ -199,7 +199,7 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
> GFP_KERNEL);
> if (!func)
> - return NULL;
> + return ERR_PTR(-ENOMEM);
>
> func->syscfg = syscfg;
> func->num_templates = num;
> @@ -231,10 +231,14 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> func->regmap = regmap_init(dev, NULL, func,
> &vexpress_syscfg_regmap_config);
>
> - if (IS_ERR(func->regmap))
> + if (IS_ERR(func->regmap)) {
> + void *err = func->regmap;
> +
> kfree(func);
> - else
> - list_add(&func->list, &syscfg->funcs);
> + return err;
> + }
> +
> + list_add(&func->list, &syscfg->funcs);
>
> return func->regmap;
> }

Uh, right. Dereferencing a freed structure. My bad. Thanks for spotting
this!

Acked-by: Pawel Moll <[email protected]>

(nit: the subject should be "misc: vexpress:" rather then "mfd:")

Arnd, Olof, can you pick this one as an early fix or do you want me to
queue it for rc1-based fixes branch?

Paweł

2014-06-11 17:08:44

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [patch v2] mfd: vexpress: fix error handling vexpress_syscfg_regmap_init()

On Wed, Jun 11, 2014 at 11:33:20AM +0100, Pawel Moll wrote:
> On Wed, 2014-06-11 at 11:17 +0100, Dan Carpenter wrote:
> > This function should be returning an ERR_PTR() on failure instead of
> > NULL. Also there is a use after free bug if regmap_init() fails because
> > we free "func" and then dereference doing the return.
> >
> > Signed-off-by: Dan Carpenter <[email protected]>
> >
> > diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
> > index 73068e5..3250fc1 100644
> > --- a/drivers/misc/vexpress-syscfg.c
> > +++ b/drivers/misc/vexpress-syscfg.c
> > @@ -199,7 +199,7 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> > func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
> > GFP_KERNEL);
> > if (!func)
> > - return NULL;
> > + return ERR_PTR(-ENOMEM);
> >
> > func->syscfg = syscfg;
> > func->num_templates = num;
> > @@ -231,10 +231,14 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> > func->regmap = regmap_init(dev, NULL, func,
> > &vexpress_syscfg_regmap_config);
> >
> > - if (IS_ERR(func->regmap))
> > + if (IS_ERR(func->regmap)) {
> > + void *err = func->regmap;
> > +
> > kfree(func);
> > - else
> > - list_add(&func->list, &syscfg->funcs);
> > + return err;
> > + }
> > +
> > + list_add(&func->list, &syscfg->funcs);
> >
> > return func->regmap;
> > }
>
> Uh, right. Dereferencing a freed structure. My bad. Thanks for spotting
> this!
>
> Acked-by: Pawel Moll <[email protected]>
>
> (nit: the subject should be "misc: vexpress:" rather then "mfd:")
>
> Arnd, Olof, can you pick this one as an early fix or do you want me to
> queue it for rc1-based fixes branch?
>

I can queue it up.

thanks,

greg k-h

2014-06-11 17:11:52

by Pawel Moll

[permalink] [raw]
Subject: Re: [patch v2] mfd: vexpress: fix error handling vexpress_syscfg_regmap_init()

On Wed, 2014-06-11 at 18:12 +0100, Greg Kroah-Hartman wrote:
> I can queue it up.

Cool, thanks!

Pawel

2014-06-13 14:03:15

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [patch v2] mfd: vexpress: fix error handling vexpress_syscfg_regmap_init()

On Wednesday 11 June 2014, Pawel Moll wrote:
> On Wed, 2014-06-11 at 11:17 +0100, Dan Carpenter wrote:
> > This function should be returning an ERR_PTR() on failure instead of
> > NULL. Also there is a use after free bug if regmap_init() fails because
> > we free "func" and then dereference doing the return.
> >
> > Signed-off-by: Dan Carpenter <[email protected]>
> >
> > diff --git a/drivers/misc/vexpress-syscfg.c b/drivers/misc/vexpress-syscfg.c
> > index 73068e5..3250fc1 100644
> > --- a/drivers/misc/vexpress-syscfg.c
> > +++ b/drivers/misc/vexpress-syscfg.c
> > @@ -199,7 +199,7 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> > func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
> > GFP_KERNEL);
> > if (!func)
> > - return NULL;
> > + return ERR_PTR(-ENOMEM);
> >
> > func->syscfg = syscfg;
> > func->num_templates = num;
> > @@ -231,10 +231,14 @@ static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
> > func->regmap = regmap_init(dev, NULL, func,
> > &vexpress_syscfg_regmap_config);
> >
> > - if (IS_ERR(func->regmap))
> > + if (IS_ERR(func->regmap)) {
> > + void *err = func->regmap;
> > +
> > kfree(func);
> > - else
> > - list_add(&func->list, &syscfg->funcs);
> > + return err;
> > + }
> > +
> > + list_add(&func->list, &syscfg->funcs);
> >
> > return func->regmap;
> > }
>
> Uh, right. Dereferencing a freed structure. My bad. Thanks for spotting
> this!
>
> Acked-by: Pawel Moll <[email protected]>
>
> (nit: the subject should be "misc: vexpress:" rather then "mfd:")
>
> Arnd, Olof, can you pick this one as an early fix or do you want me to
> queue it for rc1-based fixes branch?

I've applied it to the fixes branch now. Thanks!

Arnd