netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guillaume Nault <g.nault@alphalink.fr>
To: netfilter-devel@vger.kernel.org
Subject: [libmnl] Creating netlink socket with SOCK_CLOEXEC flag
Date: Thu, 1 Oct 2015 18:16:04 +0200	[thread overview]
Message-ID: <20151001161604.GC2911@alphalink.fr> (raw)

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

             reply	other threads:[~2015-10-01 16:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-01 16:16 Guillaume Nault [this message]
2015-10-01 16:50 ` [libmnl] Creating netlink socket with SOCK_CLOEXEC flag Pablo Neira Ayuso
2015-10-01 18:50   ` Guillaume Nault

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=20151001161604.GC2911@alphalink.fr \
    --to=g.nault@alphalink.fr \
    --cc=netfilter-devel@vger.kernel.org \
    /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.
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).