security patch for --chroot feature
Here's a patch to make the --chroot feature more secure. 1) At least with the Linux kernel, root can break out of a chroot jail: http://www.linuxsecurity.com/feature_stories/feature_story-99.html [I verified this to be true with a 2.2 kernel.] I'm not sure whether this is true for other kernels, and I'm not sure whether other kernels require the process to be root before a chroot can succeed. So, my uid check may need to be ported. 2) With older Linux kernels, you could break out of a chroot jail if you didn't cd to the directory to which you will chroot. 3) I added checks to make sure that --user and/or --group work. The most important check is to make sure that setuid succeeds (if --chroot was specified). -scott p.s. I'm not (yet) subscribed to the list. ================================================================== --- main.c~ Wed Sep 4 07:52:25 2002 +++ main.c Sun Oct 13 15:37:46 2002 @@ -481,6 +481,15 @@ return 2; } if (chroot_dir) { + if (!uid) { + werror("-C can be defeated without -u, exitting.\n"); + return 3; + } + + if (chdir(chroot_dir) < 0) { + werror("Error chdiring, exiting.\n"); + return 3; + } if (chroot(chroot_dir) < 0) { werror("Error chrooting, exiting.\n"); return 3; @@ -488,9 +497,18 @@ } if (uid || gid) { - setgid(gid); - setgroups(0, NULL); - setuid(uid); + if (gid && setgid(gid) < 0) { + werror("Error setgiding, exiting.\n"); + return 4; + } + if (setgroups(0, NULL) < 0) { + werror("Error setgrouping, exiting.\n"); + return 4; + } + if (uid && setuid(uid) < 0) { + werror("Error setuiding, exiting.\n"); + return 4; + } } if (!debug_flag) {
On Sun, Oct 13, 2002 at 03:57:59PM -0700, Scott Weikart wrote:
Here's a patch to make the --chroot feature more secure.
anyone using chroot() care to check if this patch works? I'm adding this to my pending queue, but would be happy to receive feedback. (to avoid the res_init() case, which seemed to be a good idea, but caused problems on a couple of platforms) -- Bazsi PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
On Sun, Oct 13, 2002 at 03:57:59PM -0700, Scott Weikart wrote:
I'm not sure whether this is true for other kernels, and I'm not sure whether other kernels require the process to be root before a chroot can succeed. So, my uid check may need to be ported.
Only root can chroot, that's common.
--- main.c~ Wed Sep 4 07:52:25 2002 +++ main.c Sun Oct 13 15:37:46 2002 @@ -481,6 +481,15 @@ return 2; } if (chroot_dir) { + if (!uid) { + werror("-C can be defeated without -u, exitting.\n"); + return 3; + } + + if (chdir(chroot_dir) < 0) { + werror("Error chdiring, exiting.\n"); + return 3; + } if (chroot(chroot_dir) < 0) { werror("Error chrooting, exiting.\n"); return 3;
If you chdir there, you can just chroot("."). -- :(){ :|:&};:
participants (3)
-
Balazs Scheidler
-
Scott Weikart
-
Tommi Virtanen