Skip to contentSkip to conversation

libsepol: add missing ibendport port validity check

PATCH Superseded Ondrej Mosnacek 2 participants 3 messages Oct 22, 2018 – Oct 23, 2018 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: add missing ibendport port validity check

The kernel checks if the port is in the range 1-255 when loading an ibenportcon rule. Add the same check to libsepol.

Fixes 118c0cd1038e ("libsepol: Add ibendport ocontext handling")
---
 libsepol/src/policydb.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
libsepol/src/policydb.c +9 -2
index db6765ba..e2808b2d 100644
@@ -2854,7 +2854,9 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
 					return -1;
 				break;
 			}
-			case OCON_IBENDPORT:
+			case OCON_IBENDPORT: {
+				uint32_t port;
+
 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
 				if (rc < 0)
 					return -1;
@@ -2862,6 +2864,10 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
 				if (len == 0 || len > IB_DEVICE_NAME_MAX - 1)
 					return -1;
 
+				port = le32_to_cpu(buf[1]);
+				if (port > 0xff || port == 0)
+					return -1;
+
 				c->u.ibendport.dev_name = malloc(len + 1);
 				if (!c->u.ibendport.dev_name)
 					return -1;
@@ -2869,11 +2875,12 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
 				if (rc < 0)
 					return -1;
 				c->u.ibendport.dev_name[len] = 0;
-				c->u.ibendport.port = le32_to_cpu(buf[1]);
+				c->u.ibendport.port = port;
 				if (context_read_and_validate
 				    (&c->context[0], p, fp))
 					return -1;
 				break;
+			}
 			case OCON_PORT:
 				rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
 				if (rc < 0)
Signature · 1 line
2.17.2
#1 lore raw
Quoted 26 lines · Ondrej Mosnacek

The kernel checks if the port is in the range 1-255 when loading an ibenportcon rule. Add the same check to libsepol.

Fixes 118c0cd1038e ("libsepol: Add ibendport ocontext handling")
---
 libsepol/src/policydb.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
libsepol/src/policydb.c +3 -1
index db6765ba..e2808b2d 100644
@@ -2854,7 +2854,9 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                        return -1;
                                break;
                        }
-                       case OCON_IBENDPORT:
+                       case OCON_IBENDPORT: {
+                               uint32_t port;
+
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
                                if (rc < 0)
                                        return -1;

libsepol/src/policydb.c static int ocontext_read_selinux(struct policydb_compat_info *info, · line 2869

Quoted 6 lines
libsepol/src/policydb.c +3 -0
@@ -2862,6 +2864,10 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                if (len == 0 || len > IB_DEVICE_NAME_MAX - 1)
                                        return -1;
 
+                               port = le32_to_cpu(buf[1]);
+                               if (port > 0xff || port == 0)
+                                       return -1;

You switched the other code to using UINT16_MAX, should probably use UINT8_MAX here.

Quoted 21 lines
libsepol/src/policydb.c +3 -2
+
                                c->u.ibendport.dev_name = malloc(len + 1);
                                if (!c->u.ibendport.dev_name)
                                        return -1;
@@ -2869,11 +2875,12 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                if (rc < 0)
                                        return -1;
                                c->u.ibendport.dev_name[len] = 0;
-                               c->u.ibendport.port = le32_to_cpu(buf[1]);
+                               c->u.ibendport.port = port;
                                if (context_read_and_validate
                                    (&c->context[0], p, fp))
                                        return -1;
                                break;
+                       }
                        case OCON_PORT:
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
                                if (rc < 0)
--

2.17.2

#2 lore raw
Quoted 30 lines · William Roberts
Quoted 29 lines · Ondrej Mosnacek

The kernel checks if the port is in the range 1-255 when loading an ibenportcon rule. Add the same check to libsepol.

Fixes 118c0cd1038e ("libsepol: Add ibendport ocontext handling")
---
 libsepol/src/policydb.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
libsepol/src/policydb.c +3 -1
index db6765ba..e2808b2d 100644
@@ -2854,7 +2854,9 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                        return -1;
                                break;
                        }
-                       case OCON_IBENDPORT:
+                       case OCON_IBENDPORT: {
+                               uint32_t port;
+
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
                                if (rc < 0)
                                        return -1;
@@ -2862,6 +2864,10 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                if (len == 0 || len > IB_DEVICE_NAME_MAX - 1)
                                        return -1;
 
Quoted 6 lines

libsepol/src/policydb.c static int ocontext_read_selinux(struct policydb_compat_info *info, · line 2869

Quoted 3 lines
libsepol/src/policydb.c +3 -0
+                               port = le32_to_cpu(buf[1]);
+                               if (port > 0xff || port == 0)
+                                       return -1;

You switched the other code to using UINT16_MAX, should probably use UINT8_MAX here.

Good point. I'll need to update the kernel patch as well.

Thanks,

Quoted 22 lines
Quoted 22 lines
libsepol/src/policydb.c +3 -2
+
                                c->u.ibendport.dev_name = malloc(len + 1);
                                if (!c->u.ibendport.dev_name)
                                        return -1;
@@ -2869,11 +2875,12 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                if (rc < 0)
                                        return -1;
                                c->u.ibendport.dev_name[len] = 0;
-                               c->u.ibendport.port = le32_to_cpu(buf[1]);
+                               c->u.ibendport.port = port;
                                if (context_read_and_validate
                                    (&c->context[0], p, fp))
                                        return -1;
                                break;
+                       }
                        case OCON_PORT:
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
                                if (rc < 0)
--

2.17.2

Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Associate Software Engineer, Security Technologies
Red Hat, Inc.