selinux: simple cleanup for cond_read_node()
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
@@ -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
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
@@ -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
Paul Moore
maintainerRe: [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 top->cond_listyet. This is done viacond_node_destroy()but it's not called whennext_entry()fails before the expr loop.Signed-off-by Namhyung Kim <[email protected]>--- 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.cindex4766a38fae9a..470d5cca8d14100644security/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
Paul Moore
maintainerQuoted 13 lines · Namhyung Kim
The
node->cur_stateand len can be read in a single call ofnext_entry(). And setting len before reading is a dead write so can be eliminated.Signed-off-by Namhyung Kim <[email protected]>--- security/selinux/ss/conditional.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)(quoted diff) +0
-0
b/security/selinux/ss/conditional.cindex377d148e7157..4766a38fae9a100644
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
-6if (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
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
Paul Moore
maintainerQuoted 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
Quoted 17 lines · Paul Moore
Quoted 16 lines · Namhyung Kim
The
node->cur_stateand len can be read in a single call ofnext_entry(). And setting len before reading is a dead write so can be eliminated.Signed-off-by Namhyung Kim <[email protected]>--- security/selinux/ss/conditional.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)(quoted diff) +0
-0
b/security/selinux/ss/conditional.cindex377d148e7157..4766a38fae9a100644security/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).