lkml.org 
[lkml]   [2008]   [Mar]   [3]   [last100]   RSS Feed
Views: [more markup]  [less markup]  [headers]  [forward] 
 
Messages in this thread
/
SubjectRE: CLONE_NEWNS and bind mounts to make "chroot" jail
DateSun, 2 Mar 2008 22:56:40 -0800
From"Leibowitz, Michael" <>
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 */
Thanks. 

>Serge replies:
 [snip...snip]
>Try a few more things.  Since you had entered /jail, you can view '/'
by
>looking at .. .  But if you look at /, you dereference your
>task->fsroot.  You never changed that, so it points to the original
>mount.  If however you 'ls ..', you should see your 'jail' directory.
>However it won't have the /bin and /lib mounted because you didn't
>	mount --rbind /jail /
>What you really want to do is
>	mount --bind /jail /jail
>to make sure it's a mountpoint, then set up the new /jail using bind
>mounts like you're doing (and likely some rbinds in some places), then
>use pivot_root() to change your root.  Then umount2("/old_root",
>MNT_DETACH).
>
>-serge

--
Michael Leibowitz
Software Engineer, UMG
Intel Corporation
michael.leibowitz at intel.com




\
 
 \ /
  Last update: 2008-03-03 08:01    [from the cache]
©2003-2008