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

Марк Коренберг socketpair at gmail.com
Wed Jun 15 11:12:22 EDT 2011


2011/6/15 Thomas Graf <tgraf at infradead.org>:
> 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;
>

Yes, but note, that on old kernels, SOCK_CLOEXEC may be ignored, as
was with O_CLOEXEC in open(). In that case, good libraries should use
fcntl().
I think that fcntl() will bloat your code, so your patch is sufficient.

-- 
Segmentation fault



More information about the libnl mailing list