3 // Copyright (C) 2004-2006 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.
24 #include "lib/internal.h"
25 #include "lib_internal/jail.h"
26 #include "lib_internal/sys_personality.h"
27 #include "lib_internal/sys_unshare.h"
33 #include <sys/socket.h>
37 #include <sys/types.h>
40 #include <sys/mount.h>
42 #include <linux/personality.h>
44 #define ENSC_WRAPPERS_PREFIX "vcontext: "
45 #define ENSC_WRAPPERS_UNISTD 1
46 #define ENSC_WRAPPERS_VSERVER 1
47 #define ENSC_WRAPPERS_FCNTL 1
48 #define ENSC_WRAPPERS_SOCKET 1
49 #define ENSC_WRAPPERS_IOSOCK 1
52 #define CMD_HELP 0x1000
53 #define CMD_VERSION 0x1001
54 #define CMD_XID 0x4000
55 #define CMD_CREATE 0x4001
56 #define CMD_MIGRATE 0x4003
57 #define CMD_INITPID 0x4002
58 #define CMD_DISCONNECT 0x4004
59 #define CMD_UID 0x4005
60 #define CMD_CHROOT 0x4006
61 #define CMD_SILENT 0x4007
62 #define CMD_SYNCSOCK 0x4008
63 #define CMD_SYNCMSG 0x4009
64 #define CMD_MIGRATESELF 0x400a
65 #define CMD_ENDSETUP 0x400b
66 #define CMD_SILENTEXIST 0x400c
67 #define CMD_NAMESPACE 0x400d
68 #define CMD_PERSTYPE 0x400e
69 #define CMD_PERSFLAG 0x400f
70 #define CMD_VLOGIN 0x4010
71 #define CMD_PIVOT_ROOT 0x4011
75 #define MNT_DETACH 0x0002
81 { "help", no_argument, 0, CMD_HELP },
82 { "version", no_argument, 0, CMD_VERSION },
83 { "ctx", required_argument, 0, CMD_XID },
84 { "xid", required_argument, 0, CMD_XID },
85 { "create", no_argument, 0, CMD_CREATE },
86 { "migrate", no_argument, 0, CMD_MIGRATE },
87 { "migrate-self", no_argument, 0, CMD_MIGRATESELF },
88 { "initpid", no_argument, 0, CMD_INITPID },
89 { "endsetup", no_argument, 0, CMD_ENDSETUP },
90 { "disconnect", no_argument, 0, CMD_DISCONNECT },
91 { "silent", no_argument, 0, CMD_SILENT },
92 { "silentexist", no_argument, 0, CMD_SILENTEXIST },
93 { "uid", required_argument, 0, CMD_UID },
94 { "chroot", no_argument, 0, CMD_CHROOT },
95 { "namespace", no_argument, 0, CMD_NAMESPACE },
96 { "syncsock", required_argument, 0, CMD_SYNCSOCK },
97 { "syncmsg", required_argument, 0, CMD_SYNCMSG },
98 { "personality-type", required_argument, 0, CMD_PERSTYPE },
99 { "personality-flags", required_argument, 0, CMD_PERSFLAG },
100 { "vlogin", no_argument, 0, CMD_VLOGIN },
101 { "pivot-root", no_argument, 0, CMD_PIVOT_ROOT },
103 { "fakeinit", no_argument, 0, CMD_INITPID }, // compatibility
118 uint_least32_t personality_flags;
119 uint_least32_t personality_type;
125 char const * sync_sock;
126 char const * sync_msg;
129 int wrapper_exit_code = 255;
131 void do_vlogin(int argc, char *argv[], int ind);
134 showHelp(int fd, char const *cmd, int res)
136 WRITE_MSG(fd, "Usage:\n ");
139 " --create [--xid <xid>] <opts>* [--] <program> <args>*\n ");
142 " [(--migrate --xid <xid>)|--migrate-self] <opts>* [--] <program> <args>*\n"
145 " --chroot ... chroot into current directory\n"
146 " --namespace ... execute namespace management operations\n"
147 " --uid <uid> ... change uid\n"
148 " --initpid ... set current process as general process reaper\n"
149 " for ctx (possible for --migrate only)\n"
150 " --endsetup ... clear the setup flag; usefully for migrate only\n"
151 " --disconnect ... start program in background\n"
152 " --personality-type <type>\n"
153 " ... execute <program> in the given execution domain\n"
154 " --personality-flags <flags>+\n"
155 " ... set special flags for the given execution domain\n"
156 " --silent ... be silent\n"
157 " --silentexist ... be silent when context exists already; usefully\n"
158 " for '--create' only\n"
159 " --syncsock <file-name>\n"
160 " ... before executing the program, send a message\n"
161 " to the socket and wait until it closes.\n"
162 " <file-name> must be a SOCK_STREAM unix socket\n"
163 " --syncmsg <message>\n"
164 " ... use <message> as synchronization message; by\n"
165 " default, 'ok' will be used\n"
166 " --vlogin ... enable terminal proxy\n"
168 "'vcontext --create' exits with code 254 iff the context exists already.\n"
170 "Please report bugs to " PACKAGE_BUGREPORT "\n");
179 "vcontext " VERSION " -- manages the creation of security contexts\n"
180 "This program is part of " PACKAGE_STRING "\n\n"
181 "Copyright (C) 2004-2006 Enrico Scholz\n"
182 VERSION_COPYRIGHT_DISCLAIMER);
186 #include "context-sync.hc"
188 static inline ALWAYSINLINE void
189 tellContext(xid_t ctx, bool do_it)
191 char buf[sizeof(xid_t)*3+2];
196 l = utilvserver_fmt_long(buf,ctx);
198 WRITE_MSG(1, "New security context is ");
204 connectExternalSync(char const *filename)
207 struct sockaddr_un addr;
209 if (filename==0) return -1;
211 ENSC_INIT_UNIX_SOCK(addr, filename);
213 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
214 Econnect(fd, &addr, sizeof(addr));
220 setFlags(struct Arguments const *args, xid_t xid)
222 struct vc_ctx_flags flags = { 0,0 };
224 if (args->is_initpid)
225 flags.mask |= VC_VXF_STATE_INIT;
227 if (args->do_endsetup)
228 flags.mask |= VC_VXF_STATE_SETUP;
231 DPRINTF("set_flags: mask=%08llx, flag=%08llx\n", flags.mask, flags.flagword);
232 Evc_set_cflags(xid, &flags);
237 doExternalSync(int fd, char const *msg)
243 if (msg) EsendAll(fd, msg, strlen(msg));
244 Eshutdown(fd, SHUT_WR);
246 if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
247 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unexpected external synchronization event\n");
248 exit(wrapper_exit_code);
254 static inline ALWAYSINLINE int
255 doit(struct Arguments const *args, int argc, char *argv[])
258 pid_t pid = initSync(p, args->do_disconnect);
262 int ext_sync_fd = connectExternalSync(args->sync_sock);
264 doSyncStage0(p, args->do_disconnect);
266 if (args->do_create) {
267 xid = vc_ctx_create(args->xid, NULL);
271 if (!args->is_silentexist)
272 perror(ENSC_WRAPPERS_PREFIX "vc_ctx_create()");
275 perror(ENSC_WRAPPERS_PREFIX "vc_ctx_create()");
276 return wrapper_exit_code;
279 tellContext(xid, args->verbosity>=1);
284 if (args->do_chroot) {
286 if (args->set_namespace) {
287 if (args->do_migrateself) Evc_set_namespace(xid, CLONE_NEWNS|CLONE_FS, 0);
288 else if (args->do_migrate) Evc_enter_namespace(xid, CLONE_NEWNS|CLONE_FS, 0);
291 else if (args->do_pivot_root) {
292 if (vc_enter_namespace(xid, CLONE_NEWNS | CLONE_FS, 1) == -1) {
293 bool existed = false;
294 if (sys_unshare(CLONE_NEWNS) == -1) {
295 perror(ENSC_WRAPPERS_PREFIX "unshare(NEWNS)");
296 return wrapper_exit_code;
298 if (mkdir("./.oldroot", 0700) == -1) {
302 perror(ENSC_WRAPPERS_PREFIX "mkdir()");
303 return wrapper_exit_code;
306 if (pivot_root(".", "./.oldroot") == -1) {
307 perror(ENSC_WRAPPERS_PREFIX "pivot_root()");
308 return wrapper_exit_code;
310 if (umount2("/.oldroot", MNT_DETACH) == -1) {
311 perror(ENSC_WRAPPERS_PREFIX "umount2()");
312 return wrapper_exit_code;
314 if (!existed && rmdir("/.oldroot") == -1) {
315 perror(ENSC_WRAPPERS_PREFIX "rmdir()");
316 return wrapper_exit_code;
318 Evc_set_namespace(xid, CLONE_NEWNS | CLONE_FS, 1);
324 if (args->do_migrate && !args->do_migrateself)
325 Evc_ctx_migrate(xid, 0);
327 if (args->uid != NULL) {
331 if (!isNumberUnsigned(args->uid, &tmp, false)) {
334 pw = getpwnam(args->uid);
336 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Username '");
337 WRITE_STR(2, args->uid);
338 WRITE_MSG(2, "' does not exist\n");
339 return wrapper_exit_code;
342 Einitgroups(args->uid, pw->pw_gid);
345 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Uid '");
346 WRITE_STR(2, args->uid);
347 WRITE_MSG(2, "' is not a number\n");
348 return wrapper_exit_code;
354 Esetuid((uid_t) uid);
356 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Something went wrong while changing the UID\n");
357 exit(wrapper_exit_code);
361 if (args->personality_type!=VC_BAD_PERSONALITY &&
362 sys_personality(args->personality_type | args->personality_flags)==-1) {
363 perror(ENSC_WRAPPERS_PREFIX "personality()");
364 exit(wrapper_exit_code);
367 doExternalSync(ext_sync_fd, args->sync_msg);
368 doSyncStage1(p, args->do_disconnect);
369 DPRINTF("doit: pid=%u, ppid=%u\n", getpid(), getppid());
370 if (!args->do_vlogin)
371 execvp (argv[optind],argv+optind);
373 do_vlogin(argc, argv, optind);
374 doSyncStage2(p, args->do_disconnect);
376 PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
377 exit(wrapper_exit_code);
380 assert(args->do_disconnect);
382 waitOnSync(pid, p, args->xid!=VC_DYNAMIC_XID && args->do_migrate);
386 static uint_least32_t
387 parsePersonalityType(char const *str)
389 uint_least32_t res = vc_str2personalitytype(str, 0);
390 if (res==VC_BAD_PERSONALITY) {
391 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "bad personality type\n");
392 exit(wrapper_exit_code);
398 static uint_least32_t
399 parsePersonalityFlags(char const *str)
401 struct vc_err_listparser err;
404 if (vc_list2personalityflag(str, 0, &res, &err)==-1) {
405 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "bad personality flag '");
406 Vwrite(2, err.ptr, err.len);
408 exit(wrapper_exit_code);
414 int main (int argc, char *argv[])
416 struct Arguments args = {
419 .do_migrateself = false,
420 .do_disconnect = false,
421 .do_endsetup = false,
424 .is_silentexist = false,
425 .set_namespace = false,
428 .xid = VC_DYNAMIC_XID,
429 .personality_type = VC_BAD_PERSONALITY,
430 .personality_flags = 0,
435 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
439 case CMD_HELP : showHelp(1, argv[0], 0);
440 case CMD_VERSION : showVersion();
441 case CMD_CREATE : args.do_create = true; break;
442 case CMD_MIGRATE : args.do_migrate = true; break;
443 case CMD_DISCONNECT : args.do_disconnect = true; break;
444 case CMD_ENDSETUP : args.do_endsetup = true; break;
445 case CMD_VLOGIN : args.do_vlogin = true; break;
446 case CMD_INITPID : args.is_initpid = true; break;
447 case CMD_CHROOT : args.do_chroot = true; break;
448 case CMD_PIVOT_ROOT : args.do_pivot_root = true; break;
449 case CMD_NAMESPACE : args.set_namespace = true; break;
450 case CMD_SILENTEXIST : args.is_silentexist = true; break;
451 case CMD_SYNCSOCK : args.sync_sock = optarg; break;
452 case CMD_SYNCMSG : args.sync_msg = optarg; break;
453 case CMD_UID : args.uid = optarg; break;
454 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
455 case CMD_SILENT : --args.verbosity; break;
457 args.personality_type = parsePersonalityType(optarg);
460 args.personality_flags |= parsePersonalityFlags(optarg);
462 case CMD_MIGRATESELF :
463 args.do_migrate = true;
464 args.do_migrateself = true;
468 WRITE_MSG(2, "Try '");
469 WRITE_STR(2, argv[0]);
470 WRITE_MSG(2, " --help' for more information.\n");
471 return wrapper_exit_code;
476 signal(SIGCHLD, SIG_DFL);
478 if (args.do_migrateself)
479 args.xid = Evc_get_task_xid(0);
481 if (!args.do_create && !args.do_migrate)
482 WRITE_MSG(2, "Neither '--create' nor '--migrate' specified; try '--help' for more information\n");
483 else if (args.do_create && args.do_migrate)
484 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
485 else if (!args.do_migrate && args.is_initpid)
486 WRITE_MSG(2, "'--initpid' is possible in combination with '--migrate' only\n");
487 else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
488 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
489 else if (optind>=argc)
490 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
492 return doit(&args, argc, argv);
494 return wrapper_exit_code;