Skip to contentSkip to conversation

libsepol: add public API for policy introspection

RFC PATCH In review jan janasek 3 participants 26 messages Oct 6 – Oct 8 SELinux security module

lore
Share & tools
lore
#0 lore raw

cover libsepol: add public API for policy introspection

Hi all,

This series adds a public, stable libsepol API for read-only introspection of a loaded/compiled policydb: iterators and accessors for types, roles, users, classes/permissions, booleans, conditionals, AV rules, MLS/category sets, contexts, ocontexts (initial SIDs, fs/port/node/... contexts), filename transitions, range transitions, role rules, permissive types and policy capabilities, plus a small "record" layer that snapshots the above into owned, independently-freeable structures.

This is the first time this series is posted to the list; it was developed and self-reviewed out-of-tree against an older base and has just been rebased onto current upstream/main before this posting (see "Testing" below for what that rebase involved).

Posting this as RFC because it adds a sizeable new public ABI surface and I would like design-level feedback before going through a full review cycle. Specifically:

- Is libsepol the right place for this, or would a separate library be preferable?
- Does the record/iterator split (snapshot "record" structs vs. zero-copy iterators) make sense as a general shape for this kind of API, and are the naming conventions reasonable?
- Is the libsepol.map.in symbol-versioning approach in patch 17 acceptable for a surface this size?
- Is landing this as one 17-patch series reasonable, or would maintainers prefer it split into smaller, independently mergeable increments (e.g. per policy-object family)?

Motivation
----------
SETools (https://github.com/SELinuxProject/setools) and other policy-analysis tools currently reach into libsepol's internal, unstable headers (policydb.h, hashtab.h, ebitmap.h, ...) to walk a loaded policydb, because there is no supported public way to do this. That couples those tools tightly to libsepol's internal data structures and breaks whenever those structures change. This series gives such consumers a real, documented, ABI-stable surface instead, without exposing anything from policydb.h directly.

Series organization
--------------------
The 17 patches are organized bottom-up and are individually bisectable (each one builds and passes its own tests standalone):

- Patches 1-2 add shared iterator/utility infrastructure (hashtab iteration, string-list helpers, MLS/category-set and common permission iterators) with no public API surface yet.
- Patches 3-10 add the public "record" and iterator APIs themselves, one policy-object family at a time (types, users, roles, classes, policy properties/constants, context resolution), each paired with its own test coverage.
- Patches 11-14 add the remaining rule/ocontext-level iterators (AV rules, conditionals, booleans, ocontexts, filename transitions, range transitions, role rules, permissive types, polcaps) and their tests.
- Patches 15-16 add broader record-API and tier-2 integration tests that exercise the APIs together against realistic policies.
- Patch 17 wires everything into libsepol.map.in and adds the umbrella <sepol/sepol.h> include, so the new symbols are only exported for real once the whole surface exists.

Testing
-------
- Full CUnit suite: 134 tests, 856142 assertions, 133 passed. The one remaining failure (test-downgrade.c, MLS policy downgrade) is a pre-existing, environment-specific issue reproduced identically against an unmodified upstream/main checkout and is unrelated to this series.
- Every commit in the series was verified to build standalone (via isolated git archive extraction + build), confirming bisectability is preserved end to end, including after rebasing onto current upstream/main.
- While rebasing onto upstream/main, upstream commit "libsepol: drop support for policy versions before xperms_ioctl/xen_devicetree" removed POLICYDB_VERSION_CONSTRAINT_NAMES, which patch 8 (class iterator) referenced for a version-gated fallback in constraint_expr_datum_to_record(). That fallback is now dead code (type_names is unconditionally populated by constraint_expr_init()/type_set_read()/type_set_write() for every policy version libsepol still supports), so the gate and its now-unreachable test were removed as part of the rebase.
- A handful of real bugs found during self-review were fixed and folded back into the commits that introduced them (not left as separate fixup patches), to keep each commit buildable and correct on its own:
- libsepol: add public user iterator and accessors -- modules adding a sepol_user_t via the new API were not registered in policydb->scope[SYM_USERS], so link_modules()/expand_module() would silently drop them.
- libsepol: add context resolution from policy values and SIDs -- policydb_sid_lookup_context() rejected a NULL sepol_handle_t, inconsistent with its sibling functions, which all tolerate it via the internal ERR()/msg_write() NULL-handle fallback.
- libsepol: add public role record structure and iterator -- an ebitmap allocated by type_set_expand() was leaked on one error path; separately, the role flavor conversion switch had no default case, so a role_datum->flavor value outside {ROLE_ROLE, ROLE_ATTRIB} (read unvalidated from the binary policy) would silently report the zero-initialized ROLE_ROLE default instead of being rejected.
- libsepol: add tier-2 integration tests -- filename_trans iteration had no test coverage; added one.
- Code was run through clang-format-diff (-style=file) restricted to the lines each commit actually touches, to avoid reformatting large unrelated pre-existing regions of incrementally-extended files (policydb.h, ebitmap.h, hashtab.h, ...); verified token-for-token equivalent to the pre-formatting tree modulo whitespace/line-wrapping.
- Ran clang-tidy (bugprone-*/cert-*/clang-analyzer-* checks) over every new source file; findings were either pre-existing house style shared with the rest of libsepol (e.g. the _SEPOL_*_H_ include-guard naming, or assigning malloc/realloc's void* result straight to a typed pointer) or the role-flavor default-case gap above, which is now fixed.
- Ran the full suite under GCC's AddressSanitizer+UndefinedBehavior- Sanitizer (-fsanitize=address,undefined). This caught a real, if minor, UB issue in 5 of the new test files (test-iter-types.c, test-iter-roles.c x2, test-iter-users.c, test-iter-classes.c):
qsort() is declared with a nonnull base argument, but several tests called it unconditionally on a get_*() output array that is legitimately NULL when the count is 0, which is UB per the standard even though nmemb==0 makes it harmless in practice on every libc tested. Guarded each call with nmemb > 0. No AddressSanitizer/UBSan findings remain (0 leaks, 0 runtime errors) after the fix.
- git send-email --dry-run against this series flagged 3 non-ASCII em-dash characters left over in comments (classes.c, av_rule_internal.h, policydb.h); replaced with plain ASCII hyphens for consistency with the rest of this (pure-ASCII) code base.

AI assistance disclosure
-------------------------
Portions of this series (notably test scaffolding and some iterator boilerplate) were drafted with assistance from Cursor
(https://cursor.com), an AI coding tool, under my direction and with my review. All code was read, tested, and is submitted under my own Signed-off-by. Per current kernel-adjacent convention for AI assistance, affected commits carry an "Assisted-by: LLM Cursor" trailer rather than a Co-authored-by/Signed-off-by for the tool itself.

Looking forward for review.

jan janasek (17):
libsepol: add hashtab iterator and string list utilities
libsepol: add MLS, category set, and common permission iterators
libsepol: add public type record structure and iterator
libsepol: add test infrastructure and type iterator tests
libsepol: add public user iterator and accessors
libsepol: add public role record structure and iterator
libsepol: add public class record structure
libsepol: add public class iterator and tests
libsepol: add policy properties API and public constants
libsepol: add context resolution from policy values and SIDs
libsepol: add AV rule, conditional, and boolean iterators
libsepol: add AV rule and conditional iterator tests
libsepol: add ocontext iterators for initial SIDs and named contexts
libsepol: add filename transition, permissive, polcap, range
transition, and role rule iterators
libsepol: add record API tests for type, role, class, and user
libsepol: add tier-2 integration tests
libsepol: add symbol map entries and umbrella header for new public
API

 libsepol/include/sepol/av_rule.h              |  119 +
 libsepol/include/sepol/booleans.h             |   29 +
 libsepol/include/sepol/cat_set.h              |   40 +
 libsepol/include/sepol/class_record.h         |  253 ++
 libsepol/include/sepol/classes.h              |   69 +
 libsepol/include/sepol/common_perm.h          |   54 +
 libsepol/include/sepol/conditional.h          |  109 +
 libsepol/include/sepol/constants.h            |  231 ++
 libsepol/include/sepol/context.h              |   42 +
 libsepol/include/sepol/filename_trans.h       |   42 +
 libsepol/include/sepol/flask.h                |   34 +
 libsepol/include/sepol/mls_iter.h             |   80 +
 libsepol/include/sepol/ocontext.h             |  513 ++++
 libsepol/include/sepol/permissive.h           |   33 +
 libsepol/include/sepol/polcap.h               |   41 +
 libsepol/include/sepol/policydb.h             |   29 +
 libsepol/include/sepol/policydb/avtab.h       |   33 +-
 libsepol/include/sepol/policydb/ebitmap.h     |   18 +
 libsepol/include/sepol/policydb/flask_types.h |   55 +-
 libsepol/include/sepol/policydb/hashtab.h     |   15 +
 libsepol/include/sepol/policydb/policydb.h    |   68 +-
 libsepol/include/sepol/policydb/services.h    |   19 +-
 libsepol/include/sepol/policydb/util.h        |   22 +-
 libsepol/include/sepol/range_trans.h          |   55 +
 libsepol/include/sepol/role_record.h          |   79 +
 libsepol/include/sepol/role_rule.h            |   41 +
 libsepol/include/sepol/roles.h                |   57 +
 libsepol/include/sepol/sepol.h                |   59 +-
 libsepol/include/sepol/sid_table.h            |   43 +
 libsepol/include/sepol/type_record.h          |   95 +
 libsepol/include/sepol/types.h                |   84 +
 libsepol/include/sepol/users.h                |   65 +-
 libsepol/src/av_rule.c                        |  328 +++
 libsepol/src/av_rule_internal.h               |   60 +
 libsepol/src/boolean_record.c                 |   41 +-
 libsepol/src/booleans.c                       |  174 +-
 libsepol/src/cat_set.c                        |   60 +
 libsepol/src/cat_set_internal.h               |   10 +
 libsepol/src/class_record.c                   |  950 ++++++++
 libsepol/src/classes.c                        |  581 +++++
 libsepol/src/common_perm.c                    |  143 ++
 libsepol/src/conditional_public.c             |  261 ++
 libsepol/src/context.c                        |  279 ++-
 libsepol/src/context_record.c                 |   62 +-
 libsepol/src/filename_trans.c                 |  123 +
 libsepol/src/hashtab.c                        |   48 +
 libsepol/src/initial_sids.h                   |   29 +
 libsepol/src/kernel_to_common.h               |   22 +-
 libsepol/src/libsepol.map.in                  |  379 +++
 libsepol/src/mls_iter.c                       |  154 ++
 libsepol/src/ocontext.c                       | 1058 +++++++++
 libsepol/src/permissive_iter.c                |   65 +
 libsepol/src/polcap_iter.c                    |   67 +
 libsepol/src/policydb_public.c                |  203 ++
 libsepol/src/private.h                        |   23 +
 libsepol/src/range_trans.c                    |  119 +
 libsepol/src/role_record.c                    |  316 +++
 libsepol/src/role_rule.c                      |  117 +
 libsepol/src/roles.c                          |  253 ++
 libsepol/src/services.c                       |   33 +
 libsepol/src/sidtab.c                         |   78 +
 libsepol/src/type_record.c                    |  351 +++
 libsepol/src/types.c                          |  552 +++++
 libsepol/src/user_record.c                    |  100 +-
 libsepol/src/users.c                          |  416 +++-
 libsepol/src/util.c                           |  113 +-
 libsepol/tests/helpers.c                      |    5 +
 libsepol/tests/helpers.h                      |    5 +
 libsepol/tests/libsepol-tests.c               |   10 +
 libsepol/tests/policies/test-iter/iter.conf   |  136 ++
 libsepol/tests/test-av-rule.c                 |  733 ++++++
 libsepol/tests/test-av-rule.h                 |   10 +
 libsepol/tests/test-context-api.c             |  289 +++
 libsepol/tests/test-context-api.h             |   10 +
 libsepol/tests/test-iter-classes.c            |  220 ++
 libsepol/tests/test-iter-classes.h            |    7 +
 libsepol/tests/test-iter-roles.c              |  165 ++
 libsepol/tests/test-iter-roles.h              |    7 +
 libsepol/tests/test-iter-types.c              |  137 ++
 libsepol/tests/test-iter-types.h              |    7 +
 libsepol/tests/test-iter-users.c              |  115 +
 libsepol/tests/test-iter-users.h              |    7 +
 libsepol/tests/test-iter.c                    |  117 +
 libsepol/tests/test-iter.h                    |   10 +
 libsepol/tests/test-records.c                 | 1630 +++++++++++++
 libsepol/tests/test-records.h                 |   10 +
 libsepol/tests/test-tier2.c                   | 2100 +++++++++++++++++
 libsepol/tests/test-tier2.h                   |   10 +
 88 files changed, 15535 insertions(+), 299 deletions(-)
 create mode 100644 libsepol/include/sepol/av_rule.h
 create mode 100644 libsepol/include/sepol/cat_set.h
 create mode 100644 libsepol/include/sepol/class_record.h
 create mode 100644 libsepol/include/sepol/classes.h
 create mode 100644 libsepol/include/sepol/common_perm.h
 create mode 100644 libsepol/include/sepol/conditional.h
 create mode 100644 libsepol/include/sepol/constants.h
 create mode 100644 libsepol/include/sepol/filename_trans.h
 create mode 100644 libsepol/include/sepol/flask.h
 create mode 100644 libsepol/include/sepol/mls_iter.h
 create mode 100644 libsepol/include/sepol/ocontext.h
 create mode 100644 libsepol/include/sepol/permissive.h
 create mode 100644 libsepol/include/sepol/polcap.h
 create mode 100644 libsepol/include/sepol/range_trans.h
 create mode 100644 libsepol/include/sepol/role_record.h
 create mode 100644 libsepol/include/sepol/role_rule.h
 create mode 100644 libsepol/include/sepol/roles.h
 create mode 100644 libsepol/include/sepol/sid_table.h
 create mode 100644 libsepol/include/sepol/type_record.h
 create mode 100644 libsepol/include/sepol/types.h
 create mode 100644 libsepol/src/av_rule.c
 create mode 100644 libsepol/src/av_rule_internal.h
 create mode 100644 libsepol/src/cat_set.c
 create mode 100644 libsepol/src/cat_set_internal.h
 create mode 100644 libsepol/src/class_record.c
 create mode 100644 libsepol/src/classes.c
 create mode 100644 libsepol/src/common_perm.c
 create mode 100644 libsepol/src/conditional_public.c
 create mode 100644 libsepol/src/filename_trans.c
 create mode 100644 libsepol/src/initial_sids.h
 create mode 100644 libsepol/src/mls_iter.c
 create mode 100644 libsepol/src/ocontext.c
 create mode 100644 libsepol/src/permissive_iter.c
 create mode 100644 libsepol/src/polcap_iter.c
 create mode 100644 libsepol/src/range_trans.c
 create mode 100644 libsepol/src/role_record.c
 create mode 100644 libsepol/src/role_rule.c
 create mode 100644 libsepol/src/roles.c
 create mode 100644 libsepol/src/type_record.c
 create mode 100644 libsepol/src/types.c
 create mode 100644 libsepol/tests/policies/test-iter/iter.conf
 create mode 100644 libsepol/tests/test-av-rule.c
 create mode 100644 libsepol/tests/test-av-rule.h
 create mode 100644 libsepol/tests/test-context-api.c
 create mode 100644 libsepol/tests/test-context-api.h
 create mode 100644 libsepol/tests/test-iter-classes.c
 create mode 100644 libsepol/tests/test-iter-classes.h
 create mode 100644 libsepol/tests/test-iter-roles.c
 create mode 100644 libsepol/tests/test-iter-roles.h
 create mode 100644 libsepol/tests/test-iter-types.c
 create mode 100644 libsepol/tests/test-iter-types.h
 create mode 100644 libsepol/tests/test-iter-users.c
 create mode 100644 libsepol/tests/test-iter-users.h
 create mode 100644 libsepol/tests/test-iter.c
 create mode 100644 libsepol/tests/test-iter.h
 create mode 100644 libsepol/tests/test-records.c
 create mode 100644 libsepol/tests/test-records.h
 create mode 100644 libsepol/tests/test-tier2.c
 create mode 100644 libsepol/tests/test-tier2.h
Signature · 1 line
2.55.0
#1 lore raw

RFC PATCH 1/17 libsepol: add hashtab iterator and string list utilities

Add a generic hashtab_iter for walking any policydb symbol table, used as the shared backend for the per-symbol public iterators added in later commits. Add string_list_add_unique() and an ebitmap find-next-set-bit helper shared by several ebitmap-backed iterators.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/policydb/ebitmap.h |  18 ++++
 libsepol/include/sepol/policydb/hashtab.h |  15 +++
 libsepol/include/sepol/policydb/util.h    |  22 ++++-
 libsepol/src/hashtab.c                    |  48 +++++++++
 libsepol/src/private.h                    |  23 +++++
 libsepol/src/util.c                       | 113 +++++++++++++++++++++-
 6 files changed, 236 insertions(+), 3 deletions(-)
libsepol/include/sepol/policydb/ebitmap.h +18 -0
index fd1093b0..0da4e706 100644
@@ -81,6 +81,24 @@ static inline int ebitmap_node_get_bit(const ebitmap_node_t *n,
 	ebitmap_for_each_bit(e, n, bit)          \
 		if (ebitmap_node_get_bit(n, bit))
 
+/*
+ * Resumable version of ebitmap_for_each_positive_bit(): advances the node
+ * and bit position to the next set bit at or after the current bit
+ * (exclusive of highbit), returning 1 if one was found or 0 if the walk
+ * is exhausted. Used by stateful iterators that need to pause/resume the
+ * walk across calls rather than consuming the whole bitmap in one loop.
+ */
+static inline int ebitmap_find_next_set_bit(ebitmap_node_t **n, uint32_t *bit,
+					    uint32_t highbit)
+{
+	while (*bit < highbit) {
+		if (ebitmap_node_get_bit(*n, *bit))
+			return 1;
+		*bit = ebitmap_next(n, *bit);
+	}
+	return 0;
+}
+
 extern int ebitmap_cmp(const ebitmap_t *e1, const ebitmap_t *e2);
 extern int ebitmap_or(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2);
 extern int ebitmap_union(ebitmap_t *dst, const ebitmap_t *e1);
libsepol/include/sepol/policydb/hashtab.h +15 -0
index 124f673e..e641dfac 100644
@@ -110,6 +110,21 @@ extern int hashtab_map(hashtab_t h,
 
 extern void hashtab_hash_eval(hashtab_t h, const char *tag);
 
+struct hashtab_iter {
+	hashtab_t table;
+	hashtab_node_t *curr;
+	unsigned int bucket;
+};
+typedef struct hashtab_iter hashtab_iter_t;
+
+extern void hashtab_iter_init(hashtab_t table, hashtab_iter_t *iter);
+/*
+   Advances to the next item in hashtab, sets both pointers to NULL if no items
+   are left
+ */
+extern void hashtab_iter_next(hashtab_iter_t *iter, hashtab_key_t *key,
+			      hashtab_datum_t *datum);
+
 #ifdef __cplusplus
 }
 #endif
libsepol/include/sepol/policydb/util.h +21 -1
index 03b99a92..6e64247a 100644
@@ -35,7 +35,8 @@ extern char *sepol_av_to_string(const policydb_t *policydbp,
 				sepol_security_class_t tclass,
 				sepol_access_vector_t av);
 
-char *sepol_extended_perms_to_string(const avtab_extended_perms_t *xperms);
+extern char *
+sepol_extended_perms_to_string(const avtab_extended_perms_t *xperms);
 
 /*
  * The tokenize function may be used to
@@ -43,6 +44,25 @@ char *sepol_extended_perms_to_string(const avtab_extended_perms_t *xperms);
  */
 extern int tokenize(const char *line_buf, char delim, int num_args, ...);
 
+extern int string_list_contains(char **const list, uint32_t num,
+				const char *string);
+extern int string_list_add(sepol_handle_t *handle, char ***list, uint32_t *num,
+			   const char *string);
+/*
+ * Like string_list_add(), but a no-op success (STATUS_SUCCESS without
+ * modifying the list) if string already appears in the list.
+ */
+extern int string_list_add_unique(sepol_handle_t *handle, char ***list,
+				  uint32_t *num, const char *string);
+extern int string_list_del(char **list, uint32_t *num, const char *string);
+extern int string_list_scopy(sepol_handle_t *handle, char **list, uint32_t num,
+			     const char ***copy, uint32_t *num_copy);
+
+/*
+ * Compare indirect strings (in qsort)
+ */
+extern int strcmp_qsort(const void *str1, const void *str2);
+
 #ifdef __cplusplus
 }
 #endif
libsepol/src/hashtab.c +48 -0

57 lines not on this page · raw message

libsepol/src/private.h +23 -0
index 7f7344a8..a8eba6d7 100644
@@ -103,6 +103,29 @@ extern size_t put_entry(const void *ptr, size_t size, size_t n,
 			struct policy_file *fp);
 extern int str_read(char **strp, struct policy_file *fp, size_t len);
 
+/*
+ * Generate a sepol_*_value_to_name() implementation: bounds-check "value"
+ * against p->p.NPRIM_FIELD.nprim and index p->p.VAL_TO_NAME_FIELD[value - 1].
+ * All sepol_*_value_to_name() functions share this exact shape, differing
+ * only in which symbol table / reverse-lookup array they use.
+ */
+#define DEFINE_VALUE_TO_NAME(fn, desc, nprim_field, val_to_name_field)   \
+	int fn(sepol_handle_t *handle, const sepol_policydb_t *p,        \
+	       uint32_t value, const char **name)                        \
+	{                                                                \
+		if (!p || !name)                                         \
+			return STATUS_ERR;                               \
+                                                                         \
+		if (value < 1 || value > p->p.nprim_field.nprim) {       \
+			ERR(handle, "invalid " desc " value %u", value); \
+			*name = NULL;                                    \
+			return STATUS_ERR;                               \
+		}                                                        \
+                                                                         \
+		*name = p->p.val_to_name_field[value - 1];               \
+		return STATUS_SUCCESS;                                   \
+	}
+
 #ifndef HAVE_REALLOCARRAY
 static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
 {
libsepol/src/util.c +111 -2

135 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

RFC PATCH 2/17 libsepol: add MLS, category set, and common permission iterators

In reply to jan janasek

Add sepol_cat_set_iter for iterating an ebitmap-backed category set without exposing struct ebitmap. Add sepol_sens_iter/sepol_cat_iter plus sepol_sens_value_to_name()/sepol_cat_value_to_name() for MLS sensitivities and categories. Add sepol_common_iter and sepol_common_value_to_name() for common permission sets shared across classes.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/cat_set.h     |  40 +++++++
 libsepol/include/sepol/common_perm.h |  54 ++++++++++
 libsepol/include/sepol/mls_iter.h    |  80 ++++++++++++++
 libsepol/src/cat_set.c               |  60 +++++++++++
 libsepol/src/cat_set_internal.h      |  10 ++
 libsepol/src/common_perm.c           | 143 +++++++++++++++++++++++++
 libsepol/src/mls_iter.c              | 154 +++++++++++++++++++++++++++
 7 files changed, 541 insertions(+)
 create mode 100644 libsepol/include/sepol/cat_set.h
 create mode 100644 libsepol/include/sepol/common_perm.h
 create mode 100644 libsepol/include/sepol/mls_iter.h
 create mode 100644 libsepol/src/cat_set.c
 create mode 100644 libsepol/src/cat_set_internal.h
 create mode 100644 libsepol/src/common_perm.c
 create mode 100644 libsepol/src/mls_iter.c
libsepol/include/sepol/cat_set.h new file +40 -0
new file mode 100644
index 00000000..dba0575d
@@ -0,0 +1,40 @@
+#ifndef _SEPOL_CAT_SET_H_
+#define _SEPOL_CAT_SET_H_
+
+#include <sepol/handle.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Category set iterators are not created directly. Obtain instances from
+ * MLS-related APIs such as sepol_sens_iter_get_level_cat(),
+ * sepol_isid_iter_get_mls_range_low_cat(), etc.
+ *
+ * Ownership: every function that hands out a sepol_cat_set_iter_t **
+ * (throughout ocontext.h, mls_iter.h, range_trans.h, and users.h) heap
+ * allocates a new, caller-owned iterator whenever it returns 0 with a
+ * non-NULL *cat_iter. The caller must call sepol_cat_set_iter_destroy()
+ * on it exactly once when done -- it is not borrowed from, and does not
+ * alias, any other iterator or the policydb. This applies even when the
+ * underlying category set is empty: *cat_iter is only left NULL on
+ * success when the *outer* iterator argument (e.g. the sepol_isid_iter_t)
+ * is itself NULL or already exhausted, not merely because the category
+ * set has no members -- an empty set still yields a real iterator whose
+ * first sepol_cat_set_iter_next() call reports no values.
+ */
+typedef struct sepol_cat_set_iter sepol_cat_set_iter_t;
+
+extern int sepol_cat_set_iter_next(sepol_handle_t *handle,
+				   sepol_cat_set_iter_t *iter,
+				   uint32_t *cat_value);
+
+extern void sepol_cat_set_iter_destroy(sepol_cat_set_iter_t *iter);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/common_perm.h new file +54 -0

54 lines not on this page · raw message

libsepol/include/sepol/mls_iter.h new file +80 -0

80 lines not on this page · raw message

libsepol/src/cat_set.c new file +60 -0

60 lines not on this page · raw message

libsepol/src/cat_set_internal.h new file +10 -0
new file mode 100644
index 00000000..c8b9df41
@@ -0,0 +1,10 @@
+#ifndef _SEPOL_CAT_SET_INTERNAL_H_
+#define _SEPOL_CAT_SET_INTERNAL_H_
+
+#include <sepol/cat_set.h>
+#include <sepol/policydb/ebitmap.h>
+
+int cat_set_iter_create_from_ebitmap(const ebitmap_t *map,
+				     sepol_cat_set_iter_t **iter);
+
+#endif
libsepol/src/common_perm.c new file +143 -0

143 lines not on this page · raw message

libsepol/src/mls_iter.c new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
2.55.0
maintainer
#3 lore raw

Re: [RFC PATCH userspace 02/17] libsepol: add MLS, category set, and common permission iterators

Quoted 17 lines · jan janasek

Add sepol_cat_set_iter for iterating an ebitmap-backed category set without exposing struct ebitmap. Add sepol_sens_iter/sepol_cat_iter plus sepol_sens_value_to_name()/sepol_cat_value_to_name() for MLS sensitivities and categories. Add sepol_common_iter and sepol_common_value_to_name() for common permission sets shared across classes.

Assisted-by LLM Cursor

---

libsepol/src/common_perm.c new file +0 -0
new file mode 100644
index 00000000..edac7eac

<snip>

libsepol/src/common_perm.c line 123

Quoted 3 lines
libsepol/src/common_perm.c new file +3 -0
+int sepol_common_perm_iter_next(sepol_handle_t *handle __attribute__((unused)),
+                               sepol_common_perm_iter_t *iter,
+                               const char **perm_name)

Adding a value out-parameter would let a caller line it up against bit positions without a second lookup.

#4 lore raw

RFC PATCH 3/17 libsepol: add public type record structure and iterator

In reply to jan janasek

Introduce sepol_type_t with getter/setter API for type name, value, flavor, bounds, and subtypes. Add sepol_type_iter for iterating all types in a policy via the hashtab iterator, plus sepol_type_value_to_name() and sepol_type_query_by_value() for resolving a 1-based type value without touching internal symtab structures.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/type_record.h |  95 +++++
 libsepol/include/sepol/types.h       |  84 ++++
 libsepol/src/type_record.c           | 351 +++++++++++++++++
 libsepol/src/types.c                 | 552 +++++++++++++++++++++++++++
 4 files changed, 1082 insertions(+)
 create mode 100644 libsepol/include/sepol/type_record.h
 create mode 100644 libsepol/include/sepol/types.h
 create mode 100644 libsepol/src/type_record.c
 create mode 100644 libsepol/src/types.c
libsepol/include/sepol/type_record.h new file +95 -0
new file mode 100644
index 00000000..f88de870
@@ -0,0 +1,95 @@
+#ifndef _SEPOL_TYPE_RECORD_H_
+#define _SEPOL_TYPE_RECORD_H_
+
+#include <sepol/handle.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sepol_type;
+typedef struct sepol_type sepol_type_t;
+struct sepol_type_key;
+typedef struct sepol_type_key sepol_type_key_t;
+
+/* Key */
+extern int sepol_type_key_create(sepol_handle_t *handle, const char *name,
+				 sepol_type_key_t **key);
+
+extern void sepol_type_key_unpack(const sepol_type_key_t *key,
+				  const char **name);
+
+extern int sepol_type_key_extract(sepol_handle_t *handle,
+				  const sepol_type_t *type,
+				  sepol_type_key_t **key_ptr);
+
+extern void sepol_type_key_free(sepol_type_key_t *key);
+
+extern int sepol_type_compare(const sepol_type_t *type,
+			      const sepol_type_key_t *key);
+
+extern int sepol_type_compare2(const sepol_type_t *type,
+			       const sepol_type_t *type2);
+
+/* Name */
+extern const char *sepol_type_get_name(const sepol_type_t *type);
+extern int sepol_type_set_name(sepol_handle_t *handle, sepol_type_t *type,
+			       const char *name);
+
+/* Flavor */
+#define SEPOL_TYPE_TYPE 0 /* regular type */
+#define SEPOL_TYPE_ATTRIB 1 /* attribute */
+#define SEPOL_TYPE_ALIAS 2 /* alias */
+
+extern uint32_t sepol_type_get_flavor(const sepol_type_t *type);
+extern int sepol_type_set_flavor(sepol_handle_t *handle, sepol_type_t *type,
+				 uint32_t flavor);
+
+/* Subtypes/Attributes */
+extern int sepol_type_has_subtype(const sepol_type_t *type,
+				  const char *subtype);
+extern int sepol_type_get_subtypes(sepol_handle_t *handle,
+				   const sepol_type_t *type,
+				   const char ***subtypes,
+				   uint32_t *num_subtypes);
+extern int sepol_type_add_subtype(sepol_handle_t *handle, sepol_type_t *type,
+				  const char *subtype);
+extern int sepol_type_del_subtype(sepol_handle_t *handle, sepol_type_t *type,
+				  const char *subtype);
+
+/* Flags */
+#define SEPOL_TYPE_FLAGS_PERMISSIVE (1 << 0)
+#define SEPOL_TYPE_FLAGS_EXPAND_ATTR_TRUE (1 << 1)
+#define SEPOL_TYPE_FLAGS_EXPAND_ATTR_FALSE (1 << 2)
+#define SEPOL_TYPE_FLAGS_EXPAND_ATTR \
+	(SEPOL_TYPE_FLAGS_EXPAND_ATTR_TRUE | SEPOL_TYPE_FLAGS_EXPAND_ATTR_FALSE)
+extern int sepol_type_has_flag(const sepol_type_t *type, uint32_t flag);
+extern int sepol_type_set_flag(sepol_handle_t *handle, sepol_type_t *type,
+			       uint32_t flag);
+extern int sepol_type_unset_flag(sepol_handle_t *handle, sepol_type_t *type,
+				 uint32_t flag);
+
+/* Primary names of aliases */
+extern const char *sepol_type_get_alias_of(const sepol_type_t *type);
+extern int sepol_type_set_alias_of(sepol_handle_t *handle, sepol_type_t *type,
+				   const char *name);
+
+/* Bounds */
+extern const char *sepol_type_get_bounds(const sepol_type_t *type);
+extern int sepol_type_set_bounds(sepol_handle_t *handle, sepol_type_t *type,
+				 const char *bounds);
+
+/* Create/Clone/Destroy */
+extern int sepol_type_create(sepol_handle_t *handle, sepol_type_t **type_ptr);
+
+extern int sepol_type_clone(sepol_handle_t *handle, const sepol_type_t *type,
+			    sepol_type_t **type_ptr);
+
+extern void sepol_type_free(sepol_type_t *type);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/types.h new file +84 -0

84 lines not on this page · raw message

libsepol/src/type_record.c new file +351 -0

351 lines not on this page · raw message

libsepol/src/types.c new file +552 -0

552 lines not on this page · raw message

Signature · 1 line
2.55.0
#5 lore raw

RFC PATCH 4/17 libsepol: add test infrastructure and type iterator tests

In reply to jan janasek

Add a shared test-iter policy fixture and helper routines for tests that need a loaded, expanded source policy, and cover the new type iterator/record API against it.

Assisted-by LLM Cursor
---
 libsepol/tests/helpers.c                    |   5 +
 libsepol/tests/helpers.h                    |   5 +
 libsepol/tests/libsepol-tests.c             |   2 +
 libsepol/tests/policies/test-iter/iter.conf | 124 ++++++++++++++++++
 libsepol/tests/test-iter-types.c            | 137 ++++++++++++++++++++
 libsepol/tests/test-iter-types.h            |   7 +
 libsepol/tests/test-iter.c                  |  96 ++++++++++++++
 libsepol/tests/test-iter.h                  |  10 ++
 8 files changed, 386 insertions(+)
 create mode 100644 libsepol/tests/policies/test-iter/iter.conf
 create mode 100644 libsepol/tests/test-iter-types.c
 create mode 100644 libsepol/tests/test-iter-types.h
 create mode 100644 libsepol/tests/test-iter.c
 create mode 100644 libsepol/tests/test-iter.h
libsepol/tests/helpers.c +5 -0
index 439ceae1..bd846e34 100644
@@ -84,3 +84,8 @@ avrule_decl_t *test_find_decl_by_sym(policydb_t *p, int symtab, const char *sym)
 
 	return p->decl_val_to_struct[scope->decl_ids[0] - 1];
 }
+
+int qstrcmp(const void *a, const void *b)
+{
+	return strcmp(*(char **)a, *(char **)b);
+}
libsepol/tests/helpers.h +5 -0
index 2885173f..b9463093 100644
@@ -89,4 +89,9 @@ extern int test_load_policy(policydb_t *p, int policy_type, int mls,
 extern avrule_decl_t *test_find_decl_by_sym(policydb_t *p, int symtab,
 					    const char *sym);
 
+/*
+ * qsort comparator for an array of strings
+ */
+extern int qstrcmp(const void *a, const void *b);
+
 #endif
libsepol/tests/libsepol-tests.c +2 -0
index 01f45562..a73686b9 100644
@@ -25,6 +25,7 @@
 #include "test-deps.h"
 #include "test-downgrade.h"
 #include "test-neverallow.h"
+#include "test-iter.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -74,6 +75,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(deps);
 	DECLARE_SUITE(downgrade);
 	DECLARE_SUITE(neverallow);
+	DECLARE_SUITE(iter);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsepol/tests/policies/test-iter/iter.conf new file +124 -0

124 lines not on this page · raw message

libsepol/tests/test-iter-types.c new file +137 -0

137 lines not on this page · raw message

libsepol/tests/test-iter-types.h new file +7 -0
new file mode 100644
index 00000000..39bf4523
@@ -0,0 +1,7 @@
+#ifndef TEST_ITER_TYPES_H
+#define TEST_ITER_TYPES_H
+
+void test_iter_types_empty(void);
+void test_iter_types_non_empty(void);
+
+#endif
libsepol/tests/test-iter.c new file +96 -0

96 lines not on this page · raw message

libsepol/tests/test-iter.h new file +10 -0
new file mode 100644
index 00000000..d3124356
@@ -0,0 +1,10 @@
+#ifndef TEST_ITER_H
+#define TEST_ITER_H
+
+#include <CUnit/Basic.h>
+
+int iter_test_init(void);
+int iter_test_cleanup(void);
+int iter_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.55.0
#6 lore raw

RFC PATCH 5/17 libsepol: add public user iterator and accessors

In reply to jan janasek

Add sepol_user_iter for iterating users, sepol_user_value_to_name() and sepol_user_query_by_value() for resolving a 1-based user value, and harden the existing user_record accessors against NULL arguments.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/users.h   |  65 ++++-
 libsepol/src/user_record.c       | 100 +++++++-
 libsepol/src/users.c             | 416 ++++++++++++++++++++++++++++---
 libsepol/tests/test-iter-users.c | 115 +++++++++
 libsepol/tests/test-iter-users.h |   7 +
 libsepol/tests/test-iter.c       |   7 +
 6 files changed, 659 insertions(+), 51 deletions(-)
 create mode 100644 libsepol/tests/test-iter-users.c
 create mode 100644 libsepol/tests/test-iter-users.h
libsepol/include/sepol/users.h +64 -1
index d1c7c5ff..e7b90721 100644
@@ -1,16 +1,25 @@
 #ifndef _SEPOL_USERS_H_
 #define _SEPOL_USERS_H_
 
+#include <sepol/cat_set.h>
 #include <sepol/policydb.h>
 #include <sepol/user_record.h>
 #include <sepol/handle.h>
 #include <stddef.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/* Modify the user, or add it, if the key is not found */
+struct sepol_user_iter;
+typedef struct sepol_user_iter sepol_user_iter_t;
+
+/* Modify the user, or add it, if the key is not found.
+ * On failure when adding a new user, the user may still have been inserted
+ * into the policydb with an incomplete role set; the policydb remains
+ * memory-safe to use and destroy, but should not be relied upon to be
+ * fully consistent for that user. */
 extern int sepol_user_modify(sepol_handle_t *handle, sepol_policydb_t *policydb,
 			     const sepol_user_key_t *key,
 			     const sepol_user_t *data);
@@ -29,6 +38,17 @@ extern int sepol_user_query(sepol_handle_t *handle, const sepol_policydb_t *p,
 			    const sepol_user_key_t *key,
 			    sepol_user_t **response);
 
+/* Resolve a user value to its name.  Returns non-zero on invalid value.
+ * The returned pointer is borrowed from the policydb; do not free. */
+extern int sepol_user_value_to_name(sepol_handle_t *handle,
+				    const sepol_policydb_t *p, uint32_t value,
+				    const char **name);
+
+/* Query a user by value - returns the user or NULL if not found */
+extern int sepol_user_query_by_value(sepol_handle_t *handle,
+				     const sepol_policydb_t *p, uint32_t value,
+				     sepol_user_t **response);
+
 /* Iterate the users
  * The handler may return:
  * -1 to signal an error condition,
@@ -39,6 +59,49 @@ extern int sepol_user_iterate(sepol_handle_t *handle,
 			      int (*fn)(const sepol_user_t *user, void *fn_arg),
 			      void *arg);
 
+/*
+ * Users iterator. Iteration order follows ascending internal value
+ * assignment, which typically -- but is not guaranteed to -- match
+ * declaration order; callers should not treat it as a stable guarantee
+ * across policydb versions.
+ */
+extern int sepol_user_iter_create(sepol_handle_t *handle,
+				  const sepol_policydb_t *p,
+				  sepol_user_iter_t **iter);
+extern void sepol_user_iter_destroy(sepol_user_iter_t *iter);
+extern int sepol_user_iter_next(sepol_handle_t *handle, sepol_user_iter_t *iter,
+				sepol_user_t **item);
+
+extern uint32_t sepol_user_get_dflt_level_sens(const sepol_policydb_t *p,
+					       uint32_t user_value);
+
+/*
+ * NOTE on sepol_cat_set_iter_t **cat_iter parameters below: see the
+ * ownership contract documented in cat_set.h. The callee always allocates
+ * a new, caller-owned iterator that must be freed with
+ * sepol_cat_set_iter_destroy().
+ */
+extern int sepol_user_get_dflt_level_cat(sepol_handle_t *handle,
+					 const sepol_policydb_t *p,
+					 uint32_t user_value,
+					 sepol_cat_set_iter_t **cat_iter);
+
+extern uint32_t sepol_user_get_range_low_sens(const sepol_policydb_t *p,
+					      uint32_t user_value);
+
+extern uint32_t sepol_user_get_range_high_sens(const sepol_policydb_t *p,
+					       uint32_t user_value);
+
+extern int sepol_user_get_range_low_cat(sepol_handle_t *handle,
+					const sepol_policydb_t *p,
+					uint32_t user_value,
+					sepol_cat_set_iter_t **cat_iter);
+
+extern int sepol_user_get_range_high_cat(sepol_handle_t *handle,
+					 const sepol_policydb_t *p,
+					 uint32_t user_value,
+					 sepol_cat_set_iter_t **cat_iter);
+
 #ifdef __cplusplus
 }
 #endif
libsepol/src/user_record.c +90 -10

241 lines not on this page · raw message

libsepol/src/users.c +376 -40

580 lines not on this page · raw message

libsepol/tests/test-iter-users.c new file +115 -0

115 lines not on this page · raw message

libsepol/tests/test-iter-users.h new file +7 -0
new file mode 100644
index 00000000..de6cb6ee
@@ -0,0 +1,7 @@
+#ifndef TEST_ITER_USERS_H
+#define TEST_ITER_USERS_H
+
+void test_iter_users_empty(void);
+void test_iter_users_non_empty(void);
+
+#endif
libsepol/tests/test-iter.c +7 -0
index f9b8880f..83308d4d 100644
@@ -11,6 +11,7 @@
 
 #include "parse_util.h"
 #include "test-iter-types.h"
+#include "test-iter-users.h"
 
 extern int mls;
 
@@ -92,5 +93,11 @@ int iter_add_tests(CU_pSuite suite)
 	if (CU_add_test(suite, "iter_types_non_empty",
 			test_iter_types_non_empty) == NULL)
 		return CU_get_error();
+	if (CU_add_test(suite, "iter_users_empty", test_iter_users_empty) ==
+	    NULL)
+		return CU_get_error();
+	if (CU_add_test(suite, "iter_users_non_empty",
+			test_iter_users_non_empty) == NULL)
+		return CU_get_error();
 	return 0;
 }
Signature · 1 line
2.55.0
#7 lore raw

RFC PATCH 6/17 libsepol: add public role record structure and iterator

In reply to jan janasek

Introduce sepol_role_t with getter/setter API for role name, value, type membership, dominance, bounds, and flavor. Add sepol_role_iter, sepol_role_value_to_name(), and sepol_role_query_by_value().

Assisted-by LLM Cursor
---
 libsepol/include/sepol/role_record.h        |  79 +++++
 libsepol/include/sepol/roles.h              |  57 ++++
 libsepol/src/role_record.c                  | 316 ++++++++++++++++++++
 libsepol/src/roles.c                        | 253 ++++++++++++++++
 libsepol/tests/policies/test-iter/iter.conf |   2 +
 libsepol/tests/test-iter-roles.c            | 165 ++++++++++
 libsepol/tests/test-iter-roles.h            |   7 +
 libsepol/tests/test-iter.c                  |   7 +
 8 files changed, 886 insertions(+)
 create mode 100644 libsepol/include/sepol/role_record.h
 create mode 100644 libsepol/include/sepol/roles.h
 create mode 100644 libsepol/src/role_record.c
 create mode 100644 libsepol/src/roles.c
 create mode 100644 libsepol/tests/test-iter-roles.c
 create mode 100644 libsepol/tests/test-iter-roles.h
libsepol/include/sepol/role_record.h new file +79 -0
new file mode 100644
index 00000000..4498a126
@@ -0,0 +1,79 @@
+#ifndef _SEPOL_ROLE_RECORD_H_
+#define _SEPOL_ROLE_RECORD_H_
+
+#include <stdint.h>
+
+#include <sepol/handle.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sepol_role;
+typedef struct sepol_role sepol_role_t;
+struct sepol_role_key;
+typedef struct sepol_role_key sepol_role_key_t;
+
+/* Key */
+extern int sepol_role_key_create(sepol_handle_t *handle, const char *name,
+				 sepol_role_key_t **key);
+extern void sepol_role_key_unpack(const sepol_role_key_t *key,
+				  const char **name);
+extern int sepol_role_key_extract(sepol_handle_t *handle,
+				  const sepol_role_t *role,
+				  sepol_role_key_t **key);
+extern void sepol_role_key_free(sepol_role_key_t *key);
+
+extern int sepol_role_compare(const sepol_role_t *role,
+			      const sepol_role_key_t *key);
+extern int sepol_role_compare2(const sepol_role_t *role,
+			       const sepol_role_t *role2);
+
+/* Role name */
+extern const char *sepol_role_get_name(const sepol_role_t *role);
+extern int sepol_role_set_name(sepol_handle_t *handle, sepol_role_t *role,
+			       const char *name);
+
+/* Authorized types */
+extern int sepol_role_has_type(const sepol_role_t *role, const char *type);
+extern int sepol_role_get_types(sepol_handle_t *handle,
+				const sepol_role_t *role, const char ***types,
+				uint32_t *num_types);
+extern int sepol_role_add_type(sepol_handle_t *handle, sepol_role_t *role,
+			       const char *type);
+extern int sepol_role_del_type(sepol_handle_t *handle, sepol_role_t *role,
+			       const char *type);
+
+/* Bounds role */
+extern const char *sepol_role_get_bounds(const sepol_role_t *role);
+extern int sepol_role_set_bounds(sepol_handle_t *handle, sepol_role_t *role,
+				 const char *bounds);
+
+/* Flavor */
+#define SEPOL_ROLE_ROLE 0
+#define SEPOL_ROLE_ATTRIB 1
+extern uint32_t sepol_role_get_flavor(const sepol_role_t *role);
+extern int sepol_role_set_flavor(sepol_handle_t *handle, sepol_role_t *role,
+				 uint32_t flavor);
+
+/* Subroles in attribute */
+extern int sepol_role_get_subroles(sepol_handle_t *handle,
+				   const sepol_role_t *role,
+				   const char ***subroles,
+				   uint32_t *num_subroles);
+extern int sepol_role_add_subrole(sepol_handle_t *handle, sepol_role_t *role,
+				  const char *subrole);
+extern int sepol_role_del_subrole(sepol_handle_t *handle, sepol_role_t *role,
+				  const char *subrole);
+
+/* Create/Clone/Destroy */
+extern int sepol_role_create(sepol_handle_t *handle, sepol_role_t **role_ptr);
+extern int sepol_role_clone(sepol_handle_t *handle, const sepol_role_t *role,
+			    sepol_role_t **role_ptr);
+extern void sepol_role_free(sepol_role_t *role);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/roles.h new file +57 -0

57 lines not on this page · raw message

libsepol/src/role_record.c new file +316 -0

316 lines not on this page · raw message

libsepol/src/roles.c new file +253 -0

253 lines not on this page · raw message

libsepol/tests/policies/test-iter/iter.conf +2 -0
index 2c27f9d4..fd27f5e0 100644
@@ -80,6 +80,8 @@ role ROLE1;
 role ROLE1 types { TYPE1 };
 role ROLE2;
 role ROLE2 types { TYPE2 };
+role ROLE3;
+role ROLE3 types { ATTR1 -TYPE3 };
 
 attribute_role ATTR_ROLE1;
 roleattribute ROLE1 ATTR_ROLE1;
libsepol/tests/test-iter-roles.c new file +165 -0

165 lines not on this page · raw message

libsepol/tests/test-iter-roles.h new file +7 -0
new file mode 100644
index 00000000..dd5f2fdc
@@ -0,0 +1,7 @@
+#ifndef TEST_ITER_ROLES_H
+#define TEST_ITER_ROLES_H
+
+void test_iter_roles_empty(void);
+void test_iter_roles_non_empty(void);
+
+#endif
libsepol/tests/test-iter.c +7 -0
index 83308d4d..7c58c36d 100644
@@ -12,6 +12,7 @@
 #include "parse_util.h"
 #include "test-iter-types.h"
 #include "test-iter-users.h"
+#include "test-iter-roles.h"
 
 extern int mls;
 
@@ -99,5 +100,11 @@ int iter_add_tests(CU_pSuite suite)
 	if (CU_add_test(suite, "iter_users_non_empty",
 			test_iter_users_non_empty) == NULL)
 		return CU_get_error();
+	if (CU_add_test(suite, "iter_roles_empty", test_iter_roles_empty) ==
+	    NULL)
+		return CU_get_error();
+	if (CU_add_test(suite, "iter_roles_non_empty",
+			test_iter_roles_non_empty) == NULL)
+		return CU_get_error();
 	return 0;
 }
Signature · 1 line
2.55.0
#8 lore raw

RFC PATCH 7/17 libsepol: add public class record structure

In reply to jan janasek

Introduce sepol_class_t with getter/setter API for class name, value, common, permissions, constraints, and validatetrans expressions, without exposing internal class_datum_t or constraint_node_t.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/class_record.h | 253 +++++++
 libsepol/src/class_record.c           | 950 ++++++++++++++++++++++++++
 2 files changed, 1203 insertions(+)
 create mode 100644 libsepol/include/sepol/class_record.h
 create mode 100644 libsepol/src/class_record.c
libsepol/include/sepol/class_record.h new file +253 -0
new file mode 100644
index 00000000..ab4193a0
@@ -0,0 +1,253 @@
+#ifndef _SEPOL_CLASS_RECORD_H_
+#define _SEPOL_CLASS_RECORD_H_
+
+#include <stdint.h>
+
+#include <sepol/handle.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sepol_class;
+typedef struct sepol_class sepol_class_t;
+struct sepol_class_key;
+typedef struct sepol_class_key sepol_class_key_t;
+
+struct sepol_constraint;
+typedef struct sepol_constraint sepol_constraint_t;
+struct sepol_constraint_expr;
+typedef struct sepol_constraint_expr sepol_constraint_expr_t;
+
+/* Key */
+extern int sepol_class_key_create(sepol_handle_t *handle, const char *name,
+				  sepol_class_key_t **key);
+extern void sepol_class_key_unpack(const sepol_class_key_t *key,
+				   const char **name);
+extern int sepol_class_key_extract(sepol_handle_t *handle,
+				   const sepol_class_t *cls,
+				   sepol_class_key_t **key);
+extern void sepol_class_key_free(sepol_class_key_t *key);
+
+extern int sepol_class_compare(const sepol_class_t *cls,
+			       const sepol_class_key_t *key);
+extern int sepol_class_compare2(const sepol_class_t *cls,
+				const sepol_class_t *class2);
+
+/* Class name */
+extern const char *sepol_class_get_name(const sepol_class_t *cls);
+extern int sepol_class_set_name(sepol_handle_t *handle, sepol_class_t *cls,
+				const char *name);
+
+/* Common name */
+extern const char *sepol_class_get_common(const sepol_class_t *cls);
+extern int sepol_class_set_common(sepol_handle_t *handle, sepol_class_t *cls,
+				  const char *common);
+
+/* Class-specific permissions */
+extern int sepol_class_has_perm(const sepol_class_t *cls, const char *perm);
+extern int sepol_class_get_perms(sepol_handle_t *handle,
+				 const sepol_class_t *cls, const char ***perms,
+				 uint32_t *num_perms);
+extern int sepol_class_add_perm(sepol_handle_t *handle, sepol_class_t *cls,
+				const char *perm);
+extern int sepol_class_del_perm(sepol_handle_t *handle, sepol_class_t *cls,
+				const char *perm);
+
+/* Constraints */
+extern int sepol_class_get_constraints(sepol_handle_t *handle,
+				       const sepol_class_t *cls,
+				       const sepol_constraint_t ***constraints,
+				       uint32_t *num_constraints);
+/*
+ * On success, cls takes ownership of constraint: it is stored by
+ * reference (not copied) and will be freed when cls is freed or when
+ * constraint is removed via sepol_class_del_constraint(). The caller
+ * must not free or otherwise reuse constraint after a successful call.
+ * On failure, ownership is not transferred and the caller remains
+ * responsible for freeing constraint.
+ */
+extern int sepol_class_add_constraint(sepol_handle_t *handle,
+				      sepol_class_t *cls,
+				      sepol_constraint_t *constraint);
+/*
+ * Removes and frees exactly the constraint object passed in, matched by
+ * pointer identity (i.e. it must be a value previously passed to
+ * sepol_class_add_constraint() on this cls, such as one obtained from
+ * sepol_class_get_constraints()), not by comparing field values. Two
+ * structurally-identical but distinct constraint objects are never
+ * conflated: adding both and deleting one leaves the other untouched.
+ * Returns an error, without freeing anything, if constraint is not
+ * currently stored in cls.
+ */
+extern int sepol_class_del_constraint(sepol_handle_t *handle,
+				      sepol_class_t *cls,
+				      const sepol_constraint_t *constraint);
+
+/* Validate transitions */
+extern int
+sepol_class_get_validatetrans(sepol_handle_t *handle, const sepol_class_t *cls,
+			      const sepol_constraint_t ***validatetrans,
+			      uint32_t *num_validatetrans);
+/*
+ * On success, cls takes ownership of validatetrans: it is stored by
+ * reference (not copied) and will be freed when cls is freed or when
+ * validatetrans is removed via sepol_class_del_validatetrans(). The
+ * caller must not free or otherwise reuse validatetrans after a
+ * successful call. On failure, ownership is not transferred and the
+ * caller remains responsible for freeing validatetrans.
+ */
+extern int sepol_class_add_validatetrans(sepol_handle_t *handle,
+					 sepol_class_t *cls,
+					 sepol_constraint_t *validatetrans);
+/*
+ * Removes and frees exactly the validatetrans object passed in, matched
+ * by pointer identity, not by comparing field values -- see
+ * sepol_class_del_constraint()'s documentation for the rationale, which
+ * applies identically here. Returns an error, without freeing anything,
+ * if validatetrans is not currently stored in cls.
+ */
+extern int
+sepol_class_del_validatetrans(sepol_handle_t *handle, sepol_class_t *cls,
+			      const sepol_constraint_t *validatetrans);
+
+/* Defaults */
+#define SEPOL_CLASS_DEFAULT_SOURCE 1
+#define SEPOL_CLASS_DEFAULT_TARGET 2
+extern int sepol_class_get_default_user(const sepol_class_t *cls);
+extern int sepol_class_set_default_user(sepol_handle_t *handle,
+					sepol_class_t *cls, int default_user);
+extern int sepol_class_get_default_role(const sepol_class_t *cls);
+extern int sepol_class_set_default_role(sepol_handle_t *handle,
+					sepol_class_t *cls, int default_role);
+extern int sepol_class_get_default_type(const sepol_class_t *cls);
+extern int sepol_class_set_default_type(sepol_handle_t *handle,
+					sepol_class_t *cls, int default_type);
+
+#define SEPOL_CLASS_DEFAULT_SOURCE_LOW 1
+#define SEPOL_CLASS_DEFAULT_SOURCE_HIGH 2
+#define SEPOL_CLASS_DEFAULT_SOURCE_LOW_HIGH 3
+#define SEPOL_CLASS_DEFAULT_TARGET_LOW 4
+#define SEPOL_CLASS_DEFAULT_TARGET_HIGH 5
+#define SEPOL_CLASS_DEFAULT_TARGET_LOW_HIGH 6
+#define SEPOL_CLASS_DEFAULT_GLBLUB 7
+extern int sepol_class_get_default_range(const sepol_class_t *cls);
+extern int sepol_class_set_default_range(sepol_handle_t *handle,
+					 sepol_class_t *cls, int default_range);
+
+/* Create/Clone/Destroy */
+extern int sepol_class_create(sepol_handle_t *handle,
+			      sepol_class_t **class_ptr);
+extern int sepol_class_clone(sepol_handle_t *handle, const sepol_class_t *cls,
+			     sepol_class_t **class_ptr);
+extern void sepol_class_free(sepol_class_t *cls);
+
+/* Constraints */
+extern int sepol_constraint_create(sepol_handle_t *handle,
+				   sepol_constraint_t **constraint_ptr);
+extern int sepol_constraint_clone(sepol_handle_t *handle,
+				  const sepol_constraint_t *constraint,
+				  sepol_constraint_t **constraint_ptr);
+extern void sepol_constraint_free(sepol_constraint_t *constraint);
+
+/* Constrained permissions */
+extern int sepol_constraint_has_perm(const sepol_constraint_t *constraint,
+				     const char *perm);
+extern int sepol_constraint_get_perms(sepol_handle_t *handle,
+				      const sepol_constraint_t *constraint,
+				      const char ***perms, uint32_t *num_perms);
+extern int sepol_constraint_add_perm(sepol_handle_t *handle,
+				     sepol_constraint_t *constraint,
+				     const char *perm);
+extern int sepol_constraint_del_perm(sepol_handle_t *handle,
+				     sepol_constraint_t *constraint,
+				     const char *perm);
+
+/* Constraint sub expressions */
+extern int sepol_constraint_get_exprs(sepol_handle_t *handle,
+				      const sepol_constraint_t *constraint,
+				      const sepol_constraint_expr_t ***exprs,
+				      uint32_t *num_exprs);
+extern int sepol_constraint_insert_expr(sepol_handle_t *handle,
+					sepol_constraint_t *constraint,
+					uint32_t index,
+					sepol_constraint_expr_t *expr);
+extern int sepol_constraint_remove_expr(sepol_handle_t *handle,
+					sepol_constraint_t *constraint,
+					uint32_t index);
+
+/* Constraint expressions */
+extern int sepol_constraint_expr_create(sepol_handle_t *handle,
+					sepol_constraint_expr_t **expr_ptr);
+extern int sepol_constraint_expr_clone(sepol_handle_t *handle,
+				       const sepol_constraint_expr_t *expr,
+				       sepol_constraint_expr_t **expr_ptr);
+extern void sepol_constraint_expr_free(sepol_constraint_expr_t *expr);
+
+/* Constraint expr type */
+#define SEPOL_CEXPR_TYPE_NOT 1 /* not expr */
+#define SEPOL_CEXPR_TYPE_AND 2 /* expr and expr */
+#define SEPOL_CEXPR_TYPE_OR 3 /* expr or expr */
+#define SEPOL_CEXPR_TYPE_ATTR 4 /* attr op attr */
+#define SEPOL_CEXPR_TYPE_NAMES 5 /* attr op names */
+extern uint32_t
+sepol_constraint_expr_get_type(const sepol_constraint_expr_t *expr);
+extern int sepol_constraint_expr_set_type(sepol_handle_t *handle,
+					  sepol_constraint_expr_t *expr,
+					  uint32_t type);
+
+/* Constraint expr attr */
+#define SEPOL_CEXPR_ATTR_USER 1 /* user */
+#define SEPOL_CEXPR_ATTR_ROLE 2 /* role */
+#define SEPOL_CEXPR_ATTR_TYPE 4 /* type */
+#define SEPOL_CEXPR_ATTR_TARGET 8 /* target if set, source otherwise */
+#define SEPOL_CEXPR_ATTR_XTARGET \
+	16 /* special 3rd target for validatetrans rule */
+#define SEPOL_CEXPR_ATTR_L1L2 32 /* low level 1 vs. low level 2 */
+#define SEPOL_CEXPR_ATTR_L1H2 64 /* low level 1 vs. high level 2 */
+#define SEPOL_CEXPR_ATTR_H1L2 128 /* high level 1 vs. low level 2 */
+#define SEPOL_CEXPR_ATTR_H1H2 256 /* high level 1 vs. high level 2 */
+#define SEPOL_CEXPR_ATTR_L1H1 512 /* low level 1 vs. high level 1 */
+#define SEPOL_CEXPR_ATTR_L2H2 1024 /* low level 2 vs. high level 2 */
+extern int sepol_constraint_expr_has_attr(const sepol_constraint_expr_t *expr,
+					  uint32_t attr);
+extern int sepol_constraint_expr_set_attr(sepol_handle_t *handle,
+					  sepol_constraint_expr_t *expr,
+					  uint32_t attr);
+extern int sepol_constraint_expr_unset_attr(sepol_handle_t *handle,
+					    sepol_constraint_expr_t *expr,
+					    uint32_t attr);
+extern int sepol_constraint_expr_clear_attr(sepol_handle_t *handle,
+					    sepol_constraint_expr_t *expr);
+
+/* Constraint expr attr or names operator */
+#define SEPOL_CEXPR_OP_EQ 1 /* == or eq */
+#define SEPOL_CEXPR_OP_NEQ 2 /* != */
+#define SEPOL_CEXPR_OP_DOM 3 /* dom */
+#define SEPOL_CEXPR_OP_DOMBY 4 /* domby  */
+#define SEPOL_CEXPR_OP_INCOMP 5 /* incomp */
+extern uint32_t
+sepol_constraint_expr_get_op(const sepol_constraint_expr_t *expr);
+extern int sepol_constraint_expr_set_op(sepol_handle_t *handle,
+					sepol_constraint_expr_t *expr,
+					uint32_t op);
+
+/* Constraint names */
+extern int sepol_constraint_expr_has_name(const sepol_constraint_expr_t *expr,
+					  const char *name);
+extern int sepol_constraint_expr_get_names(sepol_handle_t *handle,
+					   const sepol_constraint_expr_t *expr,
+					   const char ***names,
+					   uint32_t *num_names);
+extern int sepol_constraint_expr_add_name(sepol_handle_t *handle,
+					  sepol_constraint_expr_t *expr,
+					  const char *name);
+extern int sepol_constraint_expr_del_name(sepol_handle_t *handle,
+					  sepol_constraint_expr_t *expr,
+					  const char *name);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/src/class_record.c new file +950 -0

950 lines not on this page · raw message

Signature · 1 line
2.55.0
maintainer
#9 lore raw

Re: [RFC PATCH userspace 07/17] libsepol: add public class record structure

Quoted 17 lines · jan janasek

Introduce sepol_class_t with getter/setter API for class name, value, common, permissions, constraints, and validatetrans expressions, without exposing internal class_datum_t or constraint_node_t.

Assisted-by LLM Cursor

---

libsepol/src/class_record.c new file +0 -0
new file mode 100644
index 00000000..8acf7edd

<snip>

libsepol/src/class_record.c line 42

Quoted 7 lines
libsepol/src/class_record.c new file +7 -0
+struct sepol_constraint_expr {
+       uint32_t expr_type;
+       uint32_t attr;
+       uint32_t op;
+       char **names;
+       uint32_t num_names;
+};

This structure carries only the positive name list for CEXPR_TYPE. On a kernel policy that is complete; on a module/base policy a constraint written with ~ or * silently loses the negset or flags info. Either extend to expose the negset and flags info or prevent use on non-kernel policy or at least document the limitation please.

libsepol/src/class_record.c line 895

Quoted 2 lines
libsepol/src/class_record.c new file +2 -0
+int sepol_constraint_expr_set_op(sepol_handle_t *handle,
+                                sepol_constraint_expr_t *expr, uint32_t op)

What is the intended usage of this and other sepol_constraint_*() setters since they have no path back into a policydb since there is no sepol_class_modify()?
If it's intended for round-tripping through sepol_class_clone(), fine; otherwise it's a lot of API for data that's effectively read-only.

#10 lore raw

RFC PATCH 8/17 libsepol: add public class iterator and tests

In reply to jan janasek

Add sepol_class_iter, sepol_class_value_to_name(), sepol_class_query_by_value(), and sepol_class_perm_value_to_name() for resolving classes and their permissions by value, with tests covering constraint and validatetrans data on real policy classes.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/classes.h   |  69 ++++
 libsepol/src/classes.c             | 581 +++++++++++++++++++++++++++++
 libsepol/tests/test-iter-classes.c | 220 +++++++++++
 libsepol/tests/test-iter-classes.h |   7 +
 libsepol/tests/test-iter.c         |   7 +
 5 files changed, 884 insertions(+)
 create mode 100644 libsepol/include/sepol/classes.h
 create mode 100644 libsepol/src/classes.c
 create mode 100644 libsepol/tests/test-iter-classes.c
 create mode 100644 libsepol/tests/test-iter-classes.h
libsepol/include/sepol/classes.h new file +69 -0
new file mode 100644
index 00000000..3ca52b94
@@ -0,0 +1,69 @@
+#ifndef _SEPOL_CLASSES_H_
+#define _SEPOL_CLASSES_H_
+
+#include <sepol/policydb.h>
+#include <sepol/class_record.h>
+#include <sepol/handle.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sepol_class_iter;
+typedef struct sepol_class_iter sepol_class_iter_t;
+
+/* Return the number of classes */
+extern int sepol_class_count(sepol_handle_t *handle, const sepol_policydb_t *p,
+			     unsigned int *response);
+
+/* Check if the specified class exists */
+extern int sepol_class_exists(sepol_handle_t *handle,
+			      const sepol_policydb_t *policydb,
+			      const sepol_class_key_t *key, int *response);
+
+/* Query a class - returns the class or NULL if not found */
+extern int sepol_class_query(sepol_handle_t *handle, const sepol_policydb_t *p,
+			     const sepol_class_key_t *key,
+			     sepol_class_t **response);
+
+/* Resolve a class value to its name.  Returns non-zero on invalid value.
+ * The returned pointer is borrowed from the policydb; do not free. */
+extern int sepol_class_value_to_name(sepol_handle_t *handle,
+				     const sepol_policydb_t *p, uint32_t value,
+				     const char **name);
+
+/* Query a class by value - returns the class or NULL if not found */
+extern int sepol_class_query_by_value(sepol_handle_t *handle,
+				      const sepol_policydb_t *p, uint32_t value,
+				      sepol_class_t **response);
+
+/* Resolve a permission value to its name within a class.
+ * Returns non-zero on invalid class value.
+ * The returned pointer is borrowed from the policydb; do not free. */
+extern int sepol_class_perm_value_to_name(sepol_handle_t *handle,
+					  const sepol_policydb_t *p,
+					  uint32_t class_value,
+					  uint32_t perm_value,
+					  const char **name);
+
+/*
+ * Iterating classes. Iteration order is unspecified (currently
+ * hash-bucket order of the internal symbol table) and may change between
+ * library versions or policydb loads; callers must not rely on it
+ * matching declaration order or being stable across calls.
+ */
+extern int sepol_class_iter_create(sepol_handle_t *handle,
+				   const sepol_policydb_t *p,
+				   sepol_class_iter_t **iter);
+extern void sepol_class_iter_destroy(sepol_class_iter_t *iter);
+extern int sepol_class_iter_next(sepol_handle_t *handle,
+				 sepol_class_iter_t *iter,
+				 sepol_class_t **item);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/src/classes.c new file +581 -0

581 lines not on this page · raw message

libsepol/tests/test-iter-classes.c new file +220 -0

220 lines not on this page · raw message

libsepol/tests/test-iter-classes.h new file +7 -0
new file mode 100644
index 00000000..e85bab43
@@ -0,0 +1,7 @@
+#ifndef TEST_ITER_CLASSES_H
+#define TEST_ITER_CLASSES_H
+
+void test_iter_classes_empty(void);
+void test_iter_classes_non_empty(void);
+
+#endif
libsepol/tests/test-iter.c +7 -0
index 7c58c36d..4c57459a 100644
@@ -13,6 +13,7 @@
 #include "test-iter-types.h"
 #include "test-iter-users.h"
 #include "test-iter-roles.h"
+#include "test-iter-classes.h"
 
 extern int mls;
 
@@ -106,5 +107,11 @@ int iter_add_tests(CU_pSuite suite)
 	if (CU_add_test(suite, "iter_roles_non_empty",
 			test_iter_roles_non_empty) == NULL)
 		return CU_get_error();
+	if (CU_add_test(suite, "iter_classes_empty", test_iter_classes_empty) ==
+	    NULL)
+		return CU_get_error();
+	if (CU_add_test(suite, "iter_classes_non_empty",
+			test_iter_classes_non_empty) == NULL)
+		return CU_get_error();
 	return 0;
 }
Signature · 1 line
2.55.0
#11 lore raw

RFC PATCH 9/17 libsepol: add policy properties API and public constants

In reply to jan janasek

Add sepol/constants.h re-exporting AVTAB_*, OCON_*, SECURITY_FS_USE_*, and related policydb constants under an SEPOL_ namespace (with unprefixed aliases guarded to avoid redefinition conflicts for callers that also include internal headers), so consumers no longer need policydb/avtab.h or policydb/services.h just for these values.

Add opaque-policydb properties: sepol_policydb_mls_enabled(), sepol_policydb_compat_net(), sepol_policydb_set_permissive_flags(), and sepol_policydb_rebuild_attr_map() for maintaining the type attribute map after modifying type/attribute membership.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/constants.h         | 231 +++++++++++++++++++++
 libsepol/include/sepol/policydb.h          |  29 +++
 libsepol/include/sepol/policydb/avtab.h    |  33 +--
 libsepol/include/sepol/policydb/policydb.h |  68 +-----
 libsepol/include/sepol/policydb/services.h |  19 +-
 libsepol/src/policydb_public.c             | 203 ++++++++++++++++++
 6 files changed, 486 insertions(+), 97 deletions(-)
 create mode 100644 libsepol/include/sepol/constants.h
libsepol/include/sepol/constants.h new file +231 -0
new file mode 100644
index 00000000..69d29537
@@ -0,0 +1,231 @@
+#ifndef _SEPOL_CONSTANTS_H_
+#define _SEPOL_CONSTANTS_H_
+
+/*
+ * Policy interpretation constants shared across libsepol and consumers
+ * (e.g. SETools) that need stable numeric tokens without pulling in full
+ * policydb, avtab, or service API headers.
+ *
+ * Values match the kernel / policy binary format and must stay in sync
+ * with libsepol's internal policy representation.
+ *
+ * Public API consumers should prefer the SEPOL_-prefixed aliases below.
+ * The unprefixed forms are provided for source compatibility with code
+ * that also includes internal policydb headers; #ifndef guards prevent
+ * redefinition conflicts.
+ */
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* --- Namespace-safe constants for public API consumers --- */
+
+#define SEPOL_AVTAB_ALLOWED 0x0001
+#define SEPOL_AVTAB_AUDITALLOW 0x0002
+#define SEPOL_AVTAB_AUDITDENY 0x0004
+#define SEPOL_AVTAB_NEVERALLOW 0x0080
+#define SEPOL_AVTAB_AV \
+	(SEPOL_AVTAB_ALLOWED | SEPOL_AVTAB_AUDITALLOW | SEPOL_AVTAB_AUDITDENY)
+#define SEPOL_AVTAB_TRANSITION 0x0010
+#define SEPOL_AVTAB_MEMBER 0x0020
+#define SEPOL_AVTAB_CHANGE 0x0040
+#define SEPOL_AVTAB_TYPE \
+	(SEPOL_AVTAB_TRANSITION | SEPOL_AVTAB_MEMBER | SEPOL_AVTAB_CHANGE)
+#define SEPOL_AVTAB_XPERMS_ALLOWED 0x0100
+#define SEPOL_AVTAB_XPERMS_AUDITALLOW 0x0200
+#define SEPOL_AVTAB_XPERMS_DONTAUDIT 0x0400
+#define SEPOL_AVTAB_XPERMS_NEVERALLOW 0x0800
+#define SEPOL_AVTAB_XPERMS                                            \
+	(SEPOL_AVTAB_XPERMS_ALLOWED | SEPOL_AVTAB_XPERMS_AUDITALLOW | \
+	 SEPOL_AVTAB_XPERMS_DONTAUDIT)
+#define SEPOL_AVTAB_ENABLED_OLD 0x80000000
+#define SEPOL_AVTAB_ENABLED 0x8000
+
+#define SEPOL_AVTAB_XPERMS_IOCTLFUNCTION 0x01
+#define SEPOL_AVTAB_XPERMS_IOCTLDRIVER 0x02
+#define SEPOL_AVTAB_XPERMS_NLMSG 0x03
+
+#define SEPOL_AVRULE_ALLOWED SEPOL_AVTAB_ALLOWED
+#define SEPOL_AVRULE_AUDITALLOW SEPOL_AVTAB_AUDITALLOW
+#define SEPOL_AVRULE_AUDITDENY SEPOL_AVTAB_AUDITDENY
+#define SEPOL_AVRULE_DONTAUDIT 0x0008
+#define SEPOL_AVRULE_NEVERALLOW SEPOL_AVTAB_NEVERALLOW
+#define SEPOL_AVRULE_AV                                    \
+	(SEPOL_AVRULE_ALLOWED | SEPOL_AVRULE_AUDITALLOW |  \
+	 SEPOL_AVRULE_AUDITDENY | SEPOL_AVRULE_DONTAUDIT | \
+	 SEPOL_AVRULE_NEVERALLOW)
+#define SEPOL_AVRULE_TRANSITION SEPOL_AVTAB_TRANSITION
+#define SEPOL_AVRULE_MEMBER SEPOL_AVTAB_MEMBER
+#define SEPOL_AVRULE_CHANGE SEPOL_AVTAB_CHANGE
+#define SEPOL_AVRULE_TYPE \
+	(SEPOL_AVRULE_TRANSITION | SEPOL_AVRULE_MEMBER | SEPOL_AVRULE_CHANGE)
+#define SEPOL_AVRULE_XPERMS_ALLOWED SEPOL_AVTAB_XPERMS_ALLOWED
+#define SEPOL_AVRULE_XPERMS_AUDITALLOW SEPOL_AVTAB_XPERMS_AUDITALLOW
+#define SEPOL_AVRULE_XPERMS_DONTAUDIT SEPOL_AVTAB_XPERMS_DONTAUDIT
+#define SEPOL_AVRULE_XPERMS_NEVERALLOW SEPOL_AVTAB_XPERMS_NEVERALLOW
+#define SEPOL_AVRULE_XPERMS                                             \
+	(SEPOL_AVRULE_XPERMS_ALLOWED | SEPOL_AVRULE_XPERMS_AUDITALLOW | \
+	 SEPOL_AVRULE_XPERMS_DONTAUDIT | SEPOL_AVRULE_XPERMS_NEVERALLOW)
+#define SEPOL_AVRULE_XPERMS_IOCTLFUNCTION SEPOL_AVTAB_XPERMS_IOCTLFUNCTION
+#define SEPOL_AVRULE_XPERMS_IOCTLDRIVER SEPOL_AVTAB_XPERMS_IOCTLDRIVER
+#define SEPOL_AVRULE_XPERMS_NLMSG SEPOL_AVTAB_XPERMS_NLMSG
+
+#define SEPOL_RULE_SELF (1U << 0)
+#define SEPOL_RULE_NOTSELF (1U << 1)
+
+#define SEPOL_SECURITY_FS_USE_XATTR 1
+#define SEPOL_SECURITY_FS_USE_TRANS 2
+#define SEPOL_SECURITY_FS_USE_TASK 3
+#define SEPOL_SECURITY_FS_USE_GENFS 4
+#define SEPOL_SECURITY_FS_USE_NONE 5
+
+#define SEPOL_SYM_COMMONS 0
+#define SEPOL_SYM_CLASSES 1
+#define SEPOL_SYM_ROLES 2
+#define SEPOL_SYM_TYPES 3
+#define SEPOL_SYM_USERS 4
+#define SEPOL_SYM_BOOLS 5
+#define SEPOL_SYM_LEVELS 6
+#define SEPOL_SYM_CATS 7
+#define SEPOL_SYM_NUM 8
+
+#define SEPOL_OCON_ISID 0
+#define SEPOL_OCON_FS 1
+#define SEPOL_OCON_PORT 2
+#define SEPOL_OCON_NETIF 3
+#define SEPOL_OCON_NODE 4
+#define SEPOL_OCON_FSUSE 5
+#define SEPOL_OCON_NODE6 6
+#define SEPOL_OCON_IBPKEY 7
+#define SEPOL_OCON_IBENDPORT 8
+#define SEPOL_OCON_XEN_ISID 0
+#define SEPOL_OCON_XEN_PIRQ 1
+#define SEPOL_OCON_XEN_IOPORT 2
+#define SEPOL_OCON_XEN_IOMEM 3
+#define SEPOL_OCON_XEN_PCIDEVICE 4
+#define SEPOL_OCON_XEN_DEVICETREE 5
+#define SEPOL_OCON_NUM 9
+
+#define SEPOL_EXTENDED_PERMS_LEN 8
+#define SEPOL_MAX_AVTAB_HASH_BITS 20
+#define SEPOL_MAX_AVTAB_HASH_BUCKETS (1 << SEPOL_MAX_AVTAB_HASH_BITS)
+#define SEPOL_MAX_AVTAB_SIZE (SEPOL_MAX_AVTAB_HASH_BUCKETS << 1)
+
+/*
+ * Unprefixed forms for backward compatibility with code that mixes
+ * public and internal headers.  Guarded to avoid redefinition warnings
+ * when policydb/avtab.h or similar internal headers are also included.
+ */
+
+#ifndef AVTAB_ALLOWED
+#define AVTAB_ALLOWED SEPOL_AVTAB_ALLOWED
+#define AVTAB_AUDITALLOW SEPOL_AVTAB_AUDITALLOW
+#define AVTAB_AUDITDENY SEPOL_AVTAB_AUDITDENY
+#define AVTAB_NEVERALLOW SEPOL_AVTAB_NEVERALLOW
+#define AVTAB_AV SEPOL_AVTAB_AV
+#define AVTAB_TRANSITION SEPOL_AVTAB_TRANSITION
+#define AVTAB_MEMBER SEPOL_AVTAB_MEMBER
+#define AVTAB_CHANGE SEPOL_AVTAB_CHANGE
+#define AVTAB_TYPE SEPOL_AVTAB_TYPE
+#define AVTAB_XPERMS_ALLOWED SEPOL_AVTAB_XPERMS_ALLOWED
+#define AVTAB_XPERMS_AUDITALLOW SEPOL_AVTAB_XPERMS_AUDITALLOW
+#define AVTAB_XPERMS_DONTAUDIT SEPOL_AVTAB_XPERMS_DONTAUDIT
+#define AVTAB_XPERMS_NEVERALLOW SEPOL_AVTAB_XPERMS_NEVERALLOW
+#define AVTAB_XPERMS SEPOL_AVTAB_XPERMS
+#define AVTAB_ENABLED_OLD SEPOL_AVTAB_ENABLED_OLD
+#define AVTAB_ENABLED SEPOL_AVTAB_ENABLED
+#define AVTAB_XPERMS_IOCTLFUNCTION SEPOL_AVTAB_XPERMS_IOCTLFUNCTION
+#define AVTAB_XPERMS_IOCTLDRIVER SEPOL_AVTAB_XPERMS_IOCTLDRIVER
+#define AVTAB_XPERMS_NLMSG SEPOL_AVTAB_XPERMS_NLMSG
+#endif
+
+#ifndef AVRULE_ALLOWED
+#define AVRULE_ALLOWED SEPOL_AVRULE_ALLOWED
+#define AVRULE_AUDITALLOW SEPOL_AVRULE_AUDITALLOW
+#define AVRULE_AUDITDENY SEPOL_AVRULE_AUDITDENY
+#define AVRULE_DONTAUDIT SEPOL_AVRULE_DONTAUDIT
+#define AVRULE_NEVERALLOW SEPOL_AVRULE_NEVERALLOW
+#define AVRULE_AV SEPOL_AVRULE_AV
+#define AVRULE_TRANSITION SEPOL_AVRULE_TRANSITION
+#define AVRULE_MEMBER SEPOL_AVRULE_MEMBER
+#define AVRULE_CHANGE SEPOL_AVRULE_CHANGE
+#define AVRULE_TYPE SEPOL_AVRULE_TYPE
+#define AVRULE_XPERMS_ALLOWED SEPOL_AVRULE_XPERMS_ALLOWED
+#define AVRULE_XPERMS_AUDITALLOW SEPOL_AVRULE_XPERMS_AUDITALLOW
+#define AVRULE_XPERMS_DONTAUDIT SEPOL_AVRULE_XPERMS_DONTAUDIT
+#define AVRULE_XPERMS_NEVERALLOW SEPOL_AVRULE_XPERMS_NEVERALLOW
+#define AVRULE_XPERMS SEPOL_AVRULE_XPERMS
+#define AVRULE_XPERMS_IOCTLFUNCTION SEPOL_AVRULE_XPERMS_IOCTLFUNCTION
+#define AVRULE_XPERMS_IOCTLDRIVER SEPOL_AVRULE_XPERMS_IOCTLDRIVER
+#define AVRULE_XPERMS_NLMSG SEPOL_AVRULE_XPERMS_NLMSG
+#endif
+
+#ifndef RULE_SELF
+#define RULE_SELF SEPOL_RULE_SELF
+#define RULE_NOTSELF SEPOL_RULE_NOTSELF
+#endif
+
+#ifndef SECURITY_FS_USE_XATTR
+#define SECURITY_FS_USE_XATTR SEPOL_SECURITY_FS_USE_XATTR
+#define SECURITY_FS_USE_TRANS SEPOL_SECURITY_FS_USE_TRANS
+#define SECURITY_FS_USE_TASK SEPOL_SECURITY_FS_USE_TASK
+#define SECURITY_FS_USE_GENFS SEPOL_SECURITY_FS_USE_GENFS
+#define SECURITY_FS_USE_NONE SEPOL_SECURITY_FS_USE_NONE
+#endif
+
+#ifndef SYM_COMMONS
+#define SYM_COMMONS SEPOL_SYM_COMMONS
+#define SYM_CLASSES SEPOL_SYM_CLASSES
+#define SYM_ROLES SEPOL_SYM_ROLES
+#define SYM_TYPES SEPOL_SYM_TYPES
+#define SYM_USERS SEPOL_SYM_USERS
+#define SYM_BOOLS SEPOL_SYM_BOOLS
+#define SYM_LEVELS SEPOL_SYM_LEVELS
+#define SYM_CATS SEPOL_SYM_CATS
+#define SYM_NUM SEPOL_SYM_NUM
+#endif
+
+#ifndef OCON_ISID
+#define OCON_ISID SEPOL_OCON_ISID
+#define OCON_FS SEPOL_OCON_FS
+#define OCON_PORT SEPOL_OCON_PORT
+#define OCON_NETIF SEPOL_OCON_NETIF
+#define OCON_NODE SEPOL_OCON_NODE
+#define OCON_FSUSE SEPOL_OCON_FSUSE
+#define OCON_NODE6 SEPOL_OCON_NODE6
+#define OCON_IBPKEY SEPOL_OCON_IBPKEY
+#define OCON_IBENDPORT SEPOL_OCON_IBENDPORT
+#define OCON_XEN_ISID SEPOL_OCON_XEN_ISID
+#define OCON_XEN_PIRQ SEPOL_OCON_XEN_PIRQ
+#define OCON_XEN_IOPORT SEPOL_OCON_XEN_IOPORT
+#define OCON_XEN_IOMEM SEPOL_OCON_XEN_IOMEM
+#define OCON_XEN_PCIDEVICE SEPOL_OCON_XEN_PCIDEVICE
+#define OCON_XEN_DEVICETREE SEPOL_OCON_XEN_DEVICETREE
+#define OCON_NUM SEPOL_OCON_NUM
+#endif
+
+#ifndef EXTENDED_PERMS_LEN
+#define EXTENDED_PERMS_LEN SEPOL_EXTENDED_PERMS_LEN
+#endif
+
+#ifndef xperm_test
+#define xperm_test(x, p) (1U & ((p)[(x) >> 5] >> ((x) & 0x1f)))
+#define xperm_set(x, p) ((p)[(x) >> 5] |= (1U << ((x) & 0x1f)))
+#define xperm_clear(x, p) ((p)[(x) >> 5] &= ~(1U << ((x) & 0x1f)))
+#endif
+
+#ifndef MAX_AVTAB_HASH_BITS
+#define MAX_AVTAB_HASH_BITS SEPOL_MAX_AVTAB_HASH_BITS
+#define MAX_AVTAB_HASH_BUCKETS SEPOL_MAX_AVTAB_HASH_BUCKETS
+#define MAX_AVTAB_HASH_MASK (MAX_AVTAB_HASH_BUCKETS - 1)
+#define MAX_AVTAB_SIZE SEPOL_MAX_AVTAB_SIZE
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _SEPOL_CONSTANTS_H_ */
libsepol/include/sepol/policydb.h +29 -0
index 98357ee4..967bce3d 100644
@@ -87,6 +87,11 @@ extern int sepol_policydb_set_typevers(sepol_policydb_t *p, unsigned int type);
  */
 extern int sepol_policydb_set_vers(sepol_policydb_t *p, unsigned int vers);
 
+/*
+ * Get the policy version.
+ */
+extern unsigned int sepol_policydb_get_vers(const sepol_policydb_t *p);
+
 /* Set how to handle unknown class/perms. */
 #define SEPOL_DENY_UNKNOWN 0
 #define SEPOL_REJECT_UNKNOWN 2
@@ -94,12 +99,19 @@ extern int sepol_policydb_set_vers(sepol_policydb_t *p, unsigned int vers);
 extern int sepol_policydb_set_handle_unknown(sepol_policydb_t *p,
 					     unsigned int handle_unknown);
 
+/* Get how to handle unknown class/perms. */
+extern unsigned int
+sepol_policydb_get_handle_unknown(const sepol_policydb_t *p);
+
 /* Set the target platform */
 #define SEPOL_TARGET_SELINUX 0
 #define SEPOL_TARGET_XEN 1
 extern int sepol_policydb_set_target_platform(sepol_policydb_t *p,
 					      int target_platform);
 
+/* Get the target platform. */
+extern int sepol_policydb_get_target_platform(const sepol_policydb_t *p);
+
 /*
  * Optimize the policy by removing redundant rules.
  */
@@ -148,6 +160,23 @@ extern int sepol_policydb_mls_enabled(const sepol_policydb_t *p);
  */
 extern int sepol_policydb_compat_net(const sepol_policydb_t *p);
 
+/*
+ * Set permissive flags on types marked as permissive in the policy.
+ * Must be called after policy load to enable permissive type enforcement.
+ */
+extern int sepol_policydb_set_permissive_flags(sepol_handle_t *handle,
+					       sepol_policydb_t *p);
+
+/*
+ * Rebuild the type attribute mapping. Call after modifying type attributes
+ * to ensure consistency.
+ *
+ * On out-of-memory, returns -1; the map may be only partially rebuilt in
+ * that case, but the policydb is left in a self-consistent, usable state.
+ */
+extern int sepol_policydb_rebuild_attr_map(sepol_handle_t *handle,
+					   sepol_policydb_t *p);
+
 #ifdef __cplusplus
 }
 #endif
libsepol/include/sepol/policydb/avtab.h +4 -29
index 83e1033f..eb76c43c 100644
@@ -43,6 +43,8 @@
 #include <sys/types.h>
 #include <stdint.h>
 
+#include <sepol/constants.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -51,32 +53,11 @@ typedef struct avtab_key {
 	uint16_t source_type;
 	uint16_t target_type;
 	uint16_t target_class;
-#define AVTAB_ALLOWED 0x0001
-#define AVTAB_AUDITALLOW 0x0002
-#define AVTAB_AUDITDENY 0x0004
-#define AVTAB_NEVERALLOW 0x0080
-#define AVTAB_AV (AVTAB_ALLOWED | AVTAB_AUDITALLOW | AVTAB_AUDITDENY)
-#define AVTAB_TRANSITION 0x0010
-#define AVTAB_MEMBER 0x0020
-#define AVTAB_CHANGE 0x0040
-#define AVTAB_TYPE (AVTAB_TRANSITION | AVTAB_MEMBER | AVTAB_CHANGE)
-#define AVTAB_XPERMS_ALLOWED 0x0100
-#define AVTAB_XPERMS_AUDITALLOW 0x0200
-#define AVTAB_XPERMS_DONTAUDIT 0x0400
-#define AVTAB_XPERMS_NEVERALLOW 0x0800
-#define AVTAB_XPERMS                                      \
-	(AVTAB_XPERMS_ALLOWED | AVTAB_XPERMS_AUDITALLOW | \
-	 AVTAB_XPERMS_DONTAUDIT)
-#define AVTAB_ENABLED_OLD 0x80000000
-#define AVTAB_ENABLED 0x8000 /* reserved for used in cond_avtab */
-	uint16_t specified; /* what fields are specified */
+	uint16_t specified; /* what fields are specified; see AVTAB_* in sepol/constants.h */
 } avtab_key_t;
 
 typedef struct avtab_extended_perms {
-#define AVTAB_XPERMS_IOCTLFUNCTION 0x01
-#define AVTAB_XPERMS_IOCTLDRIVER 0x02
-#define AVTAB_XPERMS_NLMSG 0x03
-	/* extension of the avtab_key specified */
+	/* extension of the avtab_key specified; see AVTAB_XPERMS_* in sepol/constants.h */
 	uint8_t specified;
 	uint8_t driver;
 	uint32_t perms[8];
@@ -139,12 +120,6 @@ extern avtab_ptr_t avtab_search_node(avtab_t *h, avtab_key_t *key);
 
 extern avtab_ptr_t avtab_search_node_next(avtab_ptr_t node, int specified);
 
-#define MAX_AVTAB_HASH_BITS 20
-#define MAX_AVTAB_HASH_BUCKETS (1 << MAX_AVTAB_HASH_BITS)
-#define MAX_AVTAB_HASH_MASK (MAX_AVTAB_HASH_BUCKETS - 1)
-/* avtab_alloc uses one bucket per 2-4 elements, so adjust to get maximum buckets */
-#define MAX_AVTAB_SIZE (MAX_AVTAB_HASH_BUCKETS << 1)
-
 #ifdef __cplusplus
 }
 #endif
libsepol/include/sepol/policydb/policydb.h +5 -63

101 lines not on this page · raw message

libsepol/include/sepol/policydb/services.h +14 -5
index 4534c183..670bc1ab 100644
@@ -12,6 +12,9 @@
  * Security server interface.
  */
 
+#include <sepol/constants.h>
+#include <sepol/context_record.h>
+#include <sepol/handle.h>
 #include <sepol/policydb/flask_types.h>
 #include <sepol/policydb/policydb.h>
 #include <stddef.h>
@@ -146,6 +149,16 @@ extern int sepol_sid_to_context(sepol_security_id_t sid, /* IN */
 				sepol_security_context_t *scontext, /* OUT */
 				size_t *scontext_len); /* OUT */
 
+/*
+ * Allocate and return a new sepol_context_t with the context associated
+ * with `sid'. The caller owns *record and must free it with
+ * sepol_context_free().
+ * Requires an active policydb and sidtab (see sepol_set_policydb / sepol_set_sidtab).
+ */
+extern int sepol_sid_to_context_record(sepol_handle_t *handle,
+				       sepol_security_id_t sid,
+				       sepol_context_t **record);
+
 /*
  * Return a SID associated with the security context that
  * has the string representation specified by `scontext'.
@@ -220,12 +233,8 @@ extern int sepol_node_sid(uint16_t domain, void *addr, size_t addrlen,
  * Return a value indicating how to handle labeling for the
  * the specified filesystem type, and optionally return a SID
  * for the filesystem object.  
+ * (SECURITY_FS_USE_* constants are in <sepol/constants.h>.)
  */
-#define SECURITY_FS_USE_XATTR 1 /* use xattr */
-#define SECURITY_FS_USE_TRANS 2 /* use transition SIDs, e.g. devpts/tmpfs */
-#define SECURITY_FS_USE_TASK 3 /* use task SIDs, e.g. pipefs/sockfs */
-#define SECURITY_FS_USE_GENFS 4 /* use the genfs support */
-#define SECURITY_FS_USE_NONE 5 /* no labeling support */
 extern int sepol_fs_use(const char *fstype, /* IN */
 			unsigned int *behavior, /* OUT */
 			sepol_security_id_t *sid); /* OUT  */
libsepol/src/policydb_public.c +203 -0

241 lines not on this page · raw message

Signature · 1 line
2.55.0
#12 lore raw

Re: [RFC PATCH userspace 09/17] libsepol: add policy properties API and public constants

Quoted 25 lines · jan janasek

Add sepol/constants.h re-exporting AVTAB_*, OCON_*, SECURITY_FS_USE_*, and related policydb constants under an SEPOL_ namespace (with unprefixed aliases guarded to avoid redefinition conflicts for callers that also include internal headers), so consumers no longer need policydb/avtab.h or policydb/services.h just for these values.

Add opaque-policydb properties: sepol_policydb_mls_enabled(), sepol_policydb_compat_net(), sepol_policydb_set_permissive_flags(), and sepol_policydb_rebuild_attr_map() for maintaining the type attribute map after modifying type/attribute membership.

Assisted-by LLM Cursor
---
  libsepol/include/sepol/constants.h         | 231 +++++++++++++++++++++
  libsepol/include/sepol/policydb.h          |  29 +++
  libsepol/include/sepol/policydb/avtab.h    |  33 +--
  libsepol/include/sepol/policydb/policydb.h |  68 +-----
  libsepol/include/sepol/policydb/services.h |  19 +-
  libsepol/src/policydb_public.c             | 203 ++++++++++++++++++
  6 files changed, 486 insertions(+), 97 deletions(-)

create mode 100644 libsepol/include/sepol/constants.h

[...]

Quoted 47 lines
libsepol/include/sepol/policydb.h +19 -0
index 98357ee4..967bce3d 100644
@@ -87,6 +87,11 @@ extern int sepol_policydb_set_typevers(sepol_policydb_t *p, unsigned int type);
   */
  extern int sepol_policydb_set_vers(sepol_policydb_t *p, unsigned int vers);
 
+/*
+ * Get the policy version.
+ */
+extern unsigned int sepol_policydb_get_vers(const sepol_policydb_t *p);
+
  /* Set how to handle unknown class/perms. */
  #define SEPOL_DENY_UNKNOWN 0
  #define SEPOL_REJECT_UNKNOWN 2
@@ -94,12 +99,19 @@ extern int sepol_policydb_set_vers(sepol_policydb_t *p, unsigned int vers);
  extern int sepol_policydb_set_handle_unknown(sepol_policydb_t *p,
  					     unsigned int handle_unknown);
 
+/* Get how to handle unknown class/perms. */
+extern unsigned int
+sepol_policydb_get_handle_unknown(const sepol_policydb_t *p);
+
  /* Set the target platform */
  #define SEPOL_TARGET_SELINUX 0
  #define SEPOL_TARGET_XEN 1
  extern int sepol_policydb_set_target_platform(sepol_policydb_t *p,
  					      int target_platform);
 
+/* Get the target platform. */
+extern int sepol_policydb_get_target_platform(const sepol_policydb_t *p);
+
  /*
   * Optimize the policy by removing redundant rules.
   */
@@ -148,6 +160,23 @@ extern int sepol_policydb_mls_enabled(const sepol_policydb_t *p);
   */
  extern int sepol_policydb_compat_net(const sepol_policydb_t *p);
 
+/*
+ * Set permissive flags on types marked as permissive in the policy.
+ * Must be called after policy load to enable permissive type enforcement.
+ */
+extern int sepol_policydb_set_permissive_flags(sepol_handle_t *handle,
+					       sepol_policydb_t *p);
+

libsepol/include/sepol/policydb.h extern int sepol_policydb_mls_enabled(const sepol_policydb_t *p); · line 179

Quoted 10 lines
libsepol/include/sepol/policydb.h +10 -0
+/*
+ * Rebuild the type attribute mapping. Call after modifying type attributes
+ * to ensure consistency.
+ *
+ * On out-of-memory, returns -1; the map may be only partially rebuilt in
+ * that case, but the policydb is left in a self-consistent, usable state.
+ */
+extern int sepol_policydb_rebuild_attr_map(sepol_handle_t *handle,
+					   sepol_policydb_t *p);
+

I'd suggest a single public function that calls functions like these two, e.g. a function to initialize policy queries. Otherwise it exposes internal details that users don't need to be aware of.

Signature · 1 line
Chris PeBenito
maintainer
#13 lore raw

Re: [RFC PATCH userspace 09/17] libsepol: add policy properties API and public constants

Quoted 81 lines · Christopher J. PeBenito
Quoted 25 lines · jan janasek

Add sepol/constants.h re-exporting AVTAB_*, OCON_*, SECURITY_FS_USE_*, and related policydb constants under an SEPOL_ namespace (with unprefixed aliases guarded to avoid redefinition conflicts for callers that also include internal headers), so consumers no longer need policydb/avtab.h or policydb/services.h just for these values.

Add opaque-policydb properties: sepol_policydb_mls_enabled(), sepol_policydb_compat_net(), sepol_policydb_set_permissive_flags(), and sepol_policydb_rebuild_attr_map() for maintaining the type attribute map after modifying type/attribute membership.

Assisted-by LLM Cursor
---
  libsepol/include/sepol/constants.h         | 231 +++++++++++++++++++++
  libsepol/include/sepol/policydb.h          |  29 +++
  libsepol/include/sepol/policydb/avtab.h    |  33 +--
  libsepol/include/sepol/policydb/policydb.h |  68 +-----
  libsepol/include/sepol/policydb/services.h |  19 +-
  libsepol/src/policydb_public.c             | 203 ++++++++++++++++++
  6 files changed, 486 insertions(+), 97 deletions(-)

create mode 100644 libsepol/include/sepol/constants.h

[...]

Quoted 53 lines
libsepol/include/sepol/policydb.h +25 -0
index 98357ee4..967bce3d 100644
@@ -87,6 +87,11 @@ extern int sepol_policydb_set_typevers(sepol_policydb_t *p, unsigned int type);
   */
  extern int sepol_policydb_set_vers(sepol_policydb_t *p, unsigned int vers);
 
+/*
+ * Get the policy version.
+ */
+extern unsigned int sepol_policydb_get_vers(const sepol_policydb_t *p);
+
  /* Set how to handle unknown class/perms. */
  #define SEPOL_DENY_UNKNOWN 0
  #define SEPOL_REJECT_UNKNOWN 2
@@ -94,12 +99,19 @@ extern int sepol_policydb_set_vers(sepol_policydb_t *p, unsigned int vers);
  extern int sepol_policydb_set_handle_unknown(sepol_policydb_t *p,
                                           unsigned int handle_unknown);
 
+/* Get how to handle unknown class/perms. */
+extern unsigned int
+sepol_policydb_get_handle_unknown(const sepol_policydb_t *p);
+
  /* Set the target platform */
  #define SEPOL_TARGET_SELINUX 0
  #define SEPOL_TARGET_XEN 1
  extern int sepol_policydb_set_target_platform(sepol_policydb_t *p,
                                            int target_platform);
 
+/* Get the target platform. */
+extern int sepol_policydb_get_target_platform(const sepol_policydb_t *p);
+
  /*
   * Optimize the policy by removing redundant rules.
   */
@@ -148,6 +160,23 @@ extern int sepol_policydb_mls_enabled(const sepol_policydb_t *p);
   */
  extern int sepol_policydb_compat_net(const sepol_policydb_t *p);
 
+/*
+ * Set permissive flags on types marked as permissive in the policy.
+ * Must be called after policy load to enable permissive type enforcement.
+ */
+extern int sepol_policydb_set_permissive_flags(sepol_handle_t *handle,
+                                            sepol_policydb_t *p);
+
+/*
+ * Rebuild the type attribute mapping. Call after modifying type attributes
+ * to ensure consistency.
+ *
+ * On out-of-memory, returns -1; the map may be only partially rebuilt in
+ * that case, but the policydb is left in a self-consistent, usable state.
Quoted 8 lines

libsepol/include/sepol/policydb.h extern int sepol_policydb_mls_enabled(const sepol_policydb_t *p); · line 179

Quoted 4 lines
libsepol/include/sepol/policydb.h +4 -0
+ */
+extern int sepol_policydb_rebuild_attr_map(sepol_handle_t *handle,
+                                        sepol_policydb_t *p);
+

I'd suggest a single public function that calls functions like these two, e.g. a function to initialize policy queries. Otherwise it exposes internal details that users don't need to be aware of.

Agreed; see also my comments on the cover-letter for this one, and I spell xperm_* UINT32_C(1) as 1U usually.

#14 lore raw

RFC PATCH 10/17 libsepol: add context resolution from policy values and SIDs

In reply to jan janasek

Add sepol_values_to_context_string()/sepol_values_to_context_record() to build a security context string or sepol_context_t from raw user/role/type values plus an optional MLS range string, without the caller ever touching context_struct_t.

Add sepol_sid_table_t, an opaque wrapper around sidtab_t (sepol_sid_table_new/load_isids/set_opaque/free), so callers can load and install a policy's initial-SID table without including policydb/sidtab.h directly.

Add sepol_policydb_sid_to_context_string()/_record() to resolve a SID to a context using only a given sepol_policydb_t, without requiring a process-global active policydb/sidtab via sepol_set_policydb()/ sepol_set_sidtab().

Assisted-by LLM Cursor
---
 libsepol/include/sepol/context.h              |  42 +++
 libsepol/include/sepol/flask.h                |  34 +++
 libsepol/include/sepol/policydb/flask_types.h |  55 +---
 libsepol/include/sepol/sid_table.h            |  43 +++
 libsepol/src/context.c                        | 279 ++++++++++++++++-
 libsepol/src/context_record.c                 |  62 +++-
 libsepol/src/services.c                       |  33 ++
 libsepol/src/sidtab.c                         |  78 +++++
 libsepol/tests/libsepol-tests.c               |   2 +
 libsepol/tests/policies/test-iter/iter.conf   |  10 +
 libsepol/tests/test-context-api.c             | 289 ++++++++++++++++++
 libsepol/tests/test-context-api.h             |  10 +
 12 files changed, 865 insertions(+), 72 deletions(-)
 create mode 100644 libsepol/include/sepol/flask.h
 create mode 100644 libsepol/include/sepol/sid_table.h
 create mode 100644 libsepol/tests/test-context-api.c
 create mode 100644 libsepol/tests/test-context-api.h
libsepol/include/sepol/context.h +42 -0
index b0e8701b..b79793b4 100644
@@ -4,6 +4,7 @@
 #include <sepol/context_record.h>
 #include <sepol/policydb.h>
 #include <sepol/handle.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -27,6 +28,47 @@ extern int sepol_mls_contains(sepol_handle_t *handle,
 extern int sepol_mls_check(sepol_handle_t *handle,
 			   const sepol_policydb_t *policydb, const char *mls);
 
+/*
+ * Build a security context string from policy-internal symbol values (1-based)
+ * and optional MLS range string (same MLS syntax as in full context strings).
+ * When MLS is disabled for the policy, mls_range must be NULL or empty.
+ */
+extern int sepol_values_to_context_string(sepol_handle_t *handle,
+					  const sepol_policydb_t *policydb,
+					  uint32_t user_val, uint32_t role_val,
+					  uint32_t type_val,
+					  const char *mls_range, char **out,
+					  size_t *out_len);
+
+extern int sepol_values_to_context_record(sepol_handle_t *handle,
+					  const sepol_policydb_t *policydb,
+					  uint32_t user_val, uint32_t role_val,
+					  uint32_t type_val,
+					  const char *mls_range,
+					  sepol_context_t **record);
+
+/*
+ * Resolve a SID to a context using only the given policydb (no process-global
+ * sepol_set_policydb / sepol_set_sidtab). Builds a temporary SID table from
+ * the policy's initial SID contexts on each call; for many lookups prefer
+ * sepol_sid_table_t with sepol_sid_table_load_isids().
+ */
+extern int
+sepol_policydb_sid_to_context_string(sepol_handle_t *handle,
+				     const sepol_policydb_t *policydb,
+				     uint32_t sid, char **out, size_t *out_len);
+
+extern int
+sepol_policydb_sid_to_context_record(sepol_handle_t *handle,
+				     const sepol_policydb_t *policydb,
+				     uint32_t sid, sepol_context_t **record);
+
+/*
+ * Legacy security-server helpers (sepol_sid_to_context,
+ * sepol_sid_to_context_record) remain in <sepol/policydb/services.h> and
+ * require an active process-global policydb and sidtab.
+ */
+
 #ifdef __cplusplus
 }
 #endif
libsepol/include/sepol/flask.h new file +34 -0
new file mode 100644
index 00000000..510af392
@@ -0,0 +1,34 @@
+#ifndef _SEPOL_FLASK_H_
+#define _SEPOL_FLASK_H_
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef char *sepol_security_context_t;
+typedef const char *sepol_const_security_context_t;
+typedef uint32_t sepol_access_vector_t;
+typedef uint16_t sepol_security_class_t;
+#define SEPOL_SECCLASS_NULL 0x0000
+
+#define SELINUX_MAGIC 0xf97cff8c
+#define SELINUX_MOD_MAGIC 0xf97cff8d
+
+typedef uint32_t sepol_security_id_t;
+#define SEPOL_SECSID_NULL 0
+
+struct sepol_av_decision {
+	sepol_access_vector_t allowed;
+	sepol_access_vector_t decided;
+	sepol_access_vector_t auditallow;
+	sepol_access_vector_t auditdeny;
+	uint32_t seqno;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/policydb/flask_types.h +4 -51

62 lines not on this page · raw message

libsepol/include/sepol/sid_table.h new file +43 -0

43 lines not on this page · raw message

libsepol/src/context.c +271 -8

320 lines not on this page · raw message

libsepol/src/context_record.c +49 -13

152 lines not on this page · raw message

libsepol/src/services.c +33 -0
index fdd84078..a44eb395 100644
@@ -48,6 +48,7 @@
 #define EXPR_BUF_SIZE 1024
 #define STACK_LEN 32
 
+#include <errno.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -127,6 +128,18 @@ int sepol_set_sidtab(sidtab_t *s)
 	return 0;
 }
 
+/*
+ * Returns the sidtab currently installed via sepol_set_sidtab(), or NULL
+ * if none is active. Used internally (e.g. by sepol_sid_table_free()) to
+ * detect whether a given sidtab is the one actually in use, since
+ * sepol_set_sidtab() can be called directly and bypass any bookkeeping
+ * done by higher-level wrappers such as sepol_sid_table_set_opaque().
+ */
+sidtab_t *sepol_get_sidtab(void)
+{
+	return sidtab;
+}
+
 int sepol_set_policydb(policydb_t *p)
 {
 	policydb = p;
@@ -1307,6 +1320,26 @@ out:
 	return rc;
 }
 
+int sepol_sid_to_context_record(sepol_handle_t *handle, sepol_security_id_t sid,
+				sepol_context_t **record)
+{
+	context_struct_t *context;
+	int rc;
+
+	if (!record)
+		return STATUS_ERR;
+	*record = NULL;
+
+	context = sepol_sidtab_search(sidtab, sid);
+	if (!context) {
+		ERR(handle, "unrecognized SID %u", sid);
+		return STATUS_ERR;
+	}
+
+	rc = context_to_record(handle, policydb, context, record);
+	return rc;
+}
+
 /*
  * Return a SID associated with the security context that
  * has the string representation specified by `scontext'.
libsepol/src/sidtab.c +78 -0

93 lines not on this page · raw message

libsepol/tests/libsepol-tests.c +2 -0
index a73686b9..c404937b 100644
@@ -26,6 +26,7 @@
 #include "test-downgrade.h"
 #include "test-neverallow.h"
 #include "test-iter.h"
+#include "test-context-api.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -76,6 +77,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(downgrade);
 	DECLARE_SUITE(neverallow);
 	DECLARE_SUITE(iter);
+	DECLARE_SUITE(context_api);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsepol/tests/policies/test-iter/iter.conf +10 -0
index fd27f5e0..6bfeeec0 100644
@@ -7,6 +7,15 @@ class CLASS05
 class CLASS06
 
 sid kernel
+#
+# A second initial SID whose context carries an actual (non-empty)
+# category set under MLS, so tests can verify MLS category handling
+# (e.g. the ebitmap deep-copy in policydb_sid_lookup_context()) without
+# having to fabricate an ad hoc SID at runtime. Under non-MLS builds
+# gen_context() below drops the category argument entirely, so this
+# just gets the same context as "kernel".
+#
+sid isid_mls_cat
 
 common COMMON1 { COMMONPERM1 }
 
@@ -105,6 +114,7 @@ constrain CLASS1 { PERM1 } (
 );
 
 sid kernel	gen_context(USER1:ROLE1:TYPE1, s0)
+sid isid_mls_cat	gen_context(USER1:ROLE1:TYPE1, `s0:c0,c1')
 
 fs_use_xattr ext3 gen_context(USER1:ROLE1:TYPE1, s0);
 fs_use_trans devpts gen_context(USER1:ROLE1:TYPE1, s0);
libsepol/tests/test-context-api.c new file +289 -0

289 lines not on this page · raw message

libsepol/tests/test-context-api.h new file +10 -0
new file mode 100644
index 00000000..fb5ec841
@@ -0,0 +1,10 @@
+#ifndef TEST_CONTEXT_API_H
+#define TEST_CONTEXT_API_H
+
+#include <CUnit/Basic.h>
+
+int context_api_test_init(void);
+int context_api_test_cleanup(void);
+int context_api_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.55.0
maintainer
#15 lore raw

Re: [RFC PATCH userspace 10/17] libsepol: add context resolution from policy values and SIDs

Quoted 53 lines · jan janasek

Add sepol_values_to_context_string()/sepol_values_to_context_record() to build a security context string or sepol_context_t from raw user/role/type values plus an optional MLS range string, without the caller ever touching context_struct_t.

Add sepol_sid_table_t, an opaque wrapper around sidtab_t (sepol_sid_table_new/load_isids/set_opaque/free), so callers can load and install a policy's initial-SID table without including policydb/sidtab.h directly.

Add sepol_policydb_sid_to_context_string()/_record() to resolve a SID to a context using only a given sepol_policydb_t, without requiring a process-global active policydb/sidtab via sepol_set_policydb()/ sepol_set_sidtab().

Assisted-by LLM Cursor

---

libsepol/include/sepol/sid_table.h new file +27 -0
new file mode 100644
index 00000000..64d08fc7
@@ -0,0 +1,43 @@
+#ifndef _SEPOL_SID_TABLE_H_
+#define _SEPOL_SID_TABLE_H_
+
+#include <sepol/policydb.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Opaque SID table for use with the security server helpers (sepol_sid_to_context,
+ * sepol_context_to_sid, etc.) without exposing sidtab_t.
+ */
+
+typedef struct sepol_sid_table sepol_sid_table_t;
+
+extern sepol_sid_table_t *sepol_sid_table_new(void);
+
+/*
+ * Frees a SID table created by sepol_sid_table_new(). If this table is
+ * currently installed as the process-global active table (via
+ * sepol_sid_table_set_opaque()), it is automatically uninstalled first,
+ * so subsequent calls into the security server helpers (sepol_sid_to_context,
+ * sepol_context_to_sid, etc.) will not dereference the freed table.
+ */
+extern void sepol_sid_table_free(sepol_sid_table_t *tab);
+

libsepol/include/sepol/sid_table.h line 37

Quoted 10 lines
libsepol/include/sepol/sid_table.h new file +10 -0
+extern int sepol_sid_table_load_isids(sepol_sid_table_t *tab,
+                                     sepol_policydb_t *policydb);
+
+/*
+ * Installs tab as the process-global active SID table used internally by
+ * the security server helpers (sepol_sid_to_context, sepol_context_to_sid,
+ * etc.). Only one table can be active at a time; calling this again with
+ * a different table replaces the previous one as the active table.
+ */
+extern int sepol_sid_table_set_opaque(sepol_sid_table_t *tab);

set_opaque() reads oddly; _install() or _activate() would say what it does.

Quoted 9 lines
libsepol/src/services.c +1 -0
index fdd84078..a44eb395 100644
@@ -127,6 +128,18 @@ int sepol_set_sidtab(sidtab_t *s)
        return 0;
 }
 
+/*

libsepol/src/services.c int sepol_set_sidtab(sidtab_t *s) · line 141

Quoted 10 lines
libsepol/src/services.c +10 -0
+ * Returns the sidtab currently installed via sepol_set_sidtab(), or NULL
+ * if none is active. Used internally (e.g. by sepol_sid_table_free()) to
+ * detect whether a given sidtab is the one actually in use, since
+ * sepol_set_sidtab() can be called directly and bypass any bookkeeping
+ * done by higher-level wrappers such as sepol_sid_table_set_opaque().
+ */
+sidtab_t *sepol_get_sidtab(void)
+{
+       return sidtab;
+}

Defined with no prior prototype; -Wmissing-prototypes flags it from a top-level build.
Declare in src/private.h or similar.

#16 lore raw

RFC PATCH 11/17 libsepol: add AV rule, conditional, and boolean iterators

In reply to jan janasek

Add sepol_avtab_iter for iterating AV/type/xperm rules with source/target type, class, and permission accessors, plus sepol_perm_iter and sepol_xperm_iter sub-iterators for decoding a permission bitmask or xperm bitmap into names/values.

Add sepol_cond_iter, sepol_cond_expr_iter, and sepol_cond_rule_iter for inspecting conditional expressions and their true/false rule lists, sharing accessor helpers with sepol_avtab_iter via av_rule_internal.h.

Add sepol_bool_iter, sepol_bool_value_to_name(), and sepol_bool_query_by_value() for boolean definitions. Fix a potential out-of-bounds access in bool_to_record and harden boolean_record accessors against NULL arguments.

Add sepol_policydb_av_to_string() and sepol_policydb_extended_perms_to_string(), public wrappers around the existing internal av-to-string helpers that take a sepol_policydb_t instead of an internal policydb_t.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/av_rule.h     | 119 ++++++++++
 libsepol/include/sepol/booleans.h    |  29 +++
 libsepol/include/sepol/conditional.h | 109 +++++++++
 libsepol/src/av_rule.c               | 328 +++++++++++++++++++++++++++
 libsepol/src/av_rule_internal.h      |  60 +++++
 libsepol/src/boolean_record.c        |  41 +++-
 libsepol/src/booleans.c              | 174 +++++++++++---
 libsepol/src/conditional_public.c    | 261 +++++++++++++++++++++
 8 files changed, 1085 insertions(+), 36 deletions(-)
 create mode 100644 libsepol/include/sepol/av_rule.h
 create mode 100644 libsepol/include/sepol/conditional.h
 create mode 100644 libsepol/src/av_rule.c
 create mode 100644 libsepol/src/av_rule_internal.h
 create mode 100644 libsepol/src/conditional_public.c
libsepol/include/sepol/av_rule.h new file +119 -0
new file mode 100644
index 00000000..0716bd22
@@ -0,0 +1,119 @@
+#ifndef _SEPOL_AV_RULE_H_
+#define _SEPOL_AV_RULE_H_
+
+#include <sepol/handle.h>
+#include <sepol/policydb.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* --- Avtab entry iterator --- */
+
+struct sepol_avtab_iter;
+typedef struct sepol_avtab_iter sepol_avtab_iter_t;
+
+/*
+ * Create an iterator over unconditional TE rules (te_avtab).
+ * If 'specified' is 0, all rule types are returned. Otherwise it is a
+ * bitmask of AVTAB_* flags (from constants.h) to filter by.
+ *
+ * Iteration order is unspecified (currently hash-bucket order of the
+ * internal avtab) and may change between library versions or policydb
+ * loads; callers must not rely on it matching rule declaration order.
+ */
+extern int sepol_avtab_iter_create(sepol_handle_t *handle,
+				   const sepol_policydb_t *p,
+				   uint32_t specified,
+				   sepol_avtab_iter_t **iter);
+
+extern void sepol_avtab_iter_destroy(sepol_avtab_iter_t *iter);
+
+/*
+ * Advance to the next entry. When done, sets *ruletype to 0.
+ * Values are 1-based; resolve with sepol_*_value_to_name().
+ */
+extern int sepol_avtab_iter_next(sepol_handle_t *handle,
+				 sepol_avtab_iter_t *iter, uint32_t *ruletype,
+				 uint32_t *source_type, uint32_t *target_type,
+				 uint32_t *target_class);
+
+/* For AV rules: permission bitmask (inverted for AVTAB_AUDITDENY). */
+extern uint32_t sepol_avtab_iter_get_av_data(const sepol_avtab_iter_t *iter);
+
+/* For type rules: 1-based default type value. */
+extern uint32_t sepol_avtab_iter_get_type_data(const sepol_avtab_iter_t *iter);
+
+/* For xperm rules: kind (AVTAB_XPERMS_IOCTLFUNCTION etc.). */
+extern uint8_t sepol_avtab_iter_get_xperm_type(const sepol_avtab_iter_t *iter);
+
+/* For xperm rules: driver/command group. */
+extern uint8_t
+sepol_avtab_iter_get_xperm_driver(const sepol_avtab_iter_t *iter);
+
+/* --- Permission name sub-iterator --- */
+
+struct sepol_perm_iter;
+typedef struct sepol_perm_iter sepol_perm_iter_t;
+
+/*
+ * Decode a permission bitmask into individual permission names for
+ * the given class. 'target_class' is 1-based.
+ */
+extern int sepol_perm_iter_create(sepol_handle_t *handle,
+				  const sepol_policydb_t *p,
+				  uint32_t target_class, uint32_t perm_data,
+				  sepol_perm_iter_t **iter);
+
+extern void sepol_perm_iter_destroy(sepol_perm_iter_t *iter);
+
+/* Returns the next permission name, or NULL when done.
+ * The pointer is borrowed from the policydb and must not be freed.
+ * It remains valid only while the sepol_policydb_t is alive. */
+extern int sepol_perm_iter_next(sepol_handle_t *handle, sepol_perm_iter_t *iter,
+				const char **perm_name);
+
+/* --- Extended permission value sub-iterator --- */
+
+struct sepol_xperm_iter;
+typedef struct sepol_xperm_iter sepol_xperm_iter_t;
+
+/*
+ * Create an iterator over xperm values (0-255) from the current
+ * avtab entry's xperms bitmap.
+ */
+extern int sepol_xperm_iter_create(sepol_handle_t *handle,
+				   const sepol_avtab_iter_t *iter,
+				   sepol_xperm_iter_t **xperm_iter);
+
+extern void sepol_xperm_iter_destroy(sepol_xperm_iter_t *xperm_iter);
+
+/* Get the next xperm value. Sets *has_next to 0 when done. */
+extern int sepol_xperm_iter_next(sepol_handle_t *handle,
+				 sepol_xperm_iter_t *xperm_iter,
+				 uint16_t *value, int *has_next);
+
+/*
+ * Format an access-vector permission bitmask as a space-separated list of
+ * permission names for the given class (1-based). Returns a newly allocated
+ * string (caller must free), or NULL on error.
+ */
+extern char *sepol_policydb_av_to_string(const sepol_policydb_t *policydb,
+					 uint32_t class_val, uint32_t av);
+
+/*
+ * Format extended permissions (ioctl driver/function or netlink message) as a
+ * human-readable string. 'specified' is an AVTAB_XPERMS_* kind,
+ * 'driver' is the driver/group byte, and 'perms' is the 8-byte bitmap.
+ * Returns a newly allocated string (caller must free), or NULL on error.
+ */
+extern char *sepol_policydb_extended_perms_to_string(uint8_t specified,
+						     uint8_t driver,
+						     const uint32_t perms[8]);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/booleans.h +29 -0
index 55029901..ed68558d 100644
@@ -2,6 +2,7 @@
 #define _SEPOL_BOOLEANS_H_
 
 #include <stddef.h>
+#include <stdint.h>
 #include <sepol/policydb.h>
 #include <sepol/boolean_record.h>
 #include <sepol/handle.h>
@@ -40,6 +41,34 @@ sepol_bool_iterate(sepol_handle_t *handle, const sepol_policydb_t *policydb,
 		   int (*fn)(const sepol_bool_t *boolean, void *fn_arg),
 		   void *arg);
 
+/* Look up the name of a boolean by its 1-based value.
+ * Returns non-zero on invalid value.
+ * The returned pointer is borrowed from the policydb; do not free. */
+extern int sepol_bool_value_to_name(sepol_handle_t *handle,
+				    const sepol_policydb_t *p, uint32_t value,
+				    const char **name);
+
+extern int sepol_bool_query_by_value(sepol_handle_t *handle,
+				     const sepol_policydb_t *p, uint32_t value,
+				     sepol_bool_t **response);
+
+typedef struct sepol_bool_iter sepol_bool_iter_t;
+
+/*
+ * Iteration order follows ascending internal value assignment, which
+ * typically -- but is not guaranteed to -- match declaration order;
+ * callers should not treat it as a stable guarantee across policydb
+ * versions.
+ */
+extern int sepol_bool_iter_create(sepol_handle_t *handle,
+				  const sepol_policydb_t *p,
+				  sepol_bool_iter_t **iter);
+
+extern void sepol_bool_iter_destroy(sepol_bool_iter_t *iter);
+
+extern int sepol_bool_iter_next(sepol_handle_t *handle, sepol_bool_iter_t *iter,
+				sepol_bool_t **item);
+
 #ifdef __cplusplus
 }
 #endif
libsepol/include/sepol/conditional.h new file +109 -0

109 lines not on this page · raw message

libsepol/src/av_rule.c new file +328 -0

328 lines not on this page · raw message

libsepol/src/av_rule_internal.h new file +60 -0

60 lines not on this page · raw message

libsepol/src/boolean_record.c +37 -4

113 lines not on this page · raw message

libsepol/src/booleans.c +142 -32

253 lines not on this page · raw message

libsepol/src/conditional_public.c new file +261 -0

261 lines not on this page · raw message

Signature · 1 line
2.55.0
#17 lore raw

RFC PATCH 12/17 libsepol: add AV rule and conditional iterator tests

In reply to jan janasek

Cover sepol_avtab_iter, sepol_perm_iter, sepol_xperm_iter, sepol_cond_iter/cond_expr_iter/cond_rule_iter, and sepol_bool_iter against the test-iter fixture, including NULL-argument and out-of-range value_to_name regression tests.

Assisted-by LLM Cursor
---
 libsepol/tests/libsepol-tests.c |   2 +
 libsepol/tests/test-av-rule.c   | 733 ++++++++++++++++++++++++++++++++
 libsepol/tests/test-av-rule.h   |  10 +
 3 files changed, 745 insertions(+)
 create mode 100644 libsepol/tests/test-av-rule.c
 create mode 100644 libsepol/tests/test-av-rule.h
libsepol/tests/libsepol-tests.c +2 -0
index c404937b..707937cf 100644
@@ -27,6 +27,7 @@
 #include "test-neverallow.h"
 #include "test-iter.h"
 #include "test-context-api.h"
+#include "test-av-rule.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -78,6 +79,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(neverallow);
 	DECLARE_SUITE(iter);
 	DECLARE_SUITE(context_api);
+	DECLARE_SUITE(av_rule);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsepol/tests/test-av-rule.c new file +733 -0

733 lines not on this page · raw message

libsepol/tests/test-av-rule.h new file +10 -0
new file mode 100644
index 00000000..7c0b9e47
@@ -0,0 +1,10 @@
+#ifndef TEST_AV_RULE_H
+#define TEST_AV_RULE_H
+
+#include <CUnit/Basic.h>
+
+int av_rule_test_init(void);
+int av_rule_test_cleanup(void);
+int av_rule_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.55.0
#18 lore raw

RFC PATCH 13/17 libsepol: add ocontext iterators for initial SIDs and named contexts

In reply to jan janasek

Add per-kind ocontext iterators (initial SIDs, fs_use, genfscon, netifcon, node/node6, port, ibpkey, ibendport, pirq/iomem/ioport/ pcidevice/devicetree for Xen) with context and name/address accessors, deduplicating ~100 near-identical accessor bodies into DEFINE_OCON_CONTEXT_GETTERS macro instances.

Extract the initial-SID name table (indexed by SID value, not stored in the binary policy) into a shared libsepol/src/initial_sids.h, used by both sepol_isid_iter_get_name() here and the existing kernel_to_common.c CIL/conf writers, instead of duplicating the table.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/ocontext.h |  513 ++++++++++++++
 libsepol/src/initial_sids.h       |   29 +
 libsepol/src/kernel_to_common.h   |   22 +-
 libsepol/src/ocontext.c           | 1058 +++++++++++++++++++++++++++++
 4 files changed, 1602 insertions(+), 20 deletions(-)
 create mode 100644 libsepol/include/sepol/ocontext.h
 create mode 100644 libsepol/src/initial_sids.h
 create mode 100644 libsepol/src/ocontext.c
libsepol/include/sepol/ocontext.h new file +513 -0
new file mode 100644
index 00000000..195359fa
@@ -0,0 +1,513 @@
+#ifndef _SEPOL_OCONTEXT_H_
+#define _SEPOL_OCONTEXT_H_
+
+#include <sepol/cat_set.h>
+#include <sepol/handle.h>
+#include <sepol/policydb.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * NOTE on every sepol_cat_set_iter_t **cat_iter parameter below: see the
+ * ownership contract documented in cat_set.h. In short, the callee always
+ * allocates a new, caller-owned iterator that must be freed with
+ * sepol_cat_set_iter_destroy() -- do not skip destroying it just because
+ * the underlying category set turned out to be empty.
+ *
+ * NOTE on every `const char **`/`const uint32_t **` out-parameter
+ * populated by an `_iter_next()` function below (e.g. fsuse's name,
+ * genfscon's fstype/path, netifcon's name, ibendportcon's dev_name,
+ * devicetreecon's path, nodecon6's addr/mask): the returned pointer is
+ * borrowed from the policydb. It remains valid only until the next call
+ * on the same iterator (`_iter_next()` or `_destroy()`) and must not be
+ * freed by the caller.
+ */
+
+/* Initial SID iterator */
+
+typedef struct sepol_isid_iter sepol_isid_iter_t;
+
+extern int sepol_isid_iter_create(sepol_handle_t *handle,
+				  const sepol_policydb_t *p,
+				  sepol_isid_iter_t **iter);
+
+extern void sepol_isid_iter_destroy(sepol_isid_iter_t *iter);
+
+extern int sepol_isid_iter_next(sepol_handle_t *handle, sepol_isid_iter_t *iter,
+				uint32_t *sid);
+
+extern uint32_t sepol_isid_iter_get_context_user(const sepol_isid_iter_t *iter);
+extern uint32_t sepol_isid_iter_get_context_role(const sepol_isid_iter_t *iter);
+extern uint32_t sepol_isid_iter_get_context_type(const sepol_isid_iter_t *iter);
+extern uint32_t
+sepol_isid_iter_get_mls_range_low_sens(const sepol_isid_iter_t *iter);
+extern uint32_t
+sepol_isid_iter_get_mls_range_high_sens(const sepol_isid_iter_t *iter);
+extern int
+sepol_isid_iter_get_mls_range_low_cat(const sepol_isid_iter_t *iter,
+				      sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_isid_iter_get_mls_range_high_cat(const sepol_isid_iter_t *iter,
+				       sepol_cat_set_iter_t **cat_iter);
+
+/* Returns the initial SID name.
+ * The pointer is borrowed from the policydb; do not free. */
+extern const char *sepol_isid_iter_get_name(const sepol_isid_iter_t *iter);
+
+/* fs_use iterator */
+
+typedef struct sepol_fsuse_iter sepol_fsuse_iter_t;
+
+extern int sepol_fsuse_iter_create(sepol_handle_t *handle,
+				   const sepol_policydb_t *p,
+				   sepol_fsuse_iter_t **iter);
+
+extern void sepol_fsuse_iter_destroy(sepol_fsuse_iter_t *iter);
+
+extern int sepol_fsuse_iter_next(sepol_handle_t *handle,
+				 sepol_fsuse_iter_t *iter, const char **name,
+				 uint32_t *behavior);
+
+extern uint32_t
+sepol_fsuse_iter_get_context_user(const sepol_fsuse_iter_t *iter);
+extern uint32_t
+sepol_fsuse_iter_get_context_role(const sepol_fsuse_iter_t *iter);
+extern uint32_t
+sepol_fsuse_iter_get_context_type(const sepol_fsuse_iter_t *iter);
+extern uint32_t
+sepol_fsuse_iter_get_mls_range_low_sens(const sepol_fsuse_iter_t *iter);
+extern uint32_t
+sepol_fsuse_iter_get_mls_range_high_sens(const sepol_fsuse_iter_t *iter);
+extern int
+sepol_fsuse_iter_get_mls_range_low_cat(const sepol_fsuse_iter_t *iter,
+				       sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_fsuse_iter_get_mls_range_high_cat(const sepol_fsuse_iter_t *iter,
+					sepol_cat_set_iter_t **cat_iter);
+
+/* genfscon iterator */
+
+typedef struct sepol_genfscon_iter sepol_genfscon_iter_t;
+
+extern int sepol_genfscon_iter_create(sepol_handle_t *handle,
+				      const sepol_policydb_t *p,
+				      sepol_genfscon_iter_t **iter);
+
+extern void sepol_genfscon_iter_destroy(sepol_genfscon_iter_t *iter);
+
+extern int sepol_genfscon_iter_next(sepol_handle_t *handle,
+				    sepol_genfscon_iter_t *iter,
+				    const char **fstype, const char **path,
+				    uint32_t *sclass);
+
+extern uint32_t
+sepol_genfscon_iter_get_context_user(const sepol_genfscon_iter_t *iter);
+extern uint32_t
+sepol_genfscon_iter_get_context_role(const sepol_genfscon_iter_t *iter);
+extern uint32_t
+sepol_genfscon_iter_get_context_type(const sepol_genfscon_iter_t *iter);
+extern uint32_t
+sepol_genfscon_iter_get_mls_range_low_sens(const sepol_genfscon_iter_t *iter);
+extern uint32_t
+sepol_genfscon_iter_get_mls_range_high_sens(const sepol_genfscon_iter_t *iter);
+extern int
+sepol_genfscon_iter_get_mls_range_low_cat(const sepol_genfscon_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_genfscon_iter_get_mls_range_high_cat(const sepol_genfscon_iter_t *iter,
+					   sepol_cat_set_iter_t **cat_iter);
+
+/* portcon iterator */
+
+typedef struct sepol_portcon_iter sepol_portcon_iter_t;
+
+extern int sepol_portcon_iter_create(sepol_handle_t *handle,
+				     const sepol_policydb_t *p,
+				     sepol_portcon_iter_t **iter);
+
+extern void sepol_portcon_iter_destroy(sepol_portcon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; the other
+ * out-parameters cannot be used to detect end-of-iteration since 0
+ * is a valid port value. */
+extern int sepol_portcon_iter_next(sepol_handle_t *handle,
+				   sepol_portcon_iter_t *iter,
+				   uint8_t *protocol, uint16_t *low_port,
+				   uint16_t *high_port, int *has_next);
+
+extern uint32_t
+sepol_portcon_iter_get_context_user(const sepol_portcon_iter_t *iter);
+extern uint32_t
+sepol_portcon_iter_get_context_role(const sepol_portcon_iter_t *iter);
+extern uint32_t
+sepol_portcon_iter_get_context_type(const sepol_portcon_iter_t *iter);
+extern uint32_t
+sepol_portcon_iter_get_mls_range_low_sens(const sepol_portcon_iter_t *iter);
+extern uint32_t
+sepol_portcon_iter_get_mls_range_high_sens(const sepol_portcon_iter_t *iter);
+extern int
+sepol_portcon_iter_get_mls_range_low_cat(const sepol_portcon_iter_t *iter,
+					 sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_portcon_iter_get_mls_range_high_cat(const sepol_portcon_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+
+/* netifcon iterator */
+
+typedef struct sepol_netifcon_iter sepol_netifcon_iter_t;
+
+extern int sepol_netifcon_iter_create(sepol_handle_t *handle,
+				      const sepol_policydb_t *p,
+				      sepol_netifcon_iter_t **iter);
+
+extern void sepol_netifcon_iter_destroy(sepol_netifcon_iter_t *iter);
+
+extern int sepol_netifcon_iter_next(sepol_handle_t *handle,
+				    sepol_netifcon_iter_t *iter,
+				    const char **name);
+
+extern uint32_t
+sepol_netifcon_iter_get_context_user(const sepol_netifcon_iter_t *iter);
+extern uint32_t
+sepol_netifcon_iter_get_context_role(const sepol_netifcon_iter_t *iter);
+extern uint32_t
+sepol_netifcon_iter_get_context_type(const sepol_netifcon_iter_t *iter);
+extern uint32_t
+sepol_netifcon_iter_get_mls_range_low_sens(const sepol_netifcon_iter_t *iter);
+extern uint32_t
+sepol_netifcon_iter_get_mls_range_high_sens(const sepol_netifcon_iter_t *iter);
+extern int
+sepol_netifcon_iter_get_mls_range_low_cat(const sepol_netifcon_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_netifcon_iter_get_mls_range_high_cat(const sepol_netifcon_iter_t *iter,
+					   sepol_cat_set_iter_t **cat_iter);
+
+extern uint32_t
+sepol_netifcon_iter_get_msg_context_user(const sepol_netifcon_iter_t *iter);
+extern uint32_t
+sepol_netifcon_iter_get_msg_context_role(const sepol_netifcon_iter_t *iter);
+extern uint32_t
+sepol_netifcon_iter_get_msg_context_type(const sepol_netifcon_iter_t *iter);
+extern uint32_t sepol_netifcon_iter_get_msg_mls_range_low_sens(
+	const sepol_netifcon_iter_t *iter);
+extern uint32_t sepol_netifcon_iter_get_msg_mls_range_high_sens(
+	const sepol_netifcon_iter_t *iter);
+extern int
+sepol_netifcon_iter_get_msg_mls_range_low_cat(const sepol_netifcon_iter_t *iter,
+					      sepol_cat_set_iter_t **cat_iter);
+extern int sepol_netifcon_iter_get_msg_mls_range_high_cat(
+	const sepol_netifcon_iter_t *iter, sepol_cat_set_iter_t **cat_iter);
+
+/* nodecon IPv4 iterator */
+
+typedef struct sepol_nodecon_iter sepol_nodecon_iter_t;
+
+extern int sepol_nodecon_iter_create(sepol_handle_t *handle,
+				     const sepol_policydb_t *p,
+				     sepol_nodecon_iter_t **iter);
+
+extern void sepol_nodecon_iter_destroy(sepol_nodecon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; addr/mask cannot
+ * be used to detect end-of-iteration since 0.0.0.0/0.0.0.0 is a
+ * valid nodecon entry. addr and mask are each a single IPv4 address
+ * or mask in network byte order (big-endian), as they appear in the
+ * policy source (e.g. "nodecon 192.168.1.0 255.255.255.0 ..."). */
+extern int sepol_nodecon_iter_next(sepol_handle_t *handle,
+				   sepol_nodecon_iter_t *iter, uint32_t *addr,
+				   uint32_t *mask, int *has_next);
+
+extern uint32_t
+sepol_nodecon_iter_get_context_user(const sepol_nodecon_iter_t *iter);
+extern uint32_t
+sepol_nodecon_iter_get_context_role(const sepol_nodecon_iter_t *iter);
+extern uint32_t
+sepol_nodecon_iter_get_context_type(const sepol_nodecon_iter_t *iter);
+extern uint32_t
+sepol_nodecon_iter_get_mls_range_low_sens(const sepol_nodecon_iter_t *iter);
+extern uint32_t
+sepol_nodecon_iter_get_mls_range_high_sens(const sepol_nodecon_iter_t *iter);
+extern int
+sepol_nodecon_iter_get_mls_range_low_cat(const sepol_nodecon_iter_t *iter,
+					 sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_nodecon_iter_get_mls_range_high_cat(const sepol_nodecon_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+
+/* nodecon6 IPv6 iterator */
+
+typedef struct sepol_nodecon6_iter sepol_nodecon6_iter_t;
+
+extern int sepol_nodecon6_iter_create(sepol_handle_t *handle,
+				      const sepol_policydb_t *p,
+				      sepol_nodecon6_iter_t **iter);
+
+extern void sepol_nodecon6_iter_destroy(sepol_nodecon6_iter_t *iter);
+
+/* addr and mask are each set to point at a borrowed, 4-element array
+ * of uint32_t (the 128-bit IPv6 address or mask split into four
+ * 32-bit words, each in network byte order (big-endian)), matching
+ * struct ocontext_t's u.node6.addr / u.node6.mask. The pointers are
+ * valid only until the next call on this iterator. */
+extern int sepol_nodecon6_iter_next(sepol_handle_t *handle,
+				    sepol_nodecon6_iter_t *iter,
+				    const uint32_t **addr,
+				    const uint32_t **mask);
+
+extern uint32_t
+sepol_nodecon6_iter_get_context_user(const sepol_nodecon6_iter_t *iter);
+extern uint32_t
+sepol_nodecon6_iter_get_context_role(const sepol_nodecon6_iter_t *iter);
+extern uint32_t
+sepol_nodecon6_iter_get_context_type(const sepol_nodecon6_iter_t *iter);
+extern uint32_t
+sepol_nodecon6_iter_get_mls_range_low_sens(const sepol_nodecon6_iter_t *iter);
+extern uint32_t
+sepol_nodecon6_iter_get_mls_range_high_sens(const sepol_nodecon6_iter_t *iter);
+extern int
+sepol_nodecon6_iter_get_mls_range_low_cat(const sepol_nodecon6_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_nodecon6_iter_get_mls_range_high_cat(const sepol_nodecon6_iter_t *iter,
+					   sepol_cat_set_iter_t **cat_iter);
+
+/* ibpkeycon iterator */
+
+typedef struct sepol_ibpkeycon_iter sepol_ibpkeycon_iter_t;
+
+extern int sepol_ibpkeycon_iter_create(sepol_handle_t *handle,
+				       const sepol_policydb_t *p,
+				       sepol_ibpkeycon_iter_t **iter);
+
+extern void sepol_ibpkeycon_iter_destroy(sepol_ibpkeycon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; the other
+ * out-parameters cannot be used to detect end-of-iteration since 0
+ * is a valid pkey value. */
+extern int sepol_ibpkeycon_iter_next(sepol_handle_t *handle,
+				     sepol_ibpkeycon_iter_t *iter,
+				     uint64_t *subnet_prefix,
+				     uint16_t *low_pkey, uint16_t *high_pkey,
+				     int *has_next);
+
+extern uint32_t
+sepol_ibpkeycon_iter_get_context_user(const sepol_ibpkeycon_iter_t *iter);
+extern uint32_t
+sepol_ibpkeycon_iter_get_context_role(const sepol_ibpkeycon_iter_t *iter);
+extern uint32_t
+sepol_ibpkeycon_iter_get_context_type(const sepol_ibpkeycon_iter_t *iter);
+extern uint32_t
+sepol_ibpkeycon_iter_get_mls_range_low_sens(const sepol_ibpkeycon_iter_t *iter);
+extern uint32_t sepol_ibpkeycon_iter_get_mls_range_high_sens(
+	const sepol_ibpkeycon_iter_t *iter);
+extern int
+sepol_ibpkeycon_iter_get_mls_range_low_cat(const sepol_ibpkeycon_iter_t *iter,
+					   sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_ibpkeycon_iter_get_mls_range_high_cat(const sepol_ibpkeycon_iter_t *iter,
+					    sepol_cat_set_iter_t **cat_iter);
+
+/* ibendportcon iterator */
+
+typedef struct sepol_ibendportcon_iter sepol_ibendportcon_iter_t;
+
+extern int sepol_ibendportcon_iter_create(sepol_handle_t *handle,
+					  const sepol_policydb_t *p,
+					  sepol_ibendportcon_iter_t **iter);
+
+extern void sepol_ibendportcon_iter_destroy(sepol_ibendportcon_iter_t *iter);
+
+extern int sepol_ibendportcon_iter_next(sepol_handle_t *handle,
+					sepol_ibendportcon_iter_t *iter,
+					const char **dev_name, uint8_t *port);
+
+extern uint32_t
+sepol_ibendportcon_iter_get_context_user(const sepol_ibendportcon_iter_t *iter);
+extern uint32_t
+sepol_ibendportcon_iter_get_context_role(const sepol_ibendportcon_iter_t *iter);
+extern uint32_t
+sepol_ibendportcon_iter_get_context_type(const sepol_ibendportcon_iter_t *iter);
+extern uint32_t sepol_ibendportcon_iter_get_mls_range_low_sens(
+	const sepol_ibendportcon_iter_t *iter);
+extern uint32_t sepol_ibendportcon_iter_get_mls_range_high_sens(
+	const sepol_ibendportcon_iter_t *iter);
+extern int sepol_ibendportcon_iter_get_mls_range_low_cat(
+	const sepol_ibendportcon_iter_t *iter, sepol_cat_set_iter_t **cat_iter);
+extern int sepol_ibendportcon_iter_get_mls_range_high_cat(
+	const sepol_ibendportcon_iter_t *iter, sepol_cat_set_iter_t **cat_iter);
+
+/* Xen pirqcon iterator */
+
+typedef struct sepol_pirqcon_iter sepol_pirqcon_iter_t;
+
+extern int sepol_pirqcon_iter_create(sepol_handle_t *handle,
+				     const sepol_policydb_t *p,
+				     sepol_pirqcon_iter_t **iter);
+
+extern void sepol_pirqcon_iter_destroy(sepol_pirqcon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; irq cannot be
+ * used to detect end-of-iteration since 0 is a valid IRQ value. */
+extern int sepol_pirqcon_iter_next(sepol_handle_t *handle,
+				   sepol_pirqcon_iter_t *iter, uint32_t *irq,
+				   int *has_next);
+
+extern uint32_t
+sepol_pirqcon_iter_get_context_user(const sepol_pirqcon_iter_t *iter);
+extern uint32_t
+sepol_pirqcon_iter_get_context_role(const sepol_pirqcon_iter_t *iter);
+extern uint32_t
+sepol_pirqcon_iter_get_context_type(const sepol_pirqcon_iter_t *iter);
+extern uint32_t
+sepol_pirqcon_iter_get_mls_range_low_sens(const sepol_pirqcon_iter_t *iter);
+extern uint32_t
+sepol_pirqcon_iter_get_mls_range_high_sens(const sepol_pirqcon_iter_t *iter);
+extern int
+sepol_pirqcon_iter_get_mls_range_low_cat(const sepol_pirqcon_iter_t *iter,
+					 sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_pirqcon_iter_get_mls_range_high_cat(const sepol_pirqcon_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+
+/* Xen iomemcon iterator */
+
+typedef struct sepol_iomemcon_iter sepol_iomemcon_iter_t;
+
+extern int sepol_iomemcon_iter_create(sepol_handle_t *handle,
+				      const sepol_policydb_t *p,
+				      sepol_iomemcon_iter_t **iter);
+
+extern void sepol_iomemcon_iter_destroy(sepol_iomemcon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; low_iomem/
+ * high_iomem cannot be used to detect end-of-iteration since 0 is a
+ * valid address value. */
+extern int sepol_iomemcon_iter_next(sepol_handle_t *handle,
+				    sepol_iomemcon_iter_t *iter,
+				    uint64_t *low_iomem, uint64_t *high_iomem,
+				    int *has_next);
+
+extern uint32_t
+sepol_iomemcon_iter_get_context_user(const sepol_iomemcon_iter_t *iter);
+extern uint32_t
+sepol_iomemcon_iter_get_context_role(const sepol_iomemcon_iter_t *iter);
+extern uint32_t
+sepol_iomemcon_iter_get_context_type(const sepol_iomemcon_iter_t *iter);
+extern uint32_t
+sepol_iomemcon_iter_get_mls_range_low_sens(const sepol_iomemcon_iter_t *iter);
+extern uint32_t
+sepol_iomemcon_iter_get_mls_range_high_sens(const sepol_iomemcon_iter_t *iter);
+extern int
+sepol_iomemcon_iter_get_mls_range_low_cat(const sepol_iomemcon_iter_t *iter,
+					  sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_iomemcon_iter_get_mls_range_high_cat(const sepol_iomemcon_iter_t *iter,
+					   sepol_cat_set_iter_t **cat_iter);
+
+/* Xen ioportcon iterator */
+
+typedef struct sepol_ioportcon_iter sepol_ioportcon_iter_t;
+
+extern int sepol_ioportcon_iter_create(sepol_handle_t *handle,
+				       const sepol_policydb_t *p,
+				       sepol_ioportcon_iter_t **iter);
+
+extern void sepol_ioportcon_iter_destroy(sepol_ioportcon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; low_ioport/
+ * high_ioport cannot be used to detect end-of-iteration since 0 is
+ * a valid address value. */
+extern int sepol_ioportcon_iter_next(sepol_handle_t *handle,
+				     sepol_ioportcon_iter_t *iter,
+				     uint32_t *low_ioport,
+				     uint32_t *high_ioport, int *has_next);
+
+extern uint32_t
+sepol_ioportcon_iter_get_context_user(const sepol_ioportcon_iter_t *iter);
+extern uint32_t
+sepol_ioportcon_iter_get_context_role(const sepol_ioportcon_iter_t *iter);
+extern uint32_t
+sepol_ioportcon_iter_get_context_type(const sepol_ioportcon_iter_t *iter);
+extern uint32_t
+sepol_ioportcon_iter_get_mls_range_low_sens(const sepol_ioportcon_iter_t *iter);
+extern uint32_t sepol_ioportcon_iter_get_mls_range_high_sens(
+	const sepol_ioportcon_iter_t *iter);
+extern int
+sepol_ioportcon_iter_get_mls_range_low_cat(const sepol_ioportcon_iter_t *iter,
+					   sepol_cat_set_iter_t **cat_iter);
+extern int
+sepol_ioportcon_iter_get_mls_range_high_cat(const sepol_ioportcon_iter_t *iter,
+					    sepol_cat_set_iter_t **cat_iter);
+
+/* Xen pcidevicecon iterator */
+
+typedef struct sepol_pcidevicecon_iter sepol_pcidevicecon_iter_t;
+
+extern int sepol_pcidevicecon_iter_create(sepol_handle_t *handle,
+					  const sepol_policydb_t *p,
+					  sepol_pcidevicecon_iter_t **iter);
+
+extern void sepol_pcidevicecon_iter_destroy(sepol_pcidevicecon_iter_t *iter);
+
+/* has_next is set to 0 once iteration is exhausted; device cannot
+ * be used to detect end-of-iteration since 0 is a valid device
+ * value. */
+extern int sepol_pcidevicecon_iter_next(sepol_handle_t *handle,
+					sepol_pcidevicecon_iter_t *iter,
+					uint32_t *device, int *has_next);
+
+extern uint32_t
+sepol_pcidevicecon_iter_get_context_user(const sepol_pcidevicecon_iter_t *iter);
+extern uint32_t
+sepol_pcidevicecon_iter_get_context_role(const sepol_pcidevicecon_iter_t *iter);
+extern uint32_t
+sepol_pcidevicecon_iter_get_context_type(const sepol_pcidevicecon_iter_t *iter);
+extern uint32_t sepol_pcidevicecon_iter_get_mls_range_low_sens(
+	const sepol_pcidevicecon_iter_t *iter);
+extern uint32_t sepol_pcidevicecon_iter_get_mls_range_high_sens(
+	const sepol_pcidevicecon_iter_t *iter);
+extern int sepol_pcidevicecon_iter_get_mls_range_low_cat(
+	const sepol_pcidevicecon_iter_t *iter, sepol_cat_set_iter_t **cat_iter);
+extern int sepol_pcidevicecon_iter_get_mls_range_high_cat(
+	const sepol_pcidevicecon_iter_t *iter, sepol_cat_set_iter_t **cat_iter);
+
+/* Xen devicetreecon iterator */
+
+typedef struct sepol_devicetreecon_iter sepol_devicetreecon_iter_t;
+
+extern int sepol_devicetreecon_iter_create(sepol_handle_t *handle,
+					   const sepol_policydb_t *p,
+					   sepol_devicetreecon_iter_t **iter);
+
+extern void sepol_devicetreecon_iter_destroy(sepol_devicetreecon_iter_t *iter);
+
+extern int sepol_devicetreecon_iter_next(sepol_handle_t *handle,
+					 sepol_devicetreecon_iter_t *iter,
+					 const char **path);
+
+extern uint32_t sepol_devicetreecon_iter_get_context_user(
+	const sepol_devicetreecon_iter_t *iter);
+extern uint32_t sepol_devicetreecon_iter_get_context_role(
+	const sepol_devicetreecon_iter_t *iter);
+extern uint32_t sepol_devicetreecon_iter_get_context_type(
+	const sepol_devicetreecon_iter_t *iter);
+extern uint32_t sepol_devicetreecon_iter_get_mls_range_low_sens(
+	const sepol_devicetreecon_iter_t *iter);
+extern uint32_t sepol_devicetreecon_iter_get_mls_range_high_sens(
+	const sepol_devicetreecon_iter_t *iter);
+extern int sepol_devicetreecon_iter_get_mls_range_low_cat(
+	const sepol_devicetreecon_iter_t *iter,
+	sepol_cat_set_iter_t **cat_iter);
+extern int sepol_devicetreecon_iter_get_mls_range_high_cat(
+	const sepol_devicetreecon_iter_t *iter,
+	sepol_cat_set_iter_t **cat_iter);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/src/initial_sids.h new file +29 -0
new file mode 100644
index 00000000..dfab9907
@@ -0,0 +1,29 @@
+#ifndef _SEPOL_INITIAL_SIDS_H_
+#define _SEPOL_INITIAL_SIDS_H_
+
+/*
+ * Initial SID (isid) name tables, indexed by SID value. Initial SID names
+ * aren't actually stored in the binary/pp policy files, so callers that
+ * need to print or look up an initial SID by name (kernel_to_common.c's
+ * CIL/conf writers, and the sepol_isid_iter_t public API in ocontext.c)
+ * share this single mapping, taken from the Linux kernel.
+ */
+static const char *const selinux_sid_to_str[] = {
+	NULL,	"kernel", "security",	"unlabeled", NULL,    "file",
+	NULL,	"init",	  "any_socket", "port",	     "netif", "netmsg",
+	"node", NULL,	  NULL,		NULL,	     NULL,    NULL,
+	NULL,	NULL,	  NULL,		NULL,	     NULL,    NULL,
+	NULL,	NULL,	  NULL,		"devnull",
+};
+
+#define SELINUX_SID_SZ \
+	(sizeof(selinux_sid_to_str) / sizeof(selinux_sid_to_str[0]))
+
+static const char *const xen_sid_to_str[] = {
+	"null",	  "xen",   "dom0", "domio",  "domxen", "unlabeled", "security",
+	"ioport", "iomem", "irq",  "device", "domU",   "domDM",
+};
+
+#define XEN_SID_SZ (sizeof(xen_sid_to_str) / sizeof(xen_sid_to_str[0]))
+
+#endif /* _SEPOL_INITIAL_SIDS_H_ */
libsepol/src/kernel_to_common.h +2 -20
index f319ea50..5adf9d75 100644
@@ -12,30 +12,12 @@
 #include <sepol/policydb/avtab.h>
 #include <sepol/policydb/policydb.h>
 
+#include "initial_sids.h"
+
 #define STACK_SIZE 16
 #define DEFAULT_LEVEL "systemlow"
 #define DEFAULT_OBJECT "object_r"
 
-// initial sid names aren't actually stored in the pp files, need to a have
-// a mapping, taken from the linux kernel
-static const char *const selinux_sid_to_str[] = {
-	NULL,	"kernel", "security",	"unlabeled", NULL,    "file",
-	NULL,	"init",	  "any_socket", "port",	     "netif", "netmsg",
-	"node", NULL,	  NULL,		NULL,	     NULL,    NULL,
-	NULL,	NULL,	  NULL,		NULL,	     NULL,    NULL,
-	NULL,	NULL,	  NULL,		"devnull",
-};
-
-#define SELINUX_SID_SZ \
-	(sizeof(selinux_sid_to_str) / sizeof(selinux_sid_to_str[0]))
-
-static const char *const xen_sid_to_str[] = {
-	"null",	  "xen",   "dom0", "domio",  "domxen", "unlabeled", "security",
-	"ioport", "iomem", "irq",  "device", "domU",   "domDM",
-};
-
-#define XEN_SID_SZ (sizeof(xen_sid_to_str) / sizeof(xen_sid_to_str[0]))
-
 static const uint32_t avtab_flavors[] = {
 	AVTAB_ALLOWED,
 	AVTAB_AUDITALLOW,
libsepol/src/ocontext.c new file +1058 -0

1,058 lines not on this page · raw message

Signature · 1 line
2.55.0
maintainer
#19 lore raw

Re: [RFC PATCH userspace 13/17] libsepol: add ocontext iterators for initial SIDs and named contexts

Quoted 7 lines · jan janasek

Add per-kind ocontext iterators (initial SIDs, fs_use, genfscon, netifcon, node/node6, port, ibpkey, ibendport, pirq/iomem/ioport/ pcidevice/devicetree for Xen) with context and name/address accessors, deduplicating ~100 near-identical accessor bodies into DEFINE_OCON_CONTEXT_GETTERS macro instances.

Quoted 7 lines

Extract the initial-SID name table (indexed by SID value, not stored in the binary policy) into a shared libsepol/src/initial_sids.h, used by both sepol_isid_iter_get_name() here and the existing kernel_to_common.c CIL/conf writers, instead of duplicating the table.

Assisted-by LLM Cursor

As I noted on the cover letter, please document the KERN policy limitation. With this patch applied there is now overlap between the new raw-value sepol_portcon_iter_*() and the pre-existing record-based sepol_port_iterate()/sepol_port_t and likewise for node/iface/ipbkey/ibendport. Worth a sentence somewhere about which is preferred going forward.

#20 lore raw

RFC PATCH 14/17 libsepol: add filename transition, permissive, polcap, range transition, and role rule iterators

In reply to jan janasek

Add sepol_filename_trans_iter for filename_trans rules, sepol_permissive_iter for permissive types, sepol_polcap_iter plus sepol_polcap_getname() for policy capabilities, sepol_range_trans_iter for MLS range transition rules, and sepol_role_rule_iter for role-allow/role-transition rules.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/filename_trans.h |  42 ++++++++
 libsepol/include/sepol/permissive.h     |  33 +++++++
 libsepol/include/sepol/polcap.h         |  41 ++++++++
 libsepol/include/sepol/range_trans.h    |  55 +++++++++++
 libsepol/include/sepol/role_rule.h      |  41 ++++++++
 libsepol/src/filename_trans.c           | 123 ++++++++++++++++++++++++
 libsepol/src/permissive_iter.c          |  65 +++++++++++++
 libsepol/src/polcap_iter.c              |  67 +++++++++++++
 libsepol/src/range_trans.c              | 119 +++++++++++++++++++++++
 libsepol/src/role_rule.c                | 117 ++++++++++++++++++++++
 10 files changed, 703 insertions(+)
 create mode 100644 libsepol/include/sepol/filename_trans.h
 create mode 100644 libsepol/include/sepol/permissive.h
 create mode 100644 libsepol/include/sepol/polcap.h
 create mode 100644 libsepol/include/sepol/range_trans.h
 create mode 100644 libsepol/include/sepol/role_rule.h
 create mode 100644 libsepol/src/filename_trans.c
 create mode 100644 libsepol/src/permissive_iter.c
 create mode 100644 libsepol/src/polcap_iter.c
 create mode 100644 libsepol/src/range_trans.c
 create mode 100644 libsepol/src/role_rule.c
libsepol/include/sepol/filename_trans.h new file +42 -0
new file mode 100644
index 00000000..ca742fb5
@@ -0,0 +1,42 @@
+#ifndef _SEPOL_FILENAME_TRANS_H_
+#define _SEPOL_FILENAME_TRANS_H_
+
+#include <sepol/handle.h>
+#include <sepol/policydb.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sepol_filename_trans_iter;
+typedef struct sepol_filename_trans_iter sepol_filename_trans_iter_t;
+
+/*
+ * Iteration order is unspecified (currently hash-bucket order of the
+ * internal symbol table) and may change between library versions or
+ * policydb loads; callers must not rely on it matching declaration order
+ * or being stable across calls.
+ */
+extern int sepol_filename_trans_iter_create(sepol_handle_t *handle,
+					    const sepol_policydb_t *p,
+					    sepol_filename_trans_iter_t **iter);
+
+extern void
+sepol_filename_trans_iter_destroy(sepol_filename_trans_iter_t *iter);
+
+/*
+ * Advance to the next flattened filename transition rule.
+ * Sets *source_type to 0 when done.
+ * All type/class values are 1-based.
+ */
+extern int sepol_filename_trans_iter_next(
+	sepol_handle_t *handle, sepol_filename_trans_iter_t *iter,
+	uint32_t *source_type, uint32_t *target_type, uint32_t *target_class,
+	const char **filename, uint32_t *default_type);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/permissive.h new file +33 -0
new file mode 100644
index 00000000..0225bb8d
@@ -0,0 +1,33 @@
+#ifndef _SEPOL_PERMISSIVE_H_
+#define _SEPOL_PERMISSIVE_H_
+
+#include <sepol/handle.h>
+#include <sepol/policydb.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct sepol_permissive_iter sepol_permissive_iter_t;
+
+extern int sepol_permissive_iter_create(sepol_handle_t *handle,
+					const sepol_policydb_t *p,
+					sepol_permissive_iter_t **iter);
+
+extern void sepol_permissive_iter_destroy(sepol_permissive_iter_t *iter);
+
+/* On each successful call with iteration remaining, *type_value is set
+ * to the value of a type marked permissive -- i.e. the same 1-based
+ * value returned by sepol_type_query()/used as a type_datum_t's
+ * s.value, not a 0-based bit position. Iteration is exhausted once
+ * *type_value is set to 0 (0 is never a valid type value). */
+extern int sepol_permissive_iter_next(sepol_handle_t *handle,
+				      sepol_permissive_iter_t *iter,
+				      uint32_t *type_value);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
libsepol/include/sepol/polcap.h new file +41 -0

41 lines not on this page · raw message

libsepol/include/sepol/range_trans.h new file +55 -0

55 lines not on this page · raw message

libsepol/include/sepol/role_rule.h new file +41 -0

41 lines not on this page · raw message

libsepol/src/filename_trans.c new file +123 -0

123 lines not on this page · raw message

libsepol/src/permissive_iter.c new file +65 -0

65 lines not on this page · raw message

libsepol/src/polcap_iter.c new file +67 -0

67 lines not on this page · raw message

libsepol/src/range_trans.c new file +119 -0

119 lines not on this page · raw message

libsepol/src/role_rule.c new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
2.55.0
#21 lore raw

RFC PATCH 15/17 libsepol: add record API tests for type, role, class, and user

In reply to jan janasek

Cover the sepol_type_t/sepol_role_t/sepol_class_t/sepol_user_t getter/setter surface, key create/unpack/extract/compare, exists/ query/query_by_value, and value_to_name for all four kinds, including NULL-argument and out-of-range value regression tests.

Assisted-by LLM Cursor
---
 libsepol/tests/libsepol-tests.c |    2 +
 libsepol/tests/test-records.c   | 1630 +++++++++++++++++++++++++++++++
 libsepol/tests/test-records.h   |   10 +
 3 files changed, 1642 insertions(+)
 create mode 100644 libsepol/tests/test-records.c
 create mode 100644 libsepol/tests/test-records.h
libsepol/tests/libsepol-tests.c +2 -0
index 707937cf..d3f33203 100644
@@ -28,6 +28,7 @@
 #include "test-iter.h"
 #include "test-context-api.h"
 #include "test-av-rule.h"
+#include "test-records.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -80,6 +81,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(iter);
 	DECLARE_SUITE(context_api);
 	DECLARE_SUITE(av_rule);
+	DECLARE_SUITE(records);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsepol/tests/test-records.c new file +1630 -0

1,630 lines not on this page · raw message

libsepol/tests/test-records.h new file +10 -0
new file mode 100644
index 00000000..d50b77b7
@@ -0,0 +1,10 @@
+#ifndef TEST_RECORDS_H
+#define TEST_RECORDS_H
+
+#include <CUnit/Basic.h>
+
+int records_test_init(void);
+int records_test_cleanup(void);
+int records_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.55.0
#22 lore raw

RFC PATCH 16/17 libsepol: add tier-2 integration tests

In reply to jan janasek

Add broader integration coverage across ocontext, MLS/cat_set, common_perm, filename_trans, permissive, polcap, range_trans, and role_rule iterators together against the test-iter fixture, exercising combinations not covered by the per-feature unit tests.

Assisted-by LLM Cursor
---
 libsepol/tests/libsepol-tests.c |    2 +
 libsepol/tests/test-tier2.c     | 2100 +++++++++++++++++++++++++++++++
 libsepol/tests/test-tier2.h     |   10 +
 3 files changed, 2112 insertions(+)
 create mode 100644 libsepol/tests/test-tier2.c
 create mode 100644 libsepol/tests/test-tier2.h
libsepol/tests/libsepol-tests.c +2 -0
index d3f33203..cb4af493 100644
@@ -29,6 +29,7 @@
 #include "test-context-api.h"
 #include "test-av-rule.h"
 #include "test-records.h"
+#include "test-tier2.h"
 
 #include <CUnit/Basic.h>
 #include <CUnit/Console.h>
@@ -82,6 +83,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(context_api);
 	DECLARE_SUITE(av_rule);
 	DECLARE_SUITE(records);
+	DECLARE_SUITE(tier2);
 
 	if (verbose)
 		CU_basic_set_mode(CU_BRM_VERBOSE);
libsepol/tests/test-tier2.c new file +2100 -0

2,100 lines not on this page · raw message

libsepol/tests/test-tier2.h new file +10 -0
new file mode 100644
index 00000000..817a200c
@@ -0,0 +1,10 @@
+#ifndef TEST_TIER2_H
+#define TEST_TIER2_H
+
+#include <CUnit/Basic.h>
+
+int tier2_test_init(void);
+int tier2_test_cleanup(void);
+int tier2_add_tests(CU_pSuite suite);
+
+#endif
Signature · 1 line
2.55.0
#23 lore raw

RFC PATCH 17/17 libsepol: add symbol map entries and umbrella header for new public API

In reply to jan janasek

Add all new public symbols to libsepol.map.in under a new version block. Update sepol/sepol.h umbrella header to include all 20 new public headers.

Assisted-by LLM Cursor
---
 libsepol/include/sepol/sepol.h |  59 +++--
 libsepol/src/libsepol.map.in   | 379 +++++++++++++++++++++++++++++++++
 2 files changed, 418 insertions(+), 20 deletions(-)
libsepol/include/sepol/sepol.h +39 -20
index 32b4dc57..7104632b 100644
@@ -4,31 +4,50 @@
 #include <stddef.h>
 #include <stdio.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <sepol/user_record.h>
-#include <sepol/context_record.h>
-#include <sepol/iface_record.h>
-#include <sepol/ibpkey_record.h>
-#include <sepol/ibendport_record.h>
-#include <sepol/port_record.h>
+#include <sepol/av_rule.h>
 #include <sepol/boolean_record.h>
-#include <sepol/node_record.h>
-
 #include <sepol/booleans.h>
-#include <sepol/interfaces.h>
-#include <sepol/ibpkeys.h>
+#include <sepol/cat_set.h>
+#include <sepol/class_record.h>
+#include <sepol/classes.h>
+#include <sepol/common_perm.h>
+#include <sepol/conditional.h>
+#include <sepol/constants.h>
+#include <sepol/context.h>
+#include <sepol/context_record.h>
+#include <sepol/debug.h>
+#include <sepol/filename_trans.h>
+#include <sepol/flask.h>
+#include <sepol/handle.h>
+#include <sepol/ibendport_record.h>
 #include <sepol/ibendports.h>
-#include <sepol/ports.h>
+#include <sepol/ibpkey_record.h>
+#include <sepol/ibpkeys.h>
+#include <sepol/iface_record.h>
+#include <sepol/interfaces.h>
+#include <sepol/mls_iter.h>
+#include <sepol/module.h>
+#include <sepol/node_record.h>
 #include <sepol/nodes.h>
-#include <sepol/users.h>
-#include <sepol/handle.h>
-#include <sepol/debug.h>
+#include <sepol/ocontext.h>
+#include <sepol/permissive.h>
+#include <sepol/polcap.h>
 #include <sepol/policydb.h>
-#include <sepol/module.h>
-#include <sepol/context.h>
+#include <sepol/port_record.h>
+#include <sepol/ports.h>
+#include <sepol/range_trans.h>
+#include <sepol/role_record.h>
+#include <sepol/role_rule.h>
+#include <sepol/roles.h>
+#include <sepol/sid_table.h>
+#include <sepol/type_record.h>
+#include <sepol/types.h>
+#include <sepol/user_record.h>
+#include <sepol/users.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 /* Set internal policydb from a file for subsequent service calls. */
 extern int sepol_set_policydb_from_file(FILE *fp);
libsepol/src/libsepol.map.in +379 -0

382 lines not on this page · raw message

Signature · 1 line
2.55.0
#24 lore raw
In reply to jan janasek
Quoted 31 lines · jan janasek

Hi all,

This series adds a public, stable libsepol API for read-only introspection of a loaded/compiled policydb: iterators and accessors for types, roles, users, classes/permissions, booleans, conditionals, AV rules, MLS/category sets, contexts, ocontexts (initial SIDs, fs/port/node/... contexts), filename transitions, range transitions, role rules, permissive types and policy capabilities, plus a small "record" layer that snapshots the above into owned, independently-freeable structures.

This is the first time this series is posted to the list; it was developed and self-reviewed out-of-tree against an older base and has just been rebased onto current upstream/main before this posting (see "Testing" below for what that rebase involved).

Posting this as RFC because it adds a sizeable new public ABI surface and I would like design-level feedback before going through a full review cycle. Specifically:

- Is libsepol the right place for this, or would a separate library be preferable?
- Does the record/iterator split (snapshot "record" structs vs. zero-copy iterators) make sense as a general shape for this kind of API, and are the naming conventions reasonable?
- Is the libsepol.map.in symbol-versioning approach in patch 17 acceptable for a surface this size?
- Is landing this as one 17-patch series reasonable, or would maintainers prefer it split into smaller, independently mergeable increments (e.g. per policy-object family)?

Quoted 10 lines

Motivation
----------
SETools (https://github.com/SELinuxProject/setools) and other policy-analysis tools currently reach into libsepol's internal, unstable headers (policydb.h, hashtab.h, ebitmap.h, ...) to walk a loaded policydb, because there is no supported public way to do this. That couples those tools tightly to libsepol's internal data structures and breaks whenever those structures change. This series gives such consumers a real, documented, ABI-stable surface instead, without exposing anything from policydb.h directly.

I reviewed this from the user (SETools) perspective, and it looks very promising. In the iterators, I'd request functions for getting the length from the iterators, as I use that many times to get counts of types, roles, etc. A reset function would be nice to have too, but not a big deal to go without.

Signature · 1 line
Chris PeBenito
maintainer
#25 lore raw
In reply to jan janasek
Quoted 17 lines · jan janasek

Hi all,

This series adds a public, stable libsepol API for read-only introspection of a loaded/compiled policydb: iterators and accessors for types, roles, users, classes/permissions, booleans, conditionals, AV rules, MLS/category sets, contexts, ocontexts (initial SIDs, fs/port/node/... contexts), filename transitions, range transitions, role rules, permissive types and policy capabilities, plus a small "record" layer that snapshots the above into owned, independently-freeable structures.

This is the first time this series is posted to the list; it was developed and self-reviewed out-of-tree against an older base and has just been rebased onto current upstream/main before this posting (see "Testing" below for what that rebase involved).

Quoted 6 lines

Posting this as RFC because it adds a sizeable new public ABI surface and I would like design-level feedback before going through a full review cycle. Specifically:

- Is libsepol the right place for this, or would a separate library be preferable?

libsepol is the right place, thanks for doing this!

Quoted 3 lines

- Does the record/iterator split (snapshot "record" structs vs. zero-copy iterators) make sense as a general shape for this kind of API, and are the naming conventions reasonable?

In general, yes.

Quoted 2 lines

- Is the libsepol.map.in symbol-versioning approach in patch 17 acceptable for a surface this size?

IMHO, yes.

Quoted 3 lines

- Is landing this as one 17-patch series reasonable, or would maintainers prefer it split into smaller, independently mergeable increments (e.g. per policy-object family)?

No objection to doing it all in one go.

Some general comments:

1. NULL handle. Essentially every new _create() function rejects a NULL sepol_handle_t, whereas none of the existing public API does that; they all accept handle == NULL and ERR() falls back to sepol_compat_handle. I'd drop the !handle checks across the series.
2. constants.h and the public namespace. As written, sepol/constants.h is included from sepol/sepol.h, so a public API only consumer that includes sepol.h now gets AVTAB_ALLOWED, OCON_FS, ... defined. Move the unprefixed #ifndef blocks back into the sepol/policydb/ headers so sepol/constants.h exports only SEPOL_* and the compat spellings stay confined to code that is already including internal headers anyway.
3. Exhaustion error reporting. There are three conventions in flight:
*name == NULL, *value == 0, int *has_next. For the uint32_t-value iterators, 0 happens to be an impossible value in every current case but I'd rather not bake that into the contract. A has_next out-parameter or returning >0/==0/<0 would be future-proof and uniform. If that's too much churn at least documenting the convention once in each header may suffice - right now cat_set.h says the first _next() "reports no values" without saying how.
4. Record iterators and ownership/cost. sepol_type_iter_next() et al hand back a freshly-allocated, caller-owned sepol_type_t. Two things:
The headers don't say so, and on a real policy, type_datum_to_record() will string_list_add() every member of attr_type_map[v-1] / type_attr_map[v-1], which for refpolicy is ~5k types x tens of attributes each - a lot of strdup churn to walk the type table once. That's consistent with the existing sepol_user_iterate() shape so this may not be a blocker but a setools migration may want a cheap "name + value + flavor" variant that doesn't materialize the subtype list.
5. KERN-only iterators. sepol_avtab_iter, sepol_cond_iter, ... read the kernel-format tables (te_avtab, cond_list, ...). On a POLICY_BASE/MOD policydb those are empty - the data lives on per-decl avrule_block lists instead - so these iterators silently yield nothing. types.h already has a nice KERN-vs-source paragraph; the rule iterators should carry an equivalent note (or the create() could ERR on policy_type != POLICY_KERN the same way the Xen icon iters ERR on target_platform).
6. sepol_policydb_rebuild_attr_map. I can't construct a public-API call sequence that needs this: sepol_type_modify() refuses POLICY_KERN, and rebuild_attr_map() early-returns on !attr_type_map (i.e. anything that isn't KERN). So the only mutator and the only rebuilder are mutually exclusive on policy_type. If there is a future API coming that will use it, that's fine, but then that should go in this series too (or drop this interface until that one is ready).
7. Patch 05 is a real bug fix independent of the API. I'd be happy to see that one split out and sent on its own ahead of this RFC.

More specific notes to come on the individual patches.