* [libmnl] Creating netlink socket with SOCK_CLOEXEC flag
@ 2015-10-01 16:16 Guillaume Nault
2015-10-01 16:50 ` Pablo Neira Ayuso
0 siblings, 1 reply; 3+ messages in thread
From: Guillaume Nault @ 2015-10-01 16:16 UTC (permalink / raw)
To: netfilter-devel
Hi,
I'm looking at how libmnl could be used with CLOEXEC netlink sockets.
Of course, one can use the
nl = mnl_socket_open();
fd = mnl_socket_get_fd(nl);
fcntl(fd, F_SETFD, O_CLOEXEC);
sequence, but this is racy in multi-threaded programs, where another
thread could fork()/execve() between the mnl_socket_open() and the
fcntl() calls. Applying the CLOEXEC flag at socket creation closes this
issue.
There are three different approaches I can think of:
1- Make mnl_socket_open() unconditionally add the SOCK_CLOEXEC flag in
its socket() call.
2- Define mnl_socket_open2(), similar to mnl_socket_open() but with an
additional flags parameter that would be passed to socket().
3- Tell user to create its netlink socket with the required flags and
use it with mnl_socket_fdopen().
Solution #1 would provide safe default for all users, but that'd be an
ABI change. Also decision would need to be made wrt. platforms not
handling SOCK_CLOEXEC.
Solution #2 is more generic and allows all SOCK_* flags defined by the
plateform. But it's a bit more inelegant and exports yet another function
to allocate an mnl socket.
Solution #3 doesn't require any libmnl code update, but isn't (yet?) part
of any libmnl release. It also makes mnl socket creation not that well
integrated in libmnl.
Is any of these solutions acceptable? Any other idea on how to guarantee
race free application of the CLOEXEC flag to libmnl sockets?
Regards,
Guillaume
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [libmnl] Creating netlink socket with SOCK_CLOEXEC flag
2015-10-01 16:16 [libmnl] Creating netlink socket with SOCK_CLOEXEC flag Guillaume Nault
@ 2015-10-01 16:50 ` Pablo Neira Ayuso
2015-10-01 18:50 ` Guillaume Nault
0 siblings, 1 reply; 3+ messages in thread
From: Pablo Neira Ayuso @ 2015-10-01 16:50 UTC (permalink / raw)
To: Guillaume Nault; +Cc: netfilter-devel
On Thu, Oct 01, 2015 at 06:16:04PM +0200, Guillaume Nault wrote:
> Hi,
>
> I'm looking at how libmnl could be used with CLOEXEC netlink sockets.
> Of course, one can use the
> nl = mnl_socket_open();
> fd = mnl_socket_get_fd(nl);
> fcntl(fd, F_SETFD, O_CLOEXEC);
> sequence, but this is racy in multi-threaded programs, where another
> thread could fork()/execve() between the mnl_socket_open() and the
> fcntl() calls. Applying the CLOEXEC flag at socket creation closes this
> issue.
>
> There are three different approaches I can think of:
> 1- Make mnl_socket_open() unconditionally add the SOCK_CLOEXEC flag in
> its socket() call.
> 2- Define mnl_socket_open2(), similar to mnl_socket_open() but with an
> additional flags parameter that would be passed to socket().
> 3- Tell user to create its netlink socket with the required flags and
> use it with mnl_socket_fdopen().
>
> Solution #1 would provide safe default for all users, but that'd be an
> ABI change. Also decision would need to be made wrt. platforms not
> handling SOCK_CLOEXEC.
>
> Solution #2 is more generic and allows all SOCK_* flags defined by the
> plateform. But it's a bit more inelegant and exports yet another function
> to allocate an mnl socket.
I'd suggest you add mnl_socket_open2().
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [libmnl] Creating netlink socket with SOCK_CLOEXEC flag
2015-10-01 16:50 ` Pablo Neira Ayuso
@ 2015-10-01 18:50 ` Guillaume Nault
0 siblings, 0 replies; 3+ messages in thread
From: Guillaume Nault @ 2015-10-01 18:50 UTC (permalink / raw)
To: Pablo Neira Ayuso; +Cc: netfilter-devel
On Thu, Oct 01, 2015 at 06:50:05PM +0200, Pablo Neira Ayuso wrote:
> On Thu, Oct 01, 2015 at 06:16:04PM +0200, Guillaume Nault wrote:
> > Hi,
> >
> > I'm looking at how libmnl could be used with CLOEXEC netlink sockets.
> > Of course, one can use the
> > nl = mnl_socket_open();
> > fd = mnl_socket_get_fd(nl);
> > fcntl(fd, F_SETFD, O_CLOEXEC);
> > sequence, but this is racy in multi-threaded programs, where another
> > thread could fork()/execve() between the mnl_socket_open() and the
> > fcntl() calls. Applying the CLOEXEC flag at socket creation closes this
> > issue.
> >
> > There are three different approaches I can think of:
> > 1- Make mnl_socket_open() unconditionally add the SOCK_CLOEXEC flag in
> > its socket() call.
> > 2- Define mnl_socket_open2(), similar to mnl_socket_open() but with an
> > additional flags parameter that would be passed to socket().
> > 3- Tell user to create its netlink socket with the required flags and
> > use it with mnl_socket_fdopen().
> >
> > Solution #1 would provide safe default for all users, but that'd be an
> > ABI change. Also decision would need to be made wrt. platforms not
> > handling SOCK_CLOEXEC.
> >
> > Solution #2 is more generic and allows all SOCK_* flags defined by the
> > plateform. But it's a bit more inelegant and exports yet another function
> > to allocate an mnl socket.
>
> I'd suggest you add mnl_socket_open2().
Ok, will do.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-10-01 18:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-01 16:16 [libmnl] Creating netlink socket with SOCK_CLOEXEC flag Guillaume Nault
2015-10-01 16:50 ` Pablo Neira Ayuso
2015-10-01 18:50 ` Guillaume Nault
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 NNTP newsgroup(s).