Skip to contentSkip to conversation

alfred: libcap: random fixes

PATCH New Sven Eckelmann 1 participant 3 messages Jul 29

lore
Share & tools
lore
#0 lore raw

cover alfred: libcap: random fixes

Similar to the batctl fixes, I have now compiled a list of potential fixes for alfred. Instead of posting everything at once, I will be posted in smaller chunks. They are just various (mostly minor) problems which should be tackled at some point.

---
Sven Eckelmann (2):
alfred: drop CAP_NET_RAW when binding to the device fails
alfred: free the correct capability set when cap_init() fails

 main.c    |  2 +-
 netsock.c | 14 ++++++++++----
 2 files changed, 11 insertions(+), 5 deletions(-)

---
base-commit: 462a0cf40615f62e0912c36d72a75accbb26f5ee
change-id: 20260729-bugfixes-libcap-915682f4dc31

Best regards,

Signature · 1 line
Sven Eckelmann <[email protected]>
#1 lore raw

PATCH 1/2 alfred: drop CAP_NET_RAW when binding to the device fails

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")
---
 netsock.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
netsock.c +10 -4
index 60b2285..54148e2 100644
@@ -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);
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 alfred: free the correct capability set when cap_init() fails

In reply to Sven Eckelmann

reduce_capabilities() first allocates the current capability state with cap_get_proc() into cap_cur and then allocates a fresh capability set with cap_init() into cap_new. When the latter fails it tries to deallocate the NULL cap_new.

Free cap_cur instead to avoid a leak.

Fixes b0877b387ba1 ("alfred: Drop capabilities when not needed")
---
 main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
main.c +1 -1
index 9c38fd7..07e3cca 100644
@@ -84,7 +84,7 @@ static int reduce_capabilities(void)
 	cap_new = cap_init();
 	if (!cap_new) {
 		perror("cap_init");
-		cap_free(cap_new);
+		cap_free(cap_cur);
 		return -1;
 	}
 
Signature · 1 line
2.47.3