diff --git a/tools/sepolicy-analyze/README b/tools/sepolicy-analyze/README index fdee588e1fb9aa08fb5e39900e381121bf8c92ab..c6657ec4494a7cf1eeeb6efab2dbdae250a6b01c 100644 --- a/tools/sepolicy-analyze/README +++ b/tools/sepolicy-analyze/README @@ -69,6 +69,10 @@ sepolicy-analyze Displays the attributes associated with the specified type name. + sepolicy-analyze out/target/product/<board>/root/sepolicy attribute -l + + Displays all attributes in the policy. + NEVERALLOW CHECKING (neverallow) sepolicy-analyze out/target/product/<board>/root/sepolicy neverallow \ [-w] [-d] [-f neverallows.conf] | [-n "neverallow string"] diff --git a/tools/sepolicy-analyze/attribute.c b/tools/sepolicy-analyze/attribute.c index ae98aa98c435523a598ff67f8182af031778d2d0..f7c9b4c80e6aefb40cac0001181f8ab7ee0aa80a 100644 --- a/tools/sepolicy-analyze/attribute.c +++ b/tools/sepolicy-analyze/attribute.c @@ -3,7 +3,7 @@ #include "attribute.h" void attribute_usage() { - fprintf(stderr, "\tattribute <name> [-r|--reverse]\n"); + fprintf(stderr, "\tattribute [-l|--list] [-r|--reverse] <name>\n"); } static void retrieve_mapping(policydb_t *policydb, struct type_datum *dat, char *name, int reverse) { @@ -53,29 +53,58 @@ static int list_attribute(policydb_t *policydb, char *name, int reverse) return 0; } +static int print_attr(__attribute__ ((unused)) hashtab_key_t k, + hashtab_datum_t d, void *args) { + struct type_datum *dat = (struct type_datum *)d; + policydb_t *pdb = (policydb_t *)args; + if (!dat) { + fprintf(stderr, "type encountered without datum!\n"); + return -1; + } + if (dat->flavor == TYPE_ATTRIB) { + printf("%s\n", pdb->p_type_val_to_name[dat->s.value - 1]); + } + return 0; +} + +static int list_all_attributes(policydb_t *policydb) { + return hashtab_map(policydb->p_types.table, print_attr, policydb); +} + int attribute_func (int argc, char **argv, policydb_t *policydb) { + int rc = -1; + int list = 0; int reverse = 0; char ch; struct option attribute_options[] = { + {"list", no_argument, NULL, 'l'}, {"reverse", no_argument, NULL, 'r'}, {NULL, 0, NULL, 0} }; - while ((ch = getopt_long(argc, argv, "r", attribute_options, NULL)) != -1) { + while ((ch = getopt_long(argc, argv, "lr", attribute_options, NULL)) != -1) { switch (ch) { + case 'l': + list = 1; + break; case 'r': reverse = 1; break; default: USAGE_ERROR = true; - return -1; + goto out; } } - if (argc != 2 && !(reverse && argc == 3)) { + if ((argc != 2 && !(reverse && argc == 3)) || (list && reverse)) { USAGE_ERROR = true; - return -1; + goto out; } - return list_attribute(policydb, argv[optind], reverse); + if (list) + rc = list_all_attributes(policydb); + else + rc = list_attribute(policydb, argv[optind], reverse); + out: + return rc; }