Skip to contentSkip to conversation

dispol: display operations as ranges

PATCH Applied Jeff Vander Stoep 2 participants 2 messages Apr 27, 2015 – Apr 28, 2015 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dispol: display operations as ranges

Displays operations ranges more concisely. E.g.

{ 0x8901-0x8930 }

instead of

{ 0x8901 0x8902 0x8903 0x8904 80x8905 0x0806 ... 0x8930 }

---
 checkpolicy/test/dispol.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
checkpolicy/test/dispol.c +30 -3
index 133759f..157361a 100644
@@ -55,23 +55,50 @@ int render_access_mask(uint32_t mask, avtab_key_t * key, policydb_t * p,
 }
 
 #define operation_perm_test(x, p) (1 & (p[x >> 5] >> (x & 0x1f)))
+#define next_bit_in_range(i, p) \
+	((i + 1 < sizeof(p)*8) && operation_perm_test((i + 1), p))
 
 int render_operations(avtab_operations_t *ops, avtab_key_t * key, FILE * fp)
 {
 	uint16_t value;
-	unsigned int bit = 0;
+	uint16_t low_bit;
+	uint16_t low_value;
+	unsigned int bit;
+	unsigned int in_range = 0;
 
 	fprintf(fp, "{ ");
 	for (bit = 0; bit < sizeof(ops->perms)*8; bit++) {
 		if (!operation_perm_test(bit, ops->perms))
 			continue;
+
+		if (in_range && next_bit_in_range(bit, ops->perms)) {
+			/* continue until high value found */
+			continue;
+		} else if (next_bit_in_range(bit, ops->perms)) {
+			/* low value */
+			low_bit = bit;
+			in_range = 1;
+			continue;
+		}
+
 		if (key->specified & AVTAB_OPNUM) {
 			value = ops->type<<8 | bit;
-			fprintf(fp, "0x%hx ", value);
+			low_value = ops->type<<8 | low_bit;
+			if (in_range)
+				fprintf(fp, "0x%hx-0x%hx ", low_value, value);
+			else
+				fprintf(fp, "0x%hx ", value);
 		} else if (key->specified & AVTAB_OPTYPE) {
 			value = bit << 8;
-			fprintf(fp, "0x%hx-0x%hx ", value, value|0xff);
+			low_value = low_bit << 8;
+			if (in_range)
+				fprintf(fp, "0x%hx-0x%hx ", low_value, (uint16_t) (value|0xff));
+			else
+				fprintf(fp, "0x%hx-0x%hx ", value, (uint16_t) (value|0xff));
+
 		}
+		if (in_range)
+			in_range = 0;
 	}
 	fprintf(fp, "}");
 	return 0;
Signature · 1 line
2.2.0.rc0.207.ga3a616c
#1 lore raw
Quoted 10 lines · Jeff Vander Stoep

Displays operations ranges more concisely. E.g.

{ 0x8901-0x8930 }

instead of

{ 0x8901 0x8902 0x8903 0x8904 80x8905 0x0806 ... 0x8930 }

---
 checkpolicy/test/dispol.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

Thanks, applied.

Quoted 60 lines
checkpolicy/test/dispol.c +30 -3
index 133759f..157361a 100644
@@ -55,23 +55,50 @@ int render_access_mask(uint32_t mask, avtab_key_t * key, policydb_t * p,
 }
 
 #define operation_perm_test(x, p) (1 & (p[x >> 5] >> (x & 0x1f)))
+#define next_bit_in_range(i, p) \
+	((i + 1 < sizeof(p)*8) && operation_perm_test((i + 1), p))
 
 int render_operations(avtab_operations_t *ops, avtab_key_t * key, FILE * fp)
 {
 	uint16_t value;
-	unsigned int bit = 0;
+	uint16_t low_bit;
+	uint16_t low_value;
+	unsigned int bit;
+	unsigned int in_range = 0;
 
 	fprintf(fp, "{ ");
 	for (bit = 0; bit < sizeof(ops->perms)*8; bit++) {
 		if (!operation_perm_test(bit, ops->perms))
 			continue;
+
+		if (in_range && next_bit_in_range(bit, ops->perms)) {
+			/* continue until high value found */
+			continue;
+		} else if (next_bit_in_range(bit, ops->perms)) {
+			/* low value */
+			low_bit = bit;
+			in_range = 1;
+			continue;
+		}
+
 		if (key->specified & AVTAB_OPNUM) {
 			value = ops->type<<8 | bit;
-			fprintf(fp, "0x%hx ", value);
+			low_value = ops->type<<8 | low_bit;
+			if (in_range)
+				fprintf(fp, "0x%hx-0x%hx ", low_value, value);
+			else
+				fprintf(fp, "0x%hx ", value);
 		} else if (key->specified & AVTAB_OPTYPE) {
 			value = bit << 8;
-			fprintf(fp, "0x%hx-0x%hx ", value, value|0xff);
+			low_value = low_bit << 8;
+			if (in_range)
+				fprintf(fp, "0x%hx-0x%hx ", low_value, (uint16_t) (value|0xff));
+			else
+				fprintf(fp, "0x%hx-0x%hx ", value, (uint16_t) (value|0xff));
+
 		}
+		if (in_range)
+			in_range = 0;
 	}
 	fprintf(fp, "}");
 	return 0;