summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorКоренберг Марк (дома) <socketpair@gmail.com>2012-08-30 04:33:40 +0600
committerКоренберг Марк (дома) <socketpair@gmail.com>2012-08-30 04:36:28 +0600
commit5eee974e034be5bd0614dd907a603b71869646ef (patch)
treea64e1089d2536167f1373aadd50ef0630328a307
parenta2b23ffe458f7353eacb92cbe3dd29aa21dc195b (diff)
downloadlibnl-5eee974e034be5bd0614dd907a603b71869646ef.tar.gz
Prevent potential socket file descriptor leak
This may happen when passing connected socket to nl_cache_mngr_alloc(). Now, nl_connect() will return error trying to connect already connected socket. Also, dont call close(-1) if socket() fails.
-rw-r--r--lib/nl.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/nl.c b/lib/nl.c
index 6b2f027..d3e67aa 100644
--- a/lib/nl.c
+++ b/lib/nl.c
@@ -69,6 +69,8 @@
* Creates a netlink socket using the specified protocol, binds the socket
* and issues a connection attempt.
*
+ * This function fail if socket is already connected.
+ *
* @note SOCK_CLOEXEC is set on the socket if available.
*
* @return 0 on success or a negative error code.
@@ -82,6 +84,9 @@ int nl_connect(struct nl_sock *sk, int protocol)
flags |= SOCK_CLOEXEC;
#endif
+ if (sk->s_fd != -1)
+ return -NLE_BAD_SOCK;
+
sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
if (sk->s_fd < 0) {
err = -nl_syserr2nlerr(errno);
@@ -123,8 +128,10 @@ int nl_connect(struct nl_sock *sk, int protocol)
return 0;
errout:
- close(sk->s_fd);
- sk->s_fd = -1;
+ if (sk->s_fd != -1) {
+ close(sk->s_fd);
+ sk->s_fd = -1;
+ }
return err;
}