2006-12-06 19:29:10 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2006-12-07 00:45:41 +02:00
|
|
|
#include <fcntl.h>
|
2006-12-06 19:29:10 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
#include "../libmain/setuid-common.hh"
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
2006-12-07 16:14:35 +02:00
|
|
|
/* Recursively change the ownership of `path' to user `uidTo' and
|
|
|
|
group `gidTo'. `path' must currently be owned by user `uidFrom',
|
|
|
|
or, if `uidFrom' is -1, by group `gidFrom'. */
|
|
|
|
static void secureChown(uid_t uidFrom, gid_t gidFrom,
|
|
|
|
uid_t uidTo, gid_t gidTo, const Path & path)
|
2006-12-06 19:29:10 +02:00
|
|
|
{
|
2006-12-07 16:14:35 +02:00
|
|
|
format error = format("cannot change ownership of `%1%'") % path;
|
|
|
|
|
2006-12-07 01:52:25 +02:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.c_str(), &st) == -1)
|
2006-12-07 16:14:35 +02:00
|
|
|
/* Important: don't give any detailed error messages here.
|
|
|
|
Otherwise, the Nix account can discover information about
|
|
|
|
the existence of paths that it doesn't normally have access
|
|
|
|
to. */
|
|
|
|
throw Error(error);
|
|
|
|
|
|
|
|
if (uidFrom != -1) {
|
|
|
|
assert(uidFrom != 0);
|
|
|
|
if (st.st_uid != uidFrom)
|
|
|
|
throw Error(error);
|
|
|
|
} else {
|
|
|
|
assert(gidFrom != 0);
|
|
|
|
if (st.st_gid != gidFrom)
|
|
|
|
throw Error(error);
|
|
|
|
}
|
2006-12-07 01:52:25 +02:00
|
|
|
|
2006-12-07 16:14:35 +02:00
|
|
|
assert(uidTo != 0 && gidTo != 0);
|
2006-12-07 01:52:25 +02:00
|
|
|
|
|
|
|
if (lchown(path.c_str(), uidTo, gidTo) == -1)
|
2006-12-07 16:14:35 +02:00
|
|
|
throw Error(error);
|
2006-12-07 01:52:25 +02:00
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
Strings names = readDirectory(path);
|
|
|
|
for (Strings::iterator i = names.begin(); i != names.end(); ++i)
|
|
|
|
/* !!! recursion; check stack depth */
|
2006-12-07 16:14:35 +02:00
|
|
|
secureChown(uidFrom, gidFrom, uidTo, gidTo, path + "/" + *i);
|
2006-12-07 01:52:25 +02:00
|
|
|
}
|
2006-12-06 19:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-07 00:45:41 +02:00
|
|
|
static uid_t nameToUid(const string & userName)
|
2006-12-06 19:29:10 +02:00
|
|
|
{
|
|
|
|
struct passwd * pw = getpwnam(userName.c_str());
|
|
|
|
if (!pw)
|
2006-12-07 00:45:41 +02:00
|
|
|
throw Error(format("user `%1%' does not exist") % userName);
|
|
|
|
return pw->pw_uid;
|
|
|
|
}
|
2006-12-06 19:29:10 +02:00
|
|
|
|
2006-12-07 00:45:41 +02:00
|
|
|
|
2006-12-07 01:15:26 +02:00
|
|
|
/* Run `program' under user account `targetUser'. `targetUser' should
|
|
|
|
be a member of `buildUsersGroup'. The ownership of the current
|
|
|
|
directory is changed from the Nix user (uidNix) to the target
|
|
|
|
user. */
|
2006-12-07 16:14:35 +02:00
|
|
|
static void runBuilder(uid_t uidNix, gid_t gidBuildUsers,
|
|
|
|
const StringSet & buildUsers, const string & targetUser,
|
2006-12-07 02:42:30 +02:00
|
|
|
string program, int argc, char * * argv, char * * env)
|
2006-12-07 00:45:41 +02:00
|
|
|
{
|
|
|
|
uid_t uidTargetUser = nameToUid(targetUser);
|
2006-12-07 01:15:26 +02:00
|
|
|
|
|
|
|
/* Sanity check. */
|
|
|
|
if (uidTargetUser == 0)
|
|
|
|
throw Error("won't setuid to root");
|
|
|
|
|
|
|
|
/* Verify that the target user is a member of that group. */
|
2006-12-07 16:14:35 +02:00
|
|
|
if (buildUsers.find(targetUser) == buildUsers.end())
|
|
|
|
throw Error(format("user `%1%' is not a member of the build users group")
|
|
|
|
% targetUser);
|
2006-12-06 19:29:10 +02:00
|
|
|
|
|
|
|
/* Chown the current directory, *if* it is owned by the Nix
|
|
|
|
account. The idea is that the current directory is the
|
|
|
|
temporary build directory in /tmp or somewhere else, and we
|
|
|
|
don't want to create that directory here. */
|
2006-12-07 16:14:35 +02:00
|
|
|
secureChown(uidNix, -1, uidTargetUser, gidBuildUsers, ".");
|
2006-12-06 19:29:10 +02:00
|
|
|
|
|
|
|
/* Set the real, effective and saved gid. Must be done before
|
|
|
|
setuid(), otherwise it won't set the real and saved gids. */
|
2006-12-06 22:00:15 +02:00
|
|
|
if (setgroups(0, 0) == -1)
|
|
|
|
throw SysError("cannot clear the set of supplementary groups");
|
2006-12-07 01:15:26 +02:00
|
|
|
|
|
|
|
if (setgid(gidBuildUsers) == -1 ||
|
|
|
|
getgid() != gidBuildUsers ||
|
|
|
|
getegid() != gidBuildUsers)
|
|
|
|
throw SysError("setgid failed");
|
2006-12-06 19:29:10 +02:00
|
|
|
|
|
|
|
/* Set the real, effective and saved uid. */
|
2006-12-07 00:45:41 +02:00
|
|
|
if (setuid(uidTargetUser) == -1 ||
|
|
|
|
getuid() != uidTargetUser ||
|
|
|
|
geteuid() != uidTargetUser)
|
2006-12-06 22:00:15 +02:00
|
|
|
throw SysError("setuid failed");
|
2006-12-06 19:29:10 +02:00
|
|
|
|
|
|
|
/* Execute the program. */
|
|
|
|
std::vector<const char *> args;
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
args.push_back(argv[i]);
|
|
|
|
args.push_back(0);
|
|
|
|
|
2006-12-07 02:42:30 +02:00
|
|
|
if (execve(program.c_str(), (char * *) &args[0], env) == -1)
|
2006-12-06 19:29:10 +02:00
|
|
|
throw SysError(format("cannot execute `%1%'") % program);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-07 00:45:41 +02:00
|
|
|
#ifndef NIX_SETUID_CONFIG_FILE
|
|
|
|
#define NIX_SETUID_CONFIG_FILE "/etc/nix-setuid.conf"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-12-06 19:29:10 +02:00
|
|
|
static void run(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
char * * oldEnviron = environ;
|
|
|
|
|
|
|
|
setuidCleanup();
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
throw Error("nix-setuid-wrapper must be setuid root");
|
|
|
|
|
|
|
|
|
|
|
|
/* Read the configuration file. It should consist of two words:
|
|
|
|
|
|
|
|
<nix-user-name> <nix-builders-group>
|
|
|
|
|
|
|
|
The first is the privileged account under which the main Nix
|
|
|
|
processes run (i.e., the supposed caller). It should match our
|
|
|
|
real uid. The second is the Unix group to which the Nix
|
|
|
|
builders belong (and nothing else!). */
|
2006-12-07 00:45:41 +02:00
|
|
|
string configFile = NIX_SETUID_CONFIG_FILE;
|
|
|
|
AutoCloseFD fdConfig = open(configFile.c_str(), O_RDONLY);
|
|
|
|
if (fdConfig == -1)
|
|
|
|
throw SysError(format("opening `%1%'") % configFile);
|
|
|
|
|
|
|
|
/* Config file should be owned by root. */
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fdConfig, &st) == -1) throw SysError("statting file");
|
|
|
|
if (st.st_uid != 0)
|
|
|
|
throw Error(format("`%1%' not owned by root") % configFile);
|
|
|
|
if (st.st_mode & (S_IWGRP | S_IWOTH))
|
|
|
|
throw Error(format("`%1%' should not be group or world-writable") % configFile);
|
|
|
|
|
|
|
|
Strings tokens = tokenizeString(readFile(fdConfig));
|
|
|
|
|
|
|
|
fdConfig.close();
|
|
|
|
|
|
|
|
if (tokens.size() != 2)
|
|
|
|
throw Error(format("parse error in `%1%'") % configFile);
|
|
|
|
|
|
|
|
Strings::iterator i = tokens.begin();
|
2006-12-07 01:15:26 +02:00
|
|
|
string nixUser = *i++;
|
2006-12-07 00:45:41 +02:00
|
|
|
string buildUsersGroup = *i++;
|
|
|
|
|
|
|
|
|
|
|
|
/* Check that the caller (real uid) is the one allowed to call
|
|
|
|
this program. */
|
2006-12-07 01:15:26 +02:00
|
|
|
uid_t uidNix = nameToUid(nixUser);
|
|
|
|
if (uidNix != getuid())
|
2006-12-07 00:45:41 +02:00
|
|
|
throw Error("you are not allowed to call this program, go away");
|
2006-12-06 19:29:10 +02:00
|
|
|
|
|
|
|
|
2006-12-07 16:14:35 +02:00
|
|
|
/* Get the gid and members of buildUsersGroup. */
|
|
|
|
struct group * gr = getgrnam(buildUsersGroup.c_str());
|
|
|
|
if (!gr)
|
|
|
|
throw Error(format("group `%1%' does not exist") % buildUsersGroup);
|
|
|
|
gid_t gidBuildUsers = gr->gr_gid;
|
|
|
|
|
|
|
|
StringSet buildUsers;
|
|
|
|
for (char * * p = gr->gr_mem; *p; ++p)
|
|
|
|
buildUsers.insert(*p);
|
|
|
|
|
|
|
|
|
2006-12-06 19:29:10 +02:00
|
|
|
/* Perform the desired command. */
|
|
|
|
if (argc < 2)
|
|
|
|
throw Error("invalid arguments");
|
|
|
|
|
|
|
|
string command(argv[1]);
|
|
|
|
|
|
|
|
if (command == "run-builder") {
|
|
|
|
/* Syntax: nix-setuid-helper run-builder <username> <program>
|
2006-12-07 02:42:30 +02:00
|
|
|
<arg0 arg1...> */
|
2006-12-06 19:29:10 +02:00
|
|
|
if (argc < 4) throw Error("missing user name / program name");
|
2006-12-07 16:14:35 +02:00
|
|
|
runBuilder(uidNix, gidBuildUsers, buildUsers,
|
2006-12-07 02:42:30 +02:00
|
|
|
argv[2], argv[3], argc - 4, argv + 4, oldEnviron);
|
2006-12-06 19:29:10 +02:00
|
|
|
}
|
|
|
|
|
2006-12-07 16:14:35 +02:00
|
|
|
else if (command == "get-ownership") {
|
|
|
|
/* Syntax: nix-setuid-helper get-ownership <path> */
|
|
|
|
if (argc != 3) throw Error("missing path");
|
|
|
|
secureChown(-1, gidBuildUsers, uidNix, gidBuildUsers, argv[2]);
|
2006-12-06 19:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else throw Error ("invalid command");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-06 03:24:02 +02:00
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
2006-12-06 19:29:10 +02:00
|
|
|
try {
|
|
|
|
run(argc, argv);
|
|
|
|
} catch (Error & e) {
|
|
|
|
std::cerr << e.msg() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-12-06 03:24:02 +02:00
|
|
|
}
|