Hi, I would like to make a program that chroots into a directory, gives up rights, and executes a program inside the chrooted directory. But it seems not to work.
My program is able to open the file to execute with an open() , but the execl gives the "no such file or directory error".
This is my code:
And this is my output
The file check has been created in the dir where the executable is.
Thanks in advance.
My program is able to open the file to execute with an open() , but the execl gives the "no such file or directory error".
This is my code:
Code:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/types.h>
#define CHROOT_DIR "dir"
#define EXEC_FILE "/ls"
#define TEST_FILE "check"
int main(int argc, char *argv[])
{
if(chroot(CHROOT_DIR)!=0)
{
perror("error in chroot()\r\n");
}
if(setgid(502)!=0)
{
perror("error in setuid()\r\n");
}
if(setuid(502)!=0)
{
perror("error in setuid()\r\n");
}
if(chdir("/")!=0)
{
perror("error in chdir()\r\n");
}
{
int fd;
fd=open(EXEC_FILE,O_RDONLY);
if(fd<0)
perror("error in open()\r\n");
else
printf("open OK\r\n");
close(fd);
fd=open(TEST_FILE,O_WRONLY | O_CREAT);
if(fd<0)
perror("error in open() 2\r\n");
else
{
write(fd,"uuu",3);
printf("open 2 OK\r\n");
}
close(fd);
}
execl(EXEC_FILE,EXEC_FILE , NULL);
perror("exec");
}
And this is my output
Code:
open OK
open 2 OK
exec: No such file or directory
The file check has been created in the dir where the executable is.
Thanks in advance.