* [apparmor] [RFC] Support network policy for secmark labels
@ 2018-05-18 19:06 Matthew Garrett
2018-05-18 19:06 ` [apparmor] [PATCH 1/2] apparmor: Parse secmark policy Matthew Garrett
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Matthew Garrett @ 2018-05-18 19:06 UTC (permalink / raw)
To: apparmor
Secmark allows us to label packets with fairly arbitrary iptables rules,
and these patches give a mechanism for then applying Apparmor policy to
those labels. I haven't really thought through how this applies to
existing network policy, so feedback on that welcome.
^ permalink raw reply [flat|nested] 8+ messages in thread* [apparmor] [PATCH 1/2] apparmor: Parse secmark policy 2018-05-18 19:06 [apparmor] [RFC] Support network policy for secmark labels Matthew Garrett @ 2018-05-18 19:06 ` Matthew Garrett 2018-05-18 19:06 ` [apparmor] [PATCH 2/2] apparmor: Allow filtering based on " Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2] Support network policy based on secmark labeling Matthew Garrett 2 siblings, 0 replies; 8+ messages in thread From: Matthew Garrett @ 2018-05-18 19:06 UTC (permalink / raw) To: apparmor Add support for parsing secmark policy provided by userspace, and store that in the overall policy. Signed-off-by: Matthew Garrett <mjg59@google.com> --- security/apparmor/include/net.h | 7 +++++ security/apparmor/include/policy.h | 3 +++ security/apparmor/policy.c | 3 +++ security/apparmor/policy_unpack.c | 43 ++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/security/apparmor/include/net.h b/security/apparmor/include/net.h index ec7228e857a9..31f45fed1541 100644 --- a/security/apparmor/include/net.h +++ b/security/apparmor/include/net.h @@ -83,6 +83,13 @@ struct aa_sk_ctx { __e; \ }) +struct aa_secmark { + u16 audit; + u16 deny; + u32 secid; + char *label; +}; + extern struct aa_sfs_entry aa_sfs_entry_network[]; void audit_net_cb(struct audit_buffer *ab, void *va); diff --git a/security/apparmor/include/policy.h b/security/apparmor/include/policy.h index ab64c6b5db5a..8e6707c837be 100644 --- a/security/apparmor/include/policy.h +++ b/security/apparmor/include/policy.h @@ -155,6 +155,9 @@ struct aa_profile { struct aa_rlimit rlimits; + int secmark_count; + struct aa_secmark *secmark; + struct aa_loaddata *rawdata; unsigned char *hash; char *dirname; diff --git a/security/apparmor/policy.c b/security/apparmor/policy.c index b367fef33d03..490074042b1a 100644 --- a/security/apparmor/policy.c +++ b/security/apparmor/policy.c @@ -231,6 +231,9 @@ void aa_free_profile(struct aa_profile *profile) for (i = 0; i < profile->xattr_count; i++) kzfree(profile->xattrs[i]); kzfree(profile->xattrs); + for (i=0; i < profile->secmark_count; i++) + kzfree(profile->secmark[i].label); + kzfree(profile->secmark); kzfree(profile->dirname); aa_put_dfa(profile->xmatch); aa_put_dfa(profile->policy.dfa); diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c index b9e6b2cafa69..7717368b5e07 100644 --- a/security/apparmor/policy_unpack.c +++ b/security/apparmor/policy_unpack.c @@ -561,6 +561,44 @@ static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile) return 0; } +static bool unpack_secmark(struct aa_ext *e, struct aa_profile *profile) +{ + void *pos = e->pos; + + if (unpack_nameX(e, AA_STRUCT, "secmark")) { + int i, size; + + size = unpack_array(e, NULL); + + profile->secmark = kcalloc(size, sizeof(struct aa_secmark), + GFP_KERNEL); + if (!profile->secmark) + goto fail; + + profile->secmark_count = size; + + for (i = 0; i < size; i++) { + profile->secmark[i].secid = 0; + if (!unpack_u16(e, &profile->secmark[i].audit, NULL)) + goto fail; + if (!unpack_u16(e, &profile->secmark[i].deny, NULL)) + goto fail; + if (!unpack_strdup(e, &profile->secmark[i].label, NULL)) + goto fail; + } + if (!unpack_nameX(e, AA_ARRAYEND, NULL)) + goto fail; + if (!unpack_nameX(e, AA_STRUCTEND, NULL)) + goto fail; + } + + return 1; + +fail: + e->pos = pos; + return 0; +} + static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile) { void *pos = e->pos; @@ -759,6 +797,11 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) goto fail; } + if (!unpack_secmark(e, profile)) { + info = "failed to unpack profile secmark rules"; + goto fail; + } + if (unpack_nameX(e, AA_STRUCT, "policydb")) { /* generic policy dfa - optional and may be NULL */ info = "failed to unpack policydb"; -- 2.17.0.441.gb46fe60e1d-goog ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [apparmor] [PATCH 2/2] apparmor: Allow filtering based on secmark policy 2018-05-18 19:06 [apparmor] [RFC] Support network policy for secmark labels Matthew Garrett 2018-05-18 19:06 ` [apparmor] [PATCH 1/2] apparmor: Parse secmark policy Matthew Garrett @ 2018-05-18 19:06 ` Matthew Garrett 2018-05-18 20:57 ` Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2] Support network policy based on secmark labeling Matthew Garrett 2 siblings, 1 reply; 8+ messages in thread From: Matthew Garrett @ 2018-05-18 19:06 UTC (permalink / raw) To: apparmor Add support for dropping or accepting packets based on their secmark tags. Signed-off-by: Matthew Garrett <mjg59@google.com> --- security/apparmor/include/net.h | 3 + security/apparmor/lsm.c | 112 +++++++++++++++++++++++++++++++- security/apparmor/net.c | 59 +++++++++++++++++ 3 files changed, 173 insertions(+), 1 deletion(-) diff --git a/security/apparmor/include/net.h b/security/apparmor/include/net.h index 31f45fed1541..f53b887861ba 100644 --- a/security/apparmor/include/net.h +++ b/security/apparmor/include/net.h @@ -110,4 +110,7 @@ int aa_sk_perm(const char *op, u32 request, struct sock *sk); int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request, struct socket *sock); +int apparmor_secmark_check(struct aa_label *label, char *op, u32 request, + u32 secid, struct sock *sk); + #endif /* __AA_NET_H */ diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index dab5409f2608..a366b501fb6a 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -23,6 +23,8 @@ #include <linux/sysctl.h> #include <linux/audit.h> #include <linux/user_namespace.h> +#include <linux/netfilter_ipv4.h> +#include <linux/netfilter_ipv6.h> #include <net/sock.h> #include "include/apparmor.h" @@ -1029,7 +1031,13 @@ static int apparmor_socket_shutdown(struct socket *sock, int how) */ static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) { - return 0; + struct aa_sk_ctx *ctx = SK_CTX(sk); + + if (!skb->secmark) + return 0; + + return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, + skb->secmark, sk); } @@ -1125,6 +1133,18 @@ static void apparmor_sock_graft(struct sock *sk, struct socket *parent) ctx->label = aa_get_current_label(); } +static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb, + struct request_sock *req) +{ + struct aa_sk_ctx *ctx = SK_CTX(sk); + + if (!skb->secmark) + return 0; + + return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, + skb->secmark, sk); +} + static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), @@ -1182,6 +1202,7 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(socket_getpeersec_dgram, apparmor_socket_getpeersec_dgram), LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), + LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), LSM_HOOK_INIT(cred_free, apparmor_cred_free), @@ -1536,6 +1557,95 @@ static inline int apparmor_init_sysctl(void) } #endif /* CONFIG_SYSCTL */ +static unsigned int apparmor_ip_postroute(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + struct aa_sk_ctx *ctx; + struct sock *sk; + + if (!skb->secmark) + return NF_ACCEPT; + + sk = skb_to_full_sk(skb); + if (sk == NULL) + return NF_ACCEPT; + + ctx = SK_CTX(sk); + if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, + skb->secmark, sk)) + return NF_ACCEPT; + + return NF_DROP_ERR(-ECONNREFUSED); + +} + +static unsigned int apparmor_ipv4_postroute(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + return apparmor_ip_postroute(priv, skb, state); +} + +static unsigned int apparmor_ipv6_postroute(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + return apparmor_ip_postroute(priv, skb, state); +} + +static const struct nf_hook_ops apparmor_nf_ops[] = { + { + .hook = apparmor_ipv4_postroute, + .pf = NFPROTO_IPV4, + .hooknum = NF_INET_POST_ROUTING, + .priority = NF_IP_PRI_SELINUX_FIRST, + }, +#if IS_ENABLED(CONFIG_IPV6) + { + .hook = apparmor_ipv6_postroute, + .pf = NFPROTO_IPV6, + .hooknum = NF_INET_POST_ROUTING, + .priority = NF_IP6_PRI_SELINUX_FIRST, + }, +#endif +}; + +static int __net_init apparmor_nf_register(struct net *net) +{ + int ret; + + ret = nf_register_net_hooks(net, apparmor_nf_ops, + ARRAY_SIZE(apparmor_nf_ops)); + return ret; +} + +static void __net_exit apparmor_nf_unregister(struct net *net) +{ + nf_unregister_net_hooks(net, apparmor_nf_ops, + ARRAY_SIZE(apparmor_nf_ops)); +} + +static struct pernet_operations apparmor_net_ops = { + .init = apparmor_nf_register, + .exit = apparmor_nf_unregister, +}; + +static int __init apparmor_nf_ip_init(void) +{ + int err; + + if (!apparmor_enabled) + return 0; + + err = register_pernet_subsys(&apparmor_net_ops); + if (err) + panic("Apparmor: register_pernet_subsys: error %d\n", err); + + return 0; +} +__initcall(apparmor_nf_ip_init); + static int __init apparmor_init(void) { int error; diff --git a/security/apparmor/net.c b/security/apparmor/net.c index bb24cfa0a164..e94971ba84d0 100644 --- a/security/apparmor/net.c +++ b/security/apparmor/net.c @@ -185,3 +185,62 @@ int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request, return aa_label_sk_perm(label, op, request, sock->sk); } + +static int apparmor_secmark_init(struct aa_secmark *secmark) +{ + struct aa_label *label; + + label = aa_label_strn_parse(&root_ns->unconfined->label, + secmark->label, strlen(secmark->label), + GFP_ATOMIC, false, false); + + if (IS_ERR(label)) + return PTR_ERR(label); + + secmark->secid = label->secid; + + return 0; +} + +static int aa_secmark_perm(struct aa_profile *profile, u32 request, u32 secid, + struct common_audit_data *sa, struct sock *sk) +{ + int i, ret; + struct aa_perms perms = { }; + + if (profile->secmark_count == 0) + return 0; + + for (i = 0; i < profile->secmark_count; i++) { + if (!profile->secmark[i].secid) { + ret = apparmor_secmark_init(&profile->secmark[i]); + if (ret) + return ret; + } + + if (profile->secmark[i].secid == secid) { + if (profile->secmark[i].deny) + perms.deny = ALL_PERMS_MASK; + else + perms.allow = ALL_PERMS_MASK; + + if (profile->secmark[i].audit) + perms.audit = ALL_PERMS_MASK; + } + } + + aa_apply_modes_to_perms(profile, &perms); + + return aa_check_perms(profile, &perms, request, sa, audit_net_cb); +} + +int apparmor_secmark_check(struct aa_label *label, char *op, u32 request, + u32 secid, struct sock *sk) +{ + struct aa_profile *profile; + DEFINE_AUDIT_SK(sa, op, sk); + + return fn_for_each_confined(label, profile, + aa_secmark_perm(profile, request, secid, + &sa, sk)); +} -- 2.17.0.441.gb46fe60e1d-goog ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [apparmor] [PATCH 2/2] apparmor: Allow filtering based on secmark policy 2018-05-18 19:06 ` [apparmor] [PATCH 2/2] apparmor: Allow filtering based on " Matthew Garrett @ 2018-05-18 20:57 ` Matthew Garrett 0 siblings, 0 replies; 8+ messages in thread From: Matthew Garrett @ 2018-05-18 20:57 UTC (permalink / raw) To: apparmor On Fri, May 18, 2018 at 12:06 PM Matthew Garrett <mjg59@google.com> wrote: > +static int aa_secmark_perm(struct aa_profile *profile, u32 request, u32 secid, > + struct common_audit_data *sa, struct sock *sk) > +{ > + int i, ret; > + struct aa_perms perms = { }; > + > + if (profile->secmark_count == 0) > + return 0; > + > + for (i = 0; i < profile->secmark_count; i++) { > + if (!profile->secmark[i].secid) { > + ret = apparmor_secmark_init(&profile->secmark[i]); > + if (ret) > + return ret; > + } > + > + if (profile->secmark[i].secid == secid) { > + if (profile->secmark[i].deny) > + perms.deny = ALL_PERMS_MASK; > + else > + perms.allow = ALL_PERMS_MASK; > + > + if (profile->secmark[i].audit) > + perms.audit = ALL_PERMS_MASK; > + } > + } > + > + aa_apply_modes_to_perms(profile, &perms); > + > + return aa_check_perms(profile, &perms, request, sa, audit_net_cb); > +} So, specifically, my question is about whether this is doing the right thing. If someone writes a rule that's just: network then secmark_count should be 0 and the rule will grant full network access. If someone has: network label=foo then secmark_count should be > 0, and the secmark policy checking will pass for packets with a label of foo and fail otherwise. For network deny network label=foo then secmark_count should be > 0, and packets with a label of foo will get perms.deny set to ALL_PERMS_MASK. However, something with a label of bar will end up with aa_apply_modes_to_perms being called without perms.allow having been set, and I think that's going to do the wrong thing. The "easy" thing to do would be to default to perms.allow = ALL_PERMS_MASK if nothing matches, but that would mean: network label=foo on its own would allow non-foo packets to pass. So I think what actually needs to happen here is for a bare network statement to imply a wildcard secmark label? If I keep the secmark_count == 0 special case, I think this can then be done in the parser - any parser version that understands the label=foo syntax would then also generate a special wildcard secmark statement in response to a bare network permission. Thoughts? ^ permalink raw reply [flat|nested] 8+ messages in thread
* [apparmor] [PATCH V2] Support network policy based on secmark labeling 2018-05-18 19:06 [apparmor] [RFC] Support network policy for secmark labels Matthew Garrett 2018-05-18 19:06 ` [apparmor] [PATCH 1/2] apparmor: Parse secmark policy Matthew Garrett 2018-05-18 19:06 ` [apparmor] [PATCH 2/2] apparmor: Allow filtering based on " Matthew Garrett @ 2018-05-24 20:27 ` Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 1/3] apparmor: Add a wildcard secid Matthew Garrett ` (2 more replies) 2 siblings, 3 replies; 8+ messages in thread From: Matthew Garrett @ 2018-05-24 20:27 UTC (permalink / raw) To: john.johansen, apparmor Ok here's a slightly different approach that allows for a wildcard secid. In this universe: 1) Old parsers just generate a network statement as normal, secmark_count is 0 and we assume that we should do nothing in response to secmark labeling 2) New parsers that generate a bare network statement add a wildcard label, and any further deny statements will be tested in addition to that I think the audit/deny handling in the policy stuff needs to be fixed up, but does this logic look roughly plausible to people? ^ permalink raw reply [flat|nested] 8+ messages in thread
* [apparmor] [PATCH V2 1/3] apparmor: Add a wildcard secid 2018-05-24 20:27 ` [apparmor] [PATCH V2] Support network policy based on secmark labeling Matthew Garrett @ 2018-05-24 20:27 ` Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 2/3] apparmor: Parse secmark policy Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 3/3] apparmor: Allow filtering based on " Matthew Garrett 2 siblings, 0 replies; 8+ messages in thread From: Matthew Garrett @ 2018-05-24 20:27 UTC (permalink / raw) To: john.johansen, apparmor, mjg59 Reserve a secid value that we can use as a wildcard, allowing us to define policy that's expected to match against all secids. Signed-off-by: Matthew Garrett <mjg59@google.com> --- security/apparmor/include/secid.h | 5 +++++ security/apparmor/secid.c | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/security/apparmor/include/secid.h b/security/apparmor/include/secid.h index 686de8e50a79..29bdfc7b48b7 100644 --- a/security/apparmor/include/secid.h +++ b/security/apparmor/include/secid.h @@ -22,6 +22,11 @@ struct aa_label; /* secid value that will not be allocated */ #define AA_SECID_INVALID 0 +#define AA_SECID_MAX (U32_MAX - 1) + +/* secid value that matches any other secid */ +#define AA_SECID_WILDCARD U32_MAX + struct aa_label *aa_secid_to_label(u32 secid); int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen); int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid); diff --git a/security/apparmor/secid.c b/security/apparmor/secid.c index c2f0c1571156..9574660f4531 100644 --- a/security/apparmor/secid.c +++ b/security/apparmor/secid.c @@ -49,8 +49,6 @@ static unsigned long free_count; * TODO: use secid_update in label replace */ -#define SECID_MAX U32_MAX - /* TODO: mark free list as exceptional */ static void *to_ptr(u32 secid) { @@ -204,7 +202,7 @@ u32 aa_alloc_secid(struct aa_label *label, gfp_t gfp) if (gfpflags_allow_blocking(gfp) && !radix_tree_preload(gfp)) preload = 1; spin_lock_irqsave(&secid_lock, flags); - if (alloced_secid != SECID_MAX) { + if (alloced_secid != AA_SECID_MAX) { secid = ++alloced_secid; res = radix_tree_insert(&aa_secids_map, secid, label); AA_BUG(res == -EEXIST); -- 2.17.0.441.gb46fe60e1d-goog ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [apparmor] [PATCH V2 2/3] apparmor: Parse secmark policy 2018-05-24 20:27 ` [apparmor] [PATCH V2] Support network policy based on secmark labeling Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 1/3] apparmor: Add a wildcard secid Matthew Garrett @ 2018-05-24 20:27 ` Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 3/3] apparmor: Allow filtering based on " Matthew Garrett 2 siblings, 0 replies; 8+ messages in thread From: Matthew Garrett @ 2018-05-24 20:27 UTC (permalink / raw) To: john.johansen, apparmor, mjg59 Add support for parsing secmark policy provided by userspace, and store that in the overall policy. Signed-off-by: Matthew Garrett <mjg59@google.com> --- security/apparmor/include/net.h | 10 +++++ security/apparmor/include/policy.h | 3 ++ security/apparmor/policy.c | 3 ++ security/apparmor/policy_unpack.c | 61 ++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/security/apparmor/include/net.h b/security/apparmor/include/net.h index ec7228e857a9..7334ac966d01 100644 --- a/security/apparmor/include/net.h +++ b/security/apparmor/include/net.h @@ -83,6 +83,13 @@ struct aa_sk_ctx { __e; \ }) +struct aa_secmark { + u8 audit; + u8 deny; + u32 secid; + char *label; +}; + extern struct aa_sfs_entry aa_sfs_entry_network[]; void audit_net_cb(struct audit_buffer *ab, void *va); @@ -103,4 +110,7 @@ int aa_sk_perm(const char *op, u32 request, struct sock *sk); int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request, struct socket *sock); +int apparmor_secmark_check(struct aa_label *label, char *op, u32 request, + u32 secid, struct sock *sk); + #endif /* __AA_NET_H */ diff --git a/security/apparmor/include/policy.h b/security/apparmor/include/policy.h index ab64c6b5db5a..8e6707c837be 100644 --- a/security/apparmor/include/policy.h +++ b/security/apparmor/include/policy.h @@ -155,6 +155,9 @@ struct aa_profile { struct aa_rlimit rlimits; + int secmark_count; + struct aa_secmark *secmark; + struct aa_loaddata *rawdata; unsigned char *hash; char *dirname; diff --git a/security/apparmor/policy.c b/security/apparmor/policy.c index b367fef33d03..490074042b1a 100644 --- a/security/apparmor/policy.c +++ b/security/apparmor/policy.c @@ -231,6 +231,9 @@ void aa_free_profile(struct aa_profile *profile) for (i = 0; i < profile->xattr_count; i++) kzfree(profile->xattrs[i]); kzfree(profile->xattrs); + for (i=0; i < profile->secmark_count; i++) + kzfree(profile->secmark[i].label); + kzfree(profile->secmark); kzfree(profile->dirname); aa_put_dfa(profile->xmatch); aa_put_dfa(profile->policy.dfa); diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c index b9e6b2cafa69..ebada029f1b3 100644 --- a/security/apparmor/policy_unpack.c +++ b/security/apparmor/policy_unpack.c @@ -292,6 +292,19 @@ static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name) return 0; } +static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name) +{ + if (unpack_nameX(e, AA_U8, name)) { + if (!inbounds(e, sizeof(u8))) + return 0; + if (data) + *data = get_unaligned((u8 *)e->pos); + e->pos += sizeof(u8); + return 1; + } + return 0; +} + static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name) { if (unpack_nameX(e, AA_U32, name)) { @@ -561,6 +574,49 @@ static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile) return 0; } +static bool unpack_secmark(struct aa_ext *e, struct aa_profile *profile) +{ + void *pos = e->pos; + int i, size; + + if (unpack_nameX(e, AA_STRUCT, "secmark")) { + size = unpack_array(e, NULL); + + profile->secmark = kcalloc(size, sizeof(struct aa_secmark), + GFP_KERNEL); + if (!profile->secmark) + goto fail; + + profile->secmark_count = size; + + for (i = 0; i < size; i++) { + if (!unpack_u8(e, &profile->secmark[i].audit, NULL)) + goto fail; + if (!unpack_u8(e, &profile->secmark[i].deny, NULL)) + goto fail; + if (!unpack_strdup(e, &profile->secmark[i].label, NULL)) + goto fail; + } + if (!unpack_nameX(e, AA_ARRAYEND, NULL)) + goto fail; + if (!unpack_nameX(e, AA_STRUCTEND, NULL)) + goto fail; + } + + return 1; + +fail: + if (profile->secmark) { + for (i = 0; i < size; i++) + kfree(profile->secmark[i].label); + kfree(profile->secmark); + profile->secmark_count = 0; + } + + e->pos = pos; + return 0; +} + static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile) { void *pos = e->pos; @@ -759,6 +815,11 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) goto fail; } + if (!unpack_secmark(e, profile)) { + info = "failed to unpack profile secmark rules"; + goto fail; + } + if (unpack_nameX(e, AA_STRUCT, "policydb")) { /* generic policy dfa - optional and may be NULL */ info = "failed to unpack policydb"; -- 2.17.0.441.gb46fe60e1d-goog ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [apparmor] [PATCH V2 3/3] apparmor: Allow filtering based on secmark policy 2018-05-24 20:27 ` [apparmor] [PATCH V2] Support network policy based on secmark labeling Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 1/3] apparmor: Add a wildcard secid Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 2/3] apparmor: Parse secmark policy Matthew Garrett @ 2018-05-24 20:27 ` Matthew Garrett 2 siblings, 0 replies; 8+ messages in thread From: Matthew Garrett @ 2018-05-24 20:27 UTC (permalink / raw) To: john.johansen, apparmor, mjg59 Add support for dropping or accepting packets based on their secmark tags. Signed-off-by: Matthew Garrett <mjg59@google.com> --- security/apparmor/lsm.c | 112 +++++++++++++++++++++++++++++++++++++++- security/apparmor/net.c | 66 +++++++++++++++++++++++ 2 files changed, 177 insertions(+), 1 deletion(-) diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index dab5409f2608..a366b501fb6a 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -23,6 +23,8 @@ #include <linux/sysctl.h> #include <linux/audit.h> #include <linux/user_namespace.h> +#include <linux/netfilter_ipv4.h> +#include <linux/netfilter_ipv6.h> #include <net/sock.h> #include "include/apparmor.h" @@ -1029,7 +1031,13 @@ static int apparmor_socket_shutdown(struct socket *sock, int how) */ static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) { - return 0; + struct aa_sk_ctx *ctx = SK_CTX(sk); + + if (!skb->secmark) + return 0; + + return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, + skb->secmark, sk); } @@ -1125,6 +1133,18 @@ static void apparmor_sock_graft(struct sock *sk, struct socket *parent) ctx->label = aa_get_current_label(); } +static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb, + struct request_sock *req) +{ + struct aa_sk_ctx *ctx = SK_CTX(sk); + + if (!skb->secmark) + return 0; + + return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, + skb->secmark, sk); +} + static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), @@ -1182,6 +1202,7 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(socket_getpeersec_dgram, apparmor_socket_getpeersec_dgram), LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), + LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), LSM_HOOK_INIT(cred_free, apparmor_cred_free), @@ -1536,6 +1557,95 @@ static inline int apparmor_init_sysctl(void) } #endif /* CONFIG_SYSCTL */ +static unsigned int apparmor_ip_postroute(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + struct aa_sk_ctx *ctx; + struct sock *sk; + + if (!skb->secmark) + return NF_ACCEPT; + + sk = skb_to_full_sk(skb); + if (sk == NULL) + return NF_ACCEPT; + + ctx = SK_CTX(sk); + if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, + skb->secmark, sk)) + return NF_ACCEPT; + + return NF_DROP_ERR(-ECONNREFUSED); + +} + +static unsigned int apparmor_ipv4_postroute(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + return apparmor_ip_postroute(priv, skb, state); +} + +static unsigned int apparmor_ipv6_postroute(void *priv, + struct sk_buff *skb, + const struct nf_hook_state *state) +{ + return apparmor_ip_postroute(priv, skb, state); +} + +static const struct nf_hook_ops apparmor_nf_ops[] = { + { + .hook = apparmor_ipv4_postroute, + .pf = NFPROTO_IPV4, + .hooknum = NF_INET_POST_ROUTING, + .priority = NF_IP_PRI_SELINUX_FIRST, + }, +#if IS_ENABLED(CONFIG_IPV6) + { + .hook = apparmor_ipv6_postroute, + .pf = NFPROTO_IPV6, + .hooknum = NF_INET_POST_ROUTING, + .priority = NF_IP6_PRI_SELINUX_FIRST, + }, +#endif +}; + +static int __net_init apparmor_nf_register(struct net *net) +{ + int ret; + + ret = nf_register_net_hooks(net, apparmor_nf_ops, + ARRAY_SIZE(apparmor_nf_ops)); + return ret; +} + +static void __net_exit apparmor_nf_unregister(struct net *net) +{ + nf_unregister_net_hooks(net, apparmor_nf_ops, + ARRAY_SIZE(apparmor_nf_ops)); +} + +static struct pernet_operations apparmor_net_ops = { + .init = apparmor_nf_register, + .exit = apparmor_nf_unregister, +}; + +static int __init apparmor_nf_ip_init(void) +{ + int err; + + if (!apparmor_enabled) + return 0; + + err = register_pernet_subsys(&apparmor_net_ops); + if (err) + panic("Apparmor: register_pernet_subsys: error %d\n", err); + + return 0; +} +__initcall(apparmor_nf_ip_init); + static int __init apparmor_init(void) { int error; diff --git a/security/apparmor/net.c b/security/apparmor/net.c index bb24cfa0a164..0e57e4a2948d 100644 --- a/security/apparmor/net.c +++ b/security/apparmor/net.c @@ -18,6 +18,7 @@ #include "include/label.h" #include "include/net.h" #include "include/policy.h" +#include "include/secid.h" #include "net_names.h" @@ -185,3 +186,68 @@ int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request, return aa_label_sk_perm(label, op, request, sock->sk); } + +static int apparmor_secmark_init(struct aa_secmark *secmark) +{ + struct aa_label *label; + + if (secmark->label[0] == '*') { + secmark->secid = AA_SECID_WILDCARD; + return 0; + } + + label = aa_label_strn_parse(&root_ns->unconfined->label, + secmark->label, strlen(secmark->label), + GFP_ATOMIC, false, false); + + if (IS_ERR(label)) + return PTR_ERR(label); + + secmark->secid = label->secid; + + return 0; +} + +static int aa_secmark_perm(struct aa_profile *profile, u32 request, u32 secid, + struct common_audit_data *sa, struct sock *sk) +{ + int i, ret; + struct aa_perms perms = { }; + + if (profile->secmark_count == 0) + return 0; + + for (i = 0; i < profile->secmark_count; i++) { + if (!profile->secmark[i].secid) { + ret = apparmor_secmark_init(&profile->secmark[i]); + if (ret) + return ret; + } + + if (profile->secmark[i].secid == secid || + profile->secmark[i].secid == AA_SECID_WILDCARD) { + if (profile->secmark[i].deny) + perms.deny = ALL_PERMS_MASK; + else + perms.allow = ALL_PERMS_MASK; + + if (profile->secmark[i].audit) + perms.audit = ALL_PERMS_MASK; + } + } + + aa_apply_modes_to_perms(profile, &perms); + + return aa_check_perms(profile, &perms, request, sa, audit_net_cb); +} + +int apparmor_secmark_check(struct aa_label *label, char *op, u32 request, + u32 secid, struct sock *sk) +{ + struct aa_profile *profile; + DEFINE_AUDIT_SK(sa, op, sk); + + return fn_for_each_confined(label, profile, + aa_secmark_perm(profile, request, secid, + &sa, sk)); +} -- 2.17.0.441.gb46fe60e1d-goog ^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2018-05-24 20:27 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2018-05-18 19:06 [apparmor] [RFC] Support network policy for secmark labels Matthew Garrett 2018-05-18 19:06 ` [apparmor] [PATCH 1/2] apparmor: Parse secmark policy Matthew Garrett 2018-05-18 19:06 ` [apparmor] [PATCH 2/2] apparmor: Allow filtering based on " Matthew Garrett 2018-05-18 20:57 ` Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2] Support network policy based on secmark labeling Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 1/3] apparmor: Add a wildcard secid Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 2/3] apparmor: Parse secmark policy Matthew Garrett 2018-05-24 20:27 ` [apparmor] [PATCH V2 3/3] apparmor: Allow filtering based on " Matthew Garrett
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).