libsepol/cil: Add function to check CIL neverallows against binary policy
RFC PATCH 1/3 libsepol/cil: Add function to check CIL neverallows against binary policy
Add the function cil_check_neverallows_against_pdb() that will extract the neverallows from a CIL db and check them against a kernel policydb.
--- libsepol/cil/include/cil/cil.h | 1 + libsepol/cil/src/cil_binary.c | 46 ++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_binary.h | 12 +++++++++ libsepol/src/libsepol.map.in | 5 ++++ 4 files changed, 64 insertions(+)
libsepol/cil/include/cil/cil.h +1 -0
@@ -65,6 +65,7 @@ extern int cil_write_parse_ast(FILE *out, cil_db_t *db);extern int cil_write_build_ast (FILE *out,cil_db_t *db);extern int cil_write_resolve_ast (FILE *out,cil_db_t *db);extern int cil_write_post_ast (FILE *out,cil_db_t *db); +extern int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation);enum cil_log_level {CIL_ERR =1 ,
libsepol/cil/src/cil_binary.c +46 -0
52 lines not on this page · raw message
libsepol/cil/src/cil_binary.h +12 -0
@@ -36,6 +36,18 @@#include "cil_tree.h" #include "cil_list.h" + +/** +* Check neverallows from a cil database against a policy database +* +* @param[in] db The cil database. +* @param[in] pdb The policy database. +* @param[out] violation 1 if a neverallow violation was found, 0 otherwise +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation); +/** * Create a binary policydb from the cil db. *
libsepol/src/libsepol.map.in +5 -0
@@ -294,3 +294,8 @@ LIBSEPOL_3.6 { global: cil_write_post_ast; } LIBSEPOL_3.4; + +LIBSEPOL_3.11 { + global: + cil_check_neverallows_against_pdb; +} LIBSEPOL_3.6;
Signature · 1 line
2.53.0
RFC PATCH 2/3 libsepol: Add a function to output CIL declarations from a binary policy
Add the function sepol_kernel_policydb_decls_to_cil() that takes a binary policy as input and outputs the declarations in CIL format.
--- libsepol/include/sepol/kernel_to_cil.h | 3 +- libsepol/src/kernel_to_cil.c | 92 ++++++++++++++++++++++++++ libsepol/src/libsepol.map.in | 1 + 3 files changed, 95 insertions(+), 1 deletion(-)
libsepol/include/sepol/kernel_to_cil.h +2 -1
@@ -2,4 +2,5 @@#include <sepol/policydb/policydb.h> -int sepol_kernel_policydb_to_cil (FILE *fp,struct policydb *pdb); +int sepol_kernel_policydb_to_cil (FILE *out,struct policydb *pdb); +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb);
libsepol/src/kernel_to_cil.c +92 -0
95 lines not on this page · raw message
libsepol/src/libsepol.map.in +1 -0
@@ -297,5 +297,6 @@ LIBSEPOL_3.6 { LIBSEPOL_3.11 { global: + sepol_kernel_policydb_decls_to_cil; cil_check_neverallows_against_pdb; } LIBSEPOL_3.6;
Signature · 1 line
2.53.0
Re: [RFC PATCH 2/3] libsepol: Add a function to output CIL declarations from a binary policy
Quoted 27 lines · James Carter
Add the function
sepol_kernel_policydb_decls_to_cil()that takes a binary policy as input and outputs the declarations in CIL format.Signed-off-by James Carter <[email protected]>--- libsepol/include/sepol/kernel_to_cil.h | 3 +- libsepol/src/kernel_to_cil.c | 92 ++++++++++++++++++++++++++ libsepol/src/libsepol.map.in | 1 + 3 files changed, 95 insertions(+), 1 deletion(-)libsepol/include/sepol/kernel_to_cil.h +2
-1@@ -2,4 +2,5 @@#include <sepol/policydb/policydb.h> -int sepol_kernel_policydb_to_cil (FILE *fp,struct policydb *pdb); +int sepol_kernel_policydb_to_cil (FILE *out,struct policydb *pdb); +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb);libsepol/src/kernel_to_cil.c +0
-0
libsepol/src/kernel_to_cil.c exit: · line 3537
Quoted 5 lines
libsepol/src/kernel_to_cil.c +2
-0@@ -3533,3 +3533,95 @@ exit:return rc; } + +int sepol_kernel_policydb_decls_to_cil (FILE *out,struct policydb *pdb)
It looks like this duplicates a bunch of code from the existing sepol_kernel_policydb_to_cil. Could both functions be refactored to share the same logic? Thanks.
Quoted 104 lines
libsepol/src/kernel_to_cil.c +90
-0+{ +int rc =0 ; + +if (pdb ==NULL ) { +ERR (NULL ,"No policy" ); + rc =-1 ; + goto exit; + } + +if (pdb->policy_type !=SEPOL_POLICY_KERN ) { +ERR (NULL ,"Policy is not a kernel policy" ); + rc =-1 ; + goto exit; + } + +if (pdb->policyvers >=POLICYDB_VERSION_AVTAB && pdb->policyvers <=POLICYDB_VERSION_PERMISSIVE ) { +/* +* For policy versions between 20 and 23, attributes exist in the policy, +* but only in the type_attr_map. This means that there are gaps in both +* the type_val_to_struct and p_type_val_to_name arrays and policy rules +* can refer to those gaps. +*/ +ERR (NULL ,"Writing policy versions between 20 and 23 as CIL is not supported" ); + rc =-1 ; + goto exit; + } + + rc =write_class_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_sid_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_mls_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_attributes_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_role_attributes_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_boolean_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_alias_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_bounds_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_type_attribute_sets_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_role_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + + rc =write_user_decl_rules_to_cil (out, pdb); +if (rc !=0 ) { + goto exit; + } + +exit: +return rc; +}libsepol/src/libsepol.map.in +1
-1@@ -297,5 +297,6 @@ LIBSEPOL_3.6 { LIBSEPOL_3.11 { global: + sepol_kernel_policydb_decls_to_cil; cil_check_neverallows_against_pdb; } LIBSEPOL_3.6; --2.53.0
RFC PATCH 3/3 secilc: Add program that checks CIL neverallow rules against a binary policy
In reply to James CarterAdd the program secilcheck that takes a binary policy and one or more CIL files and checks the binary policy against the neverallow rules in the CIL policy.
ex/
secilcheck policy.bin neverallows.cil
--- secilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 301 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.c
secilc/.gitignore +2 -0
@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/html
secilc/Makefile +18 -2
@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff
@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml
@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs
@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs clean
secilc/secilcheck.8.xml new file +81 -0
81 lines not on this page · raw message
secilc/secilcheck.c new file +301 -0
301 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [RFC PATCH 3/3] secilc: Add program that checks CIL neverallow rules against a binary policy
Quoted 200 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks the binary policy against the neverallow rules in the CIL policy.
ex/
secilcheck policy.bin neverallows.cilSigned-off-by James Carter <[email protected]>--- secilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 301 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.csecilc/.gitignore +2
-0@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/htmlsecilc/Makefile +18
-2@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs cleansecilc/secilcheck.8.xml new file +81
-0@@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<refentry> + <refentryinfo> + <author> + <firstname>James</firstname><surname>Carter</surname><contrib></contrib> + </author> + </refentryinfo> + + <refmeta> + <refentrytitle>SECILCHECK</refentrytitle> + <manvolnum>8</manvolnum> + <refmiscinfo class="date">27 April 2026</refmiscinfo> + <refmiscinfo class="source">secilcheck</refmiscinfo> + <refmiscinfo class="manual">Check binary policy file against CIL neverallow files</refmiscinfo> + </refmeta> + <refnamediv id="name"> + <refname>secilcheck</refname> + <refpurpose>invoke the CIL neverallow checker</refpurpose> + </refnamediv> + + <refsynopsisdiv id="synopsis"> + <cmdsynopsis> + <command>secilcheck</command> + <arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg> + <arg choice="plain"><replaceable>binary</replaceable></arg> + <arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg> + </cmdsynopsis> + </refsynopsisdiv> + + <refsect1 id="description"><title>DESCRIPTION</title> + <para><emphasis role="italic">secilcheck</emphasis> invokes the CIL neverallow checker with specified <emphasis role="italic">argument</emphasis>s.</para> + </refsect1> + + <refsect1 id="options"><title>OPTIONS</title> + <variablelist> + <varlistentry> + <term><option>-Q, --qualified-names</option></term> + <listitem><para>Allow names containing dots (qualified names). Blocks, blockinherits, blockabstracts, and in-statements will not be allowed.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-m, --multiple-decls</option></term> + <listitem><para>Allow some statements to be re-declared.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-v, --verbose</option></term> + <listitem><para>Increment verbosity level.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-h, --help</option></term> + <listitem><para>Display usage information.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 id="see_also"><title>SEE ALSO</title> + <para> + <simplelist type="inline"> + <member><citerefentry> + <refentrytitle>secilc</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2tree</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2conf</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + </simplelist> + </para> + <para>HTML documentation describing the CIL language statements is available starting with <emphasis role="italic">docs/html/index.html</emphasis>.</para> + <para>PDF documentation describing the CIL language statements is available at: <emphasis role="italic">docs/pdf/CIL_Reference_Guide.pdf</emphasis>.</para> + </refsect1> +</refentry>secilc/secilcheck.c new file +0
-0
secilc/secilcheck.c line 2
Quoted 2 lines
secilc/secilcheck.c new file +2
-0@@ -0,0 +1,301 @@ +/* +* Copyright 2011 Tresys Technology, LLC. All rights reserved.
Update the date? Is that the correct header here?
Quoted 25 lines
secilc/secilcheck.c new file +25
-0+* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS +* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +* EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those +* of the authors and should not be interpreted as representing official policies, +* either expressed or implied, of Tresys Technology, LLC.
secilc/secilcheck.c line 37
Quoted 10 lines
secilc/secilcheck.c new file +10
-0+*/ + +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <getopt.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h>
I had to add unistd.h for close(2) below.
Quoted 267 lines
secilc/secilcheck.c new file +264
-1+ +#ifdef ANDROID +#include <cil/cil.h> +#else +#include <sepol/cil/cil.h> +#endif +#include <sepol/policydb.h> +#include <sepol/kernel_to_cil.h> + +static int get_binary_policy_db (const char *filename,sepol_policydb_t *pdb) +{ +int fd; +struct stat sb; +void *map =NULL ; +sepol_policy_file_t *pf =NULL ; +int rc =SEPOL_ERR ; + +/* Read binary policy */ + fd =open (filename,O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (fstat (fd, &sb) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + map =mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +if (map ==MAP_FAILED ) { +fprintf (stderr,"Could not map file: %s\n" , filename); + goto exit; + } + +if (sepol_policy_file_create (&pf) <0 ) { +fprintf (stderr,"Out of memory" ); + goto exit; + } +sepol_policy_file_set_mem (pf, map, sb.st_size); + +if (sepol_policydb_read (pdb, pf) <0 ) { +fprintf (stderr,"Error reading binary policy: %s\n" , filename); +sepol_policydb_free (pdb); + rc =SEPOL_ERR ; + goto exit; + } + + rc =SEPOL_OK ; + +exit: +if (fd >=0 ) +close (fd); +if (map) +munmap (map, sb.st_size); +sepol_policy_file_free (pf); +return rc; +} + +static int add_decls_to_cil (const char *filename,sepol_policydb_t *pdb,struct cil_db *db) +{ +FILE *cfp =NULL ; +char *cptr =NULL ; +size_t csize; +int rc =SEPOL_ERR ; + + cfp =open_memstream (&cptr, &csize); +if (!cfp) { +fprintf (stderr,"Failed to open dynamic memory buffer stream\n" ); + goto exit; + } + +if (sepol_kernel_policydb_decls_to_cil (cfp, &pdb->p) <0 ) { +fprintf (stderr,"Failed to convert binary policy to CIL\n" ); + goto exit; + } + +if (fflush (cfp) <0 ) { +fprintf (stderr,"Failed to flush CIL memory buffer stream\n" ); + goto exit; + } + +/* Add to CIL db */ +if (cil_add_file (db, filename, cptr, csize) <0 ) { +fprintf (stderr,"Failed to add binary policy file: %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (cfp) +fclose (cfp); +if (cptr) +free (cptr); +return rc; +} + +static int add_cil_file (const char *filename,struct cil_db *cdb) +{ +FILE *file; +struct stat filedata; +char *buffer =NULL ; +size_t file_size; +int rc =-1 ; + + file =fopen (filename,"r" ); +if (!file) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (stat (filename, &filedata) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + file_size = filedata.st_size; + +if (!file_size) { +fclose (file); + rc =0 ; + goto exit; + } + + buffer =malloc (file_size); +if (!buffer) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } + +if (fread (buffer, file_size,1 , file) !=1 ) { +fprintf (stderr,"Failure reading file: %s\n" , filename); + goto exit; + } + +if (cil_add_file (cdb, filename, buffer, file_size) <0 ) { +fprintf (stderr,"Failure adding %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (file) +fclose (file); +if (buffer) +free (buffer); +return rc; +} + +static __attribute__((__noreturn__))void usage (const char *prog) +{ +printf ("Usage: %s [OPTION]... BIN_POLICY CIL_NEVERALLOW_FILE1...\n" , prog); +printf ("\n" ); +printf ("Options:\n" ); +printf (" -Q, --qualified-names Allow names containing dots (qualified names).\n" ); +printf (" Blocks, blockinherits, blockabstracts, and\n" ); +printf (" in-statements will not be allowed.\n" ); +printf (" -m, --multiple-decls allow some statements to be re-declared\n" ); +printf (" -v, --verbose increment verbosity level\n" ); +printf (" -h, --help display usage information\n" ); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ +int rc =SEPOL_ERR ; +sepol_policydb_t *pdb =NULL ; +int violation =0 ; +struct cil_db *db =NULL ; +int multiple_decls =0 ; +int qualified_names =0 ; +int opt_char; +int opt_index =0 ; +enum cil_log_level log_level =CIL_ERR ; +static struct option long_opts[] = { + {"help" , no_argument,0 ,'h' }, + {"verbose" , no_argument,0 ,'v' }, + {"multiple-decls" , no_argument,0 ,'m' }, + {"qualified-names" , no_argument,0 ,'Q' }, + {0 ,0 ,0 ,0 } + }; + +while (1 ) { + opt_char =getopt_long (argc, argv,"hvQm" , long_opts, &opt_index); +if (opt_char ==-1 ) { +break ; + } +switch (opt_char) { +case 'v' : + log_level++; +break ; +case 'm' : + multiple_decls =1 ; +break ; +case 'Q' : + qualified_names =1 ; +break ; +case 'h' : +usage (argv[0 ]); +case '?' : +break ; +default : +fprintf (stderr,"Unsupported option: %s\n" , optarg); +usage (argv[0 ]); + } + } + +if (argc < optind +2 ) { +fprintf (stderr,"Not enough files specified\n" ); +usage (argv[0 ]); + } + +cil_set_log_level (log_level); + +cil_db_init (&db); +cil_set_multiple_decls (db, multiple_decls); +cil_set_qualified_names (db, qualified_names); + +if (sepol_policydb_create (&pdb) <0 ) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } +if (get_binary_policy_db (argv[optind], pdb) <0 ) { +fprintf (stderr,"Failed to get binary policy db\n" ); + rc =SEPOL_ERR ; + goto exit; + } +if (add_decls_to_cil (argv[optind], pdb, db) <0 ) { +fprintf (stderr,"Failed to convert decls to CIL\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + +do { +if (add_cil_file (argv[optind], db) <0 ) { +fprintf (stderr,"Failed to add cil file to CIL files\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + }while (optind < argc); + + rc =cil_compile (db); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to compile cildb: %d\n" , rc); + goto exit; + } + + rc =cil_check_neverallows_against_pdb (db, &pdb->p, &violation); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to complete neverallow checking\n" ); + goto exit; + } +if (violation ==1 ) { +fprintf (stderr,"There was a neverallow violation\n" ); + rc =SEPOL_ERR ; + } + + rc =SEPOL_OK ; + +exit: +cil_db_destroy (&db); +sepol_policydb_free (pdb); +return rc; +} --2.53.0
Re: [RFC PATCH 3/3] secilc: Add program that checks CIL neverallow rules against a binary policy
Quoted 198 lines · Thiébaud Weksteen
Quoted 196 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks the binary policy against the neverallow rules in the CIL policy.
ex/
secilcheck policy.bin neverallows.cilSigned-off-by James Carter <[email protected]>--- secilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 301 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.csecilc/.gitignore +2
-0@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/htmlsecilc/Makefile +18
-2@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs cleansecilc/secilcheck.8.xml new file +81
-0@@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<refentry> + <refentryinfo> + <author> + <firstname>James</firstname><surname>Carter</surname><contrib></contrib> + </author> + </refentryinfo> + + <refmeta> + <refentrytitle>SECILCHECK</refentrytitle> + <manvolnum>8</manvolnum> + <refmiscinfo class="date">27 April 2026</refmiscinfo> + <refmiscinfo class="source">secilcheck</refmiscinfo> + <refmiscinfo class="manual">Check binary policy file against CIL neverallow files</refmiscinfo> + </refmeta> + <refnamediv id="name"> + <refname>secilcheck</refname> + <refpurpose>invoke the CIL neverallow checker</refpurpose> + </refnamediv> + + <refsynopsisdiv id="synopsis"> + <cmdsynopsis> + <command>secilcheck</command> + <arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg> + <arg choice="plain"><replaceable>binary</replaceable></arg> + <arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg> + </cmdsynopsis> + </refsynopsisdiv> + + <refsect1 id="description"><title>DESCRIPTION</title> + <para><emphasis role="italic">secilcheck</emphasis> invokes the CIL neverallow checker with specified <emphasis role="italic">argument</emphasis>s.</para> + </refsect1> + + <refsect1 id="options"><title>OPTIONS</title> + <variablelist> + <varlistentry> + <term><option>-Q, --qualified-names</option></term> + <listitem><para>Allow names containing dots (qualified names). Blocks, blockinherits, blockabstracts, and in-statements will not be allowed.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-m, --multiple-decls</option></term> + <listitem><para>Allow some statements to be re-declared.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-v, --verbose</option></term> + <listitem><para>Increment verbosity level.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-h, --help</option></term> + <listitem><para>Display usage information.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 id="see_also"><title>SEE ALSO</title> + <para> + <simplelist type="inline"> + <member><citerefentry> + <refentrytitle>secilc</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2tree</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2conf</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + </simplelist> + </para> + <para>HTML documentation describing the CIL language statements is available starting with <emphasis role="italic">docs/html/index.html</emphasis>.</para> + <para>PDF documentation describing the CIL language statements is available at: <emphasis role="italic">docs/pdf/CIL_Reference_Guide.pdf</emphasis>.</para> + </refsect1> +</refentry>secilc/secilcheck.c new file +0
-0
Quoted 8 lines
secilc/secilcheck.c line 2
Quoted 6 lines
index 00000000..874f6544
secilc/secilcheck.c new file +2
-0@@ -0,0 +1,301 @@ +/* +* Copyright 2011 Tresys Technology, LLC. All rights reserved. Update the date? Is that the correct header here?
Since I work for the government, everything that I create is actually in the public domain. We can contribute to code released under open source licenses, but we can't create it.
Since this was derived from the secilc.c code, I just kept the copyright notice. That is my usual practice.
I know there is a lot of duplicate code. One of my long-term todo items is to pull out the common code from the various programs in the secilc directory.
Quoted 30 lines
Quoted 30 lines
secilc/secilcheck.c new file +29
-0+* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS +* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +* EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* The views and conclusions contained in the software and documentation are those +* of the authors and should not be interpreted as representing official policies, +* either expressed or implied, of Tresys Technology, LLC. +*/ + +#include <stdlib.h> +#include <stdio.h>
Quoted 9 lines
secilc/secilcheck.c line 37
Quoted 6 lines
secilc/secilcheck.c new file +6
-0+#include <stdint.h> +#include <string.h> +#include <getopt.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> I had to add
unistd.hfor close(2) below.
ok, I will add that.
Thanks,
Jim
Quoted 267 lines
Quoted 267 lines
secilc/secilcheck.c new file +264
-1+ +#ifdef ANDROID +#include <cil/cil.h> +#else +#include <sepol/cil/cil.h> +#endif +#include <sepol/policydb.h> +#include <sepol/kernel_to_cil.h> + +static int get_binary_policy_db (const char *filename,sepol_policydb_t *pdb) +{ +int fd; +struct stat sb; +void *map =NULL ; +sepol_policy_file_t *pf =NULL ; +int rc =SEPOL_ERR ; + +/* Read binary policy */ + fd =open (filename,O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (fstat (fd, &sb) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + map =mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +if (map ==MAP_FAILED ) { +fprintf (stderr,"Could not map file: %s\n" , filename); + goto exit; + } + +if (sepol_policy_file_create (&pf) <0 ) { +fprintf (stderr,"Out of memory" ); + goto exit; + } +sepol_policy_file_set_mem (pf, map, sb.st_size); + +if (sepol_policydb_read (pdb, pf) <0 ) { +fprintf (stderr,"Error reading binary policy: %s\n" , filename); +sepol_policydb_free (pdb); + rc =SEPOL_ERR ; + goto exit; + } + + rc =SEPOL_OK ; + +exit: +if (fd >=0 ) +close (fd); +if (map) +munmap (map, sb.st_size); +sepol_policy_file_free (pf); +return rc; +} + +static int add_decls_to_cil (const char *filename,sepol_policydb_t *pdb,struct cil_db *db) +{ +FILE *cfp =NULL ; +char *cptr =NULL ; +size_t csize; +int rc =SEPOL_ERR ; + + cfp =open_memstream (&cptr, &csize); +if (!cfp) { +fprintf (stderr,"Failed to open dynamic memory buffer stream\n" ); + goto exit; + } + +if (sepol_kernel_policydb_decls_to_cil (cfp, &pdb->p) <0 ) { +fprintf (stderr,"Failed to convert binary policy to CIL\n" ); + goto exit; + } + +if (fflush (cfp) <0 ) { +fprintf (stderr,"Failed to flush CIL memory buffer stream\n" ); + goto exit; + } + +/* Add to CIL db */ +if (cil_add_file (db, filename, cptr, csize) <0 ) { +fprintf (stderr,"Failed to add binary policy file: %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (cfp) +fclose (cfp); +if (cptr) +free (cptr); +return rc; +} + +static int add_cil_file (const char *filename,struct cil_db *cdb) +{ +FILE *file; +struct stat filedata; +char *buffer =NULL ; +size_t file_size; +int rc =-1 ; + + file =fopen (filename,"r" ); +if (!file) { +fprintf (stderr,"Could not open file: %s\n" , filename); + goto exit; + } +if (stat (filename, &filedata) <0 ) { +fprintf (stderr,"Could not stat file: %s\n" , filename); + goto exit; + } + file_size = filedata.st_size; + +if (!file_size) { +fclose (file); + rc =0 ; + goto exit; + } + + buffer =malloc (file_size); +if (!buffer) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } + +if (fread (buffer, file_size,1 , file) !=1 ) { +fprintf (stderr,"Failure reading file: %s\n" , filename); + goto exit; + } + +if (cil_add_file (cdb, filename, buffer, file_size) <0 ) { +fprintf (stderr,"Failure adding %s\n" , filename); + goto exit; + } + + rc =0 ; + +exit: +if (file) +fclose (file); +if (buffer) +free (buffer); +return rc; +} + +static __attribute__((__noreturn__))void usage (const char *prog) +{ +printf ("Usage: %s [OPTION]... BIN_POLICY CIL_NEVERALLOW_FILE1...\n" , prog); +printf ("\n" ); +printf ("Options:\n" ); +printf (" -Q, --qualified-names Allow names containing dots (qualified names).\n" ); +printf (" Blocks, blockinherits, blockabstracts, and\n" ); +printf (" in-statements will not be allowed.\n" ); +printf (" -m, --multiple-decls allow some statements to be re-declared\n" ); +printf (" -v, --verbose increment verbosity level\n" ); +printf (" -h, --help display usage information\n" ); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ +int rc =SEPOL_ERR ; +sepol_policydb_t *pdb =NULL ; +int violation =0 ; +struct cil_db *db =NULL ; +int multiple_decls =0 ; +int qualified_names =0 ; +int opt_char; +int opt_index =0 ; +enum cil_log_level log_level =CIL_ERR ; +static struct option long_opts[] = { + {"help" , no_argument,0 ,'h' }, + {"verbose" , no_argument,0 ,'v' }, + {"multiple-decls" , no_argument,0 ,'m' }, + {"qualified-names" , no_argument,0 ,'Q' }, + {0 ,0 ,0 ,0 } + }; + +while (1 ) { + opt_char =getopt_long (argc, argv,"hvQm" , long_opts, &opt_index); +if (opt_char ==-1 ) { +break ; + } +switch (opt_char) { +case 'v' : + log_level++; +break ; +case 'm' : + multiple_decls =1 ; +break ; +case 'Q' : + qualified_names =1 ; +break ; +case 'h' : +usage (argv[0 ]); +case '?' : +break ; +default : +fprintf (stderr,"Unsupported option: %s\n" , optarg); +usage (argv[0 ]); + } + } + +if (argc < optind +2 ) { +fprintf (stderr,"Not enough files specified\n" ); +usage (argv[0 ]); + } + +cil_set_log_level (log_level); + +cil_db_init (&db); +cil_set_multiple_decls (db, multiple_decls); +cil_set_qualified_names (db, qualified_names); + +if (sepol_policydb_create (&pdb) <0 ) { +fprintf (stderr,"Out of memory\n" ); + goto exit; + } +if (get_binary_policy_db (argv[optind], pdb) <0 ) { +fprintf (stderr,"Failed to get binary policy db\n" ); + rc =SEPOL_ERR ; + goto exit; + } +if (add_decls_to_cil (argv[optind], pdb, db) <0 ) { +fprintf (stderr,"Failed to convert decls to CIL\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + +do { +if (add_cil_file (argv[optind], db) <0 ) { +fprintf (stderr,"Failed to add cil file to CIL files\n" ); + rc =SEPOL_ERR ; + goto exit; + } + optind++; + }while (optind < argc); + + rc =cil_compile (db); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to compile cildb: %d\n" , rc); + goto exit; + } + + rc =cil_check_neverallows_against_pdb (db, &pdb->p, &violation); +if (rc !=SEPOL_OK ) { +fprintf (stderr,"Failed to complete neverallow checking\n" ); + goto exit; + } +if (violation ==1 ) { +fprintf (stderr,"There was a neverallow violation\n" ); + rc =SEPOL_ERR ; + } + + rc =SEPOL_OK ; + +exit: +cil_db_destroy (&db); +sepol_policydb_free (pdb); +return rc; +} --2.53.0
Re: [RFC PATCH 3/3] secilc: Add program that checks CIL neverallow rules against a binary policy
Quoted 215 lines · James Carter
Quoted 198 lines · Thiébaud Weksteen
Quoted 196 lines · James Carter
Add the program secilcheck that takes a binary policy and one or more CIL files and checks the binary policy against the neverallow rules in the CIL policy.
ex/
secilcheck policy.bin neverallows.cilSigned-off-by James Carter <[email protected]>--- secilc/.gitignore | 2 + secilc/Makefile | 20 ++- secilc/secilcheck.8.xml | 81 +++++++++++ secilc/secilcheck.c | 301 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 secilc/secilcheck.8.xml create mode 100644 secilc/secilcheck.csecilc/.gitignore +2
-0@@ -4,6 +4,8 @@ secil2conf secil2conf.8 secil2tree secil2tree.8 +secilcheck +secilcheck.8 policy.* file_contexts docs/htmlsecilc/Makefile +18
-2@@ -14,9 +14,14 @@ SECIL2TREE = secil2tree SECIL2TREE_SRCS := secil2tree.c SECIL2TREE_OBJS := $(patsubst %.c,%.o,$(SECIL2TREE_SRCS)) +SECILCHECK = secilcheck +SECILCHECK_SRCS := secilcheck.c +SECILCHECK_OBJS := $(patsubst %.c,%.o,$(SECILCHECK_SRCS)) + SECILC_MANPAGE = secilc.8 SECIL2CONF_MANPAGE = secil2conf.8 SECIL2TREE_MANPAGE = secil2tree.8 +SECILCHECK_MANPAGE = secilcheck.8 XMLTO = xmlto DIFF = diff@@ -28,7 +33,7 @@ CFLAGS ?= -Wall -Wshadow -Wextra -Wundef -Wmissing-format-attribute -Wcast-align override CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 override LDLIBS += -lsepol -all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) man +all: $(SECILC) $(SECIL2CONF) $(SECIL2TREE) $(SECILCHECK) man $(SECILC): $(SECILC_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)@@ -45,7 +50,10 @@ $(SECIL2CONF): $(SECIL2CONF_OBJS) $(SECIL2TREE): $(SECIL2TREE_OBJS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) -man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) +$(SECILCHECK): $(SECILCHECK_OBJS) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +man: $(SECILC_MANPAGE) $(SECIL2CONF_MANPAGE) $(SECIL2TREE_MANPAGE) $(SECILCHECK_MANPAGE) $(SECILC_MANPAGE): $(SECILC_MANPAGE).xml $(XMLTO) man $(SECILC_MANPAGE).xml@@ -56,15 +64,20 @@ $(SECIL2CONF_MANPAGE): $(SECIL2CONF_MANPAGE).xml $(SECIL2TREE_MANPAGE): $(SECIL2TREE_MANPAGE).xml $(XMLTO) man $(SECIL2TREE_MANPAGE).xml +$(SECILCHECK_MANPAGE): $(SECILCHECK_MANPAGE).xml + $(XMLTO) man $(SECILCHECK_MANPAGE).xml + install: all man -mkdir -p $(DESTDIR)$(BINDIR) -mkdir -p $(DESTDIR)$(MANDIR)/man8 install -m 755 $(SECILC) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2CONF) $(DESTDIR)$(BINDIR) install -m 755 $(SECIL2TREE) $(DESTDIR)$(BINDIR) + install -m 755 $(SECILCHECK) $(DESTDIR)$(BINDIR) install -m 644 $(SECILC_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2CONF_MANPAGE) $(DESTDIR)$(MANDIR)/man8 install -m 644 $(SECIL2TREE_MANPAGE) $(DESTDIR)$(MANDIR)/man8 + install -m 644 $(SECILCHECK_MANPAGE) $(DESTDIR)$(MANDIR)/man8 doc: $(MAKE) -C docs@@ -76,11 +89,14 @@ clean: rm -f $(SECIL2CONF_OBJS) rm -f $(SECIL2TREE) rm -f $(SECIL2TREE_OBJS) + rm -f $(SECILCHECK) + rm -f $(SECILCHECK_OBJS) rm -f policy.* rm -f file_contexts rm -f $(SECILC_MANPAGE) rm -f $(SECIL2CONF_MANPAGE) rm -f $(SECIL2TREE_MANPAGE) + rm -f $(SECILCHECK_MANPAGE) rm -f opt-actual.cil rm -f opt-actual.bin $(MAKE) -C docs cleansecilc/secilcheck.8.xml new file +81
-0@@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<refentry> + <refentryinfo> + <author> + <firstname>James</firstname><surname>Carter</surname><contrib></contrib> + </author> + </refentryinfo> + + <refmeta> + <refentrytitle>SECILCHECK</refentrytitle> + <manvolnum>8</manvolnum> + <refmiscinfo class="date">27 April 2026</refmiscinfo> + <refmiscinfo class="source">secilcheck</refmiscinfo> + <refmiscinfo class="manual">Check binary policy file against CIL neverallow files</refmiscinfo> + </refmeta> + <refnamediv id="name"> + <refname>secilcheck</refname> + <refpurpose>invoke the CIL neverallow checker</refpurpose> + </refnamediv> + + <refsynopsisdiv id="synopsis"> + <cmdsynopsis> + <command>secilcheck</command> + <arg choice="opt" rep="repeat"><replaceable>OPTION</replaceable></arg> + <arg choice="plain"><replaceable>binary</replaceable></arg> + <arg choice="plain" rep="repeat"><replaceable>file</replaceable></arg> + </cmdsynopsis> + </refsynopsisdiv> + + <refsect1 id="description"><title>DESCRIPTION</title> + <para><emphasis role="italic">secilcheck</emphasis> invokes the CIL neverallow checker with specified <emphasis role="italic">argument</emphasis>s.</para> + </refsect1> + + <refsect1 id="options"><title>OPTIONS</title> + <variablelist> + <varlistentry> + <term><option>-Q, --qualified-names</option></term> + <listitem><para>Allow names containing dots (qualified names). Blocks, blockinherits, blockabstracts, and in-statements will not be allowed.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-m, --multiple-decls</option></term> + <listitem><para>Allow some statements to be re-declared.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-v, --verbose</option></term> + <listitem><para>Increment verbosity level.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>-h, --help</option></term> + <listitem><para>Display usage information.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 id="see_also"><title>SEE ALSO</title> + <para> + <simplelist type="inline"> + <member><citerefentry> + <refentrytitle>secilc</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2tree</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + <member><citerefentry> + <refentrytitle>secil2conf</refentrytitle> + <manvolnum>8</manvolnum> + </citerefentry></member> + </simplelist> + </para> + <para>HTML documentation describing the CIL language statements is available starting with <emphasis role="italic">docs/html/index.html</emphasis>.</para> + <para>PDF documentation describing the CIL language statements is available at: <emphasis role="italic">docs/pdf/CIL_Reference_Guide.pdf</emphasis>.</para> + </refsect1> +</refentry>secilc/secilcheck.c new file +0
-0Quoted 8 lines
secilc/secilcheck.c line 2
Quoted 6 lines
index 00000000..874f6544
secilc/secilcheck.c new file +2
-0@@ -0,0 +1,301 @@ +/* +* Copyright 2011 Tresys Technology, LLC. All rights reserved. Update the date? Is that the correct header here?
Since I work for the government, everything that I create is actually in the public domain. We can contribute to code released under open source licenses, but we can't create it.
Since this was derived from thesecilc.ccode, I just kept the copyright notice. That is my usual practice.
Quoted 4 lines
I know there is a lot of duplicate code. One of my long-term todo items is to pull out the common code from the various programs in the secilc directory.
No worries. Thanks for sharing the context. I think the duplication is fine for now.
Thanks for taking the time to write this RFC. Again, the approach looks good to me.
Quoted 3 lines · James Carter
Add the function
cil_check_neverallows_against_pdb()that will extract the neverallows from a CIL db and check them against a kernel policydb.
It might be worth adding a bit more background here? Feel free to pull in some of the commit message from my RFC.
Quoted 59 lines
Signed-off-by James Carter <[email protected]>--- libsepol/cil/include/cil/cil.h | 1 + libsepol/cil/src/cil_binary.c | 46 ++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_binary.h | 12 +++++++++ libsepol/src/libsepol.map.in | 5 ++++ 4 files changed, 64 insertions(+)libsepol/cil/include/cil/cil.h +1
-0@@ -65,6 +65,7 @@ extern int cil_write_parse_ast(FILE *out, cil_db_t *db);extern int cil_write_build_ast (FILE *out,cil_db_t *db);extern int cil_write_resolve_ast (FILE *out,cil_db_t *db);extern int cil_write_post_ast (FILE *out,cil_db_t *db); +extern int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation);enum cil_log_level {CIL_ERR =1 ,libsepol/cil/src/cil_binary.c +30
-0@@ -5143,6 +5143,52 @@ exit:return rc; } +static int __cil_collect_neverallows_helper (struct cil_tree_node *node,uint32_t *finished,void *extra_args) +{ +if (node->flavor ==CIL_BLOCK ) { +struct cil_block *blk = node->data; +if (blk->is_abstract ==CIL_TRUE ) { + *finished =CIL_TREE_SKIP_HEAD ; + } + }else if (node->flavor ==CIL_MACRO ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_BOOLEANIF ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_AVRULE || node->flavor ==CIL_AVRULEX ) { +struct cil_avrule *rule = node->data; +if (rule->rule_kind ==CIL_AVRULE_NEVERALLOW ) { +struct cil_list *neverallows = extra_args; +cil_list_prepend (neverallows,CIL_LIST_ITEM , node); + } + } + +return SEPOL_OK ; +} + +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation) +{ +int rc =SEPOL_OK ; +struct cil_list *neverallows; + +if (db ==NULL || pdb ==NULL ) { +cil_log (CIL_ERR ,"CIL db and/or Policy db is NULL\n" ); +return SEPOL_ERR ;
libsepol/cil/src/cil_binary.c exit: · line 5185
Quoted 10 lines
libsepol/cil/src/cil_binary.c +10
-0+ } + +cil_list_init (&neverallows,CIL_LIST_ITEM ); + rc =cil_tree_walk (db->ast->root, __cil_collect_neverallows_helper,NULL ,NULL , neverallows); +if (rc !=SEPOL_OK ) { +cil_log (CIL_INFO ,"Failure collecting neverallows\n" ); + goto exit; + } + + rc =cil_check_neverallows (db, pdb, neverallows, violation);
I think violation might not be set if nothing is found. For the other existing caller (cil_binary_create_allocated_pdb), violation is manually set to false before calling cil_check_neverallows. We should do the same here?
Quoted 14 lines
libsepol/cil/src/cil_binary.c +6
-0+ +exit: +cil_list_destroy (&neverallows,CIL_FALSE ); +return rc; +} +static struct cil_list *cil_classperms_from_sepol (policydb_t *pdb,uint16_t class,uint32_t data,struct cil_class *class_value_to_cil[],struct cil_perm **perm_value_to_cil[]) {struct cil_classperms *cp;libsepol/cil/src/cil_binary.h +0
-0
libsepol/cil/src/cil_binary.h line 41
Quoted 6 lines
libsepol/cil/src/cil_binary.h +3
-0@@ -36,6 +36,18 @@#include "cil_tree.h" #include "cil_list.h" + +/** +* Check neverallows from a cil database against a policy database
Same here, maybe we can add some context on why this function exists? Do we have a strict requirement that all definitions from pdb are in db?
Quoted 28 lines
libsepol/cil/src/cil_binary.h +9
-0+* +* @param[in] db The cil database. +* @param[in] pdb The policy database. +* @param[out] violation 1 if a neverallow violation was found, 0 otherwise +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation); +/** * Create a binary policydb from the cil db. * libsepol/src/libsepol.map.in +5
-1@@ -294,3 +294,8 @@ LIBSEPOL_3.6 { global: cil_write_post_ast; } LIBSEPOL_3.4; + +LIBSEPOL_3.11 { + global: + cil_check_neverallows_against_pdb; +} LIBSEPOL_3.6; --2.53.0
Quoted 4 lines · Thiébaud Weksteen
Thanks for taking the time to write this RFC. Again, the approach looks good to me.
Quoted 8 lines
Quoted 3 lines · James Carter
Add the function
cil_check_neverallows_against_pdb()that will extract the neverallows from a CIL db and check them against a kernel policydb.It might be worth adding a bit more background here? Feel free to pull in some of the commit message from my RFC.
It could use more background and I will make use of your commit message.
Quoted 66 lines
Quoted 66 lines
Signed-off-by James Carter <[email protected]>--- libsepol/cil/include/cil/cil.h | 1 + libsepol/cil/src/cil_binary.c | 46 ++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_binary.h | 12 +++++++++ libsepol/src/libsepol.map.in | 5 ++++ 4 files changed, 64 insertions(+)libsepol/cil/include/cil/cil.h +1
-0@@ -65,6 +65,7 @@ extern int cil_write_parse_ast(FILE *out, cil_db_t *db);extern int cil_write_build_ast (FILE *out,cil_db_t *db);extern int cil_write_resolve_ast (FILE *out,cil_db_t *db);extern int cil_write_post_ast (FILE *out,cil_db_t *db); +extern int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation);enum cil_log_level {CIL_ERR =1 ,libsepol/cil/src/cil_binary.c +37
-0@@ -5143,6 +5143,52 @@ exit:return rc; } +static int __cil_collect_neverallows_helper (struct cil_tree_node *node,uint32_t *finished,void *extra_args) +{ +if (node->flavor ==CIL_BLOCK ) { +struct cil_block *blk = node->data; +if (blk->is_abstract ==CIL_TRUE ) { + *finished =CIL_TREE_SKIP_HEAD ; + } + }else if (node->flavor ==CIL_MACRO ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_BOOLEANIF ) { + *finished =CIL_TREE_SKIP_HEAD ; + }else if (node->flavor ==CIL_AVRULE || node->flavor ==CIL_AVRULEX ) { +struct cil_avrule *rule = node->data; +if (rule->rule_kind ==CIL_AVRULE_NEVERALLOW ) { +struct cil_list *neverallows = extra_args; +cil_list_prepend (neverallows,CIL_LIST_ITEM , node); + } + } + +return SEPOL_OK ; +} + +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation) +{ +int rc =SEPOL_OK ; +struct cil_list *neverallows; + +if (db ==NULL || pdb ==NULL ) { +cil_log (CIL_ERR ,"CIL db and/or Policy db is NULL\n" ); +return SEPOL_ERR ; + } + +cil_list_init (&neverallows,CIL_LIST_ITEM ); + rc =cil_tree_walk (db->ast->root, __cil_collect_neverallows_helper,NULL ,NULL , neverallows); +if (rc !=SEPOL_OK ) { +cil_log (CIL_INFO ,"Failure collecting neverallows\n" ); + goto exit;
Quoted 9 lines
libsepol/cil/src/cil_binary.c exit: · line 5185
Quoted 3 lines
libsepol/cil/src/cil_binary.c +3
-0+ } + + rc =cil_check_neverallows (db, pdb, neverallows, violation);I think violation might not be set if nothing is found. For the other existing caller (
cil_binary_create_allocated_pdb), violation is manually set to false before callingcil_check_neverallows. We should do the same here?
I believe that you are correct.
Quoted 17 lines
Quoted 17 lines
libsepol/cil/src/cil_binary.c +6
-0+ +exit: +cil_list_destroy (&neverallows,CIL_FALSE ); +return rc; +} +static struct cil_list *cil_classperms_from_sepol (policydb_t *pdb,uint16_t class,uint32_t data,struct cil_class *class_value_to_cil[],struct cil_perm **perm_value_to_cil[]) {struct cil_classperms *cp;libsepol/cil/src/cil_binary.h +0
-0@@ -36,6 +36,18 @@#include "cil_tree.h" #include "cil_list.h"
Quoted 8 lines
libsepol/cil/src/cil_binary.h line 41
Quoted 3 lines
libsepol/cil/src/cil_binary.h +3
-0+ +/** +* Check neverallows from a cil database against a policy database Same here, maybe we can add some context on why this function exists? Do we have a strict requirement that all definitions from pdb are in db?
I could definitely add more context here.
The key is that CIL uses libsepol to check neverallows, so it just converts the CIL neverallow to the policydb neverallow. The CIL db just has to have the declarations needed for the CIL neverallow. I should check to make sure appropriate errors are reported if the policydb does not have a type, class, or permission from the CIL neverallow.
Thanks for the review!
Jim
Quoted 28 lines
Quoted 28 lines
libsepol/cil/src/cil_binary.h +9
-0+* +* @param[in] db The cil database. +* @param[in] pdb The policy database. +* @param[out] violation 1 if a neverallow violation was found, 0 otherwise +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_check_neverallows_against_pdb (const struct cil_db *db,policydb_t *pdb,int *violation); +/** * Create a binary policydb from the cil db. * libsepol/src/libsepol.map.in +5
-1@@ -294,3 +294,8 @@ LIBSEPOL_3.6 { global: cil_write_post_ast; } LIBSEPOL_3.4; + +LIBSEPOL_3.11 { + global: + cil_check_neverallows_against_pdb; +} LIBSEPOL_3.6; --2.53.0