From: Sean Anderson <seanga2@gmail.com>
To: util-linux@vger.kernel.org, Karel Zak <kzak@redhat.com>
Cc: Mikhail Gusarov <dottedmag@dottedmag.net>,
Matthew Harm Bekkema <id@mbekkema.name>,
James Peach <jpeach@apache.org>,
Sean Anderson <seanga2@gmail.com>
Subject: [PATCH v2 5/6] unshare: Add option to automatically create user and group maps
Date: Wed, 24 Nov 2021 13:26:17 -0500 [thread overview]
Message-ID: <20211124182618.1801447-6-seanga2@gmail.com> (raw)
In-Reply-To: <20211124182618.1801447-1-seanga2@gmail.com>
This option is designed to handle the "garden path" user/group ID
mapping:
- The user has one big map in /etc/sub[u,g]id
- The user wants to map as many user and group IDs as they can,
especially the first 1000 users and groups.
The "auto" map is designed to handle this. We find the first map
matching the current user, and then map the whole thing to the ID range
starting at ID 0.
Signed-off-by: Sean Anderson <seanga2@gmail.com>
---
Changes in v2:
- Add "auto" option for --map-users and --map-groups
- Fix most of read_subid_range using spaces instead of tabs
- Use pathname macros for /etc/sub{u,g}id
include/pathnames.h | 3 ++
sys-utils/unshare.c | 84 +++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 85 insertions(+), 2 deletions(-)
diff --git a/include/pathnames.h b/include/pathnames.h
index 9be2baa83..5db815880 100644
--- a/include/pathnames.h
+++ b/include/pathnames.h
@@ -97,6 +97,9 @@
#define _PATH_PROC_LOCKS "/proc/locks"
#define _PATH_PROC_CDROMINFO "/proc/sys/dev/cdrom/info"
+/* unshare paths */
+#define _PATH_SUBUID "/etc/subuid"
+#define _PATH_SUBGID "/etc/subgid"
#define _PATH_PROC_UIDMAP "/proc/self/uid_map"
#define _PATH_PROC_GIDMAP "/proc/self/gid_map"
#define _PATH_PROC_SETGROUPS "/proc/self/setgroups"
diff --git a/sys-utils/unshare.c b/sys-utils/unshare.c
index 6d0a56334..b5a18ed95 100644
--- a/sys-utils/unshare.c
+++ b/sys-utils/unshare.c
@@ -418,6 +418,72 @@ static struct map_range *get_map_range(const char *s)
return ret;
}
+/**
+ * read_subid_range() - Look up a user's sub[gu]id range
+ * @filename: The file to look up the range from. This should be either
+ * ``/etc/subuid`` or ``/etc/subgid``.
+ * @uid: The uid of the user whose range we should look up.
+ *
+ * This finds the first subid range matching @uid in @filename.
+ */
+static struct map_range *read_subid_range(char *filename, uid_t uid)
+{
+ char *line = NULL, *pwbuf;
+ FILE *idmap;
+ size_t n;
+ struct passwd *pw;
+ struct map_range *map;
+
+ map = xmalloc(sizeof(*map));
+ map->inner = 0;
+
+ pw = xgetpwuid(uid, &pwbuf);
+ if (!pw)
+ errx(EXIT_FAILURE, _("you (user %d) don't exist."), uid);
+
+ idmap = fopen(filename, "r");
+ if (!idmap)
+ err(EXIT_FAILURE, _("could not open '%s'"), filename);
+
+ /*
+ * Each line in sub[ug]idmap looks like
+ * username:subuid:count
+ * OR
+ * uid:subuid:count
+ */
+ while (getline(&line, &n, idmap) != -1) {
+ char *rest, *s;
+
+ rest = strchr(line, ':');
+ if (!rest)
+ continue;
+ *rest = '\0';
+
+ if (strcmp(line, pw->pw_name) &&
+ strtoul(line, NULL, 10) != pw->pw_uid)
+ continue;
+
+ s = rest + 1;
+ rest = strchr(s, ':');
+ if (!rest)
+ continue;
+ *rest = '\0';
+ map->outer = strtoul_or_err(s, _("failed to parse subid map"));
+
+ s = rest + 1;
+ rest = strchr(s, '\n');
+ if (rest)
+ *rest = '\0';
+ map->count = strtoul_or_err(s, _("failed to parse subid map"));
+
+ fclose(idmap);
+ return map;
+ }
+
+ err(EXIT_FAILURE, _("no line matching user \"%s\" in %s"),
+ pw->pw_name, filename);
+}
+
/**
* map_ids() - Create a new uid/gid map
* @idmapper: Either newuidmap or newgidmap
@@ -603,6 +669,7 @@ static void __attribute__((__noreturn__)) usage(void)
fputs(_(" --map-group=<gid>|<name> map current group to gid (implies --user)\n"), out);
fputs(_(" -r, --map-root-user map current user to root (implies --user)\n"), out);
fputs(_(" -c, --map-current-user map current user to itself (implies --user)\n"), out);
+ fputs(_(" --map-auto map users and groups automatically (implies --user)\n"), out);
fputs(_(" --map-users=<outeruid>,<inneruid>,<count>\n"
" map count users from outeruid to inneruid (implies --user)\n"), out);
fputs(_(" --map-groups=<outergid>,<innergid>,<count>\n"
@@ -644,6 +711,7 @@ int main(int argc, char *argv[])
OPT_MAPUSERS,
OPT_MAPGROUP,
OPT_MAPGROUPS,
+ OPT_MAPAUTO,
};
static const struct option longopts[] = {
{ "help", no_argument, NULL, 'h' },
@@ -667,6 +735,7 @@ int main(int argc, char *argv[])
{ "map-groups", required_argument, NULL, OPT_MAPGROUPS },
{ "map-root-user", no_argument, NULL, 'r' },
{ "map-current-user", no_argument, NULL, 'c' },
+ { "map-auto", no_argument, NULL, OPT_MAPAUTO },
{ "propagation", required_argument, NULL, OPT_PROPAGATION },
{ "setgroups", required_argument, NULL, OPT_SETGROUPS },
{ "keep-caps", no_argument, NULL, OPT_KEEPCAPS },
@@ -778,11 +847,22 @@ int main(int argc, char *argv[])
break;
case OPT_MAPUSERS:
unshare_flags |= CLONE_NEWUSER;
- usermap = get_map_range(optarg);
+ if (!strcmp(optarg, "auto"))
+ usermap = read_subid_range(_PATH_SUBUID, real_euid);
+ else
+ usermap = get_map_range(optarg);
break;
case OPT_MAPGROUPS:
unshare_flags |= CLONE_NEWUSER;
- groupmap = get_map_range(optarg);
+ if (!strcmp(optarg, "auto"))
+ groupmap = read_subid_range(_PATH_SUBGID, real_egid);
+ else
+ groupmap = get_map_range(optarg);
+ break;
+ case OPT_MAPAUTO:
+ unshare_flags |= CLONE_NEWUSER;
+ usermap = read_subid_range(_PATH_SUBUID, real_euid);
+ groupmap = read_subid_range(_PATH_SUBGID, real_egid);
break;
case OPT_SETGROUPS:
setgrpcmd = setgroups_str2id(optarg);
--
2.33.0
next prev parent reply other threads:[~2021-11-24 18:26 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-24 18:26 [PATCH v2 0/6] unshare: Add support for mapping ranges of user/group IDs Sean Anderson
2021-11-24 18:26 ` [PATCH v2 1/6] include/c: Add abs_diff macro Sean Anderson
2021-11-24 18:26 ` [PATCH v2 2/6] unshare: Add waitchild helper Sean Anderson
2021-11-24 18:26 ` [PATCH v2 3/6] unshare: Add some helpers for forking and synchronizing Sean Anderson
2021-11-24 18:26 ` [PATCH v2 4/6] unshare: Add options to map blocks of user/group IDs Sean Anderson
2021-11-24 18:26 ` Sean Anderson [this message]
2021-11-24 18:26 ` [PATCH v2 6/6] unshare: Document --map-{groups,users,auto} Sean Anderson
2021-12-01 15:16 ` [PATCH v2 0/6] unshare: Add support for mapping ranges of user/group IDs Karel Zak
2022-01-14 10:29 ` Daniel Gerber
2022-01-14 14:42 ` Sean Anderson
2022-01-14 17:15 ` Daniel Gerber
2022-01-15 0:53 ` Sean Anderson
2022-01-18 11:50 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211124182618.1801447-6-seanga2@gmail.com \
--to=seanga2@gmail.com \
--cc=dottedmag@dottedmag.net \
--cc=id@mbekkema.name \
--cc=jpeach@apache.org \
--cc=kzak@redhat.com \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox