xref: /third_party/toybox/toys/other/chroot.c (revision 0f66f451)
1/* chroot.c - Run command in new root directory.
2 *
3 * Copyright 2007 Rob Landley <rob@landley.net>
4 *
5 * TODO: The test for root is "==" so root can trivially escape a chroot by
6 * moving it below cwd, ala mkdir("sub"); chroot("sub"); chdir("../../../..")
7 * The container guys use pivot_root() to deal with this, which does actually
8 * edit mount tree. (New option? Kernel patch?)
9
10USE_CHROOT(NEWTOY(chroot, "^<1", TOYFLAG_USR|TOYFLAG_SBIN|TOYFLAG_ARGFAIL(125)))
11
12config CHROOT
13  bool "chroot"
14  default y
15  help
16    usage: chroot NEWROOT [COMMAND [ARG...]]
17
18    Run command within a new root directory. If no command, run /bin/sh.
19*/
20
21#include "toys.h"
22
23void chroot_main(void)
24{
25  char *binsh[] = {"/bin/sh", "-i", 0};
26
27  if (chdir(*toys.optargs) || chroot(".")) {
28    toys.exitval = 125;
29    perror_exit_raw(*toys.optargs);
30  }
31  if (toys.optargs[1]) xexec(toys.optargs+1);
32  else xexec(binsh);
33}
34