Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 26615 - OpenSSH Chroot Patch
Summary: OpenSSH Chroot Patch
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Server (show other bugs)
Hardware: All Linux
: High enhancement (vote)
Assignee: The Gentoo Linux Hardened Team
URL: http://sourceforge.net/projects/chroo...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-08-14 06:03 UTC by Mikhail
Modified: 2009-10-15 05:54 UTC (History)
5 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Adds chroot patch to OpenSSH if 'chroot' is in USE (openssh-3.7.1_p2-r2.ebuild,4.11 KB, text/plain)
2003-11-17 18:51 UTC, Matthew Rickard
Details
OpenSSH Chroot Patch (osshChroot-3.7.1p2.diff,2.82 KB, patch)
2003-11-17 18:53 UTC, Matthew Rickard
Details | Diff
Another chroot patch for openssh 3.7.1p2 (openssh-3.7-chroot.patch,11.21 KB, patch)
2004-01-08 00:41 UTC, Amir Guindehi (RETIRED)
Details | Diff
new openssh patch that works for me (openssh-3.8_p1-chroot.patch,1.77 KB, patch)
2004-04-01 01:16 UTC, Lesley van Zijl
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mikhail 2003-08-14 06:03:57 UTC
Hello, 
 
I have feature request for OpenSSH package - could you include OpneSSH-chroot patch 
into openssh package? 
 
regards, 
Mikhail.
Comment 1 Matthew Rickard 2003-08-15 08:55:32 UTC
This seems like it might be a good candidate for the chroot USE flag previously discussed on the mailing list.  Comments?
Comment 2 Mikhail 2003-08-15 11:46:14 UTC
Yes, that would be great to use with chroot flag together (although I did not know/think of chroot 
flag until now). Do you plan to add it to openssh ebuild in near future? 
Which list did you mean? gentoo-dev? 
 
regards, 
Mikhail. 
Comment 3 solar (RETIRED) gentoo-dev 2003-08-15 12:12:58 UTC
As we don't really have guidelines for the chroot use flag yet, I'll say what comes to mind.

Unfortunately this might break on all grsec enabled boxes if we trigger on the chroot use flag when priv separation is used due to double chrooting (this is a good thing). As I'm not familiar with that patch myself I'd request that it be tested with grsec's chroot permissions enabled before using the USE="chroot" as a trigger. Hopefully no problems will be encountered.
Comment 4 Mikhail 2003-08-15 13:22:12 UTC
I have 2 servers running with GRSecurity (ACLs enabled) and openssh patched with this chroot 
patch - I have no problems with using both. 
 
I suggest that you look here - http://chrootssh.sourceforge.net/ , the first paragraph on the main 
page pretty sums what the patch is about: 
 
"...What this patch does is looks for a '.' in the users home directory, then calls chroot(2) to 
whatever directory was before the . and continues with the normal ssh functionality. E.G. If your 
home directory was /chroot/./home/bob, you'd be chrooted to /chroot and /chroot/home/bob 
would actually appear as /home/bob to you. Check the documentation for further explanations." 
 
So basically if you want user to be chroot'ed, you simple modify /etc/passwd, and of course make 
sure sshd is allowed to use chroot if running with GRSecurity (+CAP_SYS_CHROOT as I remember 
now). 
 
Mikhail. 
Comment 5 solar (RETIRED) gentoo-dev 2003-08-29 22:48:19 UTC
Please attach a ebuild patch if you would like to see this feature added to openssh
Comment 6 Matthew Rickard 2003-11-17 18:51:25 UTC
Created attachment 20889 [details]
Adds chroot patch to OpenSSH if 'chroot' is in USE

This seems to work fine with grsec chroot restrictions in place.  Looks like
this is a good place to start with the chroot USE flag that was discussed
awhile back.

OpenSSH maintainers please take a look.
Comment 7 Matthew Rickard 2003-11-17 18:53:54 UTC
Created attachment 20890 [details, diff]
OpenSSH Chroot Patch

This is the patch as available from http://chrootssh.sourceforge.net/

It should be placed in ${FILESDIR}
Comment 8 Nicolas Laplante 2003-12-02 11:02:18 UTC
Tried and tested, works fine here.
Comment 9 solar (RETIRED) gentoo-dev 2003-12-19 08:02:12 UTC
Ok. Looks like it's about time to add this. 
Upstream maintainers of the patch to doing a good job of rolling new patches for new versions. Some testing is now being done at hardened for this and if all goes well you should see this in before newyears.
Comment 10 solar (RETIRED) gentoo-dev 2003-12-19 08:03:13 UTC
Usefull link for those wondering about setting it up and taking advantage.
http://mail.incredimail.com/howto/openssh/
Comment 11 solar (RETIRED) gentoo-dev 2003-12-24 10:52:04 UTC
Still waiting on some testing.. We need a supporting developer.
Comment 12 solar (RETIRED) gentoo-dev 2003-12-24 10:59:05 UTC
The indentation of this patch looks incorect or it's lacking in a close an open brace { 

+               if(chroot(user_dir) != 0)
+                   fatal("Couldn't chroot to user directory % s", user_dir);
+                   pw->pw_dir = new_root;
+                   break;
+               }

Also from the c source perspective every chroot should always call a chdir("/"); right after a chroot();
The patch is so simple I suppose we dont really need a supporting maintainer.
However please correct this these minor changes & test and attach an update 
and I'll add this sucker.
Comment 13 Amir Guindehi (RETIRED) gentoo-dev 2004-01-08 00:41:33 UTC
Created attachment 23366 [details, diff]
Another chroot patch for openssh 3.7.1p2

This is another chroot patch for openssh. It adds the following sshd_config 
options:

#ChrootAll yes
#ChrootDir %h/chome/
#ChrootUsers a,b,c
#NoChrootUsers root
Comment 14 Amir Guindehi (RETIRED) gentoo-dev 2004-01-08 01:40:16 UTC
Please note: The patch "Another chroot patch for openssh 3.7.1p2" only applies correctly after having patched the source with the X509 patch included in the current ebuild. It seems as if they overlap.... this needs some further investigations.
Comment 15 solar (RETIRED) gentoo-dev 2004-01-08 01:42:08 UTC
Ok second patch fails pretty bad for me.. So for now we will run with the first patch proposed.
Amir if you can clean up the offsets or think of a creative way where we can get the best of both worlds that would be great (the "another" patch is much cleaner overall imo)
Comment 16 solar (RETIRED) gentoo-dev 2004-01-27 17:40:35 UTC
chroot patch is in portage.
Comment 17 Lesley van Zijl 2004-04-01 01:14:26 UTC
Seems like nobody is really using this patch :)

exci@nemo exci $ scp log monnik@server:
Password:
Read from remote host server: Connection reset by peer
lost connection

I could give a strace and scp -vvvvv but I already found the fix
I'll attach a new patch from the chrootssh mailing list
credit for making the patch goes too "lee fellows"  :)
Comment 18 Lesley van Zijl 2004-04-01 01:16:56 UTC
Created attachment 28499 [details, diff]
new openssh patch that works for me
Comment 19 Amir Guindehi (RETIRED) gentoo-dev 2004-04-01 01:51:59 UTC
The above patch 'new openssh patch that works for me' is the user/./path patch. It's a ugly solution to the problem! Changing the home path to user/./path is no good solution in my eyes.

The patch "Another chroot patch for openssh 3.7.1p2" is the best one as far as I can see. Someone will need to clean it up and make it apply since it overlaps with the x509 patch.
Comment 20 solar (RETIRED) gentoo-dev 2004-04-01 02:57:33 UTC
Amir, 
Unless that "someone" is "you" I don't think it's going to get done.
Comment 21 Lesley van Zijl 2004-05-19 17:50:37 UTC
why isn't the new patch in yet ?
the old one just doesn;t work
like I commented in comment #17
the fix in comment #18 works

link with the patch from comment #18
https://sourceforge.net/mailarchive/forum.php?thread_id=4000662&forum_id=9491
Comment 22 solar (RETIRED) gentoo-dev 2004-05-21 05:37:00 UTC
Lesley van Zijl
These chroot patches don't seem to spark the interest of us. 
As I've stated a few times somebody will have to accept the responsibility maintaining this. Which clearly nobody seems interested in doing.

If you wish to see this funcitonality in OpenSSH push it upstream to the OpenSSH maintainers.
Comment 23 Amir Guindehi (RETIRED) gentoo-dev 2004-05-21 06:58:13 UTC
Check the openssh ebuild on https://gentoo.datacore.ch in the section 'DataCore's Ebuilds'. You will always find a ebuild containing one of the chroot-patches. Since I need them in production I provide an ebuild there.
Comment 24 Guilherme Balena Versiani 2009-02-20 17:26:58 UTC
Hey! I do use this chroot patch! I am posting a new updated patch for you (I hope you don't remove this feature anymore!):

--- session.c.orig      2008-06-16 10:29:18.000000000 -0300
+++ session.c   2009-02-20 14:15:13.000000000 -0300
@@ -91,6 +91,8 @@
 #include "monitor_wrap.h"
 #include "sftp.h"

+#define CHROOT
+
 #if defined(KRB5) && defined(USE_AFS)
 #include <kafs.h>
 #endif
@@ -1452,6 +1454,10 @@
 do_setusercontext(struct passwd *pw)
 {
        char *chroot_path, *tmp;
+#ifdef CHROOT
+       char *user_dir;
+       char *new_root;
+#endif /* CHROOT */

 #ifdef WITH_SELINUX
        /* Cache selinux status for later use */
@@ -1471,6 +1477,27 @@
 # ifdef __bsdi__
                setpgid(0, 0);
 # endif
+
+#ifdef CHROOT
+       user_dir = xstrdup(pw->pw_dir);
+       new_root = user_dir + 1;
+
+       while ((new_root = strchr(new_root, '.')) != NULL) {
+               new_root--;
+               if (strncmp(new_root, "/./", 3) == 0) {
+                       *new_root = '\0';
+                       new_root += 2;
+
+                       if(chroot(user_dir) != 0)
+                               fatal("Couldn't chroot to user's directory %s", user_dir);
+                       pw->pw_dir = new_root;
+                       break;
+               }
+
+               new_root += 2;
+       }
+#endif /* CHROOT */
+
 # ifdef USE_PAM
                if (options.use_pam) {
                        do_pam_setcred(use_privsep);

Comment 25 Guilherme Balena Versiani 2009-02-20 17:49:20 UTC
Ooops, the working patch is the following (now tested!):

--- session.c.orig      2008-06-16 10:29:18.000000000 -0300
+++ session.c   2009-02-20 14:38:50.000000000 -0300
@@ -91,6 +91,8 @@
 #include "monitor_wrap.h"
 #include "sftp.h"

+#define CHROOT
+
 #if defined(KRB5) && defined(USE_AFS)
 #include <kafs.h>
 #endif
@@ -1452,12 +1454,36 @@
 do_setusercontext(struct passwd *pw)
 {
        char *chroot_path, *tmp;
+#ifdef CHROOT
+       char *user_dir;
+       char *new_root;
+#endif /* CHROOT */

 #ifdef WITH_SELINUX
        /* Cache selinux status for later use */
        (void)ssh_selinux_enabled();
 #endif

+#ifdef CHROOT
+       user_dir = xstrdup(pw->pw_dir);
+       new_root = user_dir + 1;
+
+       while ((new_root = strchr(new_root, '.')) != NULL) {
+               new_root--;
+               if (strncmp(new_root, "/./", 3) == 0) {
+                       *new_root = '\0';
+                       new_root += 2;
+
+                       if(chroot(user_dir) != 0)
+                               fatal("Couldn't chroot to user's directory %s", user_dir);
+                       pw->pw_dir = new_root;
+                       break;
+               }
+
+               new_root += 2;
+       }
+#endif /* CHROOT */
+
 #ifndef HAVE_CYGWIN
        if (getuid() == 0 || geteuid() == 0)
 #endif /* HAVE_CYGWIN */
@@ -1471,6 +1497,7 @@
 # ifdef __bsdi__
                setpgid(0, 0);
 # endif
+
 # ifdef USE_PAM
                if (options.use_pam) {
                        do_pam_setcred(use_privsep);
Comment 26 Sergey S. Starikoff 2009-10-15 05:54:08 UTC
Currently this patch (and idea) are hardly deprecated.
The USE flag, some time added to openssh ebuild was removed.

Use sys-auth/pam_chroot instead of this patch.