From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [PATCH 1/2] alfred: drop CAP_NET_RAW when binding to the device fails
Date: Wed, 29 Jul 2026 08:26:28 +0200 [thread overview]
Message-ID: <20260729-bugfixes-libcap-v1-1-23529745c828@narfation.org> (raw)
In-Reply-To: <20260729-bugfixes-libcap-v1-0-23529745c828@narfation.org>
netsock_open() and netsock_open4() raise the CAP_NET_RAW effective
capability around the two SO_BINDTODEVICE setsockopt() calls and drop it
again with enable_raw_bind_capability(0). When either setsockopt() fails
the "goto err" jumps straight to the socket cleanup and skipped the drop.
The daemon was then still using the CAP_NET_RAW.
Route the bind failures through a new err_bind label that drops the
capability before the existing error cleanup.
Fixes: b0877b387ba1 ("alfred: Drop capabilities when not needed")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
netsock.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/netsock.c b/netsock.c
index 60b2285..54148e2 100644
--- a/netsock.c
+++ b/netsock.c
@@ -317,14 +317,14 @@ static int netsock_open(struct globals *globals, struct interface *interface)
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, interface->interface,
strlen(interface->interface) + 1)) {
perror("can't bind to device");
- goto err;
+ goto err_bind;
}
if (setsockopt(sock_mc, SOL_SOCKET, SO_BINDTODEVICE,
interface->interface,
strlen(interface->interface) + 1)) {
perror("can't bind to device");
- goto err;
+ goto err_bind;
}
enable_raw_bind_capability(0);
@@ -408,6 +408,9 @@ static int netsock_open(struct globals *globals, struct interface *interface)
interface->netsock_mcast = sock_mc;
return 0;
+
+err_bind:
+ enable_raw_bind_capability(0);
err:
close(sock);
close(sock_mc);
@@ -464,13 +467,13 @@ static int netsock_open4(struct globals *globals, struct interface *interface)
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, interface->interface,
strlen(interface->interface) + 1)) {
perror("ipv4: can't bind to device");
- goto err;
+ goto err_bind;
}
if (setsockopt(sock_mc, SOL_SOCKET, SO_BINDTODEVICE,
interface->interface, strlen(interface->interface) + 1)) {
perror("ipv4: can't bind to device");
- goto err;
+ goto err_bind;
}
enable_raw_bind_capability(0);
@@ -558,6 +561,9 @@ static int netsock_open4(struct globals *globals, struct interface *interface)
interface->netsock_mcast = sock_mc;
return 0;
+
+err_bind:
+ enable_raw_bind_capability(0);
err:
close(sock);
close(sock_mc);
--
2.47.3
next prev parent reply other threads:[~2026-07-29 6:28 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 6:26 [PATCH 0/2] alfred: libcap: random fixes Sven Eckelmann
2026-07-29 6:26 ` Sven Eckelmann [this message]
2026-07-29 6:26 ` [PATCH 2/2] alfred: free the correct capability set when cap_init() fails Sven Eckelmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260729-bugfixes-libcap-v1-1-23529745c828@narfation.org \
--to=sven@narfation.org \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox