Skip to content

Commit 8f15b50

Browse files
devnexenummakynes
authored andcommitted
netfilter: ctnetlink: use netlink policy range checks
Replace manual range and mask validations with netlink policy annotations in ctnetlink code paths, so that the netlink core rejects invalid values early and can generate extack errors. - CTA_PROTOINFO_TCP_STATE: reject values > TCP_CONNTRACK_SYN_SENT2 at policy level, removing the manual >= TCP_CONNTRACK_MAX check. - CTA_PROTOINFO_TCP_WSCALE_ORIGINAL/REPLY: reject values > TCP_MAX_WSCALE (14). The normal TCP option parsing path already clamps to this value, but the ctnetlink path accepted 0-255, causing undefined behavior when used as a u32 shift count. - CTA_FILTER_ORIG_FLAGS/REPLY_FLAGS: use NLA_POLICY_MASK with CTA_FILTER_F_ALL, removing the manual mask checks. - CTA_EXPECT_FLAGS: use NLA_POLICY_MASK with NF_CT_EXPECT_MASK, adding a new mask define grouping all valid expect flags. Extracted from a broader nf-next patch by Florian Westphal, scoped to ctnetlink for the fixes tree. Fixes: c8e2078 ("[NETFILTER]: ctnetlink: add support for internal tcp connection tracking flags handling") Signed-off-by: David Carlier <[email protected]> Co-developed-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 6a2b724 commit 8f15b50

3 files changed

Lines changed: 12 additions & 18 deletions

File tree

include/uapi/linux/netfilter/nf_conntrack_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,9 @@ enum ip_conntrack_expect_events {
159159
#define NF_CT_EXPECT_INACTIVE 0x2
160160
#define NF_CT_EXPECT_USERSPACE 0x4
161161

162+
#ifdef __KERNEL__
163+
#define NF_CT_EXPECT_MASK (NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE | \
164+
NF_CT_EXPECT_USERSPACE)
165+
#endif
162166

163167
#endif /* _UAPI_NF_CONNTRACK_COMMON_H */

net/netfilter/nf_conntrack_netlink.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,8 @@ struct ctnetlink_filter {
910910
};
911911

912912
static const struct nla_policy cta_filter_nla_policy[CTA_FILTER_MAX + 1] = {
913-
[CTA_FILTER_ORIG_FLAGS] = { .type = NLA_U32 },
914-
[CTA_FILTER_REPLY_FLAGS] = { .type = NLA_U32 },
913+
[CTA_FILTER_ORIG_FLAGS] = NLA_POLICY_MASK(NLA_U32, CTA_FILTER_F_ALL),
914+
[CTA_FILTER_REPLY_FLAGS] = NLA_POLICY_MASK(NLA_U32, CTA_FILTER_F_ALL),
915915
};
916916

917917
static int ctnetlink_parse_filter(const struct nlattr *attr,
@@ -925,17 +925,11 @@ static int ctnetlink_parse_filter(const struct nlattr *attr,
925925
if (ret)
926926
return ret;
927927

928-
if (tb[CTA_FILTER_ORIG_FLAGS]) {
928+
if (tb[CTA_FILTER_ORIG_FLAGS])
929929
filter->orig_flags = nla_get_u32(tb[CTA_FILTER_ORIG_FLAGS]);
930-
if (filter->orig_flags & ~CTA_FILTER_F_ALL)
931-
return -EOPNOTSUPP;
932-
}
933930

934-
if (tb[CTA_FILTER_REPLY_FLAGS]) {
931+
if (tb[CTA_FILTER_REPLY_FLAGS])
935932
filter->reply_flags = nla_get_u32(tb[CTA_FILTER_REPLY_FLAGS]);
936-
if (filter->reply_flags & ~CTA_FILTER_F_ALL)
937-
return -EOPNOTSUPP;
938-
}
939933

940934
return 0;
941935
}
@@ -2634,7 +2628,7 @@ static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
26342628
[CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
26352629
.len = NF_CT_HELPER_NAME_LEN - 1 },
26362630
[CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2637-
[CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2631+
[CTA_EXPECT_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NF_CT_EXPECT_MASK),
26382632
[CTA_EXPECT_CLASS] = { .type = NLA_U32 },
26392633
[CTA_EXPECT_NAT] = { .type = NLA_NESTED },
26402634
[CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },

net/netfilter/nf_conntrack_proto_tcp.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,9 +1385,9 @@ static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
13851385
}
13861386

13871387
static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1388-
[CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1389-
[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1390-
[CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1388+
[CTA_PROTOINFO_TCP_STATE] = NLA_POLICY_MAX(NLA_U8, TCP_CONNTRACK_SYN_SENT2),
1389+
[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = NLA_POLICY_MAX(NLA_U8, TCP_MAX_WSCALE),
1390+
[CTA_PROTOINFO_TCP_WSCALE_REPLY] = NLA_POLICY_MAX(NLA_U8, TCP_MAX_WSCALE),
13911391
[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
13921392
[CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
13931393
};
@@ -1414,10 +1414,6 @@ static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
14141414
if (err < 0)
14151415
return err;
14161416

1417-
if (tb[CTA_PROTOINFO_TCP_STATE] &&
1418-
nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1419-
return -EINVAL;
1420-
14211417
spin_lock_bh(&ct->lock);
14221418
if (tb[CTA_PROTOINFO_TCP_STATE])
14231419
ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);

0 commit comments

Comments
 (0)