linux-user-chroot.c 14.2 KB
Newer Older
1 2
/* -*- mode: c; tab-width: 2; indent-tabs-mode: nil -*-
 *
3
 * linux-user-chroot: A setuid program for non-root users to safely create containers
4
 *
5 6 7 8 9 10 11
 * This program is primarily intended for use by build systems.
 *
 * Let me elaborate on "safely": I believe that this program, when
 * deployed as setuid on a typical "distribution" such as RHEL or
 * Debian, does not, even when used in combination with typical
 * software installed on that distribution, allow privilege
 * escalation.  See the README for more details.
12
 *
13
 * Copyright 2011,2012,2015 Colin Walters <walters@verbum.org>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

30 31
#include "config.h"

32
/* Core libc/linux-headers stuff */
33 34 35 36
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
37
#include <errno.h>
38 39 40 41 42 43
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/prctl.h>
44
#include <sys/fsuid.h>
45 46 47 48 49
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sched.h>

50
#include "setup-seccomp.h"
Colin Walters's avatar
Colin Walters committed
51
#include "setup-dev.h"
52

53 54 55 56
#ifndef PR_SET_NO_NEW_PRIVS
#define PR_SET_NO_NEW_PRIVS	38
#endif

57 58 59 60 61 62 63 64 65 66
/* Totally arbitrary; we're just trying to mitigate somewhat against
 * DoS attacks.  In practice uids can typically spawn multiple
 * processes, so this isn't effective.  What is needed is for the
 * kernel to understand we're creating bind mounts on behalf of a
 * given uid.  Most likely this will happen if the kernel obsoletes
 * this tool by allowing processes with PR_SET_NO_NEW_PRIVS to create
 * private mounts or chroot.
 */
#define MAX_BIND_MOUNTS 1024

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static void fatal (const char *message, ...) __attribute__ ((noreturn)) __attribute__ ((format (printf, 1, 2)));
static void fatal_errno (const char *message) __attribute__ ((noreturn));

static void
fatal (const char *fmt,
       ...)
{
  va_list args;
  
  va_start (args, fmt);

  vfprintf (stderr, fmt, args);
  putc ('\n', stderr);
  
  va_end (args);
  exit (1);
}

static void
fatal_errno (const char *message)
{
  perror (message);
  exit (1);
}

typedef enum {
  MOUNT_SPEC_BIND,
  MOUNT_SPEC_READONLY,
Colin Walters's avatar
Colin Walters committed
95 96
  MOUNT_SPEC_PROCFS,
  MOUNT_SPEC_DEVAPI
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
} MountSpecType;

typedef struct _MountSpec MountSpec;
struct _MountSpec {
  MountSpecType type;

  const char *source;
  const char *dest;
  
  MountSpec *next;
};

static MountSpec *
reverse_mount_list (MountSpec *mount)
{
  MountSpec *prev = NULL;

  while (mount)
    {
      MountSpec *next = mount->next;
      mount->next = prev;
      prev = mount;
      mount = next;
    }

  return prev;
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/**
 * fsuid_chdir:
 * @uid: User id we should use
 * @path: Path string
 *
 * Like chdir() except we use the filesystem privileges of @uid.
 */
static int
fsuid_chdir (uid_t       uid,
             const char *path)
{
  int errsv;
  int ret;
  /* Note we don't check errors here because we can't, basically */
  (void) setfsuid (uid);
  ret = chdir (path);
  errsv = errno;
  (void) setfsuid (0);
  errno = errsv;
  return ret;
}

147 148 149 150 151 152 153 154 155 156 157 158
static inline int
raw_clone (unsigned long flags, void *child_stack)
{
#if defined(__s390__) || defined(__CRIS__)
  /* On s390 and cris the order of the first and second arguments
   * of the raw clone() system call is reversed. */
  return (int) syscall(__NR_clone, child_stack, flags);
#else
  return (int) syscall(__NR_clone, flags, child_stack);
#endif
}

159 160 161 162 163 164
int
main (int      argc,
      char   **argv)
{
  const char *argv0;
  const char *chroot_dir;
Colin Walters's avatar
Colin Walters committed
165
  const char *chdir_target = "/";
166 167 168 169 170
  const char *program;
  uid_t ruid, euid, suid;
  gid_t rgid, egid, sgid;
  int after_mount_arg_index;
  unsigned int n_mounts = 0;
171
  const unsigned int max_mounts = MAX_BIND_MOUNTS;
172 173 174 175 176 177
  char **program_argv;
  MountSpec *bind_mounts = NULL;
  MountSpec *bind_mount_iter;
  int unshare_ipc = 0;
  int unshare_net = 0;
  int unshare_pid = 0;
178
  int seccomp_profile_version = -1;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  int clone_flags = 0;
  int child_status = 0;
  pid_t child;

  if (argc <= 0)
    return 1;

  argv0 = argv[0];
  argc--;
  argv++;

  if (argc < 1)
    fatal ("ROOTDIR argument must be specified");

  after_mount_arg_index = 0;
  while (after_mount_arg_index < argc)
    {
      const char *arg = argv[after_mount_arg_index];
      MountSpec *mount = NULL;

      if (n_mounts >= max_mounts)
        fatal ("Too many mounts (maximum of %u)", n_mounts);
      n_mounts++;

203 204 205 206 207 208 209 210 211 212 213
      if (strcmp (arg, "--help") == 0)
        {
          printf ("%s\n", "See \"man linux-user-chroot\"");
          exit (0);
        }
      else if (strcmp (arg, "--version") == 0)
        {
          printf ("%s\n", PACKAGE_STRING);
          exit (0);
        }
      else if (strcmp (arg, "--mount-bind") == 0)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        {
          if ((argc - after_mount_arg_index) < 3)
            fatal ("--mount-bind takes two arguments");

          mount = malloc (sizeof (MountSpec));
          mount->type = MOUNT_SPEC_BIND;
          mount->source = argv[after_mount_arg_index+1];
          mount->dest = argv[after_mount_arg_index+2];
          mount->next = bind_mounts;
          
          bind_mounts = mount;
          after_mount_arg_index += 3;
        }
      else if (strcmp (arg, "--mount-readonly") == 0)
        {
          MountSpec *mount;

          if ((argc - after_mount_arg_index) < 2)
            fatal ("--mount-readonly takes one argument");

          mount = malloc (sizeof (MountSpec));
          mount->type = MOUNT_SPEC_READONLY;
          mount->source = NULL;
          mount->dest = argv[after_mount_arg_index+1];
          mount->next = bind_mounts;
          
          bind_mounts = mount;
          after_mount_arg_index += 2;
        }
      else if (strcmp (arg, "--mount-proc") == 0)
        {
          MountSpec *mount;

          if ((argc - after_mount_arg_index) < 2)
            fatal ("--mount-proc takes one argument");

          mount = malloc (sizeof (MountSpec));
          mount->type = MOUNT_SPEC_PROCFS;
          mount->source = NULL;
          mount->dest = argv[after_mount_arg_index+1];
          mount->next = bind_mounts;
          
Colin Walters's avatar
Colin Walters committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
          bind_mounts = mount;
          after_mount_arg_index += 2;
        }
      else if (strcmp (arg, "--mount-devapi") == 0)
        {
          MountSpec *mount;

          if ((argc - after_mount_arg_index) < 2)
            fatal ("--mount-devapi takes one argument");

          mount = malloc (sizeof (MountSpec));
          mount->type = MOUNT_SPEC_DEVAPI;
          mount->source = NULL;
          mount->dest = argv[after_mount_arg_index+1];
          mount->next = bind_mounts;
          
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
          bind_mounts = mount;
          after_mount_arg_index += 2;
        }
      else if (strcmp (arg, "--unshare-ipc") == 0)
        {
          unshare_ipc = 1;
          after_mount_arg_index += 1;
        }
      else if (strcmp (arg, "--unshare-pid") == 0)
        {
          unshare_pid = 1;
          after_mount_arg_index += 1;
        }
      else if (strcmp (arg, "--unshare-net") == 0)
        {
          unshare_net = 1;
          after_mount_arg_index += 1;
        }
Colin Walters's avatar
Colin Walters committed
290 291 292 293 294 295 296 297
      else if (strcmp (arg, "--chdir") == 0)
        {
          if ((argc - after_mount_arg_index) < 2)
            fatal ("--chdir takes one argument");

          chdir_target = argv[after_mount_arg_index+1];
          after_mount_arg_index += 2;
        }
298 299 300 301 302 303 304 305
      else if (strcmp (arg, "--seccomp-profile-version") == 0)
        {
          if ((argc - after_mount_arg_index) < 2)
            fatal ("--seccomp-profile-version takes one argument");

          seccomp_profile_version = atoi(argv[after_mount_arg_index+1]);
          after_mount_arg_index += 2;
        }
306 307 308 309 310 311 312
      else
        break;
    }
        
  bind_mounts = reverse_mount_list (bind_mounts);

  if ((argc - after_mount_arg_index) < 2)
Colin Walters's avatar
Colin Walters committed
313
    fatal ("usage: %s [--unshare-ipc] [--unshare-pid] [--unshare-net] [--mount-proc DIR] [--mount-readonly DIR] [--mount-bind SOURCE DEST] [--chdir DIR] ROOTDIR PROGRAM ARGS...", argv0);
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  chroot_dir = argv[after_mount_arg_index];
  program = argv[after_mount_arg_index+1];
  program_argv = argv + after_mount_arg_index + 1;

  if (getresgid (&rgid, &egid, &sgid) < 0)
    fatal_errno ("getresgid");
  if (getresuid (&ruid, &euid, &suid) < 0)
    fatal_errno ("getresuid");

  if (rgid == 0)
    rgid = ruid;

  /* CLONE_NEWNS makes it so that when we create bind mounts below,
   * we're only affecting our children, not the entire system.  This
   * way it's harmless to bind mount e.g. /proc over an arbitrary
   * directory.
   */
  clone_flags = SIGCHLD | CLONE_NEWNS;
  /* CLONE_NEWIPC and CLONE_NEWUTS are avenues of communication that
   * might leak outside the container; any IPC can be done by setting
   * up a bind mount and using files or sockets there, if desired.
   */
  if (unshare_ipc)
    clone_flags |= (CLONE_NEWIPC | CLONE_NEWUTS);
  /* CLONE_NEWPID helps ensure random build or test scripts don't kill
   * processes outside of the container.
   */
  if (unshare_pid)
    clone_flags |= CLONE_NEWPID;

  /* Isolated networking */
  if (unshare_net)
    clone_flags |= CLONE_NEWNET;

348
  if ((child = raw_clone (clone_flags, NULL)) < 0)
349
    fatal_errno ("clone");
350 351 352 353

  if (child == 0)
    {
      /*
354 355 356 357 358 359 360
       * First, we attempt to use PR_SET_NO_NEW_PRIVS, since it does
       * exactly what we want - ensures the child can not gain any
       * privileges, even attempting to execute setuid binaries.
       *
       * http://lwn.net/Articles/504879/
       *
       * Following the belt-and-suspenders model, we also make a
361 362 363 364 365
       * MS_NOSUID bind mount below.  I don't think this is strictly
       * necessary, but at least we doubly ensure we're not going to
       * be executing any setuid binaries from the host's /.  It
       * doesn't help if there are any other mount points with setuid
       * binaries, but `PR_SET_NO_NEW_PRIVS` fixes that.
366
       */
367
      if (prctl (PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
368
        fatal_errno ("prctl (PR_SET_NO_NEW_PRIVS)");
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

      /* This is necessary to undo the damage "sandbox" creates on Fedora
       * by making / a shared mount instead of private.  This isn't
       * totally correct because the targets for our bind mounts may still
       * be shared, but really, Fedora's sandbox is broken.
       */
      if (mount (NULL, "/", "none", MS_PRIVATE | MS_REC, NULL) < 0)
        fatal_errno ("mount(/, MS_PRIVATE | MS_REC)");

      /* I had thought that SECBIT_NOROOT was enough to be safe, but Serge E. Hallyn
       * pointed out that setuid binaries still change uid to 0.  So let's just
       * disallow them at the rootfs level.
       */
      if (mount (NULL, "/", "none", MS_PRIVATE | MS_REMOUNT | MS_NOSUID, NULL) < 0)
        fatal_errno ("mount(/, MS_PRIVATE | MS_REC | MS_NOSUID)");

      /* Now let's set up our bind mounts */
      for (bind_mount_iter = bind_mounts; bind_mount_iter; bind_mount_iter = bind_mount_iter->next)
        {
          char *dest;
          
          asprintf (&dest, "%s%s", chroot_dir, bind_mount_iter->dest);
          
          if (bind_mount_iter->type == MOUNT_SPEC_READONLY)
            {
              if (mount (dest, dest,
                         NULL, MS_BIND | MS_PRIVATE, NULL) < 0)
                fatal_errno ("mount (MS_BIND)");
              if (mount (dest, dest,
                         NULL, MS_BIND | MS_PRIVATE | MS_REMOUNT | MS_RDONLY, NULL) < 0)
                fatal_errno ("mount (MS_BIND | MS_RDONLY)");
            }
          else if (bind_mount_iter->type == MOUNT_SPEC_BIND)
            {
403 404 405
              if (fsuid_chdir (ruid, bind_mount_iter->source) < 0)
                fatal ("Couldn't chdir to bind mount source");
              if (mount (".", dest,
406 407 408 409 410 411 412 413 414
                         NULL, MS_BIND | MS_PRIVATE, NULL) < 0)
                fatal_errno ("mount (MS_BIND)");
            }
          else if (bind_mount_iter->type == MOUNT_SPEC_PROCFS)
            {
              if (mount ("proc", dest,
                         "proc", MS_MGC_VAL | MS_PRIVATE, NULL) < 0)
                fatal_errno ("mount (\"proc\")");
            }
Colin Walters's avatar
Colin Walters committed
415 416 417 418 419
          else if (bind_mount_iter->type == MOUNT_SPEC_DEVAPI)
            {
              if (setup_dev (dest) < 0)
                fatal_errno ("setting up devapi");
            }
420 421 422 423
          else
            assert (0);
          free (dest);
        }
424

425
      if (fsuid_chdir (ruid, chroot_dir) < 0)
426 427
        fatal_errno ("chdir");

428
      if (mount (".", ".", NULL, MS_BIND | MS_PRIVATE, NULL) < 0)
429 430
        fatal_errno ("mount (MS_BIND)");

431 432 433 434 435
      /* Only move if we're not actually just using / */
      if (strcmp (chroot_dir, "/") != 0)
        {
          if (mount (chroot_dir, "/", NULL, MS_MOVE, NULL) < 0)
            fatal_errno ("mount (MS_MOVE)");
436

437 438 439
          if (chroot (".") < 0)
            fatal_errno ("chroot");
        }
440
      
441 442 443 444 445 446 447
      /* Switch back to the uid of our invoking process.  These calls are
       * irrevocable - see setuid(2) */
      if (setgid (rgid) < 0)
        fatal_errno ("setgid");
      if (setuid (ruid) < 0)
        fatal_errno ("setuid");

448 449 450
      if (chdir (chdir_target) < 0)
        fatal_errno ("chdir");

451 452 453 454 455 456 457 458
      /* Add the seccomp filters just before we exec */
      if (seccomp_profile_version == 0)
        setup_seccomp_v0 ();
      else if (seccomp_profile_version == -1)
        ;
      else
        fatal ("Unknown --seccomp-profile-version");

Colin Walters's avatar
Colin Walters committed
459
      if (execvp (program, program_argv) < 0)
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        fatal_errno ("execv");
    }

  /* Let's also setuid back in the parent - there's no reason to stay uid 0, and
   * it's just better to drop privileges. */
  if (setgid (rgid) < 0)
    fatal_errno ("setgid");
  if (setuid (ruid) < 0)
    fatal_errno ("setuid");

  /* Kind of lame to sit around blocked in waitpid, but oh well. */
  if (waitpid (child, &child_status, 0) < 0)
    fatal_errno ("waitpid");
  
  if (WIFEXITED (child_status))
    return WEXITSTATUS (child_status);
  else
    return 1;
}