diff options
author | Alexander Chernavin <achernavin@netgate.com> | 2022-03-04 13:18:15 +0000 |
---|---|---|
committer | Matthew Smith <mgsmith@netgate.com> | 2022-03-04 22:30:21 +0000 |
commit | 7e721954d4ea31a26ad44872acc199c91b9595e6 (patch) | |
tree | fb09b50a1a1166fd2ac745305ee40a2a336e305f /src/plugins/linux-cp/lcp_nl.c | |
parent | 2286f937d9a805324a8e46ba5a17198c039ba91a (diff) |
linux-cp: fix issue of possibly closing negative fd
Type: fix
Primarily fix an issue reported by Coverity in
lcp_nl_open_sync_socket() that close() could possibly be run with
negative fd. Also, add more checks and error logging there.
Signed-off-by: Alexander Chernavin <achernavin@netgate.com>
Change-Id: I9a88520d068392977a6eba0766451e5652fe512c
Diffstat (limited to 'src/plugins/linux-cp/lcp_nl.c')
-rw-r--r-- | src/plugins/linux-cp/lcp_nl.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/plugins/linux-cp/lcp_nl.c b/src/plugins/linux-cp/lcp_nl.c index 43f5319fc13..176d85de777 100644 --- a/src/plugins/linux-cp/lcp_nl.c +++ b/src/plugins/linux-cp/lcp_nl.c @@ -940,18 +940,27 @@ lcp_nl_open_sync_socket (nl_sock_type_t sock_type) nm->sk_route_sync[sock_type] = sk_route = nl_socket_alloc (); dest_ns_fd = lcp_get_default_ns_fd (); - if (dest_ns_fd) + if (dest_ns_fd > 0) { curr_ns_fd = clib_netns_open (NULL /* self */); - clib_setns (dest_ns_fd); + if (clib_setns (dest_ns_fd) == -1) + NL_ERROR ("Cannot set destination ns"); } nl_connect (sk_route, NETLINK_ROUTE); - if (dest_ns_fd) + if (dest_ns_fd > 0) { - clib_setns (curr_ns_fd); - close (curr_ns_fd); + if (curr_ns_fd == -1) + { + NL_ERROR ("No previous ns to set"); + } + else + { + if (clib_setns (curr_ns_fd) == -1) + NL_ERROR ("Cannot set previous ns"); + close (curr_ns_fd); + } } NL_INFO ("Opened netlink synchronization socket %d of type %d", |