libsepol: support wildcard network interface names
PATCH v2 1/3 libsepol: support wildcard network interface names
Add support for wildcard matching of network interface names. This is useful for auto-generated interfaces, for example podman creates network interfaces for containers with the naming scheme podman0, podman1, podman2, ...
To maintain backward compatibility guard this feature with a new policy capability 'netif_wildcard'.
Netifcon definitions are compared against in the order given by the policy, so userspace tools should sort them in a reasonable order.
--- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/polcaps.c | 1 + 2 files changed, 2 insertions(+)
libsepol/include/sepol/policydb/polcaps.h +1 -0
@@ -17,6 +17,7 @@ enum {POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT ,POLICYDB_CAP_NETLINK_XPERM , +POLICYDB_CAP_NETIF_WILDCARD , __POLICYDB_CAP_MAX };#define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
libsepol/src/polcaps.c +1 -0
@@ -16,6 +16,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", + [POLICYDB_CAP_NETIF_WILDCARD] = "netif_wildcard", };int sepol_polcap_getnum (const char *name)
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
PATCH v2 2/3 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
Re: [PATCH v2 2/3] libsepol: Allow multiple policycap statements
Quoted 24 lines · Inseob Kim
Same policycap statements don't conflict each other, so it's fine to allow multiple policycap statements to exist.
Signed-off-by Inseob Kim <[email protected]>--- libsepol/cil/src/cil_build_ast.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)libsepol/cil/src/cil_build_ast.c +3
-0@@ -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 ; }
libsepol/cil/src/cil_build_ast.c int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_s · line 214
Quoted 8 lines
libsepol/cil/src/cil_build_ast.c +1
-1@@ -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; }
Take a look at how role, type, and optional handle this. You need to pass SEPOL_EEXIST back, so the duplicate is destroyed. We allow duplicates, but only want the first one.
They all follow this pattern (from cil_gen_role())
rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)role,
(hashtab_key_t)key, CIL_SYM_ROLES, CIL_ROLE);
if (rc != SEPOL_OK) {
if (rc == SEPOL_EEXIST) {
cil_destroy_role(role);
role = NULL;
} else {
goto exit;
}
}I am still looking at the last patch.
Thanks,
Jim
Quoted 4 lines
--
2.49.0.1238.gf8c92423fb-goog
PATCH v2 3/3 libsepol: Support genfs_seclabel_wildcard
In reply to Inseob KimThis 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.
Changes since v1:
- Sync the order of polcaps with the kernel
- Cast to int for printf precision arguments
--- 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
@@ -18,6 +18,7 @@ enum {POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT ,POLICYDB_CAP_NETLINK_XPERM ,POLICYDB_CAP_NETIF_WILDCARD , +POLICYDB_CAP_GENFS_SECLABEL_WILDCARD , __POLICYDB_CAP_MAX };#define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
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, (int )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, (int )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, (int )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, (int )name_len, name, ctx); }free (ctx);if (rc !=0 ) {
libsepol/src/polcaps.c +1 -0
@@ -17,6 +17,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", [POLICYDB_CAP_NETIF_WILDCARD] = "netif_wildcard", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", };int sepol_polcap_getnum (const char *name)
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
Re: [PATCH v2 3/3] libsepol: Support genfs_seclabel_wildcard
Quoted 67 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]>Changes since v1:
- Sync the order of polcaps with the kernel
- Cast to int for printf precision arguments--- 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 +1
-0@@ -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)
libsepol/cil/src/cil_binary.c int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons) · line 3783
Quoted 10 lines
libsepol/cil/src/cil_binary.c +7
-1ocon_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 ); + }
You should use cil_malloc() here. It will take care of checking for NULL and exiting with an error message for you. Everything else looks good to me.
Thanks,
Jim
Quoted 117 lines
libsepol/cil/src/cil_binary.c +6
-0+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@@ -18,6 +18,7 @@ enum {POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT ,POLICYDB_CAP_NETLINK_XPERM ,POLICYDB_CAP_NETIF_WILDCARD , +POLICYDB_CAP_GENFS_SECLABEL_WILDCARD , __POLICYDB_CAP_MAX };#define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)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, (int )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, (int )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, (int )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, (int )name_len, name, ctx); }free (ctx);if (rc !=0 ) {libsepol/src/polcaps.c +1
-1@@ -17,6 +17,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", [POLICYDB_CAP_NETIF_WILDCARD] = "netif_wildcard", + [POLICYDB_CAP_GENFS_SECLABEL_WILDCARD] = "genfs_seclabel_wildcard", };int sepol_polcap_getnum (const char *name) --2.49.0.1238.gf8c92423fb-goog
Quoted 6 lines · Inseob Kim
Add support for wildcard matching of network interface names. This is useful for auto-generated interfaces, for example podman creates network interfaces for containers with the naming scheme podman0, podman1, podman2, ...
Quoted 7 lines
To maintain backward compatibility guard this feature with a new policy capability '
netif_wildcard'.Netifcon definitions are compared against in the order given by the policy, so userspace tools should sort them in a reasonable order.
Signed-off-by Inseob Kim <[email protected]>
This patch won't be needed now since Christian has posted the netif wildcard patches. (Of course, you didn't know he was going to post them when you sent this.)
Thanks,
Jim
Quoted 33 lines
--- libsepol/include/sepol/policydb/polcaps.h | 1 + libsepol/src/polcaps.c | 1 + 2 files changed, 2 insertions(+)libsepol/include/sepol/policydb/polcaps.h +1
-0@@ -17,6 +17,7 @@ enum {POLICYDB_CAP_IOCTL_SKIP_CLOEXEC ,POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT ,POLICYDB_CAP_NETLINK_XPERM , +POLICYDB_CAP_NETIF_WILDCARD , __POLICYDB_CAP_MAX };#define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)libsepol/src/polcaps.c +1
-1@@ -16,6 +16,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = { [POLICYDB_CAP_IOCTL_SKIP_CLOEXEC] = "ioctl_skip_cloexec", [POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT] = "userspace_initial_context", [POLICYDB_CAP_NETLINK_XPERM] = "netlink_xperm", + [POLICYDB_CAP_NETIF_WILDCARD] = "netif_wildcard", };int sepol_polcap_getnum (const char *name) --2.49.0.1238.gf8c92423fb-goog