git: 1ff3118d72b1 - stable/14 - MFC jail: only chdir to user's home directory when user is specified

From: Jamie Gritton <jamie_at_FreeBSD.org>
Date: Fri, 16 Aug 2024 17:12:40 UTC
The branch stable/14 has been updated by jamie:

URL: https://cgit.FreeBSD.org/src/commit/?id=1ff3118d72b15fb4c02ee156a5073e3e40528587

commit 1ff3118d72b15fb4c02ee156a5073e3e40528587
Author:     Jamie Gritton <jamie@FreeBSD.org>
AuthorDate: 2024-08-12 22:23:28 +0000
Commit:     Jamie Gritton <jamie@FreeBSD.org>
CommitDate: 2024-08-16 17:12:24 +0000

    MFC jail: only chdir to user's home directory when user is specified
    
    jail(8) with the "exec.clean" parameter not only cleans the enviromnent
    variables before running commands, but also changes to the user's home
    directory.  While this makes sense when auser is specified (via one of
    the exec.*_user parameters), it leads to all commands being run in the
    jail's /root directory even in the absence of an explicitly specified
    user.  This can lead to problems when e.g. rc scripts are run from that
    non-world-readable directory, and run counter to expectations that jail
    startup is analogous to system startup.
    
    Restrict this behvaiour to only users exlicitly specified, either via
    the command line or jail parameters, but not the implicit root user.
    While this changes long-stand practice, it's the more intuitive action.
    
    jexec(8) has the same problem, and the same fix.
    
    PR:             277210
    Reported by:    johannes.kunde at gmail
    Differential Revision:  https://reviews.freebsd.org/D46226
    
    (cherry picked from commit 5cf705491727dd963485f9911ee3d52c3bf148db)
---
 usr.sbin/jail/command.c | 2 +-
 usr.sbin/jail/jail.8    | 7 ++++++-
 usr.sbin/jexec/jexec.8  | 7 ++++++-
 usr.sbin/jexec/jexec.c  | 2 +-
 4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/usr.sbin/jail/command.c b/usr.sbin/jail/command.c
index 9eabcc5ff53c..9004b4729fec 100644
--- a/usr.sbin/jail/command.c
+++ b/usr.sbin/jail/command.c
@@ -764,7 +764,7 @@ run_command(struct cfjail *j)
 		setenv("HOME", pwd->pw_dir, 1);
 		setenv("SHELL",
 		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
-		if (clean && chdir(pwd->pw_dir) < 0) {
+		if (clean && username && chdir(pwd->pw_dir) < 0) {
 			jail_warnx(j, "chdir %s: %s",
 			    pwd->pw_dir, strerror(errno));
 			exit(1);
diff --git a/usr.sbin/jail/jail.8 b/usr.sbin/jail/jail.8
index 3a6e46b065b2..75a0b86eed4c 100644
--- a/usr.sbin/jail/jail.8
+++ b/usr.sbin/jail/jail.8
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd June 24, 2024
+.Dd August 12, 2024
 .Dt JAIL 8
 .Os
 .Sh NAME
@@ -847,8 +847,13 @@ are set to the target login's default values.
 is set to the target login.
 .Ev TERM
 is imported from the current environment.
+.Ev PATH
+is set to "/bin:/usr/bin".
 The environment variables from the login class capability database for the
 target login are also set.
+If a user is specified (as with
+.Va exec.jail_user ) ,
+commands are run from that (possibly jailed) user's directory.
 .It Va exec.jail_user
 The user to run commands as, when running in the jail environment.
 The default is to run the commands as the current user.
diff --git a/usr.sbin/jexec/jexec.8 b/usr.sbin/jexec/jexec.8
index 4400cbbe56a3..431978c4d0ae 100644
--- a/usr.sbin/jexec/jexec.8
+++ b/usr.sbin/jexec/jexec.8
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd October 7, 2023
+.Dd August 12, 2024
 .Dt JEXEC 8
 .Os
 .Sh NAME
@@ -55,6 +55,11 @@ The environment is discarded except for
 and anything from the login class capability database for the user.
 .Ev PATH
 is set to "/bin:/usr/bin".
+If a user is specified (via
+.Fl u
+or
+.Fl U ) ,
+commands are run from that (possibly jailed) user's directory.
 .It Fl u Ar username
 The user name from host environment as whom the
 .Ar command
diff --git a/usr.sbin/jexec/jexec.c b/usr.sbin/jexec/jexec.c
index 7a32efa34031..35fd9c8d20e4 100644
--- a/usr.sbin/jexec/jexec.c
+++ b/usr.sbin/jexec/jexec.c
@@ -129,7 +129,7 @@ main(int argc, char *argv[])
 		setenv("HOME", pwd->pw_dir, 1);
 		setenv("SHELL",
 		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
-		if (clean && chdir(pwd->pw_dir) < 0)
+		if (clean && username && chdir(pwd->pw_dir) < 0)
 			err(1, "chdir: %s", pwd->pw_dir);
 		endpwent();
 	}