Skip to contentSkip to conversation

libsepol: Fix index off-by-one errors for categories

PATCH In review James Carter 2 participants 3 messages May 21 – May 22 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: Fix index off-by-one errors for categories

The category bitmap for a MLS level or range start at 0 as does the p_cat_val_to_name array, so just the bit position should be used without subtracting one from it.

---
 libsepol/src/mls.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
libsepol/src/mls.c +6 -8
index a37405d1..7f53c61a 100644
@@ -138,14 +138,14 @@ int mls_compute_context_len(const policydb_t * policydb,
 				if (range > 1)
 					len +=
 					    strlen(policydb->
-						   p_cat_val_to_name[i - 1]) +
+						   p_cat_val_to_name[i]) +
 					    1;
 				range = 0;
 			}
 		}
 		/* Handle case where last category is the end of range */
 		if (range > 1)
-			len += strlen(policydb->p_cat_val_to_name[i - 1]) + 1;
+			len += strlen(policydb->p_cat_val_to_name[i]) + 1;
 
 		if (l == 0) {
 			if (mls_level_eq(&context->range.level[0],
@@ -216,11 +216,9 @@ void mls_sid_to_context(const policydb_t * policydb,
 						*scontextp++ = ',';
 
 					strcpy(scontextp,
-					       policydb->p_cat_val_to_name[i -
-									   1]);
+					       policydb->p_cat_val_to_name[i]);
 					scontextp +=
-					    strlen(policydb->
-						   p_cat_val_to_name[i - 1]);
+					    strlen(policydb->p_cat_val_to_name[i]);
 				}
 				range = 0;
 			}
@@ -232,8 +230,8 @@ void mls_sid_to_context(const policydb_t * policydb,
 			else
 				*scontextp++ = ',';
 
-			strcpy(scontextp, policydb->p_cat_val_to_name[i - 1]);
-			scontextp += strlen(policydb->p_cat_val_to_name[i - 1]);
+			strcpy(scontextp, policydb->p_cat_val_to_name[i]);
+			scontextp += strlen(policydb->p_cat_val_to_name[i]);
 		}
 
 		if (l == 0) {
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 15 lines · James Carter

The category bitmap for a MLS level or range start at 0 as does the p_cat_val_to_name array, so just the bit position should be used without subtracting one from it.

---
 libsepol/src/mls.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
libsepol/src/mls.c +0 -0
index a37405d1..7f53c61a 100644

libsepol/src/mls.c int mls_compute_context_len(const policydb_t * policydb, · line 141

Quoted 5 lines
libsepol/src/mls.c +1 -1
@@ -138,14 +138,14 @@ int mls_compute_context_len(const policydb_t * policydb,
                                if (range > 1)
                                        len +=
                                            strlen(policydb->
-                                                  p_cat_val_to_name[i - 1]) +
+                                                  p_cat_val_to_name[i]) +

FTR, this and possibly the later uses of i - 1 are intentional to reflect the last set category in the range.
May want to compare with the kernel code and synchronize them as appropriate.

Quoted 40 lines
libsepol/src/mls.c +5 -8
                                            1;
                                range = 0;
                        }
                }
                /* Handle case where last category is the end of range */
                if (range > 1)
-                       len += strlen(policydb->p_cat_val_to_name[i - 1]) + 1;
+                       len += strlen(policydb->p_cat_val_to_name[i]) + 1;
 
                if (l == 0) {
                        if (mls_level_eq(&context->range.level[0],
@@ -216,11 +216,9 @@ void mls_sid_to_context(const policydb_t * policydb,
                                                *scontextp++ = ',';
 
                                        strcpy(scontextp,
-                                              policydb->p_cat_val_to_name[i -
-                                                                          1]);
+                                              policydb->p_cat_val_to_name[i]);
                                        scontextp +=
-                                           strlen(policydb->
-                                                  p_cat_val_to_name[i - 1]);
+                                           strlen(policydb->p_cat_val_to_name[i]);
                                }
                                range = 0;
                        }
@@ -232,8 +230,8 @@ void mls_sid_to_context(const policydb_t * policydb,
                        else
                                *scontextp++ = ',';
 
-                       strcpy(scontextp, policydb->p_cat_val_to_name[i - 1]);
-                       scontextp += strlen(policydb->p_cat_val_to_name[i - 1]);
+                       strcpy(scontextp, policydb->p_cat_val_to_name[i]);
+                       scontextp += strlen(policydb->p_cat_val_to_name[i]);
                }
 
                if (l == 0) {
--

2.54.0

#2 lore raw
In reply to James Carter
Quoted 6 lines · James Carter

The category bitmap for a MLS level or range start at 0 as does the p_cat_val_to_name array, so just the bit position should be used without subtracting one from it.

Ignore this patch. I was confused. This code is collapsing category sets and actually wants to refer to the previous category, so it is correct.

Jim

Quoted 53 lines
---
 libsepol/src/mls.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
libsepol/src/mls.c +6 -9
index a37405d1..7f53c61a 100644
@@ -138,14 +138,14 @@ int mls_compute_context_len(const policydb_t * policydb,
                                if (range > 1)
                                        len +=
                                            strlen(policydb->
-                                                  p_cat_val_to_name[i - 1]) +
+                                                  p_cat_val_to_name[i]) +
                                            1;
                                range = 0;
                        }
                }
                /* Handle case where last category is the end of range */
                if (range > 1)
-                       len += strlen(policydb->p_cat_val_to_name[i - 1]) + 1;
+                       len += strlen(policydb->p_cat_val_to_name[i]) + 1;
 
                if (l == 0) {
                        if (mls_level_eq(&context->range.level[0],
@@ -216,11 +216,9 @@ void mls_sid_to_context(const policydb_t * policydb,
                                                *scontextp++ = ',';
 
                                        strcpy(scontextp,
-                                              policydb->p_cat_val_to_name[i -
-                                                                          1]);
+                                              policydb->p_cat_val_to_name[i]);
                                        scontextp +=
-                                           strlen(policydb->
-                                                  p_cat_val_to_name[i - 1]);
+                                           strlen(policydb->p_cat_val_to_name[i]);
                                }
                                range = 0;
                        }
@@ -232,8 +230,8 @@ void mls_sid_to_context(const policydb_t * policydb,
                        else
                                *scontextp++ = ',';
 
-                       strcpy(scontextp, policydb->p_cat_val_to_name[i - 1]);
-                       scontextp += strlen(policydb->p_cat_val_to_name[i - 1]);
+                       strcpy(scontextp, policydb->p_cat_val_to_name[i]);
+                       scontextp += strlen(policydb->p_cat_val_to_name[i]);
                }
 
                if (l == 0) {
--

2.54.0