From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 1CEEA5A0265 for ; Wed, 12 Oct 2022 12:47:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665571633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w8e+u5FvDIdJru3uv+LxXlqS4ptlatyLDiJXzSdmQFc=; b=F9yRCY4KLXjR0GlZNev8jrTrW+Y+nTzljaIMxGSuw++20hxW9Mc7QrwNkeKk73hFxwCmBv XB4uhV22SIf6l8rqZfPVoQbN5Pq5FHLSLhasFtAA5j/C2Q3ER68BZyEDkp3OH+OjTCig03 FJ96G4vkpsP6kHSrmJhpJXicj0VGkrk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-445-9XpaS1WDOsif0Q_4N_8RBw-1; Wed, 12 Oct 2022 06:47:11 -0400 X-MC-Unique: 9XpaS1WDOsif0Q_4N_8RBw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 48E7F811E67; Wed, 12 Oct 2022 10:47:11 +0000 (UTC) Received: from maya.cloud.tilaa.com (ovpn-208-3.brq.redhat.com [10.40.208.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A6DFD2027062; Wed, 12 Oct 2022 10:47:10 +0000 (UTC) Date: Wed, 12 Oct 2022 12:47:07 +0200 From: Stefano Brivio To: David Gibson Subject: Re: Alas for CAP_NET_BIND_SERVICE Message-ID: <20221012124707.70755587@elisabeth> In-Reply-To: References: <20221012075432.09e33625@elisabeth> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: U74UQNV26WDWIV5EG4TPYGUGVI7UQQDH X-Message-ID-Hash: U74UQNV26WDWIV5EG4TPYGUGVI7UQQDH X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.3 Precedence: list List-Id: Development discussion and patches for passt Archived-At: <> Archived-At: List-Archive: <> List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Wed, 12 Oct 2022 20:31:20 +1100 David Gibson wrote: > On Wed, Oct 12, 2022 at 07:54:32AM +0200, Stefano Brivio wrote: > > Hi David, > >=20 > > On Wed, 12 Oct 2022 13:55:02 +1100 > > David Gibson wrote: > > =20 > > > Hi Stefano, > > >=20 > > > I've looked deeper into why giving passt/pasta CAP_NET_BIND_SERVICE > > > isn't working, and I'm afraid I have bad news. =20 > >=20 > > Thanks for the investigation. > > =20 > > > We lose CAP_NET_BIND_SERVICE in the initial namespace as soon as we > > > unshare() or setns() into the isolated namespace, and this appears to > > > be intended behaviour. From user_namespaces(7), in the Capabilities = section: > > >=20 > > > The child process created by clone(2) with the CLONE_NEWUSER flag > > > starts out with a complete set of capabilities in the new user > > > namespace. Likewise, a process that creates a new user namespace > > > using unshare(2) or joins an existing user namespace using > > > setns(2) gains a full set of capabilities in that namespace. ***= On > > > the other hand, that process has no capabilities in the parent (i= n > > > the case of clone(2)) or previous (in the case of unshare(2) and > > > setns(2)) user namespace, even if the new namespace is created or > > > joined by the root user (i.e., a process with user ID 0 in the > > > root namespace).*** > > >=20 > > > Emphasis (***) mine. Basically, despite the way it's phrased in many > > > places, processes don't have an independent set of capabilities in > > > each userns, they only have a set of capabilities in their current > > > userns. Any capabilities in other namespaces are implied in a pretty > > > much all or nothing way - if the process's UID (the real, init ns one= ) > > > owns the userns (or one of its ancestors), it gets all caps, otherwis= e > > > none. cap_capable() has the specific logic in the kernel. =20 > >=20 > > Right, I missed this. > >=20 > > For a moment, I wondered about ambient capabilities, but those would > > only have an effect on an execve(), not on a clone(), I guess. =20 >=20 > Well, yes, but it doesn't really make any difference in any case. All > ambient caps can do is be another way to get things into the permitted > set. If that happens before the unshare() then we still lose them on > unshare(). If it happens after the unshare(), then it's just giving > us caps within the namespace, which isn't what we need. >=20 > > > So, using CAP_NET_BIND_SERVICE isn't compatible with isolating > > > ourselves in our own userns. At the very least "auto" inbound > > > forwarding of low ports is pretty much off the cards. > > >=20 > > > For forwarding of specific low ports, we could delay our entry into > > > the new userns until we've set up the listening sockets, although it > > > does mean rolling back some of the simplification we gained from the > > > new-style userns handling. =20 > >=20 > > If I understand correctly, the biggest hurdle would be: > >=20 > > 1. we detach namespaces > >=20 > > 2. only then we can finalise any missing bit of addressing and routing > > configuration (relevant for pasta) > >=20 > > 3. we bind ports as we parse configuration options, but we need > > addressing to be fully configured for this > >=20 > > Referring to your latest patchset (which I'm still reviewing), I guess > > that implies a further split of isolate_user() (it's great to have a > > name for that, finally!), right? =20 >=20 > Uh.. something like that, I haven't looked at the details. As we did > before my userns cleanup, we'd probably need to repeatedly enter the > userns as well as the netns to operate upon it, staying in the initial > userns, with our initial caps until sandbox()/isolate_prefork() or > thereabouts. >=20 > > > Or, we could abandon CAP_NET_BIND_SERVICE, and recommend the > > > net.ipv4.ip_unprivileged_port_start sysctl as the only way to handle > > > low ports in passt. I do see a fair bit of logic in that approach: > > > passt has no meaningful way to limit what users do with the low ports > > > it allows them (indirectly) to bind to, giving passt > > > CAP_NET_BIND_SERVICE is pretty much equivalent to giving any process > > > which can invoke passt CAP_NET_BIND_SERVICE. =20 > >=20 > > I also see the general point, even though if file capabilities are > > used, I guess the equivalence doesn't really hold. =20 >=20 > Uh.. I don't follow. It's exactly file capabilities which make this > equivalence. If the passt binary has cap_net_bind_service=3Dep, you > can, as an unprivileged user, take any server, stick it in a namespace > and use pasta to effectively bind it to a low port in the init > namespace. I actually meant with passt but... even for pasta, this depends on the decision of whether we drop capabilities for the spawned process. If we decide we don't, one day, then it's not equivalent. It would be equivalent if we just inherited capabilities from the parent as opposed to file capabilities -- that's what I meant. I think it's a bit early to decide to drop those, though. Right now pasta isn't really used as a stand-alone tool (even though I actually do that, I find it very convenient also for totally unrelated purposes). Should we see some use cases, then we could make a more informed decision. > You can do the same thing with passt, though it's fiddlier > (you'd need a shim to translate qemu socket protocol before plugging > it into the server). Oh, you mean running pasta plus a shim plus qemu? Because with passt I don't understand how you'd pass that kind of stuff over AF_UNIX... > > And perhaps we > > should at least recommend that as a preferred way. > >=20 > > What still perplexes me is: somebody gives passt CAP_NET_BIND_SERVICE, > > and due to something that's slightly more than an implementation detail= , > > it won't be able to bind to low ports, which is the very reason for tha= t > > capability. That sounds highly counterintuitive. =20 >=20 > I guess it is in the sense that the reason for this wasn't obvious to > either of us initially. However it makes sense to me now that I've > looked at it. No, no, in the sense that it makes sense to you and now to me as well, as you explained it to me. And yet it I find it hard to imagine that it would naturally make sense to users, in these terms: - we offer a program that provides network connectivity to qemu - it also includes port forwarding functionality: it binds to configured ports and maps them to the guest - it can't bind to any port: it doesn't run as root, and Linux prevents non-root processes from binding to ports lower than 1024, which is a well-known fact -- at least by default (lesser known fact) - somewhat in between on the scale of general knowledge, lies CAP_NET_BIND_SERVICE: it allows non-root processes to bind to low ports ...but not passt. For very valid reasons, indeed, but those will need to be explained over and over again. > We use a userns for two reasons: 1) to control a netns > and 2) to isolate ourselves for security. We use the same path and > the same userns for both, but they're logically different reasons. >=20 > If (1) was the only reason for the userns we could handle this pretty > easily: we'd only enter the userns transiently when we need to > manipulate it, just like we do with the netns. That way the main > thread would retain CAP_NET_BIND_SERVICE in the original ns. >=20 > For (2), we're specifically choosing to isolate ourselves: that is to > give up privilege from our original position. It's not surprising > there's some degree of granularity to how we can do that, and the deal > is that we can't give up our membership in the original userns without > also giving up our enhanced capabilities in that userns. >=20 > I don't think giving up (2) is a price worth paying for this. Absolutely, I agree, I wouldn't either. However, he could give users the choice without compromising (2) at all, by binding to low ports early (without automatic detection, sure). And, somewhat importantly, by not handling any data from them. We could even defer the listen() calls if there's any value in doing so (is there some? I can't think of anything). Actually, I'm thinking of an easier way to break the circular dependency between isolation steps and port configuration I outlined earlier, without undoing your cleanups at all. We currently need to process port configuration in a second step for two reasons: - we might bind ports in the detached namespace (-T, -U) - one between IPv4 and IPv6 support could be administratively disabled (operationally, who cares, we'll just fail to bind if that's the case) ...but for init/host facing ports (now: "inbound"), we don't care about the detached namespace, and we could simply call conf_ports() for -t and -u in a second step after the main loop. Sure, if we continue like this, we'll end up with O(n=C2=B2) option handling, but right now it doesn't look that bad to me. I would give it a shot after I'm done reviewing your patchset (it should also look clearer after that) and re-spinning my recent ones, unless you see something wrong with it. --=20 Stefano