3 // Copyright (C) 2004 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>
37 #include <linux/personality.h>
39 #define ENSC_WRAPPERS_PREFIX "vcontext: "
40 #define ENSC_WRAPPERS_UNISTD 1
41 #define ENSC_WRAPPERS_VSERVER 1
42 #define ENSC_WRAPPERS_FCNTL 1
43 #define ENSC_WRAPPERS_SOCKET 1
44 #define ENSC_WRAPPERS_IOSOCK 1
47 #define CMD_HELP 0x1000
48 #define CMD_VERSION 0x1001
49 #define CMD_XID 0x4000
50 #define CMD_CREATE 0x4001
51 #define CMD_MIGRATE 0x4003
52 #define CMD_INITPID 0x4002
53 #define CMD_DISCONNECT 0x4004
54 #define CMD_UID 0x4005
55 #define CMD_CHROOT 0x4006
56 #define CMD_SILENT 0x4007
57 #define CMD_SYNCSOCK 0x4008
58 #define CMD_SYNCMSG 0x4009
59 #define CMD_MIGRATESELF 0x400a
60 #define CMD_ENDSETUP 0x400b
61 #define CMD_SILENTEXIST 0x400c
62 #define CMD_NAMESPACE 0x400d
63 #define CMD_PERSTYPE 0x400e
64 #define CMD_PERSFLAG 0x400f
69 { "help", no_argument, 0, CMD_HELP },
70 { "version", no_argument, 0, CMD_VERSION },
71 { "ctx", required_argument, 0, CMD_XID },
72 { "xid", required_argument, 0, CMD_XID },
73 { "create", no_argument, 0, CMD_CREATE },
74 { "migrate", no_argument, 0, CMD_MIGRATE },
75 { "migrate-self", no_argument, 0, CMD_MIGRATESELF },
76 { "initpid", no_argument, 0, CMD_INITPID },
77 { "endsetup", no_argument, 0, CMD_ENDSETUP },
78 { "disconnect", no_argument, 0, CMD_DISCONNECT },
79 { "silent", no_argument, 0, CMD_SILENT },
80 { "silentexist", no_argument, 0, CMD_SILENTEXIST },
81 { "uid", required_argument, 0, CMD_UID },
82 { "chroot", no_argument, 0, CMD_CHROOT },
83 { "namespace", no_argument, 0, CMD_NAMESPACE },
84 { "syncsock", required_argument, 0, CMD_SYNCSOCK },
85 { "syncmsg", required_argument, 0, CMD_SYNCMSG },
86 { "personality-type", required_argument, 0, CMD_PERSTYPE },
87 { "personality-flags", required_argument, 0, CMD_PERSFLAG },
89 { "fakeinit", no_argument, 0, CMD_INITPID }, // compatibility
103 uint_least32_t personality_flags;
104 uint_least32_t personality_type;
109 char const * sync_sock;
110 char const * sync_msg;
113 int wrapper_exit_code = 255;
116 showHelp(int fd, char const *cmd, int res)
118 WRITE_MSG(fd, "Usage:\n ");
121 " --create [--xid <xid>] <opts>* [--] <program> <args>*\n ");
124 " [(--migrate --xid <xid>)|--migrate-self] <opts>* [--] <program> <args>*\n"
127 " --chroot ... chroot into current directory\n"
128 " --namespace ... execute namespace management operations\n"
129 " --uid <uid> ... change uid\n"
130 " --initpid ... set current process as general process reaper\n"
131 " for ctx (possible for --migrate only)\n"
132 " --endsetup ... clear the setup flag; usefully for migrate only\n"
133 " --disconnect ... start program in background\n"
134 " --personality-type <type>\n"
135 " ... execute <program> in the given execution domain\n"
136 " --personality-flags <flags>+\n"
137 " ... set special flags for the given execution domain\n"
138 " --silent ... be silent\n"
139 " --silentexist ... be silent when context exists already; usefully\n"
140 " for '--create' only\n"
141 " --syncsock <file-name>\n"
142 " ... before executing the program, send a message\n"
143 " to the socket and wait until it closes.\n"
144 " <file-name> must be a SOCK_STREAM unix socket\n"
145 " --syncmsg <message>\n"
146 " ... use <message> as synchronization message; by\n"
147 " default, 'ok' will be used\n"
149 "'vcontext --create' exits with code 254 iff the context exists already.\n"
151 "Please report bugs to " PACKAGE_BUGREPORT "\n");
160 "vcontext " VERSION " -- manages the creation of security contexts\n"
161 "This program is part of " PACKAGE_STRING "\n\n"
162 "Copyright (C) 2004 Enrico Scholz\n"
163 VERSION_COPYRIGHT_DISCLAIMER);
167 #include "context-sync.hc"
169 static inline ALWAYSINLINE void
170 tellContext(xid_t ctx, bool do_it)
172 char buf[sizeof(xid_t)*3+2];
177 l = utilvserver_fmt_long(buf,ctx);
179 WRITE_MSG(1, "New security context is ");
185 connectExternalSync(char const *filename)
188 struct sockaddr_un addr;
190 if (filename==0) return -1;
192 ENSC_INIT_UNIX_SOCK(addr, filename);
194 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
195 Econnect(fd, &addr, sizeof(addr));
201 setFlags(struct Arguments const *args, xid_t xid)
203 struct vc_ctx_flags flags = { 0,0 };
205 if (args->is_initpid)
206 flags.mask |= VC_VXF_STATE_INIT;
208 if (args->do_endsetup)
209 flags.mask |= VC_VXF_STATE_SETUP;
212 DPRINTF("set_flags: mask=%08llx, flag=%08llx\n", flags.mask, flags.flagword);
213 Evc_set_cflags(xid, &flags);
218 doExternalSync(int fd, char const *msg)
224 if (msg) EsendAll(fd, msg, strlen(msg));
225 Eshutdown(fd, SHUT_WR);
227 if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
228 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unexpected external synchronization event\n");
229 exit(wrapper_exit_code);
235 static inline ALWAYSINLINE int
236 doit(struct Arguments const *args, char *argv[])
239 pid_t pid = initSync(p, args->do_disconnect);
243 int ext_sync_fd = connectExternalSync(args->sync_sock);
245 doSyncStage0(p, args->do_disconnect);
247 if (args->do_create) {
248 xid = vc_ctx_create(args->xid);
252 if (!args->is_silentexist)
253 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
256 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
257 return wrapper_exit_code;
260 tellContext(xid, args->verbosity>=1);
265 if (args->do_chroot) {
267 if (args->set_namespace) {
268 if (args->do_migrateself) Evc_set_namespace();
269 else if (args->do_migrate) Evc_enter_namespace(xid);
275 if (args->do_migrate && !args->do_migrateself)
276 Evc_ctx_migrate(xid);
278 if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
280 if (getuid()!=args->uid) {
281 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Something went wrong while changing the UID\n");
282 exit(wrapper_exit_code);
286 if (args->personality_type!=VC_BAD_PERSONALITY &&
287 sys_personality(args->personality_type | args->personality_flags)==-1) {
288 perror(ENSC_WRAPPERS_PREFIX "personality()");
289 exit(wrapper_exit_code);
292 doExternalSync(ext_sync_fd, args->sync_msg);
293 doSyncStage1(p, args->do_disconnect);
294 DPRINTF("doit: pid=%u, ppid=%u\n", getpid(), getppid());
295 execvp (argv[optind],argv+optind);
296 doSyncStage2(p, args->do_disconnect);
298 PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
299 exit(wrapper_exit_code);
302 assert(args->do_disconnect);
304 waitOnSync(pid, p, args->xid!=VC_DYNAMIC_XID && args->do_migrate);
308 static uint_least32_t
309 parsePersonalityType(char const *str)
311 uint_least32_t res = vc_str2personalitytype(str, 0);
312 if (res==VC_BAD_PERSONALITY) {
313 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "bad personality type\n");
314 exit(wrapper_exit_code);
320 static uint_least32_t
321 parsePersonalityFlags(char const *str)
323 struct vc_err_listparser err;
326 if (vc_list2personalityflag(str, 0, &res, &err)==-1) {
327 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "bad personality flag '");
328 Vwrite(2, err.ptr, err.len);
330 exit(wrapper_exit_code);
336 int main (int argc, char *argv[])
338 struct Arguments args = {
341 .do_migrateself = false,
342 .do_disconnect = false,
343 .do_endsetup = false,
345 .is_silentexist = false,
346 .set_namespace = false,
349 .xid = VC_DYNAMIC_XID,
350 .personality_type = VC_BAD_PERSONALITY,
351 .personality_flags = 0,
356 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
360 case CMD_HELP : showHelp(1, argv[0], 0);
361 case CMD_VERSION : showVersion();
362 case CMD_CREATE : args.do_create = true; break;
363 case CMD_MIGRATE : args.do_migrate = true; break;
364 case CMD_DISCONNECT : args.do_disconnect = true; break;
365 case CMD_ENDSETUP : args.do_endsetup = true; break;
366 case CMD_INITPID : args.is_initpid = true; break;
367 case CMD_CHROOT : args.do_chroot = true; break;
368 case CMD_NAMESPACE : args.set_namespace = true; break;
369 case CMD_SILENTEXIST : args.is_silentexist = true; break;
370 case CMD_SYNCSOCK : args.sync_sock = optarg; break;
371 case CMD_SYNCMSG : args.sync_msg = optarg; break;
372 case CMD_UID : args.uid = atol(optarg); break;
373 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
374 case CMD_SILENT : --args.verbosity; break;
376 args.personality_type = parsePersonalityType(optarg);
379 args.personality_flags |= parsePersonalityFlags(optarg);
381 case CMD_MIGRATESELF :
382 args.do_migrate = true;
383 args.do_migrateself = true;
387 WRITE_MSG(2, "Try '");
388 WRITE_STR(2, argv[0]);
389 WRITE_MSG(2, " --help\" for more information.\n");
390 return wrapper_exit_code;
395 signal(SIGCHLD, SIG_DFL);
397 if (args.do_migrateself)
398 args.xid = Evc_get_task_xid(0);
400 if (!args.do_create && !args.do_migrate)
401 WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
402 else if (args.do_create && args.do_migrate)
403 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
404 else if (!args.do_migrate && args.is_initpid)
405 WRITE_MSG(2, "'--initpid' is possible in combination with '--migrate' only\n");
406 else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
407 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
408 else if (optind>=argc)
409 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
411 return doit(&args, argv);
413 return wrapper_exit_code;