Skip to content

Commit c4c07b4

Browse files
thejhummakynes
authored andcommitted
netfilter: nf_nat_snmp_basic: add missing length checks in ASN.1 cbs
The generic ASN.1 decoder infrastructure doesn't guarantee that callbacks will get as much data as they expect; callbacks have to check the `datalen` parameter before looking at `data`. Make sure that snmp_version() and snmp_helper() don't read/write beyond the end of the packet data. (Also move the assignment to `pdata` down below the check to make it clear that it isn't necessarily a pointer we can use before the `datalen` check.) Fixes: cc2d586 ("netfilter: nf_nat_snmp_basic: use asn1 decoder library") Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 31b58ad commit c4c07b4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Diff for: net/ipv4/netfilter/nf_nat_snmp_basic_main.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static void fast_csum(struct snmp_ctx *ctx, unsigned char offset)
105105
int snmp_version(void *context, size_t hdrlen, unsigned char tag,
106106
const void *data, size_t datalen)
107107
{
108+
if (datalen != 1)
109+
return -EINVAL;
108110
if (*(unsigned char *)data > 1)
109111
return -ENOTSUPP;
110112
return 1;
@@ -114,8 +116,11 @@ int snmp_helper(void *context, size_t hdrlen, unsigned char tag,
114116
const void *data, size_t datalen)
115117
{
116118
struct snmp_ctx *ctx = (struct snmp_ctx *)context;
117-
__be32 *pdata = (__be32 *)data;
119+
__be32 *pdata;
118120

121+
if (datalen != 4)
122+
return -EINVAL;
123+
pdata = (__be32 *)data;
119124
if (*pdata == ctx->from) {
120125
pr_debug("%s: %pI4 to %pI4\n", __func__,
121126
(void *)&ctx->from, (void *)&ctx->to);

0 commit comments

Comments
 (0)