Skip to contentSkip to conversation

- use common av_to_string implementation

PATCH In review Joshua Brindle 2 participants 2 messages Jan 31, 2006 – Feb 1, 2006 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 - use common av_to_string implementation

While debugging the last problem I found that dispol/dismod av rendering was not working correctly. Since we moved av_to_string into libsepol (statically) a while back for assertions we should use it for apps like dispol/dismod that link statically against libsepol. This changes it to sepol_av_to_string, moves it to util.c and removes the redundant code from dismod and dispol.

test/dismod.c +7 -45
diff -purN -x.svn checkpolicy/test/dismod.c checkpolicy/test/dismod.c
@@ -60,62 +60,24 @@ void usage(char *progname)
 	exit(1);
 }
 
-/* borrowed from checkpolicy.c */
-static int find_perm(hashtab_key_t key, hashtab_datum_t datum, void *p)
-{
-	unsigned int *valuep;
-	perm_datum_t *perdatum;
-
-	valuep = (unsigned int *) p;
-	perdatum = (perm_datum_t *) datum;
-
-	if (*valuep == perdatum->value)
-		return (int) key;
-
-	return 0;
-}
-
 static void render_access_mask(uint32_t mask, uint32_t class, policydb_t *p, FILE *fp)
 {
-	unsigned int i;
-	class_datum_t *cladatum;
 	char *perm;
-	cladatum = p->class_val_to_struct[class - 1];
 	fprintf(fp, "{");
-	for (i = 1; i <= sizeof(mask) * 8; i++) {
-		if (mask & (1 << (i - 1))) {
-			perm = (char *) hashtab_map(cladatum->permissions.table,
-				  find_perm, &i);
-
-			if (!perm && cladatum->comdatum) {
-				perm = (char *) hashtab_map(cladatum->comdatum->permissions.table,
-				  find_perm, &i);
-			}
-			if (perm)
-				fprintf(fp, " %s", perm);
-		}
-	}
-	fprintf(fp, " }");
+	perm = sepol_av_to_string(p, class, mask);
+	if (perm)
+		fprintf(fp, "%s ", perm);
+	fprintf(fp, "}");
 }
 
 static void render_access_bitmap(ebitmap_t *map, uint32_t class, policydb_t *p, FILE *fp)
 {
 	unsigned int i;
-        uint32_t perm_value;
-	class_datum_t *cladatum;
 	char *perm;
-	cladatum = p->class_val_to_struct[class - 1];
 	fprintf(fp, "{");
         for (i = ebitmap_startbit(map); i < ebitmap_length(map); i++) {
                 if (ebitmap_get_bit(map, i)) {
-                        perm_value = i + 1;
-			perm = (char *) hashtab_map(cladatum->permissions.table,
-				  find_perm, &perm_value);
-
-			if (!perm && cladatum->comdatum) {
-				perm = (char *) hashtab_map(cladatum->comdatum->permissions.table,
-				  find_perm, &perm_value);
-			}
+			perm = sepol_av_to_string(p, class, 1 << i);
 			if (perm)
 				fprintf(fp, " %s", perm);
 		}
@@ -303,11 +265,11 @@ int display_avrule(avrule_t *avrule, uin
 		fprintf(fp, " }");
 	fprintf(fp, " ");
 
-	if( avrule->specified & AVRULE_AV) {
+	if( avrule->specified & (AVRULE_AV | AVRULE_NEVERALLOW)) { 
 		render_access_mask(avrule->perms->data, avrule->perms->class, policy, fp);
 	} else if ( avrule->specified & AVRULE_TYPE) {
                 display_id(policy, fp, SYM_TYPES, avrule->perms->data - 1, "");
-	}
+	} 
 
 	fprintf(fp, ";\n");
 
test/dispol.c +4 -32
diff -purN -x.svn checkpolicy/test/dispol.c checkpolicy/test/dispol.c
@@ -41,42 +41,14 @@ void usage(char *progname)
 	exit(1);
 }
 
-/* borrowed from checkpolicy.c */
-static int find_perm(hashtab_key_t key, hashtab_datum_t datum, void *p)
-{
-	unsigned int *valuep;
-	perm_datum_t *perdatum;
-
-	valuep = (unsigned int *) p;
-	perdatum = (perm_datum_t *) datum;
-
-	if (*valuep == perdatum->value)
-		return (int) key;
-
-	return 0;
-}
-
 int render_access_mask(uint32_t mask, avtab_key_t *key, policydb_t *p, FILE *fp)
 {
-	unsigned int i;
-	class_datum_t *cladatum;
 	char *perm;
-	cladatum = p->class_val_to_struct[key->target_class -1];
 	fprintf(fp, "{");
-	for (i = 1; i <= sizeof(mask) * 8; i++) {
-		if (mask & (1 << (i - 1))) {
-			perm = (char *) hashtab_map(cladatum->permissions.table,
-				  find_perm, &i);
-
-			if (!perm && cladatum->comdatum) {
-				perm = (char *) hashtab_map(cladatum->comdatum->permissions.table,
-				  find_perm, &i);
-			}
-			if (perm)
-				fprintf(fp, " %s", perm);
-		}
-	}
-	fprintf(fp, " }");
+	perm = sepol_av_to_string(p, key->target_class, mask);
+	if (perm)
+		fprintf(fp, "%s ", perm);
+	fprintf(fp, "}");
 	return 0;
 }
 
include/sepol/policydb/policydb.h +3 -0
diff -purN -x.svn libsepol/include/sepol/policydb/policydb.h libsepol/include/sepol/policydb/policydb.h
@@ -509,6 +509,9 @@ extern int symtab_insert(policydb_t *x, 
                   uint32_t scope, uint32_t avrule_decl_id,
                   uint32_t *value);
 
+extern char *sepol_av_to_string(policydb_t *policydbp, uint32_t tclass, 
+			 sepol_access_vector_t av);
+
 /* A policy "file" may be a memory region referenced by a (data, len) pair
    or a file referenced by a FILE pointer. */
 typedef struct policy_file {
src/assertion.c +1 -60

73 lines not on this page · raw message

src/util.c +56 -0

65 lines not on this page · raw message

#1 lore raw
Quoted 6 lines · Joshua Brindle

While debugging the last problem I found that dispol/dismod av rendering was not working correctly. Since we moved av_to_string into libsepol (statically) a while back for assertions we should use it for apps like dispol/dismod that link statically against libsepol. This changes it to sepol_av_to_string, moves it to util.c and removes the redundant code from dismod and dispol.

Thanks, merged as of libsepol 1.11.11 and checkpolicy 1.29.1 with one minor modification - removed useless static keyword on struct definition (wasn't in the original code being moved, and generated a compiler warning).

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.