The chroot operation on a system changes the current root directory of the process. It effectively hides the root directory to the process.
There are two manual pages about the chroot... one talks about the chroot command and another talks about the chroot system call. We are here with talking about the chroot system call.
The manual page of the chroot system call has the following prototype.
int chroot(const char *path);
please be sure to include the header file <unistd.h>
.
only root users or root privileged programs can call the chroot. Otherwise, EACCESS will be returned due to invalid / non-privileged permissions of the caller.
usually the chroot system call is used by the daemons or remote logging programs when performing logging or opening log files (ex: ftp) . This is usually the case because the remote attacker can open a file under / or /etc/ and overwrite the file contents.
The chroot can be used as a jail by the process to limit its scope of the visibility to the file system and the files around it.
So when chroot has been called and sucessful, the current working directory for the process becomes /
although it is not running under /
.
The chroot needs to be combined with the chdir system call. It is done as follows. First execute chdir system call on and then chroot system call.
The following example demonstrates this:
#include <stdio.h>
#include <unistd.h>
#define CHROOT_DIR "/home/dev/"
int main(void)
{
FILE *fp;
int ret;
char filename[100];
strcpy(filename, "./test");
fp = fopen(filename, "w");
if (!fp) {
fprintf(stderr, "failed to open %s\n", filename);
return -1;
}
fprintf(stderr, "opened %s success\n", filename);
fclose(fp);
ret = chdir(CHROOT_DIR);
if (ret != 0) {
fprintf(stderr, "failed to chdir to " CHROOT_DIR);
return -1;
}
fprintf(stderr, "chdir success %d\n", ret);
ret = chroot(CHROOT_DIR);
if (ret != 0) {
fprintf(stderr, "failed to chroot into " CHROOT_DIR);
return -1;
}
fprintf(stderr, "chroot success %d\n", ret);
fp = fopen(filename, "r");
printf("fp %p\n", fp);
if (!fp) {
fprintf(stderr, "failed to open %s\n", filename);
return -1;
}
return 0;
}
So before running this program, we change the directory to some new directory. I was running this program in /mnt/linux_drive/gists/
. My home directory is /home/dev/
.
The program creates a new file called test
under /mnt/linux_drive/gists/
and then changes the directory to /home/dev/
and chroots into the /home/dev/
directory. It then tests if the chroot
is successful by opening the program in the same directory.
useful links on the chroot: