c++ - Why doesn't exec() work after chroot()? -



c++ - Why doesn't exec() work after chroot()? -

i playing around exec-family functions , i've seen unusual behavior: don't seem work after chroot() scheme call.

here's relevant quote manpages:

special semantics execlp() , execvp()

the execlp(), execvp(), , execvpe() functions duplicate actions of shell in searching executable file if specified filename not contain slash (/) character. file sought in colon-separated list of directory pathnames specified in path envi‐ ronment variable. if variable isn't defined, path list defaults current directory followed list of directories returned confstr(_cs_path). (this confstr(3) phone call typically returns value "/bin:/usr/bin".)

if specified filename includes slash character, path ignored, , file @ specified pathname executed.

that theory, let's see how behaves:

i have prog.c file executed execlp:

#include <stdio.h> int main() { puts("works!"); homecoming 0; }

and have exec.c file effort execute prog:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <dirent.h> int main(int argc, char* const argv[]) { const char path[] = "/home/zaffy/cool"; if (argc < 2) homecoming 1; if (argc > 2 && (chdir(path) || chroot(path))) { printf("cannot chroot(%s): %s\n", path, strerror(errno)); homecoming 1; } /* clear our environment, including path */ clearenv(); if (execlp(argv[1], argv[1], null)) { printf("cannot execlp(%s): %s\n", argv[1], strerror(errno)); /* well, failed... let's see contents of current root */ struct dirent* entry; dir* dir = opendir("/"); while ( (entry = readdir(dir)) ) printf("%s\n", entry->d_name); closedir(dir); } homecoming 0; }

all tests done in /home/zaffy/cool:

/home/zaffy/cool ├── exec ├── exec.c ├── prog └── prog.c test one: exec without phone call chroot: # /home/zaffy/cool/exec /home/zaffy/cool/prog works! test two: exec phone call 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 man-pages, if have passed absolute path execlp should not search in path, or if path not set, should set current directory i'm not able see problem here.

the file certainly exists , available! if utilize fopen right before execlp, fopen finds , opens file, execlp still emits error no such file or directory.

do have thought why happens ? why doesn't exec() work after chroot() ?

your problem programme you're trying exec dynamic linked, , dynamic linker not nowadays in /lib in chroot environment. cause enoent (no such file or directory) error. adding won't help. you'd need other files dynamic-linked programme depends on, including shared libraries , essential configuration/table/etc. files these libraries need.

c++ c linux exec

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -