803fb7
From 2ae0271ada810c06c12755699f0db955fc51061d Mon Sep 17 00:00:00 2001
803fb7
From: Petr Lautrbach <plautrba@redhat.com>
803fb7
Date: Thu, 10 Mar 2016 10:19:56 +0100
803fb7
Subject: [PATCH] socket_address_listen - do not rely on errno
803fb7
803fb7
Currently socket_address_listen() calls mac_selinux_bind() to bind a UNIX
803fb7
socket and checks its return value and errno for EADDRINUSE. This is not
803fb7
correct. When there's an SELinux context change made for the new socket,
803fb7
bind() is not the last function called in mac_selinux_bind(). In that
803fb7
case the last call is setfscreatecon() from libselinux which can change
803fb7
errno as it uses access() to check if /proc/thread-self is available.
803fb7
It fails on kernels before 3.17 and errno is set to ENOENT.
803fb7
803fb7
It's safe to check only the return value at it's set to -errno.
803fb7
803fb7
Cherry-picked from: a0c9496cc826957fe0f3926f619e073f17a9ab4d
803fb7
Resolves: #1316452
803fb7
---
803fb7
 src/shared/socket-label.c | 2 +-
803fb7
 1 file changed, 1 insertion(+), 1 deletion(-)
803fb7
803fb7
diff --git a/src/shared/socket-label.c b/src/shared/socket-label.c
803fb7
index a6289eb50..713e71ba3 100644
803fb7
--- a/src/shared/socket-label.c
803fb7
+++ b/src/shared/socket-label.c
803fb7
@@ -119,7 +119,7 @@ int socket_address_listen(
803fb7
 
803fb7
                 r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size);
803fb7
 
803fb7
-                if (r < 0 && errno == EADDRINUSE) {
803fb7
+                if (r == -EADDRINUSE) {
803fb7
                         /* Unlink and try again */
803fb7
                         unlink(a->sockaddr.un.sun_path);
803fb7
                         r = bind(fd, &a->sockaddr.sa, a->size);