Skip to content

Commit c7dd18c

Browse files
committed
cmdline: add ability to passthrough current envvars
1 parent 25004a5 commit c7dd18c

10 files changed

+27
-11
lines changed

cmdline.cc

+15-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct custom_option custom_opts[] = {
9393
{ { "quiet", no_argument, NULL, 'q' }, "Log warning and more important messages only" },
9494
{ { "really_quiet", no_argument, NULL, 'Q' }, "Log fatal messages only" },
9595
{ { "keep_env", no_argument, NULL, 'e' }, "Pass all environment variables to the child process (default: all envvars are cleared)" },
96-
{ { "env", required_argument, NULL, 'E' }, "Additional environment variable (can be used multiple times)" },
96+
{ { "env", required_argument, NULL, 'E' }, "Additional environment variable (can be used multiple times). If the envvar doesn't contain '=' (e.g. just the 'DISPLAY' string), the current envvar value will be used" },
9797
{ { "keep_caps", no_argument, NULL, 0x0501 }, "Don't drop any capabilities" },
9898
{ { "cap", required_argument, NULL, 0x0509 }, "Retain this capability, e.g. CAP_PTRACE (can be specified multiple times)" },
9999
{ { "silent", no_argument, NULL, 0x0502 }, "Redirect child process' fd:0/1/2 to /dev/null" },
@@ -187,6 +187,19 @@ static void cmdlineUsage(const char* pname) {
187187
LOG_HELP_BOLD(" nsjail -Me --chroot / --disable_proc -- /bin/echo \"ABC\"");
188188
}
189189

190+
void addEnv(nsjconf_t* nsjconf, const std::string& env) {
191+
if (env.find('=') != std::string::npos) {
192+
nsjconf->envs.push_back(env);
193+
return;
194+
}
195+
char* e = getenv(env.c_str());
196+
if (!e) {
197+
nsjconf->envs.push_back(env);
198+
return;
199+
}
200+
nsjconf->envs.push_back(std::string(env).append("=").append(e));
201+
}
202+
190203
void logParams(nsjconf_t* nsjconf) {
191204
switch (nsjconf->mode) {
192205
case MODE_LISTEN_TCP:
@@ -606,7 +619,7 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
606619
nsjconf->use_execveat = true;
607620
break;
608621
case 'E':
609-
nsjconf->envs.push_back(optarg);
622+
addEnv(nsjconf.get(), optarg);
610623
break;
611624
case 'u': {
612625
std::vector<std::string> subopts = util::strSplit(optarg, ':');

cmdline.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
#include <stdint.h>
2626

2727
#include <memory>
28+
#include <string>
2829

2930
#include "nsjail.h"
3031

3132
namespace cmdline {
3233

3334
uint64_t parseRLimit(int res, const char* optarg, unsigned long mul);
3435
void logParams(nsjconf_t* nsjconf);
36+
void addEnv(nsjconf_t* nsjconf, const std::string& env);
3537
std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]);
3638

3739
} // namespace cmdline

config.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig&
125125

126126
nsjconf->keep_env = njc.keep_env();
127127
for (ssize_t i = 0; i < njc.envar_size(); i++) {
128-
nsjconf->envs.push_back(njc.envar(i));
128+
cmdline::addEnv(nsjconf, njc.envar(i));
129129
}
130130

131131
nsjconf->keep_caps = njc.keep_caps();

config.proto

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ message NsJailConfig {
115115
/* Should the current environment variables be kept
116116
when executing the binary */
117117
optional bool keep_env = 19 [default = false];
118-
/* EnvVars to be set before executing binaries */
118+
/* EnvVars to be set before executing binaries. If the envvar doesn't contain '='
119+
(e.g. just the 'DISPLAY' string), the current envvar value will be used */
119120
repeated string envar = 20;
120121

121122
/* Should capabilities be preserved or dropped */

configs/demo-dont-use-chrome-with-net.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ cwd: "/user"
2828
time_limit: 0
2929

3030
envar: "HOME=/user"
31-
envar: "DISPLAY=:0"
31+
envar: "DISPLAY"
3232
envar: "TMP=/tmp"
3333

3434
rlimit_as: 4096

configs/firefox-with-cloned-net.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ cwd: "/user"
2828
time_limit: 0
2929

3030
envar: "HOME=/user"
31-
envar: "DISPLAY=:0"
31+
envar: "DISPLAY"
3232
envar: "TMP=/tmp"
3333

3434
rlimit_as: 4096

configs/firefox-with-net.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ time_limit: 0
2222
clone_newnet: false
2323

2424
envar: "HOME=/user"
25-
envar: "DISPLAY=:0"
25+
envar: "DISPLAY"
2626
envar: "TMP=/tmp"
2727

2828
rlimit_as: 4096

configs/home-documents-with-xorg-no-net.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cwd: "/user"
1414

1515
time_limit: 1000
1616

17-
envar: "DISPLAY=:0"
17+
envar: "DISPLAY"
1818
envar: "HOME=/user"
1919
envar: "TMP=/tmp"
2020

configs/xchat-with-net.cfg

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ cwd: "/user"
1616
time_limit: 0
1717

1818
envar: "HOME=/user"
19-
envar: "DISPLAY=:0"
19+
envar: "DISPLAY"
2020
envar: "TMP=/tmp"
2121
envar: "FONTCONFIG_FILE=/etc/fonts/fonts.conf"
2222
envar: "FC_CONFIG_FILE=/etc/fonts/fonts.conf"
23-
envar: "LANG=en_US.UTF-8"
23+
envar: "LANG"
2424

2525
rlimit_as: 4096
2626
rlimit_cpu_type: INF

nsjail.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Log fatal messages only
9292
Pass all environment variables be passed process (default: all envvars are cleared)
9393
.TP
9494
\fB\-\-env\fR|\fB\-E\fR VALUE
95-
Additional environment variable (can be used multiple times)
95+
Additional environment variable (can be used multiple times). If the envvar doesn't contain '=' (e.g. just the 'DISPLAY' string), the current envvar value will be used
9696
.TP
9797
\fB\-\-keep_caps\fR
9898
Don't drop any capabilities

0 commit comments

Comments
 (0)