LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Bodo Eggert <7eggert@gmx.de>
To: Leibowitz@vger.kernel.org, Michael <michael.leibowitz@intel.com>,
	serge@hallyn.com, linux-kernel@vger.kernel.org
Subject: RE: CLONE_NEWNS and bind mounts to make "chroot" jail
Date: Mon, 03 Mar 2008 16:54:15 +0100	[thread overview]
Message-ID: <E1JWCzY-0003zx-1z@be1.7eggert.dyndns.org> (raw)
In-Reply-To: <a3cOQ-1eQ-3@gated-at.bofh.it>

Leibowitz, Michael <michael.leibowitz@intel.com> wrote:

> If I understand correctly, the following should accomplish what I'm
> looking for.  However, pivot_root gives me EBUSY.  I played around with
> moving the mount --bind /jail /jail to before the unshared, as well as
> making old_root a bind mount to itself.  However, pivot_root always
> seems to fail.  Is there something obvious that I'm doing wrong?  The
> following is my test code (error checking has been removed for clarity,
> except for pivot_root).
> 
>   char *newargv[]= { "sh", NULL };
> 
>   chdir("/jail");
>   unshare(CLONE_NEWNS));
>   mount("/jail", "/jail", NULL, MS_BIND, NULL));
>   mount("/bin", "bin", NULL, MS_BIND, NULL));
>   mount("/usr", "usr", NULL, MS_BIND, NULL));
>   mount("/lib", "lib", NULL, MS_BIND, NULL));
>   if (pivot_root(".", "old_root")) perror("pivot_root . old_root");
>   exec("./bash-static"); /* copied to /jail prior to running */

This works for me:

#include <sys/mount.h>
#include <unistd.h>

#define _GNU_SOURCE
#include <sched.h>

#define MNT_DETACH      2       /* Detach from tree only */

int main()
{
  unshare(CLONE_NEWNS);
  mount("jail", "jail", NULL, MS_BIND, NULL);
  mount("/bin", "jail/bin", NULL, MS_BIND, NULL);
  mount("/usr", "jail/usr", NULL, MS_BIND, NULL);
  mount("/lib", "jail/lib", NULL, MS_BIND, NULL);
  /* abuse bin as the temporary old root directory */
  if (pivot_root("jail", "jail/bin"))
    perror("pivot_root");
  chdir("/");
  umount2("bin", MNT_DETACH);
  execl("./sash", NULL); /* copied to /jail prior to running */
}



       reply	other threads:[~2008-03-03 15:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <a2DnP-4KR-9@gated-at.bofh.it>
     [not found] ` <a2M7M-1rM-5@gated-at.bofh.it>
     [not found]   ` <a3cOQ-1eQ-3@gated-at.bofh.it>
2008-03-03 15:54     ` Bodo Eggert [this message]
2008-03-01 17:05 Leibowitz, Michael
2008-03-02  2:26 ` serge
2008-03-03  6:56   ` Leibowitz, Michael
2008-03-04 21:45     ` serge
2008-03-05  6:23       ` Leibowitz, Michael

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1JWCzY-0003zx-1z@be1.7eggert.dyndns.org \
    --to=7eggert@gmx.de \
    --cc=Leibowitz@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.leibowitz@intel.com \
    --cc=serge@hallyn.com \
    --subject='RE: CLONE_NEWNS and bind mounts to make "chroot" jail' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).