libsepol: Allow multiple policycap statements
PATCH 1/2 libsepol: Allow multiple policycap statements
Same policycap statements don't conflict each other, so it's fine to allow multiple policycap statements to exist.
--- libsepol/cil/src/cil_build_ast.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +4 -1
@@ -145,6 +145,9 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, encase CIL_OPTIONAL :return CIL_TRUE ;break ; +case CIL_POLICYCAP : +return CIL_TRUE ; +break ;default :break ; }
@@ -205,7 +208,7 @@ int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_s ast_node->flavor = nflavor; rc =cil_add_decl_to_symtab (db, symtab, key, datum, ast_node); -if (rc !=SEPOL_OK ) { +if (rc !=SEPOL_OK && rc !=SEPOL_EEXIST ) { goto exit; }
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
PATCH 2/2 libsepol: Support genfs_seclabel_wildcard
This adds genfs_seclabel_wildcard support to libsepol, which makes genfs paths wildcard-matched. To support backward compatibility and to keep semantics of genfscon statements, an asterisk '*' is added to paths of genfscon statements when compiling CIL/conf files having policycap genfs_seclabel_wildcard.
--- checkpolicy/policy_define.c | 14 ++++++++++++++ libsepol/cil/src/cil_binary.c | 15 ++++++++++++++- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/kernel_to_cil.c | 20 ++++++++++++++++---- libsepol/src/kernel_to_conf.c | 20 ++++++++++++++++---- libsepol/src/polcaps.c | 1 + 6 files changed, 62 insertions(+), 9 deletions(-)
checkpolicy/policy_define.c +14 -0
@@ -5967,6 +5967,7 @@ static int define_genfs_context_helper(char *fstype, int has_type)char *type =NULL ;const char *sclass;size_t len, len2; +int wildcard =ebitmap_get_bit (&policydbp->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );if (policydbp->target_platform !=SEPOL_TARGET_SELINUX ) {yyerror ("genfs not supported for target" );
@@ -6017,6 +6018,19 @@ static int define_genfs_context_helper(char *fstype, int has_type) newc->u.name = (char *)queue_remove (id_queue);if (!newc->u.name) goto fail; + +if (wildcard) { +size_t name_len =strlen (newc->u.name); + newc->u.name =realloc (newc->u.name, name_len +2 ); +if (newc->u.name ==NULL ) { +yyerror ("out of memory" ); +return -1 ; + } + + newc->u.name[name_len] ='*' ; + newc->u.name[name_len +1 ] ='\0' ; + } +if (has_type) { type = (char *)queue_remove (id_queue);if (!type)
libsepol/cil/src/cil_binary.c +14 -1
@@ -3749,6 +3749,7 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)uint32_t i =0 ;genfs_t *genfs_tail =NULL ;ocontext_t *ocon_tail =NULL ; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );for (i =0 ; i < genfscons->count; i++) {struct cil_genfscon *cil_genfscon = genfscons->array[i];
@@ -3773,7 +3774,19 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) ocon_tail = new_ocon; - new_ocon->u.name =cil_strdup (cil_genfscon->path_str); +if (wildcard) { +size_t name_len =strlen (cil_genfscon->path_str); + new_ocon->u.name =malloc (name_len +2 ); +if (new_ocon->u.name ==NULL ) { +cil_log (CIL_ERR ,"Failed to allocate memory\n" ); +exit (1 ); + } +memcpy (new_ocon->u.name, cil_genfscon->path_str, name_len); + new_ocon->u.name[name_len] ='*' ; + new_ocon->u.name[name_len +1 ] ='\0' ; + }else { + new_ocon->u.name =cil_strdup (cil_genfscon->path_str); + }if (cil_genfscon->file_type !=CIL_FILECON_ANY ) {class_datum_t *class_datum;
libsepol/include/sepol/policydb/polcaps.h +1 -0
@@ -16,6 +16,7 @@ enum {POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS ,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT , +POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ,POLICYDB_CAP_NETLINK_XPERM , __POLICYDB_CAP_MAX };
libsepol/src/kernel_to_cil.c +16 -4
@@ -2708,6 +2708,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {
@@ -2749,12 +2751,22 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s %s)" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s %s)" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s)" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {
libsepol/src/kernel_to_conf.c +16 -4
@@ -2556,6 +2556,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {
@@ -2597,12 +2599,22 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s %s" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s %s" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {
libsepol/src/polcaps.c +1 -0
@@ -15,6 +15,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS] = "genfs_seclabel_symlinks", [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", };
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
Re: [PATCH 2/2] libsepol: Support genfs_seclabel_wildcard
Quoted 87 lines · Inseob Kim
This adds
genfs_seclabel_wildcardsupport to libsepol, which makes genfs paths wildcard-matched. To support backward compatibility and to keep semantics of genfscon statements, an asterisk '*' is added to paths of genfscon statements when compiling CIL/conf files having policycapgenfs_seclabel_wildcard.Signed-off-by Inseob Kim <[email protected]>--- checkpolicy/policy_define.c | 14 ++++++++++++++ libsepol/cil/src/cil_binary.c | 15 ++++++++++++++- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/kernel_to_cil.c | 20 ++++++++++++++++---- libsepol/src/kernel_to_conf.c | 20 ++++++++++++++++---- libsepol/src/polcaps.c | 1 + 6 files changed, 62 insertions(+), 9 deletions(-)checkpolicy/policy_define.c +14
-0@@ -5967,6 +5967,7 @@ static int define_genfs_context_helper(char *fstype, int has_type)char *type =NULL ;const char *sclass;size_t len, len2; +int wildcard =ebitmap_get_bit (&policydbp->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );if (policydbp->target_platform !=SEPOL_TARGET_SELINUX ) {yyerror ("genfs not supported for target" );@@ -6017,6 +6018,19 @@ static int define_genfs_context_helper(char *fstype, int has_type) newc->u.name = (char *)queue_remove (id_queue);if (!newc->u.name) goto fail; + +if (wildcard) { +size_t name_len =strlen (newc->u.name); + newc->u.name =realloc (newc->u.name, name_len +2 ); +if (newc->u.name ==NULL ) { +yyerror ("out of memory" ); +return -1 ; + } + + newc->u.name[name_len] ='*' ; + newc->u.name[name_len +1 ] ='\0' ; + } +if (has_type) { type = (char *)queue_remove (id_queue);if (!type)libsepol/cil/src/cil_binary.c +14
-1@@ -3749,6 +3749,7 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)uint32_t i =0 ;genfs_t *genfs_tail =NULL ;ocontext_t *ocon_tail =NULL ; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );for (i =0 ; i < genfscons->count; i++) {struct cil_genfscon *cil_genfscon = genfscons->array[i];@@ -3773,7 +3774,19 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) ocon_tail = new_ocon; - new_ocon->u.name =cil_strdup (cil_genfscon->path_str); +if (wildcard) { +size_t name_len =strlen (cil_genfscon->path_str); + new_ocon->u.name =malloc (name_len +2 ); +if (new_ocon->u.name ==NULL ) { +cil_log (CIL_ERR ,"Failed to allocate memory\n" ); +exit (1 ); + } +memcpy (new_ocon->u.name, cil_genfscon->path_str, name_len); + new_ocon->u.name[name_len] ='*' ; + new_ocon->u.name[name_len +1 ] ='\0' ; + }else { + new_ocon->u.name =cil_strdup (cil_genfscon->path_str); + }if (cil_genfscon->file_type !=CIL_FILECON_ANY ) {class_datum_t *class_datum;libsepol/include/sepol/policydb/polcaps.h +0
-0
libsepol/include/sepol/policydb/polcaps.h enum { · line 22
Quoted 7 lines
libsepol/include/sepol/policydb/polcaps.h +1
-0@@ -16,6 +16,7 @@ enum {POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS ,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT , +POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ,POLICYDB_CAP_NETLINK_XPERM , __POLICYDB_CAP_MAX };
The order has to be the same as in the kernel. The number, rather than the name, is stored in the binary policy file.
Thanks,
Jim
Quoted 96 lines
libsepol/src/kernel_to_cil.c +16
-4@@ -2708,6 +2708,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2749,12 +2751,22 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s %s)" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s %s)" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s)" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/kernel_to_conf.c +16
-4@@ -2556,6 +2556,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2597,12 +2599,22 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s %s" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s %s" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/polcaps.c +1
-1@@ -15,6 +15,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS] = "genfs_seclabel_symlinks", [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", }; --2.49.0.1238.gf8c92423fb-goog
Re: [PATCH 2/2] libsepol: Support genfs_seclabel_wildcard
Quoted 93 lines · James Carter
Quoted 91 lines · Inseob Kim
This adds
genfs_seclabel_wildcardsupport to libsepol, which makes genfs paths wildcard-matched. To support backward compatibility and to keep semantics of genfscon statements, an asterisk '*' is added to paths of genfscon statements when compiling CIL/conf files having policycapgenfs_seclabel_wildcard.Signed-off-by Inseob Kim <[email protected]>--- checkpolicy/policy_define.c | 14 ++++++++++++++ libsepol/cil/src/cil_binary.c | 15 ++++++++++++++- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/kernel_to_cil.c | 20 ++++++++++++++++---- libsepol/src/kernel_to_conf.c | 20 ++++++++++++++++---- libsepol/src/polcaps.c | 1 + 6 files changed, 62 insertions(+), 9 deletions(-)checkpolicy/policy_define.c +14
-0@@ -5967,6 +5967,7 @@ static int define_genfs_context_helper(char *fstype, int has_type)char *type =NULL ;const char *sclass;size_t len, len2; +int wildcard =ebitmap_get_bit (&policydbp->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );if (policydbp->target_platform !=SEPOL_TARGET_SELINUX ) {yyerror ("genfs not supported for target" );@@ -6017,6 +6018,19 @@ static int define_genfs_context_helper(char *fstype, int has_type) newc->u.name = (char *)queue_remove (id_queue);if (!newc->u.name) goto fail; + +if (wildcard) { +size_t name_len =strlen (newc->u.name); + newc->u.name =realloc (newc->u.name, name_len +2 ); +if (newc->u.name ==NULL ) { +yyerror ("out of memory" ); +return -1 ; + } + + newc->u.name[name_len] ='*' ; + newc->u.name[name_len +1 ] ='\0' ; + } +if (has_type) { type = (char *)queue_remove (id_queue);if (!type)libsepol/cil/src/cil_binary.c +14
-1@@ -3749,6 +3749,7 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)uint32_t i =0 ;genfs_t *genfs_tail =NULL ;ocontext_t *ocon_tail =NULL ; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );for (i =0 ; i < genfscons->count; i++) {struct cil_genfscon *cil_genfscon = genfscons->array[i];@@ -3773,7 +3774,19 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) ocon_tail = new_ocon; - new_ocon->u.name =cil_strdup (cil_genfscon->path_str); +if (wildcard) { +size_t name_len =strlen (cil_genfscon->path_str); + new_ocon->u.name =malloc (name_len +2 ); +if (new_ocon->u.name ==NULL ) { +cil_log (CIL_ERR ,"Failed to allocate memory\n" ); +exit (1 ); + } +memcpy (new_ocon->u.name, cil_genfscon->path_str, name_len); + new_ocon->u.name[name_len] ='*' ; + new_ocon->u.name[name_len +1 ] ='\0' ; + }else { + new_ocon->u.name =cil_strdup (cil_genfscon->path_str); + }if (cil_genfscon->file_type !=CIL_FILECON_ANY ) {class_datum_t *class_datum;libsepol/include/sepol/policydb/polcaps.h +1
-0@@ -16,6 +16,7 @@ enum {POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS ,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT , + POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
Quoted 8 lines
libsepol/include/sepol/policydb/polcaps.h enum { · line 22
Quoted 3 lines
libsepol/include/sepol/policydb/polcaps.h +0
-0POLICYDB_CAP_NETLINK_XPERM , __POLICYDB_CAP_MAX };The order has to be the same as in the kernel. The number, rather than the name, is stored in the binary policy file.
Thanks,
Jim
ok, I checked https://github.com/torvalds/linux/blob/master/security/selinux/include/policycap.h
and currently it is
enum {POLICYDB_CAP_NETPEER,POLICYDB_CAP_OPENPERM,POLICYDB_CAP_EXTSOCKCLASS,POLICYDB_CAP_ALWAYSNETWORK,POLICYDB_CAP_CGROUPSECLABEL,POLICYDB_CAP_NNP_NOSUID_TRANSITION,POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,POLICYDB_CAP_NETLINK_XPERM,POLICYDB_CAP_NETIF_WILDCARD,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,__POLICYDB_CAP_MAX
};
It seems like POLICYDB_CAP_NETIF_WILDCARD is missing in libsepol. So I need another commit adding POLICYDB_CAP_NETIF_WILDCARD to libsepol, right?
Quoted 97 lines
Quoted 97 lines
libsepol/src/kernel_to_cil.c +16
-4@@ -2708,6 +2708,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2749,12 +2751,22 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s %s)" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s %s)" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s)" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/kernel_to_conf.c +16
-4@@ -2556,6 +2556,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2597,12 +2599,22 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s %s" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s %s" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/polcaps.c +1
-1@@ -15,6 +15,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS] = "genfs_seclabel_symlinks", [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", }; --2.49.0.1238.gf8c92423fb-goog
Signature · 1 line
Inseob Kim | Software Engineer | [email protected]
Re: [PATCH 2/2] libsepol: Support genfs_seclabel_wildcard
Quoted 123 lines · Inseob Kim
Quoted 93 lines · James Carter
Quoted 91 lines · Inseob Kim
This adds
genfs_seclabel_wildcardsupport to libsepol, which makes genfs paths wildcard-matched. To support backward compatibility and to keep semantics of genfscon statements, an asterisk '*' is added to paths of genfscon statements when compiling CIL/conf files having policycapgenfs_seclabel_wildcard.Signed-off-by Inseob Kim <[email protected]>--- checkpolicy/policy_define.c | 14 ++++++++++++++ libsepol/cil/src/cil_binary.c | 15 ++++++++++++++- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/kernel_to_cil.c | 20 ++++++++++++++++---- libsepol/src/kernel_to_conf.c | 20 ++++++++++++++++---- libsepol/src/polcaps.c | 1 + 6 files changed, 62 insertions(+), 9 deletions(-)checkpolicy/policy_define.c +14
-0@@ -5967,6 +5967,7 @@ static int define_genfs_context_helper(char *fstype, int has_type)char *type =NULL ;const char *sclass;size_t len, len2; +int wildcard =ebitmap_get_bit (&policydbp->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );if (policydbp->target_platform !=SEPOL_TARGET_SELINUX ) {yyerror ("genfs not supported for target" );@@ -6017,6 +6018,19 @@ static int define_genfs_context_helper(char *fstype, int has_type) newc->u.name = (char *)queue_remove (id_queue);if (!newc->u.name) goto fail; + +if (wildcard) { +size_t name_len =strlen (newc->u.name); + newc->u.name =realloc (newc->u.name, name_len +2 ); +if (newc->u.name ==NULL ) { +yyerror ("out of memory" ); +return -1 ; + } + + newc->u.name[name_len] ='*' ; + newc->u.name[name_len +1 ] ='\0' ; + } +if (has_type) { type = (char *)queue_remove (id_queue);if (!type)libsepol/cil/src/cil_binary.c +14
-1@@ -3749,6 +3749,7 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)uint32_t i =0 ;genfs_t *genfs_tail =NULL ;ocontext_t *ocon_tail =NULL ; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );for (i =0 ; i < genfscons->count; i++) {struct cil_genfscon *cil_genfscon = genfscons->array[i];@@ -3773,7 +3774,19 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) ocon_tail = new_ocon; - new_ocon->u.name =cil_strdup (cil_genfscon->path_str); +if (wildcard) { +size_t name_len =strlen (cil_genfscon->path_str); + new_ocon->u.name =malloc (name_len +2 ); +if (new_ocon->u.name ==NULL ) { +cil_log (CIL_ERR ,"Failed to allocate memory\n" ); +exit (1 ); + } +memcpy (new_ocon->u.name, cil_genfscon->path_str, name_len); + new_ocon->u.name[name_len] ='*' ; + new_ocon->u.name[name_len +1 ] ='\0' ; + }else { + new_ocon->u.name =cil_strdup (cil_genfscon->path_str); + }if (cil_genfscon->file_type !=CIL_FILECON_ANY ) {class_datum_t *class_datum;libsepol/include/sepol/policydb/polcaps.h +1
-0@@ -16,6 +16,7 @@ enum {POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS ,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT , + POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,Quoted 8 lines
libsepol/include/sepol/policydb/polcaps.h enum { · line 22
Quoted 3 lines
libsepol/include/sepol/policydb/polcaps.h +0
-0POLICYDB_CAP_NETLINK_XPERM , __POLICYDB_CAP_MAX };The order has to be the same as in the kernel. The number, rather than the name, is stored in the binary policy file.
Thanks,
Jimok, I checked https://github.com/torvalds/linux/blob/master/security/selinux/include/policycap.h
and currently it isenum {
POLICYDB_CAP_NETPEER,POLICYDB_CAP_OPENPERM,POLICYDB_CAP_EXTSOCKCLASS,POLICYDB_CAP_ALWAYSNETWORK,POLICYDB_CAP_CGROUPSECLABEL,POLICYDB_CAP_NNP_NOSUID_TRANSITION,POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,POLICYDB_CAP_NETLINK_XPERM,POLICYDB_CAP_NETIF_WILDCARD,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,__POLICYDB_CAP_MAX
};
Quoted 4 lines
It seems like
POLICYDB_CAP_NETIF_WILDCARDis missing in libsepol. So I need another commit addingPOLICYDB_CAP_NETIF_WILDCARDto libsepol, right?
Yes, I was reacting to the fact that you were not adding it to the end of the list, but you are right that one needs to be added as well. The order in libsepol has to match the kernel.
Thanks,
Jim
Quoted 102 lines
Quoted 93 lines
Quoted 93 lines
libsepol/src/kernel_to_cil.c +16
-4@@ -2708,6 +2708,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2749,12 +2751,22 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s %s)" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s %s)" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s)" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/kernel_to_conf.c +16
-4@@ -2556,6 +2556,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2597,12 +2599,22 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s %s" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s %s" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/polcaps.c +1
-0@@ -15,6 +15,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS] = "genfs_seclabel_symlinks", [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", };Quoted 4 lines
Quoted 4 lines
--
2.49.0.1238.gf8c92423fb-goog--
Inseob Kim | Software Engineer | [email protected]
Re: [PATCH 2/2] libsepol: Support genfs_seclabel_wildcard
+Christian ([email protected]) for your awareness
Quoted 130 lines · James Carter
Quoted 123 lines · Inseob Kim
Quoted 93 lines · James Carter
Quoted 91 lines · Inseob Kim
This adds
genfs_seclabel_wildcardsupport to libsepol, which makes genfs paths wildcard-matched. To support backward compatibility and to keep semantics of genfscon statements, an asterisk '*' is added to paths of genfscon statements when compiling CIL/conf files having policycapgenfs_seclabel_wildcard.Signed-off-by Inseob Kim <[email protected]>--- checkpolicy/policy_define.c | 14 ++++++++++++++ libsepol/cil/src/cil_binary.c | 15 ++++++++++++++- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/kernel_to_cil.c | 20 ++++++++++++++++---- libsepol/src/kernel_to_conf.c | 20 ++++++++++++++++---- libsepol/src/polcaps.c | 1 + 6 files changed, 62 insertions(+), 9 deletions(-)checkpolicy/policy_define.c +14
-0@@ -5967,6 +5967,7 @@ static int define_genfs_context_helper(char *fstype, int has_type)char *type =NULL ;const char *sclass;size_t len, len2; +int wildcard =ebitmap_get_bit (&policydbp->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );if (policydbp->target_platform !=SEPOL_TARGET_SELINUX ) {yyerror ("genfs not supported for target" );@@ -6017,6 +6018,19 @@ static int define_genfs_context_helper(char *fstype, int has_type) newc->u.name = (char *)queue_remove (id_queue);if (!newc->u.name) goto fail; + +if (wildcard) { +size_t name_len =strlen (newc->u.name); + newc->u.name =realloc (newc->u.name, name_len +2 ); +if (newc->u.name ==NULL ) { +yyerror ("out of memory" ); +return -1 ; + } + + newc->u.name[name_len] ='*' ; + newc->u.name[name_len +1 ] ='\0' ; + } +if (has_type) { type = (char *)queue_remove (id_queue);if (!type)libsepol/cil/src/cil_binary.c +14
-1@@ -3749,6 +3749,7 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)uint32_t i =0 ;genfs_t *genfs_tail =NULL ;ocontext_t *ocon_tail =NULL ; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );for (i =0 ; i < genfscons->count; i++) {struct cil_genfscon *cil_genfscon = genfscons->array[i];@@ -3773,7 +3774,19 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) ocon_tail = new_ocon; - new_ocon->u.name =cil_strdup (cil_genfscon->path_str); +if (wildcard) { +size_t name_len =strlen (cil_genfscon->path_str); + new_ocon->u.name =malloc (name_len +2 ); +if (new_ocon->u.name ==NULL ) { +cil_log (CIL_ERR ,"Failed to allocate memory\n" ); +exit (1 ); + } +memcpy (new_ocon->u.name, cil_genfscon->path_str, name_len); + new_ocon->u.name[name_len] ='*' ; + new_ocon->u.name[name_len +1 ] ='\0' ; + }else { + new_ocon->u.name =cil_strdup (cil_genfscon->path_str); + }if (cil_genfscon->file_type !=CIL_FILECON_ANY ) {class_datum_t *class_datum;libsepol/include/sepol/policydb/polcaps.h +1
-0@@ -16,6 +16,7 @@ enum {POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS ,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT , + POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,Quoted 8 lines
libsepol/include/sepol/policydb/polcaps.h enum { · line 22
Quoted 3 lines
libsepol/include/sepol/policydb/polcaps.h +0
-0POLICYDB_CAP_NETLINK_XPERM , __POLICYDB_CAP_MAX };The order has to be the same as in the kernel. The number, rather than the name, is stored in the binary policy file.
Thanks,
Jimok, I checked https://github.com/torvalds/linux/blob/master/security/selinux/include/policycap.h
and currently it isenum {
POLICYDB_CAP_NETPEER,POLICYDB_CAP_OPENPERM,POLICYDB_CAP_EXTSOCKCLASS,POLICYDB_CAP_ALWAYSNETWORK,POLICYDB_CAP_CGROUPSECLABEL,POLICYDB_CAP_NNP_NOSUID_TRANSITION,POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS,POLICYDB_CAP_IOCTL_SKIP_CLOEXEC,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,POLICYDB_CAP_NETLINK_XPERM,POLICYDB_CAP_NETIF_WILDCARD,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,__POLICYDB_CAP_MAX
};Quoted 4 lines
It seems like
POLICYDB_CAP_NETIF_WILDCARDis missing in libsepol. So I need another commit addingPOLICYDB_CAP_NETIF_WILDCARDto libsepol, right?
Quoted 6 lines
Yes, I was reacting to the fact that you were not adding it to the end of the list, but you are right that one needs to be added as well. The order in libsepol has to match the kernel.
Thanks,
Jim
We will expose POLICYDB_CAP_NETIF_WILDCARD to the userspace which you implemented in the kernel. For genfs, we are keeping backward compatibility by the userspace. You might want to try the same thing for the network interface. In that case, we might want to replace POLICYDB_CAP_NETIF_WILDCARD with some RESERVED name or something
Quoted 102 lines
Quoted 102 lines
Quoted 93 lines
Quoted 93 lines
libsepol/src/kernel_to_cil.c +16
-4@@ -2708,6 +2708,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2749,12 +2751,22 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s %s)" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s %s)" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"(genfscon %s \"%s\" %s)" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"(genfscon %s \"%.*s\" %s)" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/kernel_to_conf.c +16
-4@@ -2556,6 +2556,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)uint32_t sclass;const char *file_type;int rc; +int wildcard =ebitmap_get_bit (&pdb->policycaps,POLICYDB_CAP_GENFS_SECLABEL_WILDCARD ); +size_t name_len; rc =strs_init (&strs,32 );if (rc !=0 ) {@@ -2597,12 +2599,22 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb) goto exit; } + name_len =strlen (name); +if (wildcard) { +if (name_len ==0 || name[name_len -1 ] !='*' ) { +ERR (NULL ,"genfscon path must end with '*' when genfs_seclabel_wildcard" ); + rc =-1 ; + goto exit; + } + --name_len; + } +if (file_type) { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s %s" , - fstype, name, file_type, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s %s" , + fstype, name_len, name, file_type, ctx); }else { - rc =strs_create_and_add (strs,"genfscon %s \"%s\" %s" , - fstype, name, ctx); + rc =strs_create_and_add (strs,"genfscon %s \"%.*s\" %s" , + fstype, name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/polcaps.c +1
-0@@ -15,6 +15,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS] = "genfs_seclabel_symlinks", [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", };Quoted 4 lines
Quoted 4 lines
--
2.49.0.1238.gf8c92423fb-goog--
Inseob Kim | Software Engineer | [email protected]