Portál AbcLinuxu, 13. července 2025 15:28
chroot
u můžete posoudit třeba s takovýmto programem:
#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <dirent.h> #include <string.h> #define CHROOT_TEMP "chroot_temp" int ls(char *dirname) { DIR *dir = opendir(dirname); struct dirent *dirent; char tmp[4096]; struct stat st; if (dir == NULL) return -1; printf("Výpis adresáře '%s'\n", dirname); while ((dirent = readdir(dir))) { strcpy(tmp, dirname); strcat(tmp, "/"); strcat(tmp, dirent->d_name); lstat(tmp, &st); if (S_ISDIR(st.st_mode)) printf("d"); else if (S_ISLNK(st.st_mode)) printf("l"); else if (S_ISBLK(st.st_mode)) printf("b"); else if (S_ISCHR(st.st_mode)) printf("c"); else if (S_ISFIFO(st.st_mode)) printf("p"); else if (S_ISSOCK(st.st_mode)) printf("s"); else if (S_ISREG(st.st_mode)) printf("-"); else printf("?"); if (st.st_mode & 00400) printf("r"); else printf("-"); if (st.st_mode & 00200) printf("w"); else printf("-"); if (st.st_mode & 04000) if (st.st_mode & 00100) printf("s"); else printf("S"); else if (st.st_mode & 00100) printf("x"); else printf("-"); if (st.st_mode & 00040) printf("r"); else printf("-"); if (st.st_mode & 00020) printf("w"); else printf("-"); if (st.st_mode & 02000) if (st.st_mode & 00010) printf("s"); else printf("S"); else if (st.st_mode & 00010) printf("x"); else printf("-"); if (st.st_mode & 00004) printf("r"); else printf("-"); if (st.st_mode & 00002) printf("w"); else printf("-"); if (st.st_mode & 01000) if (st.st_mode & 00001) printf("t"); else printf("T"); else if (st.st_mode & 00001) printf("x"); else printf("-"); printf(" %2u %6u %6u %10lu %s", st.st_nlink, st.st_uid, st.st_gid, st.st_size, dirent->d_name); if (S_ISLNK(st.st_mode)) { tmp[readlink(tmp, tmp, sizeof(tmp))] = 0; printf(" -> %s", tmp); } printf("\n"); } return 0; } int main() { int d; int i; struct stat fi; char buf[1024]; printf("Aktuální adresář: %s\n", getcwd(buf, sizeof(buf))); ls("."); if (stat("chroot_temp", &fi) < 0) mkdir("chroot_temp", 0700); /* uděláme si podadresář */ else if ((fi.st_mode & S_IFDIR) == 0) { printf("Dočasný adresář '" CHROOT_TEMP "' nelze vytvořit.\n"); return 1; } d = open(".", O_RDONLY); /* vytvoříme si návratový bod */ chroot("chroot_temp"); /* zanoříme se do podadresáře */ fchdir(d); /* vyskočíme zpět a chroot pokazíme */ for (i = 0; i < 10000; i++) /* utečeme */ chdir(".."); chroot("."); /* a potvrdíme to */ printf("Aktuální adresář: %s\n", getcwd(buf, sizeof(buf))); ls("."); return 0; }Prográmek se přeloží a vyzkouší takto:
gcc -static -o chroot_test chroot_test.c chroot `pwd` /chroot_testFungovat to však bude jen se správcovskými právy - to je koneckonců napsáno i v manuálové stránce:
man 2 chroot
.
O této problematice najdete více tady a ještě tady.
If the user is root or the program is setuid root, special care must be taken. The setuid function checks the effec- tive uid of the caller and if it is the superuser, all process related user ID's are set to uid. After this has occurred, it is impossible for the program to regain root privileges.
book:/# chroot /tmp/ /chroot_test Aktuální adresár: / Výpis adresáre '.' drwxr-xr-x 3 0 0 42 . drwxr-xr-x 3 0 0 42 .. -rwxr-xr-x 1 1000 100 502147 chroot_test drwx------ 2 0 0 6 chroot_temp Aktuální adresár: / Výpis adresáre '.' drwx------ 2 0 0 6 . drwx------ 2 0 0 6 ..
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.