Fix/add optional file type handling for genfscon rules
cover Fix/add optional file type handling for genfscon rules
genfscon rules have always supported an optional file type, but when the ability for writing a policy.conf file from a kernel policy was added to libsepol it did not include that support. Support for the optional file type was also left out of CIL genfscon rules.
This patch set fixes these problems.
Patch 1 adds support for writing the optional file type in genfscon rules when writing a policy.conf file from a kernel policy.
Patches 2-4 adds support in CIL for handling an optional file type in genfscon rules, updates the CIL documentation, and adds support when writing out CIL from a kernel policy or module as well.
James Carter (4):
libsepol: Add support for file types in writing out policy.conf libsepol/cil: Allow optional file type in genfscon rules secilc/docs: Document the optional file type for genfscon rules
libsepol: Write out genfscon file type when writing out CIL policy
libsepol/cil/src/cil_binary.c | 39 +++++++++++++++++++ libsepol/cil/src/cil_build_ast.c | 43 +++++++++++++++++++-- libsepol/cil/src/cil_internal.h | 1 + libsepol/src/kernel_to_cil.c | 35 ++++++++++++++++- libsepol/src/kernel_to_conf.c | 35 ++++++++++++++++- libsepol/src/module_to_cil.c | 27 ++++++++++++- secilc/docs/cil_file_labeling_statements.md | 10 ++++- 7 files changed, 179 insertions(+), 11 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/4 libsepol: Add support for file types in writing out policy.conf
Although rarely used, genfscon rules support the specification of a file type just like the rules in a file context file. The file type is used to make the genfscon rule apply only for a specific security class. Currently, when writing out a policy.conf file from a kernel policy, it is assumed that every genfscon rule applies to all security classes and no file type will be added to the genfscon rule.
Write out the appropriate file type if the genfscon rule is only for a specific security class (file, dir, blk_file, chr_file, fifo_file, lnk_file, or sock_file).
--- libsepol/src/kernel_to_conf.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)
libsepol/src/kernel_to_conf.c +33 -2
@@ -2513,6 +2513,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)struct ocontext *ocon;struct strs *strs;char *fstype, *name, *ctx; +uint32_t sclass; +const char *file_type;int rc; rc =strs_init (&strs,32 );
@@ -2525,14 +2527,43 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb) fstype = genfs->fstype; name = ocon->u.name; + sclass = ocon->v.sclass; + file_type =NULL ; +if (sclass) { +const char *class_name = pdb->p_class_val_to_name[sclass-1 ]; +if (strcmp (class_name,"blk_file" ) ==0 ) { + file_type ="-b" ; + }else if (strcmp (class_name,"chr_file" ) ==0 ) { + file_type ="-c" ; + }else if (strcmp (class_name,"dir" ) ==0 ) { + file_type ="-d" ; + }else if (strcmp (class_name,"fifo_file" ) ==0 ) { + file_type ="-p" ; + }else if (strcmp (class_name,"lnk_file" ) ==0 ) { + file_type ="-l" ; + }else if (strcmp (class_name,"sock_file" ) ==0 ) { + file_type ="-s" ; + }else if (strcmp (class_name,"file" ) ==0 ) { + file_type ="--" ; + }else { + rc =-1 ; + goto exit; + } + } + ctx =context_to_str (pdb, &ocon->context[0 ]);if (!ctx) { rc =-1 ; goto exit; } - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" ,3 , - fstype, name, ctx); +if (file_type) { + rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s %s" ,4 , + fstype, name, file_type, ctx); + }else { + rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" ,3 , + fstype, name, ctx); + }free (ctx);if (rc !=0 ) { goto exit;
Signature · 1 line
2.31.1
PATCH 2/4 libsepol/cil: Allow optional file type in genfscon rules
In reply to James CarterThe optional specification of a file type for a genfscon rule to make it apply only to a specific security class is allowed by checkpolicy and checkmodule and should be allowed for CIL policies as well.
Allow an optional file type to be specified for a genfscon rule. The new syntax:
(genfscon FSNAME PATH [FILE_TYPE] CONTEXT)
FSNAME - The name of the supported filesystem
PATH - If FSNAME is proc then this is the partial path,
othewise this must be "/".
FILE_TYPE - A single keyword representing the file type.
file type security class
file file
dir dir
char chr_file
block blk_file
socket sock_file
pipe fifo_file
symlink lnk_file
any Same as not specifying a file type
CONTEXT - Either a previously declared security context identifier
or an anonymous security context.
--- libsepol/cil/src/cil_binary.c | 39 +++++++++++++++++++++++++++++ libsepol/cil/src/cil_build_ast.c | 43 +++++++++++++++++++++++++++++--- libsepol/cil/src/cil_internal.h | 1 + 3 files changed, 79 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_binary.c +39 -0
@@ -3462,6 +3462,45 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) new_ocon->u.name =cil_strdup (cil_genfscon->path_str); +if (cil_genfscon->file_type && cil_genfscon->file_type !=CIL_FILECON_ANY ) { +class_datum_t *class_datum; +const char *class_name; +switch (cil_genfscon->file_type) { +case CIL_FILECON_FILE : + class_name ="file" ; +break ; +case CIL_FILECON_DIR : + class_name ="dir" ; +break ; +case CIL_FILECON_CHAR : + class_name ="chr_file" ; +break ; +case CIL_FILECON_BLOCK : + class_name ="blk_file" ; +break ; +case CIL_FILECON_SOCKET : + class_name ="sock_file" ; +break ; +case CIL_FILECON_PIPE : + class_name ="fifo_file" ; +break ; +case CIL_FILECON_SYMLINK : + class_name ="lnk_file" ; +break ; +default : +fprintf (stderr,"What is going on?\n" ); + rc =SEPOL_ERR ; + goto exit; + } + class_datum =hashtab_search (pdb->p_classes.table, class_name); +if (!class_datum) { +fprintf (stderr,"What is going on?\n" ); + rc =SEPOL_ERR ; + goto exit; + } + new_ocon->v.sclass = class_datum->s.value; + } + rc =__cil_context_to_sepol_context (pdb, cil_genfscon->context, &new_ocon->context[0 ]);if (rc !=SEPOL_OK ) { goto exit;
libsepol/cil/src/cil_build_ast.c +39 -4
63 lines not on this page · raw message
libsepol/cil/src/cil_internal.h +1 -0
@@ -791,6 +791,7 @@ struct cil_ipaddr {struct cil_genfscon {char *fs_str;char *path_str; +enum cil_filecon_types file_type;char *context_str;struct cil_context *context; };
Signature · 1 line
2.31.1
PATCH 3/4 secilc/docs: Document the optional file type for genfscon rules
In reply to James CarterUpdate the CIL documentation to include the optional file type for genfscon rules.
--- secilc/docs/cil_file_labeling_statements.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
secilc/docs/cil_file_labeling_statements.md +8 -2
@@ -36,11 +36,13 @@ Define entries for labeling files. The compiler will produce these entries in a <col width="44%" /> <col width="55%" /> </colgroup> -<tbody> +<thead> <tr class="odd"> <td align="left"><p><strong>keyword</strong></p></td> <td align="left"><p><strong>file_contexts entry</strong></p></td> </tr> +</thead> +<tbody> <tr class="even"> <td align="left"><p><code>file</code></p></td> <td align="left"><p><code>--</code></p></td>
@@ -185,7 +187,7 @@ Used to allocate a security context to filesystems that cannot support any of th **Statement definition:** ```secil - (genfscon fsname path context_id) + (genfscon fsname path [file_type] context_id) ``` **Where:**
@@ -209,6 +211,10 @@ Used to allocate a security context to filesystems that cannot support any of th <td align="left"><p>If <code>fsname</code> is <code>proc</code>, then the partial path (see examples). For all other types this must be ‘<code>/</code>’.</p></td> </tr> <tr class="even"> +<td align="left"><p><code>file_type</code></p></td> +<td align="left"><p>Optional keyword representing a file type. Valid values are the same as in [`filecon`](cil_file_labeling_statements.md#filecon) rules.</p></td> +</tr> +<tr class="odd"> <td align="left"><p><code>context_id</code></p></td> <td align="left"><p>A previously declared <code>context</code> identifier or an anonymous security context (<code>user role type levelrange</code>), the range MUST be defined whether the policy is MLS/MCS enabled or not.</p></td> </tr>
Signature · 1 line
2.31.1
PATCH 4/4 libsepol: Write out genfscon file type when writing out CIL policy
In reply to James CarterWith an optional file type being added to CIL genfscon rules, it should be used when writing out a kernel policy or module to CIL when a genfscon rule should only apply to a single security class.
--- libsepol/src/kernel_to_cil.c | 35 +++++++++++++++++++++++++++++++++-- libsepol/src/module_to_cil.c | 27 ++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-)
libsepol/src/kernel_to_cil.c +33 -2
@@ -2640,6 +2640,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)struct ocontext *ocon;struct strs *strs;char *fstype, *name, *ctx; +uint32_t sclass; +const char *file_type;int rc; rc =strs_init (&strs,32 );
@@ -2652,14 +2654,43 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb) fstype = genfs->fstype; name = ocon->u.name; + sclass = ocon->v.sclass; + file_type =NULL ; +if (sclass) { +const char *class_name = pdb->p_class_val_to_name[sclass-1 ]; +if (strcmp (class_name,"blk_file" ) ==0 ) { + file_type ="block" ; + }else if (strcmp (class_name,"chr_file" ) ==0 ) { + file_type ="char" ; + }else if (strcmp (class_name,"dir" ) ==0 ) { + file_type ="dir" ; + }else if (strcmp (class_name,"fifo_file" ) ==0 ) { + file_type ="pipe" ; + }else if (strcmp (class_name,"lnk_file" ) ==0 ) { + file_type ="symlink" ; + }else if (strcmp (class_name,"sock_file" ) ==0 ) { + file_type ="socket" ; + }else if (strcmp (class_name,"file" ) ==0 ) { + file_type ="file" ; + }else { + rc =-1 ; + goto exit; + } + } + ctx =context_to_str (pdb, &ocon->context[0 ]);if (!ctx) { rc =-1 ; goto exit; } - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" ,3 , - fstype, name, ctx); +if (file_type) { + rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s %s)" ,4 , + fstype, name, file_type, ctx); + }else { + rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" ,3 , + fstype, name, ctx); + }free (ctx);if (rc !=0 ) { goto exit;
libsepol/src/module_to_cil.c +26 -1
@@ -2961,10 +2961,35 @@ static int genfscon_to_cil(struct policydb *pdb) {struct genfs *genfs;struct ocontext *ocon; +uint32_t sclass;for (genfs = pdb->genfs; genfs !=NULL ; genfs = genfs->next) {for (ocon = genfs->head; ocon !=NULL ; ocon = ocon->next) { -cil_printf ("(genfscon %s \"%s\" " , genfs->fstype, ocon->u.name); + sclass = ocon->v.sclass; +if (sclass) { +const char *file_type; +const char *class_name = pdb->p_class_val_to_name[sclass-1 ]; +if (strcmp (class_name,"blk_file" ) ==0 ) { + file_type ="block" ; + }else if (strcmp (class_name,"chr_file" ) ==0 ) { + file_type ="char" ; + }else if (strcmp (class_name,"dir" ) ==0 ) { + file_type ="dir" ; + }else if (strcmp (class_name,"fifo_file" ) ==0 ) { + file_type ="pipe" ; + }else if (strcmp (class_name,"lnk_file" ) ==0 ) { + file_type ="symlink" ; + }else if (strcmp (class_name,"sock_file" ) ==0 ) { + file_type ="socket" ; + }else if (strcmp (class_name,"file" ) ==0 ) { + file_type ="file" ; + }else { +return -1 ; + } +cil_printf ("(genfscon %s \"%s\" %s " , genfs->fstype, ocon->u.name, file_type); + }else { +cil_printf ("(genfscon %s \"%s\" " , genfs->fstype, ocon->u.name); + }context_to_cil (pdb, &ocon->context[0 ]);cil_printf (")\n" ); }
Signature · 1 line
2.31.1
Stephen Smalley
maintainerQuoted 21 lines · James Carter
genfscon rules have always supported an optional file type, but when the ability for writing a policy.conf file from a kernel policy was added to libsepol it did not include that support. Support for the optional file type was also left out of CIL genfscon rules.
This patch set fixes these problems.
Patch 1 adds support for writing the optional file type in genfscon rules when writing a policy.conf file from a kernel policy.
Patches 2-4 adds support in CIL for handling an optional file type in genfscon rules, updates the CIL documentation, and adds support when writing out CIL from a kernel policy or module as well.
James Carter (4):
libsepol: Add support for file types in writing out policy.conf libsepol/cil: Allow optional file type in genfscon rules secilc/docs: Document the optional file type for genfscon rules
libsepol: Write out genfscon file type when writing out CIL policy
Quoted 8 lines
libsepol/cil/src/cil_binary.c | 39 +++++++++++++++++++ libsepol/cil/src/cil_build_ast.c | 43 +++++++++++++++++++-- libsepol/cil/src/cil_internal.h | 1 + libsepol/src/kernel_to_cil.c | 35 ++++++++++++++++- libsepol/src/kernel_to_conf.c | 35 ++++++++++++++++- libsepol/src/module_to_cil.c | 27 ++++++++++++- secilc/docs/cil_file_labeling_statements.md | 10 ++++- 7 files changed, 179 insertions(+), 11 deletions(-)
Something here breaks on the selinux-testsuite policy:
3231# Run the test suite
3232#
3233make test
3234make -C policy load
3235make[1]: Entering directory '/root/selinux-testsuite/policy' 3236# Test for "expand-check = 0" in /etc/selinux/semanage.conf 3237# General policy build
3238make[2]: Entering directory '/root/selinux-testsuite/policy/test_policy' 3239Compiling targeted test_policy module
3240Creating targeted test_policy.pp policy package
3241rm tmp/test_policy.mod tmp/test_policy.mod.fc
3242make[2]: Leaving directory '/root/selinux-testsuite/policy/test_policy' 3243# General policy load
3244domain_fd_use --> on
3245/usr/sbin/semodule -i test_policy/test_policy.pp test_mlsconstrain.cil test_overlay_defaultrange.cil test_userfaultfd.cil test_add_levels.cil test_glblub.cil 3246What is going on?
3247Failed to generate binary
3248/usr/sbin/semodule: Failed!
3249make[1]: *** [Makefile:189: load] Error 1
3250make[1]: Leaving directory '/root/selinux-testsuite/policy' 3251make: *** [Makefile:7: test] Error 2
3252Error: Process completed with exit code 2.
Quoted 50 lines · Stephen Smalley
Quoted 21 lines · James Carter
genfscon rules have always supported an optional file type, but when the ability for writing a policy.conf file from a kernel policy was added to libsepol it did not include that support. Support for the optional file type was also left out of CIL genfscon rules.
This patch set fixes these problems.
Patch 1 adds support for writing the optional file type in genfscon rules when writing a policy.conf file from a kernel policy.
Patches 2-4 adds support in CIL for handling an optional file type in genfscon rules, updates the CIL documentation, and adds support when writing out CIL from a kernel policy or module as well.
James Carter (4):
libsepol: Add support for file types in writing out policy.conf libsepol/cil: Allow optional file type in genfscon rules secilc/docs: Document the optional file type for genfscon rules
libsepol: Write out genfscon file type when writing out CIL policyQuoted 8 lines
libsepol/cil/src/cil_binary.c | 39 +++++++++++++++++++ libsepol/cil/src/cil_build_ast.c | 43 +++++++++++++++++++-- libsepol/cil/src/cil_internal.h | 1 + libsepol/src/kernel_to_cil.c | 35 ++++++++++++++++- libsepol/src/kernel_to_conf.c | 35 ++++++++++++++++- libsepol/src/module_to_cil.c | 27 ++++++++++++- secilc/docs/cil_file_labeling_statements.md | 10 ++++- 7 files changed, 179 insertions(+), 11 deletions(-)Something here breaks on the selinux-testsuite policy:
3231# Run the test suite
3232#
3233make test
3234make -C policy load
3235make[1]: Entering directory '/root/selinux-testsuite/policy' 3236# Test for "expand-check = 0" in/etc/selinux/semanage.conf3237# General policy build
3238make[2]: Entering directory '/root/selinux-testsuite/policy/test_policy' 3239Compiling targetedtest_policymodule
3240Creating targeted test_policy.pp policy package
3241rm tmp/test_policy.mod tmp/test_policy.mod.fc
3242make[2]: Leaving directory '/root/selinux-testsuite/policy/test_policy' 3243# General policy load
3244domain_fd_use --> on
3245/usr/sbin/semodule -i test_policy/test_policy.pp test_mlsconstrain.cil test_overlay_defaultrange.cil
Quoted 8 lines
test_userfaultfd.cil test_add_levels.cil test_glblub.cil 3246What is going on?
3247Failed to generate binary
3248/usr/sbin/semodule: Failed!
3249make[1]: *** [Makefile:189: load] Error 1 3250make[1]: Leaving directory '/root/selinux-testsuite/policy' 3251make: *** [Makefile:7: test] Error 2
3252Error: Process completed with exit code 2.
Hello,
This error happens because cil_genfscon_init() does not initialize ->file_type, and cil_genfscon_to_policydb() then reads an uninitialized value. The attached patch (on top of this series) fixed the CI for me.
Cheers,
Nicolas
libsepol/cil/src/cil.c +1 -0
@@ -2576,6 +2576,7 @@ void cil_genfscon_init(struct cil_genfscon **genfscon) (*genfscon)->path_str =NULL ; (*genfscon)->context_str =NULL ; (*genfscon)->context =NULL ; + (*genfscon)->file_type =0 ;/* FIXME: "0" seems to mean "not initialized" but is not a value for enum cil_filecon_types. CIL_FILECON_ANY could be better? */ }void cil_pirqcon_init (struct cil_pirqcon **pirqcon)
libsepol/cil/src/cil_binary.c +2 -2
@@ -3494,13 +3494,13 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) class_name ="lnk_file" ;break ;default : -fprintf (stderr,"What is going on?\n" ); +cil_log (CIL_ERR ,"genfscon used an unknown file type: %u\n" , cil_genfscon->file_type); rc =SEPOL_ERR ; goto exit; } class_datum =hashtab_search (pdb->p_classes.table, class_name);if (!class_datum) { -fprintf (stderr,"What is going on?\n" ); +cil_log (CIL_ERR ,"genfscon used a class which was not found in the policy: %s\n" , class_name); rc =SEPOL_ERR ; goto exit; }
Quoted 64 lines · Nicolas Iooss
Quoted 50 lines · Stephen Smalley
Quoted 21 lines · James Carter
genfscon rules have always supported an optional file type, but when the ability for writing a policy.conf file from a kernel policy was added to libsepol it did not include that support. Support for the optional file type was also left out of CIL genfscon rules.
This patch set fixes these problems.
Patch 1 adds support for writing the optional file type in genfscon rules when writing a policy.conf file from a kernel policy.
Patches 2-4 adds support in CIL for handling an optional file type in genfscon rules, updates the CIL documentation, and adds support when writing out CIL from a kernel policy or module as well.
James Carter (4):
libsepol: Add support for file types in writing out policy.conf libsepol/cil: Allow optional file type in genfscon rules secilc/docs: Document the optional file type for genfscon rules
libsepol: Write out genfscon file type when writing out CIL policyQuoted 8 lines
libsepol/cil/src/cil_binary.c | 39 +++++++++++++++++++ libsepol/cil/src/cil_build_ast.c | 43 +++++++++++++++++++-- libsepol/cil/src/cil_internal.h | 1 + libsepol/src/kernel_to_cil.c | 35 ++++++++++++++++- libsepol/src/kernel_to_conf.c | 35 ++++++++++++++++- libsepol/src/module_to_cil.c | 27 ++++++++++++- secilc/docs/cil_file_labeling_statements.md | 10 ++++- 7 files changed, 179 insertions(+), 11 deletions(-)Something here breaks on the selinux-testsuite policy:
3231# Run the test suite
3232#
3233make test
3234make -C policy load
3235make[1]: Entering directory '/root/selinux-testsuite/policy' 3236# Test for "expand-check = 0" in/etc/selinux/semanage.conf3237# General policy build
3238make[2]: Entering directory '/root/selinux-testsuite/policy/test_policy' 3239Compiling targetedtest_policymodule
3240Creating targeted test_policy.pp policy package
3241rm tmp/test_policy.mod tmp/test_policy.mod.fc
3242make[2]: Leaving directory '/root/selinux-testsuite/policy/test_policy' 3243# General policy load
3244domain_fd_use --> on
3245/usr/sbin/semodule -i test_policy/test_policy.pp test_mlsconstrain.cil test_overlay_defaultrange.cilQuoted 8 lines
test_userfaultfd.cil test_add_levels.cil test_glblub.cil 3246What is going on?
3247Failed to generate binary
3248/usr/sbin/semodule: Failed!
3249make[1]: *** [Makefile:189: load] Error 1 3250make[1]: Leaving directory '/root/selinux-testsuite/policy' 3251make: *** [Makefile:7: test] Error 2
3252Error: Process completed with exit code 2.Hello,
Quoted 7 lines
This error happens because
cil_genfscon_init()does not initialize ->file_type, andcil_genfscon_to_policydb()then reads an uninitialized value. The attached patch (on top of this series) fixed the CI for me.Cheers,
Nicolas
Thanks for finding the problem. There is a few other additions that are needed, so a v2 will be coming.
Jim