Skip to content

Add the --chdir option #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/sio2jail.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ of the hardware sio2jail runs on.
Write the execution report to file descriptor _fd_,
instead of stderr.

*-c* _dir_, *--chdir* _dir_
Change the working directory to _dir_ before running the program.

*-s, --stderr*
Pass stderr from the sandboxed program,
instead of redirecting it to stderr.
Expand Down
10 changes: 10 additions & 0 deletions src/executor/Executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ namespace executor {
Executor::Executor(
std::string childProgramName,
std::vector<std::string> childProgramArgv,
std::string childProgramWorkingDir,
bool supportThreads)
: childProgramName_(std::move(childProgramName))
, childProgramArgv_(std::move(childProgramArgv))
, childProgramWorkingDir_(std::move(childProgramWorkingDir))
, childPid_(0)
, supportThreads_{supportThreads} {}

Expand All @@ -68,6 +70,14 @@ void Executor::executeChild() {
listener->onPostForkChild();
}

// "" is the default value
if (!childProgramWorkingDir_.empty()) {
withErrnoCheck(
"chdir to " + childProgramWorkingDir_,
chdir,
childProgramWorkingDir_.c_str());
}

// Create plain C arrays with program arguments
char* programName = stringToCStr(childProgramName_);
char** programArgv = new char*[childProgramArgv_.size() + 2];
Expand Down
2 changes: 2 additions & 0 deletions src/executor/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Executor
Executor(
std::string childProgramName,
std::vector<std::string> childProgramArgv,
std::string childProgramWorkingDir,
bool supportThreads = false);

template<typename ProgramNameType>
Expand All @@ -45,6 +46,7 @@ class Executor

std::string childProgramName_;
std::vector<std::string> childProgramArgv_;
std::string childProgramWorkingDir_;

pid_t childPid_;
const bool supportThreads_;
Expand Down
1 change: 1 addition & 0 deletions src/s2japp/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Application::ExitCode Application::handleRun() {
auto executor = std::make_shared<s2j::executor::Executor>(
settings_.programName,
settings_.programArgv,
settings_.programWorkingDir,
settings_.threadsLimit >= 0);

auto traceExecutor = createListener<tracer::TraceExecutor>();
Expand Down
10 changes: 10 additions & 0 deletions src/s2japp/ApplicationSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ ApplicationSettings::ApplicationSettings(int argc, const char* argv[])
cmd,
false);

TCLAP::ValueArg<std::string> argProgramWorkingDir(
"c",
"chdir",
"Where to chdir to before running the program",
false,
"",
"dir",
cmd);

TCLAP::ValueArg<std::string> argLoggerPath(
"l",
"log",
Expand Down Expand Up @@ -365,6 +374,7 @@ ApplicationSettings::ApplicationSettings(int argc, const char* argv[])

programName = argProgramName.getValue();
programArgv = argProgramArgv.getValue();
programWorkingDir = argProgramWorkingDir.getValue();

outputBuilderFactory = argOutputFormat.getValue().getFactory();
syscallPolicyFactory = argSyscallPolicy.getValue().getFactory();
Expand Down
1 change: 1 addition & 0 deletions src/s2japp/ApplicationSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct ApplicationSettings : public ns::MountNamespaceListener::Settings {

std::string programName;
std::vector<std::string> programArgv;
std::string programWorkingDir;

Factory<s2j::printer::OutputBuilder> outputBuilderFactory;
Factory<s2j::seccomp::policy::BaseSyscallPolicy> syscallPolicyFactory;
Expand Down