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_VSERVER 1
43 #define ENSC_WRAPPERS_UNISTD 1
44 #define ENSC_WRAPPERS_FCNTL 1
47 #define CMD_HELP 0x1000
48 #define CMD_VERSION 0x1001
49 #define CMD_CAP 0x4000
50 #define CMD_CTX 0x4001
51 #define CMD_DISCONNECT 0x4002
52 #define CMD_DOMAINNAME 0x4003
53 #define CMD_FLAG 0x4004
54 #define CMD_HOSTNAME 0x4005
55 #define CMD_SECURE 0x4006
56 #define CMD_SILENT 0x4007
58 int wrapper_exit_code = 255;
62 { "help", no_argument, 0, CMD_HELP },
63 { "version", no_argument, 0, CMD_VERSION },
64 { "cap", required_argument, 0, CMD_CAP },
65 { "ctx", required_argument, 0, CMD_CTX },
66 { "disconnect", no_argument, 0, CMD_DISCONNECT },
67 { "domainname", required_argument, 0, CMD_DOMAINNAME },
68 { "flag", required_argument, 0, CMD_FLAG },
69 { "hostname", required_argument, 0, CMD_HOSTNAME },
70 { "secure", no_argument, 0, CMD_SECURE },
71 { "silent", no_argument, 0, CMD_SILENT },
83 char const * hostname;
84 char const * domainname;
87 static struct Arguments const * global_args = 0;
90 showHelp(int fd, char const *cmd, int res)
92 WRITE_MSG(fd, "Usage: ");
95 " [--cap [!]<cap_name>] [--secure] [--ctx <num>] [--disconnect]\n"
96 " [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
97 " [--silent] [--] command arguments ...\n"
99 "chcontext allocate a new security context and executes\n"
100 "a command in that context.\n"
101 "By default, a new/unused context is allocated\n"
104 " Add a capability from the command. This option may be\n"
105 " repeated several time.\n"
106 " See /usr/include/linux/capability.h\n"
107 " In general, this option is used with the --secure option\n"
108 " --secure removes most critical capabilities and --cap\n"
109 " adds specific ones.\n"
113 " Remove a capability from the command. This option may be\n"
114 " repeated several time.\n"
115 " See /usr/include/linux/capability.h\n"
118 " Select the context. On root in context 0 is allowed to\n"
119 " select a specific context.\n"
120 " Context number 1 is special. It can see all processes\n"
121 " in any contexts, but can't kill them though.\n"
122 " Option --ctx may be repeated several times to specify up to 16 contexts.\n"
125 " Start the command in background and make the process\n"
126 " a child of process 1.\n"
128 "--domainname new_domainname\n"
129 " Set the domainname (NIS) in the new security context.\n"
130 " Use \"none\" to unset the domain name.\n"
133 " Set one flag in the new or current security context. The following\n"
134 " flags are supported. The option may be used several time.\n"
136 " fakeinit: The new process will believe it is process number 1.\n"
137 " Useful to run a real /sbin/init in a vserver.\n"
138 " lock: The new process is trapped and can't use chcontext anymore.\n"
139 " sched: The new process and its children will share a common \n"
140 " execution priority.\n"
141 " nproc: Limit the number of process in the vserver according to\n"
142 " ulimit setting. Normally, ulimit is a per user thing.\n"
143 " With this flag, it becomes a per vserver thing.\n"
144 " private: No one can join this security context once created.\n"
145 " ulimit: Apply the current ulimit to the whole context\n"
147 "--hostname new_hostname\n"
148 " Set the hostname in the new security context\n"
149 " This is need because if you create a less privileged\n"
150 " security context, it may be unable to change its hostname\n"
153 " Remove all the capabilities to make a virtual server trustable\n"
156 " Do not print the allocated context number.\n"
158 "Please report bugs to " PACKAGE_BUGREPORT "\n");
167 "chcontext " VERSION " -- allocates/enters a security context\n"
168 "This program is part of " PACKAGE_STRING "\n\n"
169 "Copyright (C) 2003,2004 Enrico Scholz\n"
170 VERSION_COPYRIGHT_DISCLAIMER);
175 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
187 bit = vc_text2cap(str);
189 if (bit!=-1) *cap |= (1<<bit);
191 WRITE_MSG(2, "Unknown capability '");
199 setFlags(char const *str, uint32_t *flags)
202 char const *ptr = strchr(str, ',');
203 size_t cnt = ptr ? (size_t)(ptr-str) : strlen(str);
205 if (strncmp(str, "lock", cnt)==0) *flags |= S_CTX_INFO_LOCK;
206 else if (strncmp(str, "sched", cnt)==0) *flags |= S_CTX_INFO_SCHED;
207 else if (strncmp(str, "nproc", cnt)==0) *flags |= S_CTX_INFO_NPROC;
208 else if (strncmp(str, "private", cnt)==0) *flags |= S_CTX_INFO_PRIVATE;
209 else if (strncmp(str, "fakeinit", cnt)==0) *flags |= S_CTX_INFO_INIT;
210 else if (strncmp(str, "hideinfo", cnt)==0) *flags |= S_CTX_INFO_HIDEINFO;
211 else if (strncmp(str, "ulimit", cnt)==0) *flags |= S_CTX_INFO_ULIMIT;
213 WRITE_MSG(2, "Unknown flag '");
225 static inline ALWAYSINLINE void
226 setHostname(char const *name)
228 if (name == NULL) return;
230 if (sethostname(name, strlen(name))==-1) {
231 perror("sethostname()");
234 if (!global_args->do_silent) {
235 WRITE_MSG(1, "Host name is now ");
241 static inline ALWAYSINLINE void
242 setDomainname(char const *name)
244 if (name == NULL) return;
246 if (setdomainname(name, strlen(name))==-1) {
247 perror("setdomainname()");
250 if (!global_args->do_silent) {
251 WRITE_MSG(1, "Domain name is now ");
257 static inline ALWAYSINLINE void
258 tellContext(xid_t ctx)
260 char buf[sizeof(xid_t)*3+2];
263 if (global_args->do_silent) return;
265 l = utilvserver_fmt_long(buf,ctx);
267 WRITE_MSG(2, "New security context is ");
272 static inline ALWAYSINLINE int
275 if (!global_args->do_disconnect) return 0;
278 fcntl(p[1], F_SETFD, FD_CLOEXEC);
282 static inline ALWAYSINLINE void
283 doSyncStage1(int p[2])
287 if (!global_args->do_disconnect) return;
289 fd = Eopen("/dev/null", O_RDONLY, 0);
293 if (fd!=0) Eclose(fd);
294 Ewrite(p[1], ".", 1);
297 static inline ALWAYSINLINE void
298 doSyncStage2(int p[2])
300 if (!global_args->do_disconnect) return;
302 Ewrite(p[1], "X", 1);
306 waitOnSync(pid_t pid, int p[2])
311 assert(global_args->do_disconnect);
315 l = Eread(p[0], &c, 1);
316 if (l!=1) exitLikeProcess(pid);
317 l = Eread(p[0], &c, 1);
318 if (l!=0) exitLikeProcess(pid);
321 int main (int argc, char *argv[])
323 struct Arguments args = {
325 .do_disconnect = false,
341 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
345 case CMD_HELP : showHelp(1, argv[0], 0);
346 case CMD_VERSION : showVersion();
347 case CMD_DISCONNECT : args.do_disconnect = true; break;
348 case CMD_SILENT : args.do_silent = true; break;
349 case CMD_DOMAINNAME : args.domainname = optarg; break;
350 case CMD_HOSTNAME : args.hostname = optarg; break;
353 setCap(optarg, &args.add_caps, &args.remove_caps);
356 args.remove_caps |= vc_get_securecaps();
359 setFlags(optarg, &args.flags);
363 WRITE_MSG(2, "WARNING: More than one ctx not supported by this version\n");
364 if (args.nbctx>=DIM_OF(args.ctxs)) {
365 WRITE_MSG(2, "Too many contexts given\n");
368 args.ctxs[args.nbctx++] = atoi(optarg);
373 WRITE_MSG(2, "Try '");
374 WRITE_STR(2, argv[0]);
375 WRITE_MSG(2, " --help\" for more information.\n");
382 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
386 if (args.domainname && strcmp(args.domainname, "none")==0)
387 args.domainname = "";
390 args.ctxs[args.nbctx++] = VC_RANDCTX;
392 xflags = args.flags & 16;
394 newctx = Evc_new_s_context(args.ctxs[0],0,args.flags&~16);
395 args.remove_caps &= (~args.add_caps);
396 setHostname(args.hostname);
397 setDomainname(args.domainname);
402 if (args.remove_caps!=0 || xflags!=0)
403 Evc_new_s_context (VC_SAMECTX,args.remove_caps,xflags);
404 tellContext(args.ctxs[0]==VC_RANDCTX ? newctx : args.ctxs[0]);
407 execvp (argv[optind],argv+optind);
418 #ifdef ENSC_TESTSUITE
419 #define FLAG_TEST(STR,EXP) \
422 setFlags(STR, &flag); \
423 assert(flag==(EXP)); \
426 #define CAP_TEST(STR,EXP_ADD,EXP_DEL) \
428 uint32_t add=0,del=0; \
429 setCap(STR, &add, &del); \
430 assert(add==(EXP_ADD)); \
431 assert(del==(EXP_DEL)); \
437 FLAG_TEST("lock", 1);
438 FLAG_TEST("lock,sched", 3);
440 CAP_TEST("CHOWN", 1, 0);
441 CAP_TEST("CAP_CHOWN", 1, 0);
442 CAP_TEST("!CHOWN", 0, 1);
443 CAP_TEST("!CAP_CHOWN", 0, 1);