Skip to content
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

jail: add JID, JNAME and JPATH to environment for exec.* commands #1609

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion usr.sbin/jail/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ run_command(struct cfjail *j)
const struct cfstring *comstring, *s;
login_cap_t *lcap;
const char **argv;
char *acs, *cs, *comcs, *devpath;
char *acs, *ajidstr, *cs, *comcs, *devpath;
const char *jidstr, *conslog, *fmt, *path, *ruleset, *term, *username;
enum intparam comparam;
size_t comlen, ret;
Expand Down Expand Up @@ -795,6 +795,18 @@ run_command(struct cfjail *j)
}
endpwent();
}
if (!injail) {
if (asprintf(&ajidstr, "%d", j->jid) == -1) {
jail_warnx(j, "asprintf jid=%d: %s", j->jid,
strerror(errno));
exit(1);
}
setenv("JID", ajidstr, 1);
free(ajidstr);
setenv("JNAME", string_param(j->intparams[KP_NAME]), 1);
path = string_param(j->intparams[KP_PATH]);
setenv("JPATH", path ? path : "", 1);
}

if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
jail_warnx(j, "exec.consolelog: %s", strerror(errno));
Expand Down
20 changes: 20 additions & 0 deletions usr.sbin/jail/jail.8
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,22 @@ commands in sequence.
All commands must succeed (return a zero exit status), or the jail will
not be created or removed, as appropriate.
.Pp
The following variables are added to the environment:
.Bl -tag -width indent -offset indent
.It Ev JID
The
.Va jid
, or jail identifier.
.It Ev JNAME
The
.Va name
of the jail.
.It Ev JPATH
The
.Va path
of the jail.
.El
.Pp
The pseudo-parameters are:
.Bl -tag -width indent
.It Va exec.prepare
Expand Down Expand Up @@ -883,6 +899,10 @@ is imported from the current environment.
is set to "/bin:/usr/bin".
The environment variables from the login class capability database for the
target login are also set.
.Ev JID , JNAME
and
.Ev JPATH
are not set.
If a user is specified (as with
.Va exec.jail_user ) ,
commands are run from that (possibly jailed) user's directory.
Expand Down
3 changes: 3 additions & 0 deletions usr.sbin/jail/tests/commands.jail.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

exec.prestop = "echo STOP";
exec.prestart = "echo START";
exec.poststart = "env";
persist;

path = "/tmp/test_${name}_root";

basejail {}
11 changes: 9 additions & 2 deletions usr.sbin/jail/tests/jail_basic_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,19 @@ commands_head()
{
atf_set descr 'Commands jail test'
atf_set require.user root
mkdir /tmp/test_basejail_root
}

commands_body()
{
# exec.prestart
atf_check -s exit:0 -o inline:"START\n" \
# exec.prestart (START) and exec.poststart (env)
atf_check -s exit:0 -o save:stdout -e empty \
jail -f $(atf_get_srcdir)/commands.jail.conf -qc basejail
grep -E '^START$' stdout || atf_fail "exec.prestart output not found"
grep -E '^JID=[0-9]+' stdout || atf_fail "JID not found in exec.poststart env output"
grep -E '^JNAME=basejail$' stdout || atf_fail "JNAME not found in exec.poststart env output"
grep -E '^JPATH=/tmp/test_basejail_root$' stdout || atf_fail "JPATH not found in exec.poststart env output"

# exec.prestop by jailname
atf_check -s exit:0 -o inline:"STOP\n" \
jail -f $(atf_get_srcdir)/commands.jail.conf -qr basejail
Expand All @@ -152,6 +158,7 @@ commands_cleanup()
then
jail -r basejail
fi
rmdir /tmp/test_basejail_root
}

atf_init_test_cases()
Expand Down
Loading