Skip to contentSkip to conversation

libsepol: fix endianity in ibpkey range checks

PATCH Superseded Ondrej Mosnacek 4 participants 9 messages Oct 17, 2018 – Oct 18, 2018 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: fix endianity in ibpkey range checks

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
 libsepol/src/policydb.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +10 -4
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
 				break;
 			case OCON_IBPKEY:
 				rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-				if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+				if (rc < 0)
 					return -1;
 
+				c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+				c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
+
+				if (c->u.ibpkey.low_pkey  > 0xffff ||
+				    c->u.ibpkey.high_pkey > 0xffff)
+					return -1;
+
+				/* we want c->u.ibpkey.subnet_prefix in network
+				 * (big-endian) order, just memcpy it */
 				memcpy(&c->u.ibpkey.subnet_prefix, buf,
 				       sizeof(c->u.ibpkey.subnet_prefix));
 
-				c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-				c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
 				if (context_read_and_validate
 				    (&c->context[0], p, fp))
 					return -1;
Signature · 1 line
2.17.2
#1 lore raw
Quoted 41 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
 libsepol/src/policydb.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +10 -5
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                break;
                        case OCON_IBPKEY:
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                               if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                               if (rc < 0)
                                        return -1;
 
+                               c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
+
+                               if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                   c->u.ibpkey.high_pkey > 0xffff)
+                                       return -1;
+
+                               /* we want c->u.ibpkey.subnet_prefix in network
+                                * (big-endian) order, just memcpy it */
                                memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                       sizeof(c->u.ibpkey.subnet_prefix));
 
-                               c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                                if (context_read_and_validate
                                    (&c->context[0], p, fp))
                                        return -1;
--
Quoted 1 line

2.17.2

This seems to line up with what I have been following on the kernel side. But since Stephen
committed the patch this fixes up and is way more involved, ill defer to him. Soft ack from me,
but I would imagine we would want to see an ack on the kernel side before pulling this in.

Quoted 6 lines

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

Acked-by on v1 1/1
#2 lore raw
In reply to Ondrej Mosnacek
Quoted 5 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
Quoted 34 lines
---
  libsepol/src/policydb.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +10 -4
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
  				break;
  			case OCON_IBPKEY:
  				rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-				if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+				if (rc < 0)
  					return -1;
 
+				c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+				c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
+
+				if (c->u.ibpkey.low_pkey  > 0xffff ||
+				    c->u.ibpkey.high_pkey > 0xffff)
+					return -1;
+
+				/* we want c->u.ibpkey.subnet_prefix in network
+				 * (big-endian) order, just memcpy it */
  				memcpy(&c->u.ibpkey.subnet_prefix, buf,
  				       sizeof(c->u.ibpkey.subnet_prefix)); >
-				c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-				c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
  				if (context_read_and_validate
  				    (&c->context[0], p, fp))
  					return -1;
 
#3 lore raw
Quoted 9 lines · Stephen Smalley
Quoted 5 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")

Stephen,
Ill stage this as a PR. Do you want to wait until the kernel changes are in or just
the normal 24 hours?

Bill

Quoted 41 lines
Quoted 25 lines
---
  libsepol/src/policydb.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +9 -1
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                              break;
                      case OCON_IBPKEY:
                              rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                             if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                             if (rc < 0)
                                      return -1;
 
+                             c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                             c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
+
+                             if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                 c->u.ibpkey.high_pkey > 0xffff)
+                                     return -1;
+
+                             /* we want c->u.ibpkey.subnet_prefix in network

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

Quoted 10 lines
libsepol/src/policydb.c +1 -3
+                              * (big-endian) order, just memcpy it */
                              memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                     sizeof(c->u.ibpkey.subnet_prefix)); >
-                             c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                             c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                              if (context_read_and_validate
                                  (&c->context[0], p, fp))
                                      return -1;
 

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

#4 lore raw
In reply to Ondrej Mosnacek
Quoted 15 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
 libsepol/src/policydb.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +0 -0
index a6d76ca3..dc201e2f 100644

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

Quoted 9 lines
libsepol/src/policydb.c +3 -1
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                break;
                        case OCON_IBPKEY:
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                               if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                               if (rc < 0)
                                        return -1;
 
+                               c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);

Ahh you're assigning a 32 bit value to a 16 bit variable low|high_pkey. I think you need to assign them to a uint32_t if you want to actually range check them.

Quoted 17 lines
libsepol/src/policydb.c +7 -4
+
+                               if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                   c->u.ibpkey.high_pkey > 0xffff)
+                                       return -1;
+
+                               /* we want c->u.ibpkey.subnet_prefix in network
+                                * (big-endian) order, just memcpy it */
                                memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                       sizeof(c->u.ibpkey.subnet_prefix));
 
-                               c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                                if (context_read_and_validate
                                    (&c->context[0], p, fp))
                                        return -1;
--
Quoted 2 lines

2.17.2

See job: https://travis-ci.org/SELinuxProject/selinux/jobs/442750208

Build fail with gcc:

policydb.c:2839:31: error: comparison is always false due to limited range of data type [-Werror=type-limits]

     if (c->u.ibpkey.low_pkey  > 0xffff ||
                               ^
policydb.c:2840:31: error: comparison is always false due to limited
range of data type [-Werror=type-limits]
         c->u.ibpkey.high_pkey > 0xffff)
maintainer
#5 lore raw
Quoted 23 lines · William Roberts
Quoted 22 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
 libsepol/src/policydb.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +1 -1
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                break;
                        case OCON_IBPKEY:
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                               if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                               if (rc < 0)
                                        return -1;
 
Quoted 5 lines

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

Quoted 2 lines
libsepol/src/policydb.c +2 -0
+                               c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);

Ahh you're assigning a 32 bit value to a 16 bit variable low|high_pkey. I think you need to assign them to a uint32_t if you want to actually range check them.

If you can, give me a chance to look over the kernel changes first. I doubt I'll see anything objectionable given the review the patches have already seen, but one never knows.

Quoted 30 lines
Quoted 17 lines
libsepol/src/policydb.c +7 -4
+
+                               if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                   c->u.ibpkey.high_pkey > 0xffff)
+                                       return -1;
+
+                               /* we want c->u.ibpkey.subnet_prefix in network
+                                * (big-endian) order, just memcpy it */
                                memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                       sizeof(c->u.ibpkey.subnet_prefix));
 
-                               c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                                if (context_read_and_validate
                                    (&c->context[0], p, fp))
                                        return -1;
--
Quoted 2 lines

2.17.2

See job: https://travis-ci.org/SELinuxProject/selinux/jobs/442750208

Build fail with gcc:

policydb.c:2839:31: error: comparison is always false due to limited range of data type [-Werror=type-limits]

     if (c->u.ibpkey.low_pkey  > 0xffff ||
                               ^
policydb.c:2840:31: error: comparison is always false due to limited
range of data type [-Werror=type-limits]
         c->u.ibpkey.high_pkey > 0xffff)
Signature · 2 lines
paul moore
www.paul-moore.com
#6 lore raw
Quoted 31 lines · Paul Moore
Quoted 23 lines · William Roberts
Quoted 22 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
  libsepol/src/policydb.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +1 -1
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                 break;
                         case OCON_IBPKEY:
                                 rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                               if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                               if (rc < 0)
                                         return -1;
 
Quoted 5 lines

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

Quoted 2 lines
libsepol/src/policydb.c +2 -0
+                               c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);

Ahh you're assigning a 32 bit value to a 16 bit variable low|high_pkey. I think you need to assign them to a uint32_t if you want to actually range check them.

Quoted 3 lines

If you can, give me a chance to look over the kernel changes first. I doubt I'll see anything objectionable given the review the patches have already seen, but one never knows.

The kernel patch has the same bug, so it will also need a re-spin. Good catch.

Quoted 34 lines
Quoted 31 lines
Quoted 18 lines
libsepol/src/policydb.c +7 -4
+
+                               if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                   c->u.ibpkey.high_pkey > 0xffff)
+                                       return -1;
+
+                               /* we want c->u.ibpkey.subnet_prefix in network
+                                * (big-endian) order, just memcpy it */
                                 memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                        sizeof(c->u.ibpkey.subnet_prefix));
 
-                               c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                                 if (context_read_and_validate
                                     (&c->context[0], p, fp))
                                         return -1;
--
Quoted 2 lines

2.17.2

See job: https://travis-ci.org/SELinuxProject/selinux/jobs/442750208

Build fail with gcc:

policydb.c:2839:31: error: comparison is always false due to limited range of data type [-Werror=type-limits]

      if (c->u.ibpkey.low_pkey  > 0xffff ||
                                ^
policydb.c:2840:31: error: comparison is always false due to limited
range of data type [-Werror=type-limits]
          c->u.ibpkey.high_pkey > 0xffff)
#7 lore raw
Quoted 33 lines · Stephen Smalley
Quoted 31 lines · Paul Moore
Quoted 28 lines · William Roberts
Quoted 15 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
  libsepol/src/policydb.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +0 -0
index a6d76ca3..dc201e2f 100644

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

Quoted 9 lines
libsepol/src/policydb.c +3 -1
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                 break;
                         case OCON_IBPKEY:
                                 rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                               if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                               if (rc < 0)
                                         return -1;
 
+                               c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);

Ahh you're assigning a 32 bit value to a 16 bit variable low|high_pkey. I think you need to assign them to a uint32_t if you want to actually range check them.

Quoted 6 lines
Quoted 3 lines

If you can, give me a chance to look over the kernel changes first. I doubt I'll see anything objectionable given the review the patches have already seen, but one never knows.

The kernel patch has the same bug, so it will also need a re-spin. Good catch.

Don't thank me, thank GCC and Travis. This compiled on my local machine and ran the test suite just fine. I had clang set up, I guess this re-iterates the need and benefit of Travis testing in different environments.

Quoted 36 lines
Quoted 35 lines
Quoted 32 lines
Quoted 19 lines
libsepol/src/policydb.c +7 -4
+
+                               if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                   c->u.ibpkey.high_pkey > 0xffff)
+                                       return -1;
+
+                               /* we want c->u.ibpkey.subnet_prefix in network
+                                * (big-endian) order, just memcpy it */
                                 memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                        sizeof(c->u.ibpkey.subnet_prefix));
 
-                               c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                                 if (context_read_and_validate
                                     (&c->context[0], p, fp))
                                         return -1;
--
Quoted 2 lines

2.17.2

See job: https://travis-ci.org/SELinuxProject/selinux/jobs/442750208

Build fail with gcc:

policydb.c:2839:31: error: comparison is always false due to limited range of data type [-Werror=type-limits]

      if (c->u.ibpkey.low_pkey  > 0xffff ||
                                ^
policydb.c:2840:31: error: comparison is always false due to limited
range of data type [-Werror=type-limits]
          c->u.ibpkey.high_pkey > 0xffff)
#8 lore raw
In reply to William Roberts
Quoted 23 lines · William Roberts
Quoted 22 lines · Ondrej Mosnacek

We need to convert from little-endian before dong range checks on the ibpkey port numbers, otherwise we would be checking a wrong value.

Fixes 9fbb3112769a ("libsepol: Add ibpkey ocontext handling")
---
 libsepol/src/policydb.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +1 -1
index a6d76ca3..dc201e2f 100644
@@ -2830,15 +2830,21 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,
                                break;
                        case OCON_IBPKEY:
                                rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
-                               if (rc < 0 || buf[2] > 0xffff || buf[3] > 0xffff)
+                               if (rc < 0)
                                        return -1;
 
Quoted 5 lines

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

Quoted 2 lines
libsepol/src/policydb.c +2 -0
+                               c->u.ibpkey.low_pkey  = le32_to_cpu(buf[2]);
+                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);

Ahh you're assigning a 32 bit value to a 16 bit variable low|high_pkey. I think you need to assign them to a uint32_t if you want to actually range check them.

Oh right, I didn't realize those fields are 16-bit... Let me fix it and re-spin.

Quoted 31 lines
Quoted 18 lines
libsepol/src/policydb.c +7 -4
+
+                               if (c->u.ibpkey.low_pkey  > 0xffff ||
+                                   c->u.ibpkey.high_pkey > 0xffff)
+                                       return -1;
+
+                               /* we want c->u.ibpkey.subnet_prefix in network
+                                * (big-endian) order, just memcpy it */
                                memcpy(&c->u.ibpkey.subnet_prefix, buf,
                                       sizeof(c->u.ibpkey.subnet_prefix));
 
-                               c->u.ibpkey.low_pkey = le32_to_cpu(buf[2]);
-                               c->u.ibpkey.high_pkey = le32_to_cpu(buf[3]);
-
                                if (context_read_and_validate
                                    (&c->context[0], p, fp))
                                        return -1;
--
Quoted 2 lines

2.17.2

See job: https://travis-ci.org/SELinuxProject/selinux/jobs/442750208

Build fail with gcc:

policydb.c:2839:31: error: comparison is always false due to limited range of data type [-Werror=type-limits]

     if (c->u.ibpkey.low_pkey  > 0xffff ||
                               ^
policydb.c:2840:31: error: comparison is always false due to limited
range of data type [-Werror=type-limits]
         c->u.ibpkey.high_pkey > 0xffff)
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Associate Software Engineer, Security Technologies
Red Hat, Inc.