Skip to content

Commit f5ca450

Browse files
ffmanceraummakynes
authored andcommitted
netfilter: nfnetlink_osf: fix out-of-bounds read on option matching
In nf_osf_match(), the nf_osf_hdr_ctx structure is initialized once and passed by reference to nf_osf_match_one() for each fingerprint checked. During TCP option parsing, nf_osf_match_one() advances the shared ctx->optp pointer. If a fingerprint perfectly matches, the function returns early without restoring ctx->optp to its initial state. If the user has configured NF_OSF_LOGLEVEL_ALL, the loop continues to the next fingerprint. However, because ctx->optp was not restored, the next call to nf_osf_match_one() starts parsing from the end of the options buffer. This causes subsequent matches to read garbage data and fail immediately, making it impossible to log more than one match or logging incorrect matches. Instead of using a shared ctx->optp pointer, pass the context as a constant pointer and use a local pointer (optp) for TCP option traversal. This makes nf_osf_match_one() strictly stateless from the caller's perspective, ensuring every fingerprint check starts at the correct option offset. Fixes: 1a6a095 ("netfilter: nfnetlink_osf: add missing fmatch check") Suggested-by: Florian Westphal <fw@strlen.de> Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de> Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 67bf42c commit f5ca450

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

net/netfilter/nfnetlink_osf.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ struct nf_osf_hdr_ctx {
6464
static bool nf_osf_match_one(const struct sk_buff *skb,
6565
const struct nf_osf_user_finger *f,
6666
int ttl_check,
67-
struct nf_osf_hdr_ctx *ctx)
67+
const struct nf_osf_hdr_ctx *ctx)
6868
{
69-
const __u8 *optpinit = ctx->optp;
69+
const __u8 *optp = ctx->optp;
7070
unsigned int check_WSS = 0;
7171
int fmatch = FMATCH_WRONG;
7272
int foptsize, optnum;
@@ -95,25 +95,25 @@ static bool nf_osf_match_one(const struct sk_buff *skb,
9595
check_WSS = f->wss.wc;
9696

9797
for (optnum = 0; optnum < f->opt_num; ++optnum) {
98-
if (f->opt[optnum].kind == *ctx->optp) {
98+
if (f->opt[optnum].kind == *optp) {
9999
__u32 len = f->opt[optnum].length;
100-
const __u8 *optend = ctx->optp + len;
100+
const __u8 *optend = optp + len;
101101

102102
fmatch = FMATCH_OK;
103103

104-
switch (*ctx->optp) {
104+
switch (*optp) {
105105
case OSFOPT_MSS:
106-
mss = ctx->optp[3];
106+
mss = optp[3];
107107
mss <<= 8;
108-
mss |= ctx->optp[2];
108+
mss |= optp[2];
109109

110110
mss = ntohs((__force __be16)mss);
111111
break;
112112
case OSFOPT_TS:
113113
break;
114114
}
115115

116-
ctx->optp = optend;
116+
optp = optend;
117117
} else
118118
fmatch = FMATCH_OPT_WRONG;
119119

@@ -156,9 +156,6 @@ static bool nf_osf_match_one(const struct sk_buff *skb,
156156
}
157157
}
158158

159-
if (fmatch != FMATCH_OK)
160-
ctx->optp = optpinit;
161-
162159
return fmatch == FMATCH_OK;
163160
}
164161

0 commit comments

Comments
 (0)