#include <lib/vserver.h>
#include <lib/internal.h>
-#include <lib_internal/sys_unshare.h>
+#include <lib_internal/sys_clone.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
-#include <sched.h>
+
// from selinux.h
// FIXME: add configure autodetection and include <selinux.h> directly
static void
reduceCapabilities()
{
+ int retried = 0;
struct __user_cap_header_struct header;
- struct __user_cap_data_struct user;
+ struct __user_cap_data_struct user[2];
- header.version = _LINUX_CAPABILITY_VERSION;
+ header.version = _LINUX_CAPABILITY_VERSION_2;
header.pid = 0;
- if (capget(&header, &user)==-1) {
+retry:
+ if (capget(&header, user)==-1) {
+ if (!retried &&
+ header.version != _LINUX_CAPABILITY_VERSION_2) {
+ header.version = _LINUX_CAPABILITY_VERSION_1;
+ retried = 1;
+ goto retry;
+ }
perror("capget()");
exit(wrapper_exit_code);
}
- user.effective &= ~(1<<CAP_MKNOD);
- user.permitted &= ~(1<<CAP_MKNOD);
- user.inheritable &= ~(1<<CAP_MKNOD);
+ user[0].effective &= ~(1<<CAP_MKNOD);
+ user[0].permitted &= ~(1<<CAP_MKNOD);
+ user[0].inheritable &= ~(1<<CAP_MKNOD);
- if (capset(&header, &user)==-1) {
+ if (capset(&header, user)==-1) {
perror("capset()");
exit(wrapper_exit_code);
}
return res;
}
+struct ExecvParams
+{
+ char const * path;
+ char * const * argv;
+ char * const * envp;
+ char const * mnts;
+};
+
static int
-removeNamespaceMounts(char const *mnts)
+removeNamespaceMountsChild(struct ExecvParams const *params)
{
- char buf[strlen(mnts)+1], *ptr;
+ char buf[strlen(params->mnts)+1], *ptr;
- strcpy(buf, mnts);
+ strcpy(buf, params->mnts);
ptr = strtok(buf, ":");
while (ptr) {
if (umount2(ptr, 0)==-1) {
ptr = strtok(0, ":");
}
- return 0;
+ return execvWorker(params->path, params->argv, params->envp);
}
static int
-execv_main(char const *path, char * const argv[], char * const envp[])
+removeNamespaceMounts(char const *path,
+ char * const argv[], char * const envp[])
{
- int rc = 0;
+ if (mnts==0) return execvWorker(path, argv, envp);
- if (sys_unshare(CLONE_NEWNS)==-1) {
- perror("unshare()");
- return -1;
- }
+ {
+ int status;
+ pid_t p, pid;
+ struct ExecvParams params;
- if (mnts)
- rc = removeNamespaceMounts(mnts);
+ params.path = path;
+ params.argv = argv;
+ params.envp = envp;
+ params.mnts = mnts;
- if (rc!=0)
- return rc;
-
- return execvWorker(path, argv, envp);
+ // the rpmlib signal-handler is still active; use the default one to
+ // make wait4() working...
+ signal(SIGCHLD, SIG_DFL);
+
+#ifdef NDEBUG
+ pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
+#else
+ pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
+#endif
+
+ switch (pid) {
+ case -1 : return -1;
+ case 0 : _exit(removeNamespaceMountsChild(¶ms));
+ default : break;
+ }
+
+ while ((p=wait4(pid, &status, 0,0))==-1 &&
+ (errno==EINTR || errno==EAGAIN)) ;
+
+ if (p==-1) return -1;
+
+ if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
+ if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
+
+ return -1;
+ }
}
+
int
execv(char const *path, char * const argv[])
{
WRITE_MSG(2, "', ...)\n");
}
- return execv_main(path, argv, environ);
+ return removeNamespaceMounts(path, argv, environ);
}
int
WRITE_MSG(2, "', ...)\n");
}
- return execv_main(filename, argv, envp);
+ return removeNamespaceMounts(filename, argv, envp);
}
int