2 * Marc E. Fiuczynski <mef@cs.princeton.edu>
4 * Copyright (c) 2004 The Trustees of Princeton University (Trustees).
6 * Portions of this file are derived from the Paul Brett's vserver.c
7 * modification to bash (vsh).
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * It is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 * License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with Poptop; see the file COPYING. If not, write to the Free Software
21 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include <sys/syscall.h>
38 #include <asm/unistd.h>
39 #include <sys/mount.h>
40 #include <sys/types.h>
44 #include "linuxcaps.h"
47 /* Null byte made explicit */
48 #define NULLBYTE_SIZE 1
50 /* Base for all vserver roots for chroot */
51 #define VSERVER_ROOT_BASE "/vservers"
54 /* Change to root:root (before entering new context) */
55 static int setuidgid_root()
58 fprintf(stderr, "setgid error\n");
62 fprintf(stderr, "setuid error\n");
68 static void compute_new_root(char *base, char **root, uid_t uid)
73 if ((pwd = getpwuid(uid)) == NULL) {
74 perror("vserver: getpwuid error ");
79 strlen(base) + strlen("/") +
80 strlen(pwd->pw_name) + NULLBYTE_SIZE;
81 (*root) = (char *)malloc(root_len);
82 if ((*root) == NULL) {
83 perror("vserver: malloc error ");
87 sprintf((*root), "%s/%s", base, pwd->pw_name);
88 (*root)[root_len - 1] = '\0';
91 /* Example: sandbox_root = /vservers/bnc, relpath = /proc/1 */
92 static int sandbox_file_exists(char *sandbox_root, char *relpath)
98 len = strlen(sandbox_root) + strlen(relpath) + NULLBYTE_SIZE;
99 if ((file = (char *)malloc(len)) == NULL) {
100 perror("vserver: malloc error ");
103 sprintf(file, "%s%s", sandbox_root, relpath);
104 file[len - 1] = '\0';
105 if (stat(file, &stat_buf) == 0) {
114 static int proc_mounted(char *sandbox_root)
116 return sandbox_file_exists(sandbox_root, "/proc/1");
119 static int devpts_mounted(char *sandbox_root)
121 return sandbox_file_exists(sandbox_root, "/dev/pts/0");
124 static void mount_proc(char *sandbox_root,uid_t uid)
126 char *source = "/proc";
130 len = strlen(sandbox_root) + strlen("/") + strlen("proc") + NULLBYTE_SIZE;
131 if ((target = (char *)malloc(len)) == NULL) {
132 perror("vserver: malloc error ");
136 sprintf(target, "%s/proc", sandbox_root);
137 target[len - 1] = '\0';
138 if (!proc_mounted(sandbox_root))
139 mount(source, target, "proc", MS_RDONLY, NULL);
144 static void mount_devpts(char *sandbox_root)
146 char *source = "/dev/pts";
150 len = strlen(sandbox_root) + strlen("/") + strlen("dev/pts") + NULLBYTE_SIZE;
151 if ((target = (char *)malloc(len)) == NULL) {
152 perror("vserver: malloc error ");
156 sprintf(target, "%s/dev/pts", sandbox_root);
157 target[len - 1] = '\0';
158 if (!devpts_mounted(sandbox_root))
159 mount(source, target, "devpts", 0, NULL);
164 static int sandbox_chroot(uid_t uid)
166 char *sandbox_root = NULL;
168 compute_new_root(VSERVER_ROOT_BASE,&sandbox_root, uid);
169 mount_proc(sandbox_root,uid);
170 mount_devpts(sandbox_root);
171 if (chroot(sandbox_root) < 0) {
172 fprintf(stderr,"vserver: chroot error (%s): ",sandbox_root);
176 if (chdir("/") < 0) {
177 perror("vserver: chdir error ");
183 static int sandbox_processes(uid_t uid)
193 flags |= 1; /* VX_INFO_LOCK -- cannot request a new vx_id */
194 flags |= 4; /* VX_INFO_NPROC -- limit number of procs in a context */
196 remove_cap = /* NOTE: keep in sync with chcontext.c */
197 (1<<CAP_LINUX_IMMUTABLE)|
198 (1<<CAP_NET_BROADCAST)|
209 (1<<CAP_SYS_RESOURCE)|
221 if (vc_new_s_context(context,remove_cap,flags) < 0) {
222 perror("vserver: new_s_context error ");
229 void runas_slice_user(char *username)
232 char *home_env, *logname_env, *mail_env, *shell_env, *user_env;
233 int home_len, logname_len, mail_len, shell_len, user_len;
234 static char *envp[10];
236 if ((pwd = getpwnam(username)) == NULL) {
237 perror("vserver: getpwnam error ");
241 if (setgid(pwd->pw_gid) < 0) {
242 perror("vserver: setgid error ");
246 if (setuid(pwd->pw_uid) < 0) {
247 perror("vserver: setuid error ");
251 if (chdir(pwd->pw_dir) < 0) {
252 perror("vserver: chdir error ");
256 home_len = strlen("HOME=") + strlen(pwd->pw_dir) + NULLBYTE_SIZE;
257 logname_len = strlen("LOGNAME=") + strlen(username) + NULLBYTE_SIZE;
258 mail_len = strlen("MAIL=/var/spool/mail/") + strlen(username)
260 shell_len = strlen("SHELL=") + strlen(pwd->pw_shell) + NULLBYTE_SIZE;
261 user_len = strlen("USER=") + strlen(username) + NULLBYTE_SIZE;
263 home_env = (char *)malloc(home_len);
264 logname_env = (char *)malloc(logname_len);
265 mail_env = (char *)malloc(mail_len);
266 shell_env = (char *)malloc(shell_len);
267 user_env = (char *)malloc(user_len);
269 if ((home_env == NULL) ||
270 (logname_env == NULL) ||
271 (mail_env == NULL) ||
272 (shell_env == NULL) ||
273 (user_env == NULL)) {
274 perror("vserver: malloc error ");
278 sprintf(home_env, "HOME=%s", pwd->pw_dir);
279 sprintf(logname_env, "LOGNAME=%s", username);
280 sprintf(mail_env, "MAIL=/var/spool/mail/%s", username);
281 sprintf(shell_env, "SHELL=%s", pwd->pw_shell);
282 sprintf(user_env, "USER=%s", username);
284 home_env[home_len - 1] = '\0';
285 logname_env[logname_len - 1] = '\0';
286 mail_env[mail_len - 1] = '\0';
287 shell_env[shell_len - 1] = '\0';
288 user_env[user_len - 1] = '\0';
291 envp[1] = logname_env;
297 if ((putenv(home_env) < 0) ||
298 (putenv(logname_env) < 0) ||
299 (putenv(mail_env) < 0) ||
300 (putenv(shell_env) < 0) ||
301 (putenv(user_env) < 0)) {
302 perror("vserver: putenv error ");
309 void slice_enter(char *context)
314 if ((pwd = getpwnam(context)) == NULL) {
315 fprintf(stderr,"vserver: getpwname(%s) failed",context);
319 context = (char*)malloc(strlen(pwd->pw_name)+NULLBYTE_SIZE);
321 perror("vserver: malloc failed");
324 strcpy(context,pwd->pw_name);
326 if (setuidgid_root() < 0) { /* For chroot, new_s_context */
327 fprintf(stderr,"vserver: Could not setuid/setguid to root:root\n");
332 if (sandbox_chroot(uid) < 0) {
333 fprintf(stderr, "vserver: Could not chroot to vserver root\n");
337 if (sandbox_processes(uid) < 0) {
338 fprintf(stderr, "vserver: Could not sandbox processes in vserver\n");