Skip to contentSkip to conversation

libselinux: Always close status page fd

PATCH In review Petr Lautrbach 2 participants 3 messages Jan 14, 2021 – Jan 18, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libselinux: Always close status page fd

According to mmap(2) after the mmap() call has returned, the file descriptor, fd, can be closed immediately without invalidating the mapping.

---

Changes against v1:
- selinux_status_fd is completely droped as it's actually unused

 libselinux/src/sestatus.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
libselinux/src/sestatus.c +1 -6
index 9ff2785d876a..12b015e088ea 100644
@@ -37,7 +37,6 @@ struct selinux_status_t
  * Valid Pointer : opened and mapped correctly
  */
 static struct selinux_status_t *selinux_status = NULL;
-static int			selinux_status_fd;
 static uint32_t			last_seqno;
 static uint32_t			last_policyload;
 
@@ -298,11 +297,10 @@ int selinux_status_open(int fallback)
 		goto error;
 
 	selinux_status = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
+	close(fd);
 	if (selinux_status == MAP_FAILED) {
-		close(fd);
 		goto error;
 	}
-	selinux_status_fd = fd;
 	last_seqno = (uint32_t)(-1);
 
 	/* sequence must not be changed during references */
@@ -336,7 +334,6 @@ error:
 
 		/* mark as fallback mode */
 		selinux_status = MAP_FAILED;
-		selinux_status_fd = avc_netlink_acquire_fd();
 		last_seqno = (uint32_t)(-1);
 
 		if (avc_using_threads)
@@ -388,7 +385,5 @@ void selinux_status_close(void)
 		munmap(selinux_status, pagesize);
 	selinux_status = NULL;
 
-	close(selinux_status_fd);
-	selinux_status_fd = -1;
 	last_seqno = (uint32_t)(-1);
 }
Signature · 1 line
2.30.0
#1 lore raw
Quoted 56 lines · Petr Lautrbach

According to mmap(2) after the mmap() call has returned, the file descriptor, fd, can be closed immediately without invalidating the mapping.

---

Changes against v1:
- selinux_status_fd is completely droped as it's actually unused

 libselinux/src/sestatus.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
libselinux/src/sestatus.c +1 -7
index 9ff2785d876a..12b015e088ea 100644
@@ -37,7 +37,6 @@ struct selinux_status_t
  * Valid Pointer : opened and mapped correctly
  */
 static struct selinux_status_t *selinux_status = NULL;
-static int                     selinux_status_fd;
 static uint32_t                        last_seqno;
 static uint32_t                        last_policyload;
 
@@ -298,11 +297,10 @@ int selinux_status_open(int fallback)
                goto error;
 
        selinux_status = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
+       close(fd);
        if (selinux_status == MAP_FAILED) {
-               close(fd);
                goto error;
        }
-       selinux_status_fd = fd;
        last_seqno = (uint32_t)(-1);
 
        /* sequence must not be changed during references */
@@ -336,7 +334,6 @@ error:
 
                /* mark as fallback mode */
                selinux_status = MAP_FAILED;
-               selinux_status_fd = avc_netlink_acquire_fd();
                last_seqno = (uint32_t)(-1);
 
                if (avc_using_threads)
@@ -388,7 +385,5 @@ void selinux_status_close(void)
                munmap(selinux_status, pagesize);
        selinux_status = NULL;
 
-       close(selinux_status_fd);
-       selinux_status_fd = -1;
        last_seqno = (uint32_t)(-1);
 }
--
Quoted 2 lines

2.30.0

ack and staged. I love a negative diffstat. Thanks Petr.
https://github.com/SELinuxProject/selinux/pull/279

#2 lore raw
Quoted 53 lines · William Roberts
Quoted 51 lines · Petr Lautrbach

According to mmap(2) after the mmap() call has returned, the file descriptor, fd, can be closed immediately without invalidating the mapping.

---

Changes against v1:
- selinux_status_fd is completely droped as it's actually unused

 libselinux/src/sestatus.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
libselinux/src/sestatus.c +1 -4
index 9ff2785d876a..12b015e088ea 100644
@@ -37,7 +37,6 @@ struct selinux_status_t
  * Valid Pointer : opened and mapped correctly
  */
 static struct selinux_status_t *selinux_status = NULL;
-static int                     selinux_status_fd;
 static uint32_t                        last_seqno;
 static uint32_t                        last_policyload;
 
@@ -298,11 +297,10 @@ int selinux_status_open(int fallback)
                goto error;
 
        selinux_status = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
+       close(fd);
        if (selinux_status == MAP_FAILED) {
-               close(fd);
                goto error;
        }
-       selinux_status_fd = fd;
        last_seqno = (uint32_t)(-1);
 
        /* sequence must not be changed during references */
@@ -336,7 +334,6 @@ error:
 
                /* mark as fallback mode */
                selinux_status = MAP_FAILED;
-               selinux_status_fd = avc_netlink_acquire_fd();
                last_seqno = (uint32_t)(-1);
 
                if (avc_using_threads)
@@ -388,7 +385,5 @@ void selinux_status_close(void)
                munmap(selinux_status, pagesize);
        selinux_status = NULL;
 
Quoted 10 lines
Quoted 7 lines
libselinux/src/sestatus.c +0 -3
-       close(selinux_status_fd);
-       selinux_status_fd = -1;
        last_seqno = (uint32_t)(-1);
 }
--

2.30.0

ack and staged. I love a negative diffstat. Thanks Petr.
https://github.com/SELinuxProject/selinux/pull/279

merged