userspace: Implement new format of filename trans rules
cover userspace: Implement new format of filename trans rules
These patches are the userspace side of the kernel change posted at [1].
The first patch changes libsepol's internal representation of filename transition rules in a way similar to kernel commit c3a276111ea2 ("selinux: optimize storage of filename transitions") [2].
The second patch then builds upon that and implements reading and writing of a new binary policy format that uses this representation also in the data layout.
See individual patches for more details.
NOTE: This series unfortunately breaks the build of setools. Moreover, when an existing build of setools dynamically links against the new libsepol, it segfaults. Sadly, there doesn't seem to be a nice way of handling this, since setools relies on non-public libsepol policydb API/ABI.
[1] https://lore.kernel.org/selinux/[email protected]/T/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git/commit/?id=c3a276111ea2572399281988b3129683e2a6b60b
Ondrej Mosnacek (2):
libsepol,checkpolicy: optimize storage of filename transitions
libsepol: implement POLICYDB_VERSION_COMP_FTRANS
checkpolicy/policy_define.c | 52 ++-- checkpolicy/test/dispol.c | 27 +- libsepol/cil/src/cil_binary.c | 29 +- libsepol/include/sepol/policydb/policydb.h | 18 +- libsepol/src/expand.c | 60 +--- libsepol/src/kernel_to_cil.c | 24 +- libsepol/src/kernel_to_conf.c | 24 +- libsepol/src/policydb.c | 313 ++++++++++++++++----- libsepol/src/write.c | 100 +++++-- 9 files changed, 440 insertions(+), 207 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 libsepol,checkpolicy: optimize storage of filename transitions
In preparation to support a new policy format with a more optimal representation of filename transition rules, this patch applies an equivalent change from kernel commit c3a276111ea2 ("selinux: optimize storage of filename transitions").
See the kernel commit's description [1] for the rationale behind this representation. This change doesn't bring any measurable difference of policy build performance (semodule -B) on Fedora.
--- checkpolicy/policy_define.c | 52 +++------ checkpolicy/test/dispol.c | 27 +++-- libsepol/cil/src/cil_binary.c | 29 ++--- libsepol/include/sepol/policydb/policydb.h | 15 ++- libsepol/src/expand.c | 60 +++------- libsepol/src/kernel_to_cil.c | 24 +++- libsepol/src/kernel_to_conf.c | 24 +++- libsepol/src/policydb.c | 126 ++++++++++++++------- libsepol/src/write.c | 46 ++++---- 9 files changed, 223 insertions(+), 180 deletions(-)
checkpolicy/policy_define.c +16 -36
@@ -3303,10 +3303,9 @@ int define_filename_trans(void)ebitmap_t e_stypes, e_ttypes;ebitmap_t e_tclasses;ebitmap_node_t *snode, *tnode, *cnode; -filename_trans_t *ft; -filename_trans_datum_t *ftdatum;filename_trans_rule_t *ftr;type_datum_t *typdatum; +char *dup_name;uint32_t otype;unsigned int c, s, t;int add, rc;
@@ -3388,40 +3387,21 @@ int define_filename_trans(void)ebitmap_for_each_positive_bit (&e_tclasses, cnode, c) {ebitmap_for_each_positive_bit (&e_stypes, snode, s) {ebitmap_for_each_positive_bit (&e_ttypes, tnode, t) { - ft =calloc (1 ,sizeof (*ft)); -if (!ft) { -yyerror ("out of memory" ); - goto bad; - } - ft->stype = s+1 ; - ft->ttype = t+1 ; - ft->tclass = c+1 ; - ft->name =strdup (name); -if (!ft->name) { -yyerror ("out of memory" ); - goto bad; - } - - ftdatum =hashtab_search (policydbp->filename_trans, - (hashtab_key_t )ft); -if (ftdatum) { -yyerror2 ("duplicate filename transition for: filename_trans %s %s %s:%s" , - name, - policydbp->p_type_val_to_name[s], - policydbp->p_type_val_to_name[t], - policydbp->p_class_val_to_name[c]); - goto bad; - } - - ftdatum =calloc (1 ,sizeof (*ftdatum)); -if (!ftdatum) { -yyerror ("out of memory" ); - goto bad; - } - rc =hashtab_insert (policydbp->filename_trans, - (hashtab_key_t )ft, - ftdatum); -if (rc) { + dup_name =NULL ; + rc =policydb_filetrans_insert ( + policydbp, s+1 , t+1 , c+1 , name, + &dup_name, otype,NULL + ); +free (dup_name); +if (rc !=SEPOL_OK ) { +if (rc ==SEPOL_EEXIST ) { +yyerror2 ("duplicate filename transition for: filename_trans %s %s %s:%s" , + name, + policydbp->p_type_val_to_name[s], + policydbp->p_type_val_to_name[t], + policydbp->p_class_val_to_name[c]); + goto bad; + }yyerror ("out of memory" ); goto bad; }
checkpolicy/test/dispol.c +20 -7
@@ -329,26 +329,39 @@ static void display_role_trans(policydb_t *p, FILE *fp)struct filenametr_display_args {policydb_t *p;FILE *fp; +filename_trans_key_t *ft; }; -static int filenametr_display (hashtab_key_t key, -hashtab_datum_t datum, -void *ptr) +static int filenametr_display_sub (hashtab_key_t key, +hashtab_datum_t datum, +void *ptr) { -struct filename_trans *ft = (struct filename_trans *)key; -struct filename_trans_datum *ftdatum = datum;struct filenametr_display_args *args = ptr; +filename_trans_key_t *ft = args->ft;policydb_t *p = args->p;FILE *fp = args->fp; +uint32_t stype = (uintptr_t )key; +uint32_t otype = (uintptr_t )datum; -display_id (p, fp,SYM_TYPES , ft->stype -1 ,"" ); +display_id (p, fp,SYM_TYPES , stype -1 ,"" );display_id (p, fp,SYM_TYPES , ft->ttype -1 ,"" );display_id (p, fp,SYM_CLASSES , ft->tclass -1 ,":" ); -display_id (p, fp,SYM_TYPES , ftdatum->otype -1 ,"" ); +display_id (p, fp,SYM_TYPES , otype -1 ,"" );fprintf (fp," %s\n" , ft->name);return 0 ; } +static int filenametr_display (hashtab_key_t key, +hashtab_datum_t datum, +void *ptr) +{ +struct filenametr_display_args *args = ptr; +hashtab_t subhash = datum; + + args->ft = (filename_trans_key_t *)key; +return hashtab_map (subhash, filenametr_display_sub, args); +} +static void display_filename_trans (policydb_t *p,FILE *fp) {
libsepol/cil/src/cil_binary.c +10 -19
@@ -1131,13 +1131,13 @@ int __cil_typetransition_to_avtab(policydb_t *pdb, const struct cil_db *db, struclass_datum_t *sepol_obj =NULL ;struct cil_list *class_list;type_datum_t *sepol_result =NULL ; -filename_trans_t *newkey =NULL ; -filename_trans_datum_t *newdatum =NULL , *otype =NULL ;ebitmap_t src_bitmap, tgt_bitmap;ebitmap_node_t *node1, *node2;unsigned int i, j; +uint32_t otype;struct cil_list_item *c;char *name =DATUM (typetrans->name)->name; +char *dup_name;if (name ==CIL_KEY_STAR ) {struct cil_type_rule trans;
@@ -1176,22 +1176,16 @@ int __cil_typetransition_to_avtab(policydb_t *pdb, const struct cil_db *db, stru rc =__cil_get_sepol_class_datum (pdb,DATUM (c->data), &sepol_obj);if (rc !=SEPOL_OK ) goto exit; - newkey =cil_calloc (1 ,sizeof (*newkey)); - newdatum =cil_calloc (1 ,sizeof (*newdatum)); - newkey->stype = sepol_src->s.value; - newkey->ttype = sepol_tgt->s.value; - newkey->tclass = sepol_obj->s.value; - newkey->name =cil_strdup (name); - newdatum->otype = sepol_result->s.value; - - rc =hashtab_insert (pdb->filename_trans, - (hashtab_key_t )newkey, - newdatum); + dup_name =NULL ; + rc =policydb_filetrans_insert ( + pdb, sepol_src->s.value, sepol_tgt->s.value, + sepol_obj->s.value, name, &dup_name, + sepol_result->s.value, &otype + ); +free (dup_name);if (rc !=SEPOL_OK ) {if (rc ==SEPOL_EEXIST ) { - otype =hashtab_search (pdb->filename_trans, - (hashtab_key_t )newkey); -if (newdatum->otype != otype->otype) { +if (sepol_result->s.value!= otype) {cil_log (CIL_ERR ,"Conflicting name type transition rules\n" ); }else { rc =SEPOL_OK ;
@@ -1199,9 +1193,6 @@ int __cil_typetransition_to_avtab(policydb_t *pdb, const struct cil_db *db, stru }else {cil_log (CIL_ERR ,"Out of memory\n" ); } -free (newkey->name); -free (newkey); -free (newdatum);if (rc !=SEPOL_OK ) { goto exit; }
libsepol/include/sepol/policydb/policydb.h +11 -4
@@ -162,15 +162,16 @@ typedef struct role_allow { } role_allow_t;/* filename_trans rules */ -typedef struct filename_trans { -uint32_t stype; +typedef struct filename_trans_key {uint32_t ttype;uint32_t tclass;char *name; -}filename_trans_t ; +}filename_trans_key_t ;typedef struct filename_trans_datum { -uint32_t otype;/* expected of new object */ +ebitmap_t stypes; +uint32_t otype; +struct filename_trans_datum *next; }filename_trans_datum_t ;/* Type attributes */
@@ -591,6 +592,7 @@ typedef struct policydb {/* file transitions with the last path component */ hashtab_t filename_trans; +uint32_t filename_trans_count;ebitmap_t *type_attr_map;
@@ -650,6 +652,11 @@ extern int policydb_load_isids(policydb_t * p, sidtab_t * s);extern int policydb_sort_ocontexts (policydb_t *p); +extern int policydb_filetrans_insert (policydb_t *p,uint32_t stype, +uint32_t ttype,uint32_t tclass, +const char *name,char **name_alloc, +uint32_t otype,uint32_t *present_otype); +/* Deprecated */ extern int policydb_context_isvalid (const policydb_t * p,const context_struct_t * c);
libsepol/src/expand.c +14 -46
89 lines not on this page · raw message
libsepol/src/kernel_to_cil.c +19 -5
@@ -1821,21 +1821,35 @@ struct map_filename_trans_args {static int map_filename_trans_to_str (hashtab_key_t key,void *data,void *arg) { -filename_trans_t *ft = (filename_trans_t *)key; +filename_trans_key_t *ft = (filename_trans_key_t *)key;filename_trans_datum_t *datum = data;struct map_filename_trans_args *map_args = arg;struct policydb *pdb = map_args->pdb;struct strs *strs = map_args->strs;char *src, *tgt, *class, *filename, *new; +struct ebitmap_node *node; +uint32_t bit; +int rc; - src = pdb->p_type_val_to_name[ft->stype -1 ]; tgt = pdb->p_type_val_to_name[ft->ttype -1 ]; class = pdb->p_class_val_to_name[ft->tclass -1 ]; filename = ft->name; - new = pdb->p_type_val_to_name[datum->otype -1 ]; +do { + new = pdb->p_type_val_to_name[datum->otype -1 ]; + +ebitmap_for_each_positive_bit (&datum->stypes, node, bit) { + src = pdb->p_type_val_to_name[bit]; + rc =strs_create_and_add (strs, +"(typetransition %s %s %s %s %s)" , +5 , src, tgt, class, filename, new); +if (rc) +return rc; + } + + datum = datum->next; + }while (datum); -return strs_create_and_add (strs,"(typetransition %s %s %s %s %s)" ,5 , - src, tgt, class, filename, new); +return 0 ; }static int write_filename_trans_rules_to_cil (FILE *out,struct policydb *pdb)
libsepol/src/kernel_to_conf.c +19 -5
@@ -1801,21 +1801,35 @@ struct map_filename_trans_args {static int map_filename_trans_to_str (hashtab_key_t key,void *data,void *arg) { -filename_trans_t *ft = (filename_trans_t *)key; +filename_trans_key_t *ft = (filename_trans_key_t *)key;filename_trans_datum_t *datum = data;struct map_filename_trans_args *map_args = arg;struct policydb *pdb = map_args->pdb;struct strs *strs = map_args->strs;char *src, *tgt, *class, *filename, *new; +struct ebitmap_node *node; +uint32_t bit; +int rc; - src = pdb->p_type_val_to_name[ft->stype -1 ]; tgt = pdb->p_type_val_to_name[ft->ttype -1 ]; class = pdb->p_class_val_to_name[ft->tclass -1 ]; filename = ft->name; - new = pdb->p_type_val_to_name[datum->otype -1 ]; +do { + new = pdb->p_type_val_to_name[datum->otype -1 ]; + +ebitmap_for_each_positive_bit (&datum->stypes, node, bit) { + src = pdb->p_type_val_to_name[bit]; + rc =strs_create_and_add (strs, +"type_transition %s %s:%s %s \"%s\";" , +5 , src, tgt, class, new, filename); +if (rc) +return rc; + } + + datum = datum->next; + }while (datum); -return strs_create_and_add (strs,"type_transition %s %s:%s %s \"%s\";" ,5 , - src, tgt, class, new, filename); +return 0 ; }static int write_filename_trans_rules_to_conf (FILE *out,struct policydb *pdb)
libsepol/src/policydb.c +88 -38
193 lines not on this page · raw message
libsepol/src/write.c +26 -20
70 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 2/2 libsepol: implement POLICYDB_VERSION_COMP_FTRANS
In reply to Ondrej MosnacekImplement a new, more space-efficient form of storing filename transitions in the binary policy. The internal structures have already been converted to this new representation; this patch just implements reading/writing an equivalent representation from/to the binary policy.
This new format reduces the size of Fedora policy from 7.6 MB to only 3.3 MB (with policy optimization enabled in both cases). With the unconfined module disabled, the size is reduced from 3.3 MB to 2.4 MB.
--- libsepol/include/sepol/policydb/policydb.h | 3 +- libsepol/src/policydb.c | 217 +++++++++++++++++---- libsepol/src/write.c | 72 ++++++- 3 files changed, 241 insertions(+), 51 deletions(-)
libsepol/include/sepol/policydb/policydb.h +2 -1
@@ -755,10 +755,11 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);#define POLICYDB_VERSION_XPERMS_IOCTL 30/* Linux-specific */ #define POLICYDB_VERSION_INFINIBAND 31/* Linux-specific */ #define POLICYDB_VERSION_GLBLUB 32 +#define POLICYDB_VERSION_COMP_FTRANS 33/* compressed filename transitions */ /* Range of policy versions we understand*/ #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE -#define POLICYDB_VERSION_MAX POLICYDB_VERSION_GLBLUB +#define POLICYDB_VERSION_MAX POLICYDB_VERSION_COMP_FTRANS/* Module versions and specific changes*/ #define MOD_POLICYDB_VERSION_BASE 4
libsepol/src/policydb.c +176 -41
255 lines not on this page · raw message
libsepol/src/write.c +63 -9
92 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 2/2] libsepol: implement POLICYDB_VERSION_COMP_FTRANS
Quoted 11 lines · Ondrej Mosnacek
Implement a new, more space-efficient form of storing filename transitions in the binary policy. The internal structures have already been converted to this new representation; this patch just implements reading/writing an equivalent representation from/to the binary policy.
This new format reduces the size of Fedora policy from 7.6 MB to only 3.3 MB (with policy optimization enabled in both cases). With the unconfined module disabled, the size is reduced from 3.3 MB to 2.4 MB.
Signed-off-by Ondrej Mosnacek <[email protected]>---
Haven't looked at the code yet, but something is wrong with the handling when it needs to dowgrade to an older policy version for a kernel that doesn't yet support this new version:
$ sudo semodule -B libsepol.mls_read_range_helper: range overflow libsepol.context_read_and_validate: error reading MLS range of context libsepol.policydb_to_image: new policy image is invalid libsepol.policydb_to_image: could not create policy image SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.33, searching for an older version.
Re: [PATCH 2/2] libsepol: implement POLICYDB_VERSION_COMP_FTRANS
Quoted 17 lines · Stephen Smalley
Quoted 11 lines · Ondrej Mosnacek
Implement a new, more space-efficient form of storing filename transitions in the binary policy. The internal structures have already been converted to this new representation; this patch just implements reading/writing an equivalent representation from/to the binary policy.
This new format reduces the size of Fedora policy from 7.6 MB to only 3.3 MB (with policy optimization enabled in both cases). With the unconfined module disabled, the size is reduced from 3.3 MB to 2.4 MB.
Signed-off-by Ondrej Mosnacek <[email protected]>---
Haven't looked at the code yet, but something is wrong with the handling when it needs to dowgrade to an older policy version for a kernel that doesn't yet support this new version:
Quoted 7 lines
$ sudo semodule -B libsepol.mls_read_range_helper: range overflow libsepol.context_read_and_validate: error reading MLS range of context libsepol.policydb_to_image: new policy image is invalid libsepol.policydb_to_image: could not create policy image SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.33, searching for an older version.
Hm, haven't tried that... I reproduced it on my end and I believe I have found the bug - filename_trans_read_one_new() is counting p->filename_trans_count in a completely wrong way. It needs to add up the cardinalities of all stype bitmaps, not just count the hashtab entries...
I'll post a v2 tomorrow, in the meantime you can test with this patch on top:
https://github.com/WOnder93/selinux/commit/738263d5be83323da7b4008e37140ec7ef99db8d.patch
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Software Engineer, Security Technologies Red Hat, Inc.
Quoted 10 lines · Ondrej Mosnacek
These patches are the userspace side of the kernel change posted at [1].
The first patch changes libsepol's internal representation of filename transition rules in a way similar to kernel commit
c3a276111ea2("selinux: optimize storage of filename transitions") [2].The second patch then builds upon that and implements reading and writing of a new binary policy format that uses this representation also in the data layout.
Quoted 7 lines
See individual patches for more details.
NOTE: This series unfortunately breaks the build of setools. Moreover, when an existing build of setools dynamically links against the new libsepol, it segfaults. Sadly, there doesn't seem to be a nice way of handling this, since setools relies on non-public libsepol policydb API/ABI.
I think this has happened before a few years ago when we made a different change to those structures, and required updates on the setools side.
Maybe we need to figure out what setools needs to be encapsulated and exported as part of the libsepol public ABI/API, and then stop having it peer into libsepol internals?
Quoted 19 lines · Stephen Smalley
Quoted 10 lines · Ondrej Mosnacek
These patches are the userspace side of the kernel change posted at [1].
The first patch changes libsepol's internal representation of filename transition rules in a way similar to kernel commit
c3a276111ea2("selinux: optimize storage of filename transitions") [2].The second patch then builds upon that and implements reading and writing of a new binary policy format that uses this representation also in the data layout.
Quoted 7 lines
See individual patches for more details.
NOTE: This series unfortunately breaks the build of setools. Moreover, when an existing build of setools dynamically links against the new libsepol, it segfaults. Sadly, there doesn't seem to be a nice way of handling this, since setools relies on non-public libsepol policydb API/ABI.
Quoted 3 lines
I think this has happened before a few years ago when we made a different change to those structures, and required updates on the setools side.
Maybe we need to figure out what setools needs to be encapsulated and exported as part of the libsepol public ABI/API, and then stop having it peer into libsepol internals?
I'd be fine with that :)
Ultimately SETools does 2 thing for the policy access:
* iterate over the entire policy, reading data out of the various objects
* use linkages between objects in the policy, e.g. get the type/attributes from an AV rule, get types from an attribute, etc. using the stuff like class_val_to_struct as needed.
So if sufficient functionality to do dispol was exported, that would get close to all that was needed. There are some optimizations I made by digging at the structs a bit more than the above, but that could potentially be dropped if libsepol has sufficient support. See <https://github.com/SELinuxProject/setools/blob/master/setools/policyrep/selinuxpolicy.pxi#L673> for the policy loading code.
Signature · 1 line
Chris PeBenito
Quoted 25 lines · Chris PeBenito
Quoted 23 lines · Stephen Smalley
Quoted 10 lines · Ondrej Mosnacek
These patches are the userspace side of the kernel change posted at [1].
The first patch changes libsepol's internal representation of filename transition rules in a way similar to kernel commit
c3a276111ea2("selinux: optimize storage of filename transitions") [2].The second patch then builds upon that and implements reading and writing of a new binary policy format that uses this representation also in the data layout.
Quoted 7 lines
See individual patches for more details.
NOTE: This series unfortunately breaks the build of setools. Moreover, when an existing build of setools dynamically links against the new libsepol, it segfaults. Sadly, there doesn't seem to be a nice way of handling this, since setools relies on non-public libsepol policydb API/ABI.
I think this has happened before a few years ago when we made a different change to those structures, and required updates on the setools side.
Quoted 6 lines
Quoted 3 lines
Maybe we need to figure out what setools needs to be encapsulated and exported as part of the libsepol public ABI/API, and then stop having it peer into libsepol internals?
I'd be fine with that :)
I am a little confused. I would consider peering into the libsepol internals to be something like assuming the memory layout of structures and pulling things out based on those assumptions, but setools is just using public header files and functions. It seems to me to be using the public API. Now I think that too much of the internals are being exposed, but I am not sure we can do anything about that now.
It doesn't seem like it would be too hard to update setools to work with this change, unless there is a requirement that it still work with older versions of libsepol. I am not very familiar with cython and I don't know how to make it work differently depending on what version of libsepol is on the system.
Speaking of versions, since we are actually modifying structs in the header files instead of just adding new things, don't we need change the version of libsepol or something?
Quoted 6 lines
Ultimately SETools does 2 thing for the policy access:
* iterate over the entire policy, reading data out of the various objects
* use linkages between objects in the policy, e.g. get the type/attributes from an AV rule, get types from an attribute, etc. using the stuff likeclass_val_to_structas needed.
Quoted 7 lines
So if sufficient functionality to do dispol was exported, that would get close to all that was needed. There are some optimizations I made by digging at the structs a bit more than the above, but that could potentially be dropped if libsepol has sufficient support. See
<https://github.com/SELinuxProject/setools/blob/master/setools/policyrep/selinuxpolicy.pxi#L673>
for the policy loading code.
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us avrule_t, role_trans_rule_t, role_allow_t, filename_trans_rule_t, range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks the filename_trans hashtable and returns a list of filename_trans_rule_t certainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I think hashtab_map() requires too much knowledge of the internal structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Jim
Stephen Smalley
maintainerQuoted 34 lines · James Carter
Quoted 25 lines · Chris PeBenito
Quoted 23 lines · Stephen Smalley
Quoted 10 lines · Ondrej Mosnacek
These patches are the userspace side of the kernel change posted at [1].
The first patch changes libsepol's internal representation of filename transition rules in a way similar to kernel commit
c3a276111ea2("selinux: optimize storage of filename transitions") [2].The second patch then builds upon that and implements reading and writing of a new binary policy format that uses this representation also in the data layout.
Quoted 7 lines
See individual patches for more details.
NOTE: This series unfortunately breaks the build of setools. Moreover, when an existing build of setools dynamically links against the new libsepol, it segfaults. Sadly, there doesn't seem to be a nice way of handling this, since setools relies on non-public libsepol policydb API/ABI.
I think this has happened before a few years ago when we made a different change to those structures, and required updates on the setools side.
Quoted 6 lines
Quoted 3 lines
Maybe we need to figure out what setools needs to be encapsulated and exported as part of the libsepol public ABI/API, and then stop having it peer into libsepol internals?
I'd be fine with that :)
Quoted 7 lines
I am a little confused. I would consider peering into the libsepol internals to be something like assuming the memory layout of structures and pulling things out based on those assumptions, but setools is just using public header files and functions. It seems to me to be using the public API. Now I think that too much of the internals are being exposed, but I am not sure we can do anything about that now.
The sepol/policydb/*.h files are considered private and are only for static users of libsepol
like checkpolicy. Only the top-level sepol/*.h headers are part of the shared library API/ABI.
Quoted 48 lines
It doesn't seem like it would be too hard to update setools to work with this change, unless there is a requirement that it still work with older versions of libsepol. I am not very familiar with cython and I don't know how to make it work differently depending on what version of libsepol is on the system.
Speaking of versions, since we are actually modifying structs in the header files instead of just adding new things, don't we need change the version of libsepol or something?
Quoted 6 lines
Ultimately SETools does 2 thing for the policy access:
* iterate over the entire policy, reading data out of the various objects
* use linkages between objects in the policy, e.g. get the type/attributes from an AV rule, get types from an attribute, etc. using the stuff likeclass_val_to_structas needed.Quoted 7 lines
So if sufficient functionality to do dispol was exported, that would get close to all that was needed. There are some optimizations I made by digging at the structs a bit more than the above, but that could potentially be dropped if libsepol has sufficient support. See
<https://github.com/SELinuxProject/setools/blob/master/setools/policyrep/selinuxpolicy.pxi#L673>
for the policy loading code.I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internal structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Stephen Smalley
maintainerQuoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internal
Quoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
I don't think we want to directly expose the existing data structures from include/sepol/policydb/*.h (or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions in include/sepol/*.h would be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.
We could also partially mitigate the problem by linking libsepol statically into setools at build time. I suggested this in [1] for Fedora at least, but in general there is the problem that you need to know the exact path to libsepol.a in setup.py [2]. I don't have enough experience with python libraries to know if this can be implemented directly in the upstream setup.py script in a reasonably generic way.
If linked statically, at least it wouldn't segfault after an update to an incompatible libsepol.so. It would still fail to build with the new headers, but at least this turns from a user-visible bug to only packager's & maintainer's problem.
[1] https://bugzilla.redhat.com/show_bug.cgi?id=1826000
[2] https://stackoverflow.com/questions/4597228/how-to-statically-link-a-library-when-compiling-a-python-module-extension/49139257#49139257
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Software Engineer, Security Technologies Red Hat, Inc.
Quoted 45 lines · Ondrej Mosnacek
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.We could also partially mitigate the problem by linking libsepol statically into setools at build time. I suggested this in [1] for Fedora at least, but in general there is the problem that you need to know the exact path to libsepol.a in
setup.py[2]. I don't have enough experience with python libraries to know if this can be implemented directly in the upstreamsetup.pyscript in a reasonably generic way.
Quoted 4 lines
If linked statically, at least it wouldn't segfault after an update to an incompatible libsepol.so. It would still fail to build with the new headers, but at least this turns from a user-visible bug to only packager's & maintainer's problem.
It used to be static and people complained about that. I moved it to dynamic knowing this would be an issue, but an uncommon one.
Signature · 1 line
Chris PeBenito
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.
I agree. The hardest thing with writing the policyrep in setools was stuff like the value_to_datum indirections, type_attr_map, etc. and knowing when to use value vs value-1. An API that has a new set of structs would be ideal.
Unfortunately, since setools policyrep is now written in Cython, we can't simply move the code over to libsepol. My guess is dispol has the most useful building blocks for making a new API.
Signature · 1 line
Chris PeBenito
Quoted 37 lines · Chris PeBenito
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.
Quoted 7 lines
I agree. The hardest thing with writing the policyrep in setools was stuff like the
value_to_datumindirections,type_attr_map, etc. and knowing when to use value vs value-1. An API that has a new set of structs would be ideal.Unfortunately, since setools policyrep is now written in Cython, we can't simply move the code over to libsepol. My guess is dispol has the most useful building blocks for making a new API.
Since you mention dispol... I also had the idea that setools could just use the existing public interface to convert the whole policydb to CIL and simply parse that as a string (this should be pretty straightforward even in pure Python). However, based on my experiments this would likely make setools a lot slower...
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Software Engineer, Security Technologies Red Hat, Inc.
Quoted 46 lines · Ondrej Mosnacek
Quoted 37 lines · Chris PeBenito
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.Quoted 7 lines
I agree. The hardest thing with writing the policyrep in setools was stuff like the
value_to_datumindirections,type_attr_map, etc. and knowing when to use value vs value-1. An API that has a new set of structs would be ideal.Unfortunately, since setools policyrep is now written in Cython, we can't simply move the code over to libsepol. My guess is dispol has the most useful building blocks for making a new API.
Quoted 5 lines
Since you mention dispol... I also had the idea that setools could just use the existing public interface to convert the whole policydb to CIL and simply parse that as a string (this should be pretty straightforward even in pure Python). However, based on my experiments this would likely make setools a lot slower...
This is an interesting idea. I'm not familiar with the CIL API; secilc.c looks like it uses public API. Can I use the existing CIL library functions to parse it, or does the resultant db lack public accessor functions?
Signature · 1 line
Chris PeBenito
Quoted 54 lines · Chris PeBenito
Quoted 46 lines · Ondrej Mosnacek
Quoted 37 lines · Chris PeBenito
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.Quoted 7 lines
I agree. The hardest thing with writing the policyrep in setools was stuff like the
value_to_datumindirections,type_attr_map, etc. and knowing when to use value vs value-1. An API that has a new set of structs would be ideal.Unfortunately, since setools policyrep is now written in Cython, we can't simply move the code over to libsepol. My guess is dispol has the most useful building blocks for making a new API.
Quoted 5 lines
Since you mention dispol... I also had the idea that setools could just use the existing public interface to convert the whole policydb to CIL and simply parse that as a string (this should be pretty straightforward even in pure Python). However, based on my experiments this would likely make setools a lot slower...
Quoted 4 lines
This is an interesting idea. I'm not familiar with the CIL API;
secilc.clooks like it uses public API. Can I use the existing CIL library functions to parse it, or does the resultant db lack public accessor functions?
The resultant db does, in fact, lack public assessor functions. They could be created, but since there is already a way to convert the cil_db to a policydb, the general solution would be to create the functions for the policydb.
Jim
Quoted 53 lines · Chris PeBenito
Quoted 46 lines · Ondrej Mosnacek
Quoted 37 lines · Chris PeBenito
Quoted 26 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.Quoted 7 lines
I agree. The hardest thing with writing the policyrep in setools was stuff like the
value_to_datumindirections,type_attr_map, etc. and knowing when to use value vs value-1. An API that has a new set of structs would be ideal.Unfortunately, since setools policyrep is now written in Cython, we can't simply move the code over to libsepol. My guess is dispol has the most useful building blocks for making a new API.
Quoted 5 lines
Since you mention dispol... I also had the idea that setools could just use the existing public interface to convert the whole policydb to CIL and simply parse that as a string (this should be pretty straightforward even in pure Python). However, based on my experiments this would likely make setools a lot slower...
Quoted 3 lines
This is an interesting idea. I'm not familiar with the CIL API;
secilc.clooks like it uses public API. Can I use the existing CIL library functions to parse it, or does the resultant db lack public accessor functions?
What I had in mind was actually to just use sepol_kernel_policydb_to_cil() to dump the textual CIL into a temporary file (maybe using tmpfile(3)), and then parse the contents in Python. The CIL format is really easy to parse (especially in Python) so a lack of existing functions for that wouldn't be much of an issue. Yes, this would be a little dirty, but you'd avoid the trouble of maintaining a stable binary interface between libsepol and setools.
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Software Engineer, Security Technologies Red Hat, Inc.
Quoted 27 lines · Stephen Smalley
Quoted 16 lines · James Carter
I think the fact that the CIL, kernel to CIL, kernel to conf, and module to CIL code is all in libsepol speaks to the fact of how tightly integrated they are to the rest of libsepol. One argument that could be made is that the policyrep stuff in setools really belongs in libsepol.
Thinking about how libsepol could be encapsulated leads me to a couple of possibilities. One way would be functions that could return lists of rules. The policy module code gives us
avrule_t,role_trans_rule_t,role_allow_t,filename_trans_rule_t,range_trans_rule_t, and others. Those structures are probably unlikely to change and, at least in this case, creating a function that walks thefilename_transhashtable and returns a list offilename_trans_rule_tcertainly seems like it wouldn't be too hard. Another possible way to encapsulate would be create a way to walk the various hashtables element by element (I thinkhashtab_map()requires too much knowledge of the internalQuoted 8 lines
structures), returning an opaque structure to track where you are in the hashtable and functions that allow you to get each part of the rule being stored. There are other ways that it could be done, but I could rewrite kernel to and module to stuff with either of those. CIL itself would require some functions to insert rules into the policydb which probably wouldn't be too hard. None of this would be too hard, but it would take some time. The real question is would it really be valuable?
Quoted 9 lines
I don't think we want to directly expose the existing data structures from
include/sepol/policydb/*.h(or at least not without a careful audit) since those are often tightly coupled to policy compiler internals and/or the kernel or module policy formats. Creating an abstraction for each with a proper API in new definitions ininclude/sepol/*.hwould be preferable albeit more work. There was a proposal a long time ago from the setools developers to create an iterator interface and accessor functions for each data type, see
https://lore.kernel.org/selinux/[email protected]/.
I am so used to everything just using the stuff in include/sepol/policydb/ that I forget that it exists. There are a number of iterators there (for users, ports, nodes, etc), so it seems like creating an iterator and other functions for filename transition rules and converting setools to use that would be the first step. Over time other iterators can be created and setools converted to use those as well.
Jim