Skip to contentSkip to conversation

selinux: simple cleanup for cond_read_node()

PATCH In review Namhyung Kim 3 participants 7 messages Jun 14, 2014 – Jun 19, 2014 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 selinux: simple cleanup for cond_read_node()

The node->cur_state and len can be read in a single call of next_entry(). And setting len before reading is a dead write so can be eliminated.

---
 security/selinux/ss/conditional.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
security/selinux/ss/conditional.c +2 -7
index 377d148e7157..4766a38fae9a 100644
@@ -402,19 +402,14 @@ static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
 	int rc;
 	struct cond_expr *expr = NULL, *last = NULL;
 
-	rc = next_entry(buf, fp, sizeof(u32));
+	rc = next_entry(buf, fp, sizeof(buf));
 	if (rc)
 		return rc;
 
 	node->cur_state = le32_to_cpu(buf[0]);
 
-	len = 0;
-	rc = next_entry(buf, fp, sizeof(u32));
-	if (rc)
-		return rc;
-
 	/* expr */
-	len = le32_to_cpu(buf[0]);
+	len = le32_to_cpu(buf[1]);
 
 	for (i = 0; i < len; i++) {
 		rc = next_entry(buf, fp, sizeof(u32) * 2);
Signature · 1 line
2.0.0
#1 lore raw

PATCH 2/2 selinux: fix a possible memory leak in cond_read_node()

The cond_read_node() should free the given node on error path as it's not linked to p->cond_list yet. This is done via cond_node_destroy() but it's not called when next_entry() fails before the expr loop.

---
 security/selinux/ss/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/ss/conditional.c +1 -1
index 4766a38fae9a..470d5cca8d14 100644
@@ -404,7 +404,7 @@ static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
 
 	rc = next_entry(buf, fp, sizeof(buf));
 	if (rc)
-		return rc;
+		goto err;
 
 	node->cur_state = le32_to_cpu(buf[0]);
 
Signature · 1 line
2.0.0
maintainer
#2 lore raw

Re: [PATCH 2/2] selinux: fix a possible memory leak in cond_read_node()

Quoted 8 lines · Namhyung Kim

The cond_read_node() should free the given node on error path as it's not linked to p->cond_list yet. This is done via cond_node_destroy() but it's not called when next_entry() fails before the expr loop.

---
 security/selinux/ss/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, nice catch. This patch looks good to me but it is dependent on patch 1/2 which I commented on ...

Quoted 13 lines
(quoted diff) +0 -0

b/security/selinux/ss/conditional.c index 4766a38fae9a..470d5cca8d14 100644

security/selinux/ss/conditional.c +1 -1
@@ -404,7 +404,7 @@ static int cond_read_node(struct policydb *p, struct
 cond_node *node, void *fp)
 
 	rc = next_entry(buf, fp, sizeof(buf));
 	if (rc)
-		return rc;
+		goto err;
 
 	node->cur_state = le32_to_cpu(buf[0]);
Signature · 2 lines
paul moore
www.paul-moore.com
maintainer
#3 lore raw
In reply to Namhyung Kim
Quoted 13 lines · Namhyung Kim

The node->cur_state and len can be read in a single call of next_entry(). And setting len before reading is a dead write so can be eliminated.

---
 security/selinux/ss/conditional.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/security/selinux/ss/conditional.c index 377d148e7157..4766a38fae9a 100644

security/selinux/ss/conditional.c static int cond_read_node(struct policydb *p, struct · line 405

Quoted 5 lines
security/selinux/ss/conditional.c +1 -1
@@ -402,19 +402,14 @@ static int cond_read_node(struct policydb *p, struct
 cond_node *node, void *fp) int rc;
 	struct cond_expr *expr = NULL, *last = NULL;
 
-	rc = next_entry(buf, fp, sizeof(u32));
+	rc = next_entry(buf, fp, sizeof(buf));

This is a bit nit-picky, but how about using "sizeof(u32) * 2"? It is more consistent with the rest of the function and helps underscore that we are reading two 32-bit values.

Assuming you're okay with the change I can fix it up when I apply the patch.

Quoted 16 lines
security/selinux/ss/conditional.c +1 -6
 	if (rc)
 		return rc;
 
 	node->cur_state = le32_to_cpu(buf[0]);
 
-	len = 0;
-	rc = next_entry(buf, fp, sizeof(u32));
-	if (rc)
-		return rc;
-
 	/* expr */
-	len = le32_to_cpu(buf[0]);
+	len = le32_to_cpu(buf[1]);
 
 	for (i = 0; i < len; i++) {
 		rc = next_entry(buf, fp, sizeof(u32) * 2);
Signature · 2 lines
paul moore
www.paul-moore.com
#4 lore raw

Hi Paul,

Quoted 4 lines · Paul Moore
Quoted 4 lines
security/selinux/ss/conditional.c +0 -0
@@ -402,19 +402,14 @@ static int cond_read_node(struct policydb *p, struct
 cond_node *node, void *fp) int rc;
      struct cond_expr *expr = NULL, *last = NULL;
 
Quoted 8 lines

security/selinux/ss/conditional.c static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp) · line 405

Quoted 2 lines
security/selinux/ss/conditional.c +1 -1
-     rc = next_entry(buf, fp, sizeof(u32));
+     rc = next_entry(buf, fp, sizeof(buf));

This is a bit nit-picky, but how about using "sizeof(u32) * 2"? It is more consistent with the rest of the function and helps underscore that we are reading two 32-bit values.

Assuming you're okay with the change I can fix it up when I apply the patch.

I'm okay with it. :)

Thanks,
Namhyung

maintainer
#5 lore raw
Quoted 18 lines · Namhyung Kim

Hi Paul,

Quoted 15 lines · Paul Moore

security/selinux/ss/conditional.c static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp) · line 405

Quoted 9 lines
(quoted diff) +0 -0
@@ -402,19 +402,14 @@ static int cond_read_node(struct policydb *p,

struct

cond_node *node, void *fp) int rc;

      struct cond_expr *expr = NULL, *last = NULL;
security/selinux/ss/conditional.c +1 -1
-     rc = next_entry(buf, fp, sizeof(u32));
+     rc = next_entry(buf, fp, sizeof(buf));

This is a bit nit-picky, but how about using "sizeof(u32) * 2"? It is more
consistent with the rest of the function and helps underscore that we are reading two 32-bit values.

Quoted 4 lines
Quoted 2 lines

Assuming you're okay with the change I can fix it up when I apply the patch.

I'm okay with it. :)

Great, both patches are applied.

Signature · 2 lines
paul moore
www.paul-moore.com
#6 lore raw
In reply to Paul Moore
Quoted 17 lines · Paul Moore
Quoted 16 lines · Namhyung Kim

The node->cur_state and len can be read in a single call of next_entry(). And setting len before reading is a dead write so can be eliminated.

---
 security/selinux/ss/conditional.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/security/selinux/ss/conditional.c index 377d148e7157..4766a38fae9a 100644

security/selinux/ss/conditional.c +0 -0
@@ -402,19 +402,14 @@ static int cond_read_node(struct policydb *p, struct
 cond_node *node, void *fp) int rc;
 	struct cond_expr *expr = NULL, *last = NULL;
 
Quoted 5 lines

security/selinux/ss/conditional.c static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp) · line 405

Quoted 2 lines
security/selinux/ss/conditional.c +1 -1
-	rc = next_entry(buf, fp, sizeof(u32));
+	rc = next_entry(buf, fp, sizeof(buf));

This is a bit nit-picky, but how about using "sizeof(u32) * 2"? It is more consistent with the rest of the function and helps underscore that we are

Concur - I don't want to assume that the buf size is always the same as the next read size (e.g. we sometimes use the same buf for multiple reads).