New issue
Advanced search Search tips
Starred by 4 users
Status: Fixed
Owner:
Closed: Mar 2017
Cc:



Sign in to add a comment
MacOS/iOS kernel heap overflow in bpf
Project Member Reported by ianbeer@google.com, Feb 13 2017 Back to list
The bpf ioctl BIOCSBLEN allows userspace to set the bpf buffer length:

  case BIOCSBLEN:     /* u_int */
    if (d->bd_bif != 0)
      error = EINVAL;
    else {
      u_int size;

      bcopy(addr, &size, sizeof (size));

      if (size > bpf_maxbufsize)
        size = bpf_maxbufsize;
      else if (size < BPF_MINBUFSIZE)
        size = BPF_MINBUFSIZE;
      bcopy(&size, addr, sizeof (size));
      d->bd_bufsize = size;
    }
    break;


d->bd_bif is set to the currently attached interface, so we can't change the length if we're already
attached to an interface.

There's no ioctl command to detach us from an interface, but we can just destroy the interface
(by for example attaching to a bridge interface.) We can then call BIOCSBLEN again with a larger
length which will set d->bd_bufsize to a new, larger value.

If we then attach to an interface again we hit this code in bpf_setif:

    if (d->bd_sbuf == 0) {
      error = bpf_allocbufs(d);
      if (error != 0)
        return (error);

This means that the buffers actually won't be reallocated since d->bd_sbuf will still point to the
old buffer. This means that d->bd_bufsize is out of sync with the actual allocated buffer size
leading to heap corruption when packets are receive on the target interface.

This PoC sets a small buffer length then creates and attaches to a bridge interface. It then destroys
the bridge interface (which causes bpfdetach to be called on that interface, clearing d->bd_bif for our
bpf device.)

We then set a large buffer size and attach to the loopback interface and sent some large ping packets.

This bug is a root -> kernel priv esc

tested on MacOS 10.12.3 (16D32) on MacbookAir5,2
 
bpf.c
3.0 KB View Download
Project Member Comment 1 by ianbeer@google.com, Feb 13 2017
Labels: Id-659225630 Reported-2017-Feb-13
Project Member Comment 2 by ianbeer@google.com, Mar 31 2017
Labels: CVE-2017-2482 Fixed-2017-Mar-27
Status: Fixed
Fixed in MacOS 10.12.4: https://support.apple.com/en-us/HT207615
Fixed in iOS 10.3: https://support.apple.com/en-us/HT207617
Project Member Comment 3 by ianbeer@google.com, Apr 3 2017
Labels: -Restrict-View-Commit
Sign in to add a comment