3 // Copyright (C) 2003,2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on chcontext.cc by Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 chcontext is a wrapper to user the new_s_context system call. It
22 does little more than mapping command line option to the system call
42 #define ENSC_WRAPPERS_PREFIX "chcontext: "
43 #define ENSC_WRAPPERS_VSERVER 1
44 #define ENSC_WRAPPERS_UNISTD 1
45 #define ENSC_WRAPPERS_FCNTL 1
48 #define CMD_HELP 0x1000
49 #define CMD_VERSION 0x1001
50 #define CMD_CAP 0x4000
51 #define CMD_CTX 0x4001
52 #define CMD_DISCONNECT 0x4002
53 #define CMD_DOMAINNAME 0x4003
54 #define CMD_FLAG 0x4004
55 #define CMD_HOSTNAME 0x4005
56 #define CMD_SECURE 0x4006
57 #define CMD_SILENT 0x4007
59 int wrapper_exit_code = 255;
63 { "help", no_argument, 0, CMD_HELP },
64 { "version", no_argument, 0, CMD_VERSION },
65 { "cap", required_argument, 0, CMD_CAP },
66 { "ctx", required_argument, 0, CMD_CTX },
67 { "disconnect", no_argument, 0, CMD_DISCONNECT },
68 { "domainname", required_argument, 0, CMD_DOMAINNAME },
69 { "flag", required_argument, 0, CMD_FLAG },
70 { "hostname", required_argument, 0, CMD_HOSTNAME },
71 { "secure", no_argument, 0, CMD_SECURE },
72 { "silent", no_argument, 0, CMD_SILENT },
84 char const * hostname;
85 char const * domainname;
88 static struct Arguments const * global_args = 0;
91 showHelp(int fd, char const *cmd, int res)
93 WRITE_MSG(fd, "Usage: ");
96 " [--cap [!]<cap_name>] [--secure] [--ctx <num>] [--disconnect]\n"
97 " [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
98 " [--silent] [--] command arguments ...\n"
100 "chcontext allocate a new security context and executes\n"
101 "a command in that context.\n"
102 "By default, a new/unused context is allocated\n"
105 " Add a capability from the command. This option may be\n"
106 " repeated several time.\n"
107 " See /usr/include/linux/capability.h\n"
108 " In general, this option is used with the --secure option\n"
109 " --secure removes most critical capabilities and --cap\n"
110 " adds specific ones.\n"
114 " Remove a capability from the command. This option may be\n"
115 " repeated several time.\n"
116 " See /usr/include/linux/capability.h\n"
119 " Select the context. On root in context 0 is allowed to\n"
120 " select a specific context.\n"
121 " Context number 1 is special. It can see all processes\n"
122 " in any contexts, but can't kill them though.\n"
123 " Option --ctx may be repeated several times to specify up to 16 contexts.\n"
126 " Start the command in background and make the process\n"
127 " a child of process 1.\n"
129 "--domainname new_domainname\n"
130 " Set the domainname (NIS) in the new security context.\n"
131 " Use \"none\" to unset the domain name.\n"
134 " Set one flag in the new or current security context. The following\n"
135 " flags are supported. The option may be used several time.\n"
137 " fakeinit: The new process will believe it is process number 1.\n"
138 " Useful to run a real /sbin/init in a vserver.\n"
139 " lock: The new process is trapped and can't use chcontext anymore.\n"
140 " sched: The new process and its children will share a common \n"
141 " execution priority.\n"
142 " nproc: Limit the number of process in the vserver according to\n"
143 " ulimit setting. Normally, ulimit is a per user thing.\n"
144 " With this flag, it becomes a per vserver thing.\n"
145 " private: No one can join this security context once created.\n"
146 " ulimit: Apply the current ulimit to the whole context\n"
148 "--hostname new_hostname\n"
149 " Set the hostname in the new security context\n"
150 " This is need because if you create a less privileged\n"
151 " security context, it may be unable to change its hostname\n"
154 " Remove all the capabilities to make a virtual server trustable\n"
157 " Do not print the allocated context number.\n"
159 "Please report bugs to " PACKAGE_BUGREPORT "\n");
168 "chcontext " VERSION " -- allocates/enters a security context\n"
169 "This program is part of " PACKAGE_STRING "\n\n"
170 "Copyright (C) 2003,2004 Enrico Scholz\n"
171 VERSION_COPYRIGHT_DISCLAIMER);
176 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
188 bit = vc_text2cap(str);
190 if (bit!=-1) *cap |= (1<<bit);
192 WRITE_MSG(2, "Unknown capability '");
195 exit(wrapper_exit_code);
200 setFlags(char const *str, uint32_t *flags)
205 *flags = vc_textlist2flag(str, 0, &err_ptr, &err_len);
208 WRITE_MSG(2, "Unknown flag '");
209 write(2, err_ptr, err_len);
211 exit(wrapper_exit_code);
215 static inline ALWAYSINLINE void
216 setHostname(char const *name)
218 if (name == NULL) return;
220 if (sethostname(name, strlen(name))==-1) {
221 perror("chcontext: sethostname()");
224 if (!global_args->do_silent) {
225 WRITE_MSG(1, "Host name is now ");
231 static inline ALWAYSINLINE void
232 setDomainname(char const *name)
234 if (name == NULL) return;
236 if (setdomainname(name, strlen(name))==-1) {
237 perror("chcontext: setdomainname()");
240 if (!global_args->do_silent) {
241 WRITE_MSG(1, "Domain name is now ");
247 static inline ALWAYSINLINE void
248 tellContext(xid_t ctx)
250 char buf[sizeof(xid_t)*3+2];
253 if (global_args->do_silent) return;
255 l = utilvserver_fmt_long(buf,ctx);
257 WRITE_MSG(2, "New security context is ");
262 static inline ALWAYSINLINE int
265 if (!global_args->do_disconnect) return 0;
268 fcntl(p[1], F_SETFD, FD_CLOEXEC);
272 static inline ALWAYSINLINE void
273 doSyncStage1(int p[2])
277 if (!global_args->do_disconnect) return;
279 fd = Eopen("/dev/null", O_RDONLY, 0);
283 if (fd!=0) Eclose(fd);
284 Ewrite(p[1], ".", 1);
287 static inline ALWAYSINLINE void
288 doSyncStage2(int p[2])
290 if (!global_args->do_disconnect) return;
292 Ewrite(p[1], "X", 1);
296 waitOnSync(pid_t pid, int p[2])
301 assert(global_args->do_disconnect);
305 l = Eread(p[0], &c, 1);
306 if (l!=1) exitLikeProcess(pid);
307 l = Eread(p[0], &c, 1);
308 if (l!=0) exitLikeProcess(pid);
311 int main (int argc, char *argv[])
313 struct Arguments args = {
315 .do_disconnect = false,
331 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
335 case CMD_HELP : showHelp(1, argv[0], 0);
336 case CMD_VERSION : showVersion();
337 case CMD_DISCONNECT : args.do_disconnect = true; break;
338 case CMD_SILENT : args.do_silent = true; break;
339 case CMD_DOMAINNAME : args.domainname = optarg; break;
340 case CMD_HOSTNAME : args.hostname = optarg; break;
343 setCap(optarg, &args.add_caps, &args.remove_caps);
346 args.remove_caps |= vc_get_securecaps();
349 setFlags(optarg, &args.flags);
353 WRITE_MSG(2, "WARNING: More than one ctx not supported by this version\n");
354 if (args.nbctx>=DIM_OF(args.ctxs)) {
355 WRITE_MSG(2, "Too many contexts given\n");
358 args.ctxs[args.nbctx++] = atoi(optarg);
363 WRITE_MSG(2, "Try '");
364 WRITE_STR(2, argv[0]);
365 WRITE_MSG(2, " --help\" for more information.\n");
372 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
376 if (args.domainname && strcmp(args.domainname, "none")==0)
377 args.domainname = "";
380 args.ctxs[args.nbctx++] = VC_RANDCTX;
382 xflags = args.flags & S_CTX_INFO_INIT;
383 args.flags &= ~S_CTX_INFO_INIT;
387 newctx = Evc_new_s_context(args.ctxs[0],0,args.flags);
388 args.remove_caps &= (~args.add_caps);
389 setHostname(args.hostname);
390 setDomainname(args.domainname);
392 if (args.remove_caps!=0 || xflags!=0)
393 Evc_new_s_context (VC_SAMECTX,args.remove_caps,xflags);
394 tellContext(args.ctxs[0]==VC_RANDCTX ? newctx : args.ctxs[0]);
397 execvp (argv[optind],argv+optind);
400 PERROR_Q("chcontext: execvp", argv[optind]);
408 #ifdef ENSC_TESTSUITE
409 #define FLAG_TEST(STR,EXP) \
412 setFlags(STR, &flag); \
413 assert(flag==(EXP)); \
416 #define CAP_TEST(STR,EXP_ADD,EXP_DEL) \
418 uint32_t add=0,del=0; \
419 setCap(STR, &add, &del); \
420 assert(add==(EXP_ADD)); \
421 assert(del==(EXP_DEL)); \
427 FLAG_TEST("lock", 1);
428 FLAG_TEST("lock,sched", 3);
430 CAP_TEST("CHOWN", 1, 0);
431 CAP_TEST("CAP_CHOWN", 1, 0);
432 CAP_TEST("!CHOWN", 0, 1);
433 CAP_TEST("!CAP_CHOWN", 0, 1);