Labeled networking patches for 2.6.28
Subsystems (6)
cover Labeled networking patches for 2.6.28
Hello everyone,
Attached is a short series of patches which fixup some issues with labeled networking and add an important new feature: NetLabel address selectors. The addition of NetLabel address selectors is pretty cool because it now allows you to toggle NetLabel based labeling by both the sending domain _and_ the destination address. For example, if you were to configure the SELinux ping_t domain to send CIPSO labeled packets every packet sent by the ping_t domain would be labeled, including DNS requests (very annoying!).
# netlabelctl -p map list
Configured NetLabel domain mappings (2)
domain: "ping_t"
protocol: CIPSOv4, DOI = 1
domain: DEFAULT
protocol: UNLABELEDThis addition of address selectors now allows you to breakdown the single domain configuration by destination address. This allows you to specify different labeling configuration within the ping_t domain.
# netlabelctl -p map list
Configured NetLabel domain mappings (2)
domain: "ping_t"
address: 192.168.0.78/32
protocol: CIPSOv4, DOI = 1
address: 0.0.0.0/0
protocol: UNLABELED
domain: DEFAULT
protocol: UNLABELEDIn the example above, only packets sent to 192.168.0.78 from the ping_t domain will be labeled with a CIPSO label; everything else, i.e. 0.0.0.0/0, is unlabeled. You will also notice that the default mapping is still using the traditional (one domain, one configuration) policy, this is because you have the option to use the address selectors on a per-domain basis.
The patches below are still pretty rough, but they do work as a proof of concept that functions without any regressions under simple testing. I would ask that you give the patches a quick review and let me know if you see anything scary; patch #4 in particular makes me nervous because of the IP header manipulation. I'll send out instructions on how to configure the new bits later but I wanted to get this out now so people could look it over.
The patches are included in the lblnet-2.6_testing tree:
* git://git.infradead.org/users/pcmoore/lblnet-2.6_testing
The matching userspace changes can be found in the netlabel_tools "addrsel" branch:
* http://netlabel.svn.sf.net/viewvc/netlabel/netlabel_tools/branches/addrsel
Thanks.
Signature · 2 lines
paul moore linux @ hp
RFC PATCH 1/6 selinux: Fix a problem in security_netlbl_sid_to_secattr()
Currently when SELinux fails to allocate memory in security_netlbl_sid_to_secattr() the NetLabel LSM domain field is set to NULL which triggers the default NetLabel LSM domain mapping which may not always be the desired mapping. This patch fixes this by returning an error when the kernel is unable to allocate memory. This could result in more failures on a system with heavy memory pressure but it is the "correct" thing to do.
---
security/selinux/ss/services.c | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
security/selinux/ss/services.c +8 -2
@@ -2788,7 +2788,7 @@ netlbl_secattr_to_sid_return_cleanup:*/ int security_netlbl_sid_to_secattr (u32 sid,struct netlbl_lsm_secattr *secattr) { -int rc = -ENOENT ; +int rc;struct context *ctx;if (!ss_initialized)
@@ -2796,10 +2796,16 @@ int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)read_lock (&policy_rwlock); ctx =sidtab_search (&sidtab, sid); -if (ctx ==NULL ) +if (ctx ==NULL ) { + rc = -ENOENT ; goto netlbl_sid_to_secattr_failure; + } secattr->domain =kstrdup (policydb.p_type_val_to_name[ctx->type -1 ],GFP_ATOMIC ); +if (secattr->domain ==NULL ) { + rc = -ENOMEM ; + goto netlbl_sid_to_secattr_failure; + } secattr->flags |=NETLBL_SECATTR_DOMAIN_CPY ;mls_export_netlbl_lvl (ctx, secattr); rc =mls_export_netlbl_cat (ctx, secattr);
RFC PATCH 2/6 netlabel: Replace protocol/NetLabel linking with refrerence counts
In reply to Paul MooreNetLabel has always had a list of backpointers in the CIPSO DOI definition structure which pointed to the NetLabel LSM domain mapping structures which referenced the CIPSO DOI struct. The rationale for this was that when an administrator removed a CIPSO DOI from the system all of the associated NetLabel LSM domain mappings should be removed as well; a list of backpointers made this a simple operation.
Unfortunately, while the backpointers did make the removal easier they were a bit of a mess from an implementation point of view which was making further development difficult. Since the removal of a CIPSO DOI is a realtively rare event it seems to make sense to remove this backpointer list as the optimization was hurting us more then it was helping. However, we still need to be able to track when a CIPSO DOI definition is being used so replace the backpointer list with a reference count. In order to preserve the current functionality of removing the associated LSM domain mappings when a CIPSO DOI is removed we walk the LSM domain mapping table, removing the relevant entries.
---
include/net/cipso_ipv4.h | 8 +- include/net/netlabel.h | 2 net/ipv4/cipso_ipv4.c | 165 +++++++++--------------------------- net/netlabel/netlabel_cipso_v4.c | 46 +++++++++- net/netlabel/netlabel_domainhash.c | 93 ++++++++++---------- net/netlabel/netlabel_domainhash.h | 2 net/netlabel/netlabel_kapi.c | 30 +++---- net/netlabel/netlabel_mgmt.c | 24 +---- security/smack/smackfs.c | 15 +++ 9 files changed, 164 insertions(+), 221 deletions(-)
include/net/cipso_ipv4.h +4 -4
@@ -40,6 +40,7 @@#include <linux/net.h> #include <linux/skbuff.h> #include <net/netlabel.h> +#include <asm/atomic.h> /* known doi values */ #define CIPSO_V4_DOI_UNKNOWN 0x00000000
@@ -79,10 +80,11 @@ struct cipso_v4_doi { } map;u8 tags[CIPSO_V4_TAG_MAXCNT ]; +atomic_t refcount; +u32 valid;struct list_head list;struct rcu_head rcu; -struct list_head dom_list; };/* Standard CIPSO mapping table */
@@ -132,12 +134,10 @@ int cipso_v4_doi_remove(u32 doi,struct netlbl_audit *audit_info, void (*callback) (struct rcu_head * head));struct cipso_v4_doi *cipso_v4_doi_getdef (u32 doi); +void cipso_v4_doi_putdef (struct cipso_v4_doi *doi_def);int cipso_v4_doi_walk (u32 *skip_cnt,int (*callback) (struct cipso_v4_doi *doi_def,void *arg),void *cb_arg); -int cipso_v4_doi_domhsh_add (struct cipso_v4_doi *doi_def,const char *domain); -int cipso_v4_doi_domhsh_remove (struct cipso_v4_doi *doi_def, -const char *domain);#else static inline int cipso_v4_doi_add (struct cipso_v4_doi *doi_def) {
include/net/netlabel.h +1 -1
@@ -354,7 +354,7 @@ int netlbl_cfg_unlbl_add_map(const char *domain, struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_add (struct cipso_v4_doi *doi_def,struct netlbl_audit *audit_info); -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_del (u32 doi,struct netlbl_audit *audit_info);
net/ipv4/cipso_ipv4.c +39 -126
216 lines not on this page · raw message
net/netlabel/netlabel_cipso_v4.c +41 -5
76 lines not on this page · raw message
net/netlabel/netlabel_domainhash.c +46 -47
140 lines not on this page · raw message
net/netlabel/netlabel_domainhash.h +2 -0
@@ -61,6 +61,8 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry, struct netlbl_audit *audit_info);int netlbl_domhsh_add_default (struct netlbl_dom_map *entry,struct netlbl_audit *audit_info); +int netlbl_domhsh_remove_entry (struct netlbl_dom_map *entry, +struct netlbl_audit *audit_info);int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info);int netlbl_domhsh_remove_default (struct netlbl_audit *audit_info);struct netlbl_dom_map *netlbl_domhsh_getentry (const char *domain);
net/netlabel/netlabel_kapi.c +12 -18
@@ -147,7 +147,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def, }/** -* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping +* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI mapping * @doi_def: the DOI definition * @domain: the domain mapping to add * @audit_info: NetLabel audit information
@@ -159,7 +159,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,* failure. * */ -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info) {
@@ -175,30 +175,24 @@ int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def, goto cfg_cipsov4_add_map_failure; } entry->type =NETLBL_NLTYPE_CIPSOV4 ; - entry->type_def.cipsov4 = doi_def; - -/* Grab a RCU read lock here so nothing happens to the doi_def variable -* between adding it to the CIPSOv4 protocol engine and adding a -* domain mapping for it. */ + entry->type_def.cipsov4 =cipso_v4_doi_getdef (doi); +if (entry->type_def.cipsov4 ==NULL ) { + ret_val = -ENOENT ; + goto cfg_cipsov4_add_map_failure; + } -rcu_read_lock (); - ret_val =netlbl_cfg_cipsov4_add (doi_def, audit_info); -if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_unlock; ret_val =netlbl_domhsh_add (entry, audit_info);if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_remove_doi; -rcu_read_unlock (); + goto cfg_cipsov4_add_map_failure;return 0 ; -cfg_cipsov4_add_map_failure_remove_doi: -cipso_v4_doi_remove (doi_def->doi, audit_info, netlbl_cipsov4_doi_free); -cfg_cipsov4_add_map_failure_unlock: -rcu_read_unlock (); cfg_cipsov4_add_map_failure: -if (entry !=NULL ) +if (entry !=NULL ) { +if (entry->type_def.cipsov4 !=NULL ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);kfree (entry->domain); + }kfree (entry);return ret_val; }
net/netlabel/netlabel_mgmt.c +6 -18
@@ -122,18 +122,12 @@ static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) goto add_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto add_failure; - } ret_val =netlbl_domhsh_add (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto add_failure;
@@ -294,18 +288,12 @@ static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) goto adddef_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto adddef_failure; - } ret_val =netlbl_domhsh_add_default (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto adddef_failure;
security/smack/smackfs.c +13 -2
@@ -342,10 +342,21 @@ static void smk_cipso_doi(void)for (rc =1 ; rc <CIPSO_V4_TAG_MAXCNT ; rc++) doip->tags[rc] =CIPSO_V4_TAG_INVALID ; - rc =netlbl_cfg_cipsov4_add_map (doip,NULL , &audit_info); -if (rc !=0 ) + rc =netlbl_cfg_cipsov4_add (doip, &audit_info); +if (rc !=0 ) {printk (KERN_WARNING "%s:%d add rc = %d\n" , + __func__, __LINE__, rc); + goto err; + } + rc =netlbl_cfg_cipsov4_add_map (doip->doi,NULL , &audit_info); +if (rc !=0 ) { +printk (KERN_WARNING "%s:%d add_map rc = %d\n" , __func__, __LINE__, rc); + goto err; + } + +err: +kfree (doip); }/**
Re: [RFC PATCH v1 2/6] netlabel: Replace protocol/NetLabel linking with refrerence counts
Quoted 7 lines · Paul Moore
NetLabel has always had a list of backpointers in the CIPSO DOI definition structure which pointed to the NetLabel LSM domain mapping structures which referenced the CIPSO DOI struct. The rationale for this was that when an administrator removed a CIPSO DOI from the system all of the associated NetLabel LSM domain mappings should be removed as well; a list of backpointers made this a simple operation.
Quoted 10 lines
Unfortunately, while the backpointers did make the removal easier they were a bit of a mess from an implementation point of view which was making further development difficult. Since the removal of a CIPSO DOI is a realtively rare event it seems to make sense to remove this backpointer list as the optimization was hurting us more then it was helping. However, we still need to be able to track when a CIPSO DOI definition is being used so replace the backpointer list with a reference count. In order to preserve the current functionality of removing the associated LSM domain mappings when a CIPSO DOI is removed we walk the LSM domain mapping table, removing the relevant entries.
I don't understand how the interplay between RCU and the reference counter works... See question below (search for empty line).
Thanx, Paul
Quoted 174 lines
Signed-off-by XXX---
include/net/cipso_ipv4.h | 8 +- include/net/netlabel.h | 2 net/ipv4/cipso_ipv4.c | 165 +++++++++--------------------------- net/netlabel/netlabel_cipso_v4.c | 46 +++++++++- net/netlabel/netlabel_domainhash.c | 93 ++++++++++---------- net/netlabel/netlabel_domainhash.h | 2 net/netlabel/netlabel_kapi.c | 30 +++---- net/netlabel/netlabel_mgmt.c | 24 +---- security/smack/smackfs.c | 15 +++ 9 files changed, 164 insertions(+), 221 deletions(-)include/net/cipso_ipv4.h +4
-4@@ -40,6 +40,7 @@#include <linux/net.h> #include <linux/skbuff.h> #include <net/netlabel.h> +#include <asm/atomic.h> /* known doi values */ #define CIPSO_V4_DOI_UNKNOWN 0x00000000@@ -79,10 +80,11 @@ struct cipso_v4_doi { } map;u8 tags[CIPSO_V4_TAG_MAXCNT ]; +atomic_t refcount; +u32 valid;struct list_head list;struct rcu_head rcu; -struct list_head dom_list; };/* Standard CIPSO mapping table */ @@ -132,12 +134,10 @@ int cipso_v4_doi_remove(u32 doi,struct netlbl_audit *audit_info, void (*callback) (struct rcu_head * head));struct cipso_v4_doi *cipso_v4_doi_getdef (u32 doi); +void cipso_v4_doi_putdef (struct cipso_v4_doi *doi_def);int cipso_v4_doi_walk (u32 *skip_cnt,int (*callback) (struct cipso_v4_doi *doi_def,void *arg),void *cb_arg); -int cipso_v4_doi_domhsh_add (struct cipso_v4_doi *doi_def,const char *domain); -int cipso_v4_doi_domhsh_remove (struct cipso_v4_doi *doi_def, -const char *domain);#else static inline int cipso_v4_doi_add (struct cipso_v4_doi *doi_def) {include/net/netlabel.h +1
-1@@ -354,7 +354,7 @@ int netlbl_cfg_unlbl_add_map(const char *domain, struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_add (struct cipso_v4_doi *doi_def,struct netlbl_audit *audit_info); -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_del (u32 doi,struct netlbl_audit *audit_info);net/ipv4/cipso_ipv4.c +16
-46@@ -47,17 +47,7 @@#include <asm/bug.h> #include <asm/unaligned.h> -struct cipso_v4_domhsh_entry { -char *domain; -u32 valid; -struct list_head list; -struct rcu_head rcu; -}; -/* List of available DOI definitions */ -/* XXX - Updates should be minimal so having a single lock for the -* cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be -* okay. */ /* XXX - This currently assumes a minimal number of different DOIs in use, * if in practice there are a lot of different DOIs this list should * probably be turned into a hash table or something similar so we @@ -194,25 +184,6 @@ static void cipso_v4_bitmap_setbit(unsigned char *bitmap, }/** -* cipso_v4_doi_domhsh_free - Frees a domain list entry -* @entry: the entry's RCU field -* -* Description: -* This function is designed to be used as a callback to the call_rcu() -* function so that the memory allocated to a domain list entry can be released -* safely. -* -*/ -static void cipso_v4_doi_domhsh_free (struct rcu_head *entry) -{ -struct cipso_v4_domhsh_entry *ptr; - - ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu); -kfree (ptr->domain); -kfree (ptr); -} - -/** * cipso_v4_cache_entry_free - Frees a cache entry * @entry: the entry to free * @@ -502,8 +473,8 @@ int cipso_v4_doi_add(struct cipso_v4_doi *doi_def) } doi_def->valid =1 ; +atomic_set (&doi_def->refcount,0 );INIT_RCU_HEAD (&doi_def->rcu); -INIT_LIST_HEAD (&doi_def->dom_list);spin_lock (&cipso_v4_doi_list_lock);if (cipso_v4_doi_search (doi_def->doi) !=NULL )@@ -536,42 +507,63 @@ int cipso_v4_doi_remove(u32 doi, void (*callback) (struct rcu_head * head)) {struct cipso_v4_doi *doi_def; -struct cipso_v4_domhsh_entry *dom_iter;spin_lock (&cipso_v4_doi_list_lock); doi_def =cipso_v4_doi_search (doi); -if (doi_def !=NULL ) { - doi_def->valid =0 ; -list_del_rcu (&doi_def->list); +if (doi_def ==NULL ) {spin_unlock (&cipso_v4_doi_list_lock); -rcu_read_lock (); -list_for_each_entry_rcu (dom_iter, &doi_def->dom_list, list) -if (dom_iter->valid) -netlbl_cfg_map_del (dom_iter->domain, - audit_info); -rcu_read_unlock (); -cipso_v4_cache_invalidate (); -call_rcu (&doi_def->rcu, callback); -return 0 ; +return -ENOENT ; + } +if (atomic_read (&doi_def->refcount) >0 ) { +spin_unlock (&cipso_v4_doi_list_lock); +return -EBUSY ; } + doi_def->valid =0 ; +list_del_rcu (&doi_def->list);spin_unlock (&cipso_v4_doi_list_lock); -return -ENOENT ; +cipso_v4_cache_invalidate (); +call_rcu (&doi_def->rcu, callback); + +return 0 ; }/** -* cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition +* cipso_v4_doi_getdef - Returns a reference to a valid DOI definition * @doi: the DOI value * * Description: * Searches for a valid DOI definition and if one is found it is returned to * the caller. Otherwise NULL is returned. The caller must ensure that -* rcu_read_lock() is held while accessing the returned definition. +* rcu_read_lock() is held while accessing the returned definition and the DOI +* definition reference count is decremented when the caller is done.
net/ipv4/cipso_ipv4.c int cipso_v4_doi_remove(u32 doi, · line 548
Quoted 10 lines
net/ipv4/cipso_ipv4.c +5
-1* */ struct cipso_v4_doi *cipso_v4_doi_getdef (u32 doi) { -return cipso_v4_doi_search (doi); +struct cipso_v4_doi *doi_def; + +rcu_read_lock (); + doi_def =cipso_v4_doi_search (doi); +if (doi_def)
Suppose that the doi_def element is removed by some other CPU at this point. The reference-count check would pass (so that the deletion function would decline to error out with -EBUSY), and the removal would proceed normally. (Right?)
So we then acquire the reference count on an element that will be freed after an RCU grace period, despite the fact that the reference count might still be held at that point.
Or am I missing something? (Wouldn't be a surprise, as it is not like I am familiar with this code.)
If I am correct, the usual resolution is to combine the reference count and the "valid" flag, so that a zero reference counter implies "not valid", allowing the atomic_inc() below to become atomic_inc_not_zero(), allowing you to simply return NULL should the race with removal be detected. There are other approaches as well...
Quoted 498 lines
net/ipv4/cipso_ipv4.c +18
-79+atomic_inc (&doi_def->refcount); +rcu_read_unlock (); + +return doi_def; +} + +/** +* cipso_v4_doi_putdef - Releases a reference for the given DOI definition +* @doi_def: the DOI definition +* +* Description: +* Releases a DOI definition reference obtained from cipso_v4_doi_getdef(). +* +*/ +void cipso_v4_doi_putdef (struct cipso_v4_doi *doi_def) +{ +if (doi_def) +atomic_dec (&doi_def->refcount); }/** @@ -613,85 +605,6 @@ doi_walk_return:return ret_val; } -/** -* cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition -* @doi_def: the DOI definition -* @domain: the domain to add -* -* Description: -* Adds the @domain to the DOI specified by @doi_def, this function -* should only be called by external functions (i.e. NetLabel). This function -* does allocate memory. Returns zero on success, negative values on failure. -* -*/ -int cipso_v4_doi_domhsh_add (struct cipso_v4_doi *doi_def,const char *domain) -{ -struct cipso_v4_domhsh_entry *iter; -struct cipso_v4_domhsh_entry *new_dom; - - new_dom =kzalloc (sizeof (*new_dom),GFP_KERNEL ); -if (new_dom ==NULL ) -return -ENOMEM ; -if (domain) { - new_dom->domain =kstrdup (domain,GFP_KERNEL ); -if (new_dom->domain ==NULL ) { -kfree (new_dom); -return -ENOMEM ; - } - } - new_dom->valid =1 ; -INIT_RCU_HEAD (&new_dom->rcu); - -spin_lock (&cipso_v4_doi_list_lock); -list_for_each_entry (iter, &doi_def->dom_list, list) -if (iter->valid && - ((domain !=NULL && iter->domain !=NULL && -strcmp (iter->domain, domain) ==0 ) || - (domain ==NULL && iter->domain ==NULL ))) { -spin_unlock (&cipso_v4_doi_list_lock); -kfree (new_dom->domain); -kfree (new_dom); -return -EEXIST ; - } -list_add_tail_rcu (&new_dom->list, &doi_def->dom_list); -spin_unlock (&cipso_v4_doi_list_lock); - -return 0 ; -} - -/** -* cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition -* @doi_def: the DOI definition -* @domain: the domain to remove -* -* Description: -* Removes the @domain from the DOI specified by @doi_def, this function -* should only be called by external functions (i.e. NetLabel). Returns zero -* on success and negative values on error. -* -*/ -int cipso_v4_doi_domhsh_remove (struct cipso_v4_doi *doi_def, -const char *domain) -{ -struct cipso_v4_domhsh_entry *iter; - -spin_lock (&cipso_v4_doi_list_lock); -list_for_each_entry (iter, &doi_def->dom_list, list) -if (iter->valid && - ((domain !=NULL && iter->domain !=NULL && -strcmp (iter->domain, domain) ==0 ) || - (domain ==NULL && iter->domain ==NULL ))) { - iter->valid =0 ; -list_del_rcu (&iter->list); -spin_unlock (&cipso_v4_doi_list_lock); -call_rcu (&iter->rcu, cipso_v4_doi_domhsh_free); -return 0 ; - } -spin_unlock (&cipso_v4_doi_list_lock); - -return -ENOENT ; -} -/* * Label Mapping Functions */ net/netlabel/netlabel_cipso_v4.c +41
-5@@ -43,6 +43,7 @@#include "netlabel_user.h" #include "netlabel_cipso_v4.h" #include "netlabel_mgmt.h" +#include "netlabel_domainhash.h" /* Argument struct for cipso_v4_doi_walk() */ struct netlbl_cipsov4_doiwalk_arg {@@ -51,6 +52,12 @@ struct netlbl_cipsov4_doiwalk_arg {u32 seq; }; +/* Argument struct for netlbl_domhsh_walk() */ +struct netlbl_domhsh_walk_arg { +struct netlbl_audit *audit_info; +u32 doi; +}; +/* NetLabel Generic NETLINK CIPSOv4 family */ static struct genl_family netlbl_cipsov4_gnl_family = { .id =GENL_ID_GENERATE ,@@ -668,6 +675,29 @@ static int netlbl_cipsov4_listall(struct sk_buff *skb, }/** +* netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE +* @entry: LSM domain mapping entry +* @arg: the netlbl_domhsh_walk_arg structure +* +* Description: +* This function is intended for use by netlbl_cipsov4_remove() as the callback +* for the netlbl_domhsh_walk() function; it removes LSM domain map entries +* which are associated with the CIPSO DOI specified in @arg. Returns zero on +* success, negative values on failure. +* +*/ +static int netlbl_cipsov4_remove_cb (struct netlbl_dom_map *entry,void *arg) +{ +struct netlbl_domhsh_walk_arg *cb_arg = arg; + +if (entry->type ==NETLBL_NLTYPE_CIPSOV4 && + entry->type_def.cipsov4->doi == cb_arg->doi) +return netlbl_domhsh_remove_entry (entry, cb_arg->audit_info); + +return 0 ; +} + +/** * netlbl_cipsov4_remove - Handle a REMOVE message * @skb: the NETLINK buffer * @info: the Generic NETLINK info block @@ -681,6 +711,7 @@ static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info) {int ret_val = -EINVAL ;u32 doi =0 ; +struct netlbl_domhsh_walk_arg cb_arg;struct audit_buffer *audit_buf;struct netlbl_audit audit_info;@@ -690,11 +721,16 @@ static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info) doi =nla_get_u32 (info->attrs[NLBL_CIPSOV4_A_DOI ]);netlbl_netlink_auditinfo (skb, &audit_info); - ret_val =cipso_v4_doi_remove (doi, - &audit_info, - netlbl_cipsov4_doi_free); -if (ret_val ==0 ) -atomic_dec (&netlabel_mgmt_protocount); + cb_arg.doi = doi; + cb_arg.audit_info = &audit_info; + ret_val =netlbl_domhsh_walk (0 ,0 , netlbl_cipsov4_remove_cb, &cb_arg); +if (ret_val ==0 ) { + ret_val =cipso_v4_doi_remove (doi, + &audit_info, + netlbl_cipsov4_doi_free); +if (ret_val ==0 ) +atomic_dec (&netlabel_mgmt_protocount); + } audit_buf =netlbl_audit_start_common (AUDIT_MAC_CIPSOV4_DEL , &audit_info);net/netlabel/netlabel_domainhash.c +46
-47@@ -217,20 +217,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,u32 bkt;struct audit_buffer *audit_buf; -switch (entry->type) { -case NETLBL_NLTYPE_UNLABELED : - ret_val =0 ; -break ; -case NETLBL_NLTYPE_CIPSOV4 : - ret_val =cipso_v4_doi_domhsh_add (entry->type_def.cipsov4, - entry->domain); -break ; -default : -return -EINVAL ; - } -if (ret_val !=0 ) -return ret_val; - entry->valid =1 ;INIT_RCU_HEAD (&entry->rcu);@@ -271,16 +257,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry, }rcu_read_unlock (); -if (ret_val !=0 ) { -switch (entry->type) { -case NETLBL_NLTYPE_CIPSOV4 : -if (cipso_v4_doi_domhsh_remove (entry->type_def.cipsov4, - entry->domain) !=0 ) -BUG (); -break ; - } - } -return ret_val; }@@ -302,35 +278,26 @@ int netlbl_domhsh_add_default(struct netlbl_dom_map *entry, }/** -* netlbl_domhsh_remove - Removes an entry from the domain hash table -* @domain: the domain to remove +* netlbl_domhsh_remove_entry - Removes a given entry from the domain table +* @entry: the entry to remove * @audit_info: NetLabel audit information * * Description: * Removes an entry from the domain hash table and handles any updates to the -* lower level protocol handler (i.e. CIPSO). Returns zero on success, -* negative on failure. +* lower level protocol handler (i.e. CIPSO). Caller is responsible for +* ensuring that the RCU read lock is held. Returns zero on success, negative +* on failure. * */ -int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info) +int netlbl_domhsh_remove_entry (struct netlbl_dom_map *entry, +struct netlbl_audit *audit_info) { -int ret_val = -ENOENT ; -struct netlbl_dom_map *entry; +int ret_val =0 ;struct audit_buffer *audit_buf; -rcu_read_lock (); -if (domain) - entry =netlbl_domhsh_search (domain); -else - entry =netlbl_domhsh_search_def (domain);if (entry ==NULL ) - goto remove_return; -switch (entry->type) { -case NETLBL_NLTYPE_CIPSOV4 : -cipso_v4_doi_domhsh_remove (entry->type_def.cipsov4, - entry->domain); -break ; - } +return -ENOENT ; +spin_lock (&netlbl_domhsh_lock);if (entry->valid) { entry->valid =0 ;@@ -338,9 +305,16 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)list_del_rcu (&entry->list); elsercu_assign_pointer (netlbl_domhsh_def ,NULL ); - ret_val =0 ; - } + }else + ret_val = -ENOENT ;spin_unlock (&netlbl_domhsh_lock); +if (ret_val ==0 ) { +switch (entry->type) { +case NETLBL_NLTYPE_CIPSOV4 : +cipso_v4_doi_putdef (entry->type_def.cipsov4); +break ; + } + } audit_buf =netlbl_audit_start_common (AUDIT_MAC_MAP_DEL , audit_info);if (audit_buf !=NULL ) {@@ -351,14 +325,39 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)audit_log_end (audit_buf); } -remove_return: -rcu_read_unlock ();if (ret_val ==0 )call_rcu (&entry->rcu, netlbl_domhsh_free_entry);return ret_val; }/** +* netlbl_domhsh_remove - Removes an entry from the domain hash table +* @domain: the domain to remove +* @audit_info: NetLabel audit information +* +* Description: +* Removes an entry from the domain hash table and handles any updates to the +* lower level protocol handler (i.e. CIPSO). Returns zero on success, +* negative on failure. +* +*/ +int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info) +{ +int ret_val; +struct netlbl_dom_map *entry; + +rcu_read_lock (); +if (domain) + entry =netlbl_domhsh_search (domain); +else + entry =netlbl_domhsh_search_def (domain); + ret_val =netlbl_domhsh_remove_entry (entry, audit_info); +rcu_read_unlock (); + +return ret_val; +} + +/** * netlbl_domhsh_remove_default - Removes the default entry from the table * @audit_info: NetLabel audit information * net/netlabel/netlabel_domainhash.h +2
-0@@ -61,6 +61,8 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry, struct netlbl_audit *audit_info);int netlbl_domhsh_add_default (struct netlbl_dom_map *entry,struct netlbl_audit *audit_info); +int netlbl_domhsh_remove_entry (struct netlbl_dom_map *entry, +struct netlbl_audit *audit_info);int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info);int netlbl_domhsh_remove_default (struct netlbl_audit *audit_info);struct netlbl_dom_map *netlbl_domhsh_getentry (const char *domain);net/netlabel/netlabel_kapi.c +12
-18@@ -147,7 +147,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def, }/** -* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping +* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI mapping * @doi_def: the DOI definition * @domain: the domain mapping to add * @audit_info: NetLabel audit information @@ -159,7 +159,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,* failure. * */ -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info) {@@ -175,30 +175,24 @@ int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def, goto cfg_cipsov4_add_map_failure; } entry->type =NETLBL_NLTYPE_CIPSOV4 ; - entry->type_def.cipsov4 = doi_def; - -/* Grab a RCU read lock here so nothing happens to the doi_def variable -* between adding it to the CIPSOv4 protocol engine and adding a -* domain mapping for it. */ + entry->type_def.cipsov4 =cipso_v4_doi_getdef (doi); +if (entry->type_def.cipsov4 ==NULL ) { + ret_val = -ENOENT ; + goto cfg_cipsov4_add_map_failure; + } -rcu_read_lock (); - ret_val =netlbl_cfg_cipsov4_add (doi_def, audit_info); -if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_unlock; ret_val =netlbl_domhsh_add (entry, audit_info);if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_remove_doi; -rcu_read_unlock (); + goto cfg_cipsov4_add_map_failure;return 0 ; -cfg_cipsov4_add_map_failure_remove_doi: -cipso_v4_doi_remove (doi_def->doi, audit_info, netlbl_cipsov4_doi_free); -cfg_cipsov4_add_map_failure_unlock: -rcu_read_unlock (); cfg_cipsov4_add_map_failure: -if (entry !=NULL ) +if (entry !=NULL ) { +if (entry->type_def.cipsov4 !=NULL ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);kfree (entry->domain); + }kfree (entry);return ret_val; }net/netlabel/netlabel_mgmt.c +6
-18@@ -122,18 +122,12 @@ static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) goto add_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto add_failure; - } ret_val =netlbl_domhsh_add (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto add_failure;@@ -294,18 +288,12 @@ static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) goto adddef_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto adddef_failure; - } ret_val =netlbl_domhsh_add_default (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto adddef_failure;security/smack/smackfs.c +13
-3@@ -342,10 +342,21 @@ static void smk_cipso_doi(void)for (rc =1 ; rc <CIPSO_V4_TAG_MAXCNT ; rc++) doip->tags[rc] =CIPSO_V4_TAG_INVALID ; - rc =netlbl_cfg_cipsov4_add_map (doip,NULL , &audit_info); -if (rc !=0 ) + rc =netlbl_cfg_cipsov4_add (doip, &audit_info); +if (rc !=0 ) {printk (KERN_WARNING "%s:%d add rc = %d\n" , + __func__, __LINE__, rc); + goto err; + } + rc =netlbl_cfg_cipsov4_add_map (doip->doi,NULL , &audit_info); +if (rc !=0 ) { +printk (KERN_WARNING "%s:%d add_map rc = %d\n" , __func__, __LINE__, rc); + goto err; + } + +err: +kfree (doip); }/** -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH v1 2/6] netlabel: Replace protocol/NetLabel linking with refrerence counts
Quoted 15 lines · Paul E. McKenney
net/ipv4/cipso_ipv4.c int cipso_v4_doi_remove(u32 doi, · line 548
Quoted 8 lines · Paul Moore
struct
cipso_v4_doi*cipso_v4_doi_getdef(u32 doi) {net/ipv4/cipso_ipv4.c +5
-1-return cipso_v4_doi_search (doi); +struct cipso_v4_doi *doi_def; + +rcu_read_lock (); + doi_def =cipso_v4_doi_search (doi); +if (doi_def)Suppose that the
doi_defelement is removed by some other CPU at this point. The reference-count check would pass (so that the deletion function would decline to error out with-EBUSY), and the removal would proceed normally. (Right?)
Quoted 6 lines
So we then acquire the reference count on an element that will be freed after an RCU grace period, despite the fact that the reference count might still be held at that point.
Or am I missing something? (Wouldn't be a surprise, as it is not like I am familiar with this code.)
Hi Paul,
Thanks for taking a look, your point sounds reasonable to me.
Quoted 5 lines
If I am correct, the usual resolution is to combine the reference count and the "valid" flag, so that a zero reference counter implies "not valid", allowing the
atomic_inc()below to becomeatomic_inc_not_zero(), allowing you to simply return NULL should the race with removal be detected. There are other approaches as well...
Combining the valid and refcount fields seems reasonable to me. I took your advice and made the following changes (as well as they other changes to replace the valid check with atomic_read(refcount) > 0) ...
struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
{
struct cipso_v4_doi *doi_def;
rcu_read_lock();
doi_def = cipso_v4_doi_search(doi);
if (doi_def == NULL)
goto doi_getdef_return;
if (!atomic_inc_not_zero(&doi_def->refcount))
doi_def = NULL;doi_getdef_return:
rcu_read_unlock(); return doi_def; }
int cipso_v4_doi_remove(u32 doi,
struct netlbl_audit *audit_info, void (*callback) (struct rcu_head * head))
{
struct cipso_v4_doi *doi_def;
spin_lock(&cipso_v4_doi_list_lock);
doi_def = cipso_v4_doi_search(doi);
if (doi_def == NULL) {
spin_unlock(&cipso_v4_doi_list_lock);
return -ENOENT;
}
if (!atomic_dec_and_test(&doi_def->refcount)) {
spin_unlock(&cipso_v4_doi_list_lock);
return -EBUSY;
}
list_del_rcu(&doi_def->list);
spin_unlock(&cipso_v4_doi_list_lock);
cipso_v4_cache_invalidate();
call_rcu(&doi_def->rcu, callback);
return 0;
}Does that look better?
Signature · 2 lines
paul moore linux @ hp
Re: [RFC PATCH v1 2/6] netlabel: Replace protocol/NetLabel linking with refrerence counts
Quoted 78 lines · Paul Moore
Quoted 15 lines · Paul E. McKenney
net/ipv4/cipso_ipv4.c int cipso_v4_doi_remove(u32 doi, · line 548
Quoted 8 lines · Paul Moore
struct
cipso_v4_doi*cipso_v4_doi_getdef(u32 doi) {net/ipv4/cipso_ipv4.c +5
-1-return cipso_v4_doi_search (doi); +struct cipso_v4_doi *doi_def; + +rcu_read_lock (); + doi_def =cipso_v4_doi_search (doi); +if (doi_def)Suppose that the
doi_defelement is removed by some other CPU at this point. The reference-count check would pass (so that the deletion function would decline to error out with-EBUSY), and the removal would proceed normally. (Right?)Quoted 6 lines
So we then acquire the reference count on an element that will be freed after an RCU grace period, despite the fact that the reference count might still be held at that point.
Or am I missing something? (Wouldn't be a surprise, as it is not like I am familiar with this code.)
Hi Paul,
Thanks for taking a look, your point sounds reasonable to me.
Quoted 5 lines
If I am correct, the usual resolution is to combine the reference count and the "valid" flag, so that a zero reference counter implies "not valid", allowing the
atomic_inc()below to becomeatomic_inc_not_zero(), allowing you to simply return NULL should the race with removal be detected. There are other approaches as well...Combining the valid and refcount fields seems reasonable to me. I took your advice and made the following changes (as well as they other changes to replace the valid check with
atomic_read(refcount)> 0) ...struct
cipso_v4_doi*cipso_v4_doi_getdef(u32 doi){ struct cipso_v4_doi *doi_def; rcu_read_lock(); doi_def = cipso_v4_doi_search(doi); if (doi_def == NULL) goto doi_getdef_return; if (!atomic_inc_not_zero(&doi_def->refcount)) doi_def = NULL;
doi_getdef_return:rcu_read_unlock(); return doi_def; }int cipso_v4_doi_remove(u32 doi,
structnetlbl_audit*audit_info, void (*callback) (structrcu_head* head)){ struct cipso_v4_doi *doi_def; spin_lock(&cipso_v4_doi_list_lock); doi_def = cipso_v4_doi_search(doi); if (doi_def == NULL) { spin_unlock(&cipso_v4_doi_list_lock); return -ENOENT; } if (!atomic_dec_and_test(&doi_def->refcount)) { spin_unlock(&cipso_v4_doi_list_lock); return -EBUSY; } list_del_rcu(&doi_def->list); spin_unlock(&cipso_v4_doi_list_lock); cipso_v4_cache_invalidate(); call_rcu(&doi_def->rcu, callback); return 0; }
Quoted 1 line
Does that look better?
Much better!!!
Of course, any other places where you decrement ->refcount will also need to deal with the possibility of a zero result, right? Or is the cipso_v4_doi_remove() case the only such decrement?
Thanx, Paul
Re: [RFC PATCH v1 2/6] netlabel: Replace protocol/NetLabel linking with refrerence counts
Quoted 81 lines · Paul E. McKenney
Quoted 80 lines · Paul Moore
Quoted 15 lines · Paul E. McKenney
net/ipv4/cipso_ipv4.c int cipso_v4_doi_remove(u32 doi, · line 548
Quoted 8 lines · Paul Moore
struct
cipso_v4_doi*cipso_v4_doi_getdef(u32 doi) {net/ipv4/cipso_ipv4.c +5
-1-return cipso_v4_doi_search (doi); +struct cipso_v4_doi *doi_def; + +rcu_read_lock (); + doi_def =cipso_v4_doi_search (doi); +if (doi_def)Suppose that the
doi_defelement is removed by some other CPU at this point. The reference-count check would pass (so that the deletion function would decline to error out with-EBUSY), and the removal would proceed normally. (Right?)Quoted 6 lines
So we then acquire the reference count on an element that will be freed after an RCU grace period, despite the fact that the reference count might still be held at that point.
Or am I missing something? (Wouldn't be a surprise, as it is not like I am familiar with this code.)
Hi Paul,
Thanks for taking a look, your point sounds reasonable to me.
Quoted 6 lines
If I am correct, the usual resolution is to combine the reference count and the "valid" flag, so that a zero reference counter implies "not valid", allowing the
atomic_inc()below to becomeatomic_inc_not_zero(), allowing you to simply return NULL should the race with removal be detected. There are other approaches as well...Combining the valid and refcount fields seems reasonable to me. I took your advice and made the following changes (as well as they other changes to replace the valid check with
atomic_read(refcount)Quoted 1 line
0) ...
struct
cipso_v4_doi*cipso_v4_doi_getdef(u32 doi){ struct cipso_v4_doi *doi_def; rcu_read_lock(); doi_def = cipso_v4_doi_search(doi); if (doi_def == NULL) goto doi_getdef_return; if (!atomic_inc_not_zero(&doi_def->refcount)) doi_def = NULL;
doi_getdef_return:rcu_read_unlock(); return doi_def; }int cipso_v4_doi_remove(u32 doi,
structnetlbl_audit*audit_info, void (*callback) (structrcu_head* head)){ struct cipso_v4_doi *doi_def; spin_lock(&cipso_v4_doi_list_lock); doi_def = cipso_v4_doi_search(doi); if (doi_def == NULL) { spin_unlock(&cipso_v4_doi_list_lock); return -ENOENT; } if (!atomic_dec_and_test(&doi_def->refcount)) { spin_unlock(&cipso_v4_doi_list_lock); return -EBUSY; } list_del_rcu(&doi_def->list); spin_unlock(&cipso_v4_doi_list_lock); cipso_v4_cache_invalidate(); call_rcu(&doi_def->rcu, callback); return 0; }
Quoted 7 lines
Quoted 1 line
Does that look better?
Much better!!!
Of course, any other places where you decrement ->refcount will also need to deal with the possibility of a zero result, right? Or is the
cipso_v4_doi_remove()case the only such decrement?
Yep cipso_v4_doi_putdef() needs to be fixed up too. It looks like stacked-git can send mail with a specific refid so let me see if I can reply to this thread with an updated patch ...
Signature · 7 lines
paul moore linux @ hp -- 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.
RFC PATCH 1/1 netlabel: Replace protocol/NetLabel linking with refrerence counts
In reply to Paul E. McKenneyNetLabel has always had a list of backpointers in the CIPSO DOI definition structure which pointed to the NetLabel LSM domain mapping structures which referenced the CIPSO DOI struct. The rationale for this was that when an administrator removed a CIPSO DOI from the system all of the associated NetLabel LSM domain mappings should be removed as well; a list of backpointers made this a simple operation.
Unfortunately, while the backpointers did make the removal easier they were a bit of a mess from an implementation point of view which was making further development difficult. Since the removal of a CIPSO DOI is a realtively rare event it seems to make sense to remove this backpointer list as the optimization was hurting us more then it was helping. However, we still need to be able to track when a CIPSO DOI definition is being used so replace the backpointer list with a reference count. In order to preserve the current functionality of removing the associated LSM domain mappings when a CIPSO DOI is removed we walk the LSM domain mapping table, removing the relevant entries.
---
include/net/cipso_ipv4.h | 15 +- include/net/netlabel.h | 2 net/ipv4/cipso_ipv4.c | 219 +++++++++++++----------------------- net/netlabel/netlabel_cipso_v4.c | 70 ++++++------ net/netlabel/netlabel_domainhash.c | 95 ++++++++-------- net/netlabel/netlabel_domainhash.h | 2 net/netlabel/netlabel_kapi.c | 32 ++--- net/netlabel/netlabel_mgmt.c | 24 +--- security/smack/smackfs.c | 15 ++ 9 files changed, 208 insertions(+), 266 deletions(-)
include/net/cipso_ipv4.h +5 -10
@@ -40,6 +40,7 @@#include <linux/net.h> #include <linux/skbuff.h> #include <net/netlabel.h> +#include <asm/atomic.h> /* known doi values */ #define CIPSO_V4_DOI_UNKNOWN 0x00000000
@@ -79,10 +80,9 @@ struct cipso_v4_doi { } map;u8 tags[CIPSO_V4_TAG_MAXCNT ]; -u32 valid; +atomic_t refcount;struct list_head list;struct rcu_head rcu; -struct list_head dom_list; };/* Standard CIPSO mapping table */
@@ -128,16 +128,12 @@ extern int cipso_v4_rbm_strictvalid;#ifdef CONFIG_NETLABEL int cipso_v4_doi_add (struct cipso_v4_doi *doi_def); -int cipso_v4_doi_remove (u32 doi, -struct netlbl_audit *audit_info, -void (*callback) (struct rcu_head * head)); +int cipso_v4_doi_remove (u32 doi,struct netlbl_audit *audit_info);struct cipso_v4_doi *cipso_v4_doi_getdef (u32 doi); +void cipso_v4_doi_putdef (struct cipso_v4_doi *doi_def);int cipso_v4_doi_walk (u32 *skip_cnt,int (*callback) (struct cipso_v4_doi *doi_def,void *arg),void *cb_arg); -int cipso_v4_doi_domhsh_add (struct cipso_v4_doi *doi_def,const char *domain); -int cipso_v4_doi_domhsh_remove (struct cipso_v4_doi *doi_def, -const char *domain);#else static inline int cipso_v4_doi_add (struct cipso_v4_doi *doi_def) {
@@ -145,8 +141,7 @@ static inline int cipso_v4_doi_add(struct cipso_v4_doi *doi_def) }static inline int cipso_v4_doi_remove (u32 doi, -struct netlbl_audit *audit_info, -void (*callback) (struct rcu_head * head)) +struct netlbl_audit *audit_info) {return 0 ; }
include/net/netlabel.h +1 -1
@@ -354,7 +354,7 @@ int netlbl_cfg_unlbl_add_map(const char *domain, struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_add (struct cipso_v4_doi *doi_def,struct netlbl_audit *audit_info); -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_del (u32 doi,struct netlbl_audit *audit_info);
net/ipv4/cipso_ipv4.c +82 -137
291 lines not on this page · raw message
net/netlabel/netlabel_cipso_v4.c +39 -31
106 lines not on this page · raw message
net/netlabel/netlabel_domainhash.c +47 -48
136 lines not on this page · raw message
net/netlabel/netlabel_domainhash.h +2 -0
@@ -61,6 +61,8 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry, struct netlbl_audit *audit_info);int netlbl_domhsh_add_default (struct netlbl_dom_map *entry,struct netlbl_audit *audit_info); +int netlbl_domhsh_remove_entry (struct netlbl_dom_map *entry, +struct netlbl_audit *audit_info);int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info);int netlbl_domhsh_remove_default (struct netlbl_audit *audit_info);struct netlbl_dom_map *netlbl_domhsh_getentry (const char *domain);
net/netlabel/netlabel_kapi.c +13 -19
@@ -147,7 +147,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def, }/** -* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping +* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI mapping * @doi_def: the DOI definition * @domain: the domain mapping to add * @audit_info: NetLabel audit information
@@ -159,7 +159,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,* failure. * */ -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info) {
@@ -175,30 +175,24 @@ int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def, goto cfg_cipsov4_add_map_failure; } entry->type =NETLBL_NLTYPE_CIPSOV4 ; - entry->type_def.cipsov4 = doi_def; - -/* Grab a RCU read lock here so nothing happens to the doi_def variable -* between adding it to the CIPSOv4 protocol engine and adding a -* domain mapping for it. */ + entry->type_def.cipsov4 =cipso_v4_doi_getdef (doi); +if (entry->type_def.cipsov4 ==NULL ) { + ret_val = -ENOENT ; + goto cfg_cipsov4_add_map_failure; + } -rcu_read_lock (); - ret_val =netlbl_cfg_cipsov4_add (doi_def, audit_info); -if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_unlock; ret_val =netlbl_domhsh_add (entry, audit_info);if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_remove_doi; -rcu_read_unlock (); + goto cfg_cipsov4_add_map_failure;return 0 ; -cfg_cipsov4_add_map_failure_remove_doi: -cipso_v4_doi_remove (doi_def->doi, audit_info, netlbl_cipsov4_doi_free); -cfg_cipsov4_add_map_failure_unlock: -rcu_read_unlock (); cfg_cipsov4_add_map_failure: -if (entry !=NULL ) +if (entry !=NULL ) { +if (entry->type_def.cipsov4 !=NULL ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);kfree (entry->domain); + }kfree (entry);return ret_val; }
@@ -215,7 +209,7 @@ cfg_cipsov4_add_map_failure:*/ int netlbl_cfg_cipsov4_del (u32 doi,struct netlbl_audit *audit_info) { -return cipso_v4_doi_remove (doi, audit_info, netlbl_cipsov4_doi_free); +return cipso_v4_doi_remove (doi, audit_info); }/*
net/netlabel/netlabel_mgmt.c +6 -18
@@ -122,18 +122,12 @@ static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) goto add_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto add_failure; - } ret_val =netlbl_domhsh_add (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto add_failure;
@@ -294,18 +288,12 @@ static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) goto adddef_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto adddef_failure; - } ret_val =netlbl_domhsh_add_default (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto adddef_failure;
security/smack/smackfs.c +13 -2
@@ -342,10 +342,21 @@ static void smk_cipso_doi(void)for (rc =1 ; rc <CIPSO_V4_TAG_MAXCNT ; rc++) doip->tags[rc] =CIPSO_V4_TAG_INVALID ; - rc =netlbl_cfg_cipsov4_add_map (doip,NULL , &audit_info); -if (rc !=0 ) + rc =netlbl_cfg_cipsov4_add (doip, &audit_info); +if (rc !=0 ) {printk (KERN_WARNING "%s:%d add rc = %d\n" , + __func__, __LINE__, rc); + goto err; + } + rc =netlbl_cfg_cipsov4_add_map (doip->doi,NULL , &audit_info); +if (rc !=0 ) { +printk (KERN_WARNING "%s:%d add_map rc = %d\n" , __func__, __LINE__, rc); + goto err; + } + +err: +kfree (doip); }/**
Re: [RFC PATCH] netlabel: Replace protocol/NetLabel linking with refrerence counts
Quoted 7 lines · Paul Moore
NetLabel has always had a list of backpointers in the CIPSO DOI definition structure which pointed to the NetLabel LSM domain mapping structures which referenced the CIPSO DOI struct. The rationale for this was that when an administrator removed a CIPSO DOI from the system all of the associated NetLabel LSM domain mappings should be removed as well; a list of backpointers made this a simple operation.
Quoted 10 lines
Unfortunately, while the backpointers did make the removal easier they were a bit of a mess from an implementation point of view which was making further development difficult. Since the removal of a CIPSO DOI is a realtively rare event it seems to make sense to remove this backpointer list as the optimization was hurting us more then it was helping. However, we still need to be able to track when a CIPSO DOI definition is being used so replace the backpointer list with a reference count. In order to preserve the current functionality of removing the associated LSM domain mappings when a CIPSO DOI is removed we walk the LSM domain mapping table, removing the relevant entries.
Looks good from an RCU viewpoint!
Thanx, Paul
Quoted 809 lines
Signed-off-by XXX---
include/net/cipso_ipv4.h | 15 +- include/net/netlabel.h | 2 net/ipv4/cipso_ipv4.c | 219 +++++++++++++----------------------- net/netlabel/netlabel_cipso_v4.c | 70 ++++++------ net/netlabel/netlabel_domainhash.c | 95 ++++++++-------- net/netlabel/netlabel_domainhash.h | 2 net/netlabel/netlabel_kapi.c | 32 ++--- net/netlabel/netlabel_mgmt.c | 24 +--- security/smack/smackfs.c | 15 ++ 9 files changed, 208 insertions(+), 266 deletions(-)include/net/cipso_ipv4.h +5
-10@@ -40,6 +40,7 @@#include <linux/net.h> #include <linux/skbuff.h> #include <net/netlabel.h> +#include <asm/atomic.h> /* known doi values */ #define CIPSO_V4_DOI_UNKNOWN 0x00000000@@ -79,10 +80,9 @@ struct cipso_v4_doi { } map;u8 tags[CIPSO_V4_TAG_MAXCNT ]; -u32 valid; +atomic_t refcount;struct list_head list;struct rcu_head rcu; -struct list_head dom_list; };/* Standard CIPSO mapping table */ @@ -128,16 +128,12 @@ extern int cipso_v4_rbm_strictvalid;#ifdef CONFIG_NETLABEL int cipso_v4_doi_add (struct cipso_v4_doi *doi_def); -int cipso_v4_doi_remove (u32 doi, -struct netlbl_audit *audit_info, -void (*callback) (struct rcu_head * head)); +int cipso_v4_doi_remove (u32 doi,struct netlbl_audit *audit_info);struct cipso_v4_doi *cipso_v4_doi_getdef (u32 doi); +void cipso_v4_doi_putdef (struct cipso_v4_doi *doi_def);int cipso_v4_doi_walk (u32 *skip_cnt,int (*callback) (struct cipso_v4_doi *doi_def,void *arg),void *cb_arg); -int cipso_v4_doi_domhsh_add (struct cipso_v4_doi *doi_def,const char *domain); -int cipso_v4_doi_domhsh_remove (struct cipso_v4_doi *doi_def, -const char *domain);#else static inline int cipso_v4_doi_add (struct cipso_v4_doi *doi_def) {@@ -145,8 +141,7 @@ static inline int cipso_v4_doi_add(struct cipso_v4_doi *doi_def) }static inline int cipso_v4_doi_remove (u32 doi, -struct netlbl_audit *audit_info, -void (*callback) (struct rcu_head * head)) +struct netlbl_audit *audit_info) {return 0 ; }include/net/netlabel.h +1
-1@@ -354,7 +354,7 @@ int netlbl_cfg_unlbl_add_map(const char *domain, struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_add (struct cipso_v4_doi *doi_def,struct netlbl_audit *audit_info); -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info);int netlbl_cfg_cipsov4_del (u32 doi,struct netlbl_audit *audit_info);net/ipv4/cipso_ipv4.c +82
-137@@ -47,17 +47,7 @@#include <asm/bug.h> #include <asm/unaligned.h> -struct cipso_v4_domhsh_entry { -char *domain; -u32 valid; -struct list_head list; -struct rcu_head rcu; -}; -/* List of available DOI definitions */ -/* XXX - Updates should be minimal so having a single lock for the -* cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be -* okay. */ /* XXX - This currently assumes a minimal number of different DOIs in use, * if in practice there are a lot of different DOIs this list should * probably be turned into a hash table or something similar so we @@ -194,25 +184,6 @@ static void cipso_v4_bitmap_setbit(unsigned char *bitmap, }/** -* cipso_v4_doi_domhsh_free - Frees a domain list entry -* @entry: the entry's RCU field -* -* Description: -* This function is designed to be used as a callback to the call_rcu() -* function so that the memory allocated to a domain list entry can be released -* safely. -* -*/ -static void cipso_v4_doi_domhsh_free (struct rcu_head *entry) -{ -struct cipso_v4_domhsh_entry *ptr; - - ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu); -kfree (ptr->domain); -kfree (ptr); -} - -/** * cipso_v4_cache_entry_free - Frees a cache entry * @entry: the entry to free * @@ -457,7 +428,7 @@ static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)struct cipso_v4_doi *iter;list_for_each_entry_rcu (iter, &cipso_v4_doi_list, list) -if (iter->doi == doi && iter->valid) +if (iter->doi == doi &&atomic_read (&iter->refcount))return iter;return NULL ; }@@ -501,9 +472,8 @@ int cipso_v4_doi_add(struct cipso_v4_doi *doi_def) } } - doi_def->valid =1 ; +atomic_set (&doi_def->refcount,1 );INIT_RCU_HEAD (&doi_def->rcu); -INIT_LIST_HEAD (&doi_def->dom_list);spin_lock (&cipso_v4_doi_list_lock);if (cipso_v4_doi_search (doi_def->doi) !=NULL )@@ -519,59 +489,113 @@ doi_add_failure: }/** +* cipso_v4_doi_free - Frees a DOI definition +* @entry: the entry's RCU field +* +* Description: +* This function is designed to be used as a callback to the call_rcu() +* function so that the memory allocated to the DOI definition can be released +* safely. +* +*/ +static void cipso_v4_doi_free (struct rcu_head *entry) +{ +struct cipso_v4_doi *ptr; + + ptr = container_of(entry, struct cipso_v4_doi, rcu); +switch (ptr->type) { +case CIPSO_V4_MAP_STD : +kfree (ptr->map.std->lvl.cipso); +kfree (ptr->map.std->lvl.local); +kfree (ptr->map.std->cat.cipso); +kfree (ptr->map.std->cat.local); +break ; + } +kfree (ptr); +} + +/** * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine * @doi: the DOI value * @audit_secid: the LSM secid to use in the audit message -* @callback: the DOI cleanup/free callback * * Description: -* Removes a DOI definition from the CIPSO engine, @callback is called to -* free any memory. The NetLabel routines will be called to release their own -* LSM domain mappings as well as our own domain list. Returns zero on -* success and negative values on failure. +* Removes a DOI definition from the CIPSO engine. The NetLabel routines will +* be called to release their own LSM domain mappings as well as our own +* domain list. Returns zero on success and negative values on failure. * */ -int cipso_v4_doi_remove (u32 doi, -struct netlbl_audit *audit_info, -void (*callback) (struct rcu_head * head)) +int cipso_v4_doi_remove (u32 doi,struct netlbl_audit *audit_info) {struct cipso_v4_doi *doi_def; -struct cipso_v4_domhsh_entry *dom_iter;spin_lock (&cipso_v4_doi_list_lock); doi_def =cipso_v4_doi_search (doi); -if (doi_def !=NULL ) { - doi_def->valid =0 ; -list_del_rcu (&doi_def->list); +if (doi_def ==NULL ) {spin_unlock (&cipso_v4_doi_list_lock); -rcu_read_lock (); -list_for_each_entry_rcu (dom_iter, &doi_def->dom_list, list) -if (dom_iter->valid) -netlbl_cfg_map_del (dom_iter->domain, - audit_info); -rcu_read_unlock (); -cipso_v4_cache_invalidate (); -call_rcu (&doi_def->rcu, callback); -return 0 ; +return -ENOENT ; + } +if (!atomic_dec_and_test (&doi_def->refcount)) { +spin_unlock (&cipso_v4_doi_list_lock); +return -EBUSY ; } +list_del_rcu (&doi_def->list);spin_unlock (&cipso_v4_doi_list_lock); -return -ENOENT ; +cipso_v4_cache_invalidate (); +call_rcu (&doi_def->rcu, cipso_v4_doi_free); + +return 0 ; }/** -* cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition +* cipso_v4_doi_getdef - Returns a reference to a valid DOI definition * @doi: the DOI value * * Description: * Searches for a valid DOI definition and if one is found it is returned to * the caller. Otherwise NULL is returned. The caller must ensure that -* rcu_read_lock() is held while accessing the returned definition. +* rcu_read_lock() is held while accessing the returned definition and the DOI +* definition reference count is decremented when the caller is done. * */ struct cipso_v4_doi *cipso_v4_doi_getdef (u32 doi) { -return cipso_v4_doi_search (doi); +struct cipso_v4_doi *doi_def; + +rcu_read_lock (); + doi_def =cipso_v4_doi_search (doi); +if (doi_def ==NULL ) + goto doi_getdef_return; +if (!atomic_inc_not_zero (&doi_def->refcount)) + doi_def =NULL ; + +doi_getdef_return: +rcu_read_unlock (); +return doi_def; +} + +/** +* cipso_v4_doi_putdef - Releases a reference for the given DOI definition +* @doi_def: the DOI definition +* +* Description: +* Releases a DOI definition reference obtained from cipso_v4_doi_getdef(). +* +*/ +void cipso_v4_doi_putdef (struct cipso_v4_doi *doi_def) +{ +if (doi_def ==NULL ) +return ; + +if (!atomic_dec_and_test (&doi_def->refcount)) +return ; +spin_lock (&cipso_v4_doi_list_lock); +list_del_rcu (&doi_def->list); +spin_unlock (&cipso_v4_doi_list_lock); + +cipso_v4_cache_invalidate (); +call_rcu (&doi_def->rcu, cipso_v4_doi_free); }/** @@ -597,7 +621,7 @@ int cipso_v4_doi_walk(u32 *skip_cnt,rcu_read_lock ();list_for_each_entry_rcu (iter_doi, &cipso_v4_doi_list, list) -if (iter_doi->valid) { +if (atomic_read (&iter_doi->refcount) >0 ) {if (doi_cnt++ < *skip_cnt)continue ; ret_val =callback (iter_doi, cb_arg);@@ -613,85 +637,6 @@ doi_walk_return:return ret_val; } -/** -* cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition -* @doi_def: the DOI definition -* @domain: the domain to add -* -* Description: -* Adds the @domain to the DOI specified by @doi_def, this function -* should only be called by external functions (i.e. NetLabel). This function -* does allocate memory. Returns zero on success, negative values on failure. -* -*/ -int cipso_v4_doi_domhsh_add (struct cipso_v4_doi *doi_def,const char *domain) -{ -struct cipso_v4_domhsh_entry *iter; -struct cipso_v4_domhsh_entry *new_dom; - - new_dom =kzalloc (sizeof (*new_dom),GFP_KERNEL ); -if (new_dom ==NULL ) -return -ENOMEM ; -if (domain) { - new_dom->domain =kstrdup (domain,GFP_KERNEL ); -if (new_dom->domain ==NULL ) { -kfree (new_dom); -return -ENOMEM ; - } - } - new_dom->valid =1 ; -INIT_RCU_HEAD (&new_dom->rcu); - -spin_lock (&cipso_v4_doi_list_lock); -list_for_each_entry (iter, &doi_def->dom_list, list) -if (iter->valid && - ((domain !=NULL && iter->domain !=NULL && -strcmp (iter->domain, domain) ==0 ) || - (domain ==NULL && iter->domain ==NULL ))) { -spin_unlock (&cipso_v4_doi_list_lock); -kfree (new_dom->domain); -kfree (new_dom); -return -EEXIST ; - } -list_add_tail_rcu (&new_dom->list, &doi_def->dom_list); -spin_unlock (&cipso_v4_doi_list_lock); - -return 0 ; -} - -/** -* cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition -* @doi_def: the DOI definition -* @domain: the domain to remove -* -* Description: -* Removes the @domain from the DOI specified by @doi_def, this function -* should only be called by external functions (i.e. NetLabel). Returns zero -* on success and negative values on error. -* -*/ -int cipso_v4_doi_domhsh_remove (struct cipso_v4_doi *doi_def, -const char *domain) -{ -struct cipso_v4_domhsh_entry *iter; - -spin_lock (&cipso_v4_doi_list_lock); -list_for_each_entry (iter, &doi_def->dom_list, list) -if (iter->valid && - ((domain !=NULL && iter->domain !=NULL && -strcmp (iter->domain, domain) ==0 ) || - (domain ==NULL && iter->domain ==NULL ))) { - iter->valid =0 ; -list_del_rcu (&iter->list); -spin_unlock (&cipso_v4_doi_list_lock); -call_rcu (&iter->rcu, cipso_v4_doi_domhsh_free); -return 0 ; - } -spin_unlock (&cipso_v4_doi_list_lock); - -return -ENOENT ; -} -/* * Label Mapping Functions */ net/netlabel/netlabel_cipso_v4.c +39
-31@@ -43,6 +43,7 @@#include "netlabel_user.h" #include "netlabel_cipso_v4.h" #include "netlabel_mgmt.h" +#include "netlabel_domainhash.h" /* Argument struct for cipso_v4_doi_walk() */ struct netlbl_cipsov4_doiwalk_arg {@@ -51,6 +52,12 @@ struct netlbl_cipsov4_doiwalk_arg {u32 seq; }; +/* Argument struct for netlbl_domhsh_walk() */ +struct netlbl_domhsh_walk_arg { +struct netlbl_audit *audit_info; +u32 doi; +}; +/* NetLabel Generic NETLINK CIPSOv4 family */ static struct genl_family netlbl_cipsov4_gnl_family = { .id =GENL_ID_GENERATE ,@@ -81,32 +88,6 @@ static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1*/ /** -* netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition -* @entry: the entry's RCU field -* -* Description: -* This function is designed to be used as a callback to the call_rcu() -* function so that the memory allocated to the DOI definition can be released -* safely. -* -*/ -void netlbl_cipsov4_doi_free (struct rcu_head *entry) -{ -struct cipso_v4_doi *ptr; - - ptr = container_of(entry, struct cipso_v4_doi, rcu); -switch (ptr->type) { -case CIPSO_V4_MAP_STD : -kfree (ptr->map.std->lvl.cipso); -kfree (ptr->map.std->lvl.local); -kfree (ptr->map.std->cat.cipso); -kfree (ptr->map.std->cat.local); -break ; - } -kfree (ptr); -} - -/** * netlbl_cipsov4_add_common - Parse the common sections of a ADD message * @info: the Generic NETLINK info block * @doi_def: the CIPSO V4 DOI definition @@ -668,6 +649,29 @@ static int netlbl_cipsov4_listall(struct sk_buff *skb, }/** +* netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE +* @entry: LSM domain mapping entry +* @arg: the netlbl_domhsh_walk_arg structure +* +* Description: +* This function is intended for use by netlbl_cipsov4_remove() as the callback +* for the netlbl_domhsh_walk() function; it removes LSM domain map entries +* which are associated with the CIPSO DOI specified in @arg. Returns zero on +* success, negative values on failure. +* +*/ +static int netlbl_cipsov4_remove_cb (struct netlbl_dom_map *entry,void *arg) +{ +struct netlbl_domhsh_walk_arg *cb_arg = arg; + +if (entry->type ==NETLBL_NLTYPE_CIPSOV4 && + entry->type_def.cipsov4->doi == cb_arg->doi) +return netlbl_domhsh_remove_entry (entry, cb_arg->audit_info); + +return 0 ; +} + +/** * netlbl_cipsov4_remove - Handle a REMOVE message * @skb: the NETLINK buffer * @info: the Generic NETLINK info block @@ -681,6 +685,7 @@ static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info) {int ret_val = -EINVAL ;u32 doi =0 ; +struct netlbl_domhsh_walk_arg cb_arg;struct audit_buffer *audit_buf;struct netlbl_audit audit_info;@@ -690,11 +695,14 @@ static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info) doi =nla_get_u32 (info->attrs[NLBL_CIPSOV4_A_DOI ]);netlbl_netlink_auditinfo (skb, &audit_info); - ret_val =cipso_v4_doi_remove (doi, - &audit_info, - netlbl_cipsov4_doi_free); -if (ret_val ==0 ) -atomic_dec (&netlabel_mgmt_protocount); + cb_arg.doi = doi; + cb_arg.audit_info = &audit_info; + ret_val =netlbl_domhsh_walk (0 ,0 , netlbl_cipsov4_remove_cb, &cb_arg); +if (ret_val ==0 ) { + ret_val =cipso_v4_doi_remove (doi, &audit_info); +if (ret_val ==0 ) +atomic_dec (&netlabel_mgmt_protocount); + } audit_buf =netlbl_audit_start_common (AUDIT_MAC_CIPSOV4_DEL , &audit_info);net/netlabel/netlabel_domainhash.c +47
-48@@ -217,20 +217,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,u32 bkt;struct audit_buffer *audit_buf; -switch (entry->type) { -case NETLBL_NLTYPE_UNLABELED : - ret_val =0 ; -break ; -case NETLBL_NLTYPE_CIPSOV4 : - ret_val =cipso_v4_doi_domhsh_add (entry->type_def.cipsov4, - entry->domain); -break ; -default : -return -EINVAL ; - } -if (ret_val !=0 ) -return ret_val; - entry->valid =1 ;INIT_RCU_HEAD (&entry->rcu);@@ -271,16 +257,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry, }rcu_read_unlock (); -if (ret_val !=0 ) { -switch (entry->type) { -case NETLBL_NLTYPE_CIPSOV4 : -if (cipso_v4_doi_domhsh_remove (entry->type_def.cipsov4, - entry->domain) !=0 ) -BUG (); -break ; - } - } -return ret_val; }@@ -302,35 +278,26 @@ int netlbl_domhsh_add_default(struct netlbl_dom_map *entry, }/** -* netlbl_domhsh_remove - Removes an entry from the domain hash table -* @domain: the domain to remove +* netlbl_domhsh_remove_entry - Removes a given entry from the domain table +* @entry: the entry to remove * @audit_info: NetLabel audit information * * Description: * Removes an entry from the domain hash table and handles any updates to the -* lower level protocol handler (i.e. CIPSO). Returns zero on success, -* negative on failure. +* lower level protocol handler (i.e. CIPSO). Caller is responsible for +* ensuring that the RCU read lock is held. Returns zero on success, negative +* on failure. * */ -int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info) +int netlbl_domhsh_remove_entry (struct netlbl_dom_map *entry, +struct netlbl_audit *audit_info) { -int ret_val = -ENOENT ; -struct netlbl_dom_map *entry; +int ret_val =0 ;struct audit_buffer *audit_buf; -rcu_read_lock (); -if (domain) - entry =netlbl_domhsh_search (domain); -else - entry =netlbl_domhsh_search_def (domain);if (entry ==NULL ) - goto remove_return; -switch (entry->type) { -case NETLBL_NLTYPE_CIPSOV4 : -cipso_v4_doi_domhsh_remove (entry->type_def.cipsov4, - entry->domain); -break ; - } +return -ENOENT ; +spin_lock (&netlbl_domhsh_lock);if (entry->valid) { entry->valid =0 ;@@ -338,8 +305,8 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)list_del_rcu (&entry->list); elsercu_assign_pointer (netlbl_domhsh_def ,NULL ); - ret_val =0 ; - } + }else + ret_val = -ENOENT ;spin_unlock (&netlbl_domhsh_lock); audit_buf =netlbl_audit_start_common (AUDIT_MAC_MAP_DEL , audit_info);@@ -351,10 +318,42 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)audit_log_end (audit_buf); } -remove_return: -rcu_read_unlock (); -if (ret_val ==0 ) +if (ret_val ==0 ) { +switch (entry->type) { +case NETLBL_NLTYPE_CIPSOV4 : +cipso_v4_doi_putdef (entry->type_def.cipsov4); +break ; + }call_rcu (&entry->rcu, netlbl_domhsh_free_entry); + } + +return ret_val; +} + +/** +* netlbl_domhsh_remove - Removes an entry from the domain hash table +* @domain: the domain to remove +* @audit_info: NetLabel audit information +* +* Description: +* Removes an entry from the domain hash table and handles any updates to the +* lower level protocol handler (i.e. CIPSO). Returns zero on success, +* negative on failure. +* +*/ +int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info) +{ +int ret_val; +struct netlbl_dom_map *entry; + +rcu_read_lock (); +if (domain) + entry =netlbl_domhsh_search (domain); +else + entry =netlbl_domhsh_search_def (domain); + ret_val =netlbl_domhsh_remove_entry (entry, audit_info); +rcu_read_unlock (); +return ret_val; }net/netlabel/netlabel_domainhash.h +2
-0@@ -61,6 +61,8 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry, struct netlbl_audit *audit_info);int netlbl_domhsh_add_default (struct netlbl_dom_map *entry,struct netlbl_audit *audit_info); +int netlbl_domhsh_remove_entry (struct netlbl_dom_map *entry, +struct netlbl_audit *audit_info);int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info);int netlbl_domhsh_remove_default (struct netlbl_audit *audit_info);struct netlbl_dom_map *netlbl_domhsh_getentry (const char *domain);net/netlabel/netlabel_kapi.c +13
-19@@ -147,7 +147,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def, }/** -* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping +* netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI mapping * @doi_def: the DOI definition * @domain: the domain mapping to add * @audit_info: NetLabel audit information @@ -159,7 +159,7 @@ int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,* failure. * */ -int netlbl_cfg_cipsov4_add_map (struct cipso_v4_doi *doi_def, +int netlbl_cfg_cipsov4_add_map (u32 doi,const char *domain,struct netlbl_audit *audit_info) {@@ -175,30 +175,24 @@ int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def, goto cfg_cipsov4_add_map_failure; } entry->type =NETLBL_NLTYPE_CIPSOV4 ; - entry->type_def.cipsov4 = doi_def; - -/* Grab a RCU read lock here so nothing happens to the doi_def variable -* between adding it to the CIPSOv4 protocol engine and adding a -* domain mapping for it. */ + entry->type_def.cipsov4 =cipso_v4_doi_getdef (doi); +if (entry->type_def.cipsov4 ==NULL ) { + ret_val = -ENOENT ; + goto cfg_cipsov4_add_map_failure; + } -rcu_read_lock (); - ret_val =netlbl_cfg_cipsov4_add (doi_def, audit_info); -if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_unlock; ret_val =netlbl_domhsh_add (entry, audit_info);if (ret_val !=0 ) - goto cfg_cipsov4_add_map_failure_remove_doi; -rcu_read_unlock (); + goto cfg_cipsov4_add_map_failure;return 0 ; -cfg_cipsov4_add_map_failure_remove_doi: -cipso_v4_doi_remove (doi_def->doi, audit_info, netlbl_cipsov4_doi_free); -cfg_cipsov4_add_map_failure_unlock: -rcu_read_unlock (); cfg_cipsov4_add_map_failure: -if (entry !=NULL ) +if (entry !=NULL ) { +if (entry->type_def.cipsov4 !=NULL ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);kfree (entry->domain); + }kfree (entry);return ret_val; }@@ -215,7 +209,7 @@ cfg_cipsov4_add_map_failure:*/ int netlbl_cfg_cipsov4_del (u32 doi,struct netlbl_audit *audit_info) { -return cipso_v4_doi_remove (doi, audit_info, netlbl_cipsov4_doi_free); +return cipso_v4_doi_remove (doi, audit_info); }/* net/netlabel/netlabel_mgmt.c +6
-18@@ -122,18 +122,12 @@ static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) goto add_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto add_failure; - } ret_val =netlbl_domhsh_add (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto add_failure;@@ -294,18 +288,12 @@ static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) goto adddef_failure; tmp_val =nla_get_u32 (info->attrs[NLBL_MGMT_A_CV4DOI ]); -/* We should be holding a rcu_read_lock() here while we hold -* the result but since the entry will always be deleted when -* the CIPSO DOI is deleted we aren't going to keep the -* lock. */ -rcu_read_lock (); entry->type_def.cipsov4 =cipso_v4_doi_getdef (tmp_val); -if (entry->type_def.cipsov4 ==NULL ) { -rcu_read_unlock (); +if (entry->type_def.cipsov4 ==NULL ) goto adddef_failure; - } ret_val =netlbl_domhsh_add_default (entry, &audit_info); -rcu_read_unlock (); +if (ret_val !=0 ) +cipso_v4_doi_putdef (entry->type_def.cipsov4);break ;default : goto adddef_failure;security/smack/smackfs.c +13
-3@@ -342,10 +342,21 @@ static void smk_cipso_doi(void)for (rc =1 ; rc <CIPSO_V4_TAG_MAXCNT ; rc++) doip->tags[rc] =CIPSO_V4_TAG_INVALID ; - rc =netlbl_cfg_cipsov4_add_map (doip,NULL , &audit_info); -if (rc !=0 ) + rc =netlbl_cfg_cipsov4_add (doip, &audit_info); +if (rc !=0 ) {printk (KERN_WARNING "%s:%d add rc = %d\n" , + __func__, __LINE__, rc); + goto err; + } + rc =netlbl_cfg_cipsov4_add_map (doip->doi,NULL , &audit_info); +if (rc !=0 ) { +printk (KERN_WARNING "%s:%d add_map rc = %d\n" , __func__, __LINE__, rc); + goto err; + } + +err: +kfree (doip); }/** -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH] netlabel: Replace protocol/NetLabel linking with refrerence counts
Quoted 21 lines · Paul E. McKenney
Quoted 8 lines · Paul Moore
NetLabel has always had a list of backpointers in the CIPSO DOI definition structure which pointed to the NetLabel LSM domain mapping structures which referenced the CIPSO DOI struct. The rationale for this was that when an administrator removed a CIPSO DOI from the system all of the associated NetLabel LSM domain mappings should be removed as well; a list of backpointers made this a simple operation.
Quoted 11 lines
Unfortunately, while the backpointers did make the removal easier they were a bit of a mess from an implementation point of view which was making further development difficult. Since the removal of a CIPSO DOI is a realtively rare event it seems to make sense to remove this backpointer list as the optimization was hurting us more then it was helping. However, we still need to be able to track when a CIPSO DOI definition is being used so replace the backpointer list with a reference count. In order to preserve the current functionality of removing the associated LSM domain mappings when a CIPSO DOI is removed we walk the LSM domain mapping table, removing the relevant entries.
Quoted 1 line
Looks good from an RCU viewpoint!
Great, thanks for all your help :)
Signature · 7 lines
paul moore linux @ hp -- 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.
RFC PATCH 3/6 netlabel: Add a generic way to create ordered linked lists of network addrs
In reply to Paul MooreCreate an ordered IP address linked list mechanism similar to the core kernel's linked list construct. The idea behind this list functionality is to create an extensibile linked list ordered by IP address mask to ease the matching of network addresses. The linked list is ordered with larger address masks at the front of the list and shorter address masks at the end to facilitate overriding network entries with individual host or subnet entries.
---
net/netlabel/Makefile | 3 net/netlabel/netlabel_addrlist.c | 258 +++++++++++++++++++++++++++++ net/netlabel/netlabel_addrlist.h | 162 ++++++++++++++++++ net/netlabel/netlabel_unlabeled.c | 329 +++++++++++++------------------------ 4 files changed, 538 insertions(+), 214 deletions(-)
net/netlabel/Makefile +2 -1
@@ -5,7 +5,8 @@ # # base objects -obj-y := netlabel_user.o netlabel_kapi.o netlabel_domainhash.o +obj-y := netlabel_user.o netlabel_kapi.o +obj-y += netlabel_domainhash.o netlabel_addrlist.o # management objects obj-y += netlabel_mgmt.o
net/netlabel/netlabel_addrlist.c new file +258 -0
258 lines not on this page · raw message
net/netlabel/netlabel_addrlist.h new file +162 -0
162 lines not on this page · raw message
net/netlabel/netlabel_unlabeled.c +116 -213
529 lines not on this page · raw message
RFC PATCH 4/6 netlabel: Add network address selectors to the NetLabel/LSM domain mapping
In reply to Paul MooreThis patch extends the NetLabel traffic labeling capabilities to individual packets based not only on the LSM domain but the by the destination address as well. The changes here only affect the core NetLabel infrastructre, changes to the NetLabel KAPI and individial protocol engines are also required but are split out into a different patch to ease review.
---
include/net/netlabel.h | 5 net/netlabel/netlabel_addrlist.c | 130 ++++++++++++ net/netlabel/netlabel_addrlist.h | 15 + net/netlabel/netlabel_domainhash.c | 286 +++++++++++++++++++++++---- net/netlabel/netlabel_domainhash.h | 38 +++- net/netlabel/netlabel_kapi.c | 7 + net/netlabel/netlabel_mgmt.c | 385 +++++++++++++++++++++++++++--------- net/netlabel/netlabel_mgmt.h | 59 +++++- net/netlabel/netlabel_unlabeled.c | 96 +-------- 9 files changed, 799 insertions(+), 222 deletions(-)
include/net/netlabel.h +4 -1
@@ -72,8 +72,9 @@ struct cipso_v4_doi;/* NetLabel NETLINK protocol version * 1: initial version * 2: added static labels for unlabeled connections +* 3: network selectors added to the NetLabel/LSM domain mapping */ -#define NETLBL_PROTO_VERSION 2 +#define NETLBL_PROTO_VERSION 3/* NetLabel NETLINK types/families */ #define NETLBL_NLTYPE_NONE 0
@@ -87,6 +88,8 @@ struct cipso_v4_doi;#define NETLBL_NLTYPE_CIPSOV6_NAME "NLBL_CIPSOv6"#define NETLBL_NLTYPE_UNLABELED 5#define NETLBL_NLTYPE_UNLABELED_NAME "NLBL_UNLBL" +#define NETLBL_NLTYPE_ADDRSELECT 6 +#define NETLBL_NLTYPE_ADDRSELECT_NAME "NLBL_ADRSEL"/* * NetLabel - Kernel API for accessing the network packet label mappings.
net/netlabel/netlabel_addrlist.c +130 -0
151 lines not on this page · raw message
net/netlabel/netlabel_addrlist.h +15 -0
@@ -36,6 +36,7 @@#include <linux/rcupdate.h> #include <linux/list.h> #include <linux/in6.h> +#include <linux/audit.h> /* XXX - needs documentation */ struct netlbl_af4list {
@@ -104,6 +105,12 @@ struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,void netlbl_af4list_remove_entry (struct netlbl_af4list *entry);struct netlbl_af4list *netlbl_af4list_search (__be32 addr,struct list_head *head); +struct netlbl_af4list *netlbl_af4list_search_exact (__be32 addr, +__be32 mask, +struct list_head *head); +void netlbl_af4list_audit_addr (struct audit_buffer *audit_buf, +int src,const char *dev, +__be32 addr,__be32 mask);#if defined(CONFIG_IPV6 ) || defined(CONFIG_IPV6_MODULE )
@@ -157,6 +164,14 @@ struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,void netlbl_af6list_remove_entry (struct netlbl_af6list *entry);struct netlbl_af6list *netlbl_af6list_search (const struct in6_addr *addr,struct list_head *head); +struct netlbl_af6list *netlbl_af6list_search_exact (const struct in6_addr *addr, +const struct in6_addr *mask, +struct list_head *head); +void netlbl_af6list_audit_addr (struct audit_buffer *audit_buf, +int src, +const char *dev, +const struct in6_addr *addr, +const struct in6_addr *mask);#endif /* IPV6 */ #endif
net/netlabel/netlabel_domainhash.c +249 -37
339 lines not on this page · raw message
net/netlabel/netlabel_domainhash.h +36 -2
@@ -11,7 +11,7 @@*/ /* -* (c) Copyright Hewlett-Packard Development Company, L.P., 2006 +* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -36,16 +36,43 @@#include <linux/rcupdate.h> #include <linux/list.h> +#include "netlabel_addrlist.h" +/* Domain hash table size */ /* XXX - currently this number is an uneducated guess */ #define NETLBL_DOMHSH_BITSIZE 7 -/* Domain mapping definition struct */ +/* Domain mapping definition structures */ +#define netlbl_domhsh_addr4_entry (iter) \ + container_of(iter, struct netlbl_domaddr4_map, list) +struct netlbl_domaddr4_map { +u32 type; +union { +struct cipso_v4_doi *cipsov4; + } type_def; + +struct netlbl_af4list list; +}; +#define netlbl_domhsh_addr6_entry (iter) \ + container_of(iter, struct netlbl_domaddr6_map, list) +struct netlbl_domaddr6_map { +u32 type; + +/* NOTE: no 'type_def' union needed at present since we don't currently +* support any IPv6 labeling protocols */ + +struct netlbl_af6list list; +}; +struct netlbl_domaddr_map { +struct list_head list4; +struct list_head list6; +};struct netlbl_dom_map {char *domain;u32 type;union {struct cipso_v4_doi *cipsov4; +struct netlbl_domaddr_map *addrsel; } type_def;u32 valid;
@@ -66,9 +93,16 @@ int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,int netlbl_domhsh_remove (const char *domain,struct netlbl_audit *audit_info);int netlbl_domhsh_remove_default (struct netlbl_audit *audit_info);struct netlbl_dom_map *netlbl_domhsh_getentry (const char *domain); +struct netlbl_domaddr4_map *netlbl_domhsh_getentry_af4 (const char *domain, +__be32 addr);int netlbl_domhsh_walk (u32 *skip_bkt,u32 *skip_chain,int (*callback) (struct netlbl_dom_map *entry,void *arg),void *cb_arg); +#if defined(CONFIG_IPV6 ) || defined(CONFIG_IPV6_MODULE ) +struct netlbl_domaddr6_map *netlbl_domhsh_getentry_af6 (const char *domain, +const struct in6_addr *addr); +#endif /* IPv6 */ +#endif
net/netlabel/netlabel_kapi.c +6 -1
@@ -446,7 +446,9 @@ int netlbl_enabled(void)* Attach the correct label to the given socket using the security attributes * specified in @secattr. This function requires exclusive access to @sk, * which means it either needs to be in the process of being created or locked. -* Returns zero on success, negative values on failure. +* Returns zero on success, -EDESTADDRREQ if the domain is configured to use +* network address selectors (can't blindly label the socket), and negative +* values on all other failures. * */ int netlbl_sock_setattr(struct sock *sk,
@@ -460,6 +462,9 @@ int netlbl_sock_setattr(struct sock *sk,if (dom_entry ==NULL ) goto socket_setattr_return;switch (dom_entry->type) { +case NETLBL_NLTYPE_ADDRSELECT : + ret_val = -EDESTADDRREQ ; +break ;case NETLBL_NLTYPE_CIPSOV4 : ret_val = cipso_v4_sock_setattr(sk, dom_entry->type_def.cipsov4,
net/netlabel/netlabel_mgmt.c +292 -93
473 lines not on this page · raw message
net/netlabel/netlabel_mgmt.h +54 -5
94 lines not on this page · raw message
net/netlabel/netlabel_unlabeled.c +13 -83
126 lines not on this page · raw message
RFC PATCH 5/6 netlabel: Add functionality to set the security attributes of a packet
In reply to Paul MooreThis patch builds upon the new NetLabel address selector functionality by providing the NetLabel KAPI and CIPSO engine support needed to enable the new packet-based labeling. The only new addition to the NetLabel KAPI at this point is shown below:
* int netlbl_skbuff_setattr(skb, family, secattr)
... and is designed to be called from a Netfilter hook after the packet's IP header has been populated such as in the FORWARD or LOCAL_OUT hooks.
This patch also provides the necessary SELinux hooks to support this new functionality. Smack support is not currently included due to uncertainty regarding the permissions needed to expand the Smack network access controls.
---
include/net/cipso_ipv4.h | 16 +++ include/net/netlabel.h | 9 + net/ipv4/cipso_ipv4.c | 220 +++++++++++++++++++++++++++++------ net/netlabel/netlabel_kapi.c | 57 +++++++++ security/selinux/hooks.c | 46 +++++++ security/selinux/include/netlabel.h | 10 ++ security/selinux/include/objsec.h | 1 security/selinux/netlabel.c | 48 +++++++- 8 files changed, 367 insertions(+), 40 deletions(-)
include/net/cipso_ipv4.h +16 -0
@@ -207,6 +207,10 @@ int cipso_v4_sock_setattr(struct sock *sk,const struct cipso_v4_doi *doi_def, const struct netlbl_lsm_secattr *secattr);int cipso_v4_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr); +int cipso_v4_skbuff_setattr (struct sk_buff *skb, +const struct cipso_v4_doi *doi_def, +const struct netlbl_lsm_secattr *secattr); +int cipso_v4_skbuff_delattr (struct sk_buff *skb);int cipso_v4_skbuff_getattr (const struct sk_buff *skb,struct netlbl_lsm_secattr *secattr);int cipso_v4_validate (unsigned char **option);
@@ -231,6 +235,18 @@ static inline int cipso_v4_sock_getattr(struct sock *sk,return -ENOSYS ; } +static inline int cipso_v4_skbuff_setattr (struct sk_buff *skb, +const struct cipso_v4_doi *doi_def, +const struct netlbl_lsm_secattr *secattr) +{ +return -ENOSYS ; +} + +static inline int cipso_v4_skbuff_delattr (struct sk_buff *skb) +{ +return -ENOSYS ; +} +static inline int cipso_v4_skbuff_getattr (const struct sk_buff *skb,struct netlbl_lsm_secattr *secattr) {
include/net/netlabel.h +9 -0
@@ -385,6 +385,9 @@ int netlbl_sock_setattr(struct sock *sk, const struct netlbl_lsm_secattr *secattr);int netlbl_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr); +int netlbl_skbuff_setattr (struct sk_buff *skb, +u16 family, +const struct netlbl_lsm_secattr *secattr);int netlbl_skbuff_getattr (const struct sk_buff *skb,u16 family,struct netlbl_lsm_secattr *secattr);
@@ -464,6 +467,12 @@ static inline int netlbl_sock_getattr(struct sock *sk, {return -ENOSYS ; } +static inline int netlbl_skbuff_setattr (struct sk_buff *skb, +u16 family, +const struct netlbl_lsm_secattr *secattr) +{ +return -ENOSYS ; +}static inline int netlbl_skbuff_getattr (const struct sk_buff *skb,u16 family,struct netlbl_lsm_secattr *secattr)
net/ipv4/cipso_ipv4.c +182 -38
253 lines not on this page · raw message
net/netlabel/netlabel_kapi.c +57 -0
63 lines not on this page · raw message
security/selinux/hooks.c +45 -1
76 lines not on this page · raw message
security/selinux/include/netlabel.h +10 -0
@@ -46,6 +46,9 @@ int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,u16 family, u32 *type, u32 *sid); +int selinux_netlbl_skbuff_setsid (struct sk_buff *skb, +u16 family, +u32 sid);void selinux_netlbl_sock_graft (struct sock *sk,struct socket *sock);int selinux_netlbl_socket_post_create (struct socket *sock);
@@ -80,6 +83,13 @@ static inline int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,return 0 ; } +static inline int selinux_netlbl_skbuff_setsid (struct sk_buff *skb, + u16 family + u32 sid) +{ +return 0 ; +} +static inline void selinux_netlbl_sock_graft (struct sock *sk,struct socket *sock) {
security/selinux/include/objsec.h +1 -0
@@ -117,6 +117,7 @@ struct sk_security_struct {NLBL_UNSET =0 ,NLBL_REQUIRE ,NLBL_LABELED , +NLBL_REQSKB , } nlbl_state;#endif };
security/selinux/netlabel.c +47 -1
61 lines not on this page · raw message
RFC PATCH 6/6 selinux: Set socket NetLabel based on connection endpoint
In reply to Paul MoorePrevious work enabled the use of address based NetLabel selectors, which while highly useful, brought the potential for additional per-packet overhead when used. This patch attempts to solve that by applying NetLabel socket labels when sockets are connect()'d. This should alleviate the per-packet NetLabel labeling for all connected sockets (yes, it even works for connected DGRAM sockets).
---
include/net/cipso_ipv4.h | 5 ++ include/net/netlabel.h | 13 ++++++ net/ipv4/cipso_ipv4.c | 47 +++++++++++++++++++++++ net/netlabel/netlabel_kapi.c | 72 +++++++++++++++++++++++++++++++++++ security/selinux/hooks.c | 4 +- security/selinux/include/netlabel.h | 8 ++++ security/selinux/netlabel.c | 44 +++++++++++++++++++++ 7 files changed, 192 insertions(+), 1 deletions(-)
include/net/cipso_ipv4.h +5 -0
@@ -206,6 +206,7 @@ void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway);int cipso_v4_sock_setattr (struct sock *sk,const struct cipso_v4_doi *doi_def,const struct netlbl_lsm_secattr *secattr); +void cipso_v4_sock_delattr (struct sock *sk);int cipso_v4_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr); int cipso_v4_skbuff_setattr(struct sk_buff *skb, const struct cipso_v4_doi *doi_def,
@@ -229,6 +230,10 @@ static inline int cipso_v4_sock_setattr(struct sock *sk,return -ENOSYS ; } +static inline void cipso_v4_sock_delattr (struct sock *sk) +{ +} +static inline int cipso_v4_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr) {
include/net/netlabel.h +13 -0
@@ -383,8 +383,12 @@ int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,int netlbl_enabled (void );int netlbl_sock_setattr (struct sock *sk,const struct netlbl_lsm_secattr *secattr); +void netlbl_sock_delattr (struct sock *sk);int netlbl_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr); +int netlbl_conn_setattr (struct sock *sk, +struct sockaddr *addr, +const struct netlbl_lsm_secattr *secattr);int netlbl_skbuff_setattr (struct sk_buff *skb,u16 family,const struct netlbl_lsm_secattr *secattr);
@@ -462,11 +466,20 @@ static inline int netlbl_sock_setattr(struct sock *sk, {return -ENOSYS ; } +static inline void netlbl_sock_delattr (struct sock *sk) +{ +}static inline int netlbl_sock_getattr (struct sock *sk,struct netlbl_lsm_secattr *secattr) {return -ENOSYS ; } +static inline int netlbl_conn_setattr (struct sock *sk, +struct sockaddr *addr, +const struct netlbl_lsm_secattr *secattr) +{ +return -ENOSYS ; +}static inline int netlbl_skbuff_setattr (struct sk_buff *skb,u16 family,const struct netlbl_lsm_secattr *secattr)
net/ipv4/cipso_ipv4.c +47 -0
53 lines not on this page · raw message
net/netlabel/netlabel_kapi.c +72 -0
84 lines not on this page · raw message
security/selinux/hooks.c +3 -1
@@ -3784,6 +3784,7 @@ out:static int selinux_socket_connect (struct socket *sock,struct sockaddr *address,int addrlen) { +struct sock *sk = sock->sk;struct inode_security_struct *isec;int err;
@@ -3797,7 +3798,6 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, isec =SOCK_INODE (sock)->i_security;if (isec->sclass ==SECCLASS_TCP_SOCKET || isec->sclass ==SECCLASS_DCCP_SOCKET ) { -struct sock *sk = sock->sk;struct avc_audit_data ad;struct sockaddr_in *addr4 =NULL ;struct sockaddr_in6 *addr6 =NULL ;
@@ -3831,6 +3831,8 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, goto out; } + err =selinux_netlbl_conn_setsid (sk, address); + out:return err; }
security/selinux/include/netlabel.h +8 -0
@@ -49,6 +49,8 @@ int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,int selinux_netlbl_skbuff_setsid (struct sk_buff *skb,u16 family,u32 sid); +int selinux_netlbl_conn_setsid (struct sock *sk, +struct sockaddr *addr);void selinux_netlbl_sock_graft (struct sock *sk,struct socket *sock);int selinux_netlbl_socket_post_create (struct socket *sock);
@@ -90,6 +92,12 @@ static inline int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,return 0 ; } +static inline int selinux_netlbl_conn_setsid (struct sock *sk, +struct sockaddr *addr) +{ +return 0 ; +} +static inline void selinux_netlbl_sock_graft (struct sock *sk,struct socket *sock) {
security/selinux/netlabel.c +44 -0
50 lines not on this page · raw message
David Miller
maintainerFrom: Paul Moore <[email protected]>
Date: Fri, 08 Aug 2008 16:52:45 -0400
Quoted 2 lines
Attached is a short series of patches which fixup some issues with labeled networking and add an important new feature: NetLabel address selectors.
If relevant, please submit the bug fix parts for 2.6.27, thanks.
Quoted 8 lines · David Miller
From: Paul Moore <[email protected]>
Date: Fri, 08 Aug 2008 16:52:45 -0400Quoted 3 lines
Attached is a short series of patches which fixup some issues with labeled networking and add an important new feature: NetLabel address selectors.
If relevant, please submit the bug fix parts for 2.6.27, thanks.
Most of the "problems" I've "fixed" haven't really been bugs, but rather issues that made the new features difficult to implement. The one exception may be the first patch in the series; I've debated submitting it for 2.6.27 but I'm not sure if it is significant. Perhaps the SELinux folks could comment on this, if they think it is a good idea I'll respin that patch and push it for 2.6.27.
Signature · 2 lines
paul moore linux @ hp