summaryrefslogtreecommitdiff
path: root/sys-utils/nsenter.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys-utils/nsenter.c')
-rw-r--r--sys-utils/nsenter.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/sys-utils/nsenter.c b/sys-utils/nsenter.c
index 106349c7e..dfb1a3b51 100644
--- a/sys-utils/nsenter.c
+++ b/sys-utils/nsenter.c
@@ -28,6 +28,7 @@
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <grp.h>
#include "strutils.h"
#include "nls.h"
@@ -72,6 +73,8 @@ static void usage(int status)
fputs(_(" -n, --net [=<file>] enter network namespace\n"), out);
fputs(_(" -p, --pid [=<file>] enter pid namespace\n"), out);
fputs(_(" -U, --user [=<file>] enter user namespace\n"), out);
+ fputs(_(" -S, --setuid <uid> set uid in user namespace\n"), out);
+ fputs(_(" -G, --setgid <gid> set gid in user namespace\n"), out);
fputs(_(" -r, --root [=<dir>] set the root directory\n"), out);
fputs(_(" -w, --wd [=<dir>] set the working directory\n"), out);
fputs(_(" -F, --no-fork do not fork before exec'ing <program>\n"), out);
@@ -169,6 +172,8 @@ int main(int argc, char *argv[])
{ "net", optional_argument, NULL, 'n' },
{ "pid", optional_argument, NULL, 'p' },
{ "user", optional_argument, NULL, 'U' },
+ { "setuid", required_argument, NULL, 'S' },
+ { "setgid", required_argument, NULL, 'G' },
{ "root", optional_argument, NULL, 'r' },
{ "wd", optional_argument, NULL, 'w' },
{ "no-fork", no_argument, NULL, 'F' },
@@ -179,14 +184,16 @@ int main(int argc, char *argv[])
int c, namespaces = 0;
bool do_rd = false, do_wd = false;
int do_fork = -1; /* unknown yet */
+ uid_t uid = 0;
+ gid_t gid = 0;
- setlocale(LC_MESSAGES, "");
+ setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
atexit(close_stdout);
while ((c =
- getopt_long(argc, argv, "hVt:m::u::i::n::p::U::r::w::F",
+ getopt_long(argc, argv, "hVt:m::u::i::n::p::U::S:G:r::w::F",
longopts, NULL)) != -1) {
switch (c) {
case 'h':
@@ -234,6 +241,12 @@ int main(int argc, char *argv[])
else
namespaces |= CLONE_NEWUSER;
break;
+ case 'S':
+ uid = strtoul_or_err(optarg, _("failed to parse uid"));
+ break;
+ case 'G':
+ gid = strtoul_or_err(optarg, _("failed to parse gid"));
+ break;
case 'F':
do_fork = 0;
break;
@@ -315,6 +328,15 @@ int main(int argc, char *argv[])
if (do_fork == 1)
continue_as_child();
+ if (namespaces & CLONE_NEWUSER) {
+ if (setgroups(0, NULL)) /* drop supplementary groups */
+ err(EXIT_FAILURE, _("setgroups failed"));
+ if (setgid(gid) < 0)
+ err(EXIT_FAILURE, _("setgid failed"));
+ if (setuid(uid) < 0)
+ err(EXIT_FAILURE, _("setuid failed"));
+ }
+
if (optind < argc) {
execvp(argv[optind], argv + optind);
err(EXIT_FAILURE, _("failed to execute %s"), argv[optind]);