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"
32 #include <sys/socket.h>
36 #include <sys/types.h>
40 #include <linux/personality.h>
42 #define ENSC_WRAPPERS_PREFIX "vcontext: "
43 #define ENSC_WRAPPERS_UNISTD 1
44 #define ENSC_WRAPPERS_VSERVER 1
45 #define ENSC_WRAPPERS_FCNTL 1
46 #define ENSC_WRAPPERS_SOCKET 1
47 #define ENSC_WRAPPERS_IOSOCK 1
50 #define CMD_HELP 0x1000
51 #define CMD_VERSION 0x1001
52 #define CMD_XID 0x4000
53 #define CMD_CREATE 0x4001
54 #define CMD_MIGRATE 0x4003
55 #define CMD_INITPID 0x4002
56 #define CMD_DISCONNECT 0x4004
57 #define CMD_UID 0x4005
58 #define CMD_CHROOT 0x4006
59 #define CMD_SILENT 0x4007
60 #define CMD_SYNCSOCK 0x4008
61 #define CMD_SYNCMSG 0x4009
62 #define CMD_MIGRATESELF 0x400a
63 #define CMD_ENDSETUP 0x400b
64 #define CMD_SILENTEXIST 0x400c
65 #define CMD_NAMESPACE 0x400d
66 #define CMD_PERSTYPE 0x400e
67 #define CMD_PERSFLAG 0x400f
68 #define CMD_VLOGIN 0x4010
73 { "help", no_argument, 0, CMD_HELP },
74 { "version", no_argument, 0, CMD_VERSION },
75 { "ctx", required_argument, 0, CMD_XID },
76 { "xid", required_argument, 0, CMD_XID },
77 { "create", no_argument, 0, CMD_CREATE },
78 { "migrate", no_argument, 0, CMD_MIGRATE },
79 { "migrate-self", no_argument, 0, CMD_MIGRATESELF },
80 { "initpid", no_argument, 0, CMD_INITPID },
81 { "endsetup", no_argument, 0, CMD_ENDSETUP },
82 { "disconnect", no_argument, 0, CMD_DISCONNECT },
83 { "silent", no_argument, 0, CMD_SILENT },
84 { "silentexist", no_argument, 0, CMD_SILENTEXIST },
85 { "uid", required_argument, 0, CMD_UID },
86 { "chroot", no_argument, 0, CMD_CHROOT },
87 { "namespace", no_argument, 0, CMD_NAMESPACE },
88 { "syncsock", required_argument, 0, CMD_SYNCSOCK },
89 { "syncmsg", required_argument, 0, CMD_SYNCMSG },
90 { "personality-type", required_argument, 0, CMD_PERSTYPE },
91 { "personality-flags", required_argument, 0, CMD_PERSFLAG },
92 { "vlogin", no_argument, 0, CMD_VLOGIN },
94 { "fakeinit", no_argument, 0, CMD_INITPID }, // compatibility
109 uint_least32_t personality_flags;
110 uint_least32_t personality_type;
115 char const * sync_sock;
116 char const * sync_msg;
119 int wrapper_exit_code = 255;
121 void do_vlogin(int argc, char *argv[], int ind);
124 showHelp(int fd, char const *cmd, int res)
126 WRITE_MSG(fd, "Usage:\n ");
129 " --create [--xid <xid>] <opts>* [--] <program> <args>*\n ");
132 " [(--migrate --xid <xid>)|--migrate-self] <opts>* [--] <program> <args>*\n"
135 " --chroot ... chroot into current directory\n"
136 " --namespace ... execute namespace management operations\n"
137 " --uid <uid> ... change uid\n"
138 " --initpid ... set current process as general process reaper\n"
139 " for ctx (possible for --migrate only)\n"
140 " --endsetup ... clear the setup flag; usefully for migrate only\n"
141 " --disconnect ... start program in background\n"
142 " --personality-type <type>\n"
143 " ... execute <program> in the given execution domain\n"
144 " --personality-flags <flags>+\n"
145 " ... set special flags for the given execution domain\n"
146 " --silent ... be silent\n"
147 " --silentexist ... be silent when context exists already; usefully\n"
148 " for '--create' only\n"
149 " --syncsock <file-name>\n"
150 " ... before executing the program, send a message\n"
151 " to the socket and wait until it closes.\n"
152 " <file-name> must be a SOCK_STREAM unix socket\n"
153 " --syncmsg <message>\n"
154 " ... use <message> as synchronization message; by\n"
155 " default, 'ok' will be used\n"
156 " --vlogin ... enable terminal proxy\n"
158 "'vcontext --create' exits with code 254 iff the context exists already.\n"
160 "Please report bugs to " PACKAGE_BUGREPORT "\n");
169 "vcontext " VERSION " -- manages the creation of security contexts\n"
170 "This program is part of " PACKAGE_STRING "\n\n"
171 "Copyright (C) 2004-2006 Enrico Scholz\n"
172 VERSION_COPYRIGHT_DISCLAIMER);
176 #include "context-sync.hc"
178 static inline ALWAYSINLINE void
179 tellContext(xid_t ctx, bool do_it)
181 char buf[sizeof(xid_t)*3+2];
186 l = utilvserver_fmt_long(buf,ctx);
188 WRITE_MSG(1, "New security context is ");
194 connectExternalSync(char const *filename)
197 struct sockaddr_un addr;
199 if (filename==0) return -1;
201 ENSC_INIT_UNIX_SOCK(addr, filename);
203 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
204 Econnect(fd, &addr, sizeof(addr));
210 setFlags(struct Arguments const *args, xid_t xid)
212 struct vc_ctx_flags flags = { 0,0 };
214 if (args->is_initpid)
215 flags.mask |= VC_VXF_STATE_INIT;
217 if (args->do_endsetup)
218 flags.mask |= VC_VXF_STATE_SETUP;
221 DPRINTF("set_flags: mask=%08llx, flag=%08llx\n", flags.mask, flags.flagword);
222 Evc_set_cflags(xid, &flags);
227 doExternalSync(int fd, char const *msg)
233 if (msg) EsendAll(fd, msg, strlen(msg));
234 Eshutdown(fd, SHUT_WR);
236 if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
237 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unexpected external synchronization event\n");
238 exit(wrapper_exit_code);
244 static inline ALWAYSINLINE int
245 doit(struct Arguments const *args, int argc, char *argv[])
248 pid_t pid = initSync(p, args->do_disconnect);
252 int ext_sync_fd = connectExternalSync(args->sync_sock);
254 doSyncStage0(p, args->do_disconnect);
256 if (args->do_create) {
257 xid = vc_ctx_create(args->xid, NULL);
261 if (!args->is_silentexist)
262 perror(ENSC_WRAPPERS_PREFIX "vc_ctx_create()");
265 perror(ENSC_WRAPPERS_PREFIX "vc_ctx_create()");
266 return wrapper_exit_code;
269 tellContext(xid, args->verbosity>=1);
274 if (args->do_chroot) {
276 if (args->set_namespace) {
277 if (args->do_migrateself) Evc_set_namespace(xid, CLONE_NEWNS|CLONE_FS);
278 else if (args->do_migrate) Evc_enter_namespace(xid, CLONE_NEWNS|CLONE_FS);
284 if (args->do_migrate && !args->do_migrateself)
285 Evc_ctx_migrate(xid, 0);
287 if (args->uid != NULL) {
291 if (!isNumberUnsigned(args->uid, &tmp, false)) {
294 pw = getpwnam(args->uid);
296 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Username '");
297 WRITE_STR(2, args->uid);
298 WRITE_MSG(2, "' does not exist\n");
299 return wrapper_exit_code;
302 Einitgroups(args->uid, pw->pw_gid);
305 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Uid '");
306 WRITE_STR(2, args->uid);
307 WRITE_MSG(2, "' is not a number\n");
308 return wrapper_exit_code;
314 Esetuid((uid_t) uid);
316 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Something went wrong while changing the UID\n");
317 exit(wrapper_exit_code);
321 if (args->personality_type!=VC_BAD_PERSONALITY &&
322 sys_personality(args->personality_type | args->personality_flags)==-1) {
323 perror(ENSC_WRAPPERS_PREFIX "personality()");
324 exit(wrapper_exit_code);
327 doExternalSync(ext_sync_fd, args->sync_msg);
328 doSyncStage1(p, args->do_disconnect);
329 DPRINTF("doit: pid=%u, ppid=%u\n", getpid(), getppid());
330 if (!args->do_vlogin)
331 execvp (argv[optind],argv+optind);
333 do_vlogin(argc, argv, optind);
334 doSyncStage2(p, args->do_disconnect);
336 PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
337 exit(wrapper_exit_code);
340 assert(args->do_disconnect);
342 waitOnSync(pid, p, args->xid!=VC_DYNAMIC_XID && args->do_migrate);
346 static uint_least32_t
347 parsePersonalityType(char const *str)
349 uint_least32_t res = vc_str2personalitytype(str, 0);
350 if (res==VC_BAD_PERSONALITY) {
351 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "bad personality type\n");
352 exit(wrapper_exit_code);
358 static uint_least32_t
359 parsePersonalityFlags(char const *str)
361 struct vc_err_listparser err;
364 if (vc_list2personalityflag(str, 0, &res, &err)==-1) {
365 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "bad personality flag '");
366 Vwrite(2, err.ptr, err.len);
368 exit(wrapper_exit_code);
374 int main (int argc, char *argv[])
376 struct Arguments args = {
379 .do_migrateself = false,
380 .do_disconnect = false,
381 .do_endsetup = false,
384 .is_silentexist = false,
385 .set_namespace = false,
388 .xid = VC_DYNAMIC_XID,
389 .personality_type = VC_BAD_PERSONALITY,
390 .personality_flags = 0,
395 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
399 case CMD_HELP : showHelp(1, argv[0], 0);
400 case CMD_VERSION : showVersion();
401 case CMD_CREATE : args.do_create = true; break;
402 case CMD_MIGRATE : args.do_migrate = true; break;
403 case CMD_DISCONNECT : args.do_disconnect = true; break;
404 case CMD_ENDSETUP : args.do_endsetup = true; break;
405 case CMD_VLOGIN : args.do_vlogin = true; break;
406 case CMD_INITPID : args.is_initpid = true; break;
407 case CMD_CHROOT : args.do_chroot = true; break;
408 case CMD_NAMESPACE : args.set_namespace = true; break;
409 case CMD_SILENTEXIST : args.is_silentexist = true; break;
410 case CMD_SYNCSOCK : args.sync_sock = optarg; break;
411 case CMD_SYNCMSG : args.sync_msg = optarg; break;
412 case CMD_UID : args.uid = optarg; break;
413 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
414 case CMD_SILENT : --args.verbosity; break;
416 args.personality_type = parsePersonalityType(optarg);
419 args.personality_flags |= parsePersonalityFlags(optarg);
421 case CMD_MIGRATESELF :
422 args.do_migrate = true;
423 args.do_migrateself = true;
427 WRITE_MSG(2, "Try '");
428 WRITE_STR(2, argv[0]);
429 WRITE_MSG(2, " --help' for more information.\n");
430 return wrapper_exit_code;
435 signal(SIGCHLD, SIG_DFL);
437 if (args.do_migrateself)
438 args.xid = Evc_get_task_xid(0);
440 if (!args.do_create && !args.do_migrate)
441 WRITE_MSG(2, "Neither '--create' nor '--migrate' specified; try '--help' for more information\n");
442 else if (args.do_create && args.do_migrate)
443 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
444 else if (!args.do_migrate && args.is_initpid)
445 WRITE_MSG(2, "'--initpid' is possible in combination with '--migrate' only\n");
446 else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
447 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
448 else if (optind>=argc)
449 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
451 return doit(&args, argc, argv);
453 return wrapper_exit_code;