Fix SECMARK target comparison
Phil Sutter
reviewercover Fix SECMARK target comparison
The kernel sets struct secmark_target_info->secid, so target comparison in user space failed every time. Given that target data comparison happens in libiptc, fixing this is a bit harder than just adding a cmp() callback to struct xtables_target. Instead, allow for targets to write the matchmask bits for their private data themselves and account for that in both legacy and nft code. Then make use of the new infrastructure to fix libxt_SECMARK.
Phil Sutter (3):
xshared: Share make_delete_mask() between ip{,6}tables
libxtables: Introduce 'matchmask' target callback
libxt_SECMARK: Fix for failing target comparison
configure.ac | 4 ++-- extensions/libxt_SECMARK.c | 10 ++++++++++ extensions/libxt_SECMARK.t | 4 ++++ include/xtables.h | 3 +++ iptables/ip6tables.c | 38 ++------------------------------------ iptables/iptables.c | 38 ++------------------------------------ iptables/nft-shared.c | 15 ++++++++++++++- iptables/xshared.c | 38 ++++++++++++++++++++++++++++++++++++++ iptables/xshared.h | 4 ++++ 9 files changed, 79 insertions(+), 75 deletions(-) create mode 100644 extensions/libxt_SECMARK.t
Signature · 1 line
2.25.1
Phil Sutter
reviewerPATCH 1/3 xshared: Share make_delete_mask() between ip{,6}tables
Function bodies were mostly identical, the only difference being the use of struct ipt_entry or ip6t_entry for size calculation. Pass this value via parameter to make them fully identical.
--- iptables/ip6tables.c | 38 ++------------------------------------ iptables/iptables.c | 38 ++------------------------------------ iptables/xshared.c | 34 ++++++++++++++++++++++++++++++++++ iptables/xshared.h | 4 ++++ 4 files changed, 42 insertions(+), 72 deletions(-)
iptables/ip6tables.c +2 -36
@@ -622,40 +622,6 @@ insert_entry(const xt_chainlabel chain,return ret; } -static unsigned char * -make_delete_mask (const struct xtables_rule_match *matches, -const struct xtables_target *target) -{ -/* Establish mask for comparison */ -unsigned int size; -const struct xtables_rule_match *matchp; -unsigned char *mask, *mptr; - - size =sizeof (struct ip6t_entry ); -for (matchp = matches; matchp; matchp = matchp->next) - size +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; - - mask =xtables_calloc (1 , size - +XT_ALIGN (sizeof (struct xt_entry_target )) - + target->size); - -memset (mask,0xFF ,sizeof (struct ip6t_entry )); - mptr = mask +sizeof (struct ip6t_entry ); - -for (matchp = matches; matchp; matchp = matchp->next) { -memset (mptr,0xFF , -XT_ALIGN (sizeof (struct xt_entry_match )) - + matchp->match->userspacesize); - mptr +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; - } - -memset (mptr,0xFF , -XT_ALIGN (sizeof (struct xt_entry_target )) - + target->userspacesize); - -return mask; -} - static int delete_entry(const xt_chainlabel chain, struct ip6t_entry *fw,
@@ -674,7 +640,7 @@ delete_entry(const xt_chainlabel chain,int ret =1 ;unsigned char *mask; - mask =make_delete_mask (matches, target); + mask =make_delete_mask (matches, target,sizeof (*fw));for (i =0 ; i < nsaddrs; i++) { fw->ipv6.src = saddrs[i]; fw->ipv6.smsk = smasks[i];
@@ -704,7 +670,7 @@ check_entry(const xt_chainlabel chain, struct ip6t_entry *fw,int ret =1 ;unsigned char *mask; - mask =make_delete_mask (matches, target); + mask =make_delete_mask (matches, target,sizeof (fw));for (i =0 ; i < nsaddrs; i++) { fw->ipv6.src = saddrs[i]; fw->ipv6.smsk = smasks[i];
iptables/iptables.c +2 -36
@@ -614,40 +614,6 @@ insert_entry(const xt_chainlabel chain,return ret; } -static unsigned char * -make_delete_mask (const struct xtables_rule_match *matches, -const struct xtables_target *target) -{ -/* Establish mask for comparison */ -unsigned int size; -const struct xtables_rule_match *matchp; -unsigned char *mask, *mptr; - - size =sizeof (struct ipt_entry ); -for (matchp = matches; matchp; matchp = matchp->next) - size +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; - - mask =xtables_calloc (1 , size - +XT_ALIGN (sizeof (struct xt_entry_target )) - + target->size); - -memset (mask,0xFF ,sizeof (struct ipt_entry )); - mptr = mask +sizeof (struct ipt_entry ); - -for (matchp = matches; matchp; matchp = matchp->next) { -memset (mptr,0xFF , -XT_ALIGN (sizeof (struct xt_entry_match )) - + matchp->match->userspacesize); - mptr +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; - } - -memset (mptr,0xFF , -XT_ALIGN (sizeof (struct xt_entry_target )) - + target->userspacesize); - -return mask; -} - static int delete_entry(const xt_chainlabel chain, struct ipt_entry *fw,
@@ -666,7 +632,7 @@ delete_entry(const xt_chainlabel chain,int ret =1 ;unsigned char *mask; - mask =make_delete_mask (matches, target); + mask =make_delete_mask (matches, target,sizeof (*fw));for (i =0 ; i < nsaddrs; i++) { fw->ip.src.s_addr = saddrs[i].s_addr; fw->ip.smsk.s_addr = smasks[i].s_addr;
@@ -696,7 +662,7 @@ check_entry(const xt_chainlabel chain, struct ipt_entry *fw,int ret =1 ;unsigned char *mask; - mask =make_delete_mask (matches, target); + mask =make_delete_mask (matches, target,sizeof (*fw));for (i =0 ; i < nsaddrs; i++) { fw->ip.src.s_addr = saddrs[i].s_addr; fw->ip.smsk.s_addr = smasks[i].s_addr;
iptables/xshared.c +34 -0
@@ -774,3 +774,37 @@ int parse_rulenumber(const char *rule)return rulenum; } + +unsigned char * +make_delete_mask (const struct xtables_rule_match *matches, +const struct xtables_target *target, +size_t entry_size) +{ +/* Establish mask for comparison */ +unsigned int size = entry_size; +const struct xtables_rule_match *matchp; +unsigned char *mask, *mptr; + +for (matchp = matches; matchp; matchp = matchp->next) + size +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; + + mask =xtables_calloc (1 , size + +XT_ALIGN (sizeof (struct xt_entry_target )) + + target->size); + +memset (mask,0xFF , entry_size); + mptr = mask + entry_size; + +for (matchp = matches; matchp; matchp = matchp->next) { +memset (mptr,0xFF , +XT_ALIGN (sizeof (struct xt_entry_match )) + + matchp->match->userspacesize); + mptr +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; + } + +memset (mptr,0xFF , +XT_ALIGN (sizeof (struct xt_entry_target )) + + target->userspacesize); + +return mask; +}
iptables/xshared.h +4 -0
@@ -216,4 +216,8 @@ void add_command(unsigned int *cmd, const int newcmd, const int othercmds, int invert);int parse_rulenumber (const char *rule); +unsigned char *make_delete_mask (const struct xtables_rule_match *matches, +const struct xtables_target *target, +size_t entry_size); +#endif /* IPTABLES_XSHARED_H */
Signature · 1 line
2.25.1
Phil Sutter
reviewerPATCH 2/3 libxtables: Introduce 'matchmask' target callback
In reply to Phil SutterIn order to support customized target data comparison, introduce a callback which sets the matchmask used by libiptc's target_same() function for the per-target part.
If defined, use this function in both legacy and nft variants.
Since this changes struct xtables_target, bump libxtables current and reset age to zero.
--- configure.ac | 4 ++-- include/xtables.h | 3 +++ iptables/nft-shared.c | 15 ++++++++++++++- iptables/xshared.c | 10 +++++++--- 4 files changed, 26 insertions(+), 6 deletions(-)
configure.ac +2 -2
@@ -2,8 +2,8 @@ AC_INIT([iptables], [1.8.4]) # See libtool.info "Libtool's versioning system" -libxtables_vcurrent=15 -libxtables_vage=3 +libxtables_vcurrent=16 +libxtables_vage=0 AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_HEADERS([config.h])
include/xtables.h +3 -0
@@ -356,6 +356,9 @@ struct xtables_target {/* Print target name or alias */ const char *(*alias)(const struct xt_entry_target *target); +/* fill matchmask of size userspacesize */ +void (*matchmask)(void *mask); +/* Pointer to list of extra command-line options */ const struct option *extra_opts;
iptables/nft-shared.c +14 -1
@@ -968,8 +968,21 @@ bool compare_targets(struct xtables_target *tg1, struct xtables_target *tg2)if (strcmp (tg1->t->u.user.name, tg2->t->u.user.name) !=0 )return false; -if (memcmp (tg1->t->data, tg2->t->data, tg1->userspacesize) !=0 ) +if (tg1->matchmask) { +char *mask =xtables_malloc (tg1->userspacesize); +int i; + + tg1->matchmask (mask); +for (i =0 ; i < tg1->userspacesize; i++) { +if ((tg1->t->data[i] ^ tg2->t->data[i]) & mask[i]) { +free (mask); +return false; + } + } +free (mask); + }else if (memcmp (tg1->t->data, tg2->t->data, tg1->userspacesize)) {return false; + }return true; }
iptables/xshared.c +7 -3
@@ -802,9 +802,13 @@ make_delete_mask(const struct xtables_rule_match *matches, mptr +=XT_ALIGN (sizeof (struct xt_entry_match )) + matchp->match->size; } -memset (mptr,0xFF , -XT_ALIGN (sizeof (struct xt_entry_target )) - + target->userspacesize); +memset (mptr,0xFF ,XT_ALIGN (sizeof (struct xt_entry_target ))); + mptr +=XT_ALIGN (sizeof (struct xt_entry_target )); + +if (target->matchmask) + target->matchmask (mptr); +else +memset (mptr,0xFF , target->userspacesize);return mask; }
Signature · 1 line
2.25.1
Phil Sutter
reviewerPATCH 3/3 libxt_SECMARK: Fix for failing target comparison
In reply to Phil SutterThe kernel fills in structxt_secmark_target_info->secid, so when the rule is received from kernel it won't match a newly created one. This prevented delete by rulespec and check commands.
Make use of newly introduced matchmask callback to prepare a mask which explicitly excludes the secid field.
--- extensions/libxt_SECMARK.c | 10 ++++++++++ extensions/libxt_SECMARK.t | 4 ++++ 2 files changed, 14 insertions(+) create mode 100644 extensions/libxt_SECMARK.t
extensions/libxt_SECMARK.c +10 -0
@@ -6,6 +6,7 @@* Copyright (C) 2006 Red Hat, Inc., James Morris <[email protected]> */ #include <stdio.h> +#include <string.h> #include <xtables.h> #include <linux/netfilter/xt_SECMARK.h>
@@ -68,6 +69,14 @@ static void SECMARK_save(const void *ip, const struct xt_entry_target *target)print_secmark (info); } +static void SECMARK_matchmask (void *mask) +{ +struct xt_secmark_target_info *info = mask; + +memset (mask,0xFF ,XT_ALIGN (sizeof (struct xt_secmark_target_info ))); + info->secid =0 ; +} +static struct xtables_target secmark_target = { .family =NFPROTO_UNSPEC , .name ="SECMARK" ,
@@ -80,6 +89,7 @@ static struct xtables_target secmark_target = { .save = SECMARK_save, .x6_parse = SECMARK_parse, .x6_options = SECMARK_opts, + .matchmask = SECMARK_matchmask, };void _init (void )
extensions/libxt_SECMARK.t new file +4 -0
@@ -0,0 +1,4 @@ +:INPUT,FORWARD,OUTPUT +*security +-j SECMARK --selctx system_u:object_r:firewalld_exec_t:s0;=;OK +-j SECMARK;;FAIL
Signature · 1 line
2.25.1
Pablo Neira Ayuso
maintainerQuoted 7 lines · Phil Sutter
The kernel sets struct
secmark_target_info->secid, so target comparison in user space failed every time. Given that target data comparison happens in libiptc, fixing this is a bit harder than just adding acmp()callback to structxtables_target. Instead, allow for targets to write the matchmask bits for their private data themselves and account for that in both legacy and nft code. Then make use of the new infrastructure to fixlibxt_SECMARK.
Hm, -D and -C with SECMARK are broken since the beginning.
Another possible would be to fix the kernel to update the layout, to get it aligned with other existing extensions.
Phil Sutter
reviewerHi Pablo,
Quoted 10 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter
The kernel sets struct
secmark_target_info->secid, so target comparison in user space failed every time. Given that target data comparison happens in libiptc, fixing this is a bit harder than just adding acmp()callback to structxtables_target. Instead, allow for targets to write the matchmask bits for their private data themselves and account for that in both legacy and nft code. Then make use of the new infrastructure to fixlibxt_SECMARK.Hm, -D and -C with SECMARK are broken since the beginning.
Yes, sadly.
Quoted 2 lines
Another possible would be to fix the kernel to update the layout, to get it aligned with other existing extensions.
You mean using 'usersize' just like e.g. xt_bpf.c?
One advantage of my fix is it works with old kernels as well.
Cheers, Phil