checkpolicy: extend bad-data and robustness tests
cover checkpolicy: extend bad-data and robustness tests
This is a follow-up to the checkmodule bad-data harness already merged:
checkpolicy: add bad data protection tests
https://lore.kernel.org/selinux/[email protected]/
Patch 1 adds negative .te fixtures for duplicate type/attribute declarations, type/attribute name conflicts, invalid type names, and malformed role/user stanzas. It also runs the negative suite as non-root in CI so the unreadable .te case is exercised (root can still read mode 000 files).
Patch 2 adds expect_robust coverage for oversized generated .te/.conf inputs and a large attribute membership set, with tunable sizes. It also fixes expect_* helpers to return 0 after counting a failure so set -e does not abort the suite early; the script exit status is still non-zero when FAIL != 0.
Together these bring the harness from 16 to 25 tests (1 positive + 21 negative + 3 robust).
Tested with:
cd checkpolicy
make checkmodule checkpolicy LIBSEPOLA=../libsepol/src/libsepol.a \
CPPFLAGS='-I../libsepol/include'
make test LIBSEPOLA=../libsepol/src/libsepol.a \
CPPFLAGS='-I../libsepol/include'
Result: roundtrip tests pass; checkmodule negative tests:
25 passed, 0 failed.
Akhil Kohli (2):
checkpolicy: extend bad-data tests for type, role, and user errors
checkpolicy: add robustness tests for large policy inputs
.github/workflows/run_tests.yml | 11 ++ checkpolicy/tests/negative/bad_role.te | 12 ++ checkpolicy/tests/negative/bad_user.te | 3 + checkpolicy/tests/negative/dup_attribute.te | 13 ++ checkpolicy/tests/negative/dup_type.te | 11 ++ .../tests/negative/invalid_type_name.te | 9 + .../tests/negative/type_attr_conflict.te | 10 + .../tests/test_checkmodule_negative.sh | 187 +++++++++++++++++- 8 files changed, 248 insertions(+), 8 deletions(-) create mode 100644 checkpolicy/tests/negative/bad_role.te create mode 100644 checkpolicy/tests/negative/bad_user.te create mode 100644 checkpolicy/tests/negative/dup_attribute.te create mode 100644 checkpolicy/tests/negative/dup_type.te create mode 100644 checkpolicy/tests/negative/invalid_type_name.te create mode 100644 checkpolicy/tests/negative/type_attr_conflict.te
Signature · 1 line
2.55.0
PATCH 1/2 checkpolicy: extend bad-data tests for type, role, and user errors
Add negative .te fixtures for duplicate type/attribute declarations, type/attribute name conflicts, invalid type names, and malformed role and user stanzas. Run checkmodule negative tests as non-root in CI so the unreadable .te case is exercised outside root-only skips.
--- .github/workflows/run_tests.yml | 11 +++++++++++ checkpolicy/tests/negative/bad_role.te | 12 ++++++++++++ checkpolicy/tests/negative/bad_user.te | 3 +++ checkpolicy/tests/negative/dup_attribute.te | 13 +++++++++++++ checkpolicy/tests/negative/dup_type.te | 11 +++++++++++ checkpolicy/tests/negative/invalid_type_name.te | 9 +++++++++ checkpolicy/tests/negative/type_attr_conflict.te | 10 ++++++++++ checkpolicy/tests/test_checkmodule_negative.sh | 9 +++++++++ 8 files changed, 78 insertions(+) create mode 100644 checkpolicy/tests/negative/bad_role.te create mode 100644 checkpolicy/tests/negative/bad_user.te create mode 100644 checkpolicy/tests/negative/dup_attribute.te create mode 100644 checkpolicy/tests/negative/dup_type.te create mode 100644 checkpolicy/tests/negative/invalid_type_name.te create mode 100644 checkpolicy/tests/negative/type_attr_conflict.te
.github/workflows/run_tests.yml +11 -0
@@ -95,6 +95,17 @@ jobs: echo "::endgroup::" fi + # checkmodule bad-data: unreadable .te needs non-root (root reads mode 000). + if [ "${{ matrix.python-ruby-version.other }}" != "sanitizers" ] ; then + echo "::group::checkpolicy bad-data (non-root)" + sudo useradd -m -s /usr/sbin/nologin bad-data-test 2>/dev/null || true + chmod -R a+rX . + sudo runuser -u bad-data-test -- env PATH="$PATH" LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \ + ./checkpolicy/tests/test_checkmodule_negative.sh + sudo userdel -r bad-data-test 2>/dev/null || true + echo "::endgroup::" + fi + if [ "${{ matrix.python-ruby-version.other }}" != "sanitizers" ] ; then # Test Python and Ruby wrappers echo "::group::Test Python and Ruby wrappers"
checkpolicy/tests/negative/bad_role.te new file +12 -0
@@ -0,0 +1,12 @@ +module bad_role 1.0; + +require { + type foo_t; + class file { read }; +} + +role bad_role_r types ; + +type foo_t; + +allow foo_t foo_t:file read;
checkpolicy/tests/negative/bad_user.te new file +3 -0
@@ -0,0 +1,3 @@ +module bad_user 1.0; + +user bad_user_u garbage_token;
checkpolicy/tests/negative/dup_attribute.te new file +13 -0
@@ -0,0 +1,13 @@ +module dup_attribute 1.0; + +require { + type foo_t; + class file { read }; +} + +attribute bar_attr; +attribute bar_attr; + +type foo_t; + +allow foo_t foo_t:file read;
checkpolicy/tests/negative/dup_type.te new file +11 -0
@@ -0,0 +1,11 @@ +module dup_type 1.0; + +require { + type foo_t; + class file { read }; +} + +type foo_t; +type foo_t; + +allow foo_t foo_t:file read;
checkpolicy/tests/negative/invalid_type_name.te new file +9 -0
@@ -0,0 +1,9 @@ +module invalid_type_name 1.0; + +require { + class file { read }; +} + +type 1bad; + +allow 1bad 1bad:file read;
checkpolicy/tests/negative/type_attr_conflict.te new file +10 -0
@@ -0,0 +1,10 @@ +module type_attr_conflict 1.0; + +require { + class file { read }; +} + +type shared_id; +attribute shared_id; + +allow shared_id shared_id:file read;
checkpolicy/tests/test_checkmodule_negative.sh +9 -0
@@ -159,6 +159,15 @@ expect_fail "bad_module_line.te" "syntax error" "bad_module_line" "${NEGDIR}/bad expect_fail "bad_require.te" "syntax error" "bad_require" "${NEGDIR}/bad_require.te" expect_fail "invalid_module_version.te" "syntax error" "invalid_module_version" "${NEGDIR}/invalid_module_version.te" expect_fail "dup_module.te" "syntax error" "dup_module" "${NEGDIR}/dup_module.te" +expect_fail "dup_type.te" "Duplicate declaration of type" "dup_type" "${NEGDIR}/dup_type.te" +expect_fail "dup_attribute.te" "Duplicate declaration of type" "dup_attribute" \ + "${NEGDIR}/dup_attribute.te" +expect_fail "type_attr_conflict.te" "Duplicate declaration of type" "type_attr_conflict" \ + "${NEGDIR}/type_attr_conflict.te" +expect_fail "invalid_type_name.te" "syntax error" "invalid_type_name" \ + "${NEGDIR}/invalid_type_name.te" +expect_fail "bad_role.te" "syntax error" "bad_role" "${NEGDIR}/bad_role.te" +expect_fail "bad_user.te" "syntax error" "bad_user" "${NEGDIR}/bad_user.te" # Missing / bad-path .te inputs (PDF #1). expect_fail "missing .te path" "unable to open" "missing_path" "${OUTDIR}/does_not_exist.te"
Signature · 1 line
2.55.0
Stephen Smalley
maintainerRe: [PATCH 1/2] checkpolicy: extend bad-data tests for type, role, and user errors
Quoted 14 lines · Akhil Kohli
Add negative .te fixtures for duplicate type/attribute declarations, type/attribute name conflicts, invalid type names, and malformed role and user stanzas. Run checkmodule negative tests as non-root in CI so the unreadable .te case is exercised outside root-only skips.
Signed-off-by Akhil Kohli <[email protected]>---
checkpolicy/tests/negative/bad_user.te new file +0
-0
checkpolicy/tests/negative/bad_user.te line 3
Quoted 3 lines
checkpolicy/tests/negative/bad_user.te new file +3
-0@@ -0,0 +1,3 @@ +module bad_user 1.0; + +user bad_user_u garbage_token;
This won't reach the garbage_token because it fails on token "user" due to lacking any avrule_decls, e.g. require { }.
Not sure if that was your intent.
PATCH 2/2 checkpolicy: add robustness tests for large policy inputs
In reply to Akhil KohliAdd expect_robust to the checkmodule negative harness so oversized generated .te and .conf sources are exercised without crashing or hanging. Also cover a module with a large attribute membership set.
Fixtures are generated at runtime to keep the repository small. Test size is tunable via LARGE_TE_ALLOWS, LARGE_CONF_ALLOWS, HUGE_ATTR_TYPES, and BAD_DATA_STRESS.
Also fix expect_* to return 0 after counting a failure so set -e does not stop the suite early. Same bug affected the older cases; exit status is still non-zero when FAIL != 0.
--- .../tests/test_checkmodule_negative.sh | 178 +++++++++++++++++- 1 file changed, 170 insertions(+), 8 deletions(-)
checkpolicy/tests/test_checkmodule_negative.sh +170 -8
@@ -3,16 +3,30 @@ # Negative / bad-data tests for checkmodule on module (.te) inputs. # Matches the modular policy compile path: checkmodule -M -m <input> -o <module>.mod # +# §11 size / resource limits: generated fixtures; scale via LARGE_TE_ALLOWS, +# LARGE_CONF_ALLOWS, HUGE_ATTR_TYPES, or BAD_DATA_STRESS=1 for heavier runs. +# set -eu BASEDIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd) NEGDIR="${BASEDIR}/negative" CHECKMODULE="${BASEDIR}/../checkmodule" +CHECKPOLICY="${BASEDIR}/../checkpolicy" OUTDIR=$(mktemp -d "${TMPDIR:-/tmp}/checkmodule-negative.XXXXXX") PASS=0 FAIL=0 +LARGE_TE_ALLOWS="${LARGE_TE_ALLOWS:-5000}" +LARGE_CONF_ALLOWS="${LARGE_CONF_ALLOWS:-5000}" +HUGE_ATTR_TYPES="${HUGE_ATTR_TYPES:-2000}" + +if [ "${BAD_DATA_STRESS:-0}" = "1" ]; then + LARGE_TE_ALLOWS=10000 + LARGE_CONF_ALLOWS=10000 + HUGE_ATTR_TYPES=5000 +fi + cleanup() { rm -rf "${OUTDIR}" }
@@ -41,12 +55,12 @@ expect_pass() { echo "FAIL: expected success (rc=0), got rc=${rc}" >&2 cat "${stderr}" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi if [ ! -s "${outmod}" ]; then echo "FAIL: expected non-empty ${outmod}" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi echo "==== ${desc} success"
@@ -74,18 +88,18 @@ expect_fail() { if [ "${rc}" -eq 0 ]; then echo "FAIL: expected non-zero exit, got rc=0" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi if [ -f "${outmod}" ]; then echo "FAIL: did not expect output module ${outmod}" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi if ! grep -Eq "${pattern}" "${stderr}"; then echo "FAIL: stderr did not match /${pattern}/" >&2 cat "${stderr}" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi echo "==== ${desc}: rejected as expected"
@@ -117,18 +131,18 @@ expect_fail_unreadable() { if [ "${rc}" -eq 0 ]; then echo "FAIL: expected non-zero exit, got rc=0" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi if [ -f "${outmod}" ]; then echo "FAIL: did not expect output module ${outmod}" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi if ! grep -Eq 'unable to open|Permission denied' "${stderr}"; then echo "FAIL: stderr did not mention unable to open or Permission denied" >&2 cat "${stderr}" >&2 FAIL=$((FAIL + 1)) - return 1 + return 0 fi echo "==== ${desc}: rejected as expected"
@@ -136,6 +150,138 @@ expect_fail_unreadable() { echo "" } +robust_exit_is_crash() { + rc="$1" + + # Fatal signals: SEGV (139), ABRT (134), ILL (132), BUS (135), FPE (136). + case "${rc}" in + 132|134|135|136|139) + return 0 + ;; + esac + return 1 +} + +expect_robust() { + desc="$1" + timeout_sec="$2" + outartifact="$3" + shift 3 + + tag=$(echo "${desc}" | tr ' /.' '___') + stderr="${OUTDIR}/${tag}.err" + + echo "==== ROBUST (expect no crash/hang): ${desc}" + rm -f "${outartifact}" + + if ! command -v timeout >/dev/null 2>&1; then + echo "FAIL: timeout(1) required for robustness tests" >&2 + FAIL=$((FAIL + 1)) + return 0 + fi + + set +e + timeout "${timeout_sec}" "$@" 2>"${stderr}" + rc=$? + set -e + + if [ "${rc}" -eq 124 ]; then + echo "FAIL: timed out after ${timeout_sec}s" >&2 + FAIL=$((FAIL + 1)) + return 0 + fi + if robust_exit_is_crash "${rc}"; then + echo "FAIL: crashed with rc=${rc}" >&2 + cat "${stderr}" >&2 + FAIL=$((FAIL + 1)) + return 0 + fi + if [ "${rc}" -eq 0 ]; then + if [ ! -s "${outartifact}" ]; then + echo "FAIL: expected non-empty ${outartifact} on success" >&2 + cat "${stderr}" >&2 + FAIL=$((FAIL + 1)) + return 0 + fi + echo "==== ${desc}: finished (rc=0, output produced)" + else + if [ -f "${outartifact}" ]; then + echo "FAIL: did not expect output ${outartifact} on rc=${rc}" >&2 + cat "${stderr}" >&2 + FAIL=$((FAIL + 1)) + return 0 + fi + echo "==== ${desc}: finished safely (rc=${rc}, no output)" + fi + PASS=$((PASS + 1)) + echo "" +} + +generate_large_module_te() { + outte="$1" + count="$2" + + { + echo 'module large_module 1.0;' + echo 'require {' + echo ' type a_t, b_t;' + echo ' class file { read };' + echo '}' + i=0 + while [ "${i}" -lt "${count}" ]; do + echo 'allow a_t b_t:file read;' + i=$((i + 1)) + done + } > "${outte}" +} + +generate_large_conf() { + outconf="$1" + count="$2" + + { + echo '# handle_unknown deny' + echo 'class CLASS1' + echo 'sid kernel' + echo 'class CLASS1 { PERM1 }' + echo 'type TYPE1;' + echo 'type TYPE2;' + i=0 + while [ "${i}" -lt "${count}" ]; do + echo 'allow TYPE1 TYPE2:CLASS1 { PERM1 };' + i=$((i + 1)) + done + echo 'role ROLE1;' + echo 'role ROLE1 types { TYPE1 };' + echo 'user USER1 roles { ROLE1 };' + echo 'sid kernel USER1:ROLE1:TYPE1' + } > "${outconf}" +} + +generate_huge_attr_te() { + outte="$1" + count="$2" + + { + echo 'module huge_attr 1.0;' + echo 'require {' + echo ' class file { read };' + i=0 + while [ "${i}" -lt "${count}" ]; do + echo " type a${i}_t;" + i=$((i + 1)) + done + echo '}' + echo 'attribute big_attr;' + i=0 + while [ "${i}" -lt "${count}" ]; do + echo "typeattribute a${i}_t big_attr;" + i=$((i + 1)) + done + echo 'allow a0_t a1_t:file read;' + } > "${outte}" +} + # Ephemeral fixtures for path-based cases. ln -sf /nonexistent/path "${OUTDIR}/broken_symlink.te" cat > "${OUTDIR}/unreadable.te" <<'EOF'
@@ -181,6 +327,22 @@ expect_fail "checkmodule with no input file" "unable to open policy.conf" "no_in expect_fail "checkmodule -o name mismatch" "Module name good_module is different" "name_mismatch" \ -o "${OUTDIR}/wrong_name.mod" "${NEGDIR}/good_module.te" +# §11 size / resource limits (hypothesis §6 item 11). +generate_large_module_te "${OUTDIR}/large_module.te" "${LARGE_TE_ALLOWS}" +expect_robust "11.1 very large .te module (${LARGE_TE_ALLOWS} allow rules)" 120 \ + "${OUTDIR}/large_module.mod" \ + "${CHECKMODULE}" -M -m -o "${OUTDIR}/large_module.mod" "${OUTDIR}/large_module.te" + +generate_large_conf "${OUTDIR}/large.conf" "${LARGE_CONF_ALLOWS}" +expect_robust "11.1 very large .conf policy (${LARGE_CONF_ALLOWS} allow rules)" 120 \ + "${OUTDIR}/large.bin" \ + "${CHECKPOLICY}" -E -o "${OUTDIR}/large.bin" "${OUTDIR}/large.conf" + +generate_huge_attr_te "${OUTDIR}/huge_attr.te" "${HUGE_ATTR_TYPES}" +expect_robust "11.2 huge attribute set (${HUGE_ATTR_TYPES} types)" 120 \ + "${OUTDIR}/huge_attr.mod" \ + "${CHECKMODULE}" -M -m -o "${OUTDIR}/huge_attr.mod" "${OUTDIR}/huge_attr.te" + echo "checkmodule negative tests: ${PASS} passed, ${FAIL} failed" if [ "${FAIL}" -ne 0 ]; then exit 1
Signature · 1 line
2.55.0
Stephen Smalley
maintainerRe: [PATCH 2/2] checkpolicy: add robustness tests for large policy inputs
Quoted 23 lines · Akhil Kohli
Add
expect_robustto the checkmodule negative harness so oversized generated .te and .conf sources are exercised without crashing or hanging. Also cover a module with a large attribute membership set.Fixtures are generated at runtime to keep the repository small. Test size is tunable via
LARGE_TE_ALLOWS,LARGE_CONF_ALLOWS,HUGE_ATTR_TYPES, andBAD_DATA_STRESS.Also fix expect_* to return 0 after counting a failure so set -e does not stop the suite early. Same bug affected the older cases; exit status is still non-zero when FAIL != 0.
Signed-off-by Akhil Kohli <[email protected]>--- .../tests/test_checkmodule_negative.sh | 178 +++++++++++++++++- 1 file changed, 170 insertions(+), 8 deletions(-)checkpolicy/tests/test_checkmodule_negative.sh +0
-0
checkpolicy/tests/test_checkmodule_negative.sh expect_fail_unreadable() { · line 159
Quoted 10 lines
checkpolicy/tests/test_checkmodule_negative.sh +7
-0@@ -136,6 +150,138 @@ expect_fail_unreadable() { echo "" } +robust_exit_is_crash() { + rc="$1" + + # Fatal signals: SEGV (139), ABRT (134), ILL (132), BUS (135), FPE (136). + case "${rc}" in + 132|134|135|136|139) + return 0
You omitted KILL (137), so OOM-kill would pass as finished safely. Not sure that's your intent.
Stephen Smalley
maintainerRe: [PATCH 2/2] checkpolicy: add robustness tests for large policy inputs
Quoted 28 lines · Stephen Smalley
Quoted 26 lines · Akhil Kohli
Add
expect_robustto the checkmodule negative harness so oversized generated .te and .conf sources are exercised without crashing or hanging. Also cover a module with a large attribute membership set.Fixtures are generated at runtime to keep the repository small. Test size is tunable via
LARGE_TE_ALLOWS,LARGE_CONF_ALLOWS,HUGE_ATTR_TYPES, andBAD_DATA_STRESS.Also fix expect_* to return 0 after counting a failure so set -e does not stop the suite early. Same bug affected the older cases; exit status is still non-zero when FAIL != 0.
Signed-off-by Akhil Kohli <[email protected]>--- .../tests/test_checkmodule_negative.sh | 178 +++++++++++++++++- 1 file changed, 170 insertions(+), 8 deletions(-)checkpolicy/tests/test_checkmodule_negative.sh +0
-0@@ -136,6 +150,138 @@ expect_fail_unreadable() { echo "" }
Quoted 10 lines
checkpolicy/tests/test_checkmodule_negative.sh expect_fail_unreadable() { · line 159
Quoted 7 lines
checkpolicy/tests/test_checkmodule_negative.sh +7
-0+robust_exit_is_crash() { + rc="$1" + + # Fatal signals: SEGV (139), ABRT (134), ILL (132), BUS (135), FPE (136). + case "${rc}" in + 132|134|135|136|139) + return 0You omitted KILL (137), so OOM-kill would pass as finished safely. Not sure that's your intent.
Also these patches break CI,
https://github.com/stephensmalley/selinux/actions/runs/30291360086/job/90061649922
Re: [PATCH 2/2] checkpolicy: add robustness tests for large policy inputs
Thanks for the note on exit 137 (OOM/KILL). Addressed in v2 along with the non-root CI BASEDIR fix:
https://lore.kernel.org/selinux/[email protected]/T/#u
Quoted 45 lines · Stephen Smalley
Quoted 28 lines · Stephen Smalley
Quoted 26 lines · Akhil Kohli
Add
expect_robustto the checkmodule negative harness so oversized generated .te and .conf sources are exercised without crashing or hanging. Also cover a module with a large attribute membership set.Fixtures are generated at runtime to keep the repository small. Test size is tunable via
LARGE_TE_ALLOWS,LARGE_CONF_ALLOWS,HUGE_ATTR_TYPES, andBAD_DATA_STRESS.Also fix expect_* to return 0 after counting a failure so set -e does not stop the suite early. Same bug affected the older cases; exit status is still non-zero when FAIL != 0.
Signed-off-by Akhil Kohli <[email protected]>--- .../tests/test_checkmodule_negative.sh | 178 +++++++++++++++++- 1 file changed, 170 insertions(+), 8 deletions(-)checkpolicy/tests/test_checkmodule_negative.sh +0
-0@@ -136,6 +150,138 @@ expect_fail_unreadable() { echo "" }Quoted 10 lines
checkpolicy/tests/test_checkmodule_negative.sh expect_fail_unreadable() { · line 159
Quoted 7 lines
checkpolicy/tests/test_checkmodule_negative.sh +7
-0+robust_exit_is_crash() { + rc="$1" + + # Fatal signals: SEGV (139), ABRT (134), ILL (132), BUS (135), FPE (136). + case "${rc}" in + 132|134|135|136|139) + return 0You omitted KILL (137), so OOM-kill would pass as finished safely. Not sure that's your intent.
Also these patches break CI,
https://github.com/stephensmalley/selinux/actions/runs/30291360086/job/90061649922