3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "pathconfig.h"
26 #include <lib/vserver.h>
27 #include <lib/internal.h>
28 #include <lib_internal/sys_clone.h>
30 #include <sys/socket.h>
36 #include <asm/unistd.h>
42 #include <sys/mount.h>
46 #include <sys/types.h>
55 // FIXME: add configure autodetection and include <selinux.h> directly
56 int rpm_execcon(unsigned int verified,
58 char *const argv[], char *const envp[]);
61 #define ENSC_WRAPPERS_PREFIX "rpm-fake.so: "
62 #define ENSC_WRAPPERS_VSERVER 1
63 #define ENSC_WRAPPERS_UNISTD 1
67 #include "capability-compat.h"
69 #define LIBNAME "rpm-fake.so"
70 #define PLATFORM_FILE "/etc/rpm/platform"
72 #define INIT(FILE,FUNC) FUNC##_func = ((__typeof__(FUNC) *) (xdlsym(FILE, #FUNC)))
73 #define DECLARE(FUNC) static __typeof__(FUNC) * FUNC##_func = 0
77 #define DBG_INIT 0x0001
78 #define DBG_VARIABLES 0x0002
79 #define DBG_RESOLVER 0x0004
80 #define DBG_EXECV 0x0008
81 #define DBG_ENV 0x0010
82 #define DBG_VERBOSE0 0x8000
83 #define DBG_VERBOSE1 (0x4000 | DBG_VERBOSE0)
84 #define DBG_VERBOSE2 (0x2000 | DBG_VERBOSE1)
86 int wrapper_exit_code = 255;
88 static xid_t ctx = VC_NOCTX;
89 static uint32_t caps = ~0;
91 static char const * mnts = 0;
92 static char const * root = 0;
93 static int pw_sock = -1;
94 static int sync_sock = -1;
95 static unsigned int debug_level = 0;
97 static bool is_initialized = false;
99 //DECLARE(rpm_execcon);
106 static void initRPMFake() __attribute__((__constructor__));
107 static void exitRPMFake() __attribute__((__destructor__));
110 isDbgLevel(unsigned int level)
112 return ((debug_level&level)==level);
116 xdlsym(void *handle, const char *symbol)
118 void *res = dlsym(handle, symbol);
120 char const *error = dlerror();
121 Vwrite(2, symbol, strlen(symbol));
123 Vwrite(2, error, strlen(error));
136 "Usage: LD_PRELOAD=" LIBNAME " <executable> <args>*\n\n"
137 LIBNAME " unterstands the following environment variables:\n"
138 " $RPM_FAKE_RESOLVER ... program which does the NSS resolving (defaults\n"
139 " to " RESOLVER_PROG ")\n"
140 " $RPM_FAKE_RESOLVER_UID ... uid of the resolver program\n"
141 " $RPM_FAKE_RESOLVER_GID ... gid of the resolver program\n"
142 " $RPM_FAKE_CTX ... vserver context which shall be used for resolver\n"
144 " $RPM_FAKE_CAP ... linux capability remove-mask for the context\n"
145 " $RPM_FAKE_FLAGS ... vserver flags of the context\n"
146 " $RPM_FAKE_CHROOT ... directory of the chroot environment\n"
147 " $RPM_FAKE_NAMESPACE_MOUNTS\n"
148 " ... colon separated list of directories which will\n"
149 " umounted before scriptlet execution\n\n"
150 " $RPM_FAKE_HELP ... shows this message\n"
151 " $RPM_FAKE_VERSION ... shows the version of this program\n\n"
152 " $RPM_FAKE_DEBUG ... sets the debuglevel bitmask\n\n"
153 "Please report bugs to " PACKAGE_BUGREPORT "\n");
161 LIBNAME " " VERSION " -- wrapper around rpm\n"
162 "This program is part of " PACKAGE_STRING "\n\n"
163 "Copyright (C) 2003 Enrico Scholz\n"
164 VERSION_COPYRIGHT_DISCLAIMER);
171 char *env = getenv("LD_PRELOAD");
174 // the const <-> non-const assignment is not an issue since the following
175 // modifying operations will not be executed in the const-case
176 env = env ? env : "";
177 pos = strstr(env, LIBNAME);
180 char *end_pos = pos + sizeof(LIBNAME);
181 bool is_end = (end_pos[-1]=='\0');
185 start_pos = strrchr(env, ':');
186 if (start_pos==0) start_pos = env;
187 else if (!is_end) ++start_pos;
189 if (is_end) *start_pos = '\0';
190 else memmove(start_pos, end_pos, strlen(end_pos)+1);
194 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
195 WRITE_MSG(2, "env='");
201 if (*env=='\0') unsetenv("LD_PRELOAD");
207 if (isDbgLevel(DBG_ENV)) WRITE_MSG(2, "clearEnv()\n");
209 unsetenv("RPM_FAKE_S_CONTEXT_REV");
210 unsetenv("RPM_FAKE_S_CONTEXT_NR");
211 unsetenv("RPM_FAKE_CTX");
212 unsetenv("RPM_FAKE_FLAGS");
213 unsetenv("RPM_FAKE_CHROOT");
214 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
216 unsetenv("RPM_FAKE_RESOLVER_GID");
217 unsetenv("RPM_FAKE_RESOLVER_UID");
218 unsetenv("RPM_FAKE_RESOLVER");
219 unsetenv("RPM_FAKE_PWSOCKET");
221 unsetenv("RPM_FAKE_DEBUG");
227 getDefaultEnv(char const *key, int dflt)
229 char *env = getenv(key);
232 if (env==0 || env[0]=='\0') res = dflt;
233 else res = atoi(env);
238 /// \returns true iff we are in ctx after leaving this function
240 setupContext(xid_t xid, char const **xid_str)
244 if (vc_isSupported(vcFEATURE_MIGRATE)) {
247 if ((xid==VC_DYNAMIC_XID || !vc_is_dynamic_xid(xid)) &&
248 (rc=vc_ctx_create(xid, NULL))==VC_NOCTX &&
250 perror(ENSC_WRAPPERS_PREFIX "vc_ctx_create()");
255 char buf[sizeof(xid_t)*3 + 128];
257 struct vc_ctx_caps caps;
259 strcpy(buf, "rpm-fake.so #");
260 l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
261 Evc_set_vhi_name(rc, vcVHI_CONTEXT, buf, sizeof("rpm-fake.so #")+l-1);
265 caps.bcaps = ~vc_get_insecurebcaps();
267 Evc_set_ccaps(rc, &caps);
269 // context will be activated later...
276 if (xid==VC_DYNAMIC_XID)
279 char buf[sizeof(xid_t)*3 + 2];
282 l = utilvserver_fmt_uint(buf, xid); buf[l] = '\0';
283 *xid_str = strdup(buf);
286 Ewrite(3, &xid, sizeof xid);
294 char const * sock_name = getenv("RPM_FAKE_PWSOCKET");
297 struct sockaddr_un addr = {
298 .sun_family = AF_UNIX,
301 strncpy(addr.sun_path, sock_name, sizeof(addr.sun_path)-1);
302 addr.sun_path[sizeof(addr.sun_path)-1]='\0';
304 if ((pw_sock=socket(AF_UNIX, SOCK_STREAM, 0))==-1 ||
305 connect(pw_sock, (struct sockaddr *)(&addr), sizeof addr)==-1 ||
306 (flag=fcntl(pw_sock, F_GETFD))==-1 ||
307 fcntl(pw_sock, F_SETFD, flag | FD_CLOEXEC)==-1) {
308 perror(ENSC_WRAPPERS_PREFIX "error while initializing pw-socket");
317 char const * resolver = getenv("RPM_FAKE_RESOLVER");
318 if (resolver==0) resolver=RESOLVER_PROG;
320 if (resolver!=0 && *resolver!='\0') {
327 uid=getenv("RPM_FAKE_RESOLVER_UID");
328 gid=getenv("RPM_FAKE_RESOLVER_GID");
330 if (socketpair(AF_UNIX, SOCK_STREAM, 0, res_sock)==-1 ||
331 pipe(sync_pipe)==-1 ||
332 fcntl(res_sock[0], F_SETFD, FD_CLOEXEC)==-1 ||
333 fcntl(sync_pipe[0], F_SETFD, FD_CLOEXEC)==-1) {
334 perror(ENSC_WRAPPERS_PREFIX "failed to create/initialize resolver-socket or pipe");
340 perror(ENSC_WRAPPERS_PREFIX "fork()");
345 char const *args[20];
346 char const **ptr = args;
347 char const *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
349 char flag_str[ sizeof(flags)*3 + 2];
350 char caps_str[ sizeof(caps)*3 + 2];
355 dup2(res_sock[1], 0);
356 dup2(res_sock[1], 1);
357 if (sync_pipe[1]!=3) {
359 dup2(sync_pipe[1], 3);
363 /* ... *socket[0] are marked as close-on-exec ...*/
365 flag_str[utilvserver_fmt_uint(flag_str, flags)] = '\0';
366 caps_str[utilvserver_fmt_uint(caps_str, caps)] = '\0';
369 *ptr++ = "-F"; *ptr++ = flag_str;
370 *ptr++ = "-C"; *ptr++ = caps_str;
371 if (root) { *ptr++ = "-r"; *ptr++ = "."; }
372 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
373 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
375 if (root) Echdir(root);
377 if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
378 else if (xid_str) { *ptr++ = "-c"; *ptr++ = xid_str; }
381 execve(resolver, (char **)args, (char **)env);
382 perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
390 pw_sock = res_sock[0];
391 sync_sock = sync_pipe[0];
393 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
394 read(sync_sock, &c, 1)!=1 ||
395 write(pw_sock, ".", 1)!=1 ||
396 read(pw_sock, &c, 1)!=1 ||
398 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
402 if (wait4(pid, 0, WNOHANG,0)==-1) {
403 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
414 struct __user_cap_header_struct header;
415 struct __user_cap_data_struct user;
417 header.version = _LINUX_CAPABILITY_VERSION;
420 if (capget(&header, &user)==-1) {
422 exit(wrapper_exit_code);
425 user.effective &= ~(1<<CAP_MKNOD);
426 user.permitted &= ~(1<<CAP_MKNOD);
427 user.inheritable &= ~(1<<CAP_MKNOD);
429 if (capset(&header, &user)==-1) {
431 exit(wrapper_exit_code);
441 if (is_initialized) return;
443 syscall_rev = getDefaultEnv("RPM_FAKE_S_CONTEXT_REV", 0);
444 syscall_nr = getDefaultEnv("RPM_FAKE_S_CONTEXT_NR", 273);
446 #ifdef VC_ENABLE_API_LEGACY
448 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
449 int ipv4_rev, int ipv4_number);
451 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
455 ctx = getDefaultEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
456 caps = getDefaultEnv("RPM_FAKE_CAP", ~0x3404040f);
457 flags = getDefaultEnv("RPM_FAKE_FLAGS", 0);
458 root = getenv("RPM_FAKE_CHROOT");
459 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
460 if (mnts && *mnts) mnts = strdup(mnts);
464 if (isDbgLevel(DBG_VERBOSE1))
465 dprintf(2, "ctx=%u, caps=%016x, flags=%016x,\nroot='%s',\nmnts='%s'\n",
466 ctx, caps, flags, root, mnts);
469 is_initialized = true;
475 //INIT(RTLD_NEXT, rpm_execcon);
476 //INIT(RTLD_NEXT, execv);
477 INIT(RTLD_NEXT, getgrnam);
478 INIT(RTLD_NEXT, getpwnam);
479 INIT(RTLD_NEXT, endpwent);
480 INIT(RTLD_NEXT, endgrent);
486 if (getenv("RPM_FAKE_VERSION")) showVersion();
487 if (getenv("RPM_FAKE_HELP")) showHelp();
489 debug_level = getDefaultEnv("RPM_FAKE_DEBUG", 0);
491 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
493 reduceCapabilities();
499 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
508 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
511 if (read(sync_sock, &c, 1)!=1) { /*...*/ }
512 if (write(pw_sock, "Q", 1)!=1) { /*...*/ }
517 //============ the worker part ===========
521 doPwStringRequest(uint32_t *result, char style, char const *name)
523 uint32_t len = strlen(name);
527 return (TEMP_FAILURE_RETRY(read (sync_sock, &c, 1))==1 &&
528 TEMP_FAILURE_RETRY(write(pw_sock, &style, 1))==1 &&
529 TEMP_FAILURE_RETRY(write(pw_sock, &len, sizeof len))==sizeof(len) &&
530 TEMP_FAILURE_RETRY(write(pw_sock, name, len))==(ssize_t)(len) &&
531 TEMP_FAILURE_RETRY(read (pw_sock, &code, sizeof code))==sizeof(code) &&
532 TEMP_FAILURE_RETRY(read (pw_sock, result, sizeof *result))==sizeof(*result) &&
537 getpwnam(const char * name)
539 if (pw_sock==-1) return getpwnam_func(name);
542 static struct passwd res = {
547 .pw_shell = "/bin/false"
550 res.pw_name = (char *)(name);
551 if (!doPwStringRequest(&id, 'P', name)) return 0;
559 getgrnam(const char * name)
561 if (pw_sock==-1) return getgrnam_func(name);
564 static struct group res = {
569 res.gr_name = (char *)(name);
570 if (!doPwStringRequest(&id, 'G', name)) return 0;
580 if (pw_sock==-1) endgrent_func();
581 TEMP_FAILURE_RETRY(write(pw_sock, "Cg", 2));
587 if (pw_sock==-1) endpwent_func();
588 TEMP_FAILURE_RETRY(write(pw_sock, "Cp", 2));
593 execvWorker(char const *path, char * const argv[], char * const envp[])
597 if (vc_isSupported(vcFEATURE_MIGRATE))
598 res = vc_ctx_migrate(ctx, 0);
600 #ifdef VC_ENABLE_API_COMPAT
601 res = vc_new_s_context(ctx,caps,flags);
603 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
610 res=execve(path, argv, envp);
624 removeNamespaceMountsChild(struct ExecvParams const *params)
626 char buf[strlen(params->mnts)+1], *ptr;
628 strcpy(buf, params->mnts);
629 ptr = strtok(buf, ":");
631 if (umount2(ptr, 0)==-1) {
632 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
633 // chroots are visible only, when chroot is on /dev/root?
635 // For now, ignore any errors, but future versions should handle them.
639 ptr = strtok(0, ":");
642 return execvWorker(params->path, params->argv, params->envp);
646 removeNamespaceMounts(char const *path,
647 char * const argv[], char * const envp[])
649 if (mnts==0) return execvWorker(path, argv, envp);
654 struct ExecvParams params;
661 // the rpmlib signal-handler is still active; use the default one to
662 // make wait4() working...
663 signal(SIGCHLD, SIG_DFL);
666 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
668 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
673 case 0 : _exit(removeNamespaceMountsChild(¶ms));
677 while ((p=wait4(pid, &status, 0,0))==-1 &&
678 (errno==EINTR || errno==EAGAIN)) ;
680 if (p==-1) return -1;
682 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
683 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
691 execv(char const *path, char * const argv[])
693 extern char **environ;
695 if (isDbgLevel(DBG_EXECV)) {
696 WRITE_MSG(2, "execv('");
698 WRITE_MSG(2, "', ...)\n");
701 return removeNamespaceMounts(path, argv, environ);
705 rpm_execcon(unsigned int UNUSED verified,
706 const char *filename,
707 char *const argv[], char *const envp[])
709 if (isDbgLevel(DBG_EXECV)) {
710 WRITE_MSG(2, "rpm_execcon(..., '");
711 WRITE_STR(2, filename);
712 WRITE_MSG(2, "', ...)\n");
715 return removeNamespaceMounts(filename, argv, envp);