Local port leak on nl_socket_alloc/nl_socket_set_local_port(, 0)

Andrew Rybchenko Andrew.Rybchenko at oktetlabs.ru
Tue Aug 30 01:27:37 EDT 2011


Hi,

I've found a bug in the following scenario (fragment of code):

while (1) {
    struct nl_sock *sk = nl_socket_alloc();

    if (sk == NULL) {
        fprintf(stderr, "Failed to allocate nl socket\n");
	break;
    }
    nl_socket_set_local_port(sk, 0);
    nl_socket_free(sk);
}

The problem is that nl_socket_set_local_port(, 0) does not
release local port if it is allocated before.

The following patch fixes the problem:

Index: lib/socket.c
===================================================================
--- lib/socket.c        (revision 156955)
+++ lib/socket.c        (working copy)
@@ -268,7 +268,14 @@
 {
        if (port == 0) {
                port = generate_local_port(); 
-               sk->s_flags &= ~NL_OWN_PORT;
+               /*
+                * Release local port after generation of a new one to be
+                * able to change local port using nl_socket_set_local_port(, 0)
+                */
+               if (!(sk->s_flags & NL_OWN_PORT))
+                       release_local_port(sk->s_local.nl_pid);
+               else
+                       sk->s_flags &= ~NL_OWN_PORT;
        } else  {
                if (!(sk->s_flags & NL_OWN_PORT))
                        release_local_port(sk->s_local.nl_pid);

Best Regards,
-- 
Andrew Rybchenko
OKTET Labs, St.-Petersburg, Russia    Web: www.oktetlabs.ru
Office: +7 812 7832191  Fax: +7 812 7846591  Mobile: +7 921 7479683




More information about the libnl mailing list