calipso: Allow the lsm to label the skbuff directly.
Subsystems (5)
RFC PATCH v2 14/18 calipso: Allow the lsm to label the skbuff directly.
In some cases, the lsm needs to add the label to the skbuff directly. A NF_INET_LOCAL_OUT IPv6 hook is added to selinux to match the IPv4 behaviour. This allows selinux to label the skbuffs that it requires.
--- include/net/ipv6.h | 2 +- include/net/netlabel.h | 11 +++ net/ipv6/calipso.c | 180 ++++++++++++++++++++++++++++++++++++++++ net/ipv6/exthdrs_core.c | 2 +- net/netlabel/netlabel_calipso.c | 82 ++++++++++++++++++ net/netlabel/netlabel_calipso.h | 7 ++ net/netlabel/netlabel_kapi.c | 32 ++++++- security/selinux/hooks.c | 15 ++++ 8 files changed, 325 insertions(+), 6 deletions(-)
include/net/ipv6.h +1 -1
@@ -920,7 +920,7 @@ enum {int ipv6_find_hdr (const struct sk_buff *skb,unsigned int *offset,int target,unsigned short *fragoff,int *fragflg); -int ipv6_find_tlv (struct sk_buff *skb,int offset,int type); +int ipv6_find_tlv (const struct sk_buff *skb,int offset,int type); struct in6_addr *fl6_update_dst(struct flowi6 *fl6, const struct ipv6_txoptions *opt,
include/net/netlabel.h +11 -0
@@ -231,6 +231,10 @@ struct netlbl_lsm_secattr {* @sock_delattr: remove the socket's attr * @req_setattr: set the req socket's attr * @req_delattr: remove the req socket's attr +* @optptr: find option in packet +* @getattr: retrieve attr from memory block +* @skbuff_setattr: set the skbuff's attr +* @skbuff_delattr: remove the skbuff's attr * * Description: * This structure is filled out by the CALIPSO engine and passed
@@ -258,6 +262,13 @@ struct netlbl_calipso_ops {const struct calipso_doi *doi_def, const struct netlbl_lsm_secattr *secattr);void (*req_delattr)(struct request_sock *req); +unsigned char *(*optptr)(const struct sk_buff *skb); +int (*getattr)(const unsigned char *calipso, +struct netlbl_lsm_secattr *secattr); +int (*skbuff_setattr)(struct sk_buff *skb, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr); +int (*skbuff_delattr)(struct sk_buff *skb); };/*
net/ipv6/calipso.c +180 -0
198 lines not on this page · raw message
net/ipv6/exthdrs_core.c +1 -1
@@ -112,7 +112,7 @@ int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp, }EXPORT_SYMBOL (ipv6_skip_exthdr); -int ipv6_find_tlv (struct sk_buff *skb,int offset,int type) +int ipv6_find_tlv (const struct sk_buff *skb,int offset,int type) {const unsigned char *nh =skb_network_header (skb);int packet_len =skb_tail_pointer (skb) -skb_network_header (skb);
net/netlabel/netlabel_calipso.c +82 -0
85 lines not on this page · raw message
net/netlabel/netlabel_calipso.h +7 -0
@@ -139,5 +139,12 @@ int calipso_req_setattr(struct request_sock *req,const struct calipso_doi *doi_def, const struct netlbl_lsm_secattr *secattr);void calipso_req_delattr (struct request_sock *req); +unsigned char *calipso_optptr (const struct sk_buff *skb); +int calipso_getattr (const unsigned char *calipso, +struct netlbl_lsm_secattr *secattr); +int calipso_skbuff_setattr (struct sk_buff *skb, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr); +int calipso_skbuff_delattr (struct sk_buff *skb);#endif
net/netlabel/netlabel_kapi.c +28 -4
@@ -1148,13 +1148,17 @@ int netlbl_skbuff_setattr(struct sk_buff *skb, {int ret_val;struct iphdr *hdr4; +#if IS_ENABLED (CONFIG_IPV6 ) +struct ipv6hdr *hdr6; +#endif struct netlbl_dommap_def *entry;rcu_read_lock ();switch (family) {case AF_INET : hdr4 =ip_hdr (skb); - entry =netlbl_domhsh_getentry_af4 (secattr->domain,hdr4->daddr); + entry =netlbl_domhsh_getentry_af4 (secattr->domain, + hdr4->daddr);if (entry ==NULL ) { ret_val = -ENOENT ; goto skbuff_setattr_return;
@@ -1175,9 +1179,26 @@ int netlbl_skbuff_setattr(struct sk_buff *skb,break ;#if IS_ENABLED (CONFIG_IPV6 )case AF_INET6 : -/* since we don't support any IPv6 labeling protocols right -* now we can optimize everything away until we do */ - ret_val =0 ; + hdr6 =ipv6_hdr (skb); + entry =netlbl_domhsh_getentry_af6 (secattr->domain, + &hdr6->daddr); +if (entry ==NULL ) { + ret_val = -ENOENT ; + goto skbuff_setattr_return; + } +switch (entry->type) { +case NETLBL_NLTYPE_CALIPSO : + ret_val =calipso_skbuff_setattr (skb, entry->calipso, + secattr); +break ; +case NETLBL_NLTYPE_UNLABELED : +/* just delete the protocols we support for right now +* but we could remove other protocols if needed */ + ret_val =calipso_skbuff_delattr (skb); +break ; +default : + ret_val = -ENOENT ; + }break ;#endif /* IPv6 */ default :
@@ -1216,6 +1237,9 @@ int netlbl_skbuff_getattr(const struct sk_buff *skb,break ;#if IS_ENABLED (CONFIG_IPV6 )case AF_INET6 : + ptr =calipso_optptr (skb); +if (ptr &&calipso_getattr (ptr, secattr) ==0 ) +return 0 ;break ;#endif /* IPv6 */ }
security/selinux/hooks.c +15 -0
@@ -4929,6 +4929,15 @@ static unsigned int selinux_ipv4_output(void *priv,return selinux_ip_output (skb,PF_INET ); } +#if defined(CONFIG_IPV6 ) || defined(CONFIG_IPV6_MODULE ) +static unsigned int selinux_ipv6_output (void *priv, +struct sk_buff *skb, +const struct nf_hook_state *state) +{ +return selinux_ip_output (skb,PF_INET6 ); +} +#endif /* IPV6 */ +static unsigned int selinux_ip_postroute_compat (struct sk_buff *skb,int ifindex,u16 family)
@@ -6159,6 +6168,12 @@ static struct nf_hook_ops selinux_nf_ops[] = { .hooknum = NF_INET_FORWARD, .priority = NF_IP6_PRI_SELINUX_FIRST, }, + { + .hook = selinux_ipv6_output, + .pf = NFPROTO_IPV6, + .hooknum = NF_INET_LOCAL_OUT, + .priority = NF_IP6_PRI_SELINUX_FIRST, + },#endif /* IPV6 */ };
Signature · 1 line
1.8.0
Quoted 5 lines · Huw Davies
In some cases, the lsm needs to add the label to the skbuff directly. A
NF_INET_LOCAL_OUTIPv6 hook is added to selinux to match the IPv4 behaviour. This allows selinux to label the skbuffs that it requires.Signed-off-by Huw Davies <[email protected]>
...
include/net/ipv6.h enum { · line 923
Quoted 10 lines
include/net/ipv6.h +1
-1@@ -920,7 +920,7 @@ enum {int ipv6_find_hdr (const struct sk_buff *skb,unsigned int *offset,int target,unsigned short *fragoff,int *fragflg); -int ipv6_find_tlv (struct sk_buff *skb,int offset,int type); +int ipv6_find_tlv (const struct sk_buff *skb,int offset,int type);
It probably does make sense to constify the skb argument, but you should do that in a separate patch.
include/net/netlabel.h struct netlbl_lsm_secattr { · line 234
Quoted 11 lines
include/net/ipv6.h +0
-0struct in6_addr *fl6_update_dst(struct flowi6 *fl6, const struct ipv6_txoptions *opt,include/net/netlabel.h +1
-0@@ -231,6 +231,10 @@ struct netlbl_lsm_secattr {* @sock_delattr: remove the socket's attr * @req_setattr: set the req socket's attr * @req_delattr: remove the req socket's attr +* @optptr: find option in packet
How about "skbuff_optptr" instead?
include/net/netlabel.h struct netlbl_lsm_secattr { · line 235
Quoted 1 line
include/net/netlabel.h +1
-0+* @getattr: retrieve attr from memory block
How about "opt_getattr"?
Quoted 2 lines
include/net/netlabel.h +2
-0+* @skbuff_setattr: set the skbuff's attr +* @skbuff_delattr: remove the skbuff's attr
...
Quoted 38 lines
+/**
+ *calipso_skbuff_setattr- Set the CALIPSO option on a packet
+ * @skb: the packet
+ * @doi_def: the CALIPSO DOI to use
+ * @secattr: the security attributes
+ *
+ * Description:
+ * Set the CALIPSO option on the given packet based on the security attributes. + * Returns a pointer to the IP header on success and NULL on failure. + *
+ */include/net/netlabel.h +27
-0+static int calipso_skbuff_setattr (struct sk_buff *skb, +const struct calipso_doi *doi_def, +const struct netlbl_lsm_secattr *secattr) +{ +int ret_val; +struct ipv6hdr *ip6_hdr; +struct ipv6_opt_hdr *hop; +unsigned char buf[CALIPSO_MAX_BUFFER ]; +int len_delta; +unsigned int start, end, next_opt, pad; + + ip6_hdr =ipv6_hdr (skb); +if (ip6_hdr->nexthdr ==NEXTHDR_HOP ) { + hop = (struct ipv6_opt_hdr *)(ip6_hdr +1 ); + ret_val =calipso_opt_find (hop, &start, &end); +if (ret_val && ret_val != -ENOENT ) +return ret_val; +if (end !=ipv6_optlen (hop)) + next_opt = end; +else + next_opt =0 ; + len_delta = -(int )end; + }else { + start =0 ; + next_opt =0 ; + len_delta =0 ; + }
include/net/netlabel.h
Quoted 10 lines
include/net/netlabel.h +10
-0+ +memset (buf,0 ,sizeof (buf)); + ret_val =calipso_genopt (buf, start &3 ,sizeof (buf), doi_def, secattr); +if (ret_val <0 ) +return ret_val; + + end = start + ret_val; + +if (WARN_ON_ONCE (end &3 )) +return -EINVAL ;
See my earlier comments about the use of WARN_ON_ONCE().
include/net/netlabel.h
Quoted 5 lines
include/net/netlabel.h +5
-0+ pad = ((end &7 ) + (next_opt &7 )) &7 ; + len_delta += end + pad; + +if (WARN_ON_ONCE (len_delta &7 )) +return -EINVAL ;
Same.
Quoted 14 lines
include/net/netlabel.h +14
-0+ ret_val =skb_cow (skb,skb_headroom (skb) + len_delta); +if (ret_val <0 ) +return ret_val; + +if (len_delta) { +if (len_delta >0 ) +skb_push (skb, len_delta); +else +skb_pull (skb, -len_delta); +memmove ((char *)ip6_hdr - len_delta, ip6_hdr, +sizeof (*ip6_hdr) + start); +skb_reset_network_header (skb); + ip6_hdr =ipv6_hdr (skb); + }
Signature · 2 lines
paul moore security @ redhat