libsepol: validate portcon protocol values
Stephen Smalley
maintainerPATCH 1/4 libsepol: validate portcon protocol values
validate_ocontexts() sanity checks the port range but does not check the protocol field. kernel_to_conf, kernel_to_cil, and module_to_cil all reject a portcon whose protocol is not one of tcp, udp, dccp, or sctp, so a binary policy with any other value passes read+validate but cannot be decompiled.
checkpolicy and secilc only ever emit these four protocols and there is no source syntax for a numeric protocol, so bound the protocol at validate time to the same set. The kernel additionally bounds protocol to U8_MAX at load time, which this covers.
--- libsepol/src/policydb_validate.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
libsepol/src/policydb_validate.c +17 -0
@@ -1,5 +1,13 @@#include <ctype.h> +#include <netinet/in.h> +#ifndef IPPROTO_DCCP +#define IPPROTO_DCCP 33 +#endif +#ifndef IPPROTO_SCTP +#define IPPROTO_SCTP 132 +#endif +#include <sepol/policydb/conditional.h> #include <sepol/policydb/ebitmap.h> #include <sepol/policydb/polcaps.h>
@@ -1586,6 +1594,15 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p,if (octx->u.port.low_port > octx->u.port.high_port) goto bad; +switch (octx->u.port.protocol) { +case IPPROTO_TCP : +case IPPROTO_UDP : +case IPPROTO_DCCP : +case IPPROTO_SCTP : +break ; +default : + goto bad; + }break ;case OCON_FSUSE :switch (octx->v.behavior) {
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 2/4 libsepol: validate genfs_seclabel_wildcard paths
When the genfs_seclabel_wildcard policy capability is enabled in a policy, kernel_to_conf, kernel_to_cil, and module_to_cil all require every genfscon path to end in '*' and reject it otherwise. Check and reject such genfscon paths up front during policydb_validate() so that no validated policy can reach the decompilers with an invalid path and fail there. While we are here, also validate that genfscon paths are non-empty since that was dropped when I removed the prior string field validation since paths can contain ctrl characters.
--- libsepol/src/policydb_validate.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
libsepol/src/policydb_validate.c +10 -0
@@ -1680,6 +1680,9 @@ static int validate_genfs(sepol_handle_t *handle, const policydb_t *p, {const genfs_t *genfs;const ocontext_t *octx; +size_t len; +int wildcard =ebitmap_get_bit (&p->policycaps, +POLICYDB_CAP_GENFS_SECLABEL_WILDCARD );for (genfs = p->genfs; genfs; genfs = genfs->next) {for (octx = genfs->head; octx; octx = octx->next) {
@@ -1693,6 +1696,13 @@ static int validate_genfs(sepol_handle_t *handle, const policydb_t *p,if (validate_genfs_class (octx->v.sclass, p)) goto bad; } +if (!octx->u.name || !octx->u.name[0 ]) + goto bad; +if (wildcard) { + len =strlen (octx->u.name); +if (octx->u.name[len -1 ] !='*' ) + goto bad; + } }if (validate_string_field (genfs->fstype))
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 3/4 libsepol: check user MLS levels by scope not policy type
In reply to Stephen SmalleyCommit e4be0092 ("libsepol: Tighten checks on MLS range and level when validating") requires a set semantic level and range for every user in a POLICY_MOD policydb. But a bare:
require { user system_u; }in an MLS module inserts a user datum into the global user symtab with a zeroed semantic level, so any such module now fails policydb_read() at the tail policydb_validate() and cannot be installed via the pp HLL.
At the same time the check accepts an unset semantic level for a POLICY_BASE policydb, but checkpolicy always requires the level for a declared user in a MLS base ("no default level specified for user"), so the only way to reach that path is a required-only user in an optional block - the same case that must be accepted.
The distinction the policy compilers draw is scope: declared users have their level set, required-only users do not. Look up the user in the global scope table and only insist on a set semantic level when the scope is SCOPE_DECL. Kernel policies carry no scope table, and their expanded level and range remain checked separately.
--- libsepol/src/policydb_validate.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
libsepol/src/policydb_validate.c +9 -6
@@ -893,9 +893,11 @@ bad:return -1 ; } -static int validate_user_datum (sepol_handle_t *handle,const user_datum_t *user, -validate_t flavors[],const policydb_t *p) +static int validate_user_datum (sepol_handle_t *handle,hashtab_key_t key, +const user_datum_t *user,validate_t flavors[], +const policydb_t *p) { +const scope_datum_t *scope;int allow_unset;if (validate_value (user->s.value, &flavors[SYM_USERS ]))
@@ -903,7 +905,8 @@ static int validate_user_datum(sepol_handle_t *handle, const user_datum_t *user,if (validate_role_set (&user->roles, &flavors[SYM_ROLES ])) goto bad; - allow_unset = (!p->mls) || (p->policy_type !=POLICY_MOD ); + scope =hashtab_search (p->scope[SYM_USERS ].table, key); + allow_unset = (!p->mls) || (!scope || scope->scope !=SCOPE_DECL );if (validate_mls_semantic_range (&user->range, &flavors[SYM_LEVELS ], &flavors[SYM_CATS ], allow_unset)) goto bad;
@@ -929,12 +932,12 @@ bad:return -1 ; } -static int validate_user_datum_wrapper (__attribute__((unused))hashtab_key_t k, -hashtab_datum_t d,void *args) +static int validate_user_datum_wrapper (hashtab_key_t k,hashtab_datum_t d, +void *args) {map_arg_t *margs = args; -return validate_user_datum (margs->handle, d, margs->flavors, +return validate_user_datum (margs->handle, k, d, margs->flavors, margs->policy); }
Signature · 1 line
2.55.0
Stephen Smalley
maintainerPATCH 4/4 libsepol,checkpolicy,python/semanage: validate portcon ranges
In reply to Stephen SmalleyThe kernel policydb reader rejects an OCON_PORT entry with low_port 0, low_port > high-port, or a port above U16_MAX. Apply the same checking in the policy compilers and semanage front-end.
--- checkpolicy/policy_define.c | 10 +++++----- libsepol/cil/src/cil_build_ast.c | 8 ++++++++ libsepol/src/policydb_validate.c | 5 +++-- python/semanage/seobject.py | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-)
checkpolicy/policy_define.c +5 -5
@@ -5241,15 +5241,15 @@ int define_port_context(unsigned int low, unsigned int high) goto bad; } +if (low ==0 || low > high || high >0xffff ) { +yyerror2 ("Invalid port range %u-%u" , low, high); + goto bad; + } + newc->u.port.protocol = protocol; newc->u.port.low_port = low; newc->u.port.high_port = high; -if (low > high) { -yyerror2 ("low port %d exceeds high port %d" , low, high); - goto bad; - } -if (parse_security_context (&newc->context[0 ])) { goto bad; }
libsepol/cil/src/cil_build_ast.c +8 -0
@@ -4438,6 +4438,14 @@ int cil_gen_portcon(struct cil_db *db, struct cil_tree_node *parse_current, portcon->port_high = portcon->port_low; } +if (portcon->port_low ==0 || portcon->port_low > portcon->port_high || + portcon->port_high >0xffff ) { +cil_log (CIL_ERR ,"Invalid port range %u-%u\n" , + portcon->port_low, portcon->port_high); + rc =SEPOL_ERR ; + goto exit; + } +if (parse_current->next->next->next->cl_head ==NULL ) { portcon->context_str = parse_current->next->next->next->data; }else {
libsepol/src/policydb_validate.c +3 -2
@@ -1594,8 +1594,9 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, goto bad;break ;case OCON_PORT : -if (octx->u.port.low_port > - octx->u.port.high_port) +if (octx->u.port.low_port ==0 || + octx->u.port.low_port > + octx->u.port.high_port) goto bad;switch (octx->u.port.protocol) {case IPPROTO_TCP :
python/semanage/seobject.py +1 -1
@@ -1103,7 +1103,7 @@ class portRecords(semanageRecords): low = int(ports[0]) high = int(ports[1]) - if high > 65535: + if low < 1 or low > high or high > 65535: raise ValueError(_("Invalid Port")) (rc, k) = semanage_port_key_create(self.sh, low, high, proto_d)
Signature · 1 line
2.55.0
Stephen Smalley
maintainerQuoted 7 lines · Stephen Smalley
validate_ocontexts()sanity checks the port range but does not check the protocol field.kernel_to_conf,kernel_to_cil, andmodule_to_cilall reject a portcon whose protocol is not one of tcp, udp, dccp, or sctp, so a binary policy with any other value passes read+validate but cannot be decompiled.
Quoted 6 lines
checkpolicy and secilc only ever emit these four protocols and there is no source syntax for a numeric protocol, so bound the protocol at validate time to the same set. The kernel additionally bounds protocol to
U8_MAXat load time, which this covers.Signed-off-by Stephen Smalley <[email protected]>
This series has been merged.