Bug 177 - provide chroot option for sftp-server
Summary: provide chroot option for sftp-server
Status: CLOSED FIXED
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: sshd (show other bugs)
Version: -current
Hardware: Other Other
: P3 enhancement
Assignee: Assigned to nobody
URL:
Keywords:
Depends on:
Blocks: V_4_8
  Show dependency treegraph
 
Reported: 2002-03-21 05:16 AEDT by Nico Kadel-Garcia
Modified: 2008-10-29 08:56 AEDT (History)
6 users (show)

See Also:


Attachments
Chroot patch for openssh-3.8p1 (3.94 KB, patch)
2004-07-11 23:50 AEST, Bill Swartz
no flags Details | Diff
A patch to provide chroot functionality to sftp-server (1.23 KB, patch)
2005-10-30 16:25 AEDT, dAniel hAhler
no flags Details | Diff
sftp chroot patch for OpenSSH -current 2006/07/06+ (3.12 KB, patch)
2006-07-06 21:00 AEST, Damien Miller
no flags Details | Diff
Updated patch (3.71 KB, patch)
2006-11-10 02:52 AEDT, Damien Miller
no flags Details | Diff
Pre-shell tilde expantion proof-of-concept hack (1.24 KB, patch)
2007-05-11 08:02 AEST, Joshua Pettett
no flags Details | Diff
Updated chroot patch for OpenSSH 4.7_p1 (3.24 KB, patch)
2007-09-11 08:09 AEST, Joshua Pettett
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Nico Kadel-Garcia 2002-03-21 05:16:38 AEDT
I've updated and modified some older OpenSSH chroot tools to work correctly 
with OpenSSH 3.1p1. I'd like to get them integrated into the code base, in 
strong preference to that ghods-awful "chroot" shell scripting done by ssh-3.x. 
The patch modifies OpenSSH to do a chroot to the part of any $HOMEDIR which 
contains the characters '/./', to the directory location preceeding that set of 
characters.

The chroot tarball building tool is designed to grab the user-selected shell, 
ssh, and another other selected components along with the required dynamic 
libraries to allow those tools function.
Comment 1 Markus Friedl 2002-03-22 08:07:08 AEDT
chroot would be nice to have, but having sshd chroot for /./ in $HOME
is not a standard behaviour.
Comment 2 Nico Kadel-Garcia 2002-03-22 08:43:16 AEDT
Well, it wasn't my original idea, I'm just trying to get it implemented 
cleanly. It's not "common behavior" for rather different chroot environments, 
such as the limited environment of ftpd. That works for anonymous ftpd logins 
because the ftpd remains present as the user's interactive shell, interpreting 
their commands. To do this for OpenSSH, sshd or something like it would have to 
be use some kind of restricted shell, maintained and forked off, and it would 
prohibit local user login.

By using the "/./" as a flag for the local user, the chroot behavior remains 
under root control, the user can use any shell the admin is willing to install 
for them, and once can even created shared environments as follows.

    nkadel:*:1000:1000:Shared SSH chroot for 
Nico:/home/shared/./../nkadel:/bin/bash

If I log in locally, or look for my email, I wind up in /home/nkadel. If I come 
in via SSH, I wind up in /home/shared.

This as opposed to:

    nkadel2:*:1000:1000:chroot SSH for Nico:/home/nkadel/./:/bin/bash

For this, I'd wind up in /home/nkadel in a chroot cage.

Does this make sense? I'd welcome better ideas.
Comment 3 Russell McOrmond 2003-07-29 02:48:47 AEST
  I am wondering if a patch, or something like it, will end up in the mainline
openssh source tree?

I found http://chrootssh.sourceforge.net/ and then went looking to see if that
patch was made and found this bug.

I'm just wanting to set up a chroot() jail to accomplish with sftp/scp and
rsync/cvs over SSH what we now get with many ftpd's.

Comment 4 Marius Andreiana 2004-06-30 01:36:59 AEST
I saw other chroot bugs were marked as WONTFIX. Why do you refuse adding chroot
to openssh?
SCP is a secure alternative to FTP, but doesn't have chroot as most of FTP
servers do.

This project is up to date
http://chrootssh.sourceforge.net/

Thank you
Comment 5 Bill Swartz 2004-07-11 23:50:18 AEST
Created attachment 683 [details]
Chroot patch for openssh-3.8p1

I have looked at the proposed patch and found it very strange.	For what it is
worth, here is a patch based on SSH2's ChRootUsers sshd configuration option.
Of course, you will need to add system files such as /dev/ttys and /lib to each
user account but that is exactly what I needed in order to control the user's
environment. See page 185 of Barrett and Silverman's O'Reilly SSH book.
Comment 6 dAniel hAhler 2005-10-30 16:25:20 AEDT
Created attachment 1018 [details]
A patch to provide chroot functionality to sftp-server

The patch is taken from http://mail.incredimail.com/howto/openssh/addons/sftp-chroot.diff

It allows to setup sftp-server for an user without putting any libs into the chroot, using the described "magic /./" to enable it and define the chroot directory (within the home directory).
Comment 7 Damien Miller 2006-04-25 18:37:56 AEST
Please see bug #474 for a patch against CVS -current
Comment 8 Damien Miller 2006-07-06 21:00:41 AEST
Created attachment 1156 [details]
sftp chroot patch for OpenSSH -current 2006/07/06+

This patch adds chroot support for sftp-server. It won't be committed until better sshd_config support for enabling it is ready, because it is still too easy for people to activate it in a way that is easy to circumvent or that creates new holes
Comment 9 Damien Miller 2006-07-06 21:01:32 AEST
remove URL, it is dead.
Comment 10 dAniel hAhler 2006-09-06 06:38:27 AEST
What about using PAM for sftp-server?

Currently, as it seems, only /etc/pam.d/ssh gets used (also for the sftp subsystem), but I thought that it would be a nice idea to use /etc/pam.d/sftp-server instead, if it is available.

This way, you could use
  session    required   pam_chroot.so
in /etc/pam.d/sftp-server and it would chroot to the path given in /etc/security/chroot.conf from within sftp-server (and therefor should also not require to have any libs in each chroot).

Does this sound reasonable? Should I open a new tracker wish item for that?
Comment 11 Damien Miller 2006-11-10 02:52:28 AEDT
Created attachment 1206 [details]
Updated patch

Patch updated to -current as of November 9th 2006. Same caveats that I mentioned previously apply.
Comment 12 Joshua Pettett 2007-05-09 09:06:42 AEST
(In reply to comment #11)

So far, this is the best solution I have seen to the SFTP chroot problem.  I would love to see it officially implemented, but may begin using it on a production system long before that.

However, I would like to know what the potential risks are and would also like to help eliminate them if I can.  So far, the only serious issue I've found is that tilde expansion done by the shell uses the $HOME environment variable, which can of course be set in .ssh/environment .

If I can be of any help in addressing this or other issues, please let me know.
Comment 13 Joshua Pettett 2007-05-11 08:02:44 AEST
Created attachment 1277 [details]
Pre-shell tilde expantion proof-of-concept hack

(In reply to comment #12)
> So far, the only serious issue I've found is that tilde expansion
> done by the shell uses the $HOME environment variable, which can of
> course be set in .ssh/environment .

In light of this, would it be appropriate for sshd to expand a tilde in a subsystem argument before passing it to the shell?  A kludgy proof-of-concept hack is attached.
Comment 14 Joshua Pettett 2007-09-11 08:09:33 AEST
Created attachment 1346 [details]
Updated chroot patch for OpenSSH 4.7_p1

Revision 1.73 of sftp-server.c is slightly incompatible with the previous path.  This tweaked one works with it.
Comment 15 Damien Miller 2008-02-10 22:52:37 AEDT
A newer version of the patch attached to bug #1352 has just been committed, with additional support for an in-process sftp-server to avoid the need to configure the chroot with support files. This will be in openssh-4.8.
Comment 16 Damien Miller 2008-03-31 15:19:42 AEDT
Fix shipped in 4.9/4.9p1 release.
Comment 17 yezkvisvadk 2008-04-08 04:47:28 AEST
Out of curiosity, could you summarize the reasons that the sftp-server level chrooting was discarded in favor of sshd ChrootDirectory? At first look it seems better to have a small sftp-server process running than the full blown sshd, to provide this functionality. 

Thanks in advance.
Comment 18 Damien Miller 2008-04-08 09:05:16 AEST
Huh? You need sshd running anyway.
Comment 19 yezkvisvadk 2008-04-08 22:12:33 AEST
I guess I was not clear. Why have the sftp-server embedded, risking bugs in the full blown sshd code, and not just fork the simple sftp-server and perform the chroot there (like your patch in this bug does)? What were the reasons the first solution was prefered? In the first comments you mention some things but I don't quite understand.
Comment 20 Joshua Pettett 2008-10-28 07:43:47 AEDT
As far as I can tell, ChrootDirectory does not allow per-user chrooting to "empty jails" (e.g. home directories) such as this bug's patch does (and modern FTP servers).  So from my perspective this bug isn't resolved.  Should it be reopened, or should I submit a new bug/feature request?

Thanks.
Comment 21 Damien Miller 2008-10-29 08:39:32 AEDT
No, ChrootDirectory can be set per-user using the sshd_config(5) Match option and may be used to chroot users into empty jails for sftp use.
Comment 22 Joshua Pettett 2008-10-29 08:56:21 AEDT
(In reply to comment #21)
> No, ChrootDirectory can be set per-user using the sshd_config(5) Match
> option and may be used to chroot users into empty jails for sftp use.

Yes, but since the directory has to be owned by root, then users can't write to their home directory, correct?