Re: CLONE_NEWNS and bind mounts to make "chroot" jail
From: Leibowitz, Michael
Date:  Wed Mar 05 2008 - 01:27:08 EST
I'm not 100% sure if this is what you meant, but I did get the following
to work: 
chdir("/jail"); 
unshare(CLONE_NEWNS);
mount("/jail", "/jail", NULL, MS_BIND, NULL);
pivot_root("/jail", "/jail/old_root");
chdir("/");
mount("/old_root/bin", "bin", NULL, MS_BIND, NULL);
mount("/old_root/usr", "usr", NULL, MS_BIND, NULL);
mount("/old_root/lib", "lib", NULL, MS_BIND, NULL);
umount2("/old_root", MNT_DETACH);
exec("/busybox");
Thanks for the help.  
On Tue, 2008-03-04 at 15:45 -0600, serge@xxxxxxxxxx wrote:
> Quoting Leibowitz, Michael (michael.leibowitz@xxxxxxxxx):
> Yes, you
> 	cd /jail
> 	mount --bind /jail /jail
> 	pivot_root . old_root
> 
> but . is now mounted over.
> >   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 */
-- 
Michael Leibowitz <michael.leibowitz@xxxxxxxxx>
Attachment:
signature.asc
Description: This is a digitally signed message part