Skip to content

ASan stack-buffer-overflow in SLIRP networking due to struct mismatches with LKL #33

@ChuWeiChang

Description

@ChuWeiChang

Problem

Running kbox with SLIRP networking enabled (./kbox image -S alpine.ext4 --net) triggers a stack-buffer-overflow detected by AddressSanitizer (ASan).

The root cause stems from mismatches between the custom-defined network structures (ifr and rt) in net-slirp.c and the actual structures expected by the LKL interface. This manifests in two distinct ways:

  1. ifr struct (Architecture Mismatch): The current custom struct hardcodes a size of 32 bytes (16 for name + 16 for the union/address). While this matches the 32-bit Linux struct ifreq(https://man7.org/linux/man-pages/man7/netdevice.7.html), the standard size on 64-bit architectures is 40 bytes. Running this on a 64-bit host results in an 8-byte discrepancy, causing the overflow.
  2. rt struct (Incomplete Definition): The custom rt struct, corresponding to Linux struct rtentry , is structurally incomplete. It is missing two standard fields: unsigned long rt_window and unsigned short rt_irtt, causing the overflow.

Proposed Changes

Modify net-slirp.c to improve memory safety and compatibility:
That is, replacing the custom, hardcoded ifr and rt struct definitions with the standard Linux structs (struct ifreq and struct rtentry) .

Considerations

  • Architecture Portability: Using the standard headers natively resolves the 64-bit pointer and padding differences for ifreq across different host architectures (x86_64, aarch64).
  • Current Workarounds: For debugging purposes, the ASan checks can currently be bypassed by manually padding the ifr struct with 8 bytes and the rt struct with 16 bytes on 64-bit system. However, this is brittle and should be discarded in favor of standard headers.
  • Scope of Impact: The necessary modifications are strictly localized to net-slirp.c.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions