Skip to content

RFC libsepol: add public API for policy introspection

jan janasek selinux 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest17 patches 8 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/17libsepol: add hashtab iterator and string list utilitieslibsepol/include/sepol/policydb/ebitmap.h libsepol/include/sepol/policydb/hashtab.h libsepol/include/sepol/policydb/util.h +3 more+236 -30 replies
2/17libsepol: add MLS, category set, and common permission iteratorslibsepol/include/sepol/cat_set.h libsepol/include/sepol/common_perm.h libsepol/include/sepol/mls_iter.h +4 more+541 -01 reply
3/17libsepol: add public type record structure and iteratorlibsepol/include/sepol/type_record.h libsepol/include/sepol/types.h libsepol/src/type_record.c +1 more+1082 -00 replies
4/17libsepol: add test infrastructure and type iterator testslibsepol/tests/helpers.c libsepol/tests/helpers.h libsepol/tests/libsepol-tests.c +5 more+386 -00 replies
5/17libsepol: add public user iterator and accessorslibsepol/include/sepol/users.h libsepol/src/user_record.c libsepol/src/users.c +3 more+659 -510 replies
6/17libsepol: add public role record structure and iteratorlibsepol/include/sepol/role_record.h libsepol/include/sepol/roles.h libsepol/src/role_record.c +5 more+886 -00 replies
7/17libsepol: add public class record structurelibsepol/include/sepol/class_record.h libsepol/src/class_record.c+1203 -01 reply
8/17libsepol: add public class iterator and testslibsepol/include/sepol/classes.h libsepol/src/classes.c libsepol/tests/test-iter-classes.c +2 more+884 -00 replies
9/17libsepol: add policy properties API and public constantslibsepol/include/sepol/constants.h libsepol/include/sepol/policydb.h libsepol/include/sepol/policydb/avtab.h +3 more+486 -972 replies
10/17libsepol: add context resolution from policy values and SIDslibsepol/include/sepol/context.h libsepol/include/sepol/flask.h libsepol/include/sepol/policydb/flask_types.h +9 more+865 -721 reply
11/17libsepol: add AV rule, conditional, and boolean iteratorslibsepol/include/sepol/av_rule.h libsepol/include/sepol/booleans.h libsepol/include/sepol/conditional.h +5 more+1085 -360 replies
12/17libsepol: add AV rule and conditional iterator testslibsepol/tests/libsepol-tests.c libsepol/tests/test-av-rule.c libsepol/tests/test-av-rule.h+745 -00 replies
13/17libsepol: add ocontext iterators for initial SIDs and named contextslibsepol/include/sepol/ocontext.h libsepol/src/initial_sids.h libsepol/src/kernel_to_common.h +1 more+1602 -201 reply
14/17libsepol: add filename transition, permissive, polcap, range transition, and role rule iteratorslibsepol/include/sepol/filename_trans.h libsepol/include/sepol/permissive.h libsepol/include/sepol/polcap.h +7 more+703 -00 replies
15/17libsepol: add record API tests for type, role, class, and userlibsepol/tests/libsepol-tests.c libsepol/tests/test-records.c libsepol/tests/test-records.h+1642 -00 replies
16/17libsepol: add tier-2 integration testslibsepol/tests/libsepol-tests.c libsepol/tests/test-tier2.c libsepol/tests/test-tier2.h+2112 -00 replies
17/17libsepol: add symbol map entries and umbrella header for new public APIlibsepol/include/sepol/sepol.h libsepol/src/libsepol.map.in+418 -200 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH userspace 00/17] 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