Skip to contentSkip to conversation

pstore: fix NULL pointer fault if get NULL prz in ramoops_get_next_prz

PATCH In review Liu ShuoX 2 participants 6 messages Mar 11, 2014 – Mar 12, 2014 Pstore filesystem

lore
Share & tools
lore

Liu ShuoX

author
#0 lore raw

PATCH 1/2 pstore: fix NULL pointer fault if get NULL prz in ramoops_get_next_prz

These two patches are applied on top of patches:
https://lkml.org/lkml/2014/3/3/368
It has been added in -mm tree. Below is the first patch, and i will send the second by replying this one.

From: Liu ShuoX <[email protected]>

ramoops_get_next_prz get the prz according the paramters. If it get a uninitialized prz, access its members by following persistent_ram_old_size(prz) will cause a NULL pointer crash.
Ex: if ftrace_size is 0, fprz will be NULL.

Fix it by return NULL in advance.

---
 fs/pstore/ram.c | 2 ++
 1 file changed, 2 insertions(+)
fs/pstore/ram.c +2 -0
index 1daed28..6f96d8c 100644
@@ -119,6 +119,8 @@ ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
 		return NULL;
 
 	prz = przs[i];
+	if (!prz)
+		return NULL;
 
 	/* Update old/shadowed buffer. */
 	if (update)
Signature · 1 line
1.8.3.2
author
#1 lore raw

PATCH 2/2 pstore: correct the max_dump_cnt clearing of ramoops

From: Liu ShuoX <[email protected]>

In case that ramoops_init_przs failed, max_dump_cnt won't be reset to zero in error handle path.

---
 fs/pstore/ram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +2 -2
index 6f96d8c..522e530 100644
@@ -326,6 +326,7 @@ static void ramoops_free_przs(struct ramoops_context *cxt)
 	for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
 		persistent_ram_free(cxt->przs[i]);
 	kfree(cxt->przs);
+	cxt->max_dump_cnt = 0;
 }
 
 static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
@@ -350,7 +351,7 @@ static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
 			     GFP_KERNEL);
 	if (!cxt->przs) {
 		dev_err(dev, "failed to initialize a prz array for dumps\n");
-		return -ENOMEM;
+		goto fail_prz;
 	}
 
 	for (i = 0; i < cxt->max_dump_cnt; i++) {
@@ -508,7 +509,6 @@ fail_buf:
 	kfree(cxt->pstore.buf);
 fail_clear:
 	cxt->pstore.bufsize = 0;
-	cxt->max_dump_cnt = 0;
 fail_cnt:
 	kfree(cxt->fprz);
 fail_init_fprz:
Signature · 1 line
1.8.3.2
#2 lore raw

Re: [PATCH 2/2] pstore: correct the max_dump_cnt clearing of ramoops

Quoted 24 lines · Liu ShuoX

From: Liu ShuoX <[email protected]>

In case that ramoops_init_przs failed, max_dump_cnt won't be reset to zero in error handle path.

---
 fs/pstore/ram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +1 -0
index 6f96d8c..522e530 100644
@@ -326,6 +326,7 @@ static void ramoops_free_przs(struct ramoops_context
 *cxt)
        for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
                persistent_ram_free(cxt->przs[i]);
        kfree(cxt->przs);
+       cxt->max_dump_cnt = 0;
 }
  static int ramoops_init_przs(struct device *dev, struct ramoops_context

*cxt,

fs/pstore/ram.c static int ramoops_init_przs(struct device *dev, struct · line 356

Quoted 7 lines
fs/pstore/ram.c +1 -1
@@ -350,7 +351,7 @@ static int ramoops_init_przs(struct device *dev, struct
 ramoops_context *cxt,
                             GFP_KERNEL);
        if (!cxt->przs) {
                dev_err(dev, "failed to initialize a prz array for
 dumps\n");
-               return -ENOMEM;
+               goto fail_prz;

This will have no effect. If cxt->przs == NULL, ramoops_free_przs will immediately exit too, not hitting your max_dump_cnt = 0 change. Perhaps move the =0 in that function to the top before the check and return?

Quoted 11 lines
fs/pstore/ram.c +0 -0
        }
        for (i = 0; i < cxt->max_dump_cnt; i++) {
fs/pstore/ram.c +0 -2
@@ -508,7 +509,6 @@ fail_buf:
        kfree(cxt->pstore.buf);
 fail_clear:
        cxt->pstore.bufsize = 0;
-       cxt->max_dump_cnt = 0;
 fail_cnt:
        kfree(cxt->fprz);
 fail_init_fprz:
--
Quoted 2 lines

1.8.3.2

Otherwise, yes, once fixed, this clean-up looks good -- it keeps the variable initialization and cleanup all in ramoops_init_przs() which is how it should be.

Thanks!

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
author
#3 lore raw

Re: [PATCH 2/2] pstore: correct the max_dump_cnt clearing of ramoops

Quoted 29 lines · Kees Cook
Quoted 28 lines · Liu ShuoX

From: Liu ShuoX <[email protected]>

In case that ramoops_init_przs failed, max_dump_cnt won't be reset to zero in error handle path.

---
 fs/pstore/ram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +1 -0
index 6f96d8c..522e530 100644
@@ -326,6 +326,7 @@ static void ramoops_free_przs(struct ramoops_context
 *cxt)
        for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
                persistent_ram_free(cxt->przs[i]);
        kfree(cxt->przs);
+       cxt->max_dump_cnt = 0;
 }
  static int ramoops_init_przs(struct device *dev, struct ramoops_context

*cxt,

fs/pstore/ram.c +0 -0
@@ -350,7 +351,7 @@ static int ramoops_init_przs(struct device *dev, struct
 ramoops_context *cxt,
                             GFP_KERNEL);
        if (!cxt->przs) {
                dev_err(dev, "failed to initialize a prz array for
Quoted 8 lines

fs/pstore/ram.c static int ramoops_init_przs(struct device *dev, struct · line 356

Quoted 3 lines
fs/pstore/ram.c +1 -1
 dumps\n");
-               return -ENOMEM;
+               goto fail_prz;

This will have no effect. If cxt->przs == NULL, ramoops_free_przs will immediately exit too, not hitting your max_dump_cnt = 0 change. Perhaps move the =0 in that function to the top before the check and return?

Yes, you are right. Below has the latest patch which move the =0 to the top of that function, just as you mentioned. Thanks.

Quoted 19 lines
Quoted 12 lines
fs/pstore/ram.c +0 -0
        }
        for (i = 0; i < cxt->max_dump_cnt; i++) {
fs/pstore/ram.c +0 -2
@@ -508,7 +509,6 @@ fail_buf:
        kfree(cxt->pstore.buf);
 fail_clear:
        cxt->pstore.bufsize = 0;
-       cxt->max_dump_cnt = 0;
 fail_cnt:
        kfree(cxt->fprz);
 fail_init_fprz:
--
Quoted 2 lines

1.8.3.2

Otherwise, yes, once fixed, this clean-up looks good -- it keeps the variable initialization and cleanup all in ramoops_init_przs() which is how it should be.

Quoted 3 lines

Thanks!

-Kees

-----

From: Liu ShuoX <[email protected]>

In case that ramoops_init_przs failed, max_dump_cnt won't be reset to zero in error handle path.

---
 fs/pstore/ram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +2 -2
index 6f96d8c..3b57443 100644
@@ -320,6 +320,7 @@ static void ramoops_free_przs(struct ramoops_context *cxt)
 {
 	int i;
 
+	cxt->max_dump_cnt = 0;
 	if (!cxt->przs)
 		return;
 
@@ -350,7 +351,7 @@ static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
 			     GFP_KERNEL);
 	if (!cxt->przs) {
 		dev_err(dev, "failed to initialize a prz array for dumps\n");
-		return -ENOMEM;
+		goto fail_prz;
 	}
 
 	for (i = 0; i < cxt->max_dump_cnt; i++) {
@@ -508,7 +509,6 @@ fail_buf:
 	kfree(cxt->pstore.buf);
 fail_clear:
 	cxt->pstore.bufsize = 0;
-	cxt->max_dump_cnt = 0;
 fail_cnt:
 	kfree(cxt->fprz);
 fail_init_fprz:
Signature · 1 line
1.8.3.2
Acked-by
#4 lore raw

Re: [PATCH 2/2] pstore: correct the max_dump_cnt clearing of ramoops

Quoted 72 lines · Liu ShuoX
Quoted 33 lines · Kees Cook
Quoted 31 lines · Liu ShuoX

From: Liu ShuoX <[email protected]>

In case that ramoops_init_przs failed, max_dump_cnt won't be reset to zero in error handle path.

---
 fs/pstore/ram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +1 -0
index 6f96d8c..522e530 100644
@@ -326,6 +326,7 @@ static void ramoops_free_przs(struct ramoops_context
 *cxt)
        for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
                persistent_ram_free(cxt->przs[i]);
        kfree(cxt->przs);
+       cxt->max_dump_cnt = 0;
 }
  static int ramoops_init_przs(struct device *dev, struct ramoops_context

*cxt,

fs/pstore/ram.c +0 -0
@@ -350,7 +351,7 @@ static int ramoops_init_przs(struct device *dev,

struct
ramoops_context *cxt,

                             GFP_KERNEL);
        if (!cxt->przs) {
                dev_err(dev, "failed to initialize a prz array for

dumps\n");

Quoted 8 lines

fs/pstore/ram.c static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt, · line 354

Quoted 2 lines
fs/pstore/ram.c +1 -1
-               return -ENOMEM;
+               goto fail_prz;

This will have no effect. If cxt->przs == NULL, ramoops_free_przs will immediately exit too, not hitting your max_dump_cnt = 0 change. Perhaps move the =0 in that function to the top before the check and return?

Yes, you are right. Below has the latest patch which move the =0 to the top of that function, just as you mentioned. Thanks.

Quoted 19 lines
Quoted 12 lines
        }
        for (i = 0; i < cxt->max_dump_cnt; i++) {
fs/pstore/ram.c +0 -2
@@ -508,7 +509,6 @@ fail_buf:
        kfree(cxt->pstore.buf);
 fail_clear:
        cxt->pstore.bufsize = 0;
-       cxt->max_dump_cnt = 0;
 fail_cnt:
        kfree(cxt->fprz);
 fail_init_fprz:
--
Quoted 2 lines

1.8.3.2

Otherwise, yes, once fixed, this clean-up looks good -- it keeps the variable initialization and cleanup all in ramoops_init_przs() which is how it should be.

Quoted 3 lines

Thanks!

-Kees

-----

Quoted 6 lines

From: Liu ShuoX <[email protected]>

In case that ramoops_init_przs failed, max_dump_cnt won't be reset to zero in error handle path.

Thanks! Looks right now.

-Kees

Quoted 38 lines
---
 fs/pstore/ram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +0 -0
index 6f96d8c..3b57443 100644
@@ -320,6 +320,7 @@ static void ramoops_free_przs(struct ramoops_context
 *cxt)
 {
        int i;
 
 +      cxt->max_dump_cnt = 0;
        if (!cxt->przs)
                return;
 
 @@ -350,7 +351,7 @@ static int ramoops_init_przs(struct device *dev, struct

ramoops_context *cxt,

                             GFP_KERNEL);
        if (!cxt->przs) {
                dev_err(dev, "failed to initialize a prz array for

dumps\n");

fs/pstore/ram.c +1 -3
-               return -ENOMEM;
+               goto fail_prz;
        }
        for (i = 0; i < cxt->max_dump_cnt; i++) {
@@ -508,7 +509,6 @@ fail_buf:
        kfree(cxt->pstore.buf);
 fail_clear:
        cxt->pstore.bufsize = 0;
-       cxt->max_dump_cnt = 0;
 fail_cnt:
        kfree(cxt->fprz);
 fail_init_fprz:
--

1.8.3.2

Signature · 2 lines
Kees Cook
Chrome OS Security
Acked-by on v1 1/2
#5 lore raw
In reply to Liu ShuoX
Quoted 13 lines · Liu ShuoX

These two patches are applied on top of patches:
https://lkml.org/lkml/2014/3/3/368
It has been added in -mm tree. Below is the first patch, and i will send the second by replying this one.

From: Liu ShuoX <[email protected]>

ramoops_get_next_prz get the prz according the paramters. If it get a uninitialized prz, access its members by following persistent_ram_old_size(prz)
will cause a NULL pointer crash.
Ex: if ftrace_size is 0, fprz will be NULL.

Quoted 3 lines

Fix it by return NULL in advance.

Thanks!

-Kees

Quoted 18 lines
---
 fs/pstore/ram.c | 2 ++
 1 file changed, 2 insertions(+)
fs/pstore/ram.c +2 -1
index 1daed28..6f96d8c 100644
@@ -119,6 +119,8 @@ ramoops_get_next_prz(struct persistent_ram_zone *przs[],
 uint *c, uint max,
                return NULL;
        prz = przs[i];
+       if (!prz)
+               return NULL;
        /* Update old/shadowed buffer. */
        if (update)
--

1.8.3.2

Signature · 2 lines
Kees Cook
Chrome OS Security