Skip to content

Commit 4203f2a

Browse files
Liping Zhanggregkh
authored andcommitted
netfilter: nfnl_cthelper: fix incorrect helper->expect_class_max
[ Upstream commit ae5c682113f9f94cc5e76f92cf041ee624c173ee ] The helper->expect_class_max must be set to the total number of expect_policy minus 1, since we will use the statement "if (class > helper->expect_class_max)" to validate the CTA_EXPECT_CLASS attr in ctnetlink_alloc_expect. So for compatibility, set the helper->expect_class_max to the NFCTH_POLICY_SET_NUM attr's value minus 1. Also: it's invalid when the NFCTH_POLICY_SET_NUM attr's value is zero. 1. this will result "expect_policy = kzalloc(0, GFP_KERNEL);"; 2. we cannot set the helper->expect_class_max to a proper value. So if nla_get_be32(tb[NFCTH_POLICY_SET_NUM]) is zero, report -EINVAL to the userspace. Signed-off-by: Liping Zhang <zlpnobody@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Sasha Levin <alexander.levin@verizon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent fa02902 commit 4203f2a

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

net/netfilter/nfnetlink_cthelper.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,
161161
int i, ret;
162162
struct nf_conntrack_expect_policy *expect_policy;
163163
struct nlattr *tb[NFCTH_POLICY_SET_MAX+1];
164+
unsigned int class_max;
164165

165166
ret = nla_parse_nested(tb, NFCTH_POLICY_SET_MAX, attr,
166167
nfnl_cthelper_expect_policy_set);
@@ -170,19 +171,18 @@ nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,
170171
if (!tb[NFCTH_POLICY_SET_NUM])
171172
return -EINVAL;
172173

173-
helper->expect_class_max =
174-
ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
175-
176-
if (helper->expect_class_max != 0 &&
177-
helper->expect_class_max > NF_CT_MAX_EXPECT_CLASSES)
174+
class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
175+
if (class_max == 0)
176+
return -EINVAL;
177+
if (class_max > NF_CT_MAX_EXPECT_CLASSES)
178178
return -EOVERFLOW;
179179

180180
expect_policy = kzalloc(sizeof(struct nf_conntrack_expect_policy) *
181-
helper->expect_class_max, GFP_KERNEL);
181+
class_max, GFP_KERNEL);
182182
if (expect_policy == NULL)
183183
return -ENOMEM;
184184

185-
for (i=0; i<helper->expect_class_max; i++) {
185+
for (i = 0; i < class_max; i++) {
186186
if (!tb[NFCTH_POLICY_SET+i])
187187
goto err;
188188

@@ -191,6 +191,8 @@ nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,
191191
if (ret < 0)
192192
goto err;
193193
}
194+
195+
helper->expect_class_max = class_max - 1;
194196
helper->expect_policy = expect_policy;
195197
return 0;
196198
err:
@@ -377,10 +379,10 @@ nfnl_cthelper_dump_policy(struct sk_buff *skb,
377379
goto nla_put_failure;
378380

379381
if (nla_put_be32(skb, NFCTH_POLICY_SET_NUM,
380-
htonl(helper->expect_class_max)))
382+
htonl(helper->expect_class_max + 1)))
381383
goto nla_put_failure;
382384

383-
for (i=0; i<helper->expect_class_max; i++) {
385+
for (i = 0; i < helper->expect_class_max + 1; i++) {
384386
nest_parms2 = nla_nest_start(skb,
385387
(NFCTH_POLICY_SET+i) | NLA_F_NESTED);
386388
if (nest_parms2 == NULL)

0 commit comments

Comments
 (0)