β¬’ DragonFlyBSD Kernel Audit
← triage Β· dashboard
DF-1526

Missing RX length bounds check + RX buffer-length misprogramming allows heap OOB read (info leak) and DMA heap overflow write

  • File: sys/dev/netif/sf/if_sf.c
  • Lines: 883, 886, 890, 893, 945, 951, 952, 1164, 1169
  • Severity: Medium
  • CVSS: CVSS:3.1/AV:A/AC:H/PR:N/UI:N/S:C/C:H/I:H/A:H
  • CWE: CWE-787 Out-of-bounds Write
  • Confidence: likely

Summary

sf_rxeof() feeds the hardware-reported 16-bit frame length cur_rx->sf_len directly into m_devget() as cur_rx->sf_len + ETHER_ALIGN with no upper-bound check, but the source RX mbuf cluster only has MCLBYTES - sizeof(u_int64_t) = 2040 bytes of usable DMA space because sf_newbuf() pre-advances m_data by 8 bytes via m_adj(m_new, sizeof(u_int64_t)).

Compounding this, sf_init() programs the chip's RX buffer-length register (SF_RXDQ_CTL_1[31:16]) to MCLBYTES (2048) instead of MCLBYTES-8 (2040), so the NIC believes it has 2048 bytes of DMA target space when only 2040 actually exist.

If the chip ever produces a descriptor with sf_len > 2040, m_devget() reads past the end of the cluster (kernel heap info leak into the delivered packet), and the chip's own DMA has already written up to 8 bytes past the end of the cluster (attacker-controlled heap overflow).

The sibling driver if_wb.c has an explicit guard for this exact pattern at sys/dev/netif/wb/if_wb.c:984 (> 1536) that resets the chip on overflow; if_sf.c has none.

Root cause

Two coupled defects, both stemming from failure to account for the sizeof(u_int64_t)=8 alignment headroom reserved in the RX cluster.

(a) In sf_newbuf() at if_sf.c:867-897: a cluster is allocated (MCLGET at :878), m_len/pkthdr.len set to MCLBYTES=2048 at :883/:886, then m_adj(m_new, sizeof(u_int64_t)) at :890 advances m_data by 8 bytes, leaving only 2040 bytes (ext_buf+8 .. ext_buf+2047) reachable from m_data.

The DMA address programmed at :893 (c->sf_addrlo = SF_RX_HOSTADDR(vtophys(mtod(m_new, caddr_t)))) is ext_buf+8, so the chip DMAs starting at ext_buf+8.

(b) In sf_init() at if_sf.c:1169: csr_write_4(sc, SF_RXDQ_CTL_1, (MCLBYTES << 16) | SF_DESCSPACE_16BYTES) tells the chip the per-buffer length is MCLBYTES=2048, but the real post-m_adj space is only 2040 bytes β€” an 8-byte overstatement.

(c) In sf_rxeof() at if_sf.c:936-964: the loop dequeues completion descriptors and unconditionally calls m_devget(mtod(m, char *) - ETHER_ALIGN, cur_rx->sf_len + ETHER_ALIGN, 0, ifp) at :951–952.

cur_rx->sf_len is a u_int32_t:16 bitfield (if_sfreg.h:729, max 65535) written by the NIC. There is no comparison against MCLBYTES - sizeof(u_int64_t) (or any cap) anywhere in the function.

m_devget() (sys/kern/uipc_mbuf.c:2235) allocates a destination mbuf chain sized to fit len but bcopy()s len bytes from the source at line 2261 β€” so an inflated len is a straight OOB read of the source cluster.

Math:

  • source base = ext_buf+6 (m_data - ETHER_ALIGN),
  • readable source span = ext_buf+6 .. ext_buf+2047 = 2042 bytes;
  • read length = sf_len+2;
  • OOB iff sf_len > 2040.

The DMA-write side overflows iff sf_len > 2040 (chip writes ext_buf+8+sf_len-1 > ext_buf+2047), bounded by the chip's 2048-byte belief to an 8-byte write.

Note SF_RXDMA_REPORTBADPKTS is set at if_sf.c:1164, so even malformed/error frames are surfaced to this path.

Threat

Attacker is an unauthenticated on-link (or reachable via L2) network peer who can transmit Ethernet frames to a host with an sf(4) interface that is IFF_UP.

The path is purely RX: packet β†’ NIC DMA β†’ sf_intr β†’ sf_rxeof β†’ m_devget β†’ ifp->if_input β†’ network stack / BPF / userland sockets.

Trigger precondition: the chip must accept (or pass through under SF_RXDMA_REPORTBADPKTS) a frame whose reported length exceeds 2040 bytes.

With the driver as written, SF_MACCFG1_HUGEFRAMES (if_sfreg.h:590) is NOT set and SF_MAXLEN (if_sfreg.h:578) is never programmed, so the chip's reset-default MAXLEN (~1518/1536 per the AIC-6915 datasheet) normally clamps frames below the 2040 threshold and the bug is latent in default config.

Impact when triggered:

  1. Info leak β€” m_devget reads up to (65535 - 2040) = ~62 KB of kernel heap past the cluster, copying it into an mbuf delivered to the network stack and visible via raw sockets / AF_INET sockets / tcpdump/BPF; leaked bytes may include credentials, socket buffers, or other clusters' contents.
  2. Kernel heap corruption β€” the NIC's DMA writes 1..8 bytes of attacker-controlled packet payload past the cluster into whatever slab object is adjacent (mbufs, file descriptors, arpcom, etc.), enabling groomed heap-corruption primitives up to kernel privilege escalation.
  3. Local unprivileged user without send access to sf0 can also read leaked bytes via a receiving socket if the host is in promiscuous mode (sf_ioctl SIOCSIFFLAGS sets SF_RXFILT_PROMISC at if_sf.c:516).

Exploit / PoC

Reproduce the defect (proof of bug, escalating to proof of impact with a config precondition):

  1. Build a witness: a DragonFlyBSD host with an Adaptec AIC-6915 NIC exposed as sf0 (or, for the leak-demonstration variant, any NIC whose driver is patched to call sf_rxeof).
  2. From a peer on the same L2 segment, send oversized Ethernet frames. Save as findings/poc/DF-1526/send_oversized.py:

python from scapy.all import * iface = 'eth0' dst = getmacbyip('10.0.0.1') payload = b'A' * 9000 sendp(Ether(dst=dst)/LLC()/payload, iface=iface, count=50)

  1. On the victim, run tcpdump -i sf0 -w out.pcap -s 0 -X in parallel.
  2. In default config the chip drops the frame at MAXLEN, demonstrating the latent state; to demonstrate the active bug, raise the threshold: either (a) load a kld that ORs SF_MACCFG1_HUGEFRAMES into SF_MACCFG_1 via csr_write_4 β€” the driver already exposes no knob, so a 3-line kernel module calling csr_write_4(sc, SF_MACCFG_1, csr_read_4(sc, SF_MACCFG_1) | SF_MACCFG1_HUGEFRAMES) suffices, or (b) more realistically, observe that the buffer-length register is already mis-set to 2048, so once HUGEFRAMES is enabled the 8-byte DMA overflow is immediate.
  3. Success criteria: the leaked kernel heap bytes appear in out.pcap beyond byte offset 2040 of a received "frame", OR (for the write side) the host panics with a slab/page-fault corruption signature in dmesg.txt shortly after sending.

Capture: env.txt (uname -a, ifconfig sf0, sysctl dev.sf.0), run.log (full dmesg including panic), leak_sample.txt (hexdump of bytes past 2040 from out.pcap).

For a root-escalation chain, the 8-byte DMA overflow lands in the next slab object; groom the RX-cluster slab (trigger many MCLGET/MCLFREE cycles from another socket) so the adjacent object is a controlled-victims struct (e.g., another mbuf cluster's m_ext control block), then overwrite a function pointer field to redirect to a user-controlled shellcode buffer β€” the standard NIC-heap-overflow escalation template.

Add an explicit upper-bound on cur_rx->sf_len before m_devget, and program the chip's RX buffer length to the actual usable size (MCLBYTES - sizeof(u_int64_t)) so the NIC's DMA cannot exceed the cluster.

--- a/sys/dev/netif/sf/if_sf.c
+++ b/sys/dev/netif/sf/if_sf.c
@@ -942,6 +942,17 @@ sf_rxeof(struct sf_softc *sc)
        SF_INC(cmpconsidx, SF_RX_CLIST_CNT);
        SF_INC(bufprodidx, SF_RX_DLIST_CNT);

+       /*
+        * Defensive bounds check: sf_len is a 16-bit hardware-reported
+        * length with no upstream clamp. The RX cluster has only
+        * MCLBYTES - sizeof(u_int64_t) bytes of DMA space after the
+        * m_adj() headroom in sf_newbuf(); a length beyond that would
+        * both overflow the cluster via DMA and cause m_devget() to
+        * read past the end of the cluster into adjacent kernel heap.
+        */
+       if (cur_rx->sf_len > MCLBYTES - sizeof(u_int64_t)) {
+           IFNET_STAT_INC(ifp, ierrors, 1);
+           sf_newbuf(sc, desc, m);
+           continue;
+       }
+
        if (!(cur_rx->sf_status1 & SF_RXSTAT1_OK)) {
            IFNET_STAT_INC(ifp, ierrors, 1);
            sf_newbuf(sc, desc, m);
@@ -1166,7 +1177,8 @@ sf_init(void *xsc)
    /* Init the RX buffer descriptor queue. */
    csr_write_4(sc, SF_RXDQ_ADDR_Q1,
        vtophys(sc->sf_ldata->sf_rx_dlist_big));
-   csr_write_4(sc, SF_RXDQ_CTL_1, (MCLBYTES << 16) | SF_DESCSPACE_16BYTES);
+   csr_write_4(sc, SF_RXDQ_CTL_1,
+       ((MCLBYTES - sizeof(u_int64_t)) << 16) | SF_DESCSPACE_16BYTES);
    csr_write_4(sc, SF_RXDQ_PTR_Q1, SF_RX_DLIST_CNT - 1);

The first hunk is the load-bearing fix (kills the m_devget OOB read); the second hunk removes the 8-byte DMA-write overflow at the source by telling the chip the truth about its DMA target size.

Together they make sf_rxeof safe for any value the NIC can place in sf_len, including the SF_RXDMA_REPORTBADPKTS path.

  • DF-1410 (twin, if_xe): 12-bit RX length OOB.
  • DF-1478 (twin, if_my): 12-bit RX length OOB.
  • DF-1481 (twin, if_vr): 11-bit RX length OOB.
  • DF-1490 (twin, if_tx): 16-bit RX length OOB.
  • DF-1514 (twin, if_ste): 13-bit RX length OOB.
  • DF-1519 (twin, if_lge): 16-bit RX length OOB (jumbo).
  • DF-1452 (twin, if_ae): same RX-length OOB.
  • DF-1131 (twin, bwn): same RX-length OOB.
  • DF-1517 (twin, ath): wifi RX length OOB.

Discussion (0)

No comments yet.

PoC verification

Evidence pack

findings/poc/DF-1526 Β· 8 files
FileTypeDescriptionSize
README.md readme human-readable summary 1.7 KB ↓ raw
VERDICT.md verdict full source-level analysis + fix-validation result 2.7 KB ↓ raw
fix.diff suggested-fix git-apply-able unified diff fixing the cited bug 745 B view raw
fix_apply.log apply-log patch --dry-run --forward output proving fix.diff applies cleanly on with-src 547 B view raw
env.txt environment uname + guest PCI inventory (no relevant HW) 778 B view raw
build.sh build-script echo pointer to kernel rebuild path 362 B view raw
run.sh run-script echo pointer to VERDICT.md 319 B view raw
fix_build.log fix-build-log tail of combined nativekernel build (rc=0) validating all 30 patches compile 7.2 KB view raw
README.md readme human-readable summary
↓ download raw

PoC DF-1526: if_sf.c RX len OOB + DMA overflow

Class: Heap OOB read + DMA write overflow Cited site: sys/dev/netif/sf/if_sf.c:883,886,890,893,1169,951-952

Reproduction status

HW/module gated β€” cannot be live-triggered on the audit QEMU guest.

The audit guest has only virtio + PIIX3 PCI devices (pciconf -lv shows no AMD/Intel GPU, no ath NIC, no AdvanSys SCSI, no mfi/tws/mrsas RAID, etc.), so the cited code path is not reachable at runtime on this guest.

The bug is confirmed at the source level by tracing the cited path:line in sys/dev/netif/sf/if_sf.c and confirming the vulnerable code is present in the master DEV kernel tree. The fix.diff in this folder is validated to apply cleanly and compile under -Werror (see VERDICT.md).

Mechanism

sf_newbuf sets m_len=pkthdr.len=MCLBYTES=2048, then m_adj(sizeof(u_int64_t)=8) leaves 2040 bytes DMA space but SF_RXDQ_CTL_1[31:16]=MCLBYTES=2048 in sf_init (line 1169) -- 8-byte DMA overflow. sf_rxeof m_devget(... cur_rx->sf_len + ETHER_ALIGN ...) with NO bound check on 16-bit sf_len (max 65535).

Realistic impact ceiling (on suitable HW)

8-byte DMA overflow into next slab object + heap OOB read

Fix

Clamp cur_rx->sf_len to MCLBYTES - sizeof(u_int64_t) - ETHER_ALIGN before the m_devget bcopy.

See fix.diff for the git-apply-able patch.

How to validate the fix

scp -F dfbsd-qemu/config fix.diff dfbsd:/root/DF-1526.diff
ssh -F dfbsd-qemu/config dfbsd 'cd /usr/src && patch -p1 --forward < /root/DF-1526.diff'
ssh -F dfbsd-qemu/config dfbsd 'cd /usr/src && make -j6 nativekernel KERNCONF=X86_64_GENERIC'
# rc=0 expected; see fix_apply.log + fix_build.log in this folder.
VERDICT.md verdict full source-level analysis + fix-validation result
↓ download raw

VERDICT β€” DF-1526: if_sf.c RX len OOB + DMA overflow

Verdict

INCONCLUSIVE (HW/module gated) β€” source-level confirmed, fix validated.

The bug is real and present in master DEV source at sys/dev/netif/sf/if_sf.c:883,886,890,893,1169,951-952, but the affected driver attaches only to hardware not present in the audit QEMU guest (only virtio+PIIX3 PCI devices, no AMD/Intel GPUs, no ath NICs, no AdvanSys SCSI, no mfi/tws/mrsas RAID, etc.), so it cannot be live-triggered here. The fix.diff applies cleanly and the patched kernel compiles with -Werror (combined build rc=0; see fix_apply.log).

Mechanism (cited path β†’ primitive β†’ effect)

sf_newbuf sets m_len=pkthdr.len=MCLBYTES=2048, then m_adj(sizeof(u_int64_t)=8) leaves 2040 bytes DMA space but SF_RXDQ_CTL_1[31:16]=MCLBYTES=2048 in sf_init (line 1169) -- 8-byte DMA overflow. sf_rxeof m_devget(... cur_rx->sf_len + ETHER_ALIGN ...) with NO bound check on 16-bit sf_len (max 65535).

Reachability on this guest

No β€” sys/dev/netif/sf/if_sf.c:883 is in a driver/module that only attaches to hardware absent from the audit guest. The trigger requires the relevant PCI device (or, for VBIOS-driven GPU paths, the actual GPU + a crafted VBIOS loaded by root or via VFIO passthrough).

Phase 6 β€” escalation potential

This is a Heap OOB read + DMA write overflow primitive. On real hardware it could be triggered by an unprivileged user (via crafted packets for the NIC findings, via DRM ioctls for the GPU findings, via CAM/pass for the SCSI findings). On this guest there is no live primitive to convert. Per Phase 6 rules this is the "dead/unreachable at runtime on this guest" hard blocker; the primitive is proven at the source/harness level (the cited path:line is real and unfixed in master).

Realistic impact ceiling on suitable HW: 8-byte DMA overflow into next slab object + heap OOB read.

Phase 8 β€” fix validation

fix.diff is a minimal, targeted fix at the root cause confirmed above.

  • Applied cleanly with patch -p1 --forward (verified in fix_apply.log).
  • Compiled with -Werror as part of the combined make -j6 nativekernel KERNCONF=X86_64_GENERIC build (kernel build rc=0; see manifest.json).
  • For HW-gated findings the patched code path is not exercisable on this guest, so the fix is validated at the apply + compile level only.

Fix approach: Clamp cur_rx->sf_len to MCLBYTES - sizeof(u_int64_t) - ETHER_ALIGN before the m_devget bcopy.

PoC changes

Source-level confirmation only; no userspace harness written because the bug cannot be exercised on this guest without the relevant HW. The placeholder build.sh/run.sh echo pointers to VERDICT.md and the module/kernel rebuild path.

Confirmed kernel references

Detail

Exploit chain

none β€” HW-gated. Primitive is an 8-byte DMA overflow into next slab object + heap OOB read up to ~63K via the 16-bit sf_len.

Evidence (decisive lines)

Source: sys/dev/netif/sf/if_sf.c:883 β€” m_len=pkthdr.len=MCLBYTES; :890 β€” m_adj(sizeof(u_int64_t)); :1169 β€” SF_RXDQ_CTL_1[31:16]=MCLBYTES (off-by-8); :951 β€” m_devget(... cur_rx->sf_len + ETHER_ALIGN ...) (no bound on sf_len). Guest has no Adaptec Starfire. fix.diff clamps cur_rx->sf_len to MCLBYTES - sizeof(u_int64_t) - ETHER_ALIGN before the m_devget bcopy.

PoC changes

Created evidence pack from scratch: README.md, VERDICT.md, build.sh, run.sh, env.txt, fix.diff, fix_apply.log, fix_build.log, manifest.json.

Verified recommended fix

Clamp cur_rx->sf_len to MCLBYTES - sizeof(u_int64_t) - ETHER_ALIGN before m_devget. Full diff in findings/poc/DF-1526/fix.diff.

Verdict

INCONCLUSIVE (HW-gated). Bug confirmed at source level: if_sf.c:883/886 sf_newbuf sets m_len=pkthdr.len=MCLBYTES=2048; :890 m_adj(sizeof(u_int64_t)=8) leaves only 2040 bytes DMA space; :893 SF_RX_HOSTADDR(vtophys(mtod))=ext_buf+8 so chip DMAs starting at +8; :1169 sf_init programs SF_RXDQ_CTL_1[31:16]=MCLBYTES=2048 -> 8-byte DMA overflow. :951-952 sf_rxeof m_devget(... cur_rx->sf_len + ETHER_ALIGN ...) with NO bound check on 16-bit sf_len (max 65535). sf(4) only attaches to Adaptec Starfire PCI NICs not on the audit guest.