Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3593)

Unified Diff: chromeos/process.cc

Issue 6865041: libchromeos: Support setting uid/gid of child processes in process.h (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/common.git@master
Patch Set: improve comments Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/process.h ('k') | chromeos/process_mock.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/process.cc
diff --git a/chromeos/process.cc b/chromeos/process.cc
index 54d9a9ff8462ba80b843ee8c9e1f449489f289d6..ff044ba74991bfcf51bcf0d8ea17f43ae832daaa 100644
--- a/chromeos/process.cc
+++ b/chromeos/process.cc
@@ -8,6 +8,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
+#include <unistd.h>
#include <map>
@@ -30,7 +31,7 @@ bool Process::ProcessExists(pid_t pid) {
return file_util::DirectoryExists(FilePath(StringPrintf("/proc/%d", pid)));
}
-ProcessImpl::ProcessImpl() : pid_(0) {
+ProcessImpl::ProcessImpl() : pid_(0), uid_(-1), gid_(-1) {
}
ProcessImpl::~ProcessImpl() {
@@ -51,6 +52,14 @@ void ProcessImpl::RedirectUsingPipe(int child_fd, bool is_input) {
pipe_map_[child_fd] = info;
}
+void ProcessImpl::SetUid(uid_t uid) {
+ uid_ = uid;
+}
+
+void ProcessImpl::SetGid(gid_t gid) {
+ gid_ = gid;
+}
+
int ProcessImpl::GetPipe(int child_fd) {
PipeMap::iterator i = pipe_map_.find(child_fd);
if (i == pipe_map_.end())
@@ -144,7 +153,7 @@ bool ProcessImpl::Start() {
LOG(ERROR) << "Could not create " << output_file_
<< ": " << saved_errno;
// Avoid exit() to avoid atexit handlers from parent.
- _exit(127);
+ _exit(kErrorExitStatus);
}
HANDLE_EINTR(dup2(output_handle, STDOUT_FILENO));
HANDLE_EINTR(dup2(output_handle, STDERR_FILENO));
@@ -154,10 +163,20 @@ bool ProcessImpl::Start() {
HANDLE_EINTR(close(output_handle));
}
}
+ if (uid_ >= 0 && setresuid(uid_, uid_, uid_) < 0) {
+ int saved_errno = errno;
+ LOG(ERROR) << "Unable to set UID to " << uid_ << ": " << saved_errno;
+ _exit(kErrorExitStatus);
+ }
+ if (gid_ >= 0 && setresgid(gid_, gid_, gid_) < 0) {
+ int saved_errno = errno;
+ LOG(ERROR) << "Unable to set GID to " << gid_ << ": " << saved_errno;
+ _exit(kErrorExitStatus);
+ }
execv(argv[0], &argv[0]);
saved_errno = errno;
LOG(ERROR) << "Exec of " << argv[0] << " failed: " << saved_errno;
- _exit(127);
+ _exit(kErrorExitStatus);
} else {
// Still executing inside the parent process with known child pid.
arguments_.clear();
« no previous file with comments | « chromeos/process.h ('k') | chromeos/process_mock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698