user823738
user823738

Reputation: 17521

Why doesn't exec() work after chroot()?

I was playing around with exec-family functions and I've seen a really strange behavior: they don't seem to work after chroot() system call.

Here's a relevant Quote from manpages:

Special semantics for execlp() and execvp()

The execlp(), execvp(), and execvpe() functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in the PATH envi‐ ronment variable. If this variable isn't defined, the path list defaults to the current directory followed by the list of directories returned by confstr(_CS_PATH). (This confstr(3) call typically returns the value "/bin:/usr/bin".)

If the specified filename includes a slash character, then PATH is ignored, and the file at the specified pathname is executed.

That was the theory, now let's see how it behaves:

Test One: exec without call to chroot:

# /home/zaffy/cool/exec /home/zaffy/cool/prog
works!

Test Two: exec with call to chroot:

# /home/zaffy/cool/exec /prog 1
Cannot execlp(/prog): No such file or directory
.
..
prog.c
prog
exec.c
exec

I'm confused! According to man-pages, if I have passed absolute path to execlp it should not search in PATH, or if the PATH is not set, it should be set also to the current directory so I'm not able to see the problem here.

The file surely exists and is available! Even if I use fopen right before execlp, the fopen finds and opens the file, but execlp still emits the error No such file or directory.

Do you have any idea why this happens ? Why doesn't exec() work after chroot() ?

Upvotes: 3

Views: 1408

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215447

Your problem is most likely that the program you're trying to exec is dynamic linked, and the dynamic linker is not present in /lib in the chroot environment. That would cause the ENOENT (No such file or directory) error. However just adding it by itself won't help. You'd need all the other files the dynamic-linked program depends on, including shared libraries and any essential configuration/table/etc. files these libraries need.

Upvotes: 11

Related Questions