Skip to content

Commit 18219d3

Browse files
Jan Engelhardtkaber
authored andcommitted
netfilter: ebtables: do centralized size checking
Signed-off-by: Jan Engelhardt <jengelh@medozas.de> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent d2f2603 commit 18219d3

20 files changed

Lines changed: 104 additions & 88 deletions

‎include/linux/netfilter_bridge/ebtables.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ struct ebt_match
215215
int (*check)(const char *tablename, unsigned int hookmask,
216216
const struct ebt_entry *e, void *matchdata, unsigned int datalen);
217217
void (*destroy)(void *matchdata, unsigned int datalen);
218+
unsigned int matchsize;
218219
struct module *me;
219220
};
220221

@@ -229,6 +230,7 @@ struct ebt_watcher
229230
int (*check)(const char *tablename, unsigned int hookmask,
230231
const struct ebt_entry *e, void *watcherdata, unsigned int datalen);
231232
void (*destroy)(void *watcherdata, unsigned int datalen);
233+
unsigned int targetsize;
232234
struct module *me;
233235
};
234236

@@ -244,6 +246,7 @@ struct ebt_target
244246
int (*check)(const char *tablename, unsigned int hookmask,
245247
const struct ebt_entry *e, void *targetdata, unsigned int datalen);
246248
void (*destroy)(void *targetdata, unsigned int datalen);
249+
unsigned int targetsize;
247250
struct module *me;
248251
};
249252

‎net/bridge/netfilter/ebt_802_3.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* May 2003
88
*
99
*/
10-
10+
#include <linux/module.h>
11+
#include <linux/netfilter/x_tables.h>
1112
#include <linux/netfilter_bridge/ebtables.h>
1213
#include <linux/netfilter_bridge/ebt_802_3.h>
13-
#include <linux/module.h>
1414

1515
static int ebt_filter_802_3(const struct sk_buff *skb, const struct net_device *in,
1616
const struct net_device *out, const void *data, unsigned int datalen)
@@ -42,8 +42,6 @@ static int ebt_802_3_check(const char *tablename, unsigned int hookmask,
4242
{
4343
const struct ebt_802_3_info *info = data;
4444

45-
if (datalen < sizeof(struct ebt_802_3_info))
46-
return -EINVAL;
4745
if (info->bitmask & ~EBT_802_3_MASK || info->invflags & ~EBT_802_3_MASK)
4846
return -EINVAL;
4947

@@ -54,6 +52,7 @@ static struct ebt_match filter_802_3 __read_mostly = {
5452
.name = EBT_802_3_MATCH,
5553
.match = ebt_filter_802_3,
5654
.check = ebt_802_3_check,
55+
.matchsize = XT_ALIGN(sizeof(struct ebt_802_3_info)),
5756
.me = THIS_MODULE,
5857
};
5958

‎net/bridge/netfilter/ebt_among.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ static struct ebt_match filter_among __read_mostly = {
216216
.name = EBT_AMONG_MATCH,
217217
.match = ebt_filter_among,
218218
.check = ebt_among_check,
219+
.matchsize = -1, /* special case */
219220
.me = THIS_MODULE,
220221
};
221222

‎net/bridge/netfilter/ebt_arp.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
* April, 2002
99
*
1010
*/
11-
12-
#include <linux/netfilter_bridge/ebtables.h>
13-
#include <linux/netfilter_bridge/ebt_arp.h>
1411
#include <linux/if_arp.h>
1512
#include <linux/if_ether.h>
1613
#include <linux/module.h>
14+
#include <linux/netfilter/x_tables.h>
15+
#include <linux/netfilter_bridge/ebtables.h>
16+
#include <linux/netfilter_bridge/ebt_arp.h>
1717

1818
static int ebt_filter_arp(const struct sk_buff *skb, const struct net_device *in,
1919
const struct net_device *out, const void *data, unsigned int datalen)
@@ -105,8 +105,6 @@ static int ebt_arp_check(const char *tablename, unsigned int hookmask,
105105
{
106106
const struct ebt_arp_info *info = data;
107107

108-
if (datalen != EBT_ALIGN(sizeof(struct ebt_arp_info)))
109-
return -EINVAL;
110108
if ((e->ethproto != htons(ETH_P_ARP) &&
111109
e->ethproto != htons(ETH_P_RARP)) ||
112110
e->invflags & EBT_IPROTO)
@@ -120,6 +118,7 @@ static struct ebt_match filter_arp __read_mostly = {
120118
.name = EBT_ARP_MATCH,
121119
.match = ebt_filter_arp,
122120
.check = ebt_arp_check,
121+
.matchsize = XT_ALIGN(sizeof(struct ebt_arp_info)),
123122
.me = THIS_MODULE,
124123
};
125124

‎net/bridge/netfilter/ebt_arpreply.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
* August, 2003
99
*
1010
*/
11-
12-
#include <linux/netfilter_bridge/ebtables.h>
13-
#include <linux/netfilter_bridge/ebt_arpreply.h>
1411
#include <linux/if_arp.h>
1512
#include <net/arp.h>
1613
#include <linux/module.h>
14+
#include <linux/netfilter/x_tables.h>
15+
#include <linux/netfilter_bridge/ebtables.h>
16+
#include <linux/netfilter_bridge/ebt_arpreply.h>
1717

1818
static int ebt_target_reply(struct sk_buff *skb, unsigned int hooknr,
1919
const struct net_device *in, const struct net_device *out,
@@ -63,8 +63,6 @@ static int ebt_target_reply_check(const char *tablename, unsigned int hookmask,
6363
{
6464
const struct ebt_arpreply_info *info = data;
6565

66-
if (datalen != EBT_ALIGN(sizeof(struct ebt_arpreply_info)))
67-
return -EINVAL;
6866
if (BASE_CHAIN && info->target == EBT_RETURN)
6967
return -EINVAL;
7068
if (e->ethproto != htons(ETH_P_ARP) ||
@@ -80,6 +78,7 @@ static struct ebt_target reply_target __read_mostly = {
8078
.name = EBT_ARPREPLY_TARGET,
8179
.target = ebt_target_reply,
8280
.check = ebt_target_reply_check,
81+
.targetsize = XT_ALIGN(sizeof(struct ebt_arpreply_info)),
8382
.me = THIS_MODULE,
8483
};
8584

‎net/bridge/netfilter/ebt_dnat.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* June, 2002
88
*
99
*/
10-
10+
#include <linux/module.h>
11+
#include <net/sock.h>
1112
#include <linux/netfilter.h>
13+
#include <linux/netfilter/x_tables.h>
1214
#include <linux/netfilter_bridge/ebtables.h>
1315
#include <linux/netfilter_bridge/ebt_nat.h>
14-
#include <linux/module.h>
15-
#include <net/sock.h>
1616

1717
static int ebt_target_dnat(struct sk_buff *skb, unsigned int hooknr,
1818
const struct net_device *in, const struct net_device *out,
@@ -39,8 +39,6 @@ static int ebt_target_dnat_check(const char *tablename, unsigned int hookmask,
3939
(hookmask & ~((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT)))) &&
4040
(strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
4141
return -EINVAL;
42-
if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
43-
return -EINVAL;
4442
if (INVALID_TARGET)
4543
return -EINVAL;
4644
return 0;
@@ -50,6 +48,7 @@ static struct ebt_target dnat __read_mostly = {
5048
.name = EBT_DNAT_TARGET,
5149
.target = ebt_target_dnat,
5250
.check = ebt_target_dnat_check,
51+
.targetsize = XT_ALIGN(sizeof(struct ebt_nat_info)),
5352
.me = THIS_MODULE,
5453
};
5554

‎net/bridge/netfilter/ebt_ip.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* Innominate Security Technologies AG <mhopf@innominate.com>
1212
* September, 2002
1313
*/
14-
15-
#include <linux/netfilter_bridge/ebtables.h>
16-
#include <linux/netfilter_bridge/ebt_ip.h>
1714
#include <linux/ip.h>
1815
#include <net/ip.h>
1916
#include <linux/in.h>
2017
#include <linux/module.h>
18+
#include <linux/netfilter/x_tables.h>
19+
#include <linux/netfilter_bridge/ebtables.h>
20+
#include <linux/netfilter_bridge/ebt_ip.h>
2121

2222
struct tcpudphdr {
2323
__be16 src;
@@ -83,8 +83,6 @@ static int ebt_ip_check(const char *tablename, unsigned int hookmask,
8383
{
8484
const struct ebt_ip_info *info = data;
8585

86-
if (datalen != EBT_ALIGN(sizeof(struct ebt_ip_info)))
87-
return -EINVAL;
8886
if (e->ethproto != htons(ETH_P_IP) ||
8987
e->invflags & EBT_IPROTO)
9088
return -EINVAL;
@@ -111,6 +109,7 @@ static struct ebt_match filter_ip __read_mostly = {
111109
.name = EBT_IP_MATCH,
112110
.match = ebt_filter_ip,
113111
.check = ebt_ip_check,
112+
.matchsize = XT_ALIGN(sizeof(struct ebt_ip_info)),
114113
.me = THIS_MODULE,
115114
};
116115

‎net/bridge/netfilter/ebt_ip6.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
*
1414
* Jan, 2008
1515
*/
16-
17-
#include <linux/netfilter_bridge/ebtables.h>
18-
#include <linux/netfilter_bridge/ebt_ip6.h>
1916
#include <linux/ipv6.h>
2017
#include <net/ipv6.h>
2118
#include <linux/in.h>
2219
#include <linux/module.h>
2320
#include <net/dsfield.h>
21+
#include <linux/netfilter/x_tables.h>
22+
#include <linux/netfilter_bridge/ebtables.h>
23+
#include <linux/netfilter_bridge/ebt_ip6.h>
2424

2525
struct tcpudphdr {
2626
__be16 src;
@@ -97,8 +97,6 @@ static int ebt_ip6_check(const char *tablename, unsigned int hookmask,
9797
{
9898
struct ebt_ip6_info *info = (struct ebt_ip6_info *)data;
9999

100-
if (datalen != EBT_ALIGN(sizeof(struct ebt_ip6_info)))
101-
return -EINVAL;
102100
if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
103101
return -EINVAL;
104102
if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
@@ -125,6 +123,7 @@ static struct ebt_match filter_ip6 =
125123
.name = EBT_IP6_MATCH,
126124
.match = ebt_filter_ip6,
127125
.check = ebt_ip6_check,
126+
.matchsize = XT_ALIGN(sizeof(struct ebt_ip6_info)),
128127
.me = THIS_MODULE,
129128
};
130129

‎net/bridge/netfilter/ebt_limit.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
* September, 2003
1111
*
1212
*/
13-
14-
#include <linux/netfilter_bridge/ebtables.h>
15-
#include <linux/netfilter_bridge/ebt_limit.h>
1613
#include <linux/module.h>
17-
1814
#include <linux/netdevice.h>
1915
#include <linux/spinlock.h>
16+
#include <linux/netfilter/x_tables.h>
17+
#include <linux/netfilter_bridge/ebtables.h>
18+
#include <linux/netfilter_bridge/ebt_limit.h>
2019

2120
static DEFINE_SPINLOCK(limit_lock);
2221

@@ -71,9 +70,6 @@ static int ebt_limit_check(const char *tablename, unsigned int hookmask,
7170
{
7271
struct ebt_limit_info *info = data;
7372

74-
if (datalen != EBT_ALIGN(sizeof(struct ebt_limit_info)))
75-
return -EINVAL;
76-
7773
/* Check for overflow. */
7874
if (info->burst == 0 ||
7975
user2credits(info->avg * info->burst) < user2credits(info->avg)) {
@@ -94,6 +90,7 @@ static struct ebt_match ebt_limit_reg __read_mostly = {
9490
.name = EBT_LIMIT_MATCH,
9591
.match = ebt_limit_match,
9692
.check = ebt_limit_check,
93+
.matchsize = XT_ALIGN(sizeof(struct ebt_limit_info)),
9794
.me = THIS_MODULE,
9895
};
9996

‎net/bridge/netfilter/ebt_log.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* April, 2002
99
*
1010
*/
11-
12-
#include <linux/netfilter_bridge/ebtables.h>
13-
#include <linux/netfilter_bridge/ebt_log.h>
14-
#include <linux/netfilter.h>
1511
#include <linux/module.h>
1612
#include <linux/ip.h>
1713
#include <linux/in.h>
@@ -21,6 +17,10 @@
2117
#include <linux/ipv6.h>
2218
#include <net/ipv6.h>
2319
#include <linux/in6.h>
20+
#include <linux/netfilter/x_tables.h>
21+
#include <linux/netfilter_bridge/ebtables.h>
22+
#include <linux/netfilter_bridge/ebt_log.h>
23+
#include <linux/netfilter.h>
2424

2525
static DEFINE_SPINLOCK(ebt_log_lock);
2626

@@ -29,8 +29,6 @@ static int ebt_log_check(const char *tablename, unsigned int hookmask,
2929
{
3030
struct ebt_log_info *info = data;
3131

32-
if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
33-
return -EINVAL;
3432
if (info->bitmask & ~EBT_LOG_MASK)
3533
return -EINVAL;
3634
if (info->loglevel >= 8)
@@ -218,6 +216,7 @@ static struct ebt_watcher log =
218216
.name = EBT_LOG_WATCHER,
219217
.watcher = ebt_log,
220218
.check = ebt_log_check,
219+
.targetsize = XT_ALIGN(sizeof(struct ebt_log_info)),
221220
.me = THIS_MODULE,
222221
};
223222

0 commit comments

Comments
 (0)