开发者

How to exit a chroot inside a perl script?

开发者 https://www.devze.com 2023-04-09 22:53 出处:网络
While writing a perl script intended to fully automate the setup of virtual machines (Xen pv) I hit a small maybe very simple problem.

While writing a perl script intended to fully automate the setup of virtual machines (Xen pv) I hit a small maybe very simple problem.

Using perl's chroot function I do my things on the guest file system and then I need to get back to my initial real root. How the hell I do that?

Script example:

`mount $disk_image $mount_point`;

chdir($mount_point);
chroot($mount_point);

#[Do my things...]

#<Exit chroot wanted here>

`umount $mount_point`;

#[Post install things...]

I've tried exit; but obviously that exit the whole script.开发者_StackOverflow

Searching for a way to exit the chroot I've found a number of scripts who aim to exit an already setup chroot (privilege escalation). Since I do the chroot here theses methods do not aplies.

Tried some crazy things like:

opendir REAL_ROOT, "/";
chdir($mount_point);
chroot($mount_point);
chdir(*REAL_ROOT);

But no go.

UPDATE Some points to consider:

  • I can't split the script in multiple files. (Silly reasons, but really, I can't)
  • The chrooted part involve using a lot of data gathered earlier by the script (before the chroot), enforcing the need of not lunching another script inside the chroot.
  • Using open, system or backticks is not good, I need to run commands and based on the output (not the exit code, the actual output) do other things.
  • Steps after the chroot depends on what was done inside the chroot, hence I need to have all the variables I defined or changed while inside, outside.
  • Fork is possible, but I don't know a good way to handle correctly the passing of informations from and to the child.


The chrooted process() cannot "unchroot" itself by exiting (which would just exit).

You have to spawn a children process, which will chroot.

Something along the lines of the following should do the trick:

if (fork())
{
   # parent
   wait;
}
else
{
   # children
   chroot("/path/to/somewhere/");
   # do some Perl stuff inside the chroot...
   exit;  
}

# The parent can continue it's stuff after his chrooted children did some others stuff...

It stills lacks of some error checking thought.


You can't undo a chroot() on a process - that's the whole point of the system call.

You need a second process (a child process) to do the work in the chrooted environment. Fork, and have the child undergo the chroot and do its stuff and exit, leaving the parent to do the cleanup.


Try spawning a child process that does the chroot, e.g. with system or fork depending on your needs, and waiting for the child to return the main program continues.


This looks like it might be promising:

Breaking Out of a Chroot Jail Using PERL


Save the original root as the current working directory or as a file descriptor:

chdir "/";
chroot "/mnt";
# Do something
chroot ".";

OR

open DIR, "<", "/";
chroot "/mnt";
# Do something
chdir DIR;
chroot ".";
close DIR;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号