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"
27 #include "linuxcaps.h"
33 #include <sys/socket.h>
37 #define ENSC_WRAPPERS_PREFIX "vcontext: "
38 #define ENSC_WRAPPERS_UNISTD 1
39 #define ENSC_WRAPPERS_VSERVER 1
40 #define ENSC_WRAPPERS_FCNTL 1
41 #define ENSC_WRAPPERS_SOCKET 1
42 #define ENSC_WRAPPERS_IOSOCK 1
45 #define CMD_HELP 0x1000
46 #define CMD_VERSION 0x1001
47 #define CMD_XID 0x4000
48 #define CMD_CREATE 0x4001
49 #define CMD_MIGRATE 0x4003
50 #define CMD_FAKEINIT 0x4002
51 #define CMD_DISCONNECT 0x4004
52 #define CMD_UID 0x4005
53 #define CMD_CHROOT 0x4006
54 #define CMD_SILENT 0x4007
55 #define CMD_SYNCSOCK 0x4008
56 #define CMD_SYNCMSG 0x4009
57 #define CMD_MIGRATESELF 0x400a
58 #define CMD_ENDSETUP 0x400b
61 #define CMD_CCAP 0x2002
62 #define CMD_BCAP 0x2005
67 { "help", no_argument, 0, CMD_HELP },
68 { "version", no_argument, 0, CMD_VERSION },
69 { "ctx", required_argument, 0, CMD_XID },
70 { "xid", required_argument, 0, CMD_XID },
71 { "create", no_argument, 0, CMD_CREATE },
72 { "migrate", no_argument, 0, CMD_MIGRATE },
73 { "migrate-self", no_argument, 0, CMD_MIGRATESELF },
74 { "fakeinit", no_argument, 0, CMD_FAKEINIT },
75 { "endsetup", no_argument, 0, CMD_ENDSETUP },
76 { "disconnect", no_argument, 0, CMD_DISCONNECT },
77 { "silent", no_argument, 0, CMD_SILENT },
78 { "uid", required_argument, 0, CMD_UID },
79 { "chroot", no_argument, 0, CMD_CHROOT },
80 { "syncsock", required_argument, 0, CMD_SYNCSOCK },
81 { "syncmsg", required_argument, 0, CMD_SYNCMSG },
82 // { "ccap", required_argument, 0, CMD_CCAP },
83 // { "bcap", required_argument, 0, CMD_BCAP },
98 char const * sync_sock;
99 char const * sync_msg;
100 //struct vc_ctx_caps caps;
103 int wrapper_exit_code = 255;
106 showHelp(int fd, char const *cmd, int res)
108 WRITE_MSG(fd, "Usage:\n ");
111 " --create [--xid <xid>] <opts>* [--] <program> <args>*\n ");
114 " [(--migrate --xid <xid>)|--migrate-self] <opts>* [--] <program> <args>*\n"
117 " --chroot ... chroot into current directory\n"
118 " --uid <uid> ... change uid\n"
119 " --fakeinit ... set current process as general process reaper\n"
120 " for ctx (possible for --migrate only)\n"
121 " --endsetup ... clear the setup flag; usefully for migrate only\n"
122 " --disconnect ... start program in background\n"
123 " --silent ... be silent\n"
124 " --syncsock <file-name>\n"
125 " ... before executing the program, send a message\n"
126 " to the socket and wait until it closes.\n"
127 " <file-name> must be a SOCK_STREAM unix socket\n"
128 " --syncmsg <message>\n"
129 " ... use <message> as synchronization message; by\n"
130 " default, 'ok' will be used\n"
132 "'vcontext --create' exits with code 254 iff the context exists already.\n"
134 "Please report bugs to " PACKAGE_BUGREPORT "\n");
143 "vcontext " VERSION " -- manages the creation of security contexts\n"
144 "This program is part of " PACKAGE_STRING "\n\n"
145 "Copyright (C) 2004 Enrico Scholz\n"
146 VERSION_COPYRIGHT_DISCLAIMER);
150 static inline ALWAYSINLINE int
151 initSync(int p[2], bool do_disconnect)
153 if (!do_disconnect) return 0;
156 fcntl(p[1], F_SETFD, FD_CLOEXEC);
160 static inline ALWAYSINLINE void
161 doSyncStage1(int p[2], bool do_disconnect)
165 if (!do_disconnect) return;
167 fd = Eopen("/dev/null", O_RDONLY, 0);
171 if (fd!=0) Eclose(fd);
172 Ewrite(p[1], ".", 1);
175 static inline ALWAYSINLINE void
176 doSyncStage2(int p[2], bool do_disconnect)
178 if (!do_disconnect) return;
180 Ewrite(p[1], "X", 1);
184 waitOnSync(pid_t pid, int p[2])
190 l = Eread(p[0], &c, 1);
191 if (l!=1) exitLikeProcess(pid);
192 l = Eread(p[0], &c, 1);
193 if (l!=0) exitLikeProcess(pid);
196 static inline ALWAYSINLINE void
197 tellContext(xid_t ctx, bool do_it)
199 char buf[sizeof(xid_t)*3+2];
204 l = utilvserver_fmt_long(buf,ctx);
206 WRITE_MSG(2, "New security context is ");
212 connectExternalSync(char const *filename)
215 struct sockaddr_un addr;
217 if (filename==0) return -1;
219 ENSC_INIT_UNIX_SOCK(addr, filename);
221 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
222 Econnect(fd, &addr, sizeof(addr));
228 setFlags(struct Arguments const *args, xid_t xid)
230 struct vc_ctx_flags flags = { 0,0 };
232 if (args->is_fakeinit)
233 flags.mask |= VC_VXF_STATE_INIT;
235 if (args->do_endsetup)
236 flags.mask |= VC_VXF_STATE_SETUP;
239 Evc_set_flags(xid, &flags);
243 doExternalSync(int fd, char const *msg)
249 if (msg) EsendAll(fd, msg, strlen(msg));
250 Eshutdown(fd, SHUT_WR);
252 if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
253 WRITE_MSG(2, "vcontext: unexpected external synchronization event\n");
263 extern int capget (struct __user_cap_header_struct *,
264 struct __user_cap_data_struct *);
265 extern int capset (struct __user_cap_header_struct *,
266 struct __user_cap_data_struct *);
267 struct vc_ctx_caps caps;
268 struct __user_cap_header_struct header;
269 struct __user_cap_data_struct user;
271 header.version = _LINUX_CAPABILITY_VERSION;
274 Evc_get_ccaps(xid, &caps);
275 if (capget(&header, &user)==-1)
276 perror(ENSC_WRAPPERS_PREFIX "capget()");
277 else if ( (user.permitted &= caps.ccaps),
278 (user.effective &= user.permitted),
279 (user.inheritable &= user.permitted),
280 capset(&header, &user)==-1 )
281 perror(ENSC_WRAPPERS_PREFIX "capset()");
285 exit(wrapper_exit_code);
290 parseBCaps(char const *str, struct vc_ctx_caps *caps)
292 struct vc_err_listparser err;
295 rc = vc_list2bcap(str,0, &err, caps);
298 WRITE_MSG(2, "Unknown bcap '");
299 write(2, err.ptr, err.len);
301 exit(wrapper_exit_code);
306 parseCCaps(char const *str, struct vc_ctx_caps *caps)
308 struct vc_err_listparser err;
311 rc = vc_list2ccap(str,0, &err, caps);
314 WRITE_MSG(2, "Unknown ccap '");
315 write(2, err.ptr, err.len);
317 exit(wrapper_exit_code);
322 parseSecure(struct vc_ctx_flags UNUSED * flags,
323 struct vc_ctx_caps UNUSED * caps)
327 caps->bcaps = ~vc_get_securecaps();
331 static inline ALWAYSINLINE int
332 doit(struct Arguments const *args, char *argv[])
335 pid_t pid = initSync(p, args->do_disconnect);
339 int ext_sync_fd = connectExternalSync(args->sync_sock);
341 if (args->do_create) {
342 xid = vc_create_context(args->xid);
344 if (errno==EEXIST) return 254;
346 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
347 return wrapper_exit_code;
350 tellContext(xid, args->verbosity>=1);
361 if (args->do_migrate || args->do_migrateself)
365 if (args->do_migrate && !args->do_migrateself)
366 Evc_migrate_context(xid);
368 if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
370 if (getuid()!=args->uid) {
371 WRITE_MSG(2, "vcontext: Something went wrong while changing the UID\n");
376 doExternalSync(ext_sync_fd, args->sync_msg);
377 doSyncStage1(p, args->do_disconnect);
378 execvp (argv[optind],argv+optind);
379 doSyncStage2(p, args->do_disconnect);
381 PERROR_Q("chcontext: execvp", argv[optind]);
385 assert(args->do_disconnect);
387 #warning add an additional sync here
388 if (!jailIntoTempDir()) {
389 perror(ENSC_WRAPPERS_PREFIX "jailIntoTempDir()");
397 int main (int argc, char *argv[])
399 struct Arguments args = {
402 .do_migrateself = false,
403 .do_disconnect = false,
404 .do_endsetup = false,
405 .is_fakeinit = false,
408 .xid = VC_DYNAMIC_XID,
413 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
417 case CMD_HELP : showHelp(1, argv[0], 0);
418 case CMD_VERSION : showVersion();
419 case CMD_CREATE : args.do_create = true; break;
420 case CMD_MIGRATE : args.do_migrate = true; break;
421 case CMD_DISCONNECT : args.do_disconnect = true; break;
422 case CMD_ENDSETUP : args.do_endsetup = true; break;
423 case CMD_FAKEINIT : args.is_fakeinit = true; break;
424 case CMD_CHROOT : args.do_chroot = true; break;
425 case CMD_SILENT : --args.verbosity; break;
426 case CMD_XID : args.xid = atol(optarg); break;
427 case CMD_UID : args.uid = atol(optarg); break;
428 case CMD_SYNCSOCK : args.sync_sock = optarg; break;
429 case CMD_SYNCMSG : args.sync_msg = optarg; break;
431 case CMD_CCAP : parseCCaps(optarg, &args.caps); break;
432 case CMD_BCAP : parseBCaps(optarg, &args.caps); break;
433 case CMD_SECURE : parseSecure(&args.caps); break;
435 case CMD_MIGRATESELF :
436 args.do_migrate = true;
437 args.do_migrateself = true;
441 WRITE_MSG(2, "Try '");
442 WRITE_STR(2, argv[0]);
443 WRITE_MSG(2, " --help\" for more information.\n");
449 if (args.do_migrateself)
450 args.xid = Evc_get_task_xid(0);
452 if (!args.do_create && !args.do_migrate)
453 WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
454 else if (args.do_create && args.do_migrate)
455 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
456 else if (!args.do_migrate && args.is_fakeinit)
457 WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
458 else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
459 WRITE_MSG(2, "vcontext: Can not migrate to an unknown context\n");
460 else if (optind>=argc)
461 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
463 return doit(&args, argv);