Skip to contentSkip to conversation

netlink: do not SIGSEGV when socket() fails

PATCH In review Shawn Landden 4 participants 6 messages Dec 4, 2013 – Dec 5, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netlink: do not SIGSEGV when socket() fails

---
 src/netlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/netlink.c +6 -2
index 533634a..664487d 100644
@@ -33,8 +33,11 @@ static struct mnl_socket *nf_sock;
 static void __init netlink_open_sock(void)
 {
 	nf_sock = mnl_socket_open(NETLINK_NETFILTER);
-	if (nf_sock == NULL)
+	if (nf_sock == NULL) {
+                fprintf(fdopen(STDERR_FILENO, "a"), "Could not open AF_NETLINK socket: %s\n",
+                                        strerror(errno));
 		memory_allocation_error();
+        }
 
 	fcntl(mnl_socket_get_fd(nf_sock), F_SETFL, O_NONBLOCK);
 	mnl_batch_init();
@@ -42,7 +45,8 @@ static void __init netlink_open_sock(void)
 
 static void __exit netlink_close_sock(void)
 {
-	mnl_socket_close(nf_sock);
+        if (nf_sock)
+                mnl_socket_close(nf_sock);
 }
 
 int netlink_io_error(struct netlink_ctx *ctx, const struct location *loc,
Signature · 1 line
1.8.5.1
#1 lore raw
Quoted 4 lines · Shawn Landden
---
 src/netlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Hi!

I would suggest to provide a description and a Signed-off-by line within the patch.
Also, a small trace may help others (like me) to understand the situation.

src/netlink.c static struct mnl_socket *nf_sock; · line 38

Quoted 12 lines
src/netlink.c +3 -1
index 533634a..664487d 100644
@@ -33,8 +33,11 @@ static struct mnl_socket *nf_sock;
 static void __init netlink_open_sock(void)
 {
        nf_sock = mnl_socket_open(NETLINK_NETFILTER);
-       if (nf_sock == NULL)
+       if (nf_sock == NULL) {
+                fprintf(fdopen(STDERR_FILENO, "a"), "Could not open AF_NETLINK socket: %s\n",
+                                        strerror(errno));

In your patch, the code seem indented with spaces. Tabs are used all around the nft code.
Also, the second line should be aligned with the position+1 of first '(' character of fprintf.

bonus: please consider adding a tag to the [PATCH] subject, like [nft PATCH] or [libmnl PATCH].

regards.

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

PATCH 1/1 netlink: do not SIGSEGV when socket() fails

Program received signal SIGSEGV, Segmentation fault. mnl_socket_close (nl=0x0) at socket.c:248
248 int ret = close(nl->fd);
(gdb) bt

 #0  mnl_socket_close (nl=0x0) at socket.c:248
 #1  0x0000000000410f79 in netlink_close_sock () at src/netlink.c:45
 #2  0x00007ffff7de9fcf in _dl_fini () at dl-fini.c:253
 #3  0x00007ffff717fa91 in __run_exit_handlers (status=2, listp=0x7ffff74ec5c8
<__exit_funcs>,
    run_list_atexit=run_list_atexit@entry=true) at exit.c:77
 #4  0x00007ffff717fb15 in __GI_exit (status=<optimized out>) at exit.c:99
 #5  0x0000000000419b60 in memory_allocation_error () at src/utils.c:24
 #6  0x0000000000410f3a in netlink_open_sock () at src/netlink.c:37
 #7  0x00000000004291cd in __libc_csu_init ()
 #8  0x00007ffff7167925 in __libc_start_main (main=0x405219 <main>, argc=1,
    ubp_av=0x7fffffffe738, init=0x429170 <__libc_csu_init>, fini=<optimized
out>,
    rtld_fini=<optimized out>, stack_end=0x7fffffffe728) at libc-start.c:235

#9 0x0000000000404d49 in _start ()

---
 src/netlink.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
src/netlink.c +8 -2
index 533634a..5b2961e 100644
@@ -33,8 +33,14 @@ static struct mnl_socket *nf_sock;
 static void __init netlink_open_sock(void)
 {
 	nf_sock = mnl_socket_open(NETLINK_NETFILTER);
-	if (nf_sock == NULL)
-		memory_allocation_error();
+	if (nf_sock == NULL) {
+		fprintf(fdopen(STDERR_FILENO, "a"),
+			"Could not open AF_NETLINK socket: %s\n",
+			strerror(errno));
+		/* the __exit netlink_close_sock() will dereference nf_sock
+		   if we exit() */
+		_exit(EFT_EXIT_NOMEM);
+	}
 
 	fcntl(mnl_socket_get_fd(nf_sock), F_SETFL, O_NONBLOCK);
 	mnl_batch_init();
Signature · 1 line
1.8.5.1
maintainer
#3 lore raw

src/netlink.c static struct mnl_socket *nf_sock; · line 39

Quoted 4 lines · Shawn Landden
src/netlink.c +4 -0
+	if (nf_sock == NULL) {
+		fprintf(fdopen(STDERR_FILENO, "a"),
+			"Could not open AF_NETLINK socket: %s\n",
+			strerror(errno));

Why this fdopen() [ which can fail ] construct?

#4 lore raw

PATCH 1/1 netlink: do not SIGSEGV when socket() fails

Program received signal SIGSEGV, Segmentation fault. mnl_socket_close (nl=0x0) at socket.c:248
248 int ret = close(nl->fd);
(gdb) bt

 #0  mnl_socket_close (nl=0x0) at socket.c:248
 #1  0x0000000000410f79 in netlink_close_sock () at src/netlink.c:45
 #2  0x00007ffff7de9fcf in _dl_fini () at dl-fini.c:253
 #3  0x00007ffff717fa91 in __run_exit_handlers (status=2, listp=0x7ffff74ec5c8
<__exit_funcs>,
    run_list_atexit=run_list_atexit@entry=true) at exit.c:77
 #4  0x00007ffff717fb15 in __GI_exit (status=<optimized out>) at exit.c:99
 #5  0x0000000000419b60 in memory_allocation_error () at src/utils.c:24
 #6  0x0000000000410f3a in netlink_open_sock () at src/netlink.c:37
 #7  0x00000000004291cd in __libc_csu_init ()
 #8  0x00007ffff7167925 in __libc_start_main (main=0x405219 <main>, argc=1,
    ubp_av=0x7fffffffe738, init=0x429170 <__libc_csu_init>, fini=<optimized
out>,
    rtld_fini=<optimized out>, stack_end=0x7fffffffe728) at libc-start.c:235

#9 0x0000000000404d49 in _start ()

The include of <nftables.h> is wierd so I can't use NFT_EXIT_NOMEM in this file.

---
 src/netlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/netlink.c +6 -2
index 533634a..5240633 100644
@@ -33,8 +33,11 @@ static struct mnl_socket *nf_sock;
 static void __init netlink_open_sock(void)
 {
 	nf_sock = mnl_socket_open(NETLINK_NETFILTER);
-	if (nf_sock == NULL)
+	if (nf_sock == NULL) {
+		dprintf(STDERR_FILENO,
+			"Could not open AF_NETLINK socket: %m\n");
 		memory_allocation_error();
+	}
 
 	fcntl(mnl_socket_get_fd(nf_sock), F_SETFL, O_NONBLOCK);
 	mnl_batch_init();
@@ -42,7 +45,8 @@ static void __init netlink_open_sock(void)
 
 static void __exit netlink_close_sock(void)
 {
-	mnl_socket_close(nf_sock);
+	if (nf_sock)
+		mnl_socket_close(nf_sock);
 }
 
 int netlink_io_error(struct netlink_ctx *ctx, const struct location *loc,
Signature · 1 line
1.8.5.1
#5 lore raw
Quoted 18 lines · Shawn Landden

Program received signal SIGSEGV, Segmentation fault. mnl_socket_close (nl=0x0) at socket.c:248
248 int ret = close(nl->fd);
(gdb) bt

 #0  mnl_socket_close (nl=0x0) at socket.c:248
 #1  0x0000000000410f79 in netlink_close_sock () at src/netlink.c:45
 #2  0x00007ffff7de9fcf in _dl_fini () at dl-fini.c:253
 #3  0x00007ffff717fa91 in __run_exit_handlers (status=2, listp=0x7ffff74ec5c8
<__exit_funcs>,
    run_list_atexit=run_list_atexit@entry=true) at exit.c:77
 #4  0x00007ffff717fb15 in __GI_exit (status=<optimized out>) at exit.c:99
 #5  0x0000000000419b60 in memory_allocation_error () at src/utils.c:24
 #6  0x0000000000410f3a in netlink_open_sock () at src/netlink.c:37
 #7  0x00000000004291cd in __libc_csu_init ()
 #8  0x00007ffff7167925 in __libc_start_main (main=0x405219 <main>, argc=1,
    ubp_av=0x7fffffffe738, init=0x429170 <__libc_csu_init>, fini=<optimized
out>,
    rtld_fini=<optimized out>, stack_end=0x7fffffffe728) at libc-start.c:235
Quoted 3 lines

#9 0x0000000000404d49 in _start ()

The include of <nftables.h> is wierd so I can't use NFT_EXIT_NOMEM in this file.

Please, add NFT_EXIT_NONL.

Quoted 10 lines
---
 src/netlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/netlink.c +0 -0
index 533634a..5240633 100644

src/netlink.c static struct mnl_socket *nf_sock; · line 40

Quoted 9 lines
src/netlink.c +4 -1
@@ -33,8 +33,11 @@ static struct mnl_socket *nf_sock;
 static void __init netlink_open_sock(void)
 {
 	nf_sock = mnl_socket_open(NETLINK_NETFILTER);
-	if (nf_sock == NULL)
+	if (nf_sock == NULL) {
+		dprintf(STDERR_FILENO,
+			"Could not open AF_NETLINK socket: %m\n");
 		memory_allocation_error();
+	}

Better add this function to utils.c

void __noreturn netlink_error(void)

{
        fprintf(stderr, "Netlink failure: %s\n", strerror(errno));
        exit(NFT_EXIT_NONL);
}

And call it from there. Thanks.