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"
70 # define CLONE_NEWNS 0x00020000
73 #define LIBNAME "rpm-fake.so"
74 #define PLATFORM_FILE "/etc/rpm/platform"
76 #define INIT(FILE,FUNC) FUNC##_func = ((__typeof__(FUNC) *) (xdlsym(FILE, #FUNC)))
77 #define DECLARE(FUNC) static __typeof__(FUNC) * FUNC##_func = 0
81 #define DBG_INIT 0x0001
82 #define DBG_VARIABLES 0x0002
83 #define DBG_RESOLVER 0x0004
84 #define DBG_EXECV 0x0008
85 #define DBG_ENV 0x0010
86 #define DBG_VERBOSE0 0x8000
87 #define DBG_VERBOSE1 (0x4000 | DBG_VERBOSE0)
88 #define DBG_VERBOSE2 (0x2000 | DBG_VERBOSE1)
90 int wrapper_exit_code = 255;
92 static xid_t ctx = VC_NOCTX;
93 static uint32_t caps = ~0;
95 static char const * mnts = 0;
96 static char const * root = 0;
97 static int pw_sock = -1;
98 static int sync_sock = -1;
99 static unsigned int debug_level = 0;
101 static bool is_initialized = false;
103 //DECLARE(rpm_execcon);
110 static void initRPMFake() __attribute__((__constructor__));
111 static void exitRPMFake() __attribute__((__destructor__));
114 isDbgLevel(unsigned int level)
116 return ((debug_level&level)==level);
120 xdlsym(void *handle, const char *symbol)
122 void *res = dlsym(handle, symbol);
124 char const *error = dlerror();
125 Vwrite(2, symbol, strlen(symbol));
127 Vwrite(2, error, strlen(error));
140 "Usage: LD_PRELOAD=" LIBNAME " <executable> <args>*\n\n"
141 LIBNAME " unterstands the following environment variables:\n"
142 " $RPM_FAKE_RESOLVER ... program which does the NSS resolving (defaults\n"
143 " to " RESOLVER_PROG ")\n"
144 " $RPM_FAKE_RESOLVER_UID ... uid of the resolver program\n"
145 " $RPM_FAKE_RESOLVER_GID ... gid of the resolver program\n"
146 " $RPM_FAKE_CTX ... vserver context which shall be used for resolver\n"
148 " $RPM_FAKE_CAP ... linux capability remove-mask for the context\n"
149 " $RPM_FAKE_FLAGS ... vserver flags of the context\n"
150 " $RPM_FAKE_CHROOT ... directory of the chroot environment\n"
151 " $RPM_FAKE_NAMESPACE_MOUNTS\n"
152 " ... colon separated list of directories which will\n"
153 " umounted before scriptlet execution\n\n"
154 " $RPM_FAKE_HELP ... shows this message\n"
155 " $RPM_FAKE_VERSION ... shows the version of this program\n\n"
156 " $RPM_FAKE_DEBUG ... sets the debuglevel bitmask\n\n"
157 "Please report bugs to " PACKAGE_BUGREPORT "\n");
165 LIBNAME " " VERSION " -- wrapper around rpm\n"
166 "This program is part of " PACKAGE_STRING "\n\n"
167 "Copyright (C) 2003 Enrico Scholz\n"
168 VERSION_COPYRIGHT_DISCLAIMER);
175 char *env = getenv("LD_PRELOAD");
178 // the const <-> non-const assignment is not an issue since the following
179 // modifying operations will not be executed in the const-case
180 env = env ? env : "";
181 pos = strstr(env, LIBNAME);
184 char *end_pos = pos + sizeof(LIBNAME);
185 bool is_end = (end_pos[-1]=='\0');
189 start_pos = strrchr(env, ':');
190 if (start_pos==0) start_pos = env;
191 else if (!is_end) ++start_pos;
193 if (is_end) *start_pos = '\0';
194 else memmove(start_pos, end_pos, strlen(end_pos)+1);
198 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
199 WRITE_MSG(2, "env='");
205 if (*env=='\0') unsetenv("LD_PRELOAD");
211 if (isDbgLevel(DBG_ENV)) WRITE_MSG(2, "clearEnv()\n");
213 unsetenv("RPM_FAKE_S_CONTEXT_REV");
214 unsetenv("RPM_FAKE_S_CONTEXT_NR");
215 unsetenv("RPM_FAKE_CTX");
216 unsetenv("RPM_FAKE_FLAGS");
217 unsetenv("RPM_FAKE_CHROOT");
218 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
220 unsetenv("RPM_FAKE_RESOLVER_GID");
221 unsetenv("RPM_FAKE_RESOLVER_UID");
222 unsetenv("RPM_FAKE_RESOLVER");
223 unsetenv("RPM_FAKE_PWSOCKET");
225 unsetenv("RPM_FAKE_DEBUG");
231 getDefaultEnv(char const *key, int dflt)
233 char *env = getenv(key);
236 if (env==0 || env[0]=='\0') res = dflt;
237 else res = atoi(env);
242 /// \returns true iff we are in ctx after leaving this function
244 setupContext(xid_t xid, char const **xid_str)
248 if (vc_isSupported(vcFEATURE_MIGRATE)) {
251 if ((xid==VC_DYNAMIC_XID || !vc_is_dynamic_xid(xid)) &&
252 (rc=vc_ctx_create(xid))==VC_NOCTX &&
254 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
259 char buf[sizeof(xid_t)*3 + 128];
261 struct vc_ctx_caps caps;
263 strcpy(buf, "rpm-fake.so #");
264 l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
265 Evc_set_vhi_name(rc, vcVHI_CONTEXT, buf, sizeof("rpm-fake.so #")+l-1);
269 caps.bcaps = ~vc_get_insecurebcaps();
271 Evc_set_ccaps(rc, &caps);
273 // context will be activated later...
280 if (xid==VC_DYNAMIC_XID)
283 char buf[sizeof(xid_t)*3 + 2];
286 l = utilvserver_fmt_uint(buf, xid); buf[l] = '\0';
287 *xid_str = strdup(buf);
290 Ewrite(3, &xid, sizeof xid);
298 char const * sock_name = getenv("RPM_FAKE_PWSOCKET");
301 struct sockaddr_un addr = {
302 .sun_family = AF_UNIX,
305 strncpy(addr.sun_path, sock_name, sizeof(addr.sun_path)-1);
306 addr.sun_path[sizeof(addr.sun_path)-1]='\0';
308 if ((pw_sock=socket(AF_UNIX, SOCK_STREAM, 0))==-1 ||
309 connect(pw_sock, (struct sockaddr *)(&addr), sizeof addr)==-1 ||
310 (flag=fcntl(pw_sock, F_GETFD))==-1 ||
311 fcntl(pw_sock, F_SETFD, flag | FD_CLOEXEC)==-1) {
312 perror(ENSC_WRAPPERS_PREFIX "error while initializing pw-socket");
321 char const * resolver = getenv("RPM_FAKE_RESOLVER");
322 if (resolver==0) resolver=RESOLVER_PROG;
324 if (resolver!=0 && *resolver!='\0') {
331 uid=getenv("RPM_FAKE_RESOLVER_UID");
332 gid=getenv("RPM_FAKE_RESOLVER_GID");
334 if (socketpair(AF_UNIX, SOCK_STREAM, 0, res_sock)==-1 ||
335 pipe(sync_pipe)==-1 ||
336 fcntl(res_sock[0], F_SETFD, FD_CLOEXEC)==-1 ||
337 fcntl(sync_pipe[0], F_SETFD, FD_CLOEXEC)==-1) {
338 perror(ENSC_WRAPPERS_PREFIX "failed to create/initialize resolver-socket or pipe");
344 perror(ENSC_WRAPPERS_PREFIX "fork()");
349 char const *args[20];
350 char const **ptr = args;
351 char const *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
353 char flag_str[ sizeof(flags)*3 + 2];
354 char caps_str[ sizeof(caps)*3 + 2];
359 dup2(res_sock[1], 0);
360 dup2(res_sock[1], 1);
361 if (sync_pipe[1]!=3) {
363 dup2(sync_pipe[1], 3);
367 /* ... *socket[0] are marked as close-on-exec ...*/
369 flag_str[utilvserver_fmt_uint(flag_str, flags)] = '\0';
370 caps_str[utilvserver_fmt_uint(caps_str, caps)] = '\0';
373 *ptr++ = "-F"; *ptr++ = flag_str;
374 *ptr++ = "-C"; *ptr++ = caps_str;
375 if (root) { *ptr++ = "-r"; *ptr++ = "."; }
376 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
377 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
379 if (root) Echdir(root);
381 if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
382 else if (xid_str) { *ptr++ = "-c"; *ptr++ = xid_str; }
385 execve(resolver, (char **)args, (char **)env);
386 perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
394 pw_sock = res_sock[0];
395 sync_sock = sync_pipe[0];
397 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
398 read(sync_sock, &c, 1)!=1 ||
399 write(pw_sock, ".", 1)!=1 ||
400 read(pw_sock, &c, 1)!=1 ||
402 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
406 if (wait4(pid, 0, WNOHANG,0)==-1) {
407 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
418 struct __user_cap_header_struct header;
419 struct __user_cap_data_struct user;
421 header.version = _LINUX_CAPABILITY_VERSION;
424 if (capget(&header, &user)==-1) {
426 exit(wrapper_exit_code);
429 user.effective &= ~(1<<CAP_MKNOD);
430 user.permitted &= ~(1<<CAP_MKNOD);
431 user.inheritable &= ~(1<<CAP_MKNOD);
433 if (capset(&header, &user)==-1) {
435 exit(wrapper_exit_code);
445 if (is_initialized) return;
447 syscall_rev = getDefaultEnv("RPM_FAKE_S_CONTEXT_REV", 0);
448 syscall_nr = getDefaultEnv("RPM_FAKE_S_CONTEXT_NR", 273);
450 #ifdef VC_ENABLE_API_LEGACY
452 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
453 int ipv4_rev, int ipv4_number);
455 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
459 ctx = getDefaultEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
460 caps = getDefaultEnv("RPM_FAKE_CAP", ~0x3404040f);
461 flags = getDefaultEnv("RPM_FAKE_FLAGS", 0);
462 root = getenv("RPM_FAKE_CHROOT");
463 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
464 if (mnts && *mnts) mnts = strdup(mnts);
468 if (isDbgLevel(DBG_VERBOSE1))
469 dprintf(2, "ctx=%u, caps=%016x, flags=%016x,\nroot='%s',\nmnts='%s'\n",
470 ctx, caps, flags, root, mnts);
473 is_initialized = true;
479 //INIT(RTLD_NEXT, rpm_execcon);
480 //INIT(RTLD_NEXT, execv);
481 INIT(RTLD_NEXT, getgrnam);
482 INIT(RTLD_NEXT, getpwnam);
483 INIT(RTLD_NEXT, endpwent);
484 INIT(RTLD_NEXT, endgrent);
490 if (getenv("RPM_FAKE_VERSION")) showVersion();
491 if (getenv("RPM_FAKE_HELP")) showHelp();
493 debug_level = getDefaultEnv("RPM_FAKE_DEBUG", 0);
495 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
497 reduceCapabilities();
503 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
512 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
515 if (read(sync_sock, &c, 1)!=1) { /*...*/ }
516 if (write(pw_sock, "Q", 1)!=1) { /*...*/ }
521 //============ the worker part ===========
525 doPwStringRequest(uint32_t *result, char style, char const *name)
527 uint32_t len = strlen(name);
531 return (TEMP_FAILURE_RETRY(read (sync_sock, &c, 1))==1 &&
532 TEMP_FAILURE_RETRY(write(pw_sock, &style, 1))==1 &&
533 TEMP_FAILURE_RETRY(write(pw_sock, &len, sizeof len))==sizeof(len) &&
534 TEMP_FAILURE_RETRY(write(pw_sock, name, len))==(ssize_t)(len) &&
535 TEMP_FAILURE_RETRY(read (pw_sock, &code, sizeof code))==sizeof(code) &&
536 TEMP_FAILURE_RETRY(read (pw_sock, result, sizeof *result))==sizeof(*result) &&
541 getpwnam(const char * name)
543 if (pw_sock==-1) return getpwnam_func(name);
546 static struct passwd res = {
551 .pw_shell = "/bin/false"
554 res.pw_name = (char *)(name);
555 if (!doPwStringRequest(&id, 'P', name)) return 0;
563 getgrnam(const char * name)
565 if (pw_sock==-1) return getgrnam_func(name);
568 static struct group res = {
573 res.gr_name = (char *)(name);
574 if (!doPwStringRequest(&id, 'G', name)) return 0;
584 if (pw_sock==-1) endgrent_func();
585 TEMP_FAILURE_RETRY(write(pw_sock, "Cg", 2));
591 if (pw_sock==-1) endpwent_func();
592 TEMP_FAILURE_RETRY(write(pw_sock, "Cp", 2));
597 execvWorker(char const *path, char * const argv[], char * const envp[])
601 if (vc_isSupported(vcFEATURE_MIGRATE))
602 res = vc_ctx_migrate(ctx);
604 #ifdef VC_ENABLE_API_COMPAT
605 res = vc_new_s_context(ctx,caps,flags);
607 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
614 res=execve(path, argv, envp);
628 removeNamespaceMountsChild(struct ExecvParams const *params)
630 char buf[strlen(params->mnts)+1], *ptr;
632 strcpy(buf, params->mnts);
633 ptr = strtok(buf, ":");
635 if (umount2(ptr, 0)==-1) {
636 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
637 // chroots are visible only, when chroot is on /dev/root?
639 // For now, ignore any errors, but future versions should handle them.
643 ptr = strtok(0, ":");
646 return execvWorker(params->path, params->argv, params->envp);
650 removeNamespaceMounts(char const *path,
651 char * const argv[], char * const envp[])
653 if (mnts==0) return execvWorker(path, argv, envp);
658 struct ExecvParams params;
665 // the rpmlib signal-handler is still active; use the default one to
666 // make wait4() working...
667 signal(SIGCHLD, SIG_DFL);
670 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
672 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
677 case 0 : _exit(removeNamespaceMountsChild(¶ms));
681 while ((p=wait4(pid, &status, 0,0))==-1 &&
682 (errno==EINTR || errno==EAGAIN)) ;
684 if (p==-1) return -1;
686 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
687 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
695 execv(char const *path, char * const argv[])
697 extern char **environ;
699 if (isDbgLevel(DBG_EXECV)) {
700 WRITE_MSG(2, "execv('");
702 WRITE_MSG(2, "', ...)\n");
705 return removeNamespaceMounts(path, argv, environ);
709 rpm_execcon(unsigned int UNUSED verified,
710 const char *filename,
711 char *const argv[], char *const envp[])
713 if (isDbgLevel(DBG_EXECV)) {
714 WRITE_MSG(2, "rpm_execcon(..., '");
715 WRITE_STR(2, filename);
716 WRITE_MSG(2, "', ...)\n");
719 return removeNamespaceMounts(filename, argv, envp);