Detecting a chroot jail from within

后端 未结 8 631
名媛妹妹
名媛妹妹 2020-12-24 03:02

How can one detect being in a chroot jail without root privileges? Assume a standard BSD or Linux system. The best I came up with was to look at the inode value for \"/\"

相关标签:
8条回答
  • 2020-12-24 03:40

    I wanted the same information for a jail running on FreeBSD (as Ansible doesn't seem to detect this scenario).

    On the FreeNAS distribution of FreeBSD 11, /proc is not mounted on the host, but it is within the jail. Whether this is also true on regular FreeBSD I don't know for sure, but procfs: Gone But Not Forgotten seems to suggest it is. Either way, you probably wouldn't want to try mounting it just to detect jail status and therefore I'm not certain it can be used as a reliable predictor of being within a jail.

    I also ruled out using stat on / as certainly on FreeNAS all jails are given their own file system (i.e. a ZFS dataset) and therefore the / node on the host and in the jail both have inode 4. I expect this is common on FreeBSD 11 in general.

    So the approach I settled on was using procstat on pid 0.

    [root@host ~]# procstat 0
      PID  PPID  PGID   SID  TSID THR LOGIN    WCHAN     EMUL          COMM        
        0     0     0     0     0 1234 -        swapin    -             kernel      
    [root@host ~]# echo $?
    0
    [root@host ~]# jexec guest tcsh
    root@guest:/ # procstat 0
    procstat: sysctl(kern.proc): No such process
    procstat: procstat_getprocs()
    root@guest:/ # echo $?
    1
    

    I am making an assumption here that pid 0 will always be the kernel on the host, and there won't be a pid 0 inside the jail.

    0 讨论(0)
  • 2020-12-24 03:42

    If you entered the chroot with schroot, then you can check the value of $debian_chroot.

    0 讨论(0)
提交回复
热议问题