Skip to content

Commit d3b990b

Browse files
pcmooredavem330
authored andcommitted
netlabel: fix problems with mapping removal
This patch fixes two main problems seen when removing NetLabel mappings: memory leaks and potentially extra audit noise. The memory leaks are caused by not properly free'ing the mapping's address selector struct when free'ing the entire entry as well as not properly cleaning up a temporary mapping entry when adding new address selectors to an existing entry. This patch fixes both these problems such that kmemleak reports no NetLabel associated leaks after running the SELinux test suite. The potentially extra audit noise was caused by the auditing code in netlbl_domhsh_remove_entry() being called regardless of the entry's validity. If another thread had already marked the entry as invalid, but not removed/free'd it from the list of mappings, then it was possible that an additional mapping removal audit record would be generated. This patch fixes this by returning early from the removal function when the entry was previously marked invalid. This change also had the side benefit of improving the code by decreasing the indentation level of large chunk of code by one (accounting for most of the diffstat). Fixes: 63c4168 ("netlabel: Add network address selectors to the NetLabel/LSM domain mapping") Reported-by: Stephen Smalley <stephen.smalley.work@gmail.com> Signed-off-by: Paul Moore <paul@paul-moore.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3106ecb commit d3b990b

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

‎net/netlabel/netlabel_domainhash.c‎

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static void netlbl_domhsh_free_entry(struct rcu_head *entry)
8585
kfree(netlbl_domhsh_addr6_entry(iter6));
8686
}
8787
#endif /* IPv6 */
88+
kfree(ptr->def.addrsel);
8889
}
8990
kfree(ptr->domain);
9091
kfree(ptr);
@@ -537,6 +538,8 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,
537538
goto add_return;
538539
}
539540
#endif /* IPv6 */
541+
/* cleanup the new entry since we've moved everything over */
542+
netlbl_domhsh_free_entry(&entry->rcu);
540543
} else
541544
ret_val = -EINVAL;
542545

@@ -580,6 +583,12 @@ int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
580583
{
581584
int ret_val = 0;
582585
struct audit_buffer *audit_buf;
586+
struct netlbl_af4list *iter4;
587+
struct netlbl_domaddr4_map *map4;
588+
#if IS_ENABLED(CONFIG_IPV6)
589+
struct netlbl_af6list *iter6;
590+
struct netlbl_domaddr6_map *map6;
591+
#endif /* IPv6 */
583592

584593
if (entry == NULL)
585594
return -ENOENT;
@@ -597,6 +606,9 @@ int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
597606
ret_val = -ENOENT;
598607
spin_unlock(&netlbl_domhsh_lock);
599608

609+
if (ret_val)
610+
return ret_val;
611+
600612
audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
601613
if (audit_buf != NULL) {
602614
audit_log_format(audit_buf,
@@ -606,40 +618,29 @@ int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
606618
audit_log_end(audit_buf);
607619
}
608620

609-
if (ret_val == 0) {
610-
struct netlbl_af4list *iter4;
611-
struct netlbl_domaddr4_map *map4;
612-
#if IS_ENABLED(CONFIG_IPV6)
613-
struct netlbl_af6list *iter6;
614-
struct netlbl_domaddr6_map *map6;
615-
#endif /* IPv6 */
616-
617-
switch (entry->def.type) {
618-
case NETLBL_NLTYPE_ADDRSELECT:
619-
netlbl_af4list_foreach_rcu(iter4,
620-
&entry->def.addrsel->list4) {
621-
map4 = netlbl_domhsh_addr4_entry(iter4);
622-
cipso_v4_doi_putdef(map4->def.cipso);
623-
}
621+
switch (entry->def.type) {
622+
case NETLBL_NLTYPE_ADDRSELECT:
623+
netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
624+
map4 = netlbl_domhsh_addr4_entry(iter4);
625+
cipso_v4_doi_putdef(map4->def.cipso);
626+
}
624627
#if IS_ENABLED(CONFIG_IPV6)
625-
netlbl_af6list_foreach_rcu(iter6,
626-
&entry->def.addrsel->list6) {
627-
map6 = netlbl_domhsh_addr6_entry(iter6);
628-
calipso_doi_putdef(map6->def.calipso);
629-
}
628+
netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
629+
map6 = netlbl_domhsh_addr6_entry(iter6);
630+
calipso_doi_putdef(map6->def.calipso);
631+
}
630632
#endif /* IPv6 */
631-
break;
632-
case NETLBL_NLTYPE_CIPSOV4:
633-
cipso_v4_doi_putdef(entry->def.cipso);
634-
break;
633+
break;
634+
case NETLBL_NLTYPE_CIPSOV4:
635+
cipso_v4_doi_putdef(entry->def.cipso);
636+
break;
635637
#if IS_ENABLED(CONFIG_IPV6)
636-
case NETLBL_NLTYPE_CALIPSO:
637-
calipso_doi_putdef(entry->def.calipso);
638-
break;
638+
case NETLBL_NLTYPE_CALIPSO:
639+
calipso_doi_putdef(entry->def.calipso);
640+
break;
639641
#endif /* IPv6 */
640-
}
641-
call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
642642
}
643+
call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
643644

644645
return ret_val;
645646
}

0 commit comments

Comments
 (0)