Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] shared/gatt: Fix freeing uninitialized attributes
@ 2014-05-16 10:07 Marcin Kraglak
  2014-05-16 12:36 ` Szymon Janc
  2014-05-16 15:17 ` Marcel Holtmann
  0 siblings, 2 replies; 4+ messages in thread
From: Marcin Kraglak @ 2014-05-16 10:07 UTC (permalink / raw)
  To: linux-bluetooth

Don't free uninitialized attributes. It could cause NULL pointer
dereference.
---
 src/shared/gatt-db.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index d9f63be..36316af 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -99,6 +99,10 @@ static void attribute_destroy(void *data)
 {
 	struct gatt_db_attribute *attribute = data;
 
+	/* Attribute was not initialized by user */
+	if (!attribute)
+		return;
+
 	free(attribute->value);
 	free(attribute);
 }
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] shared/gatt: Fix freeing uninitialized attributes
  2014-05-16 10:07 [PATCH] shared/gatt: Fix freeing uninitialized attributes Marcin Kraglak
@ 2014-05-16 12:36 ` Szymon Janc
  2014-05-16 15:17 ` Marcel Holtmann
  1 sibling, 0 replies; 4+ messages in thread
From: Szymon Janc @ 2014-05-16 12:36 UTC (permalink / raw)
  To: Marcin Kraglak; +Cc: linux-bluetooth

Hi Marcin,

On Friday 16 of May 2014 12:07:07 Marcin Kraglak wrote:
> Don't free uninitialized attributes. It could cause NULL pointer
> dereference.
> ---
>  src/shared/gatt-db.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> index d9f63be..36316af 100644
> --- a/src/shared/gatt-db.c
> +++ b/src/shared/gatt-db.c
> @@ -99,6 +99,10 @@ static void attribute_destroy(void *data)
>  {
>  	struct gatt_db_attribute *attribute = data;
>  
> +	/* Attribute was not initialized by user */
> +	if (!attribute)
> +		return;
> +
>  	free(attribute->value);
>  	free(attribute);
>  }
> 

Applied, thanks.

-- 
Best regards, 
Szymon Janc

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] shared/gatt: Fix freeing uninitialized attributes
  2014-05-16 10:07 [PATCH] shared/gatt: Fix freeing uninitialized attributes Marcin Kraglak
  2014-05-16 12:36 ` Szymon Janc
@ 2014-05-16 15:17 ` Marcel Holtmann
  2014-05-16 16:24   ` Szymon Janc
  1 sibling, 1 reply; 4+ messages in thread
From: Marcel Holtmann @ 2014-05-16 15:17 UTC (permalink / raw)
  To: Marcin Kraglak; +Cc: linux-bluetooth

Hi Marcin,

> Don't free uninitialized attributes. It could cause NULL pointer
> dereference.
> ---
> src/shared/gatt-db.c | 4 ++++
> 1 file changed, 4 insertions(+)
> 
> diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> index d9f63be..36316af 100644
> --- a/src/shared/gatt-db.c
> +++ b/src/shared/gatt-db.c
> @@ -99,6 +99,10 @@ static void attribute_destroy(void *data)
> {
> 	struct gatt_db_attribute *attribute = data;
> 
> +	/* Attribute was not initialized by user */
> +	if (!attribute)
> +		return;
> +
> 	free(attribute->value);
> 	free(attribute);

I think the real question is why a function with void *data is called with a NULL pointer in the first place. That is the thing you should fix and not hack around this by doing a NULL check. So this is clearly a function that you gave to callback somehow, fix that part. I do not remember any of the code in src/shared/ that has the destroy callbacks requiring any of this. We are expecting that data is valid.

Regards

Marcel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] shared/gatt: Fix freeing uninitialized attributes
  2014-05-16 15:17 ` Marcel Holtmann
@ 2014-05-16 16:24   ` Szymon Janc
  0 siblings, 0 replies; 4+ messages in thread
From: Szymon Janc @ 2014-05-16 16:24 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: Marcin Kraglak, linux-bluetooth

Hi Marcel,

On Friday 16 May 2014 08:17:25 Marcel Holtmann wrote:
> Hi Marcin,
> 
> > Don't free uninitialized attributes. It could cause NULL pointer
> > dereference.
> > ---
> > src/shared/gatt-db.c | 4 ++++
> > 1 file changed, 4 insertions(+)
> > 
> > diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> > index d9f63be..36316af 100644
> > --- a/src/shared/gatt-db.c
> > +++ b/src/shared/gatt-db.c
> > @@ -99,6 +99,10 @@ static void attribute_destroy(void *data)
> > {
> > 
> > 	struct gatt_db_attribute *attribute = data;
> > 
> > +	/* Attribute was not initialized by user */
> > +	if (!attribute)
> > +		return;
> > +
> > 
> > 	free(attribute->value);
> > 	free(attribute);
> 
> I think the real question is why a function with void *data is called with a
> NULL pointer in the first place. That is the thing you should fix and not
> hack around this by doing a NULL check. So this is clearly a function that
> you gave to callback somehow, fix that part. I do not remember any of the
> code in src/shared/ that has the destroy callbacks requiring any of this.
> We are expecting that data is valid.

This is due to attributes[] in struct gatt_db_service might not be fully used 
by user (we preallocate handles). But since attribute_destroy() is used only 
from gatt_db_service_destroy() maybe it would be better to check if pointer is 
not NULL there since checking this inside gatt_db_service_destroy() would be 
easier to follow.

Something like this maybe: ?

-static void attribute_destroy(void *data)
+static void attribute_destroy(struct gatt_db_attribute *attribute)
 {
-       struct gatt_db_attribute *attribute = data;
-
-       /* Attribute was not initialized by user */
-       if (!attribute)
-               return;
-
        free(attribute->value);
        free(attribute);
 }
@@ -131,8 +125,13 @@ static void gatt_db_service_destroy(void *data)
        struct gatt_db_service *service = data;
        int i;
 
-       for (i = 0; i < service->num_handles; i++)
+       for (i = 0; i < service->num_handles; i++) {
+               /* Attribute was not initialized by user */
+               if (!service->attributes[i])
+                       continue;
+
                attribute_destroy(service->attributes[i]);
+       }




-- 
Szymon K. Janc
szymon.janc@gmail.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-05-16 16:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-16 10:07 [PATCH] shared/gatt: Fix freeing uninitialized attributes Marcin Kraglak
2014-05-16 12:36 ` Szymon Janc
2014-05-16 15:17 ` Marcel Holtmann
2014-05-16 16:24   ` Szymon Janc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox