secmark: make secmark object handling generic
Subsystems (6)
PATCH 1/4 secmark: make secmark object handling generic
Right now secmark has lots of direct selinux calls. Use all LSM calls and remove all SELinux specific knowledge. The only SELinux specific knowledge we leave is the mode. The only point is to make sure that other LSMs at least test this generic code before they assume it works. (They may also have to make changes if they do not represent labels as strings)
---
include/linux/netfilter/xt_SECMARK.h | 12 ++---- include/linux/security.h | 25 +++++++++++++ include/linux/selinux.h | 63 ---------------------------------- net/netfilter/xt_CT.c | 1 - net/netfilter/xt_SECMARK.c | 35 +++++++++---------- security/capability.c | 17 +++++++++ security/security.c | 18 ++++++++++ security/selinux/exports.c | 49 -------------------------- security/selinux/hooks.c | 24 +++++++++++++ security/selinux/include/security.h | 1 + 10 files changed, 104 insertions(+), 141 deletions(-)
include/linux/netfilter/xt_SECMARK.h +3 -9
@@ -11,18 +11,12 @@* packets are being marked for. */ #define SECMARK_MODE_SEL 0x01/* SELinux */ -#define SECMARK_SELCTX_MAX 256 - -struct xt_secmark_target_selinux_info { -__u32 selsid; -char selctx[SECMARK_SELCTX_MAX ]; -}; +#define SECMARK_SECCTX_MAX 256struct xt_secmark_target_info {__u8 mode; -union { -struct xt_secmark_target_selinux_info sel; - } u; +__u32 secid; +char secctx[SECMARK_SECCTX_MAX ]; };#endif /*_XT_SECMARK_H_target */
include/linux/security.h +25 -0
@@ -960,6 +960,12 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)* Sets the new child socket's sid to the openreq sid. * @inet_conn_established: * Sets the connection's peersid to the secmark on skb. +* @secmark_relabel_packet: +* check if the process should be allowed to relabel packets to the given secid +* @security_secmark_refcount_inc +* tells the LSM to increment the number of secmark labeling rules loaded +* @security_secmark_refcount_dec +* tells the LSM to decrement the number of secmark labeling rules loaded * @req_classify_flow: * Sets the flow's sid to the openreq sid. * @tun_dev_create:
@@ -1596,6 +1602,9 @@ struct security_operations { struct request_sock *req);void (*inet_csk_clone) (struct sock *newsk,const struct request_sock *req);void (*inet_conn_established) (struct sock *sk,struct sk_buff *skb); +int (*secmark_relabel_packet) (u32 secid); +void (*secmark_refcount_inc) (void ); +void (*secmark_refcount_dec) (void );void (*req_classify_flow) (const struct request_sock *req,struct flowi *fl);int (*tun_dev_create)(void );void (*tun_dev_post_create)(struct sock *sk);
@@ -2556,6 +2565,9 @@ void security_inet_csk_clone(struct sock *newsk, const struct request_sock *req);void security_inet_conn_established (struct sock *sk,struct sk_buff *skb); +int security_secmark_relabel_packet (u32 secid); +void security_secmark_refcount_inc (void ); +void security_secmark_refcount_dec (void );int security_tun_dev_create (void );void security_tun_dev_post_create (struct sock *sk);int security_tun_dev_attach (struct sock *sk);
@@ -2710,6 +2722,19 @@ static inline void security_inet_conn_established(struct sock *sk, { } +static inline int security_secmark_relabel_packet (u32 secid) +{ +return 0 ; +} + +static inline void security_secmark_refcount_inc (void ) +{ +} + +static inline void security_secmark_refcount_dec (void ) +{ +} +static inline int security_tun_dev_create (void ) {return 0 ;
include/linux/selinux.h +0 -63
74 lines not on this page · raw message
net/netfilter/xt_CT.c +0 -1
@@ -9,7 +9,6 @@#include <linux/module.h> #include <linux/gfp.h> #include <linux/skbuff.h> -#include <linux/selinux.h> #include <linux/netfilter_ipv4/ip_tables.h> #include <linux/netfilter_ipv6/ip6_tables.h> #include <linux/netfilter/x_tables.h>
net/netfilter/xt_SECMARK.c +17 -18
@@ -14,8 +14,8 @@*/ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/module.h> +#include <linux/security.h> #include <linux/skbuff.h> -#include <linux/selinux.h> #include <linux/netfilter/x_tables.h> #include <linux/netfilter/xt_SECMARK.h>
@@ -39,9 +39,8 @@ secmark_tg(struct sk_buff *skb, const struct xt_action_param *par)switch (mode) {case SECMARK_MODE_SEL : - secmark = info->u.sel.selsid; + secmark = info->secid;break ; -default :BUG (); }
@@ -50,33 +49,33 @@ secmark_tg(struct sk_buff *skb, const struct xt_action_param *par)return XT_CONTINUE ; } -static int checkentry_selinux (struct xt_secmark_target_info *info) +static int checkentry_lsm (struct xt_secmark_target_info *info) {int err; -struct xt_secmark_target_selinux_info *sel = &info->u.sel; - sel->selctx[SECMARK_SELCTX_MAX -1 ] ='\0' ; + info->secctx[SECMARK_SECCTX_MAX -1 ] ='\0' ; + info->secid =0 ; - err =selinux_string_to_sid (sel->selctx, &sel->selsid); + err =security_secctx_to_secid (info->secctx,strlen (info->secctx), + &info->secid);if (err) {if (err == -EINVAL ) -pr_info ("invalid SELinux context \'%s\'\n" , - sel->selctx); +pr_info ("invalid security context \'%s\'\n" , info->secctx);return err; } -if (!sel->selsid) { -pr_info ("unable to map SELinux context \'%s\'\n" , sel->selctx); +if (!info->secid) { +pr_info ("unable to map security context \'%s\'\n" , info->secctx);return -ENOENT ; } - err =selinux_secmark_relabel_packet_permission (sel->selsid); + err =security_secmark_relabel_packet (info->secid);if (err) {pr_info ("unable to obtain relabeling permission\n" );return err; } -selinux_secmark_refcount_inc (); +security_secmark_refcount_inc ();return 0 ; }
@@ -100,16 +99,16 @@ static int secmark_tg_check(const struct xt_tgchk_param *par)switch (info->mode) {case SECMARK_MODE_SEL : - err =checkentry_selinux (info); -if (err) -return err;break ; -default :pr_info ("invalid mode: %hu\n" , info->mode);return -EINVAL ; } + err =checkentry_lsm (info); +if (err) +return err; +if (!mode) mode = info->mode;return 0 ;
@@ -119,7 +118,7 @@ static void secmark_tg_destroy(const struct xt_tgdtor_param *par) {switch (mode) {case SECMARK_MODE_SEL : -selinux_secmark_refcount_dec (); +security_secmark_refcount_dec (); } }
security/capability.c +16 -1
@@ -681,7 +681,18 @@ static void cap_inet_conn_established(struct sock *sk, struct sk_buff *skb) { } +static int cap_secmark_relabel_packet (u32 secid) +{ +return 0 ; +} +static void cap_secmark_refcount_inc (void ) +{ +} + +static void cap_secmark_refcount_dec (void ) +{ +}static void cap_req_classify_flow (const struct request_sock *req,struct flowi *fl)
@@ -781,7 +792,8 @@ static int cap_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)static int cap_secctx_to_secid (const char *secdata,u32 seclen,u32 *secid) { -return -EOPNOTSUPP ; + *secid =0 ; +return 0 ; }static void cap_release_secctx (char *secdata,u32 seclen)
@@ -1023,6 +1035,9 @@ void __init security_fixup_ops(struct security_operations *ops)set_to_cap_if_null (ops, inet_conn_request);set_to_cap_if_null (ops, inet_csk_clone);set_to_cap_if_null (ops, inet_conn_established); +set_to_cap_if_null (ops, secmark_relabel_packet); +set_to_cap_if_null (ops, secmark_refcount_inc); +set_to_cap_if_null (ops, secmark_refcount_dec);set_to_cap_if_null (ops, req_classify_flow);set_to_cap_if_null (ops, tun_dev_create);set_to_cap_if_null (ops, tun_dev_post_create);
security/security.c +18 -0
@@ -1143,6 +1143,24 @@ void security_inet_conn_established(struct sock *sk, security_ops->inet_conn_established (sk, skb); } +int security_secmark_relabel_packet (u32 secid) +{ +return security_ops->secmark_relabel_packet (secid); +} +EXPORT_SYMBOL (security_secmark_relabel_packet); + +void security_secmark_refcount_inc (void ) +{ + security_ops->secmark_refcount_inc (); +} +EXPORT_SYMBOL (security_secmark_refcount_inc); + +void security_secmark_refcount_dec (void ) +{ + security_ops->secmark_refcount_dec (); +} +EXPORT_SYMBOL (security_secmark_refcount_dec); +int security_tun_dev_create (void ) {return security_ops->tun_dev_create ();
security/selinux/exports.c +0 -49
58 lines not on this page · raw message
security/selinux/hooks.c +24 -0
@@ -4290,6 +4290,27 @@ static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)selinux_skb_peerlbl_sid (skb, family, &sksec->peer_sid); } +static int selinux_secmark_relabel_packet (u32 sid) +{ +const struct task_security_struct *__tsec; +u32 tsid; + + __tsec =current_security (); + tsid = __tsec->sid; + +return avc_has_perm (tsid, sid,SECCLASS_PACKET ,PACKET__RELABELTO ,NULL ); +} + +static void selinux_secmark_refcount_inc (void ) +{ +atomic_inc (&selinux_secmark_refcount); +} + +static void selinux_secmark_refcount_dec (void ) +{ +atomic_dec (&selinux_secmark_refcount); +} +static void selinux_req_classify_flow (const struct request_sock *req,struct flowi *fl) {
@@ -5545,6 +5566,9 @@ static struct security_operations selinux_ops = { .inet_conn_request = selinux_inet_conn_request, .inet_csk_clone = selinux_inet_csk_clone, .inet_conn_established = selinux_inet_conn_established, + .secmark_relabel_packet = selinux_secmark_relabel_packet, + .secmark_refcount_inc = selinux_secmark_refcount_inc, + .secmark_refcount_dec = selinux_secmark_refcount_dec, .req_classify_flow = selinux_req_classify_flow, .tun_dev_create = selinux_tun_dev_create, .tun_dev_post_create = selinux_tun_dev_post_create,
security/selinux/include/security.h +1 -0
@@ -9,6 +9,7 @@#define _SELINUX_SECURITY_H_#include <linux/magic.h> +#include <linux/types.h> #include "flask.h" #define SECSID_NULL 0x00000000/* unspecified SID */
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 2/4 security: secid_to_secctx returns len when data is NULL
With the (long ago) interface change to have the secid_to_secctx functions do the string allocation instead of having the caller do the allocation we lost the ability to query the security server for the length of the upcoming string. The SECMARK code would like to allocate a netlink skb with enough length to hold the string but it is just too unclean to do the string allocation twice or to do the allocation the first time and hold onto the string and slen. This patch adds the ability to call security_secid_to_secctx() with a NULL data pointer and it will just set the slen pointer.
---
include/linux/security.h | 6 +++++- security/selinux/ss/services.c | 11 +++++++++-- security/smack/smack_lsm.c | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-)
include/linux/security.h +5 -1
@@ -1286,9 +1286,13 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)* Return 0 if permission is granted. * * @secid_to_secctx: -* Convert secid to security context. +* Convert secid to security context. If secdata is NULL the length of +* the result will be returned in seclen, but no secdata will be returned. +* This does mean that the length could change between calls to check the +* length and the next call which actually allocates and returns the secdata. * @secid contains the security ID. * @secdata contains the pointer that stores the converted security context. +* @seclen pointer which contains the length of the data * @secctx_to_secid: * Convert security context to secid. * @secid contains the pointer to the generated security ID.
security/selinux/ss/services.c +9 -2
@@ -998,7 +998,8 @@ static int context_struct_to_string(struct context *context, char **scontext, u3 {char *scontextp; - *scontext =NULL ; +if (scontext) + *scontext =NULL ; *scontext_len =0 ;if (context->len) {
@@ -1015,6 +1016,9 @@ static int context_struct_to_string(struct context *context, char **scontext, u3 *scontext_len +=strlen (sym_name (&policydb,SYM_TYPES , context->type -1 )) +1 ; *scontext_len +=mls_compute_context_len (context); +if (!scontext) +return 0 ; +/* Allocate space for the context; caller must free this space. */ scontextp =kmalloc (*scontext_len,GFP_ATOMIC );if (!scontextp)
@@ -1054,7 +1058,8 @@ static int security_sid_to_context_core(u32 sid, char **scontext,struct context *context;int rc =0 ; - *scontext =NULL ; +if (scontext) + *scontext =NULL ; *scontext_len =0 ;if (!ss_initialized) {
@@ -1062,6 +1067,8 @@ static int security_sid_to_context_core(u32 sid, char **scontext,char *scontextp; *scontext_len =strlen (initial_sid_to_string[sid]) +1 ; +if (!scontext) + goto out; scontextp =kmalloc (*scontext_len,GFP_ATOMIC );if (!scontextp) { rc = -ENOMEM ;
security/smack/smack_lsm.c +2 -1
@@ -3004,7 +3004,8 @@ static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) {char *sp =smack_from_secid (secid); - *secdata = sp; +if (secdata) + *secdata = sp; *seclen =strlen (sp);return 0 ; }
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 3/4 conntrack: export lsm context rather than internal secid via netlink
In reply to Eric ParisThe conntrack code can export the internal secid to userspace. These are dynamic, can change on lsm changes, and have no meaning in userspace. We should instead be sending lsm contexts to userspace instead. This patch sends the secctx (rather than secid) to userspace over the netlink socket. We use a new field CTA_SECCTX and stop using the the old CTA_SECMARK field since it did not send particularly useful information.
---
include/linux/netfilter/nfnetlink_conntrack.h | 10 +++++ net/netfilter/nf_conntrack_netlink.c | 46 ++++++++++++++++++++----- 2 files changed, 45 insertions(+), 11 deletions(-)
include/linux/netfilter/nfnetlink_conntrack.h +9 -1
@@ -39,8 +39,9 @@ enum ctattr_type {CTA_TUPLE_MASTER ,CTA_NAT_SEQ_ADJ_ORIG ,CTA_NAT_SEQ_ADJ_REPLY , -CTA_SECMARK , +CTA_SECMARK ,/* obsolete */ CTA_ZONE , +CTA_SECCTX , __CTA_MAX };#define CTA_MAX (__CTA_MAX - 1)
@@ -172,4 +173,11 @@ enum ctattr_help { };#define CTA_HELP_MAX (__CTA_HELP_MAX - 1) +enum ctattr_secctx { +CTA_SECCTX_UNSPEC , +CTA_SECCTX_NAME , + __CTA_SECCTX_MAX +}; +#define CTA_SECCTX_MAX (__CTA_SECCTX_MAX - 1) +#endif /* _IPCONNTRACK_NETLINK_H */
net/netfilter/nf_conntrack_netlink.c +36 -10
89 lines not on this page · raw message
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Re: [PATCH 3/4] conntrack: export lsm context rather than internal secid via netlink
Am 13.10.2010 22:24, schrieb Eric Paris:
Quoted 9 lines
The conntrack code can export the internal secid to userspace. These are dynamic, can change on lsm changes, and have no meaning in userspace. We should instead be sending lsm contexts to userspace instead. This patch sends the secctx (rather than secid) to userspace over the netlink socket. We use a new field
CTA_SECCTXand stop using the the oldCTA_SECMARKfield since it did not send particularly useful information.Signed-off-by Eric Paris <[email protected]>Reviewed-by Paul Moore <[email protected]>
PATCH 4/4 secmark: export secctx, drop secmark in procfs
In reply to Eric ParisThe current secmark code exports a secmark= field which just indicates if there is special labeling on a packet or not. We drop this field as it isn't particularly useful and instead export a new field secctx= which is the actual human readable text label.
---
.../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 28 ++++++++++++++++++-- net/netfilter/nf_conntrack_standalone.c | 28 ++++++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-)
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +25 -3
@@ -11,6 +11,7 @@#include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/percpu.h> +#include <linux/security.h> #include <net/net_namespace.h> #include <linux/netfilter.h>
@@ -87,6 +88,29 @@ static void ct_seq_stop(struct seq_file *s, void *v)rcu_read_unlock (); } +#ifdef CONFIG_NF_CONNTRACK_SECMARK +static int ct_show_secctx (struct seq_file *s,const struct nf_conn *ct) +{ +int ret; +u32 len; +char *secctx; + + ret =security_secid_to_secctx (ct->secmark, &secctx, &len); +if (ret) +return ret; + + ret =seq_printf (s,"secctx=%s " , secctx); + +security_release_secctx (secctx, len); +return ret; +} +#else +static inline int ct_show_secctx (struct seq_file *s,const struct nf_conn *ct) +{ +return 0 ; +} +#endif +static int ct_seq_show (struct seq_file *s,void *v) {struct nf_conntrack_tuple_hash *hash = v;
@@ -148,10 +172,8 @@ static int ct_seq_show(struct seq_file *s, void *v) goto release;#endif -#ifdef CONFIG_NF_CONNTRACK_SECMARK -if (seq_printf (s,"secmark=%u " , ct->secmark)) +if (ct_show_secctx (s, ct)) goto release; -#endif if (seq_printf (s,"use=%u\n" ,atomic_read (&ct->ct_general.use))) goto release;
net/netfilter/nf_conntrack_standalone.c +25 -3
@@ -15,6 +15,7 @@#include <linux/seq_file.h> #include <linux/percpu.h> #include <linux/netdevice.h> +#include <linux/security.h> #include <net/net_namespace.h> #ifdef CONFIG_SYSCTL #include <linux/sysctl.h>
@@ -108,6 +109,29 @@ static void ct_seq_stop(struct seq_file *s, void *v)rcu_read_unlock (); } +#ifdef CONFIG_NF_CONNTRACK_SECMARK +static int ct_show_secctx (struct seq_file *s,const struct nf_conn *ct) +{ +int ret; +u32 len; +char *secctx; + + ret =security_secid_to_secctx (ct->secmark, &secctx, &len); +if (ret) +return ret; + + ret =seq_printf (s,"secctx=%s " , secctx); + +security_release_secctx (secctx, len); +return ret; +} +#else +static inline int ct_show_secctx (struct seq_file *s,const struct nf_conn *ct) +{ +return 0 ; +} +#endif +/* return 0 on success, 1 in case of error */ static int ct_seq_show (struct seq_file *s,void *v) {
@@ -168,10 +192,8 @@ static int ct_seq_show(struct seq_file *s, void *v) goto release;#endif -#ifdef CONFIG_NF_CONNTRACK_SECMARK -if (seq_printf (s,"secmark=%u " , ct->secmark)) +if (ct_show_secctx (s, ct)) goto release; -#endif #ifdef CONFIG_NF_CONNTRACK_ZONES if (seq_printf (s,"zone=%u " ,nf_ct_zone (ct)))
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Re: [PATCH 4/4] secmark: export secctx, drop secmark in procfs
Am 13.10.2010 22:25, schrieb Eric Paris:
Quoted 4 lines
The current secmark code exports a secmark= field which just indicates if there is special labeling on a packet or not. We drop this field as it isn't particularly useful and instead export a new field secctx= which is the actual human readable text label.
We usually don't do this to avoid breaking parsers, however in this case the field was dependant on a config option anyways, so userspace must already be prepared that it is missing.
Re: [PATCH 4/4] secmark: export secctx, drop secmark in procfs
Quoted 9 lines · Patrick McHardy
Am 13.10.2010 22:25, schrieb Eric Paris:
Quoted 4 lines
The current secmark code exports a secmark= field which just indicates if there is special labeling on a packet or not. We drop this field as it isn't particularly useful and instead export a new field secctx= which is the actual human readable text label.
We usually don't do this to avoid breaking parsers, however in this case the field was dependant on a config option anyways, so userspace must already be prepared that it is missing.
But do we really /have/ to add more and more stuff to procfs?
Re: [PATCH 4/4] secmark: export secctx, drop secmark in procfs
Am 15.10.2010 17:25, schrieb Jan Engelhardt:
Quoted 8 lines · Patrick McHardy
Quoted 7 lines
Am 13.10.2010 22:25, schrieb Eric Paris:
Quoted 4 lines
The current secmark code exports a secmark= field which just indicates if there is special labeling on a packet or not. We drop this field as it isn't particularly useful and instead export a new field secctx= which is the actual human readable text label.
Quoted 5 lines
Quoted 3 lines
We usually don't do this to avoid breaking parsers, however in this case the field was dependant on a config option anyways, so userspace must already be prepared that it is missing.
But do we really /have/ to add more and more stuff to procfs?
As long as we don't add new files, I don't really mind.
Am 13.10.2010 22:24, schrieb Eric Paris:
Quoted 8 lines
Right now secmark has lots of direct selinux calls. Use all LSM calls and remove all SELinux specific knowledge. The only SELinux specific knowledge we leave is the mode. The only point is to make sure that other LSMs at least test this generic code before they assume it works. (They may also have to make changes if they do not represent labels as strings)
Signed-off-by Eric Paris <[email protected]>Acked-by Paul Moore <[email protected]>
I think these patches should go through the security tree.
James Morris
maintainerQuoted 7 lines · Patrick McHardy
Am 13.10.2010 22:24, schrieb Eric Paris:
Quoted 6 lines
Right now secmark has lots of direct selinux calls. Use all LSM calls and remove all SELinux specific knowledge. The only SELinux specific knowledge we leave is the mode. The only point is to make sure that other LSMs at least test this generic code before they assume it works. (They may also have to make changes if they do not represent labels as strings)
Quoted 6 lines
Quoted 2 lines
Signed-off-by Eric Paris <[email protected]>Acked-by Paul Moore <[email protected]>I think these patches should go through the security tree.
Acked-by Patrick McHardy <[email protected]>
All applied togit://git.kernel.org/pub/scm/linux/kernel/git/jmorris/security-testing-2.6#next
Signature · 7 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.