From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 07/10] isolation: Replace drop_caps() with a version that actually does something
Date: Thu, 13 Oct 2022 20:44:58 +1100 [thread overview]
Message-ID: <Y0feGg+7yKaDPom5@yekko> (raw)
In-Reply-To: <20221013041713.16db5ad1@elisabeth>
[-- Attachment #1: Type: text/plain, Size: 4678 bytes --]
On Thu, Oct 13, 2022 at 04:18:24AM +0200, Stefano Brivio wrote:
> Well, this drop_caps() is pretty much the same as patch 8/10, so it
> actually did something. :)
Yes, but not what we wanted :).
> On Tue, 11 Oct 2022 16:40:15 +1100
> David Gibson <david@gibson.dropbear.id.au> wrote:
>
> > The current implementation of drop_caps() doesn't really work because it
> > attempts to drop capabilities from the bounding set. hat's not the set
> > that really matters: the bounding set is about limiting the abilities of
> > otherwise things we might later exec() rather than our own capabilities.
> > In addition altering the bounding set requires CAP_SETPCAP which we won't
> > usually have.
> >
> > Replace it with a new version which uses setcap(2) to drop capabilities
> > from the effective and permitted sets, which is what actually matters for
> > most purposes. For now we leave the inheritable set alone, since we don't
> > want to preclude the user from passing inheritable capabilities to the
> > command spawed by pasta.
> >
> > Correctly dropping caps reveals that we actually need CAP_SYS_ADMIN within
> > the userns we create/join in pasta mode, so that we can later setns() to
> > the netns within it.
> >
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> > isolation.c | 52 ++++++++++++++++++++++++++++++++++++++++++++--------
> > 1 file changed, 44 insertions(+), 8 deletions(-)
> >
> > diff --git a/isolation.c b/isolation.c
> > index 4aa75e6..2468f84 100644
> > --- a/isolation.c
> > +++ b/isolation.c
> > @@ -86,18 +86,37 @@
> > #include "passt.h"
> > #include "isolation.h"
> >
> > +#define CAP_VERSION _LINUX_CAPABILITY_VERSION_3
> > +#define CAP_WORDS _LINUX_CAPABILITY_U32S_3
> > +
> > /**
> > - * drop_caps() - Drop capabilities we might have except for CAP_NET_BIND_SERVICE
> > + * drop_caps_ep_except() - Drop capabilities from effective & permitted sets
> > + * @keep: Capabilities to keep
> > */
> > -static void drop_caps(void)
> > +static void drop_caps_ep_except(uint64_t keep)
> > {
> > + struct __user_cap_header_struct hdr = {
> > + .version = CAP_VERSION,
> > + .pid = 0,
> > + };
> > + struct __user_cap_data_struct data[CAP_WORDS];
> > int i;
> >
> > - for (i = 0; i < 64; i++) {
> > - if (i == CAP_NET_BIND_SERVICE)
> > - continue;
> > + if (syscall(SYS_capget, &hdr, data)) {
> > + err("Couldn't get current capabilities: %s", strerror(errno));
> > + exit(EXIT_FAILURE);
> > + }
> > +
> > + for (i = 0; i < CAP_WORDS; i++) {
> > + uint32_t mask = keep >> (32 * i);
> > +
> > + data[i].effective &= mask;
> > + data[i].permitted &= mask;
> > + }
> >
> > - prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
> > + if (syscall(SYS_capset, &hdr, data)) {
> > + err("Couldn't drop capabilities: %s", strerror(errno));
> > + exit(EXIT_FAILURE);
> > }
> > }
> >
> > @@ -111,7 +130,11 @@ static void drop_caps(void)
> > */
> > void isolate_initial(void)
> > {
> > - drop_caps();
> > + /* We want to keep CAP_NET_BIND_SERVICE in the initial
> > + * namespace if we have it, so that we can forward low ports
> > + * into the guest/namespace
> > + */
> > + drop_caps_ep_except((1UL << CAP_NET_BIND_SERVICE));
>
> You could use BIT() (util.h) here,
Ah, yes, done.
> > }
> >
> > /**
> > @@ -211,6 +234,7 @@ void isolate_user(uid_t uid, gid_t gid, bool use_userns, const char *userns)
> > int isolate_prefork(struct ctx *c)
> > {
> > int flags = CLONE_NEWIPC | CLONE_NEWNS | CLONE_NEWUTS;
> > + uint64_t ns_caps = 0;
> >
> > /* If we run in foreground, we have no chance to actually move to a new
> > * PID namespace. For passt, use CLONE_NEWPID anyway, in case somebody
> > @@ -251,7 +275,19 @@ int isolate_prefork(struct ctx *c)
> > return -errno;
> > }
> >
> > - drop_caps(); /* Relative to the new user namespace this time. */
> > + /* Drop capabilites in our new userns */
> > + if (c->mode == MODE_PASTA) {
> > + /* Keep CAP_SYS_ADMIN, so that we can setns() to the
> > + * netns when we need to act upon it
> > + */
> > + ns_caps |= 1UL << CAP_SYS_ADMIN;
>
> here,
>
> > + /* Keep CAP_NET_BIND_SERVICE, so we can splice
> > + * outbound connections to low port numbers
> > + */
> > + ns_caps |= 1UL << CAP_NET_BIND_SERVICE;
>
> and here.
>
> > + }
> > +
> > + drop_caps_ep_except(ns_caps);
> >
> > return 0;
> > }
>
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2022-10-13 10:05 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-11 5:40 [PATCH 00/10] Fixes and cleanups for capability handling David Gibson
2022-10-11 5:40 ` [PATCH 01/10] test: Move slower tests to end of test run David Gibson
2022-10-11 5:40 ` [PATCH 02/10] pasta: More general way of starting spawned shell as a login shell David Gibson
2022-10-13 2:16 ` Stefano Brivio
2022-10-13 8:22 ` David Gibson
2022-10-13 9:48 ` Stefano Brivio
2022-10-13 23:24 ` David Gibson
2022-10-11 5:40 ` [PATCH 03/10] pasta_start_ns() always ends in parent context David Gibson
2022-10-11 5:40 ` [PATCH 04/10] Remove unhelpful drop_caps() call in pasta_start_ns() David Gibson
2022-10-11 5:40 ` [PATCH 05/10] Clarify various self-isolation steps David Gibson
2022-10-13 2:17 ` Stefano Brivio
2022-10-13 8:31 ` David Gibson
2022-10-13 12:49 ` Stefano Brivio
2022-10-13 23:25 ` David Gibson
2022-10-11 5:40 ` [PATCH 06/10] Replace FWRITE with a function David Gibson
2022-10-13 2:17 ` Stefano Brivio
2022-10-13 8:51 ` David Gibson
2022-10-11 5:40 ` [PATCH 07/10] isolation: Replace drop_caps() with a version that actually does something David Gibson
2022-10-13 2:18 ` Stefano Brivio
2022-10-13 9:44 ` David Gibson [this message]
2022-10-13 4:01 ` Stefano Brivio
2022-10-13 13:08 ` Stefano Brivio
2022-10-13 16:37 ` Stefano Brivio
2022-10-13 23:42 ` David Gibson
2022-10-11 5:40 ` [PATCH 08/10] isolation: Prevent any child processes gaining capabilities David Gibson
2022-10-13 2:17 ` Stefano Brivio
2022-10-13 9:33 ` David Gibson
2022-10-13 9:50 ` Stefano Brivio
2022-10-11 5:40 ` [PATCH 09/10] isolation: Only configure UID/GID mappings in userns when spawning shell David Gibson
2022-10-13 2:18 ` Stefano Brivio
2022-10-13 9:36 ` David Gibson
2022-10-11 5:40 ` [PATCH 10/10] Rename pasta_setup_ns() to pasta_spawn_cmd() David Gibson
2022-10-13 2:44 ` [PATCH 00/10] Fixes and cleanups for capability handling Stefano Brivio
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=Y0feGg+7yKaDPom5@yekko \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/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.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).