All of lore.kernel.org
 help / color / mirror / Atom feed
* CLONE_NEWNET + unix domain sockets
@ 2011-04-25 13:56 Alex Bligh
       [not found] ` <6E3DBEA16997DE780A11C637-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Bligh @ 2011-04-25 13:56 UTC (permalink / raw)
  To: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA

This is probably a bit of a newbie question, but:

I have a parent and a child process. The child does
  unshare(CLONE_NEWNET)
after the fork(). It does not unshare the filings system
namespace or anything else.

I want the child to expose a unix domain socket, of type SOCK_STREAM.
Both act as servers, i.e. they listen on the service, accept(), then
handle the resultant connections. The socket needs to be accessed
both by the parent and by other processes (preferably processes
with both network namespaces, but primarily from the parent's).

If I create and bind the socket in the child after the unshare(),
then I cannot connect to it from the parent or processes sharing
the parent namespace. This seems surprising, as the documentation
for CLONE_NEWNET suggests only the networking space is separated,
and that would not normally appear to include UNIX domain sockets
(I would have thought they would be CLONE_NEWNS or CLONE_NEWIPC).

If I'm wrong in this assumption, and CLONE_NEWNET should isolate
unix domain sockets, something surprising still happens: if I create
the listen socket before the CLONE_NEWNET, then everything
works as intended, even though I am creating new fds via
accept() after the unshare(), i.e. the unix domain socket space
does not appear to be isolated.

It appears to be working by doing:
  bind()
  listen()
  unshare()
  accept()

but I don't understand why, or what the semantics are for interaction
between unshare(CLONE_NEWNET) and unix domain sockets. Any ideas?

-- 
Alex Bligh

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: CLONE_NEWNET + unix domain sockets
       [not found] ` <6E3DBEA16997DE780A11C637-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
@ 2011-04-25 14:12   ` Serge Hallyn
       [not found]     ` <20110425141228.GB29132-A9i7LUbDfNHQT0dZR+AlfA@public.gmane.org>
  2011-04-25 18:35   ` Bastian Blank
  1 sibling, 1 reply; 6+ messages in thread
From: Serge Hallyn @ 2011-04-25 14:12 UTC (permalink / raw)
  To: Alex Bligh; +Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA

Quoting Alex Bligh (alex-rWA27mgs/Jz10XsdtD+oqA@public.gmane.org):
> This is probably a bit of a newbie question, but:
> 
> I have a parent and a child process. The child does
>   unshare(CLONE_NEWNET)
> after the fork(). It does not unshare the filings system
> namespace or anything else.
> 
> I want the child to expose a unix domain socket, of type SOCK_STREAM.
> Both act as servers, i.e. they listen on the service, accept(), then
> handle the resultant connections. The socket needs to be accessed
> both by the parent and by other processes (preferably processes
> with both network namespaces, but primarily from the parent's).
> 
> If I create and bind the socket in the child after the unshare(),
> then I cannot connect to it from the parent or processes sharing
> the parent namespace. This seems surprising, as the documentation
> for CLONE_NEWNET suggests only the networking space is separated,
> and that would not normally appear to include UNIX domain sockets
> (I would have thought they would be CLONE_NEWNS or CLONE_NEWIPC).

Nope, while there have been discussions about the right thing to do,
last I knew unix domain sockets were completely tied to the network
namespace.

> If I'm wrong in this assumption, and CLONE_NEWNET should isolate
> unix domain sockets, something surprising still happens: if I create
> the listen socket before the CLONE_NEWNET, then everything
> works as intended, even though I am creating new fds via
> accept() after the unshare(), i.e. the unix domain socket space
> does not appear to be isolated.
> 
> It appears to be working by doing:
>   bind()
>   listen()
>   unshare()
>   accept()
> 
> but I don't understand why, or what the semantics are for interaction
> between unshare(CLONE_NEWNET) and unix domain sockets. Any ideas?

Sockets, like file descriptors, persist as handles in the namespace
in which they were created.  So if you open a file, then unshare
mounts ns and pivot_root into a directory which doesn't have a path
to that file, you can still use the file descriptor, and, if it is
directory, use openat to look underneath it.

Likewise, if you connect a socket before CLONE_NEWNET, then you
can continue to use it after CLONE_NEWNET.  This is by design.  A
server can (and some do) create hunderds of thousands of network
namespaces, creating one connected socket in each, with no other
handle to that ns left other than that socket.

It's not so surprising so long as you remember that the namespace
deals only with name to object resolution.  Once you have the socket,
you are not having to resolve a name.

-serge

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: CLONE_NEWNET + unix domain sockets
       [not found]     ` <20110425141228.GB29132-A9i7LUbDfNHQT0dZR+AlfA@public.gmane.org>
@ 2011-04-25 14:43       ` Alex Bligh
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Bligh @ 2011-04-25 14:43 UTC (permalink / raw)
  To: Serge Hallyn; +Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA



--On 25 April 2011 09:12:28 -0500 Serge Hallyn <serge.hallyn-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org> 
wrote:

> Nope, while there have been discussions about the right thing to do,
> last I knew unix domain sockets were completely tied to the network
> namespace.

OK

> Sockets, like file descriptors, persist as handles in the namespace
> in which they were created.
...
> Likewise, if you connect a socket before CLONE_NEWNET, then you
> can continue to use it after CLONE_NEWNET.  This is by design.  A
> server can (and some do) create hunderds of thousands of network
> namespaces, creating one connected socket in each, with no other
> handle to that ns left other than that socket.

Ah, so the socket persists because of the FD despite its namespace being
unshared, simply because the listen fd persists across the unshare(); I can
thus accept() on a listening socket which is in another namespace, and
generate an fd that works just fine. This what I missed. It is
useful behaviour. Thanks.

-- 
Alex Bligh

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: CLONE_NEWNET + unix domain sockets
       [not found] ` <6E3DBEA16997DE780A11C637-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
  2011-04-25 14:12   ` Serge Hallyn
@ 2011-04-25 18:35   ` Bastian Blank
       [not found]     ` <20110425183459.GA29536-0IJIQSrh9RL9UF0aPl6fsj8Kkb2uy4ct@public.gmane.org>
  1 sibling, 1 reply; 6+ messages in thread
From: Bastian Blank @ 2011-04-25 18:35 UTC (permalink / raw)
  To: Alex Bligh; +Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA

On Mon, Apr 25, 2011 at 02:56:25PM +0100, Alex Bligh wrote:
> but I don't understand why, or what the semantics are for interaction
> between unshare(CLONE_NEWNET) and unix domain sockets. Any ideas?

AFAIK sharing unix sockets between network namespaces is supported since
2.6.36 or so.

Bastian

-- 
Killing is wrong.
		-- Losira, "That Which Survives", stardate unknown

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: CLONE_NEWNET + unix domain sockets
       [not found]     ` <20110425183459.GA29536-0IJIQSrh9RL9UF0aPl6fsj8Kkb2uy4ct@public.gmane.org>
@ 2011-04-25 18:54       ` Alex Bligh
       [not found]         ` <E5092261EC5F5ABA2AE6E7EE-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Bligh @ 2011-04-25 18:54 UTC (permalink / raw)
  To: Bastian Blank; +Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA



--On 25 April 2011 20:35:00 +0200 Bastian Blank <bastian-yyjItF7Rl6lg9hUCZPvPmw@public.gmane.org> 
wrote:

> On Mon, Apr 25, 2011 at 02:56:25PM +0100, Alex Bligh wrote:
>> but I don't understand why, or what the semantics are for interaction
>> between unshare(CLONE_NEWNET) and unix domain sockets. Any ideas?
>
> AFAIK sharing unix sockets between network namespaces is supported since
> 2.6.36 or so.

I'm using 2.6.32-28-generic, and I'm doing
	fork()
	listen()
	unshare(CLONE_NEWNET)
	...
	accept()

and it seems to be working. Is that forward compatible?

-- 
Alex Bligh

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: CLONE_NEWNET + unix domain sockets
       [not found]         ` <E5092261EC5F5ABA2AE6E7EE-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
@ 2011-04-28 20:03           ` Eric W. Biederman
  0 siblings, 0 replies; 6+ messages in thread
From: Eric W. Biederman @ 2011-04-28 20:03 UTC (permalink / raw)
  To: Alex Bligh
  Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA,
	Bastian Blank

Alex Bligh <alex-rWA27mgs/Jz10XsdtD+oqA@public.gmane.org> writes:

> --On 25 April 2011 20:35:00 +0200 Bastian Blank <bastian-yyjItF7Rl6lg9hUCZPvPmw@public.gmane.org> 
> wrote:
>
>> On Mon, Apr 25, 2011 at 02:56:25PM +0100, Alex Bligh wrote:
>>> but I don't understand why, or what the semantics are for interaction
>>> between unshare(CLONE_NEWNET) and unix domain sockets. Any ideas?
>>
>> AFAIK sharing unix sockets between network namespaces is supported since
>> 2.6.36 or so.
>
> I'm using 2.6.32-28-generic, and I'm doing
> 	fork()
> 	listen()
> 	unshare(CLONE_NEWNET)
> 	...
> 	accept()
>
> and it seems to be working. Is that forward compatible?

Yes.

Eric

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-04-28 20:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-25 13:56 CLONE_NEWNET + unix domain sockets Alex Bligh
     [not found] ` <6E3DBEA16997DE780A11C637-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
2011-04-25 14:12   ` Serge Hallyn
     [not found]     ` <20110425141228.GB29132-A9i7LUbDfNHQT0dZR+AlfA@public.gmane.org>
2011-04-25 14:43       ` Alex Bligh
2011-04-25 18:35   ` Bastian Blank
     [not found]     ` <20110425183459.GA29536-0IJIQSrh9RL9UF0aPl6fsj8Kkb2uy4ct@public.gmane.org>
2011-04-25 18:54       ` Alex Bligh
     [not found]         ` <E5092261EC5F5ABA2AE6E7EE-F+tRR8lnwRmNj9Bq2fkWzw@public.gmane.org>
2011-04-28 20:03           ` Eric W. Biederman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.