Make rdcd run as user "rdc"

The rdc account will be created on installation if it does
not already exist. It will be a system account with no
home directory.

rdcd will be started as a systemd service, but change to
user "rdc". The rdc user will drop all priviliges except
CAP_DAC_OVERRIDE, permitted. This means the default mode
will have no special privileges, but have the ability to
gain write access (e.g., to sysfs) when needed.

rdc tests were being inadvertantly added to the
installation. This was adversely impacting the new
functionality, so it was corrected in this commit.

Also included are a few small formatting changes.

Change-Id: I9c6bb132fee28119fd3960594dfb97bd2e7b282a
This commit is contained in:
Chris Freehill
2020-01-27 19:08:09 -06:00
والد 4729c47866
کامیت 5cc498c6aa
17فایلهای تغییر یافته به همراه303 افزوده شده و 112 حذف شده
@@ -51,5 +51,5 @@ RDCAdminServiceImpl::VerifyConnection(::grpc::ServerContext* context,
return ::grpc::Status::OK;
}
} // rdc
} // amd
} // namespace rdc
} // namespace amd
+57 -21
مشاهده پرونده
@@ -28,6 +28,7 @@ THE SOFTWARE.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/capability.h>
#include <iostream>
#include <memory>
@@ -38,6 +39,7 @@ THE SOFTWARE.
#include "rocm_smi/rocm_smi.h"
#include "rdc/rdc_server_main.h"
#include "rdc/rdc_rsmi_service.h"
#include "rdc/rdc_server_utils.h"
static bool sShutDownServer = false;
static bool sRestartServer = false;
@@ -88,13 +90,13 @@ RDCServer::Run() {
// std::unique_ptr<::grpc::Server> server(builder.BuildAndStart());
server_ = builder.BuildAndStart();
std::cerr << "Server listening on " << server_address_.c_str() << std::endl;
std::cout << "Server listening on " << server_address_.c_str() << std::endl;
server_->Wait();
}
static void HandleSignal(int sig) {
std::cerr << "Caught signal " << sig << std::endl;
std::cout << "Caught signal " << sig << std::endl;
// For most signals, we will want to exit, so make that the default case
// Handle the other signals specifically.
@@ -142,13 +144,13 @@ static void * ProcessSignalLoop(void *server_ptr) {
while (1) {
if (sShutDownServer) {
std::cerr << "Shutting down RDC Server." << std::endl;
std::cout << "Shutting down RDC Server." << std::endl;
server->ShutDown();
// We will need to add shutdown of any completion queues
// here, when/if we add them
break;
} else if (sRestartServer) {
std::cerr << "Re-starting RDC Server." << std::endl;
std::cout << "Re-starting RDC Server." << std::endl;
// We will need to add shutdown of any completion queues
// here, when/if we add them
server->ShutDown();
@@ -184,7 +186,7 @@ static void ExitIfAlreadyRunning(void) {
static void
MakeDaemon() {
int fd0, fd1, fd2;
int fd0;
struct rlimit max_files;
// RSMI, for one thing, will need to be able to read/write files
@@ -230,21 +232,15 @@ MakeDaemon() {
if (max_files.rlim_max > 1024) {
max_files.rlim_max = 1024;
}
for (uint32_t i = 0; i < max_files.rlim_max; i++) {
close(0); // close stdin; leave stdout and stderr open
for (uint32_t i = 3; i < max_files.rlim_max; i++) {
close(i);
}
// Direct stdin, stdout, stdout to /dev/null.
// Direct stdin to /dev/null.
fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
// Set up log file
// openlog(kDaemonName, LOG_CONS|LOG_PID, LOG_DAEMON);
if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
std::cerr << "unexpected fildes: " << fd0 << " " << fd1 <<
" " << fd2 << std::endl;
if (fd0 != 0) {
std::cerr << "unexpected fildes: " << fd0 << std::endl;
exit(1);
}
@@ -255,6 +251,7 @@ MakeDaemon() {
int main(int argc, char** argv) {
RDCServer rdc_server;
int err;
(void)argc; // Ignore for now
(void)argv;
@@ -263,6 +260,49 @@ int main(int argc, char** argv) {
rdc_server.Initialize();
bool cap_enabled;
err = GetCapability(CAP_DAC_OVERRIDE, CAP_EFFECTIVE, &cap_enabled);
if (err) {
std::cerr << "Failed to get capability" << std::endl;
return 1;
}
if (!cap_enabled) {
std::cerr <<
"Expected CAP_DAC_OVERRIDE CAP_EFFECTIVE to be enabled, but it not." <<
std::endl;
return 1;
}
err = GetCapability(CAP_DAC_OVERRIDE, CAP_PERMITTED, &cap_enabled);
if (err) {
std::cerr << "Failed to get capability" << std::endl;
return 1;
}
if (!cap_enabled) {
std::cerr <<
"Expected CAP_DAC_OVERRIDE CAP_PERMITTED to be enabled, but it not." <<
std::endl;
return 1;
}
// Don't allow rwx access to all files to ever be inheritable. We may need
// relax this restriction if some new feature requires it.
err = ModifyCapability(CAP_DAC_OVERRIDE, CAP_INHERITABLE, false);
if (err) {
std::cerr << "Failed to disable CAP_DAC_OVERRIDE, CAP_INHERITABLE" <<
std::endl;
return 1;
}
// By default, disable CAP_DAC_OVERRIDE. Turn on, when needed.
err = ModifyCapability(CAP_DAC_OVERRIDE, CAP_EFFECTIVE, false);
if (err) {
std::cerr << "Failed to disable CAP_DAC_OVERRIDE, CAP_EFFECTIVE" <<
std::endl;
return 1;
}
// Create a thread to handle signals to shutdown gracefully
pthread_t sig_listen_thread;
int thr_ret = pthread_create(&sig_listen_thread, NULL,
@@ -272,7 +312,6 @@ int main(int argc, char** argv) {
std::cerr <<
"Failed to create ProcessSignalLoop. pthread_create() returned " <<
thr_ret;
return 1;
}
@@ -280,13 +319,10 @@ int main(int argc, char** argv) {
rdc_server.set_start_rsmi_service(true);
rdc_server.set_start_rdc_admin_service(true);
// rdc_server.set_secure_communications(false);
// rdc_server.set_address("0.0.0.0:50051")
rdc_server.Run();
if (sShutDownServer) {
std::cerr << "RDC server successfully shut down." << std::endl;
std::cout << "RDC server successfully shut down." << std::endl;
return 0;
} else {
std::cerr << "RDC server failed to start." << std::endl;
+89
مشاهده پرونده
@@ -0,0 +1,89 @@
/*
Copyright (c) 2020 - present Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <sys/capability.h>
#include <errno.h>
#include <assert.h>
int GetCapability(cap_value_t cap, cap_flag_t cap_type, bool *enabled) {
cap_t caps;
assert(enabled != nullptr);
if (enabled == nullptr) {
return -1;
}
// Get process's current capabilities
caps = cap_get_proc();
if (caps == nullptr) {
return errno;
}
cap_flag_value_t val;
if (cap_get_flag(caps, cap, cap_type, &val) == -1) {
int ret = errno;
cap_free(caps);
return ret;
}
if (cap_free(caps) == -1) {
return errno;
}
*enabled = (val == CAP_SET ? true : false);
return 0;
}
// !enable means disable;
int ModifyCapability(cap_value_t cap, cap_flag_t cap_type, bool enable) {
cap_t caps;
cap_value_t cap_list[1];
// Get process's current capabilities
caps = cap_get_proc();
if (caps == nullptr) {
return errno;
}
// the 1 in the call below is the size of the cap_list array
cap_list[0] = cap;
if (cap_set_flag(caps, cap_type, 1, cap_list, enable ? CAP_SET : CAP_CLEAR)
== -1) {
int ret = errno;
cap_free(caps);
return ret;
}
if (cap_set_proc(caps) == -1) {
int ret = errno;
cap_free(caps);
return ret;
}
if (cap_free(caps) == -1) {
return errno;
}
return 0;
}