From owner-freebsd-bugs@FreeBSD.ORG Thu Aug 5 10:10:19 2004 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 82CA516A4CE for ; Thu, 5 Aug 2004 10:10:19 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6959A43D2D for ; Thu, 5 Aug 2004 10:10:19 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.11/8.12.11) with ESMTP id i75AAJR9054790 for ; Thu, 5 Aug 2004 10:10:19 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.11/8.12.11/Submit) id i75AAJW4054789; Thu, 5 Aug 2004 10:10:19 GMT (envelope-from gnats) Resent-Date: Thu, 5 Aug 2004 10:10:19 GMT Resent-Message-Id: <200408051010.i75AAJW4054789@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Dmitry Sivachenko Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C66E116A4CE for ; Thu, 5 Aug 2004 10:09:20 +0000 (GMT) Received: from dwarf.demos.su (dwarf.demos.ru [194.87.2.159]) by mx1.FreeBSD.org (Postfix) with ESMTP id F25C143D2D for ; Thu, 5 Aug 2004 10:09:19 +0000 (GMT) (envelope-from mitya@dwarf.demos.su) Received: by dwarf.demos.su (Postfix, from userid 1002) id 231558A001; Thu, 5 Aug 2004 14:09:18 +0400 (MSD) Message-Id: <20040805100918.231558A001@dwarf.demos.su> Date: Thu, 5 Aug 2004 14:09:18 +0400 (MSD) From: Dmitry Sivachenko To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Subject: bin/70024: jail(8) enhancement: run program in the clean environment X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Dmitry Sivachenko List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Aug 2004 10:10:19 -0000 >Number: 70024 >Category: bin >Synopsis: jail(8) enhancement: run program in the clean environment >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu Aug 05 10:10:19 GMT 2004 >Closed-Date: >Last-Modified: >Originator: Dmitry Sivachenko >Release: FreeBSD 5.2-CURRENT i386 >Organization: >Environment: System: FreeBSD dwarf.demos.su 5.2-CURRENT FreeBSD 5.2-CURRENT #1: Mon Aug 2 10:41:29 MSD 2004 azher@dwarf.demos.su:/mnt/ad0s2d/obj/mnt/ad0s2d/src/sys/DWARF i386 >Description: Add -l option to jail(8). Before running jail'ed program under specific user's credentials, clean the environment and set only a few variables. >How-To-Repeat: >Fix: --- /usr/src/usr.sbin/jail/jail.c Mon Jun 28 00:51:09 2004 +++ jail.c Thu Aug 5 13:40:10 2004 @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,7 @@ #include static void usage(void); +extern char **environ; #define GET_USER_INFO do { \ pwd = getpwnam(username); \ @@ -51,13 +53,15 @@ struct jail j; struct passwd *pwd; struct in_addr in; - int ch, groups[NGROUPS], i, iflag, ngroups, uflag, Uflag; + int ch, groups[NGROUPS], i, iflag, lflag, ngroups, uflag, Uflag; char path[PATH_MAX], *username; + static char *cleanenv; + const char *shell, *p; - iflag = uflag = Uflag = 0; - username = NULL; + iflag = lflag = uflag = Uflag = 0; + username = cleanenv = NULL; - while ((ch = getopt(argc, argv, "iu:U:")) != -1) { + while ((ch = getopt(argc, argv, "ilu:U:")) != -1) { switch (ch) { case 'i': iflag = 1; @@ -70,6 +74,9 @@ username = optarg; Uflag = 1; break; + case 'l': + lflag = 1; + break; default: usage(); } @@ -80,6 +87,8 @@ usage(); if (uflag && Uflag) usage(); + if (lflag && username == NULL) + usage(); if (uflag) GET_USER_INFO; if (realpath(argv[0], path) == NULL) @@ -103,6 +112,10 @@ if (username != NULL) { if (Uflag) GET_USER_INFO; + if (lflag) { + p = getenv("TERM"); + environ = &cleanenv; + } if (setgroups(ngroups, groups) != 0) err(1, "setgroups"); if (setgid(pwd->pw_gid) != 0) @@ -112,6 +125,19 @@ err(1, "setusercontext"); login_close(lcap); } + if (lflag) { + if (*pwd->pw_shell) + shell = pwd->pw_shell; + else + shell = _PATH_BSHELL; + if (chdir(pwd->pw_dir) < 0) + errx(1, "no home directory"); + setenv("HOME", pwd->pw_dir, 1); + setenv("SHELL", shell, 1); + setenv("USER", pwd->pw_name, 1); + if (p) + setenv("TERM", p, 1); + } if (execv(argv[3], argv + 3) != 0) err(1, "execv: %s", argv[3]); exit(0); @@ -122,7 +148,7 @@ { (void)fprintf(stderr, "%s%s\n", - "usage: jail [-i] [-u username | -U username]", + "usage: jail [-i] [-l -u username | -U username]", " path hostname ip-number command ..."); exit(1); } --- /usr/src/usr.sbin/jail/jail.8 Sun Jul 4 00:51:15 2004 +++ jail.8 Thu Aug 5 14:03:49 2004 @@ -42,7 +42,7 @@ .Sh SYNOPSIS .Nm .Op Fl i -.Op Fl u Ar username | Fl U Ar username +.Op Fl l Fl u Ar username | Fl U Ar username .Ar path hostname ip-number command ... .Sh DESCRIPTION The @@ -53,6 +53,24 @@ .Bl -tag -width ".Fl u Ar username" .It Fl i Output the jail identifier of the newly created jail. +.It Fl l +Run program in the clean environment. +The environment is discarded except for +.Ev HOME , +.Ev SHELL , +.Ev TERM +and +.Ev USER . +.Ev HOME +and +.Ev SHELL +are set to the target login's default values. +.Ev USER +is set to the target login. +.Ev TERM +is imported from your current environment. +The environment variables from the login class capability database for the +target login are also set. .It Fl u Ar username The user name from host environment as whom the .Ar command >Release-Note: >Audit-Trail: >Unformatted: