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++ = "."; }
315 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
316 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
318 if (root) Echdir(root);
320 if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
321 else if (xid_str) { *ptr++ = "-c"; *ptr++ = xid_str; }
324 execve(resolver, (char **)args, (char **)env);
325 perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
333 pw_sock = res_sock[0];
334 sync_sock = sync_pipe[0];
336 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
337 read(sync_sock, &c, 1)!=1 ||
338 write(pw_sock, ".", 1)!=1 ||
339 read(pw_sock, &c, 1)!=1 ||
341 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
345 if (wait4(pid, 0, WNOHANG,0)==-1) {
346 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
351 unsetenv("RPM_FAKE_RESOLVER_GID");
352 unsetenv("RPM_FAKE_RESOLVER_UID");
353 unsetenv("RPM_FAKE_RESOLVER");
364 if (is_initialized) return;
366 syscall_rev = getAndClearEnv("RPM_FAKE_S_CONTEXT_REV", 0);
367 syscall_nr = getAndClearEnv("RPM_FAKE_S_CONTEXT_NR", 273);
369 #ifdef VC_ENABLE_API_LEGACY
371 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
372 int ipv4_rev, int ipv4_number);
374 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
378 ctx = getAndClearEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
379 caps = getAndClearEnv("RPM_FAKE_CAP", ~0x3404040f);
380 flags = getAndClearEnv("RPM_FAKE_FLAGS", 0);
381 root = getenv("RPM_FAKE_CHROOT");
382 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
383 if (mnts && *mnts) mnts = strdup(mnts);
386 unsetenv("RPM_FAKE_CHROOT");
387 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
390 is_initialized = true;
396 //INIT(RTLD_NEXT, rpm_execcon);
397 //INIT(RTLD_NEXT, execv);
398 INIT(RTLD_NEXT, getgrnam);
399 INIT(RTLD_NEXT, getpwnam);
400 INIT(RTLD_NEXT, endpwent);
401 INIT(RTLD_NEXT, endgrent);
407 char *env = getenv("LD_PRELOAD");
410 // the const <-> non-const assignment is not an issue since the following
411 // modifying operations will not be executed in the const-case
412 env = env ? env : "";
413 pos = strstr(env, LIBNAME);
416 char *end_pos = pos + sizeof(LIBNAME);
417 bool is_end = (end_pos[-1]=='\0');
421 start_pos = strrchr(env, ':');
422 if (start_pos==0) start_pos = env;
423 else if (!is_end) ++start_pos;
425 if (is_end) *start_pos = '\0';
426 else memmove(start_pos, end_pos, strlen(end_pos)+1);
430 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
431 WRITE_MSG(2, "env='");
437 if (*env=='\0') unsetenv("LD_PRELOAD");
443 if (getenv("RPM_FAKE_VERSION")) showVersion();
444 if (getenv("RPM_FAKE_HELP")) showHelp();
446 debug_level = getAndClearEnv("RPM_FAKE_DEBUG", 0);
448 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
456 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
465 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
468 read(sync_sock, &c, 1);
469 write(pw_sock, "Q", 1);
474 //============ the worker part ===========
478 doPwStringRequest(uint32_t *result, char style, char const *name)
480 uint32_t len = strlen(name);
485 read(sync_sock, &c, 1);
487 write(pw_sock, &style, 1);
488 write(pw_sock, &len, sizeof len);
489 write(pw_sock, name, len);
490 read (pw_sock, &code, sizeof code);
491 read (pw_sock, result, sizeof *result);
497 getpwnam(const char * name)
499 if (pw_sock==-1) return getpwnam_func(name);
501 static struct passwd res = {
506 .pw_shell = "/bin/false"
509 res.pw_name = (char *)(name);
510 if (!doPwStringRequest(&res.pw_uid, 'P', name)) return 0;
517 getgrnam(const char * name)
519 if (pw_sock==-1) return getgrnam_func(name);
521 static struct group res = {
526 res.gr_name = (char *)(name);
527 if (!doPwStringRequest(&res.gr_gid, 'G', name)) return 0;
536 if (pw_sock==-1) endgrent_func();
542 if (pw_sock==-1) endpwent_func();
547 execvWorker(char const *path, char * const argv[], char * const envp[])
551 if (vc_isSupported(vcFEATURE_MIGRATE))
552 res = vc_ctx_migrate(ctx);
554 #ifdef VC_ENABLE_API_COMPAT
555 res = vc_new_s_context(ctx,caps,flags);
557 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
561 res=execve(path, argv, envp);
575 removeNamespaceMountsChild(struct ExecvParams const *params)
577 char buf[strlen(params->mnts)+1], *ptr;
579 strcpy(buf, params->mnts);
580 ptr = strtok(buf, ":");
582 if (umount2(ptr, 0)==-1) {
583 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
584 // chroots are visible only, when chroot is on /dev/root?
586 // For now, ignore any errors, but future versions should handle them.
590 ptr = strtok(0, ":");
593 return execvWorker(params->path, params->argv, params->envp);
597 removeNamespaceMounts(char const *path,
598 char * const argv[], char * const envp[])
600 if (mnts==0) return execvWorker(path, argv, envp);
605 struct ExecvParams params;
612 // the rpmlib signal-handler is still active; use the default one to
613 // make wait4() working...
614 signal(SIGCHLD, SIG_DFL);
617 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
619 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
624 case 0 : _exit(removeNamespaceMountsChild(¶ms));
628 while ((p=wait4(pid, &status, 0,0))==-1 &&
629 (errno==EINTR || errno==EAGAIN)) ;
631 if (p==-1) return -1;
633 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
634 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
642 execv(char const *path, char * const argv[])
644 extern char **environ;
646 return removeNamespaceMounts(path, argv, environ);
650 rpm_execcon(unsigned int UNUSED verified,
651 const char *filename,
652 char *const argv[], char *const envp[])
654 return removeNamespaceMounts(filename, argv, envp);