Subject: Re: fchroot(2) emulation ?
To: None <port-sparc@NetBSD.ORG>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: port-sparc
Date: 12/12/1997 14:49:54
(Does this really belong on port-sparc?)

>> Is there a case for adding fchroot(2) to NetBSD ?

> As a quick fix:
>     fchroot(fd);
> becomes
>     fchdir(fd); chroot(".");
> but you will lose the directory you are currently in (cwd becomes the
> new root).

I've never understood why chroot didn't do that.  If nothing else, it
would make it (a little bit) harder to escape a chroot jail....

> Alternatively, you can use the following function, which will save
> and restore the current directory.

> [-current users note:  I've seen fchroot on other OS'es; does the
> below function look sane enough to go in libcompat?  It certainly
> doesn't *need* to be a syscall as far as I can tell.]

[compressed vertically -dM]
> int fchroot(int fd) { int cfd, rc = -1;
> 	if ((cfd = open(".", O_RDONLY)) >= 0) {
> 		if (fchdir(fd) == 0) {	if (chroot(".") == 0) rc = 0;
>					fchdir(cfd);
> 		}
> 		close(cfd);
> 	}
> 	return rc;
> }

Biggest problem I see with it is, what happens if the open() call
fails?  I don't think fchroot() should fail just because the current
directory has been rmdir()ed.  (Of course, that might be acceptable in
a compatability routine.)

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B