[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to detect if inside a buildd chroot



On 9/29/07, Anthony DeRobertis <anthony@derobert.net> wrote:
> Hmm, if you're root you probably can. Something like this (completely
> untested; probably doesn't even compile):

Yes, it works, and AFAIK it has always done.  A shorter program to test it:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int
main(void) {
        struct stat s1, s2;

        chdir("/");
        chroot("/tmp");
        stat(".", &s1);
        stat("..", &s2);

        if (s1.st_dev != s2.st_dev || s1.st_ino != s2.st_ino) {
                printf("we are in a chroot\n");
        }
        return 0;
}

> Actually, come to think of it, I wonder if stat'ing "/.." would work...
> If it does, then you don't even need root.

No, it doesn't.  "/" is always equal to "/.."; if not, anyone could
escape anytime from a chroot environment.

Juan



Reply to: