|
|
ead27a |
From 0150f21be9739ad3fc8d5ce7cee2a2ff4a09326f Mon Sep 17 00:00:00 2001
|
|
|
ead27a |
From: Karel Zak <kzak@redhat.com>
|
|
|
ead27a |
Date: Wed, 22 Aug 2018 11:43:32 +0200
|
|
|
ead27a |
Subject: [PATCH] setpriv: add --reset-env
|
|
|
ead27a |
|
|
|
ead27a |
Clear environment in way like su(1), but PATH is set to hard-coded
|
|
|
ead27a |
defaults and /etc/login.defs is not used at all (I guess we want to
|
|
|
ead27a |
keep setpriv(1) simple).
|
|
|
ead27a |
|
|
|
ead27a |
If you need anything more advanced than use env(1).
|
|
|
ead27a |
|
|
|
ead27a |
Addresses: https://github.com/karelzak/util-linux/issues/325
|
|
|
ead27a |
Signed-off-by: Karel Zak <kzak@redhat.com>
|
|
|
ead27a |
---
|
|
|
ead27a |
sys-utils/setpriv.1 | 9 ++++++++
|
|
|
ead27a |
sys-utils/setpriv.c | 54 ++++++++++++++++++++++++++++++++++++++++++++-
|
|
|
ead27a |
2 files changed, 62 insertions(+), 1 deletion(-)
|
|
|
ead27a |
|
|
|
ead27a |
diff --git a/sys-utils/setpriv.1 b/sys-utils/setpriv.1
|
|
|
ead27a |
index b900f6e08..45bc5a23b 100644
|
|
|
ead27a |
--- a/sys-utils/setpriv.1
|
|
|
ead27a |
+++ b/sys-utils/setpriv.1
|
|
|
ead27a |
@@ -159,6 +159,15 @@ to abort if AppArmor is not in use, and the transition may be ignored or cause
|
|
|
ead27a |
.BR execve (2)
|
|
|
ead27a |
to fail at AppArmor's whim.
|
|
|
ead27a |
.TP
|
|
|
ead27a |
+.BI \-\-reset\-env
|
|
|
ead27a |
+Clears all the environment variables except TERM; initializes the environment variables HOME, SHELL, USER, LOGNAME
|
|
|
ead27a |
+according to the user's passwd entry; sets PATH to \fI/usr/local/bin:/bin:/usr/bin\fR for a regual user and to
|
|
|
ead27a |
+\fI/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\fR for root.
|
|
|
ead27a |
+.sp
|
|
|
ead27a |
+The environment variable PATH may be different on systems where /bin and /sbin
|
|
|
ead27a |
+are merged into /usr. The environment variable SHELL defaults to \fI/bin/sh\fR if none is given in the user's
|
|
|
ead27a |
+passwd entry.
|
|
|
ead27a |
+.TP
|
|
|
ead27a |
.BR \-V , " \-\-version"
|
|
|
ead27a |
Display version information and exit.
|
|
|
ead27a |
.TP
|
|
|
ead27a |
diff --git a/sys-utils/setpriv.c b/sys-utils/setpriv.c
|
|
|
ead27a |
index 4147978cc..30f8f9b15 100644
|
|
|
ead27a |
--- a/sys-utils/setpriv.c
|
|
|
ead27a |
+++ b/sys-utils/setpriv.c
|
|
|
ead27a |
@@ -38,6 +38,7 @@
|
|
|
ead27a |
#include "strutils.h"
|
|
|
ead27a |
#include "xalloc.h"
|
|
|
ead27a |
#include "pathnames.h"
|
|
|
ead27a |
+#include "env.h"
|
|
|
ead27a |
|
|
|
ead27a |
#ifndef PR_SET_NO_NEW_PRIVS
|
|
|
ead27a |
# define PR_SET_NO_NEW_PRIVS 38
|
|
|
ead27a |
@@ -55,6 +56,9 @@
|
|
|
ead27a |
|
|
|
ead27a |
#define SETPRIV_EXIT_PRIVERR 127 /* how we exit when we fail to set privs */
|
|
|
ead27a |
|
|
|
ead27a |
+/* The shell to set SHELL env.variable if none is given in the user's passwd entry. */
|
|
|
ead27a |
+#define DEFAULT_SHELL "/bin/sh"
|
|
|
ead27a |
+
|
|
|
ead27a |
enum cap_type {
|
|
|
ead27a |
CAP_TYPE_EFFECTIVE = CAPNG_EFFECTIVE,
|
|
|
ead27a |
CAP_TYPE_PERMITTED = CAPNG_PERMITTED,
|
|
|
ead27a |
@@ -82,6 +86,7 @@ struct privctx {
|
|
|
ead27a |
keep_groups:1, /* keep groups */
|
|
|
ead27a |
clear_groups:1, /* remove groups */
|
|
|
ead27a |
init_groups:1, /* initialize groups */
|
|
|
ead27a |
+ reset_env:1, /* reset environment */
|
|
|
ead27a |
have_securebits:1; /* remove groups */
|
|
|
ead27a |
|
|
|
ead27a |
/* uids and gids */
|
|
|
ead27a |
@@ -137,6 +142,8 @@ static void __attribute__((__noreturn__)) usage(void)
|
|
|
ead27a |
fputs(_(" --securebits <bits> set securebits\n"), out);
|
|
|
ead27a |
fputs(_(" --selinux-label <label> set SELinux label\n"), out);
|
|
|
ead27a |
fputs(_(" --apparmor-profile <pr> set AppArmor profile\n"), out);
|
|
|
ead27a |
+ fputs(_(" --reset-env clear all environment and initialize\n"
|
|
|
ead27a |
+ " HOME, SHELL, USER, LOGNAME and PATH\n"), out);
|
|
|
ead27a |
|
|
|
ead27a |
fputs(USAGE_SEPARATOR, out);
|
|
|
ead27a |
printf(USAGE_HELP_OPTIONS(29));
|
|
|
ead27a |
@@ -643,6 +650,36 @@ static void do_apparmor_profile(const char *label)
|
|
|
ead27a |
_("write failed: %s"), _PATH_PROC_ATTR_EXEC);
|
|
|
ead27a |
}
|
|
|
ead27a |
|
|
|
ead27a |
+
|
|
|
ead27a |
+static void do_reset_environ(struct passwd *pw)
|
|
|
ead27a |
+{
|
|
|
ead27a |
+ char *term = getenv("TERM");
|
|
|
ead27a |
+
|
|
|
ead27a |
+ if (term)
|
|
|
ead27a |
+ term = xstrdup(term);
|
|
|
ead27a |
+#ifdef HAVE_CLEARENV
|
|
|
ead27a |
+ clearenv();
|
|
|
ead27a |
+#else
|
|
|
ead27a |
+ environ = NULL;
|
|
|
ead27a |
+#endif
|
|
|
ead27a |
+ if (term)
|
|
|
ead27a |
+ xsetenv("TERM", term, 1);
|
|
|
ead27a |
+
|
|
|
ead27a |
+ if (pw->pw_shell && *pw->pw_shell)
|
|
|
ead27a |
+ xsetenv("SHELL", pw->pw_shell, 1);
|
|
|
ead27a |
+ else
|
|
|
ead27a |
+ xsetenv("SHELL", DEFAULT_SHELL, 1);
|
|
|
ead27a |
+
|
|
|
ead27a |
+ xsetenv("HOME", pw->pw_dir, 1);
|
|
|
ead27a |
+ xsetenv("USER", pw->pw_name, 1);
|
|
|
ead27a |
+ xsetenv("LOGNAME", pw->pw_name, 1);
|
|
|
ead27a |
+
|
|
|
ead27a |
+ if (pw->pw_uid)
|
|
|
ead27a |
+ xsetenv("PATH", _PATH_DEFPATH, 1);
|
|
|
ead27a |
+ else
|
|
|
ead27a |
+ xsetenv("PATH", _PATH_DEFPATH_ROOT, 1);
|
|
|
ead27a |
+}
|
|
|
ead27a |
+
|
|
|
ead27a |
static uid_t get_user(const char *s, const char *err)
|
|
|
ead27a |
{
|
|
|
ead27a |
struct passwd *pw;
|
|
|
ead27a |
@@ -712,7 +749,8 @@ int main(int argc, char **argv)
|
|
|
ead27a |
CAPBSET,
|
|
|
ead27a |
SECUREBITS,
|
|
|
ead27a |
SELINUX_LABEL,
|
|
|
ead27a |
- APPARMOR_PROFILE
|
|
|
ead27a |
+ APPARMOR_PROFILE,
|
|
|
ead27a |
+ RESET_ENV
|
|
|
ead27a |
};
|
|
|
ead27a |
|
|
|
ead27a |
static const struct option longopts[] = {
|
|
|
ead27a |
@@ -737,6 +775,7 @@ int main(int argc, char **argv)
|
|
|
ead27a |
{ "selinux-label", required_argument, NULL, SELINUX_LABEL },
|
|
|
ead27a |
{ "apparmor-profile", required_argument, NULL, APPARMOR_PROFILE },
|
|
|
ead27a |
{ "help", no_argument, NULL, 'h' },
|
|
|
ead27a |
+ { "reset-env", no_argument, NULL, RESET_ENV, },
|
|
|
ead27a |
{ "version", no_argument, NULL, 'V' },
|
|
|
ead27a |
{ NULL, 0, NULL, 0 }
|
|
|
ead27a |
};
|
|
|
ead27a |
@@ -883,6 +922,9 @@ int main(int argc, char **argv)
|
|
|
ead27a |
_("duplicate --apparmor-profile option"));
|
|
|
ead27a |
opts.apparmor_profile = optarg;
|
|
|
ead27a |
break;
|
|
|
ead27a |
+ case RESET_ENV:
|
|
|
ead27a |
+ opts.reset_env = 1;
|
|
|
ead27a |
+ break;
|
|
|
ead27a |
case 'h':
|
|
|
ead27a |
usage();
|
|
|
ead27a |
case 'V':
|
|
|
ead27a |
@@ -928,6 +970,16 @@ int main(int argc, char **argv)
|
|
|
ead27a |
"can be found on the system"),
|
|
|
ead27a |
(long) opts.ruid);
|
|
|
ead27a |
|
|
|
ead27a |
+ if (opts.reset_env) {
|
|
|
ead27a |
+ if (opts.have_passwd)
|
|
|
ead27a |
+ /* pwd according to --ruid or --reuid */
|
|
|
ead27a |
+ pw = &opts.passwd;
|
|
|
ead27a |
+ else
|
|
|
ead27a |
+ /* pwd for the current user */
|
|
|
ead27a |
+ pw = getpwuid(getuid());
|
|
|
ead27a |
+ do_reset_environ(pw);
|
|
|
ead27a |
+ }
|
|
|
ead27a |
+
|
|
|
ead27a |
if (opts.nnp && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
|
|
|
ead27a |
err(EXIT_FAILURE, _("disallow granting new privileges failed"));
|
|
|
ead27a |
|
|
|
ead27a |
--
|
|
|
ead27a |
2.30.2
|
|
|
ead27a |
|