Expected bahaviour of os.chroot and os.getcwd

r0g aioe.org at technicalbloke.com
Mon Sep 13 20:04:53 CEST 2010


Hi CLP!

Not been here for ages, I hope everyone is doing well :)

I just want to check if this is the intended behaviour (2.2 to 2.7)...

> import os
> print os.getcwd()
> os.chroot("/home/r0g/whatever/")
> print os.getcwd()
> os.chdir("/")
> print os.getcwd()

>>> /home/r0g/AAA_BACKED_UP/Code/py
>>> /home/r0g/AAA_BACKED_UP/Code/py
>>> /

i.e. So do I always have to change directory after changing into a chroot?

The reason I ask is because an app I was running inside the chrooted 
environment (specifically: apt-get) was trying to access files outside 
the chroot and erroring when it couldn't. I figured it must be doing a 
getcwd() and getting the cwd of the script that initialized the chroot. 
I just wanted to confirm that's how it's supposed to work so I'd 
appreciate it if anyone either knows or can point me to the docs that 
explain in more detail than http://docs.python.org/library/os.html

Also, out of curiosity... If it does work (and should work) the way I 
think it does how come os.chroot doesn't set the cwd to "/" for you? 
It's not a costly operation and it could prevent errors of ignorance 
such as my own. Are there any good reasons why a person (who isn't a 
hacker / cracker / kludger) would want chrooted processes to be able to 
see the calling script's cwd anyway? Maybe I'm having a failure of 
imagination today but the only things I can think that info could be 
useful for are jailbreaking, nefarious reconnaissance and real ugly 
hacks. Maybe someone here can enlighten me :)

Yours curiously,

Roger Heathcote






More information about the Python-list mailing list