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



Sign in to add a comment
iOS/MacOS kernel uaf due to bad locking in unix domain socket file descriptor externalization
Project Member Reported by ianbeer@google.com, Feb 10 2017 Back to list
unp_externalize is responsible for externalizing the file descriptors carried within a unix domain socket message.
That means allocating new fd table entries in the receiver and recreating a file which looks looks (to userspace) like the file
the sender sent.

Here's the relevant code:

  for (i = 0; i < newfds; i++) {     <----------- (a)
#if CONFIG_MACF_SOCKET
    /*
     * If receive access is denied, don't pass along
     * and error message, just discard the descriptor.
     */
    if (mac_file_check_receive(kauth_cred_get(), rp[i])) {
      proc_fdunlock(p);
      unp_discard(rp[i], p);
      fds[i] = 0;
      proc_fdlock(p);
      continue;
    }
#endif
    if (fdalloc(p, 0, &f))
      panic("unp_externalize:fdalloc");
    fp = fileproc_alloc_init(NULL);
    if (fp == NULL)
      panic("unp_externalize: MALLOC_ZONE");
    fp->f_iocount = 0;
    fp->f_fglob = rp[i];
    if (fg_removeuipc_mark(rp[i]))
      fgl[i] = rp[i];
    else
      fgl[i] = NULL;
    procfdtbl_releasefd(p, f, fp);
    fds[i] = f;
  }
  proc_fdunlock(p);                <-------- (b)

  for (i = 0; i < newfds; i++) {
    if (fgl[i] != NULL) {
      VERIFY(fgl[i]->fg_lflags & FG_RMMSGQ);
      fg_removeuipc(fgl[i]);      <--------- (c)
    }
    if (fds[i])
      (void) OSAddAtomic(-1, &unp_rights);
  }


The loop at a gets the fileglobs from the socket message buffer and allocates new fds in the receiver
and sets the fp->f_fglob fileglob pointer to point to the sent fg. After each new fd is allocated and initialized
the fd is released and associated with the new fp.

At (b) the code then drops the process fd lock at which point other threads can access the fd table again.

At (c) the code then removes each of the fileglobs from the list of in-transit fileglobs; however this list *doesn't*
hold an fg reference therefore there's nothing stopping the fg from getting free'd via another thread closing
the fd between (b) and (c).

Use zone poisoning for a reliable crasher.

tested on MacOS 10.12.3 (16D32) on MacbookAir5,2 
 
domain_sock.c
6.6 KB View Download
Project Member Comment 1 by ianbeer@google.com, Feb 10 2017
Labels: Reported-2017-Feb-10 Id-659059133
Project Member Comment 2 by ianbeer@google.com, May 9 2017
Labels: Deadline-Grace
Project Member Comment 3 by ianbeer@google.com, May 17 2017
Labels: CVE-2017-2501 Fixed-2017-May-15
Status: Fixed
Fixed in MacOS 10.12.5: https://support.apple.com/en-us/HT207797
Fixed in iOS 10.3.2: https://support.apple.com/kb/HT207798

Project Member Comment 4 by ianbeer@google.com, May 23 2017
Labels: -Restrict-View-Commit
Sign in to add a comment