On Tue, Aug 30, 2022 at 6:04 PM Ted Toth <txtoth@xxxxxxxxx> wrote: > On Mon, Aug 29, 2022 at 4:22 PM Paul Moore <paul@xxxxxxxxxxxxxx> wrote: > > > > On Thu, Aug 25, 2022 at 9:22 AM Ted Toth <txtoth@xxxxxxxxx> wrote: > > > I asked on the systemd-devel list about enabling systemd to set the > > > context of a socket and got the answer I've included below. I don't > > > know how a transition rule can be written to transition tcp sockets to > > > multiple different target contexts, is this possible and if so how? > > > > Ignoring setsockcreatecon(3) as that really isn't an option here, > > If we determine that policy can't be written to accomplish the > transition then maybe systemd will reconsider not wanting to set the > socket context using a .socket file option. I think the challenge is going to be having enough information when the socket is created to do any useful type transition. I'm open to suggestions, but I'm skeptical there is anything we can do beyond the current approach. > > sockets created via socket(2) do check to see if there is a type > > transition defined in the policy. In the case of a TCP socket the > > type transition would look something like this: > > > > type_transition <domain> <domain>:tcp_socket <new_socket_type> > > > > ... so you can see there is not much one can select on other than the > > socket's object class. The reason is that the socket(2) call itself > > is rather spartan, with not even any clue as to if this is a client or > > server socket in the case of TCP. > > Having written many policy modules, some of which use the > type_transition statement for tcp_socket objects, I do not see how it > can be used to transition sockets created by systemd. And under this > circumstance I see that the selinux socket create hook would not be > able query the policy database for the port context since the port is > not known until the bind occurs but what about having the bind hook > set the socket context if it finds a sid for the port? The problem with waiting until the connect()/bind() is that you are effectively doing a relabel operation, which is a big no-no (but you already know that). *Maybe* you could justify it in the special case of stream sockets, as I'm pretty sure there is no way to do anything useful with them as a data sink/source until they are either connected to a remote peer or bound to a local port, however, we would all need to think on that for a bit (it is still a relabel, and thus nasty) and probably spend some time staring at the code to make sure there is no way to do something sneaky with an unconnected or unbound stream socket. > > Taking a step back, what are you trying to do? Perhaps there is > > another approach that would get you where you want to go. > > I want to create socket activation services using systemd and to have > the type of the socket being listened on be one that I've defined so > that I can write policy to control which source types can connect to > it. -- paul-moore.com