compartment-1.1.0/0040755000175000017500000000000007772304743012211 5ustar jfsjfscompartment-1.1.0/Makefile0100644000175000017500000000114207101025113013615 0ustar jfsjfsCC=gcc OPTS=-Wall -O2 BIN_DIR=/usr/sbin BIN_LIST=compartment DOC_DIR=/usr/doc/packages/compartment DOC_LIST=README LICENCE CHANGES TODO MAN_DIR=/usr/share/man/man1 MAN_LIST=compartment.1 all: compartment compartment: compartment.c ${CC} ${OPTS} -o ${BIN_LIST} compartment.c strip ${BIN_LIST} clean: rm -f ${BIN_LIST} core *~ install: compartment install -o root -g root -m 751 ${BIN_LIST} ${BIN_DIR} install -d -o root -g root -m 755 ${DOC_DIR} install -o root -g root -m 644 ${DOC_LIST} ${DOC_DIR} install -d -o root -g root -m 755 ${MAN_DIR} install -o root -g root -m 644 ${MAN_LIST} ${MAN_DIR} compartment-1.1.0/compartment.c0100644000175000017500000003375007246173653014713 0ustar jfsjfs/* SuSE secure compartment v1.1 (c) 2000 by Marc Heuse * * Description: * Compartment provides all possibilities to securely run insecure and/or * untrusted programs. It provides you with all necessary options to fine * grain the tightening process as you need it. * * Please note that you need a Linux kernel 2.2.12 or later! * * Official releases and updates can be found on the SuSE Linux distribution * from version 7.0 on and ftp.suse.com plus mirror sites. * * Unofficial updates and betas can be found at http://www.suse.de/~marc * * Please contact me directly at marc@suse.de for bugs, comments or ideas. * * Thanks go to Solar Designer for comments & critics ... * */ #include #include #include #include #include #define _LINUX_STRING_H_ #include #include #include #include #include #include #include #include #include #include #define PROGRAM_NAME "SuSE secure compartment" #define VERSION "v1.1" #define AUTHOR "Marc Heuse " #define POINTER "http://www.suse.de/~marc" #define LOGBUF 5000 char *_env[] = { "HOME=/", "COMPARTMENT=YES", "PATH=/bin:/usr/bin:/", "" }; int cap_set_no[29] = { CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_DAC_READ_SEARCH,CAP_FOWNER,CAP_FSETID, CAP_FS_MASK,CAP_KILL,CAP_SETGID,CAP_SETUID,CAP_SETPCAP,CAP_LINUX_IMMUTABLE, CAP_NET_BIND_SERVICE,CAP_NET_BROADCAST,CAP_NET_ADMIN,CAP_NET_RAW,CAP_IPC_LOCK, CAP_IPC_OWNER,CAP_SYS_MODULE,CAP_SYS_RAWIO,CAP_SYS_CHROOT,CAP_SYS_PTRACE, CAP_SYS_PACCT,CAP_SYS_ADMIN,CAP_SYS_BOOT,CAP_SYS_NICE,CAP_SYS_RESOURCE, CAP_SYS_TIME,CAP_SYS_TTY_CONFIG, 0 }; char cap_set_names[29][29] = { "CAP_CHOWN","CAP_DAC_OVERRIDE","CAP_DAC_READ_SEARCH","CAP_FOWNER","CAP_FSETID", "CAP_FS_MASK","CAP_KILL","CAP_SETGID","CAP_SETUID","CAP_SETPCAP", "CAP_LINUX_IMMUTABLE","CAP_NET_BIND_SERVICE","CAP_NET_BROADCAST", "CAP_NET_ADMIN","CAP_NET_RAW","CAP_IPC_LOCK","CAP_IPC_OWNER","CAP_SYS_MODULE", "CAP_SYS_RAWIO","CAP_SYS_CHROOT","CAP_SYS_PTRACE","CAP_SYS_PACCT", "CAP_SYS_ADMIN","CAP_SYS_BOOT","CAP_SYS_NICE","CAP_SYS_RESOURCE","CAP_SYS_TIME", "CAP_SYS_TTY_CONFIG", "" }; extern int capset(cap_user_header_t header, cap_user_data_t data); extern char **environ; int program_params, fd; int do_chroot, do_group, do_user, do_caps, do_init, verbose, quiet; char *chroot_path, *init_program, *prg, *user, **_argv; long tmp; __u32 temp, caps = 0; uid_t set_user; gid_t set_group; struct passwd *pw; struct group *gr; void help() { fprintf(stderr, "%s %s %s %s\n\n", PROGRAM_NAME, VERSION, AUTHOR, POINTER); fprintf(stderr, "Syntax: %s [options] /full/path/to/program\n", prg); fprintf(stderr, "Options: \t --chroot path\t chroot to path \t --user user\t change uid to this user \t --group group\t change gid to this group \t --init program\t execute this program/script before doing anything \t --cap capset\t set capset name. This option can be used several times. \t --verbose\t be verbose \t --quiet\t do no logging (to syslog) \t --fork\t\t fork (if everything is fine) \nHints: always try to chroot; use --user&group if possible; chroot and chown all files to another user than root if you use capabilties. Read the README file! \nKnown capset names: none"); tmp = 0; while(strlen(cap_set_names[tmp])>0) fprintf(stderr," %s",cap_set_names[tmp++]); fprintf(stderr, " (see linux/capability.h for more information)\n"); exit(-1); } void print_msg(const char *format, ...) { va_list arg; char *log; if (quiet == 0) { log = malloc(LOGBUF); va_start(arg, format); vfprintf(stderr, format, arg); vsnprintf(log, LOGBUF, format, arg); syslog(LOG_NOTICE, log); va_end(arg); free(log); } } void my_secure() { char file[10] = "/dev/null"; int mode = O_RDWR; alarm(0); if (verbose) print_msg("Turned possible alarm(2)s off\n"); environ = _env; // set a safe and clean environment if (verbose) print_msg("Safe environment set\n"); if ((fd = open(file, mode)) < 0) { file[1] = '\0'; mode = O_RDONLY; if ((fd = open(file, mode)) < 0) { print_msg("Can not open /dev/null nor /, no more fd's?\n"); exit(-1); } } while (fd < 2) fd = open(file, mode); close(fd); if (verbose) print_msg("Ensured that fd 0-2 are open\n"); /* FIXME: should limits be reset or signals be blocked or ignored? */ return; } void my_caps() { cap_user_header_t head = (cap_user_header_t) calloc(1, sizeof(cap_user_header_t)); cap_user_data_t cap = (cap_user_data_t) calloc(1, sizeof(cap_user_data_t)); head->version = _LINUX_CAPABILITY_VERSION; head->pid = 0; cap->inheritable = cap->permitted = cap->effective = caps; // if (capget(head, cap) != 0) { // print_msg("Could not get capabilities.\n"); // exit(-1); // } if (capset(head, cap) != 0) { print_msg("Could not set capabilities.\n"); exit(-1); } else { if (verbose) print_msg("Capabilities successfully set to 0x%0x\n",cap->effective); } free(head); free(cap); return; } int main (int argc, char *argv[]) { struct stat st; int do_fork = 0; struct rlimit limit = {0, 0}; unsigned long int uidrange = 65535; /* process the parameters */ prg = argv[0]; if (argc < 2) help(); if (strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0) help(); my_secure(); openlog("SuSEcompartment", LOG_PID, LOG_DAEMON); if (sizeof(uid_t) == 4) { (unsigned long int) uidrange = 65535; } else { if (sizeof(uid_t) == 8) { (unsigned long int) uidrange = (unsigned long int) 2147483646; //4294967295; } else fprintf(stderr, "Warning: weird uid size: %d\n", sizeof(uid_t)); } program_params = 1; while((argc - 1) > program_params && strncmp(argv[program_params], "--", 2) == 0) { if (strcmp(argv[program_params], "--chroot") == 0) { chroot_path = argv[++program_params]; do_chroot = 1; } else { if (strcmp(argv[program_params], "--user") == 0) { if ((pw = (struct passwd *) getpwnam(argv[++program_params])) != NULL) { user = argv[program_params]; set_user = pw->pw_uid; } else { tmp = strtol(argv[program_params], (char **)NULL, 10); if (tmp < 0 || tmp > uidrange) { print_msg("--user is out of bounds (username or a value between 0 and %lu): %ld\n", uidrange, tmp); exit(-1); } set_user = (uid_t) tmp; } if (verbose) print_msg("UID will be set to %d\n", set_user); do_user = 1; } else { if (strcmp(argv[program_params], "--group") == 0) { if ((gr = (struct group *) getgrnam(argv[++program_params])) != NULL) { set_group = gr->gr_gid; } else { tmp = strtol(argv[program_params], (char **)NULL, 10); if (tmp < 0 || tmp > uidrange) { print_msg("--group is out of bounds (groupname or a value between 0 and %lu): %ld\n", uidrange, tmp); exit(-1); } set_group = (gid_t) tmp; } if (verbose) print_msg("GID will be set to %d\n", set_group); do_group = 1; } else { if (strcmp(argv[program_params], "--init") == 0) { init_program = argv[++program_params]; if (verbose) print_msg("Initial program will be %s\n", init_program); do_init = 1; } else { if (strcmp(argv[program_params], "--fork") == 0) { if (verbose) print_msg("Will fork if everything is fine\n"); do_fork = 1; } else { if (strcmp(argv[program_params], "--cap") == 0) { program_params++; tmp = 0; temp = caps; if (strcmp(argv[program_params], "none") == 0 || strcmp(argv[program_params], "null") == 0) { temp = caps = 0; if (verbose) print_msg("Capabilities all cleared\n"); } else { while((temp == caps) && (strlen(cap_set_names[tmp]) > 0)) { if (strcmp(argv[program_params], cap_set_names[tmp]) == 0) { temp = cap_set_no[tmp]; caps |= (1<<((temp)&31)); if (verbose) print_msg("Capabilities will be set to 0x%0x\n",caps); } tmp++; } if (caps == temp) { print_msg("Unknown capset name: %s\n", argv[program_params]); exit(-1); } } do_caps = 1; } else { if (strcmp(argv[program_params], "--verbose") == 0) { print_msg("I am in verbose mode now\n"); verbose = 1; } else { if (strcmp(argv[program_params], "--quiet") == 0) { quiet = 1; } else { if (strcmp(argv[program_params], "--help") == 0) { help(); } else { print_msg("Unknown parameter: %s\n\n",argv[program_params]); help(); } }}}}}}}} program_params++; } if ((getuid() != geteuid()) || (getgid() != getegid())) print_msg("Warning: euid/egid and uid/gid are different, are we running suid/sgid??\nDo not do that!\n"); /* the first parameter without an -- has to be the program plus options to execute */ if (program_params == argc) { print_msg("No program given to execute.\n\n"); help(); } _argv = argv; _argv += program_params; /* some further insightful deductions */ if (do_user == 0) set_user = geteuid(); if (do_group == 0) set_group = getegid(); if (user == NULL) { if ((pw = (struct passwd *) getpwuid(set_user)) == NULL) { print_msg("Warning: username for uid %u is not found in /etc/passwd.\n", set_user); } else { user = (char *) pw->pw_name; } } if (do_caps && do_user) { print_msg("Warning: --user and --caps will not work together!\n"); } if (do_init) { if (system(init_program)) { print_msg("Error executing init program %s\n", init_program); exit(1); } else { if (verbose) print_msg("Init program %s run successfully\n", init_program); } } if (do_chroot) { if (chroot(chroot_path)) { print_msg("Error chrooting to %s\n", chroot_path); exit(1); } if (chdir("/") != 0) { print_msg("chdir to / in chroot failed\n"); exit(1); } if (verbose) print_msg("Chrooted sucessfully to %s\n", chroot_path); } if (do_user || do_group || do_caps) { // if (user != NULL) { if (setgroups(0, NULL)) { print_msg("Error removing supplementary groups via setgroups().\n"); exit(1); } else { if (verbose) print_msg("setgroups() successfully set\n"); } // } else { // print_msg("Can not do setgroups() because username for uid %u is not found in /etc/passwd.\n", set_user); // } } if (do_group) { if (setgid(set_group)) { print_msg("Error setting gid to %u\n", set_group); exit(1); } else { if (verbose) print_msg("GID successfully set to %d\n",set_group); } } if (do_caps) my_caps(); if (do_user) { if (setuid(set_user)) { print_msg("Error setting uid to %u\n", set_user); exit(1); } else { if (verbose) print_msg("UID successfully set to %d\n", set_user); } } for (fd = 3; fd <= 1023; fd++) // set close_on_exec on all open fd´s > 2 (void) fcntl(fd, F_SETFD, FD_CLOEXEC); if (verbose) print_msg("FD_CLOEXEC successfully set on all filedescriptors > 2\n"); if (setrlimit(RLIMIT_CORE, &limit) < 0) print_msg("Could not set core size limit to 0 bytes\n"); else if (verbose) print_msg("core size limit successfully set to 0 bytes\n"); if (quiet == 0) { char *buf = malloc(LOGBUF); char *buff = malloc(LOGBUF); int i = 1; strcpy(buf, "Running as: "); if (strlen(argv[0]) > 4095) { print_msg("Illegal length of argv[0]: %d\n", strlen(argv[0])); exit(1); } strcat(buf, argv[0]); // argv[0] can not be bigger than 4096 while (i < argc) { snprintf(buff, LOGBUF, "%s %s", buf, argv[i++]); strcpy(buf, buff); } syslog(LOG_NOTICE, "%s\n", buf); free(buf); free(buff); } if (stat(_argv[0], &st) < 0) { if (do_chroot || _argv[0][0] == '/' || _argv[0][0] == '.') print_msg("Warning: Can not find %s\n", _argv[0]); } else { if (do_fork) { do_fork = fork(); if (do_fork < 0) { print_msg("Could not fork\n"); exit(1); } if (do_fork > 0) return 0; // this is the parent process if (verbose) print_msg("Successfully forked\n"); } } execvp(_argv[0], _argv); if (do_chroot == 0) print_msg("Could not execute %s\n", _argv[0]); else { if (stat(_argv[0], &st) < 0) print_msg("Could not find file: %s\n", _argv[0]); else if (access(_argv[0], X_OK) < 0) print_msg("Execute bit missing, or no permissions to execute %s\n", _argv[0]); else print_msg("Could not properly execute %s - the chroot environment might not be set up correctly: Create the directories /etc and /lib in chroot_dir and run \"ldd %s\" to see which libraries are needed. Copy these to chroot_dir/lib, then chdir to chroot_dir and execute \"ldconfig -X -r .\"\n", _argv[0], _argv[0]); } return 1; } compartment-1.1.0/README0100644000175000017500000003001107246173657013065 0ustar jfsjfs SuSE secure compartment v1.1 (c) 2000 by Marc Heuse INTRODUCTION ------------ This tool was written to securely run untrusted services and programs. It has all options needed to make the impact of an successful hacker attempt on a service as small as possible. INSTALLING ---------- Type "make install" (this will also compile the program) The binary "compartment" will be installed in /usr/sbin, the textfiles README, CHANGES and TODO to /usr/doc/packages/compartment and the man page compartment.1 to /usr/share/man/man1 IMPORTANT TIPS -------------- This section should be below "how to run" however I fear that *you* might stop reading before getting to this section. So listen up and take notice of these important tips: * always try to use the --chroot option. This might take some work and pain, however it is the best way to secure a service. * always try to use the --user AND --group option if no root priviliges are needed. * if root privs are needed, use capabilities * if you use capabilities: -> always chroot ! -> chown all files/dirs to another user than root if thats possible - otherwise just those which really need to be writable by the program * there are several capsets which should not be used, because then it might be easy to become the real superuser of the machine: CAP_NET_ADMIN, CAP_NET_RAW, CAP_FOWNER, CAP_SYS_MODULE, CAP_SYS_CHROOT, CAP_SYS_PTRACE, CAP_SYS_ADMIN And several others can be used to denial-of-service the machine! Once again: always chroot when using capabilities! Remember these tips! If you have got problems getting a program running chrooted or with capsets, try to "strace -v -s 250 -ff -F -qix -o problem /usr/sbin/compartment ...." the problem - check for system calls which failed and try to figure out how to get it working. And please: if you get a program/daemon running with chrooting and/or capsets, please send me an email (marc@suse.de) with all information. Far below is a section called "how to get the service xxx running" which needs *your* input. HOW TO RUN ---------- Run "compartment" without any argument to see the options: SuSE secure compartment v1.0 Marc Heuse http://www.suse.de/~marc Syntax: compartment [options] /full/path/to/program Options: --chroot path chroot to path --user user change uid to this user --group group change gid to this group --init program execute this program/script before doing anything --cap capset set capset name. This option can be used several times. --verbose be verbose --quiet do no logging (to syslog) --fork fork (if everything is fine) Hints: always try to chroot; use --user&group if possible; chroot and chown all files to another user than root if you use capabilties. Read the README file! Known capset names: none CAP_CHOWN CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_FSETID CAP_FS_MASK CAP_KILL CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_LINUX_IMMUTABLE CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_ADMIN CAP_NET_RAW CAP_IPC_LOCK CAP_IPC_OWNER CAP_SYS_MODULE CAP_SYS_RAWIO CAP_SYS_CHROOT CAP_SYS_PTRACE CAP_SYS_PACCT CAP_SYS_ADMIN CAP_SYS_BOOT CAP_SYS_NICE CAP_SYS_RESOURCE CAP_SYS_TIME CAP_SYS_TTY_CONFIG (see linux/capability.h for more information) THE OPTIONS ----------- --chroot is the path to chroot to. Notice that you need all needed libraries in (chroot_path)/lib! (run "ldd program_to_run" to see which libraries are needed.) After you created (chroot_path)/lib and copied the libraries there, create (chroot_path)/etc as well and run "ldconfig -X -r chroot_path". Finally copy all needed program files and create /dev devices if necessary. --user sets the userid which will run the program --group sets the groupid which will run the program if either --user or --group is used, setgroups(0, NULL) will be called, to drop all suplementary groups. --init is a program which will be initially run. You can use this e.g. to create a fresh chroot environment before launching the real programs chrooted. --verbose tells you everything "compartment" does --quiet tells you nothing (doesn't log anything to syslog) --fork forks before executing the program, however checks that everything is right beforehand. That means that you have to specify the full pathname of the program to execute! --cap let's you define one or more capsets. e.g. if you set --cap CAP_NET_BIND_SERVICE the process will be able to bind to ports < 1024, but not any other thing, root can normally do! NOTE: You can't use --cap and --user together (the 2.2 and most 2.3 kernels do not support this. This is fixed in the late 2.3 series. SuSE compartment will support this when 2.4 gets outs) If you want to know what capsets are available and what they do, check out (/usr/include/)linux/capability.h A WORD ABOUT /CHROOT -------------------- If you are chrooting a service, you have to create a secure chroot compartment. Most important: /chroot should be on a partition of it's own, to prevent local hardlink attacks. Furthermore, you can use ext2 mount security options: nosuid - to prevent suid file creation from a compromised service noatime - for performance reasons About other mount options: ro (readonly) - is great but usually won't work with your services because most would need to write to a file somewhere. nodev - also great and important, but then you can't log to syslog via the log device (e.g. /chroot/dev/log) noexec - can't be used because compartment has to execute the service After you have setup this, create the following directories within /chroot: bin and dev. Both should have 0755 permissions. Ensure that syslog creates a second log device in /chroot/dev. This is done by starting syslogd with the option "-a /chroot/dev/log". If you have SuSE Linux, this can easily be done by setting the SYSLOGD_PARAMS variable in /etc/rc.config with this parameter. If you use the example scripts init_bind8 and/or init_squid2, then put the init_* file(s) into the /chroot/bin directory with 0700 permissions. HOW TO GET THE SERVICE/PROGRAM XXX RUNNING ------------------------------------------ This section needs input. Have you successfully used chrooting etc. on apache, wu-ftpd, named etc. etc.? Send the information to marc@suse.de and I will put it here so everyone can enhance the security on his system. BIND8 - named: See the file init_bind8 which is appended to this README on how this could be implemented with bind8. You have to create /chroot and put init_bind8 into /chroot/bin/ and afterwards exchange the named execution in the rc startup scripts (it's /etc/rc.d/named on SuSE Linux, where the line is startproc /usr/sbin/named -u named -g named ) with: /usr/sbin/compartment --chroot /chroot/bind8 --init /chroot/bin/init_bind8 \ --cap CAP_NET_BIND_SERVICE --fork --group root /usr/sbin/named Please note that if you want to change the zones or config files, that you have to change the normal files within /etc and /var/named and NOT the ones in /chroot! You have to restart the service for the changes to take effect. You also have to change the syslogd startup: add the following commandline option: "-a /chroot/dev/log" which adds another unix domain socket for logging. SQUID2: See the file init_squid2 which is appended to this README (after init_bind8) on how this couldbe implemented with squid2. You have to create /chroot and put init_squid2 into /chroot/bin/ and afterwards exchange the squid execution in the rc startup scripts (it's /etc/rc.d/squid on SuSE Linux, where the lines are: /usr/sbin/squid -z and startproc -l /var/squid/squid.out /usr/sbin/squid -sYD ) with: /usr/sbin/compartment --chroot /chroot/squid2 --init /chroot/bin/init_squid2 \ --user squid --fork /usr/sbin/squid -sYD You also have to change the syslogd startup: add the following commandline option: "-a /chroot/dev/log" which adds another unix domain socket for logging. The logfiles of squid2 will be placed in /chroot/log ! LOGGING ------- Everything is logged to stderr and syslog (unless --quiet is used). Also the whole argument list is printed to syslog. Everything is logged with DAEMON.NOTICE QUESTIONS / BUGS ---------------- Send an email to marc@suse.de. A beta of the package can always be found at http://www.suse.de/~marc ----- EXAMPLE SCRIPT : init_bind8 ----- #!/bin/sh # # auto_chroot_script for named from the bind8 package and SuSEcompartment v0.8 # (c) 2000 by Marc Heuse # # # compartment call (where this script is /chroot/bin/init_bind8): # /usr/sbin/compartment --chroot /chroot/bind8 --init /chroot/bin/init_bind8 \ # --cap CAP_NET_BIND_SERVICE --fork --group root /usr/sbin/named # CHROOT_DIR="/chroot/bind8" # chroot directory OWNER="bin.bin" # user.group owner of all chrooted files DEV_LOG="/chroot/dev/log" # add "-a /chroot/dev/log" to the syslogd start DIR_LIST_755="/var /etc /lib /bin /dev" DIR_LIST_1775="/tmp /var/run /var/named/slave" FILE_LIST="/usr/sbin/named /etc/localtime /etc/named.conf /etc/protocols \ /etc/resolv.conf /etc/services /var/named/* /dev/null" CHOWN_ROOT="" CHGRP_ROOT="$DIR_LIST_1775" # start umask 022 export PATH="/usr/sbin:/sbin:/usr/bin:/bin" rm -rf "$CHROOT_DIR" mkdir -p -m 755 "$CHROOT_DIR" || exit 1 cd "$CHROOT_DIR" for i in $DIR_LIST_755; do mkdir -p -m 755 "$CHROOT_DIR/$i" done for i in $DIR_LIST_1775; do mkdir -p -m 1775 "$CHROOT_DIR/$i" done ln -s . usr ln -s bin sbin ln -s tmp var/tmp for i in $FILE_LIST; do LIB=`ldd $i 2> /dev/null |grep -v "not a "| awk '{print$ 3}'` cp -a $i "$CHROOT_DIR/$i" for j in $LIB; do test -e "$CHROOT_DIR/$j" || cp -p "$j" "$CHROOT_DIR/lib" done done ldconfig -r "$CHROOT_DIR" 2> /dev/null chown -R $OWNER "$CHROOT_DIR" for i in $CHOWN_ROOT; do chown root "$CHROOT_DIR/$i" done for i in $CHGRP_ROOT; do chgrp root "$CHROOT_DIR/$i" done test -e "$DEV_LOG" || { echo "Warning: $DEV_LOG not found. Add \"-a $DEV_LOG\" to the syslogd startup." exit 1 } ln "$DEV_LOG" dev/log # end ----- EXAMPLE SCRIPT : init_squid2 ----- #!/bin/sh # # auto_chroot_script for squid from the squid2 package and SuSEcompartment v0.8 # (c) 2000 by Marc Heuse # # # compartment call (where this script is /chroot/bin/init_squid2): # /usr/sbin/compartment --chroot /chroot/squid2 --init /chroot/bin/init_squid2 \ # --user squid --fork /usr/sbin/squid -sYD # CHROOT_DIR="/chroot/squid2" # chroot directory OWNER="bin.bin" # user.group owner of all chrooted files DEV_LOG="/chroot/dev/log" # add "-a /chroot/dev/log" to the syslogd start DIR_LIST_755="/var/squid/logs /var/squid/cache /usr/share/squid /etc /lib \ /bin /dev" DIR_LIST_1775="/tmp" FILE_LIST="/etc/squid.conf /etc/resolv.conf /etc/nsswitch.conf /etc/hosts \ /etc/host.conf /lib/libnss_*.so.2 /etc/localtime /etc/services \ /usr/sbin/squid /usr/sbin/dnsserver /usr/sbin/unlinkd \ /dev/null" CHOWN_SQUID="/var/squid/*" LOGS="access.log cache.log store.log" # in /var/squid/logs # start umask 022 export PATH="/usr/sbin:/sbin:/usr/bin:/bin" test -d /chroot/log || mkdir -m 0755 /chroot/log rm -rf "$CHROOT_DIR" mkdir -p -m 755 "$CHROOT_DIR" || exit 1 cd "$CHROOT_DIR" mkdir -m 755 var ln -s . usr ln -s bin sbin ln -s tmp var/tmp for i in $DIR_LIST_755; do mkdir -p -m 755 "$CHROOT_DIR/$i" done for i in $DIR_LIST_1775; do mkdir -p -m 1775 "$CHROOT_DIR/$i" done for i in $FILE_LIST; do LIB=`ldd $i 2> /dev/null |grep -v "not a "| awk '{print$ 3}'` cp -a $i "$CHROOT_DIR/$i" for j in $LIB; do test -e "$CHROOT_DIR/$j" || cp -p "$j" "$CHROOT_DIR/lib" done done ldconfig -r "$CHROOT_DIR" 2> /dev/null cp -a /usr/share/squid "$CHROOT_DIR"/usr/share chown -R $OWNER "$CHROOT_DIR" for i in $CHOWN_SQUID; do chown squid "$CHROOT_DIR/$i" 2> /dev/null done test -e "$DEV_LOG" || { echo "Warning: $DEV_LOG not found. Add \"-a $DEV_LOG\" to the syslogd startup." exit 1 } ln "$DEV_LOG" dev/log set -o noclobber for i in $LOGS; do test -e /chroot/log/"$i" || { > /chroot/log/$i || exit 1 } chown squid.root /chroot/log/"$i" chmod 640 /chroot/log/"$i" ln /chroot/log/$i var/squid/logs/$i done set +o noclobber /usr/sbin/compartment --chroot "$CHROOT_DIR" --user squid /usr/sbin/squid -z # end compartment-1.1.0/LICENCE0100644000175000017500000004360707033635757013207 0ustar jfsjfs GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundations software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each authors protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyones free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Programs source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w. This is free software, and you are welcome to redistribute it under certain conditions; type `show c for details. The hypothetical commands `show w and `show c should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w and `show c; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. compartment-1.1.0/CHANGES0100644000175000017500000000323107246173617013200 0ustar jfsjfsCHANGES ------- v1.1 25.02.01 (final) -> SuSE 7.1 * Added a #define which is needed for 2.4 kernels v1.0 24.04.00 (final) -> SuSE 7.0 -> I think this tool is perfect now, just some more examples are missing - maybe some people might send some in? See the end of the README for an example for bind8 and squid! * Added some more tips and information to the README * Added squid2 example. See README * --group was not working correctly, fixed. v0.9 16.04.00 (final) * Added a manpage! * Enhanced/beautified the Makefile v0.8 20.02.00 (public beta) * Fixed bug: there was a parsing error with the --cap option * Added --fork option (only forks if full pathname is given) * Added bind8/named example. See README v0.7 13.02.00 (public beta) * Added "null"/"none" option to the -cap option to drop all capbits. * Added runtime autodetection of uid_t size so the 2.4 kernel patch for big uid's is supported. * Disabled the generation of core files from the applications run. v0.6 05.01.00 (public beta) * Removed the initgroups() command and replaced it with setgroups(0, NULL) which will remove *all* supplementary groups. This is called if --user and/or --group is used. * Setting the CLOSE_ON_EXEC Flag on the /dev/log fd, however, glibc should do this. We just want to be sure ... * Added suid/sgid warning * Saving now 14kb memory while running by allocating/freeing memory * added alarm(0) call to turn off a pending SIGALRM timer * Fixed a small bug in the fd0-2 open check * Fixed some typos and improved documentation and program help -> thanks go to Solar Designer for comments & critics ... v0.5 02.01.00 (public beta) * Initial release compartment-1.1.0/TODO0100644000175000017500000000071307246173720012672 0ustar jfsjfs* when the 2.4 kernel gets out, I'll add the special function call where capabilities can be saved over uid switching. This will remove the restriction of not having the ability of --cap and --user together * what would be cool: to be able to let the untrusted program think it is a special date and/or time. I think this is only possible with a kernel module :-( * have you any bugs found or have comments/ideas/critics? TELL ME -> marc@suse.de compartment-1.1.0/IGNORE0100644000175000017500000000013407056060441013077 0ustar jfsjfs# # this is not for you. ignore it. # for i in compartment.c README; do joe $i done delbak compartment-1.1.0/compartment.10100644000175000017500000000543307076334017014620 0ustar jfsjfs.de Sp .if n .sp .if t .sp 0.4 .. .TH COMPARTMENT 1 .SH NAME compartment \- secure program/service wrapper .SH SYNOPSIS .B compartment [--cap CAPSET] [--chroot PATH] [--user USER] [--group GROUP] [--init PROGRAM] [--verbose] [--quiet] [--fork] /full/path/to/program .SH DESCRIPTION The .I SuSE Secure Compartment was designed to allow safe execution of priviliged and/or untrusted executables and services. It has got all features possible included, which can be used to minimize the risk of a trojanized or vulnerable program/service. .SH COMMANDLINE OPTIONS .PP .TP .B \--cap CAPSET sets the defined .I CAPABILITY for the process. See the README file and the section LIMITATIONS for more information and examples. .PP .TP .B \--chroot PATH .I chroots to the PATH defined. It has to be a valid chroot environment. See the README file for more information and examples. .PP .TP .B \--user USER runs the program with uid/euid of USER .PP .TP .B \--group GROUP runs the program with gid/egid of GROUP .PP .TP .B \--init PROGRAM runs PROGRAM before running the untrusted program/service, e.g. to build a chroot environment .PP .TP .B \--verbose prints detailled information what .I compartment does. .PP .TP .B \--quit does not print syslog information about the use of .I compartment .PP .TP .B \--fork forks if everything was set up correctly, mother process will exit. .SH FEATURES .PP .I Linux Capabilities .TP .B supports all Linux capabilites (see /usr/include/linux/capability.h and the README file) .PP .I Chrooting .TP .B supports a chroot setup .PP .I Privileges .TP .B supports running with defined user and/or group privileges .PP .I Setup Scripts .TP .B supports running of initial scripts before running a program/service, e.g. to build a chroot environment. .SH LIMITATIONS Currently the kernel does not allow capabilities on processes which are not running with euid 0. Therefore .I compartment will exit with an error if --user and --cap is used together. .PP Please note that this will change for the 2.4 kernel. .SH BUGS No bugs are currently known .SH AUTHOR .Sp Marc Heuse .I .SH DISTRIBUTION .I compartment is part of the SuSE Linux Distribtution since 7.0 so it can be downloaded as an RPM file from the SuSE FTP servers. It can also be downloaded as a .tar.gz file from .I http://www.suse.de/~marc .SH LICENCE .Sp This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; Version 2. .Sp This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. .SH SEE ALSO .I capset (2), .I chroot (1), .I chroot (2)