Netfilter updates for net-next
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi David,
The following patchset contains Netfilter updates for your net-next tree, mostly from Florian Westphal to sort out the lack of sufficient validation in x_tables and connlabel preparation patches to add nf_tables support. They are:
1) Ensure we don't go over the ruleset blob boundaries in mark_source_chains().
2) Validate that target jumps land on an existing xt_entry. This extra sanitization comes with a performance penalty when loading the ruleset.
3) Introduce xt_check_entry_offsets() and use it from {arp,ip,ip6}tables.
4) Get rid of the smallish check_entry() functions in {arp,ip,ip6}tables.
5) Make sure the minimal possible target size in x_tables.
6) Similar to #3, add xt_compat_check_entry_offsets() for compat code.
7) Check that standard target size is valid.
8) More sanitization to ensure that the target_offset field is correct.
9) Add xt_check_entry_match() to validate that matches are well-formed.
10-12) Three patch to reduce the number of parameters in
translate_compat_table() for {arp,ip,ip6}tables by using a container structure.
13) No need to return value from xt_compat_match_from_user(), so make it void.
14) Consolidate translate_table() so it can be used by compat code too.
15) Remove obsolete check for compat code, so we keep consistent with what was already removed in the native layout code (back in 2007).
16) Get rid of target jump validation from mark_source_chains(), obsoleted by #2.
17) Introduce xt_copy_counters_from_user() to consolidate counter copying, and use it from {arp,ip,ip6}tables.
18,22) Get rid of unnecessary explicit inlining in ctnetlink for dump
functions.
19) Move nf_connlabel_match() to xt_connlabel.
20) Skip event notification if connlabel did not change.
21) Update of nf_connlabels_get() to make the upcoming nft connlabel support easier.
23) Remove spinlock to read protocol state field in conntrack.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thanks!
----------------------------------------------------------------
The following changes since commit 7d45a04cbc2683f9552572850f1c711d9b96dd26:
tipc: remove remnants of old broadcast code (2016-04-13 17:49:11 -0400)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git HEAD
for you to fetch changes up to a163f2cb393d9d71cad57bfe6a8c7f452a478fb4:
netfilter: conntrack: don't acquire lock during seq_printf (2016-04-19 20:26:25 +0200)
----------------------------------------------------------------
Florian Westphal (21):
netfilter: x_tables: don't move to non-existent next rule
netfilter: x_tables: validate targets of jumps
netfilter: x_tables: add and use xt_check_entry_offsets
netfilter: x_tables: kill check_entry helper
netfilter: x_tables: assert minimum target size
netfilter: x_tables: add compat version of xt_check_entry_offsets
netfilter: x_tables: check standard target size too
netfilter: x_tables: check for bogus target offset
netfilter: x_tables: validate all offsets and sizes in a rule
netfilter: ip_tables: simplify translate_compat_table args
netfilter: ip6_tables: simplify translate_compat_table args
netfilter: arp_tables: simplify translate_compat_table args
netfilter: x_tables: xt_compat_match_from_user doesn't need a retval
netfilter: x_tables: do compat validation via translate_table
netfilter: x_tables: remove obsolete overflow check for compat case too
netfilter: x_tables: remove obsolete check
netfilter: x_tables: introduce and use xt_copy_counters_from_user
netfilter: connlabels: move helpers to xt_connlabel
netfilter: labels: don't emit ct event if labels were not changed
netfilter: connlabels: change nf_connlabels_get bit arg to 'highest used'
netfilter: conntrack: don't acquire lock during seq_printf
Pablo Neira Ayuso (2):
netfilter: ctnetlink: remove unnecessary inlining
netfilter: ctnetlink: restore inlining for netlink message size calculation
include/linux/netfilter/x_tables.h | 12 +- include/net/netfilter/nf_conntrack_labels.h | 5 +- net/ipv4/netfilter/arp_tables.c | 303 ++++++++------------------ net/ipv4/netfilter/ip_tables.c | 327 ++++++++-------------------- net/ipv6/netfilter/ip6_tables.c | 320 +++++++-------------------- net/netfilter/nf_conntrack_labels.c | 44 ++-- net/netfilter/nf_conntrack_netlink.c | 119 +++++----- net/netfilter/nf_conntrack_proto_sctp.c | 8 +- net/netfilter/nf_conntrack_proto_tcp.c | 8 +- net/netfilter/nft_ct.c | 2 + net/netfilter/x_tables.c | 245 ++++++++++++++++++++- net/netfilter/xt_connlabel.c | 14 +- net/openvswitch/conntrack.c | 2 +- 13 files changed, 591 insertions(+), 818 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/23 netfilter: x_tables: don't move to non-existent next rule
From: Florian Westphal <[email protected]>
Ben Hawkes says:
In the mark_source_chains function (net/ipv4/netfilter/ip_tables.c) it is possible for a user-supplied ipt_entry structure to have a large next_offset field. This field is not bounds checked prior to writing a counter value at the supplied offset.
Base chains enforce absolute verdict.
User defined chains are supposed to end with an unconditional return, xtables userspace adds them automatically.
But if such return is missing we will move to non-existent next rule.
--- net/ipv4/netfilter/arp_tables.c | 8 +++++--- net/ipv4/netfilter/ip_tables.c | 4 ++++ net/ipv6/netfilter/ip6_tables.c | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-)
net/ipv4/netfilter/arp_tables.c +5 -3
@@ -439,6 +439,8 @@ static int mark_source_chains(const struct xt_table_info *newinfo, size = e->next_offset; e = (struct arpt_entry *) (entry0 + pos + size); +if (pos + size >= newinfo->size) +return 0 ; e->counters.pcnt = pos; pos += size; }else {
@@ -461,6 +463,8 @@ static int mark_source_chains(const struct xt_table_info *newinfo, }else {/* ... this is a fallthru */ newpos = pos + e->next_offset; +if (newpos >= newinfo->size) +return 0 ; } e = (struct arpt_entry *) (entry0 + newpos);
@@ -691,10 +695,8 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0, } } -if (!mark_source_chains (newinfo, repl->valid_hooks, entry0)) { -duprintf ("Looping hook\n" ); +if (!mark_source_chains (newinfo, repl->valid_hooks, entry0))return -ELOOP ; - }/* Finally, each sanity check must pass */ i =0 ;
net/ipv4/netfilter/ip_tables.c +4 -0
@@ -520,6 +520,8 @@ mark_source_chains(const struct xt_table_info *newinfo, size = e->next_offset; e = (struct ipt_entry *) (entry0 + pos + size); +if (pos + size >= newinfo->size) +return 0 ; e->counters.pcnt = pos; pos += size; }else {
@@ -541,6 +543,8 @@ mark_source_chains(const struct xt_table_info *newinfo, }else {/* ... this is a fallthru */ newpos = pos + e->next_offset; +if (newpos >= newinfo->size) +return 0 ; } e = (struct ipt_entry *) (entry0 + newpos);
net/ipv6/netfilter/ip6_tables.c +4 -0
@@ -532,6 +532,8 @@ mark_source_chains(const struct xt_table_info *newinfo, size = e->next_offset; e = (struct ip6t_entry *) (entry0 + pos + size); +if (pos + size >= newinfo->size) +return 0 ; e->counters.pcnt = pos; pos += size; }else {
@@ -553,6 +555,8 @@ mark_source_chains(const struct xt_table_info *newinfo, }else {/* ... this is a fallthru */ newpos = pos + e->next_offset; +if (newpos >= newinfo->size) +return 0 ; } e = (struct ip6t_entry *) (entry0 + newpos);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/23 netfilter: x_tables: validate targets of jumps
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
When we see a jump also check that the offset gets us to beginning of a rule (an ipt_entry).
The extra overhead is negible, even with absurd cases.
300k custom rules, 300k jumps to 'next' user chain:
[ plus one jump from INPUT to first userchain ]:
Before:
real 0m24.874s user 0m7.532s sys 0m16.076s
After:
real 0m27.464s user 0m7.436s sys 0m18.840s
--- net/ipv4/netfilter/arp_tables.c | 16 ++++++++++++++++ net/ipv4/netfilter/ip_tables.c | 16 ++++++++++++++++ net/ipv6/netfilter/ip6_tables.c | 16 ++++++++++++++++ 3 files changed, 48 insertions(+)
net/ipv4/netfilter/arp_tables.c +16 -0
@@ -367,6 +367,18 @@ static inline bool unconditional(const struct arpt_entry *e)memcmp (&e->arp, &uncond,sizeof (uncond)) ==0 ; } +static bool find_jump_target (const struct xt_table_info *t, +const struct arpt_entry *target) +{ +struct arpt_entry *iter; + + xt_entry_foreach(iter, t->entries, t->size) { +if (iter == target) +return true; + } +return false; +} +/* Figures out from what hook each rule can be called: returns 0 if * there are loops. Puts hook bitmask in comefrom. */
@@ -460,6 +472,10 @@ static int mark_source_chains(const struct xt_table_info *newinfo,/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos); + e = (struct arpt_entry *) + (entry0 + newpos); +if (!find_jump_target (newinfo, e)) +return 0 ; }else {/* ... this is a fallthru */ newpos = pos + e->next_offset;
net/ipv4/netfilter/ip_tables.c +16 -0
@@ -443,6 +443,18 @@ ipt_do_table(struct sk_buff *skb,#endif } +static bool find_jump_target (const struct xt_table_info *t, +const struct ipt_entry *target) +{ +struct ipt_entry *iter; + + xt_entry_foreach(iter, t->entries, t->size) { +if (iter == target) +return true; + } +return false; +} +/* Figures out from what hook each rule can be called: returns 0 if there are loops. Puts hook bitmask in comefrom. */ static int
@@ -540,6 +552,10 @@ mark_source_chains(const struct xt_table_info *newinfo,/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos); + e = (struct ipt_entry *) + (entry0 + newpos); +if (!find_jump_target (newinfo, e)) +return 0 ; }else {/* ... this is a fallthru */ newpos = pos + e->next_offset;
net/ipv6/netfilter/ip6_tables.c +16 -0
@@ -455,6 +455,18 @@ ip6t_do_table(struct sk_buff *skb,#endif } +static bool find_jump_target (const struct xt_table_info *t, +const struct ip6t_entry *target) +{ +struct ip6t_entry *iter; + + xt_entry_foreach(iter, t->entries, t->size) { +if (iter == target) +return true; + } +return false; +} +/* Figures out from what hook each rule can be called: returns 0 if there are loops. Puts hook bitmask in comefrom. */ static int
@@ -552,6 +564,10 @@ mark_source_chains(const struct xt_table_info *newinfo,/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos); + e = (struct ip6t_entry *) + (entry0 + newpos); +if (!find_jump_target (newinfo, e)) +return 0 ; }else {/* ... this is a fallthru */ newpos = pos + e->next_offset;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/23 netfilter: x_tables: add and use xt_check_entry_offsets
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Currently arp/ip and ip6tables each implement a short helper to check that the target offset is large enough to hold one xt_entry_target struct and that t->u.target_size fits within the current rule.
Unfortunately these checks are not sufficient.
To avoid adding new tests to all of ip/ip6/arptables move the current checks into a helper, then extend this helper in followup patches.
--- include/linux/netfilter/x_tables.h | 4 ++++ net/ipv4/netfilter/arp_tables.c | 11 +---------- net/ipv4/netfilter/ip_tables.c | 12 +----------- net/ipv6/netfilter/ip6_tables.c | 12 +----------- net/netfilter/x_tables.c | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 32 deletions(-)
include/linux/netfilter/x_tables.h +4 -0
@@ -242,6 +242,10 @@ void xt_unregister_match(struct xt_match *target);int xt_register_matches (struct xt_match *match,unsigned int n);void xt_unregister_matches (struct xt_match *match,unsigned int n); +int xt_check_entry_offsets (const void *base, +unsigned int target_offset, +unsigned int next_offset); +int xt_check_match (struct xt_mtchk_param *,unsigned int size,u_int8_t proto,bool inv_proto); int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
net/ipv4/netfilter/arp_tables.c +1 -10
@@ -496,19 +496,10 @@ next:static inline int check_entry (const struct arpt_entry *e) { -const struct xt_entry_target *t; -if (!arp_checkentry (&e->arp))return -EINVAL ; -if (e->target_offset +sizeof (struct xt_entry_target ) > e->next_offset) -return -EINVAL ; - - t =arpt_get_target_c (e); -if (e->target_offset + t->u.target_size > e->next_offset) -return -EINVAL ; - -return 0 ; +return xt_check_entry_offsets (e, e->target_offset, e->next_offset); }static inline int check_target (struct arpt_entry *e,const char *name)
net/ipv4/netfilter/ip_tables.c +1 -11
@@ -590,20 +590,10 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)static int check_entry (const struct ipt_entry *e) { -const struct xt_entry_target *t; -if (!ip_checkentry (&e->ip))return -EINVAL ; -if (e->target_offset +sizeof (struct xt_entry_target ) > - e->next_offset) -return -EINVAL ; - - t =ipt_get_target_c (e); -if (e->target_offset + t->u.target_size > e->next_offset) -return -EINVAL ; - -return 0 ; +return xt_check_entry_offsets (e, e->target_offset, e->next_offset); } static int
net/ipv6/netfilter/ip6_tables.c +1 -11
@@ -602,20 +602,10 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)static int check_entry (const struct ip6t_entry *e) { -const struct xt_entry_target *t; -if (!ip6_checkentry (&e->ipv6))return -EINVAL ; -if (e->target_offset +sizeof (struct xt_entry_target ) > - e->next_offset) -return -EINVAL ; - - t =ip6t_get_target_c (e); -if (e->target_offset + t->u.target_size > e->next_offset) -return -EINVAL ; - -return 0 ; +return xt_check_entry_offsets (e, e->target_offset, e->next_offset); }static int check_match (struct xt_entry_match *m,struct xt_mtchk_param *par)
net/netfilter/x_tables.c +34 -0
@@ -541,6 +541,40 @@ int xt_compat_match_to_user(const struct xt_entry_match *m,EXPORT_SYMBOL_GPL (xt_compat_match_to_user);#endif /* CONFIG_COMPAT */ +/** +* xt_check_entry_offsets - validate arp/ip/ip6t_entry +* +* @base: pointer to arp/ip/ip6t_entry +* @target_offset: the arp/ip/ip6_t->target_offset +* @next_offset: the arp/ip/ip6_t->next_offset +* +* validates that target_offset and next_offset are sane. +* +* The arp/ip/ip6t_entry structure @base must have passed following tests: +* - it must point to a valid memory location +* - base to base + next_offset must be accessible, i.e. not exceed allocated +* length. +* +* Return: 0 on success, negative errno on failure. +*/ +int xt_check_entry_offsets (const void *base, +unsigned int target_offset, +unsigned int next_offset) +{ +const struct xt_entry_target *t; +const char *e = base; + +if (target_offset +sizeof (*t) > next_offset) +return -EINVAL ; + + t = (void *)(e + target_offset); +if (target_offset + t->u.target_size > next_offset) +return -EINVAL ; + +return 0 ; +} +EXPORT_SYMBOL (xt_check_entry_offsets); +int xt_check_target (struct xt_tgchk_param *par,unsigned int size,u_int8_t proto,bool inv_proto) {
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/23 netfilter: x_tables: kill check_entry helper
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Once we add more sanity testing to xt_check_entry_offsets it becomes relvant if we're expecting a 32bit 'config_compat' blob or a normal one.
Since we already have a lot of similar-named functions (check_entry, compat_check_entry, find_and_check_entry, etc.) and the current incarnation is short just fold its contents into the callers.
--- net/ipv4/netfilter/arp_tables.c | 19 ++++++++----------- net/ipv4/netfilter/ip_tables.c | 20 ++++++++------------ net/ipv6/netfilter/ip6_tables.c | 20 ++++++++------------ 3 files changed, 24 insertions(+), 35 deletions(-)
net/ipv4/netfilter/arp_tables.c +8 -11
@@ -494,14 +494,6 @@ next:return 1 ; } -static inline int check_entry (const struct arpt_entry *e) -{ -if (!arp_checkentry (&e->arp)) -return -EINVAL ; - -return xt_check_entry_offsets (e, e->target_offset, e->next_offset); -} -static inline int check_target (struct arpt_entry *e,const char *name) {struct xt_entry_target *t =arpt_get_target (e);
@@ -597,7 +589,10 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,return -EINVAL ; } - err =check_entry (e); +if (!arp_checkentry (&e->arp)) +return -EINVAL ; + + err =xt_check_entry_offsets (e, e->target_offset, e->next_offset);if (err)return err;
@@ -1256,8 +1251,10 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,return -EINVAL ; } -/* For purposes of check_entry casting the compat entry is fine */ - ret =check_entry ((struct arpt_entry *)e); +if (!arp_checkentry (&e->arp)) +return -EINVAL ; + + ret =xt_check_entry_offsets (e, e->target_offset, e->next_offset);if (ret)return ret;
net/ipv4/netfilter/ip_tables.c +8 -12
@@ -588,15 +588,6 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net) }static int -check_entry (const struct ipt_entry *e) -{ -if (!ip_checkentry (&e->ip)) -return -EINVAL ; - -return xt_check_entry_offsets (e, e->target_offset, e->next_offset); -} - -static int check_match (struct xt_entry_match *m,struct xt_mtchk_param *par) {const struct ipt_ip *ip = par->entryinfo;
@@ -760,7 +751,10 @@ check_entry_size_and_hooks(struct ipt_entry *e,return -EINVAL ; } - err =check_entry (e); +if (!ip_checkentry (&e->ip)) +return -EINVAL ; + + err =xt_check_entry_offsets (e, e->target_offset, e->next_offset);if (err)return err;
@@ -1516,8 +1510,10 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,return -EINVAL ; } -/* For purposes of check_entry casting the compat entry is fine */ - ret =check_entry ((struct ipt_entry *)e); +if (!ip_checkentry (&e->ip)) +return -EINVAL ; + + ret =xt_check_entry_offsets (e, e->target_offset, e->next_offset);if (ret)return ret;
net/ipv6/netfilter/ip6_tables.c +8 -12
@@ -599,15 +599,6 @@ static void cleanup_match(struct xt_entry_match *m, struct net *net)module_put (par.match->me); } -static int -check_entry (const struct ip6t_entry *e) -{ -if (!ip6_checkentry (&e->ipv6)) -return -EINVAL ; - -return xt_check_entry_offsets (e, e->target_offset, e->next_offset); -} -static int check_match (struct xt_entry_match *m,struct xt_mtchk_param *par) {const struct ip6t_ip6 *ipv6 = par->entryinfo;
@@ -772,7 +763,10 @@ check_entry_size_and_hooks(struct ip6t_entry *e,return -EINVAL ; } - err =check_entry (e); +if (!ip6_checkentry (&e->ipv6)) +return -EINVAL ; + + err =xt_check_entry_offsets (e, e->target_offset, e->next_offset);if (err)return err;
@@ -1528,8 +1522,10 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,return -EINVAL ; } -/* For purposes of check_entry casting the compat entry is fine */ - ret =check_entry ((struct ip6t_entry *)e); +if (!ip6_checkentry (&e->ipv6)) +return -EINVAL ; + + ret =xt_check_entry_offsets (e, e->target_offset, e->next_offset);if (ret)return ret;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/23 netfilter: x_tables: assert minimum target size
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The target size includes the size of the xt_entry_target struct.
--- net/netfilter/x_tables.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/x_tables.c +3 -0
@@ -568,6 +568,9 @@ int xt_check_entry_offsets(const void *base,return -EINVAL ; t = (void *)(e + target_offset); +if (t->u.target_size <sizeof (*t)) +return -EINVAL ; +if (target_offset + t->u.target_size > next_offset)return -EINVAL ;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 6/23 netfilter: x_tables: add compat version of xt_check_entry_offsets
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
32bit rulesets have different layout and alignment requirements, so once more integrity checks get added to xt_check_entry_offsets it will reject well-formed 32bit rulesets.
--- include/linux/netfilter/x_tables.h | 3 +++ net/ipv4/netfilter/arp_tables.c | 3 ++- net/ipv4/netfilter/ip_tables.c | 3 ++- net/ipv6/netfilter/ip6_tables.c | 3 ++- net/netfilter/x_tables.c | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 3 deletions(-)
include/linux/netfilter/x_tables.h +3 -0
@@ -494,6 +494,9 @@ void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, unsigned int *size);int xt_compat_target_to_user (const struct xt_entry_target *t,void __user **dstptr,unsigned int *size); +int xt_compat_check_entry_offsets (const void *base, +unsigned int target_offset, +unsigned int next_offset);#endif /* CONFIG_COMPAT */ #endif /* _X_TABLES_H */
net/ipv4/netfilter/arp_tables.c +2 -1
@@ -1254,7 +1254,8 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,if (!arp_checkentry (&e->arp))return -EINVAL ; - ret =xt_check_entry_offsets (e, e->target_offset, e->next_offset); + ret =xt_compat_check_entry_offsets (e, e->target_offset, + e->next_offset);if (ret)return ret;
net/ipv4/netfilter/ip_tables.c +2 -1
@@ -1513,7 +1513,8 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,if (!ip_checkentry (&e->ip))return -EINVAL ; - ret =xt_check_entry_offsets (e, e->target_offset, e->next_offset); + ret =xt_compat_check_entry_offsets (e, + e->target_offset, e->next_offset);if (ret)return ret;
net/ipv6/netfilter/ip6_tables.c +2 -1
@@ -1525,7 +1525,8 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,if (!ip6_checkentry (&e->ipv6))return -EINVAL ; - ret =xt_check_entry_offsets (e, e->target_offset, e->next_offset); + ret =xt_compat_check_entry_offsets (e, + e->target_offset, e->next_offset);if (ret)return ret;
net/netfilter/x_tables.c +22 -0
@@ -539,6 +539,27 @@ int xt_compat_match_to_user(const struct xt_entry_match *m, return 0; }EXPORT_SYMBOL_GPL (xt_compat_match_to_user); + +int xt_compat_check_entry_offsets (const void *base, +unsigned int target_offset, +unsigned int next_offset) +{ +const struct compat_xt_entry_target *t; +const char *e = base; + +if (target_offset +sizeof (*t) > next_offset) +return -EINVAL ; + + t = (void *)(e + target_offset); +if (t->u.target_size <sizeof (*t)) +return -EINVAL ; + +if (target_offset + t->u.target_size > next_offset) +return -EINVAL ; + +return 0 ; +} +EXPORT_SYMBOL (xt_compat_check_entry_offsets);#endif /* CONFIG_COMPAT */ /**
@@ -549,6 +570,7 @@ EXPORT_SYMBOL_GPL(xt_compat_match_to_user);* @next_offset: the arp/ip/ip6_t->next_offset * * validates that target_offset and next_offset are sane. +* Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. * * The arp/ip/ip6t_entry structure @base must have passed following tests: * - it must point to a valid memory location
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 7/23 netfilter: x_tables: check standard target size too
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
We have targets and standard targets -- the latter carries a verdict.
The ip/ip6tables validation functions will access t->verdict for the standard targets to fetch the jump offset or verdict for chainloop detection, but this happens before the targets get checked/validated.
Thus we also need to check for verdict presence here, else t->verdict can point right after a blob.
Spotted with UBSAN while testing malformed blobs.
--- net/netfilter/x_tables.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
net/netfilter/x_tables.c +15 -0
@@ -540,6 +540,13 @@ int xt_compat_match_to_user(const struct xt_entry_match *m, }EXPORT_SYMBOL_GPL (xt_compat_match_to_user); +/* non-compat version may have padding after verdict */ +struct compat_xt_standard_target { +struct compat_xt_entry_target t; +compat_uint_t verdict; +}; + +/* see xt_check_entry_offsets */ int xt_compat_check_entry_offsets (const void *base,unsigned int target_offset,unsigned int next_offset)
@@ -557,6 +564,10 @@ int xt_compat_check_entry_offsets(const void *base,if (target_offset + t->u.target_size > next_offset)return -EINVAL ; +if (strcmp (t->u.user.name,XT_STANDARD_TARGET ) ==0 && + target_offset +sizeof (struct compat_xt_standard_target ) != next_offset) +return -EINVAL ; +return 0 ; }EXPORT_SYMBOL (xt_compat_check_entry_offsets);
@@ -596,6 +607,10 @@ int xt_check_entry_offsets(const void *base,if (target_offset + t->u.target_size > next_offset)return -EINVAL ; +if (strcmp (t->u.user.name,XT_STANDARD_TARGET ) ==0 && + target_offset +sizeof (struct xt_standard_target ) != next_offset) +return -EINVAL ; +return 0 ; }EXPORT_SYMBOL (xt_check_entry_offsets);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 8/23 netfilter: x_tables: check for bogus target offset
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
We're currently asserting that targetoff + targetsize <= nextoff.
Extend it to also check that targetoff is >= sizeof(xt_entry). Since this is generic code, add an argument pointing to the start of the match/target, we can then derive the base structure size from the delta.
We also need the e->elems pointer in a followup change to validate matches.
--- include/linux/netfilter/x_tables.h | 4 ++-- net/ipv4/netfilter/arp_tables.c | 5 +++-- net/ipv4/netfilter/ip_tables.c | 5 +++-- net/ipv6/netfilter/ip6_tables.c | 5 +++-- net/netfilter/x_tables.c | 17 +++++++++++++++-- 5 files changed, 26 insertions(+), 10 deletions(-)
include/linux/netfilter/x_tables.h +2 -2
@@ -242,7 +242,7 @@ void xt_unregister_match(struct xt_match *target);int xt_register_matches (struct xt_match *match,unsigned int n);void xt_unregister_matches (struct xt_match *match,unsigned int n); -int xt_check_entry_offsets (const void *base, +int xt_check_entry_offsets (const void *base,const char *elems,unsigned int target_offset,unsigned int next_offset);
@@ -494,7 +494,7 @@ void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, unsigned int *size);int xt_compat_target_to_user (const struct xt_entry_target *t,void __user **dstptr,unsigned int *size); -int xt_compat_check_entry_offsets (const void *base, +int xt_compat_check_entry_offsets (const void *base,const char *elems,unsigned int target_offset,unsigned int next_offset);
net/ipv4/netfilter/arp_tables.c +3 -2
@@ -592,7 +592,8 @@ static inline int check_entry_size_and_hooks(struct arpt_entry *e,if (!arp_checkentry (&e->arp))return -EINVAL ; - err =xt_check_entry_offsets (e, e->target_offset, e->next_offset); + err =xt_check_entry_offsets (e, e->elems, e->target_offset, + e->next_offset);if (err)return err;
@@ -1254,7 +1255,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,if (!arp_checkentry (&e->arp))return -EINVAL ; - ret =xt_compat_check_entry_offsets (e, e->target_offset, + ret =xt_compat_check_entry_offsets (e, e->elems, e->target_offset, e->next_offset);if (ret)return ret;
net/ipv4/netfilter/ip_tables.c +3 -2
@@ -754,7 +754,8 @@ check_entry_size_and_hooks(struct ipt_entry *e,if (!ip_checkentry (&e->ip))return -EINVAL ; - err =xt_check_entry_offsets (e, e->target_offset, e->next_offset); + err =xt_check_entry_offsets (e, e->elems, e->target_offset, + e->next_offset);if (err)return err;
@@ -1513,7 +1514,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,if (!ip_checkentry (&e->ip))return -EINVAL ; - ret =xt_compat_check_entry_offsets (e, + ret =xt_compat_check_entry_offsets (e, e->elems, e->target_offset, e->next_offset);if (ret)return ret;
net/ipv6/netfilter/ip6_tables.c +3 -2
@@ -766,7 +766,8 @@ check_entry_size_and_hooks(struct ip6t_entry *e,if (!ip6_checkentry (&e->ipv6))return -EINVAL ; - err =xt_check_entry_offsets (e, e->target_offset, e->next_offset); + err =xt_check_entry_offsets (e, e->elems, e->target_offset, + e->next_offset);if (err)return err;
@@ -1525,7 +1526,7 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,if (!ip6_checkentry (&e->ipv6))return -EINVAL ; - ret =xt_compat_check_entry_offsets (e, + ret =xt_compat_check_entry_offsets (e, e->elems, e->target_offset, e->next_offset);if (ret)return ret;
net/netfilter/x_tables.c +15 -2
@@ -546,14 +546,17 @@ struct compat_xt_standard_target {compat_uint_t verdict; }; -/* see xt_check_entry_offsets */ -int xt_compat_check_entry_offsets (const void *base, +int xt_compat_check_entry_offsets (const void *base,const char *elems,unsigned int target_offset,unsigned int next_offset) { +long size_of_base_struct = elems - (const char *)base;const struct compat_xt_entry_target *t;const char *e = base; +if (target_offset < size_of_base_struct) +return -EINVAL ; +if (target_offset +sizeof (*t) > next_offset)return -EINVAL ;
@@ -577,12 +580,16 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);* xt_check_entry_offsets - validate arp/ip/ip6t_entry * * @base: pointer to arp/ip/ip6t_entry +* @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems * @target_offset: the arp/ip/ip6_t->target_offset * @next_offset: the arp/ip/ip6_t->next_offset * * validates that target_offset and next_offset are sane. * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. * +* This function does not validate the targets or matches themselves, it +* only tests that all the offsets and sizes are correct. +* * The arp/ip/ip6t_entry structure @base must have passed following tests: * - it must point to a valid memory location * - base to base + next_offset must be accessible, i.e. not exceed allocated
@@ -591,12 +598,18 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);* Return: 0 on success, negative errno on failure. */ int xt_check_entry_offsets (const void *base, +const char *elems,unsigned int target_offset,unsigned int next_offset) { +long size_of_base_struct = elems - (const char *)base;const struct xt_entry_target *t;const char *e = base; +/* target start is within the ip/ip6/arpt_entry struct */ +if (target_offset < size_of_base_struct) +return -EINVAL ; +if (target_offset +sizeof (*t) > next_offset)return -EINVAL ;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 9/23 netfilter: x_tables: validate all offsets and sizes in a rule
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Validate that all matches (if any) add up to the beginning of the target and that each match covers at least the base structure size.
The compat path should be able to safely re-use the function as the structures only differ in alignment; added a BUILD_BUG_ON just in case we have an arch that adds padding as well.
--- net/netfilter/x_tables.c | 81 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-)
net/netfilter/x_tables.c +76 -5
@@ -416,6 +416,47 @@ int xt_check_match(struct xt_mtchk_param *par, }EXPORT_SYMBOL_GPL (xt_check_match); +/** xt_check_entry_match - check that matches end before start of target +* +* @match: beginning of xt_entry_match +* @target: beginning of this rules target (alleged end of matches) +* @alignment: alignment requirement of match structures +* +* Validates that all matches add up to the beginning of the target, +* and that each match covers at least the base structure size. +* +* Return: 0 on success, negative errno on failure. +*/ +static int xt_check_entry_match (const char *match,const char *target, +const size_t alignment) +{ +const struct xt_entry_match *pos; +int length = target - match; + +if (length ==0 )/* no matches */ +return 0 ; + + pos = (struct xt_entry_match *)match; +do { +if ((unsigned long )pos % alignment) +return -EINVAL ; + +if (length < (int )sizeof (struct xt_entry_match )) +return -EINVAL ; + +if (pos->u.match_size <sizeof (struct xt_entry_match )) +return -EINVAL ; + +if (pos->u.match_size > length) +return -EINVAL ; + + length -= pos->u.match_size; + pos = ((void *)((char *)(pos) + (pos)->u.match_size)); + }while (length >0 ); + +return 0 ; +} +#ifdef CONFIG_COMPAT int xt_compat_add_offset (u_int8_t af,unsigned int offset,int delta) {
@@ -571,7 +612,14 @@ int xt_compat_check_entry_offsets(const void *base, const char *elems, target_offset + sizeof(struct compat_xt_standard_target) != next_offset) return -EINVAL; - return 0; +/* compat_xt_entry match has less strict aligment requirements, +* otherwise they are identical. In case of padding differences +* we need to add compat version of xt_check_entry_match. +*/ +BUILD_BUG_ON (sizeof (struct compat_xt_entry_match ) !=sizeof (struct xt_entry_match )); + +return xt_check_entry_match (elems, base + target_offset, + __alignof__(struct compat_xt_entry_match)); }EXPORT_SYMBOL (xt_compat_check_entry_offsets);#endif /* CONFIG_COMPAT */
@@ -584,17 +632,39 @@ EXPORT_SYMBOL(xt_compat_check_entry_offsets);* @target_offset: the arp/ip/ip6_t->target_offset * @next_offset: the arp/ip/ip6_t->next_offset * -* validates that target_offset and next_offset are sane. -* Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. +* validates that target_offset and next_offset are sane and that all +* match sizes (if any) align with the target offset. * * This function does not validate the targets or matches themselves, it -* only tests that all the offsets and sizes are correct. +* only tests that all the offsets and sizes are correct, that all +* match structures are aligned, and that the last structure ends where +* the target structure begins. +* +* Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. * * The arp/ip/ip6t_entry structure @base must have passed following tests: * - it must point to a valid memory location * - base to base + next_offset must be accessible, i.e. not exceed allocated * length. * +* A well-formed entry looks like this: +* +* ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry +* e->elems[]-----' | | +* matchsize | | +* matchsize | | +* | | +* target_offset---------------------------------' | +* next_offset---------------------------------------------------' +* +* elems[]: flexible array member at end of ip(6)/arpt_entry struct. +* This is where matches (if any) and the target reside. +* target_offset: beginning of target. +* next_offset: start of the next rule; also: size of this rule. +* Since targets have a minimum size, target_offset + minlen <= next_offset. +* +* Every match stores its size, sum of sizes must not exceed target_offset. +* * Return: 0 on success, negative errno on failure. */ int xt_check_entry_offsets(const void *base,
@@ -624,7 +694,8 @@ int xt_check_entry_offsets(const void *base, target_offset + sizeof(struct xt_standard_target) != next_offset) return -EINVAL; -return 0 ; +return xt_check_entry_match (elems, base + target_offset, + __alignof__(struct xt_entry_match )); }EXPORT_SYMBOL (xt_check_entry_offsets);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 10/23 netfilter: ip_tables: simplify translate_compat_table args
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
--- net/ipv4/netfilter/ip_tables.c | 59 +++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 35 deletions(-)
net/ipv4/netfilter/ip_tables.c +24 -35
@@ -1449,7 +1449,6 @@ compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,static int compat_find_calc_match (struct xt_entry_match *m, -const char *name,const struct ipt_ip *ip,int *size) {
@@ -1486,8 +1485,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,const unsigned char *base, const unsigned char *limit, const unsigned int *hook_entries, - const unsigned int *underflows, - const char *name) + const unsigned int *underflows) { struct xt_entry_match *ematch;struct xt_entry_target *t;
@@ -1523,7 +1521,7 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e, entry_offset = (void *)e - (void *)base; j =0 ; xt_ematch_foreach(ematch, e) { - ret =compat_find_calc_match (ematch, name, &e->ip, &off); + ret =compat_find_calc_match (ematch, &e->ip, &off);if (ret !=0 ) goto release_matches; ++j;
@@ -1572,7 +1570,7 @@ release_matches:static int compat_copy_entry_from_user (struct compat_ipt_entry *e,void **dstptr, -unsigned int *size,const char *name, +unsigned int *size,struct xt_table_info *newinfo,unsigned char *base) {struct xt_entry_target *t;
@@ -1655,14 +1653,9 @@ compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)static int translate_compat_table (struct net *net, -const char *name, -unsigned int valid_hooks,struct xt_table_info **pinfo,void **pentry0, -unsigned int total_size, -unsigned int number, -unsigned int *hook_entries, -unsigned int *underflows) +const struct compat_ipt_replace *compatr) {unsigned int i, j;struct xt_table_info *newinfo, *info;
@@ -1674,8 +1667,8 @@ translate_compat_table(struct net *net, info = *pinfo; entry0 = *pentry0; - size = total_size; - info->number = number; + size = compatr->size; + info->number = compatr->num_entries;/* Init all hooks to impossible value. */ for (i =0 ; i <NF_INET_NUMHOOKS ; i++) {
@@ -1686,40 +1679,39 @@ translate_compat_table(struct net *net,duprintf ("translate_compat_table: size %u\n" , info->size); j =0 ;xt_compat_lock (AF_INET ); -xt_compat_init_offsets (AF_INET , number); +xt_compat_init_offsets (AF_INET , compatr->num_entries);/* Walk through entries, checking offsets. */ - xt_entry_foreach(iter0, entry0, total_size) { + xt_entry_foreach(iter0, entry0, compatr->size) { ret =check_compat_entry_size_and_hooks (iter0, info, &size, entry0, - entry0 + total_size, - hook_entries, - underflows, - name); + entry0 + compatr->size, + compatr->hook_entry, + compatr->underflow);if (ret !=0 ) goto out_unlock; ++j; } ret = -EINVAL ; -if (j != number) { +if (j != compatr->num_entries) {duprintf ("translate_compat_table: %u not %u entries\n" , - j, number); + j, compatr->num_entries); goto out_unlock; }/* Check hooks all assigned */ for (i =0 ; i <NF_INET_NUMHOOKS ; i++) {/* Only hooks which are valid */ -if (!(valid_hooks & (1 << i))) +if (!(compatr->valid_hooks & (1 << i)))continue ;if (info->hook_entry[i] ==0xFFFFFFFF ) {duprintf ("Invalid hook entry %u %u\n" , - i, hook_entries[i]); + i, info->hook_entry[i]); goto out_unlock; }if (info->underflow[i] ==0xFFFFFFFF ) {duprintf ("Invalid underflow %u %u\n" , - i, underflows[i]); + i, info->underflow[i]); goto out_unlock; } }
@@ -1729,17 +1721,17 @@ translate_compat_table(struct net *net,if (!newinfo) goto out_unlock; - newinfo->number = number; + newinfo->number = compatr->num_entries;for (i =0 ; i <NF_INET_NUMHOOKS ; i++) { newinfo->hook_entry[i] = info->hook_entry[i]; newinfo->underflow[i] = info->underflow[i]; } entry1 = newinfo->entries; pos = entry1; - size = total_size; - xt_entry_foreach(iter0, entry0, total_size) { + size = compatr->size; + xt_entry_foreach(iter0, entry0, compatr->size) { ret =compat_copy_entry_from_user (iter0, &pos, &size, - name, newinfo, entry1); + newinfo, entry1);if (ret !=0 )break ; }
@@ -1749,12 +1741,12 @@ translate_compat_table(struct net *net, goto free_newinfo; ret = -ELOOP ; -if (!mark_source_chains (newinfo, valid_hooks, entry1)) +if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1)) goto free_newinfo; i =0 ; xt_entry_foreach(iter1, entry1, newinfo->size) { - ret =compat_check_entry (iter1, net, name); + ret =compat_check_entry (iter1, net, compatr->name);if (ret !=0 )break ; ++i;
@@ -1794,7 +1786,7 @@ translate_compat_table(struct net *net, free_newinfo:xt_free_table_info (newinfo); out: - xt_entry_foreach(iter0, entry0, total_size) { + xt_entry_foreach(iter0, entry0, compatr->size) {if (j-- ==0 )break ;compat_release_entry (iter0);
@@ -1839,10 +1831,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len) goto free_newinfo; } - ret =translate_compat_table (net, tmp.name, tmp.valid_hooks, - &newinfo, &loc_cpu_entry, tmp.size, - tmp.num_entries, tmp.hook_entry, - tmp.underflow); + ret =translate_compat_table (net, &newinfo, &loc_cpu_entry, &tmp);if (ret !=0 ) goto free_newinfo;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 11/23 netfilter: ip6_tables: simplify translate_compat_table args
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
--- net/ipv6/netfilter/ip6_tables.c | 59 +++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 35 deletions(-)
net/ipv6/netfilter/ip6_tables.c +24 -35
@@ -1461,7 +1461,6 @@ compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,static int compat_find_calc_match (struct xt_entry_match *m, -const char *name,const struct ip6t_ip6 *ipv6,int *size) {
@@ -1498,8 +1497,7 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,const unsigned char *base, const unsigned char *limit, const unsigned int *hook_entries, - const unsigned int *underflows, - const char *name) + const unsigned int *underflows) { struct xt_entry_match *ematch;struct xt_entry_target *t;
@@ -1535,7 +1533,7 @@ check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e, entry_offset = (void *)e - (void *)base; j =0 ; xt_ematch_foreach(ematch, e) { - ret =compat_find_calc_match (ematch, name, &e->ipv6, &off); + ret =compat_find_calc_match (ematch, &e->ipv6, &off);if (ret !=0 ) goto release_matches; ++j;
@@ -1584,7 +1582,7 @@ release_matches:static int compat_copy_entry_from_user (struct compat_ip6t_entry *e,void **dstptr, -unsigned int *size,const char *name, +unsigned int *size,struct xt_table_info *newinfo,unsigned char *base) {struct xt_entry_target *t;
@@ -1664,14 +1662,9 @@ static int compat_check_entry(struct ip6t_entry *e, struct net *net,static int translate_compat_table (struct net *net, -const char *name, -unsigned int valid_hooks,struct xt_table_info **pinfo,void **pentry0, -unsigned int total_size, -unsigned int number, -unsigned int *hook_entries, -unsigned int *underflows) +const struct compat_ip6t_replace *compatr) {unsigned int i, j;struct xt_table_info *newinfo, *info;
@@ -1683,8 +1676,8 @@ translate_compat_table(struct net *net, info = *pinfo; entry0 = *pentry0; - size = total_size; - info->number = number; + size = compatr->size; + info->number = compatr->num_entries;/* Init all hooks to impossible value. */ for (i =0 ; i <NF_INET_NUMHOOKS ; i++) {
@@ -1695,40 +1688,39 @@ translate_compat_table(struct net *net,duprintf ("translate_compat_table: size %u\n" , info->size); j =0 ;xt_compat_lock (AF_INET6 ); -xt_compat_init_offsets (AF_INET6 , number); +xt_compat_init_offsets (AF_INET6 , compatr->num_entries);/* Walk through entries, checking offsets. */ - xt_entry_foreach(iter0, entry0, total_size) { + xt_entry_foreach(iter0, entry0, compatr->size) { ret =check_compat_entry_size_and_hooks (iter0, info, &size, entry0, - entry0 + total_size, - hook_entries, - underflows, - name); + entry0 + compatr->size, + compatr->hook_entry, + compatr->underflow);if (ret !=0 ) goto out_unlock; ++j; } ret = -EINVAL ; -if (j != number) { +if (j != compatr->num_entries) {duprintf ("translate_compat_table: %u not %u entries\n" , - j, number); + j, compatr->num_entries); goto out_unlock; }/* Check hooks all assigned */ for (i =0 ; i <NF_INET_NUMHOOKS ; i++) {/* Only hooks which are valid */ -if (!(valid_hooks & (1 << i))) +if (!(compatr->valid_hooks & (1 << i)))continue ;if (info->hook_entry[i] ==0xFFFFFFFF ) {duprintf ("Invalid hook entry %u %u\n" , - i, hook_entries[i]); + i, info->hook_entry[i]); goto out_unlock; }if (info->underflow[i] ==0xFFFFFFFF ) {duprintf ("Invalid underflow %u %u\n" , - i, underflows[i]); + i, info->underflow[i]); goto out_unlock; } }
@@ -1738,17 +1730,17 @@ translate_compat_table(struct net *net,if (!newinfo) goto out_unlock; - newinfo->number = number; + newinfo->number = compatr->num_entries;for (i =0 ; i <NF_INET_NUMHOOKS ; i++) { newinfo->hook_entry[i] = info->hook_entry[i]; newinfo->underflow[i] = info->underflow[i]; } entry1 = newinfo->entries; pos = entry1; - size = total_size; - xt_entry_foreach(iter0, entry0, total_size) { + size = compatr->size; + xt_entry_foreach(iter0, entry0, compatr->size) { ret =compat_copy_entry_from_user (iter0, &pos, &size, - name, newinfo, entry1); + newinfo, entry1);if (ret !=0 )break ; }
@@ -1758,12 +1750,12 @@ translate_compat_table(struct net *net, goto free_newinfo; ret = -ELOOP ; -if (!mark_source_chains (newinfo, valid_hooks, entry1)) +if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1)) goto free_newinfo; i =0 ; xt_entry_foreach(iter1, entry1, newinfo->size) { - ret =compat_check_entry (iter1, net, name); + ret =compat_check_entry (iter1, net, compatr->name);if (ret !=0 )break ; ++i;
@@ -1803,7 +1795,7 @@ translate_compat_table(struct net *net, free_newinfo:xt_free_table_info (newinfo); out: - xt_entry_foreach(iter0, entry0, total_size) { + xt_entry_foreach(iter0, entry0, compatr->size) {if (j-- ==0 )break ;compat_release_entry (iter0);
@@ -1848,10 +1840,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len) goto free_newinfo; } - ret =translate_compat_table (net, tmp.name, tmp.valid_hooks, - &newinfo, &loc_cpu_entry, tmp.size, - tmp.num_entries, tmp.hook_entry, - tmp.underflow); + ret =translate_compat_table (net, &newinfo, &loc_cpu_entry, &tmp);if (ret !=0 ) goto free_newinfo;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 12/23 netfilter: arp_tables: simplify translate_compat_table args
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
--- net/ipv4/netfilter/arp_tables.c | 82 ++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 46 deletions(-)
net/ipv4/netfilter/arp_tables.c +36 -46
@@ -1214,6 +1214,18 @@ static int do_add_counters(struct net *net, const void __user *user, }#ifdef CONFIG_COMPAT +struct compat_arpt_replace { +char name[XT_TABLE_MAXNAMELEN ]; +u32 valid_hooks; +u32 num_entries; +u32 size; +u32 hook_entry[NF_ARP_NUMHOOKS ]; +u32 underflow[NF_ARP_NUMHOOKS ]; +u32 num_counters; +compat_uptr_t counters; +struct compat_arpt_entry entries[0 ]; +}; +static inline void compat_release_entry (struct compat_arpt_entry *e) {struct xt_entry_target *t;
@@ -1229,8 +1241,7 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,const unsigned char *base, const unsigned char *limit, const unsigned int *hook_entries, - const unsigned int *underflows, - const char *name) + const unsigned int *underflows) { struct xt_entry_target *t;struct xt_target *target;
@@ -1301,7 +1312,7 @@ out:static int compat_copy_entry_from_user (struct compat_arpt_entry *e,void **dstptr, -unsigned int *size,const char *name, +unsigned int *size,struct xt_table_info *newinfo,unsigned char *base) {struct xt_entry_target *t;
@@ -1334,14 +1345,9 @@ compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,return ret; } -static int translate_compat_table (const char *name, -unsigned int valid_hooks, -struct xt_table_info **pinfo, +static int translate_compat_table (struct xt_table_info **pinfo,void **pentry0, -unsigned int total_size, -unsigned int number, -unsigned int *hook_entries, -unsigned int *underflows) +const struct compat_arpt_replace *compatr) {unsigned int i, j;struct xt_table_info *newinfo, *info;
@@ -1353,8 +1359,8 @@ static int translate_compat_table(const char *name, info = *pinfo; entry0 = *pentry0; - size = total_size; - info->number = number; + size = compatr->size; + info->number = compatr->num_entries;/* Init all hooks to impossible value. */ for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) {
@@ -1365,40 +1371,39 @@ static int translate_compat_table(const char *name,duprintf ("translate_compat_table: size %u\n" , info->size); j =0 ;xt_compat_lock (NFPROTO_ARP ); -xt_compat_init_offsets (NFPROTO_ARP , number); +xt_compat_init_offsets (NFPROTO_ARP , compatr->num_entries);/* Walk through entries, checking offsets. */ - xt_entry_foreach(iter0, entry0, total_size) { + xt_entry_foreach(iter0, entry0, compatr->size) { ret =check_compat_entry_size_and_hooks (iter0, info, &size, entry0, - entry0 + total_size, - hook_entries, - underflows, - name); + entry0 + compatr->size, + compatr->hook_entry, + compatr->underflow);if (ret !=0 ) goto out_unlock; ++j; } ret = -EINVAL ; -if (j != number) { +if (j != compatr->num_entries) {duprintf ("translate_compat_table: %u not %u entries\n" , - j, number); + j, compatr->num_entries); goto out_unlock; }/* Check hooks all assigned */ for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) {/* Only hooks which are valid */ -if (!(valid_hooks & (1 << i))) +if (!(compatr->valid_hooks & (1 << i)))continue ;if (info->hook_entry[i] ==0xFFFFFFFF ) {duprintf ("Invalid hook entry %u %u\n" , - i, hook_entries[i]); + i, info->hook_entry[i]); goto out_unlock; }if (info->underflow[i] ==0xFFFFFFFF ) {duprintf ("Invalid underflow %u %u\n" , - i, underflows[i]); + i, info->underflow[i]); goto out_unlock; } }
@@ -1408,17 +1413,17 @@ static int translate_compat_table(const char *name,if (!newinfo) goto out_unlock; - newinfo->number = number; + newinfo->number = compatr->num_entries;for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { newinfo->hook_entry[i] = info->hook_entry[i]; newinfo->underflow[i] = info->underflow[i]; } entry1 = newinfo->entries; pos = entry1; - size = total_size; - xt_entry_foreach(iter0, entry0, total_size) { + size = compatr->size; + xt_entry_foreach(iter0, entry0, compatr->size) { ret =compat_copy_entry_from_user (iter0, &pos, &size, - name, newinfo, entry1); + newinfo, entry1);if (ret !=0 )break ; }
@@ -1428,7 +1433,7 @@ static int translate_compat_table(const char *name, goto free_newinfo; ret = -ELOOP ; -if (!mark_source_chains (newinfo, valid_hooks, entry1)) +if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1)) goto free_newinfo; i =0 ;
@@ -1439,7 +1444,7 @@ static int translate_compat_table(const char *name,break ; } - ret =check_target (iter1, name); + ret =check_target (iter1, compatr->name);if (ret !=0 ) {xt_percpu_counter_free (iter1->counters.pcnt);break ;
@@ -1481,7 +1486,7 @@ static int translate_compat_table(const char *name, free_newinfo:xt_free_table_info (newinfo); out: - xt_entry_foreach(iter0, entry0, total_size) { + xt_entry_foreach(iter0, entry0, compatr->size) {if (j-- ==0 )break ;compat_release_entry (iter0);
@@ -1493,18 +1498,6 @@ out_unlock: goto out; } -struct compat_arpt_replace { -char name[XT_TABLE_MAXNAMELEN ]; -u32 valid_hooks; -u32 num_entries; -u32 size; -u32 hook_entry[NF_ARP_NUMHOOKS ]; -u32 underflow[NF_ARP_NUMHOOKS ]; -u32 num_counters; -compat_uptr_t counters; -struct compat_arpt_entry entries[0 ]; -}; -static int compat_do_replace (struct net *net,void __user *user,unsigned int len) {
@@ -1537,10 +1530,7 @@ static int compat_do_replace(struct net *net, void __user *user, goto free_newinfo; } - ret =translate_compat_table (tmp.name, tmp.valid_hooks, - &newinfo, &loc_cpu_entry, tmp.size, - tmp.num_entries, tmp.hook_entry, - tmp.underflow); + ret =translate_compat_table (&newinfo, &loc_cpu_entry, &tmp);if (ret !=0 ) goto free_newinfo;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 13/23 netfilter: x_tables: xt_compat_match_from_user doesn't need a retval
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Always returned 0.
--- include/linux/netfilter/x_tables.h | 2 +- net/ipv4/netfilter/arp_tables.c | 17 +++++------------ net/ipv4/netfilter/ip_tables.c | 26 +++++++++----------------- net/ipv6/netfilter/ip6_tables.c | 27 +++++++++------------------ net/netfilter/x_tables.c | 5 ++--- 5 files changed, 26 insertions(+), 51 deletions(-)
include/linux/netfilter/x_tables.h +1 -1
@@ -484,7 +484,7 @@ void xt_compat_init_offsets(u_int8_t af, unsigned int number);int xt_compat_calc_jump (u_int8_t af,unsigned int offset);int xt_compat_match_offset (const struct xt_match *match); -int xt_compat_match_from_user (struct xt_entry_match *m,void **dstptr, +void xt_compat_match_from_user (struct xt_entry_match *m,void **dstptr,unsigned int *size);int xt_compat_match_to_user (const struct xt_entry_match *m,void __user **dstptr,unsigned int *size);
net/ipv4/netfilter/arp_tables.c +5 -12
@@ -1310,7 +1310,7 @@ out:return ret; } -static int +static void compat_copy_entry_from_user (struct compat_arpt_entry *e,void **dstptr,unsigned int *size,struct xt_table_info *newinfo,unsigned char *base)
@@ -1319,9 +1319,8 @@ compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,struct xt_target *target;struct arpt_entry *de;unsigned int origsize; -int ret, h; +int h; - ret =0 ; origsize = *size; de = (struct arpt_entry *)*dstptr;memcpy (de, e,sizeof (struct arpt_entry ));
@@ -1342,7 +1341,6 @@ compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,if ((unsigned char *)de - base < newinfo->underflow[h]) newinfo->underflow[h] -= origsize - *size; } -return ret; } static int translate_compat_table(struct xt_table_info **pinfo,
@@ -1421,16 +1419,11 @@ static int translate_compat_table(struct xt_table_info **pinfo, entry1 = newinfo->entries; pos = entry1; size = compatr->size; - xt_entry_foreach(iter0, entry0, compatr->size) { - ret =compat_copy_entry_from_user (iter0, &pos, &size, - newinfo, entry1); -if (ret !=0 ) -break ; - } + xt_entry_foreach(iter0, entry0, compatr->size) +compat_copy_entry_from_user (iter0, &pos, &size, + newinfo, entry1);xt_compat_flush_offsets (NFPROTO_ARP );xt_compat_unlock (NFPROTO_ARP ); -if (ret) - goto free_newinfo; ret = -ELOOP ;if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1))
net/ipv4/netfilter/ip_tables.c +9 -17
@@ -1568,7 +1568,7 @@ release_matches:return ret; } -static int +static void compat_copy_entry_from_user (struct compat_ipt_entry *e,void **dstptr,unsigned int *size,struct xt_table_info *newinfo,unsigned char *base)
@@ -1577,10 +1577,9 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,struct xt_target *target;struct ipt_entry *de;unsigned int origsize; -int ret, h; +int h;struct xt_entry_match *ematch; - ret =0 ; origsize = *size; de = (struct ipt_entry *)*dstptr;memcpy (de, e,sizeof (struct ipt_entry ));
@@ -1589,11 +1588,9 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr, *dstptr +=sizeof (struct ipt_entry ); *size +=sizeof (struct ipt_entry ) -sizeof (struct compat_ipt_entry ); - xt_ematch_foreach(ematch, e) { - ret =xt_compat_match_from_user (ematch, dstptr, size); -if (ret !=0 ) -return ret; - } + xt_ematch_foreach(ematch, e) +xt_compat_match_from_user (ematch, dstptr, size); + de->target_offset = e->target_offset - (origsize - *size); t =compat_ipt_get_target (e); target = t->u.kernel.target;
@@ -1606,7 +1603,6 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,if ((unsigned char *)de - base < newinfo->underflow[h]) newinfo->underflow[h] -= origsize - *size; } -return ret; } static int
@@ -1729,16 +1725,12 @@ translate_compat_table(struct net *net, entry1 = newinfo->entries; pos = entry1; size = compatr->size; - xt_entry_foreach(iter0, entry0, compatr->size) { - ret =compat_copy_entry_from_user (iter0, &pos, &size, - newinfo, entry1); -if (ret !=0 ) -break ; - } + xt_entry_foreach(iter0, entry0, compatr->size) +compat_copy_entry_from_user (iter0, &pos, &size, + newinfo, entry1); +xt_compat_flush_offsets (AF_INET );xt_compat_unlock (AF_INET ); -if (ret) - goto free_newinfo; ret = -ELOOP ;if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1))
net/ipv6/netfilter/ip6_tables.c +9 -18
@@ -1580,7 +1580,7 @@ release_matches:return ret; } -static int +static void compat_copy_entry_from_user (struct compat_ip6t_entry *e,void **dstptr,unsigned int *size,struct xt_table_info *newinfo,unsigned char *base)
@@ -1588,10 +1588,9 @@ compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,struct xt_entry_target *t;struct ip6t_entry *de;unsigned int origsize; -int ret, h; +int h;struct xt_entry_match *ematch; - ret =0 ; origsize = *size; de = (struct ip6t_entry *)*dstptr;memcpy (de, e,sizeof (struct ip6t_entry ));
@@ -1600,11 +1599,9 @@ compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr, *dstptr +=sizeof (struct ip6t_entry ); *size +=sizeof (struct ip6t_entry ) -sizeof (struct compat_ip6t_entry ); - xt_ematch_foreach(ematch, e) { - ret =xt_compat_match_from_user (ematch, dstptr, size); -if (ret !=0 ) -return ret; - } + xt_ematch_foreach(ematch, e) +xt_compat_match_from_user (ematch, dstptr, size); + de->target_offset = e->target_offset - (origsize - *size); t =compat_ip6t_get_target (e);xt_compat_target_from_user (t, dstptr, size);
@@ -1616,7 +1613,6 @@ compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,if ((unsigned char *)de - base < newinfo->underflow[h]) newinfo->underflow[h] -= origsize - *size; } -return ret; } static int compat_check_entry(struct ip6t_entry *e, struct net *net,
@@ -1737,17 +1733,12 @@ translate_compat_table(struct net *net, } entry1 = newinfo->entries; pos = entry1; - size = compatr->size; - xt_entry_foreach(iter0, entry0, compatr->size) { - ret =compat_copy_entry_from_user (iter0, &pos, &size, - newinfo, entry1); -if (ret !=0 ) -break ; - } + xt_entry_foreach(iter0, entry0, compatr->size) +compat_copy_entry_from_user (iter0, &pos, &size, + newinfo, entry1); +xt_compat_flush_offsets (AF_INET6 );xt_compat_unlock (AF_INET6 ); -if (ret) - goto free_newinfo; ret = -ELOOP ;if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1))
net/netfilter/x_tables.c +2 -3
@@ -526,8 +526,8 @@ int xt_compat_match_offset(const struct xt_match *match) }EXPORT_SYMBOL_GPL (xt_compat_match_offset); -int xt_compat_match_from_user (struct xt_entry_match *m,void **dstptr, -unsigned int *size) +void xt_compat_match_from_user (struct xt_entry_match *m,void **dstptr, +unsigned int *size) {const struct xt_match *match = m->u.kernel.match;struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
@@ -549,7 +549,6 @@ int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, *size += off; *dstptr += msize; -return 0 ; }EXPORT_SYMBOL_GPL (xt_compat_match_from_user);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 14/23 netfilter: x_tables: do compat validation via translate_table
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
This looks like refactoring, but its also a bug fix.
Problem is that the compat path (32bit iptables, 64bit kernel) lacks a few sanity tests that are done in the normal path.
For example, we do not check for underflows and the base chain policies.
While its possible to also add such checks to the compat path, its more copy&pastry, for instance we cannot reuse check_underflow() helper as e->target_offset differs in the compat case.
Other problem is that it makes auditing for validation errors harder; two places need to be checked and kept in sync.
At a high level 32 bit compat works like this:
1- initial pass over blob:
validate match/entry offsets, bounds checking lookup all matches and targets
do bookkeeping wrt. size delta of 32/64bit structures assign match/target.u.kernel pointer (points at kernel implementation, needed to access ->compatsize etc.)
2- allocate memory according to the total bookkeeping size to
contain the translated ruleset
3- second pass over original blob:
for each entry, copy the 32bit representation to the newly allocated memory. This also does any special match translations (e.g. adjust 32bit to 64bit longs, etc).
4- check if ruleset is free of loops (chase all jumps)
5-first pass over translated blob:
call the checkentry function of all matches and targets.
The alternative implemented by this patch is to drop steps 3&4 from the compat process, the translation is changed into an intermediate step rather than a full 1:1 translate_table replacement.
In the 2nd pass (step #3), change the 64bit ruleset back to a kernel representation, i.e. put() the kernel pointer and restore ->u.user.name .
This gets us a 64bit ruleset that is in the format generated by a 64bit iptables userspace -- we can then use translate_table() to get the 'native' sanity checks.
This has two drawbacks:
1. we re-validate all the match and target entry structure sizes even though compat translation is supposed to never generate bogus offsets.
2. we put and then re-lookup each match and target.
THe upside is that we get all sanity tests and ruleset validations provided by the normal path and can remove some duplicated compat code.
iptables-restore time of autogenerated ruleset with 300k chains of form -A CHAIN0001 -m limit --limit 1/s -j CHAIN0002
-A CHAIN0002 -m limit --limit 1/s -j CHAIN0003
shows no noticeable differences in restore times:
old: 0m30.796s new: 0m31.521s
64bit: 0m25.674s
--- net/ipv4/netfilter/arp_tables.c | 114 ++++++----------------------- net/ipv4/netfilter/ip_tables.c | 155 ++++++++-------------------------------- net/ipv6/netfilter/ip6_tables.c | 148 ++++++-------------------------------- net/netfilter/x_tables.c | 8 +++ 4 files changed, 83 insertions(+), 342 deletions(-)
net/ipv4/netfilter/arp_tables.c +23 -91
@@ -1234,19 +1234,17 @@ static inline void compat_release_entry(struct compat_arpt_entry *e)module_put (t->u.kernel.target->me); } -static inline int +static int check_compat_entry_size_and_hooks (struct compat_arpt_entry *e,struct xt_table_info *newinfo,unsigned int *size,const unsigned char *base, -const unsigned char *limit, -const unsigned int *hook_entries, -const unsigned int *underflows) +const unsigned char *limit) {struct xt_entry_target *t;struct xt_target *target;unsigned int entry_offset; -int ret, off, h; +int ret, off;duprintf ("check_compat_entry_size_and_hooks %p\n" , e); if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
@@ -1291,17 +1289,6 @@ check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,if (ret) goto release_target; -/* Check hooks & underflows */ -for (h =0 ; h <NF_ARP_NUMHOOKS ; h++) { -if ((unsigned char *)e - base == hook_entries[h]) - newinfo->hook_entry[h] = hook_entries[h]; -if ((unsigned char *)e - base == underflows[h]) - newinfo->underflow[h] = underflows[h]; - } - -/* Clear counters and comefrom */ -memset (&e->counters,0 ,sizeof (e->counters)); - e->comefrom =0 ;return 0 ; release_target:
@@ -1351,7 +1338,7 @@ static int translate_compat_table(struct xt_table_info **pinfo,struct xt_table_info *newinfo, *info;void *pos, *entry0, *entry1;struct compat_arpt_entry *iter0; -struct arpt_entry *iter1; +struct arpt_replace repl;unsigned int size;int ret =0 ;
@@ -1360,12 +1347,6 @@ static int translate_compat_table(struct xt_table_info **pinfo, size = compatr->size; info->number = compatr->num_entries; -/* Init all hooks to impossible value. */ -for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { - info->hook_entry[i] =0xFFFFFFFF ; - info->underflow[i] =0xFFFFFFFF ; - } -duprintf ("translate_compat_table: size %u\n" , info->size); j =0 ;xt_compat_lock (NFPROTO_ARP );
@@ -1374,9 +1355,7 @@ static int translate_compat_table(struct xt_table_info **pinfo, xt_entry_foreach(iter0, entry0, compatr->size) { ret =check_compat_entry_size_and_hooks (iter0, info, &size, entry0, - entry0 + compatr->size, - compatr->hook_entry, - compatr->underflow); + entry0 + compatr->size);if (ret !=0 ) goto out_unlock; ++j;
@@ -1389,23 +1368,6 @@ static int translate_compat_table(struct xt_table_info **pinfo, goto out_unlock; } -/* Check hooks all assigned */ -for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { -/* Only hooks which are valid */ -if (!(compatr->valid_hooks & (1 << i))) -continue ; -if (info->hook_entry[i] ==0xFFFFFFFF ) { -duprintf ("Invalid hook entry %u %u\n" , - i, info->hook_entry[i]); - goto out_unlock; - } -if (info->underflow[i] ==0xFFFFFFFF ) { -duprintf ("Invalid underflow %u %u\n" , - i, info->underflow[i]); - goto out_unlock; - } - } - ret = -ENOMEM ; newinfo =xt_alloc_table_info (size);if (!newinfo)
@@ -1422,55 +1384,26 @@ static int translate_compat_table(struct xt_table_info **pinfo, xt_entry_foreach(iter0, entry0, compatr->size)compat_copy_entry_from_user (iter0, &pos, &size, newinfo, entry1); + +/* all module references in entry0 are now gone */ +xt_compat_flush_offsets (NFPROTO_ARP );xt_compat_unlock (NFPROTO_ARP ); - ret = -ELOOP ; -if (!mark_source_chains (newinfo, compatr->valid_hooks, entry1)) - goto free_newinfo; - - i =0 ; - xt_entry_foreach(iter1, entry1, newinfo->size) { - iter1->counters.pcnt =xt_percpu_counter_alloc (); -if (IS_ERR_VALUE (iter1->counters.pcnt)) { - ret = -ENOMEM ; -break ; - } +memcpy (&repl, compatr,sizeof (*compatr)); - ret =check_target (iter1, compatr->name); -if (ret !=0 ) { -xt_percpu_counter_free (iter1->counters.pcnt); -break ; - } - ++i; -if (strcmp (arpt_get_target (iter1)->u.user.name, -XT_ERROR_TARGET ) ==0 ) - ++newinfo->stacksize; - } -if (ret) { -/* -* The first i matches need cleanup_entry (calls ->destroy) -* because they had called ->check already. The other j-i -* entries need only release. -*/ -int skip = i; - j -= i; - xt_entry_foreach(iter0, entry0, newinfo->size) { -if (skip-- >0 ) -continue ; -if (j-- ==0 ) -break ; -compat_release_entry (iter0); - } - xt_entry_foreach(iter1, entry1, newinfo->size) { -if (i-- ==0 ) -break ; -cleanup_entry (iter1); - } -xt_free_table_info (newinfo); -return ret; +for (i =0 ; i <NF_ARP_NUMHOOKS ; i++) { + repl.hook_entry[i] = newinfo->hook_entry[i]; + repl.underflow[i] = newinfo->underflow[i]; } + repl.num_counters =0 ; + repl.counters =NULL ; + repl.size = newinfo->size; + ret =translate_table (newinfo, entry1, &repl); +if (ret) + goto free_newinfo; + *pinfo = newinfo; *pentry0 = entry1;xt_free_table_info (info);
@@ -1478,17 +1411,16 @@ static int translate_compat_table(struct xt_table_info **pinfo, free_newinfo:xt_free_table_info (newinfo); -out: +return ret; +out_unlock: +xt_compat_flush_offsets (NFPROTO_ARP ); +xt_compat_unlock (NFPROTO_ARP ); xt_entry_foreach(iter0, entry0, compatr->size) {if (j-- ==0 )break ;compat_release_entry (iter0); }return ret; -out_unlock: -xt_compat_flush_offsets (NFPROTO_ARP ); -xt_compat_unlock (NFPROTO_ARP ); - goto out; } static int compat_do_replace(struct net *net, void __user *user,
net/ipv4/netfilter/ip_tables.c +29 -126
239 lines not on this page · raw message
net/ipv6/netfilter/ip6_tables.c +23 -125
227 lines not on this page · raw message
net/netfilter/x_tables.c +8 -0
@@ -533,6 +533,7 @@ void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;int pad, off =xt_compat_match_offset (match);u_int16_t msize = cm->u.user.match_size; +char name[sizeof (m->u.user.name)]; m = *dstptr;memcpy (m, cm,sizeof (*cm));
@@ -546,6 +547,9 @@ void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, msize += off; m->u.user.match_size = msize; +strlcpy (name, match->name,sizeof (name)); +module_put (match->me); +strncpy (m->u.user.name, name,sizeof (m->u.user.name)); *size += off; *dstptr += msize;
@@ -763,6 +767,7 @@ void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;int pad, off =xt_compat_target_offset (target);u_int16_t tsize = ct->u.user.target_size; +char name[sizeof (t->u.user.name)]; t = *dstptr;memcpy (t, ct,sizeof (*ct));
@@ -776,6 +781,9 @@ void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, tsize += off; t->u.user.target_size = tsize; +strlcpy (name, target->name,sizeof (name)); +module_put (target->me); +strncpy (t->u.user.name, name,sizeof (t->u.user.name)); *size += off; *dstptr += tsize;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 15/23 netfilter: x_tables: remove obsolete overflow check for compat case too
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
commit 9e67d5a739327c44885adebb4f3a538050be73e4 ("[NETFILTER]: x_tables: remove obsolete overflow check") left the compat parts alone, but we can kill it there as well.
--- net/ipv4/netfilter/arp_tables.c | 2 -- net/ipv4/netfilter/ip_tables.c | 2 -- net/ipv6/netfilter/ip6_tables.c | 2 -- 3 files changed, 6 deletions(-)
net/ipv4/netfilter/arp_tables.c +0 -2
@@ -1436,8 +1436,6 @@ static int compat_do_replace(struct net *net, void __user *user,return -EFAULT ;/* overflow check */ -if (tmp.size >=INT_MAX /num_possible_cpus ()) -return -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ;if (tmp.num_counters ==0 )
net/ipv4/netfilter/ip_tables.c +0 -2
@@ -1706,8 +1706,6 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)return -EFAULT ;/* overflow check */ -if (tmp.size >=INT_MAX /num_possible_cpus ()) -return -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ;if (tmp.num_counters ==0 )
net/ipv6/netfilter/ip6_tables.c +0 -2
@@ -1709,8 +1709,6 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)return -EFAULT ;/* overflow check */ -if (tmp.size >=INT_MAX /num_possible_cpus ()) -return -ENOMEM ;if (tmp.num_counters >=INT_MAX /sizeof (struct xt_counters ))return -ENOMEM ;if (tmp.num_counters ==0 )
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 16/23 netfilter: x_tables: remove obsolete check
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Since 'netfilter: x_tables: validate targets of jumps' change we validate that the target aligns exactly with beginning of a rule, so offset test is now redundant.
--- net/ipv4/netfilter/arp_tables.c | 8 -------- net/ipv4/netfilter/ip_tables.c | 7 ------- net/ipv6/netfilter/ip6_tables.c | 7 ------- 3 files changed, 22 deletions(-)
net/ipv4/netfilter/arp_tables.c +0 -8
@@ -461,14 +461,6 @@ static int mark_source_chains(const struct xt_table_info *newinfo,if (strcmp (t->target.u.user.name,XT_STANDARD_TARGET ) ==0 && newpos >=0 ) { -if (newpos > newinfo->size - -sizeof (struct arpt_entry )) { -duprintf ("mark_source_chains: " -"bad verdict (%i)\n" , - newpos); -return 0 ; - } -/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos);
net/ipv4/netfilter/ip_tables.c +0 -7
@@ -542,13 +542,6 @@ mark_source_chains(const struct xt_table_info *newinfo,if (strcmp (t->target.u.user.name,XT_STANDARD_TARGET ) ==0 && newpos >=0 ) { -if (newpos > newinfo->size - -sizeof (struct ipt_entry )) { -duprintf ("mark_source_chains: " -"bad verdict (%i)\n" , - newpos); -return 0 ; - }/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos);
net/ipv6/netfilter/ip6_tables.c +0 -7
@@ -554,13 +554,6 @@ mark_source_chains(const struct xt_table_info *newinfo,if (strcmp (t->target.u.user.name,XT_STANDARD_TARGET ) ==0 && newpos >=0 ) { -if (newpos > newinfo->size - -sizeof (struct ip6t_entry )) { -duprintf ("mark_source_chains: " -"bad verdict (%i)\n" , - newpos); -return 0 ; - }/* This a jump; chase it. */ duprintf ("Jump rule %u -> %u\n" , pos, newpos);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 17/23 netfilter: x_tables: introduce and use xt_copy_counters_from_user
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The three variants use same copy&pasted code, condense this into a helper and use that.
Make sure info.name is 0-terminated.
--- include/linux/netfilter/x_tables.h | 3 ++ net/ipv4/netfilter/arp_tables.c | 48 +++---------------------- net/ipv4/netfilter/ip_tables.c | 48 +++---------------------- net/ipv6/netfilter/ip6_tables.c | 49 +++---------------------- net/netfilter/x_tables.c | 74 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 130 deletions(-)
include/linux/netfilter/x_tables.h +3 -0
@@ -251,6 +251,9 @@ int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,int xt_check_target (struct xt_tgchk_param *,unsigned int size,u_int8_t proto,bool inv_proto); +void *xt_copy_counters_from_user (const void __user *user,unsigned int len, +struct xt_counters_info *info,bool compat); + struct xt_table *xt_register_table(struct net *net, const struct xt_table *table, struct xt_table_info *bootstrap,
net/ipv4/netfilter/arp_tables.c +5 -43
67 lines not on this page · raw message
net/ipv4/netfilter/ip_tables.c +5 -43
67 lines not on this page · raw message
net/ipv6/netfilter/ip6_tables.c +5 -44
67 lines not on this page · raw message
net/netfilter/x_tables.c +74 -0
80 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 18/23 netfilter: ctnetlink: remove unnecessary inlining
In reply to Pablo Neira AyusoMany of these functions are called from control plane path. Move ctnetlink_nlmsg_size() under CONFIG_NF_CONNTRACK_EVENTS to avoid a compilation warning when CONFIG_NF_CONNTRACK_EVENTS=n.
--- net/netfilter/nf_conntrack_netlink.c | 117 ++++++++++++++--------------------- 1 file changed, 48 insertions(+), 69 deletions(-)
net/netfilter/nf_conntrack_netlink.c +48 -69
@@ -58,10 +58,9 @@ MODULE_LICENSE("GPL");static char __initdata version[] ="0.93" ; -static inline int -ctnetlink_dump_tuples_proto (struct sk_buff *skb, -const struct nf_conntrack_tuple *tuple, -struct nf_conntrack_l4proto *l4proto) +static int ctnetlink_dump_tuples_proto (struct sk_buff *skb, +const struct nf_conntrack_tuple *tuple, +struct nf_conntrack_l4proto *l4proto) {int ret =0 ;struct nlattr *nest_parms;
@@ -83,10 +82,9 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_tuples_ip (struct sk_buff *skb, -const struct nf_conntrack_tuple *tuple, -struct nf_conntrack_l3proto *l3proto) +static int ctnetlink_dump_tuples_ip (struct sk_buff *skb, +const struct nf_conntrack_tuple *tuple, +struct nf_conntrack_l3proto *l3proto) {int ret =0 ;struct nlattr *nest_parms;
@@ -106,9 +104,8 @@ nla_put_failure:return -1 ; } -static int -ctnetlink_dump_tuples (struct sk_buff *skb, -const struct nf_conntrack_tuple *tuple) +static int ctnetlink_dump_tuples (struct sk_buff *skb, +const struct nf_conntrack_tuple *tuple) {int ret;struct nf_conntrack_l3proto *l3proto;
@@ -127,9 +124,8 @@ ctnetlink_dump_tuples(struct sk_buff *skb,return ret; } -static inline int -ctnetlink_dump_zone_id (struct sk_buff *skb,int attrtype, -const struct nf_conntrack_zone *zone,int dir) +static int ctnetlink_dump_zone_id (struct sk_buff *skb,int attrtype, +const struct nf_conntrack_zone *zone,int dir) {if (zone->id ==NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)return 0 ;
@@ -141,8 +137,7 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_status (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_status (struct sk_buff *skb,const struct nf_conn *ct) {if (nla_put_be32 (skb,CTA_STATUS ,htonl (ct->status))) goto nla_put_failure;
@@ -152,8 +147,7 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_timeout (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_timeout (struct sk_buff *skb,const struct nf_conn *ct) {long timeout = ((long )ct->timeout.expires - (long )jiffies) /HZ ;
@@ -168,8 +162,7 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_protoinfo (struct sk_buff *skb,struct nf_conn *ct) +static int ctnetlink_dump_protoinfo (struct sk_buff *skb,struct nf_conn *ct) {struct nf_conntrack_l4proto *l4proto;struct nlattr *nest_proto;
@@ -193,8 +186,8 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_helpinfo (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_helpinfo (struct sk_buff *skb, +const struct nf_conn *ct) {struct nlattr *nest_helper;const struct nf_conn_help *help =nfct_help (ct);
@@ -300,8 +293,7 @@ nla_put_failure: }#ifdef CONFIG_NF_CONNTRACK_MARK -static inline int -ctnetlink_dump_mark (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_mark (struct sk_buff *skb,const struct nf_conn *ct) {if (nla_put_be32 (skb,CTA_MARK ,htonl (ct->mark))) goto nla_put_failure;
@@ -315,8 +307,7 @@ nla_put_failure:#endif #ifdef CONFIG_NF_CONNTRACK_SECMARK -static inline int -ctnetlink_dump_secctx (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_secctx (struct sk_buff *skb,const struct nf_conn *ct) {struct nlattr *nest_secctx;int len, ret;
@@ -380,8 +371,7 @@ ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)#define master_tuple (ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) -static inline int -ctnetlink_dump_master (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_master (struct sk_buff *skb,const struct nf_conn *ct) {struct nlattr *nest_parms;
@@ -426,8 +416,8 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_ct_seq_adj (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_ct_seq_adj (struct sk_buff *skb, +const struct nf_conn *ct) {struct nf_conn_seqadj *seqadj =nfct_seqadj (ct);struct nf_ct_seqadj *seq;
@@ -446,8 +436,7 @@ ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)return 0 ; } -static inline int -ctnetlink_dump_id (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_id (struct sk_buff *skb,const struct nf_conn *ct) {if (nla_put_be32 (skb,CTA_ID ,htonl ((unsigned long )ct))) goto nla_put_failure;
@@ -457,8 +446,7 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_dump_use (struct sk_buff *skb,const struct nf_conn *ct) +static int ctnetlink_dump_use (struct sk_buff *skb,const struct nf_conn *ct) {if (nla_put_be32 (skb,CTA_USE ,htonl (atomic_read (&ct->ct_general.use)))) goto nla_put_failure;
@@ -538,8 +526,7 @@ nla_put_failure:return -1 ; } -static inline size_t -ctnetlink_proto_size (const struct nf_conn *ct) +static size_t ctnetlink_proto_size (const struct nf_conn *ct) {struct nf_conntrack_l3proto *l3proto;struct nf_conntrack_l4proto *l4proto;
@@ -556,8 +543,7 @@ ctnetlink_proto_size(const struct nf_conn *ct)return len; } -static inline size_t -ctnetlink_acct_size (const struct nf_conn *ct) +static size_t ctnetlink_acct_size (const struct nf_conn *ct) {if (!nf_ct_ext_exist (ct,NF_CT_EXT_ACCT ))return 0 ;
@@ -567,8 +553,7 @@ ctnetlink_acct_size(const struct nf_conn *ct) ; } -static inline int -ctnetlink_secctx_size (const struct nf_conn *ct) +static int ctnetlink_secctx_size (const struct nf_conn *ct) {#ifdef CONFIG_NF_CONNTRACK_SECMARK int len, ret;
@@ -584,8 +569,7 @@ ctnetlink_secctx_size(const struct nf_conn *ct)#endif } -static inline size_t -ctnetlink_timestamp_size (const struct nf_conn *ct) +static size_t ctnetlink_timestamp_size (const struct nf_conn *ct) {#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP if (!nf_ct_ext_exist (ct,NF_CT_EXT_TSTAMP ))
@@ -596,8 +580,8 @@ ctnetlink_timestamp_size(const struct nf_conn *ct)#endif } -static inline size_t -ctnetlink_nlmsg_size (const struct nf_conn *ct) +#ifdef CONFIG_NF_CONNTRACK_EVENTS +static size_t ctnetlink_nlmsg_size (const struct nf_conn *ct) {return NLMSG_ALIGN (sizeof (struct nfgenmsg )) +3 *nla_total_size (0 )/* CTA_TUPLE_ORIG|REPL|MASTER */
@@ -628,7 +612,6 @@ ctnetlink_nlmsg_size(const struct nf_conn *ct) ; } -#ifdef CONFIG_NF_CONNTRACK_EVENTS static int ctnetlink_conntrack_event (unsigned int events,struct nf_ct_event *item) {
@@ -891,8 +874,8 @@ out:return skb->len; } -static inline int -ctnetlink_parse_tuple_ip (struct nlattr *attr,struct nf_conntrack_tuple *tuple) +static int ctnetlink_parse_tuple_ip (struct nlattr *attr, +struct nf_conntrack_tuple *tuple) {struct nlattr *tb[CTA_IP_MAX +1 ];struct nf_conntrack_l3proto *l3proto;
@@ -921,9 +904,8 @@ static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { [CTA_PROTO_NUM] = { .type = NLA_U8 }, }; -static inline int -ctnetlink_parse_tuple_proto (struct nlattr *attr, -struct nf_conntrack_tuple *tuple) +static int ctnetlink_parse_tuple_proto (struct nlattr *attr, +struct nf_conntrack_tuple *tuple) {struct nlattr *tb[CTA_PROTO_MAX +1 ];struct nf_conntrack_l4proto *l4proto;
@@ -1050,9 +1032,8 @@ static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = { .len = NF_CT_HELPER_NAME_LEN - 1 }, }; -static inline int -ctnetlink_parse_help (const struct nlattr *attr,char **helper_name, -struct nlattr **helpinfo) +static int ctnetlink_parse_help (const struct nlattr *attr,char **helper_name, +struct nlattr **helpinfo) {int err;struct nlattr *tb[CTA_HELP_MAX +1 ];
@@ -1463,8 +1444,8 @@ ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])#endif } -static inline int -ctnetlink_change_helper (struct nf_conn *ct,const struct nlattr *const cda[]) +static int ctnetlink_change_helper (struct nf_conn *ct, +const struct nlattr *const cda[]) {struct nf_conntrack_helper *helper;struct nf_conn_help *help =nfct_help (ct);
@@ -1524,8 +1505,8 @@ ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])return -EOPNOTSUPP ; } -static inline int -ctnetlink_change_timeout (struct nf_conn *ct,const struct nlattr *const cda[]) +static int ctnetlink_change_timeout (struct nf_conn *ct, +const struct nlattr *const cda[]) {u_int32_t timeout =ntohl (nla_get_be32 (cda[CTA_TIMEOUT ]));
@@ -1544,8 +1525,8 @@ static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = { [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED }, }; -static inline int -ctnetlink_change_protoinfo (struct nf_conn *ct,const struct nlattr *const cda[]) +static int ctnetlink_change_protoinfo (struct nf_conn *ct, +const struct nlattr *const cda[]) {const struct nlattr *attr = cda[CTA_PROTOINFO ];struct nlattr *tb[CTA_PROTOINFO_MAX +1 ];
@@ -1571,8 +1552,8 @@ static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = { [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 }, }; -static inline int -change_seq_adj (struct nf_ct_seqadj *seq,const struct nlattr *const attr) +static int change_seq_adj (struct nf_ct_seqadj *seq, +const struct nlattr *const attr) {int err;struct nlattr *cda[CTA_SEQADJ_MAX +1 ];
@@ -2405,10 +2386,9 @@ static struct nfnl_ct_hook ctnetlink_glue_hook = {* EXPECT ***********************************************************************/ -static inline int -ctnetlink_exp_dump_tuple (struct sk_buff *skb, -const struct nf_conntrack_tuple *tuple, -enum ctattr_expect type) +static int ctnetlink_exp_dump_tuple (struct sk_buff *skb, +const struct nf_conntrack_tuple *tuple, +enum ctattr_expect type) {struct nlattr *nest_parms;
@@ -2425,10 +2405,9 @@ nla_put_failure:return -1 ; } -static inline int -ctnetlink_exp_dump_mask (struct sk_buff *skb, -const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_tuple_mask *mask) +static int ctnetlink_exp_dump_mask (struct sk_buff *skb, +const struct nf_conntrack_tuple *tuple, +const struct nf_conntrack_tuple_mask *mask) {int ret;struct nf_conntrack_l3proto *l3proto;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 19/23 netfilter: connlabels: move helpers to xt_connlabel
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Currently labels can only be set either by iptables connlabel match or via ctnetlink.
Before adding nftables set support, clean up the clabel core and move helpers that nft will not need after all to the xtables module.
--- include/net/netfilter/nf_conntrack_labels.h | 1 - net/netfilter/nf_conntrack_labels.c | 19 +------------------ net/netfilter/xt_connlabel.c | 12 +++++++++++- 3 files changed, 12 insertions(+), 20 deletions(-)
include/net/netfilter/nf_conntrack_labels.h +0 -1
@@ -45,7 +45,6 @@ static inline struct nf_conn_labels *nf_ct_labels_ext_add(struct nf_conn *ct)#endif } -bool nf_connlabel_match (const struct nf_conn *ct,u16 bit);int nf_connlabel_set (struct nf_conn *ct,u16 bit); int nf_connlabels_replace(struct nf_conn *ct,
net/netfilter/nf_conntrack_labels.c +1 -18
@@ -16,28 +16,11 @@static spinlock_t nf_connlabels_lock; -static unsigned int label_bits (const struct nf_conn_labels *l) -{ -unsigned int longs = l->words; -return longs *BITS_PER_LONG ; -} - -bool nf_connlabel_match (const struct nf_conn *ct,u16 bit) -{ -struct nf_conn_labels *labels =nf_ct_labels_find (ct); - -if (!labels) -return false; - -return bit <label_bits (labels) &&test_bit (bit, labels->bits); -} -EXPORT_SYMBOL_GPL (nf_connlabel_match); -int nf_connlabel_set (struct nf_conn *ct,u16 bit) {struct nf_conn_labels *labels =nf_ct_labels_find (ct); -if (!labels || bit >=label_bits (labels)) +if (!labels ||BIT_WORD (bit) >= labels->words)return -ENOSPC ;if (test_bit (bit, labels->bits))
net/netfilter/xt_connlabel.c +11 -1
@@ -18,6 +18,16 @@ MODULE_DESCRIPTION("Xtables: add/match connection trackling labels");MODULE_ALIAS ("ipt_connlabel" );MODULE_ALIAS ("ip6t_connlabel" ); +static bool connlabel_match (const struct nf_conn *ct,u16 bit) +{ +struct nf_conn_labels *labels =nf_ct_labels_find (ct); + +if (!labels) +return false; + +return BIT_WORD (bit) < labels->words &&test_bit (bit, labels->bits); +} +static bool connlabel_mt (const struct sk_buff *skb,struct xt_action_param *par) {
@@ -33,7 +43,7 @@ connlabel_mt(const struct sk_buff *skb, struct xt_action_param *par)if (info->options &XT_CONNLABEL_OP_SET )return (nf_connlabel_set (ct, info->bit) ==0 ) ^ invert; -return nf_connlabel_match (ct, info->bit) ^ invert; +return connlabel_match (ct, info->bit) ^ invert; }static int connlabel_mt_check (const struct xt_mtchk_param *par)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 20/23 netfilter: labels: don't emit ct event if labels were not changed
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
make the replace function only send a ctnetlink event if the contents of the new set is different.
Otherwise 'ct label set ct label | bar'
will cause netlink event storm since we "replace" labels for each packet.
--- net/netfilter/nf_conntrack_labels.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
net/netfilter/nf_conntrack_labels.c +10 -6
@@ -33,14 +33,18 @@ int nf_connlabel_set(struct nf_conn *ct, u16 bit) }EXPORT_SYMBOL_GPL (nf_connlabel_set); -static void replace_u32 (u32 *address,u32 mask,u32 new) +static int replace_u32 (u32 *address,u32 mask,u32 new) {u32 old, tmp;do { old = *address; tmp = (old & mask) ^ new; +if (old == tmp) +return 0 ; }while (cmpxchg (address, old, tmp) != old); + +return 1 ; } int nf_connlabels_replace(struct nf_conn *ct,
@@ -49,6 +53,7 @@ int nf_connlabels_replace(struct nf_conn *ct, {struct nf_conn_labels *labels;unsigned int size, i; +int changed =0 ;u32 *dst; labels =nf_ct_labels_find (ct);
@@ -60,16 +65,15 @@ int nf_connlabels_replace(struct nf_conn *ct, words32 = size /sizeof (u32); dst = (u32 *) labels->bits; -if (words32) { -for (i =0 ; i < words32; i++) -replace_u32 (&dst[i], mask ? ~mask[i] :0 , data[i]); - } +for (i =0 ; i < words32; i++) + changed |=replace_u32 (&dst[i], mask ? ~mask[i] :0 , data[i]); size /=sizeof (u32);for (i = words32; i < size; i++)/* pad */ replace_u32 (&dst[i],0 ,0 ); -nf_conntrack_event_cache (IPCT_LABEL , ct); +if (changed) +nf_conntrack_event_cache (IPCT_LABEL , ct);return 0 ; }EXPORT_SYMBOL_GPL (nf_connlabels_replace);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 21/23 netfilter: connlabels: change nf_connlabels_get bit arg to 'highest used'
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
nf_connlabel_set() takes the bit number that we would like to set. nf_connlabels_get() however took the number of bits that we want to support.
So e.g. nf_connlabels_get(32) support bits 0 to 31, but not 32. This changes nf_connlabels_get() to take the highest bit that we want to set.
Callers then don't have to cope with a potential integer wrap when using nf_connlabels_get(bit + 1) anymore.
Current callers are fine, this change is only to make folloup nft ct label set support simpler.
--- include/net/netfilter/nf_conntrack_labels.h | 4 ++-- net/netfilter/nf_conntrack_labels.c | 9 +++++---- net/netfilter/nft_ct.c | 2 ++ net/netfilter/xt_connlabel.c | 2 +- net/openvswitch/conntrack.c | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-)
include/net/netfilter/nf_conntrack_labels.h +2 -2
@@ -53,11 +53,11 @@ int nf_connlabels_replace(struct nf_conn *ct,#ifdef CONFIG_NF_CONNTRACK_LABELS int nf_conntrack_labels_init (void );void nf_conntrack_labels_fini (void ); -int nf_connlabels_get (struct net *net,unsigned int n_bits); +int nf_connlabels_get (struct net *net,unsigned int bit);void nf_connlabels_put (struct net *net);#else static inline int nf_conntrack_labels_init (void ) {return 0 ; }static inline void nf_conntrack_labels_fini (void ) {} -static inline int nf_connlabels_get (struct net *net,unsigned int n_bits) {return 0 ; } +static inline int nf_connlabels_get (struct net *net,unsigned int bit) {return 0 ; }static inline void nf_connlabels_put (struct net *net) {}#endif
net/netfilter/nf_conntrack_labels.c +5 -4
@@ -78,15 +78,14 @@ int nf_connlabels_replace(struct nf_conn *ct, }EXPORT_SYMBOL_GPL (nf_connlabels_replace); -int nf_connlabels_get (struct net *net,unsigned int n_bits) +int nf_connlabels_get (struct net *net,unsigned int bits) {size_t words; -if (n_bits > (NF_CT_LABELS_MAX_SIZE *BITS_PER_BYTE )) + words =BIT_WORD (bits) +1 ; +if (words >NF_CT_LABELS_MAX_SIZE /sizeof (long ))return -ERANGE ; - words =BITS_TO_LONGS (n_bits); -spin_lock (&nf_connlabels_lock); net->ct.labels_used++;if (words > net->ct.label_words)
@@ -115,6 +114,8 @@ static struct nf_ct_ext_type labels_extend __read_mostly = {int nf_conntrack_labels_init (void ) { +BUILD_BUG_ON (NF_CT_LABELS_MAX_SIZE /sizeof (long ) >=U8_MAX ); +spin_lock_init (&nf_connlabels_lock);return nf_ct_extend_register (&labels_extend); }
net/netfilter/nft_ct.c +2 -0
@@ -484,6 +484,8 @@ static struct nft_expr_type nft_ct_type __read_mostly = {static int __init nft_ct_module_init (void ) { +BUILD_BUG_ON (NF_CT_LABELS_MAX_SIZE >NFT_REG_SIZE ); +return nft_register_expr (&nft_ct_type); }
net/netfilter/xt_connlabel.c +1 -1
@@ -65,7 +65,7 @@ static int connlabel_mt_check(const struct xt_mtchk_param *par)return ret; } - ret =nf_connlabels_get (par->net, info->bit +1 ); + ret =nf_connlabels_get (par->net, info->bit);if (ret <0 )nf_ct_l3proto_module_put (par->family);return ret;
net/openvswitch/conntrack.c +1 -1
@@ -1344,7 +1344,7 @@ void ovs_ct_init(struct net *net)unsigned int n_bits =sizeof (struct ovs_key_ct_labels ) *BITS_PER_BYTE ;struct ovs_net *ovs_net =net_generic (net, ovs_net_id); -if (nf_connlabels_get (net, n_bits)) { +if (nf_connlabels_get (net, n_bits -1 )) { ovs_net->xt_label = false;OVS_NLERR (true,"Failed to set connlabel length" ); }else {
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 22/23 netfilter: ctnetlink: restore inlining for netlink message size calculation
In reply to Pablo Neira AyusoCalm down gcc warnings:
net/netfilter/nf_conntrack_netlink.c:529:15: warning: 'ctnetlink_proto_size' defined but not used [-Wunused-function]
static size_t ctnetlink_proto_size(const struct nf_conn *ct)
^
net/netfilter/nf_conntrack_netlink.c:546:15: warning: 'ctnetlink_acct_size' defined but not used [-Wunused-function]
static size_t ctnetlink_acct_size(const struct nf_conn *ct)
^
net/netfilter/nf_conntrack_netlink.c:556:12: warning: 'ctnetlink_secctx_size' defined but not used [-Wunused-function]
static int ctnetlink_secctx_size(const struct nf_conn *ct)
^
net/netfilter/nf_conntrack_netlink.c:572:15: warning: 'ctnetlink_timestamp_size' defined but not used [-Wunused-function]
static size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
^So gcc compiles them out when CONFIG_NF_CONNTRACK_EVENTS and CONFIG_NETFILTER_NETLINK_GLUE_CT are not set.
--- net/netfilter/nf_conntrack_netlink.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_netlink.c +5 -5
@@ -336,7 +336,7 @@ nla_put_failure:#endif #ifdef CONFIG_NF_CONNTRACK_LABELS -static int ctnetlink_label_size (const struct nf_conn *ct) +static inline int ctnetlink_label_size (const struct nf_conn *ct) {struct nf_conn_labels *labels =nf_ct_labels_find (ct);
@@ -526,7 +526,7 @@ nla_put_failure:return -1 ; } -static size_t ctnetlink_proto_size (const struct nf_conn *ct) +static inline size_t ctnetlink_proto_size (const struct nf_conn *ct) {struct nf_conntrack_l3proto *l3proto;struct nf_conntrack_l4proto *l4proto;
@@ -543,7 +543,7 @@ static size_t ctnetlink_proto_size(const struct nf_conn *ct)return len; } -static size_t ctnetlink_acct_size (const struct nf_conn *ct) +static inline size_t ctnetlink_acct_size (const struct nf_conn *ct) {if (!nf_ct_ext_exist (ct,NF_CT_EXT_ACCT ))return 0 ;
@@ -553,7 +553,7 @@ static size_t ctnetlink_acct_size(const struct nf_conn *ct) ; } -static int ctnetlink_secctx_size (const struct nf_conn *ct) +static inline int ctnetlink_secctx_size (const struct nf_conn *ct) {#ifdef CONFIG_NF_CONNTRACK_SECMARK int len, ret;
@@ -569,7 +569,7 @@ static int ctnetlink_secctx_size(const struct nf_conn *ct)#endif } -static size_t ctnetlink_timestamp_size (const struct nf_conn *ct) +static inline size_t ctnetlink_timestamp_size (const struct nf_conn *ct) {#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP if (!nf_ct_ext_exist (ct,NF_CT_EXT_TSTAMP ))
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 23/23 netfilter: conntrack: don't acquire lock during seq_printf
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
read access doesn't need any lock here.
--- net/netfilter/nf_conntrack_proto_sctp.c | 8 +------- net/netfilter/nf_conntrack_proto_tcp.c | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +1 -7
@@ -191,13 +191,7 @@ static void sctp_print_tuple(struct seq_file *s,/* Print out the private part of the conntrack. */ static void sctp_print_conntrack (struct seq_file *s,struct nf_conn *ct) { -enum sctp_conntrack state; - -spin_lock_bh (&ct->lock); - state = ct->proto.sctp.state; -spin_unlock_bh (&ct->lock); - -seq_printf (s,"%s " , sctp_conntrack_names[state]); +seq_printf (s,"%s " , sctp_conntrack_names[ct->proto.sctp.state]); }#define for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) \
net/netfilter/nf_conntrack_proto_tcp.c +1 -7
@@ -313,13 +313,7 @@ static void tcp_print_tuple(struct seq_file *s,/* Print out the private part of the conntrack. */ static void tcp_print_conntrack (struct seq_file *s,struct nf_conn *ct) { -enum tcp_conntrack state; - -spin_lock_bh (&ct->lock); - state = ct->proto.tcp.state; -spin_unlock_bh (&ct->lock); - -seq_printf (s,"%s " , tcp_conntrack_names[state]); +seq_printf (s,"%s " , tcp_conntrack_names[ct->proto.tcp.state]); }static unsigned int get_conntrack_index (const struct tcphdr *tcph)
Signature · 1 line
2.1.4
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Fri, 22 Apr 2016 15:39:31 +0200
Quoted 4 lines
The following patchset contains Netfilter updates for your net-next tree, mostly from Florian Westphal to sort out the lack of sufficient validation in
x_tablesand connlabel preparation patches to addnf_tablessupport. They are:
...
Quoted 3 lines
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Pulled, thanks a lot Pablo!
Re: [PATCH 07/23] netfilter: x_tables: check standard target size too
In reply to Pablo Neira AyusoQuoted 4 lines · Pablo Neira Ayuso
From: Florian Westphal <[email protected]>
We have targets and standard targets -- the latter carries a verdict.
Quoted 8 lines
The ip/ip6tables validation functions will access
t->verdictfor the standard targets to fetch the jump offset or verdict for chainloop detection, but this happens before the targets get checked/validated.Thus we also need to check for verdict presence here, else
t->verdictcan point right after a blob.Spotted with UBSAN while testing malformed blobs.
This breaks iptables on PPC32.
# iptables -nL iptables v1.4.21: can't initialize iptables table `filter': Table does not exist (do you need to insmod?) Perhaps iptables or your kernel needs to be upgraded. # modprobe iptable-filter FATAL: Error inserting iptable_filter (/lib/modules/4.7.0-rc1/kernel/net/ipv4/netfilter/iptable_filter.ko): Invalid argument
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Florian Westphal
maintainerRe: [PATCH 07/23] netfilter: x_tables: check standard target size too
Quoted 8 lines · Andreas Schwab
Quoted 8 lines
From: Florian Westphal <[email protected]>
We have targets and standard targets -- the latter carries a verdict.
The ip/ip6tables validation functions will access
t->verdictfor the standard targets to fetch the jump offset or verdict for chainloop detection, but this happens before the targets get checked/validated.
Quoted 6 lines
Quoted 4 lines
Thus we also need to check for verdict presence here, else
t->verdictcan point right after a blob.Spotted with UBSAN while testing malformed blobs.
This breaks iptables on PPC32.
Yes, we got bug report for arm32, I'm sorry about this -- only 32bit platform I tested was i686 and that only needs 4byte alignment for u64.
This fix should help:
Pablo Neira Ayuso
maintainerRe: [PATCH 07/23] netfilter: x_tables: check standard target size too
Quoted 14 lines · Florian Westphal
Quoted 13 lines · Andreas Schwab
Quoted 12 lines
From: Florian Westphal <[email protected]>
We have targets and standard targets -- the latter carries a verdict.
The ip/ip6tables validation functions will access
t->verdictfor the standard targets to fetch the jump offset or verdict for chainloop detection, but this happens before the targets get checked/validated.Thus we also need to check for verdict presence here, else
t->verdictcan point right after a blob.Spotted with UBSAN while testing malformed blobs.
Quoted 8 lines
Quoted 1 line
This breaks iptables on PPC32.
Yes, we got bug report for arm32, I'm sorry about this -- only 32bit platform I tested was i686 and that only needs 4byte alignment for u64.
This fix should help:
Short notice: Will be handing over this in a pull request for David at some point today.