runsuid.c 4.31 KB
Newer Older
1
/*
2
 * Copyright (c) 2012-2014 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 */

/*
 * Replacement for suid perl. Installed setuid, and just execs the
 * script/binary in the magic directory with the same name as us.
 * We run the script/binary with the following constraints:
 *
 * EUID is 0, UID is not changed.
 * GID and EGID list is not changed.
 * Path is set to a standard value
 * A couple of environment variables are purged.
 *
 * The latter two are not things that suidperl would do, but is something
 * that our scripts routinely do.
 */

#include <stdio.h>
#include <stdlib.h>
40
#include <unistd.h>
41 42
#include <string.h>
#include <assert.h>
43
#include <sys/param.h>
44 45 46 47 48 49 50 51
#include <sys/stat.h>
#include "config.h"

#define SUIDDIR	TBROOT "/suidbin/"

char *safepath[] = {
	TBROOT "/bin",
	TBROOT "/sbin",
52
	TBROOT "/cgi-bin",
53 54 55 56 57 58 59 60 61 62 63 64
	"/bin",
	"/sbin",
	"/usr/bin",
	"/usr/sbin",
	"/usr/local/bin",
	"/usr/local/sbin"
};

char *badvars[] = {
	"IFS=", "CDPATH=", "ENV=", "BASH_ENV="
};

Mike Hibler's avatar
Mike Hibler committed
65
static int debug = 0;
66 67 68 69 70

static void
sanedir(char *dir)
{
	struct stat sb;
71
	char *rpath;
72

73 74 75 76 77
	if ((rpath = malloc(PATH_MAX)) == NULL) {
		fprintf(stderr, "no memory!\n");
		exit(2);
	}
	if (realpath(dir, rpath) == NULL) {
78 79 80 81 82 83 84 85 86
		perror(dir);
		exit(1);
	}
	if (strncmp(rpath, TBROOT, strlen(TBROOT)) != 0) {
		fprintf(stderr, "%s: must be in %s\n", dir, TBROOT);
		exit(1);
	}

	if (stat(rpath, &sb) != 0) {
87 88 89 90 91 92
		perror(dir);
		exit(1);
	}
	if (sb.st_uid != 0 ||
	    !S_ISDIR(sb.st_mode) || (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
		fprintf(stderr, "%s: must be root-owned, unwritable dir\n",
93
			dir);
94 95
		exit(1);
	}
96 97

	free(rpath);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

static char **
saneenviron(char **oenvp, char *path)
{
	int i, ni, j;
	char **nenvp;
	int havepath = 0;

	for (i = 0; oenvp != NULL && oenvp[i] != NULL; i++)
		if (!strncmp(oenvp[i], "PATH=", 5))
			havepath++;
	if (!havepath)
		i++;

	nenvp = calloc(i, sizeof(char *));
	if (nenvp == NULL)
		return NULL;

	for (i = ni = 0; oenvp != NULL && oenvp[i] != NULL; i++) {
		for (j = 0; j < sizeof(badvars)/sizeof(badvars[0]); j++)
			if (!strncmp(oenvp[i], badvars[j], strlen(badvars[j])))
				break;
		if (j < sizeof(badvars)/sizeof(badvars[0]))
			continue;
		if (!strncmp(oenvp[i], "PATH=", 5)) {
			nenvp[ni++] = path;
			continue;
		}
		nenvp[ni++] = oenvp[i];
	}
	if (!havepath)
		nenvp[ni] = path;

	return nenvp;
}

int
main(int argc, char **argv)
{
	char *cp, *name = argv[0];
	char *exepath, *path, **envp;
	int i, len;
	extern char **environ;

Mike Hibler's avatar
Mike Hibler committed
143
	if (geteuid()) {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		fprintf(stderr, "%s: not running as root; not suid?\n", name);
		exit(1);
	}

	/* Check the state of the SUIDDIR */
	sanedir(SUIDDIR);

	if ((cp = strrchr(name, '/')) == NULL)
		cp = name;
	else
		cp++;

	assert(strlen(cp) > 0);
	if ((exepath = malloc(strlen(SUIDDIR) + strlen(cp) + 1)) == NULL) {
	nomem:
		fprintf(stderr, "%s: no memory!\n", name);
		exit(2);
	}
	strcpy(exepath, SUIDDIR);
	strcat(exepath, cp);

	len = 0;
	for (i = 0; i < sizeof(safepath)/sizeof(safepath[0]); i++)
		len += strlen(safepath[i]) + 1;
	if ((path = malloc(5+len)) == NULL)
		goto nomem;
	strcpy(path, "PATH=");
	cp = path + 5;
	for (i = 0; i < sizeof(safepath)/sizeof(safepath[0]); i++) {
		strcpy(cp, safepath[i]);
		cp += strlen(safepath[i]);
		*cp++ = ':';
	}
	if (cp != path)
		*--cp = '\0';

	if ((envp = saneenviron(environ, path)) == NULL)
		goto nomem;

	argv[0] = exepath;

	if (debug) {
		printf("Command: '%s'\n", exepath);
		printf("Args:\n");
		for (i = 0; argv[i] != NULL; i++)
			printf("  '%s'\n", argv[i]);
		if (envp) {
			printf("Env:\n");
			for (i = 0; envp[i] != NULL; i++)
				printf("  '%s'\n", envp[i]);
		}
		exit(0);
	}

	execve(exepath, argv, envp);
	perror(exepath);
}