Skip to contentSkip to conversation

mcstrans: fix off-by-one in MAX_CATS / maxbit comparisons

PATCH In review Stephen Smalley 1 participant 4 messages May 28 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/2 mcstrans: fix off-by-one in MAX_CATS / maxbit comparisons

For a given category name cN, N must be strictly less than MAX_CATS and maxbit, e.g. if MAX_CATS is 1024, the last category is c1023.

---
 mcstrans/src/mcstrans.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mcstrans/src/mcstrans.c +4 -4
index ee99a19e..ff33621c 100644
@@ -192,18 +192,18 @@ parse_category(ebitmap_t *e, const char *raw, bool allowinverse,
 		}
 		if (sscanf(raw,"c%u", &low) != 1)
 			return -1;
-		if (low > MAX_CATS)
+		if (low >= MAX_CATS)
 			return -1;
-		if (!fromconfig && low > maxbit)
+		if (!fromconfig && low >= maxbit)
 			return -1;
 		raw += numdigits(low) + 1;
 		if (*raw == '.') {
 			raw++;
 			if (sscanf(raw,"c%u", &high) != 1)
 				return -1;
-			if (high > MAX_CATS)
+			if (high >= MAX_CATS)
 				return -1;
-			if (!fromconfig && high > maxbit)
+			if (!fromconfig && high >= maxbit)
 				return -1;
 			raw += numdigits(high) + 1;
 		} else {
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 2/2 mcstrans: cap the cache size

Cap the cache size and do not fail with an error if we are unable to cache a given translation.

---
 mcstrans/src/mcstrans.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
mcstrans/src/mcstrans.c +11 -1
index ff33621c..9e49ab7f 100644
@@ -38,6 +38,8 @@
 #include "mcstrans.h"
 
 #define N_BUCKETS 1453
+#define MAX_CACHE_ENTRIES 65536
+static unsigned int cache_entries;
 
 #define log_error(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
 
@@ -648,7 +650,15 @@ add_base_classification(domain_t *domain, char *raw, char *trans) {
 
 static int
 add_cache(domain_t *domain, char *raw, char *trans) {
-	context_map_t *map = calloc(1, sizeof(context_map_t));
+	context_map_t *map;
+
+	if (cache_entries > MAX_CACHE_ENTRIES) {
+		log_debug(" add_cache: cache full, not caching (%s, %s)\n",
+			raw, trans);
+		return 0;
+	}
+
+	map = calloc(1, sizeof(context_map_t));
 	if (!map) goto err;
 
 	map->raw = strdup(raw);
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

Re: [PATCH 2/2] mcstrans: cap the cache size

Quoted 8 lines · Stephen Smalley

Cap the cache size and do not fail with an error if we are unable to cache a given translation.

---
 mcstrans/src/mcstrans.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Sorry, disregard this patch - wasn't quite ready.

Quoted 34 lines
mcstrans/src/mcstrans.c +11 -2
index ff33621c..9e49ab7f 100644
@@ -38,6 +38,8 @@
 #include "mcstrans.h"
 
 #define N_BUCKETS 1453
+#define MAX_CACHE_ENTRIES 65536
+static unsigned int cache_entries;
 
 #define log_error(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
 
@@ -648,7 +650,15 @@ add_base_classification(domain_t *domain, char *raw, char *trans) {
 
 static int
 add_cache(domain_t *domain, char *raw, char *trans) {
-       context_map_t *map = calloc(1, sizeof(context_map_t));
+       context_map_t *map;
+
+       if (cache_entries > MAX_CACHE_ENTRIES) {
+               log_debug(" add_cache: cache full, not caching (%s, %s)\n",
+                       raw, trans);
+               return 0;
+       }
+
+       map = calloc(1, sizeof(context_map_t));
        if (!map) goto err;
 
        map->raw = strdup(raw);
--

2.54.0

author maintainer
#3 lore raw
In reply to Stephen Smalley
Quoted 6 lines · Stephen Smalley

For a given category name cN, N must be strictly less than MAX_CATS and maxbit, e.g. if MAX_CATS is 1024, the last category is c1023.

Merged.

Quoted 34 lines
---
 mcstrans/src/mcstrans.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mcstrans/src/mcstrans.c +4 -5
index ee99a19e..ff33621c 100644
@@ -192,18 +192,18 @@ parse_category(ebitmap_t *e, const char *raw, bool allowinverse,
                }
                if (sscanf(raw,"c%u", &low) != 1)
                        return -1;
-               if (low > MAX_CATS)
+               if (low >= MAX_CATS)
                        return -1;
-               if (!fromconfig && low > maxbit)
+               if (!fromconfig && low >= maxbit)
                        return -1;
                raw += numdigits(low) + 1;
                if (*raw == '.') {
                        raw++;
                        if (sscanf(raw,"c%u", &high) != 1)
                                return -1;
-                       if (high > MAX_CATS)
+                       if (high >= MAX_CATS)
                                return -1;
-                       if (!fromconfig && high > maxbit)
+                       if (!fromconfig && high >= maxbit)
                                return -1;
                        raw += numdigits(high) + 1;
                } else {
--

2.54.0