Skip to contentSkip to conversation

selinux: detect invalid ebitmap

PATCH Applied [email protected] 3 participants 10 messages Aug 23, 2016 – Aug 30, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 selinux: detect invalid ebitmap

From: William Roberts <[email protected]>

When count is 0 and the highbit is not zero, the ebitmap is not valid and the internal node is not allocated. This causes issues when routines, like mls_context_isvalid() attempt to use the ebitmap_for_each_bit() and ebitmap_node_get_bit() as they assume a highbit > 0 will have a node allocated.

---
 security/selinux/ss/ebitmap.c | 3 +++
 1 file changed, 3 insertions(+)
security/selinux/ss/ebitmap.c +3 -0
index 894b6cd..7d10e5d 100644
@@ -374,6 +374,9 @@ int ebitmap_read(struct ebitmap *e, void *fp)
 		goto ok;
 	}
 
+	if (e->highbit && !count)
+		goto bad;
+
 	for (i = 0; i < count; i++) {
 		rc = next_entry(&startbit, fp, sizeof(u32));
 		if (rc < 0) {
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/3 selinux: initialize structures

From: William Roberts <[email protected]>

libsepol pointed out an issue where its possible to have an unitialized jmp and invalid dereference, fix this. While we're here, zero allocate all the *_val_to_struct structures.

---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/ss/policydb.c +4 -4
index 992a315..4b24385 100644
@@ -541,21 +541,21 @@ static int policydb_index(struct policydb *p)
 
 	rc = -ENOMEM;
 	p->class_val_to_struct =
-		kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
+		kzalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
 			GFP_KERNEL);
 	if (!p->class_val_to_struct)
 		goto out;
 
 	rc = -ENOMEM;
 	p->role_val_to_struct =
-		kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
+		kzalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
 			GFP_KERNEL);
 	if (!p->role_val_to_struct)
 		goto out;
 
 	rc = -ENOMEM;
 	p->user_val_to_struct =
-		kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
+		kzalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
 			GFP_KERNEL);
 	if (!p->user_val_to_struct)
 		goto out;
@@ -964,7 +964,7 @@ int policydb_context_isvalid(struct policydb *p, struct context *c)
 		 * Role must be authorized for the type.
 		 */
 		role = p->role_val_to_struct[c->role - 1];
-		if (!ebitmap_get_bit(&role->types, c->type - 1))
+		if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
 			/* role may not be associated with type */
 			return 0;
 
Signature · 1 line
1.9.1
maintainer Applied
#2 lore raw

Re: [PATCH 2/3] selinux: initialize structures

Quoted 11 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

libsepol pointed out an issue where its possible to have an unitialized jmp and invalid dereference, fix this. While we're here, zero allocate all the *_val_to_struct structures.

---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Merged, thanks.

Quoted 46 lines
security/selinux/ss/policydb.c +4 -5
index 992a315..4b24385 100644
@@ -541,21 +541,21 @@ static int policydb_index(struct policydb *p)
 
        rc = -ENOMEM;
        p->class_val_to_struct =
-               kmalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
+               kzalloc(p->p_classes.nprim * sizeof(*(p->class_val_to_struct)),
                        GFP_KERNEL);
        if (!p->class_val_to_struct)
                goto out;
 
        rc = -ENOMEM;
        p->role_val_to_struct =
-               kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
+               kzalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
                        GFP_KERNEL);
        if (!p->role_val_to_struct)
                goto out;
 
        rc = -ENOMEM;
        p->user_val_to_struct =
-               kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
+               kzalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
                        GFP_KERNEL);
        if (!p->user_val_to_struct)
                goto out;
@@ -964,7 +964,7 @@ int policydb_context_isvalid(struct policydb *p, struct context *c)
                 * Role must be authorized for the type.
                 */
                role = p->role_val_to_struct[c->role - 1];
-               if (!ebitmap_get_bit(&role->types, c->type - 1))
+               if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
                        /* role may not be associated with type */
                        return 0;
 
--

1.9.1

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

Signature · 2 lines
paul moore
www.paul-moore.com
#3 lore raw

PATCH 3/3 selinux: fix overflow and 0 length allocations

In reply to [email protected]

From: William Roberts <[email protected]>

Throughout the SE Linux LSM, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.

---
 security/selinux/ss/conditional.c | 3 +++
 security/selinux/ss/policydb.c    | 4 ++++
 security/selinux/ss/private.h     | 7 +++++++
 3 files changed, 14 insertions(+)
 create mode 100644 security/selinux/ss/private.h
security/selinux/ss/conditional.c +3 -0
index 456e1a9..ecc0fb6 100644
@@ -16,6 +16,7 @@
 #include "security.h"
 #include "conditional.h"
 #include "services.h"
+#include "private.h"
 
 /*
  * cond_evaluate_expr evaluates a conditional expr
@@ -242,6 +243,8 @@ int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
 		goto err;
 
 	len = le32_to_cpu(buf[2]);
+	if (zero_or_saturated(len))
+		goto err;
 
 	rc = -ENOMEM;
 	key = kmalloc(len + 1, GFP_KERNEL);
security/selinux/ss/policydb.c +4 -0
index 4b24385..0e881f3 100644
@@ -38,6 +38,7 @@
 #include "conditional.h"
 #include "mls.h"
 #include "services.h"
+#include "private.h"
 
 #define _DEBUG_HASHES
 
@@ -1094,6 +1095,9 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
 	int rc;
 	char *str;
 
+	if (zero_or_saturated(len))
+		return -EINVAL;
+
 	str = kmalloc(len + 1, flags);
 	if (!str)
 		return -ENOMEM;
security/selinux/ss/private.h new file +7 -0
new file mode 100644
index 0000000..0e81a78
@@ -0,0 +1,7 @@
+#ifndef PRIVATE_H_
+#define PRIVATE_H_
+
+#define is_saturated(x) (x == (typeof(x))-1)
+#define zero_or_saturated(x) ((x == 0) || is_saturated(x))
+
+#endif
Signature · 1 line
1.9.1
maintainer
#4 lore raw

Re: [PATCH 3/3] selinux: fix overflow and 0 length allocations

Quoted 3 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Throughout the SE Linux LSM, values taken from sepolicy are

I'll take a closer look at this patchset after LinuxCon/LSS, but thanks for doing this ... however, one little bikeshed thing that drives me crazy is the use of "SE Linux" instead of "SELinux". Don't change anything you've submitted, but in the future please use SELinux ;)

Quoted 75 lines

used in places where length == 0 or length == <saturated> matter, find and fix these.

---
 security/selinux/ss/conditional.c | 3 +++
 security/selinux/ss/policydb.c    | 4 ++++
 security/selinux/ss/private.h     | 7 +++++++
 3 files changed, 14 insertions(+)
 create mode 100644 security/selinux/ss/private.h
security/selinux/ss/conditional.c +3 -0
index 456e1a9..ecc0fb6 100644
@@ -16,6 +16,7 @@
 #include "security.h"
 #include "conditional.h"
 #include "services.h"
+#include "private.h"
 
 /*
  * cond_evaluate_expr evaluates a conditional expr
@@ -242,6 +243,8 @@ int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
                goto err;
 
        len = le32_to_cpu(buf[2]);
+       if (zero_or_saturated(len))
+               goto err;
 
        rc = -ENOMEM;
        key = kmalloc(len + 1, GFP_KERNEL);
security/selinux/ss/policydb.c +4 -0
index 4b24385..0e881f3 100644
@@ -38,6 +38,7 @@
 #include "conditional.h"
 #include "mls.h"
 #include "services.h"
+#include "private.h"
 
 #define _DEBUG_HASHES
 
@@ -1094,6 +1095,9 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
        int rc;
        char *str;
 
+       if (zero_or_saturated(len))
+               return -EINVAL;
+
        str = kmalloc(len + 1, flags);
        if (!str)
                return -ENOMEM;
security/selinux/ss/private.h new file +7 -1
new file mode 100644
index 0000000..0e81a78
@@ -0,0 +1,7 @@
+#ifndef PRIVATE_H_
+#define PRIVATE_H_
+
+#define is_saturated(x) (x == (typeof(x))-1)
+#define zero_or_saturated(x) ((x == 0) || is_saturated(x))
+
+#endif
--

1.9.1

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

Signature · 2 lines
paul moore
www.paul-moore.com
maintainer
#5 lore raw

Re: [PATCH 3/3] selinux: fix overflow and 0 length allocations

In reply to [email protected]
Quoted 63 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Throughout the SE Linux LSM, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.

---
 security/selinux/ss/conditional.c | 3 +++
 security/selinux/ss/policydb.c    | 4 ++++
 security/selinux/ss/private.h     | 7 +++++++
 3 files changed, 14 insertions(+)
 create mode 100644 security/selinux/ss/private.h
security/selinux/ss/conditional.c +3 -0
index 456e1a9..ecc0fb6 100644
@@ -16,6 +16,7 @@
 #include "security.h"
 #include "conditional.h"
 #include "services.h"
+#include "private.h"
 
 /*
  * cond_evaluate_expr evaluates a conditional expr
@@ -242,6 +243,8 @@ int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
                goto err;
 
        len = le32_to_cpu(buf[2]);
+       if (zero_or_saturated(len))
+               goto err;
 
        rc = -ENOMEM;
        key = kmalloc(len + 1, GFP_KERNEL);
security/selinux/ss/policydb.c +4 -0
index 4b24385..0e881f3 100644
@@ -38,6 +38,7 @@
 #include "conditional.h"
 #include "mls.h"
 #include "services.h"
+#include "private.h"
 
 #define _DEBUG_HASHES
 
@@ -1094,6 +1095,9 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
        int rc;
        char *str;
 
+       if (zero_or_saturated(len))
+               return -EINVAL;
+
        str = kmalloc(len + 1, flags);
        if (!str)
                return -ENOMEM;
security/selinux/ss/private.h new file +0 -0
new file mode 100644
index 0000000..0e81a78

security/selinux/ss/private.h line 7

Quoted 7 lines
security/selinux/ss/private.h new file +7 -0
@@ -0,0 +1,7 @@
+#ifndef PRIVATE_H_
+#define PRIVATE_H_
+
+#define is_saturated(x) (x == (typeof(x))-1)
+#define zero_or_saturated(x) ((x == 0) || is_saturated(x))
+
+#endif

While I'm not opposed to the idea of using a macro for this purpose, e.g. is_saturated() and zero_or_saturated(), I don't see much value in creating this macro buried in the SELinux directory. Especially if we end up creating a new header file just for these macros. Something as generic as this should be something we inherit from the generic kernel code so we can leverage existing conventions.

If you can find a macro like these in the core kernel code, go ahead and use it, otherwise please respin this with the bound checks open coded.

Oh, and use "SELinux" ;)

Signature · 2 lines
paul moore
www.paul-moore.com
#6 lore raw

Re: [PATCH 3/3] selinux: fix overflow and 0 length allocations

Quoted 16 lines · Paul Moore
Quoted 14 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

Throughout the SE Linux LSM, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.

---
 security/selinux/ss/conditional.c | 3 +++
 security/selinux/ss/policydb.c    | 4 ++++
 security/selinux/ss/private.h     | 7 +++++++
 3 files changed, 14 insertions(+)
 create mode 100644 security/selinux/ss/private.h
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/security/selinux/ss/conditional.c

Quoted 9 lines
Quoted 9 lines

index 456e1a9..ecc0fb6 100644

security/selinux/ss/conditional.c +1 -0
@@ -16,6 +16,7 @@
 #include "security.h"
 #include "conditional.h"
 #include "services.h"
+#include "private.h"
 
Quoted 3 lines

security/selinux/ss/conditional.c int cond_read_bool(struct policydb *p, struct

Quoted 3 lines
security/selinux/ss/conditional.c +0 -0
 /*
  * cond_evaluate_expr evaluates a conditional expr
@@ -242,6 +243,8 @@ int cond_read_bool(struct policydb *p, struct

hashtab *h, void *fp)

Quoted 9 lines

Review

Quoted 9 lines
security/selinux/ss/conditional.c +2 -0
                goto err;
 
        len = le32_to_cpu(buf[2]);
+       if (zero_or_saturated(len))
+               goto err;
 
        rc = -ENOMEM;
        key = kmalloc(len + 1, GFP_KERNEL);
(quoted diff) +0 -0

b/security/selinux/ss/policydb.c

Quoted 9 lines
Quoted 9 lines

index 4b24385..0e881f3 100644

security/selinux/ss/policydb.c +1 -0
@@ -38,6 +38,7 @@
 #include "conditional.h"
 #include "mls.h"
 #include "services.h"
+#include "private.h"
 
Quoted 3 lines

security/selinux/ss/policydb.c static int str_read(char **strp, gfp_t flags,

Quoted 3 lines
security/selinux/ss/policydb.c +0 -0
 #define _DEBUG_HASHES
 
@@ -1094,6 +1095,9 @@ static int str_read(char **strp, gfp_t flags,

void *fp, u32 len)

Quoted 10 lines

Review

Quoted 10 lines
security/selinux/ss/policydb.c +3 -0
        int rc;
        char *str;
 
+       if (zero_or_saturated(len))
+               return -EINVAL;
+
        str = kmalloc(len + 1, flags);
        if (!str)
                return -ENOMEM;
(quoted diff) +0 -0

b/security/selinux/ss/private.h

Quoted 11 lines
Quoted 11 lines

new file mode 100644
index 0000000..0e81a78

security/selinux/ss/private.h new file +6 -0
@@ -0,0 +1,7 @@
+#ifndef PRIVATE_H_
+#define PRIVATE_H_
+
+#define is_saturated(x) (x == (typeof(x))-1)
+#define zero_or_saturated(x) ((x == 0) || is_saturated(x))
+
Quoted 8 lines

security/selinux/ss/private.h line 7

Quoted 1 line
security/selinux/ss/private.h new file +1 -0
+#endif

While I'm not opposed to the idea of using a macro for this purpose, e.g. is_saturated() and zero_or_saturated(), I don't see much value in creating this macro buried in the SELinux directory. Especially if we end up creating a new header file just for these macros. Something as generic as this should be something we inherit from the generic kernel code so we can leverage existing conventions.

It made more sense when I wanted to keep the change similar to libsepol. But the str_read() function really reduced the number of checks. I ended up porting that to libsepol. With that said, the macro serves little purpose with the exception of readability.

Quoted 2 lines

If you can find a macro like these in the core kernel code, go ahead

Ill look, doubt I'll find anything.

Quoted 2 lines

and use it, otherwise please respin this with the bound checks open code

Sure

Quoted 11 lines

Oh, and use "SELinux" ;)

--
paul moore
www.paul-moore.com
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

maintainer
#7 lore raw
In reply to [email protected]
Quoted 10 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

When count is 0 and the highbit is not zero, the ebitmap is not valid and the internal node is not allocated. This causes issues when routines, like mls_context_isvalid() attempt to use the ebitmap_for_each_bit() and ebitmap_node_get_bit() as they assume a highbit > 0 will have a node allocated.

---
 security/selinux/ss/ebitmap.c | 3 +++
 1 file changed, 3 insertions(+)

Hi William,

This patch looks good to me, but do I have your permission to add your sign-off?

Quoted 22 lines
security/selinux/ss/ebitmap.c +3 -1
index 894b6cd..7d10e5d 100644
@@ -374,6 +374,9 @@ int ebitmap_read(struct ebitmap *e, void *fp)
                goto ok;
        }
 
+       if (e->highbit && !count)
+               goto bad;
+
        for (i = 0; i < count; i++) {
                rc = next_entry(&startbit, fp, sizeof(u32));
                if (rc < 0) {
--

1.9.1

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

Signature · 2 lines
paul moore
www.paul-moore.com
#8 lore raw
Quoted 22 lines

-----Original Message-----
From: Paul Moore [mailto:[email protected]]
Sent: Monday, August 29, 2016 4:21 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; Stephen Smalley <[email protected]>
Subject: Re: [PATCH 1/3] selinux: detect invalid ebitmap

Quoted 10 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

When count is 0 and the highbit is not zero, the ebitmap is not valid and the internal node is not allocated. This causes issues when routines, like mls_context_isvalid() attempt to use the ebitmap_for_each_bit() and ebitmap_node_get_bit() as they assume a highbit > 0 will have a node allocated.

---
 security/selinux/ss/ebitmap.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 3 lines

Hi William,

This patch looks good to me, but do I have your permission to add your sign-off?

Yes, I guess I missed it. Just so it's easy for you to copy paste:

Quoted 29 lines
Quoted 18 lines
(quoted diff) +0 -0

b/security/selinux/ss/ebitmap.c index 894b6cd..7d10e5d 100644

security/selinux/ss/ebitmap.c +3 -1
@@ -374,6 +374,9 @@ int ebitmap_read(struct ebitmap *e, void *fp)
                goto ok;
        }
 
+       if (e->highbit && !count)
+               goto bad;
+
        for (i = 0; i < count; i++) {
                rc = next_entry(&startbit, fp, sizeof(u32));
                if (rc < 0) {
--

1.9.1

Quoted 5 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

--
paul moore
www.paul-moore.com

maintainer
#9 lore raw
Quoted 20 lines · William C
Quoted 20 lines

-----Original Message-----
From: Paul Moore [mailto:[email protected]]
Sent: Monday, August 29, 2016 4:21 PM
To: Roberts, William C <[email protected]>
Cc: [email protected]; [email protected]; Stephen Smalley <[email protected]>
Subject: Re: [PATCH 1/3] selinux: detect invalid ebitmap

Quoted 10 lines · On Tue, Aug 23, 2016 at 4:49 PM, <[email protected]> wrote:

From: William Roberts <[email protected]>

When count is 0 and the highbit is not zero, the ebitmap is not valid and the internal node is not allocated. This causes issues when routines, like mls_context_isvalid() attempt to use the ebitmap_for_each_bit() and ebitmap_node_get_bit() as they assume a highbit > 0 will have a node allocated.

---
 security/selinux/ss/ebitmap.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 3 lines

Hi William,

This patch looks good to me, but do I have your permission to add your sign-off?

Yes, I guess I missed it. Just so it's easy for you to copy paste:

Great, thanks!

Quoted 28 lines
Quoted 28 lines
Quoted 17 lines
(quoted diff) +0 -0

b/security/selinux/ss/ebitmap.c index 894b6cd..7d10e5d 100644

security/selinux/ss/ebitmap.c +3 -1
@@ -374,6 +374,9 @@ int ebitmap_read(struct ebitmap *e, void *fp)
                goto ok;
        }
 
+       if (e->highbit && !count)
+               goto bad;
+
        for (i = 0; i < count; i++) {
                rc = next_entry(&startbit, fp, sizeof(u32));
                if (rc < 0) {
--

1.9.1

Quoted 5 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

--
paul moore
www.paul-moore.com

Signature · 2 lines
paul moore
www.paul-moore.com