File descriptor may leak in all libnl versions if pthreads are used.

Thomas Graf tgraf at infradead.org
Wed Jun 15 10:14:05 EDT 2011


On Tue, Jun 14, 2011 at 09:17:57PM +0600, Марк Коренберг wrote:
> lib/nl.c:
> 
> -----
> sk->s_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
> -----
> 
> should be changed to
> 
> -----
> sk->s_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
> -----
> 
> Note: SOCK_CLOEXEC should be checked in autotools via querying
> fcntl(fd, F_GETFD) & FD_CLOEXEC)  after calling to socket() with
> SOCK_CLOEXEC

Is this what you have in mind?

diff --git a/lib/nl.c b/lib/nl.c
index f5f94e3..41d2b1b 100644
--- a/lib/nl.c
+++ b/lib/nl.c
@@ -105,10 +105,14 @@
  */
 int nl_connect(struct nl_sock *sk, int protocol)
 {
-       int err;
+       int err, flags = 0;
        socklen_t addrlen;
 
-       sk->s_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
+#ifdef SOCK_CLOEXEC
+       flags |= SOCK_CLOEXEC;
+#endif
+
+       sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
        if (sk->s_fd < 0) {
                err = -nl_syserr2nlerr(errno);
                goto errout;



More information about the libnl mailing list