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 # define CLONE_NEWNS 0x00020000
70 #define LIBNAME "rpm-fake.so"
71 #define PLATFORM_FILE "/etc/rpm/platform"
73 #define INIT(FILE,FUNC) FUNC##_func = ((__typeof__(FUNC) *) (xdlsym(FILE, #FUNC)))
74 #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_VERBOSE0 0x8000
82 #define DBG_VERBOSE1 (0x4000 | DBG_VERBOSE0)
83 #define DBG_VERBOSE2 (0x2000 | DBG_VERBOSE1)
85 int wrapper_exit_code = 255;
87 static xid_t ctx = VC_NOCTX;
88 static uint32_t caps = ~0;
90 static char const * mnts = 0;
91 static char const * root = 0;
92 static int pw_sock = -1;
93 static int sync_sock = -1;
94 static unsigned int debug_level = 0;
96 static bool is_initialized = false;
98 //DECLARE(rpm_execcon);
105 static void initRPMFake() __attribute__((__constructor__));
106 static void exitRPMFake() __attribute__((__destructor__));
109 isDbgLevel(unsigned int level)
111 return ((debug_level&level)==level);
115 xdlsym(void *handle, const char *symbol)
117 void *res = dlsym(handle, symbol);
119 char const *error = dlerror();
120 write(2, symbol, strlen(symbol));
122 write(2, error, strlen(error));
135 "Usage: LD_PRELOAD=" LIBNAME " <executable> <args>*\n\n"
136 LIBNAME " unterstands the following environment variables:\n"
137 " $RPM_FAKE_RESOLVER ... program which does the NSS resolving (defaults\n"
138 " to " RESOLVER_PROG ")\n"
139 " $RPM_FAKE_RESOLVER_UID ... uid of the resolver program\n"
140 " $RPM_FAKE_RESOLVER_GID ... gid of the resolver program\n"
141 " $RPM_FAKE_CTX ... vserver context which shall be used for resolver\n"
143 " $RPM_FAKE_CAP ... linux capability remove-mask for the context\n"
144 " $RPM_FAKE_FLAGS ... vserver flags of the context\n"
145 " $RPM_FAKE_CHROOT ... directory of the chroot environment\n"
146 " $RPM_FAKE_NAMESPACE_MOUNTS\n"
147 " ... colon separated list of directories which will\n"
148 " umounted before scriptlet execution\n\n"
149 " $RPM_FAKE_HELP ... shows this message\n"
150 " $RPM_FAKE_VERSION ... shows the version of this program\n\n"
151 " $RPM_FAKE_DEBUG ... sets the debuglevel bitmask\n\n"
152 "Please report bugs to " PACKAGE_BUGREPORT "\n");
160 LIBNAME " " VERSION " -- wrapper around rpm\n"
161 "This program is part of " PACKAGE_STRING "\n\n"
162 "Copyright (C) 2003 Enrico Scholz\n"
163 VERSION_COPYRIGHT_DISCLAIMER);
168 getAndClearEnv(char const *key, int dflt)
170 char *env = getenv(key);
173 if (env==0 || env[0]=='\0') res = dflt;
182 /// \returns true iff we are in ctx after leaving this function
184 setupContext(xid_t xid, char const **xid_str)
188 if (vc_isSupported(vcFEATURE_MIGRATE)) {
191 if ((xid==VC_DYNAMIC_XID || !vc_is_dynamic_xid(xid)) &&
192 (rc=vc_ctx_create(xid))==VC_NOCTX &&
194 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
199 char buf[sizeof(xid_t)*3 + 128];
201 struct vc_ctx_caps caps;
203 strcpy(buf, "rpm-fake.so #");
204 l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
205 Evc_set_vhi_name(rc, vcVHI_CONTEXT, buf, sizeof("rpm-fake.so #")+l-1);
209 caps.bcaps = ~vc_get_insecurebcaps();
211 Evc_set_ccaps(rc, &caps);
213 // context will be activated later...
220 if (xid==VC_DYNAMIC_XID)
223 char buf[sizeof(xid_t)*3 + 2];
226 l = utilvserver_fmt_uint(buf, xid); buf[l] = '\0';
227 *xid_str = strdup(buf);
230 Ewrite(3, &xid, sizeof xid);
238 char const * sock_name = getenv("RPM_FAKE_PWSOCKET");
241 struct sockaddr_un addr = {
242 .sun_family = AF_UNIX,
245 strncpy(addr.sun_path, sock_name, sizeof(addr.sun_path)-1);
246 addr.sun_path[sizeof(addr.sun_path)-1]='\0';
248 if ((pw_sock=socket(AF_UNIX, SOCK_STREAM, 0))==-1 ||
249 connect(pw_sock, (struct sockaddr *)(&addr), sizeof addr)==-1 ||
250 (flag=fcntl(pw_sock, F_GETFD))==-1 ||
251 fcntl(pw_sock, F_SETFD, flag | FD_CLOEXEC)==-1) {
252 perror(ENSC_WRAPPERS_PREFIX "error while initializing pw-socket");
256 unsetenv("RPM_FAKE_PWSOCKET");
262 char const * resolver = getenv("RPM_FAKE_RESOLVER");
263 if (resolver==0) resolver=RESOLVER_PROG;
265 if (resolver!=0 && *resolver!='\0') {
272 uid=getenv("RPM_FAKE_RESOLVER_UID");
273 gid=getenv("RPM_FAKE_RESOLVER_GID");
275 if (socketpair(AF_UNIX, SOCK_STREAM, 0, res_sock)==-1 ||
276 pipe(sync_pipe)==-1 ||
277 fcntl(res_sock[0], F_SETFD, FD_CLOEXEC)==-1 ||
278 fcntl(sync_pipe[0], F_SETFD, FD_CLOEXEC)==-1) {
279 perror(ENSC_WRAPPERS_PREFIX "failed to create/initialize resolver-socket or pipe");
285 perror(ENSC_WRAPPERS_PREFIX "fork()");
290 char const *args[20];
291 char const **ptr = args;
292 char const *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
294 char flag_str[ sizeof(flags)*3 + 2];
295 char caps_str[ sizeof(caps)*3 + 2];
298 dup2(res_sock[1], 0);
299 dup2(res_sock[1], 1);
300 if (sync_pipe[1]!=3) {
302 dup2(sync_pipe[1], 3);
306 /* ... *socket[0] are marked as close-on-exec ...*/
308 flag_str[utilvserver_fmt_uint(flag_str, flags)] = '\0';
309 caps_str[utilvserver_fmt_uint(caps_str, caps)] = '\0';
312 *ptr++ = "-F"; *ptr++ = flag_str;
313 *ptr++ = "-C"; *ptr++ = caps_str;
314 if (root) { *ptr++ = "-r"; *ptr++ = root; }
315 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
316 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
318 if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
319 else if (xid_str) { *ptr++ = "-c"; *ptr++ = xid_str; }
322 execve(resolver, (char **)args, (char **)env);
323 perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
331 pw_sock = res_sock[0];
332 sync_sock = sync_pipe[0];
334 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
335 read(sync_sock, &c, 1)!=1 ||
336 write(pw_sock, ".", 1)!=1 ||
337 read(pw_sock, &c, 1)!=1 ||
339 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
343 if (wait4(pid, 0, WNOHANG,0)==-1) {
344 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
349 unsetenv("RPM_FAKE_RESOLVER_GID");
350 unsetenv("RPM_FAKE_RESOLVER_UID");
351 unsetenv("RPM_FAKE_RESOLVER");
362 if (is_initialized) return;
364 syscall_rev = getAndClearEnv("RPM_FAKE_S_CONTEXT_REV", 0);
365 syscall_nr = getAndClearEnv("RPM_FAKE_S_CONTEXT_NR", 273);
367 #ifdef VC_ENABLE_API_LEGACY
369 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
370 int ipv4_rev, int ipv4_number);
372 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
376 ctx = getAndClearEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
377 caps = getAndClearEnv("RPM_FAKE_CAP", ~0x3404040f);
378 flags = getAndClearEnv("RPM_FAKE_FLAGS", 0);
379 root = getenv("RPM_FAKE_CHROOT");
380 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
381 if (mnts && *mnts) mnts = strdup(mnts);
384 unsetenv("RPM_FAKE_CHROOT");
385 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
388 is_initialized = true;
394 //INIT(RTLD_NEXT, rpm_execcon);
395 //INIT(RTLD_NEXT, execv);
396 INIT(RTLD_NEXT, getgrnam);
397 INIT(RTLD_NEXT, getpwnam);
398 INIT(RTLD_NEXT, endpwent);
399 INIT(RTLD_NEXT, endgrent);
405 char *env = getenv("LD_PRELOAD");
408 // the const <-> non-const assignment is not an issue since the following
409 // modifying operations will not be executed in the const-case
410 env = env ? env : "";
411 pos = strstr(env, LIBNAME);
414 char *end_pos = pos + sizeof(LIBNAME);
415 bool is_end = (end_pos[-1]=='\0');
419 start_pos = strrchr(env, ':');
420 if (start_pos==0) start_pos = env;
421 else if (!is_end) ++start_pos;
423 if (is_end) *start_pos = '\0';
424 else memmove(start_pos, end_pos, strlen(end_pos)+1);
428 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
429 WRITE_MSG(2, "env='");
435 if (*env=='\0') unsetenv("LD_PRELOAD");
441 if (getenv("RPM_FAKE_VERSION")) showVersion();
442 if (getenv("RPM_FAKE_HELP")) showHelp();
444 debug_level = getAndClearEnv("RPM_FAKE_DEBUG", 0);
446 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
454 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
463 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
466 read(sync_sock, &c, 1);
467 write(pw_sock, "Q", 1);
472 //============ the worker part ===========
476 doPwStringRequest(uint32_t *result, char style, char const *name)
478 uint32_t len = strlen(name);
483 read(sync_sock, &c, 1);
485 write(pw_sock, &style, 1);
486 write(pw_sock, &len, sizeof len);
487 write(pw_sock, name, len);
488 read (pw_sock, &code, sizeof code);
489 read (pw_sock, result, sizeof *result);
495 getpwnam(const char * name)
497 if (pw_sock==-1) return getpwnam_func(name);
499 static struct passwd res = {
504 .pw_shell = "/bin/false"
507 res.pw_name = (char *)(name);
508 if (!doPwStringRequest(&res.pw_uid, 'P', name)) return 0;
515 getgrnam(const char * name)
517 if (pw_sock==-1) return getgrnam_func(name);
519 static struct group res = {
524 res.gr_name = (char *)(name);
525 if (!doPwStringRequest(&res.gr_gid, 'G', name)) return 0;
534 if (pw_sock==-1) endgrent_func();
540 if (pw_sock==-1) endpwent_func();
545 execvWorker(char const *path, char * const argv[], char * const envp[])
549 if (vc_isSupported(vcFEATURE_MIGRATE))
550 res = vc_ctx_migrate(ctx);
552 #ifdef VC_ENABLE_API_COMPAT
553 res = vc_new_s_context(ctx,caps,flags);
555 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
559 res=execve(path, argv, envp);
573 removeNamespaceMountsChild(struct ExecvParams const *params)
575 char buf[strlen(params->mnts)+1], *ptr;
577 strcpy(buf, params->mnts);
578 ptr = strtok(buf, ":");
580 if (umount2(ptr, 0)==-1) {
581 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
582 // chroots are visible only, when chroot is on /dev/root?
584 // For now, ignore any errors, but future versions should handle them.
588 ptr = strtok(0, ":");
591 return execvWorker(params->path, params->argv, params->envp);
595 removeNamespaceMounts(char const *path,
596 char * const argv[], char * const envp[])
598 if (mnts==0) return execvWorker(path, argv, envp);
603 struct ExecvParams params;
610 // the rpmlib signal-handler is still active; use the default one to
611 // make wait4() working...
612 signal(SIGCHLD, SIG_DFL);
615 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
617 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
622 case 0 : _exit(removeNamespaceMountsChild(¶ms));
626 while ((p=wait4(pid, &status, 0,0))==-1 &&
627 (errno==EINTR || errno==EAGAIN)) ;
629 if (p==-1) return -1;
631 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
632 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
640 execv(char const *path, char * const argv[])
642 extern char **environ;
644 return removeNamespaceMounts(path, argv, environ);
648 rpm_execcon(unsigned int UNUSED verified,
649 const char *filename,
650 char *const argv[], char *const envp[])
652 return removeNamespaceMounts(filename, argv, envp);