Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

chroot + execl

Status
Not open for further replies.

scienzia

Programmer
Feb 21, 2002
160
0
0
IT
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:

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.
 
found solution:

using ldd I find the libraries needed by ls:

Code:
# ldd ls
        librt.so.1 => /lib/tls/librt.so.1 (0x00c8d000)
        libacl.so.1 => /lib/libacl.so.1 (0x00708000)
        libselinux.so.1 => /lib/libselinux.so.1 (0x00c3a000)
        libc.so.6 => /lib/tls/libc.so.6 (0x005b4000)
        libpthread.so.0 => /lib/tls/libpthread.so.0 (0x007f3000)
        /lib/ld-linux.so.2 (0x0059b000)
        libattr.so.1 => /lib/libattr.so.1 (0x0098d000)

I have to put them in the chroot, and it works fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top