Skip to contentSkip to conversation

libselinux: Always close status page fd

PATCH Superseded Petr Lautrbach 2 participants 6 messages Jan 14, 2021 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 libselinux/src/sestatus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libselinux/src/sestatus.c +2 -4
index 9ff2785d876a..6a243b7bcdfb 100644
@@ -298,11 +298,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 */
@@ -379,6 +378,7 @@ void selinux_status_close(void)
 		avc_netlink_release_fd();
 		avc_netlink_close();
 		selinux_status = NULL;
+		close(selinux_status_fd);
 		return;
 	}
 
@@ -388,7 +388,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 44 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.

---
 libselinux/src/sestatus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libselinux/src/sestatus.c +2 -5
index 9ff2785d876a..6a243b7bcdfb 100644
@@ -298,11 +298,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 */
@@ -379,6 +378,7 @@ void selinux_status_close(void)
                avc_netlink_release_fd();
                avc_netlink_close();
                selinux_status = NULL;
+               close(selinux_status_fd);
                return;
        }
 
@@ -388,7 +388,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

Nack, the fd in the mmap of the status page and the selinux_status_fd (avc mount) are different fd's.
The selinux_status_fd is for the AVC netlink socket fallback. If you drop those hunks I'd take the patch.

#2 lore raw
Quoted 41 lines · William Roberts
Quoted 39 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.

---
 libselinux/src/sestatus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libselinux/src/sestatus.c +2 -2
index 9ff2785d876a..6a243b7bcdfb 100644
@@ -298,11 +298,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 */
@@ -379,6 +378,7 @@ void selinux_status_close(void)
                avc_netlink_release_fd();
                avc_netlink_close();
                selinux_status = NULL;
+               close(selinux_status_fd);
                return;
        }
 
@@ -388,7 +388,5 @@ void selinux_status_close(void)
                munmap(selinux_status, pagesize);
        selinux_status = NULL;
 
Quoted 12 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

Nack, the fd in the mmap of the status page and the selinux_status_fd (avc mount) are different fd's.
The selinux_status_fd is for the AVC netlink socket fallback. If you drop those hunks I'd take the patch.

Sorry I misread that, I missed the assignment from fd to that variable...

Ack, this should fix that umount issue hopefully I didn't test that though.

#3 lore raw
Quoted 57 lines · William Roberts
Quoted 41 lines · William Roberts
Quoted 39 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.

---
 libselinux/src/sestatus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libselinux/src/sestatus.c +2 -2
index 9ff2785d876a..6a243b7bcdfb 100644
@@ -298,11 +298,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 */
@@ -379,6 +378,7 @@ void selinux_status_close(void)
                avc_netlink_release_fd();
                avc_netlink_close();
                selinux_status = NULL;
+               close(selinux_status_fd);
                return;
        }
 
@@ -388,7 +388,5 @@ void selinux_status_close(void)
                munmap(selinux_status, pagesize);
        selinux_status = NULL;
 
Quoted 12 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

Nack, the fd in the mmap of the status page and the selinux_status_fd (avc mount) are different fd's.
The selinux_status_fd is for the AVC netlink socket fallback. If you drop those hunks I'd take the patch.

Quoted 3 lines

Sorry I misread that, I missed the assignment from fd to that variable...

Ack, this should fix that umount issue hopefully I didn't test that though.

As an aside though, I did notice a bug in the existing code:

A failure in:
selinux_status_fd = avc_netlink_acquire_fd();

Will cause the code to still return success to the caller of selinux_status_open().

And cause the close to return EBADF which is silently ignored and doesn't matter.

#4 lore raw
In reply to William Roberts
Quoted 27 lines · William Roberts
Quoted 26 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.

---
 libselinux/src/sestatus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libselinux/src/sestatus.c +1 -2
index 9ff2785d876a..6a243b7bcdfb 100644
@@ -298,11 +298,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);
 
Quoted 8 lines

libselinux/src/sestatus.c void selinux_status_close(void) · line 383

Quoted 8 lines
libselinux/src/sestatus.c +1 -0
        /* sequence must not be changed during references */
@@ -379,6 +378,7 @@ void selinux_status_close(void)
                avc_netlink_release_fd();
                avc_netlink_close();
                selinux_status = NULL;
+               close(selinux_status_fd);
                return;
        }

I'll drop this one. It's already closed by avc_netlink_close()

Quoted 8 lines

libselinux/src/sestatus.c void selinux_status_close(void) · line 391

Quoted 8 lines
libselinux/src/sestatus.c +0 -2
@@ -388,7 +388,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);

I believe this is correct. selinux_stats_fd is not assigned when mmap() doesn't return MAP_FAILED

Quoted 9 lines
Quoted 4 lines
libselinux/src/sestatus.c +0 -1
 }
--

2.30.0

Nack, the fd in the mmap of the status page and the selinux_status_fd (avc mount) are different fd's.
The selinux_status_fd is for the AVC netlink socket fallback. If you drop those hunks I'd take the patch.

#5 lore raw
Quoted 30 lines · Petr Lautrbach
Quoted 27 lines · William Roberts
Quoted 26 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.

---
 libselinux/src/sestatus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libselinux/src/sestatus.c +1 -2
index 9ff2785d876a..6a243b7bcdfb 100644
@@ -298,11 +298,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);
 
Quoted 10 lines
Quoted 8 lines

libselinux/src/sestatus.c void selinux_status_close(void) · line 383

Quoted 8 lines
libselinux/src/sestatus.c +1 -0
        /* sequence must not be changed during references */
@@ -379,6 +378,7 @@ void selinux_status_close(void)
                avc_netlink_release_fd();
                avc_netlink_close();
                selinux_status = NULL;
+               close(selinux_status_fd);
                return;
        }

I'll drop this one. It's already closed by avc_netlink_close()

We can actually get rid of selinux_status_fd all together. The call to avc_netlink_acquire_fd() just returns it's static cache of the fd acquired in avc_netlink_open(). So we can just pair the avc_netlink_open with it's avc_netlink_code().

Quoted 23 lines
Quoted 9 lines

libselinux/src/sestatus.c void selinux_status_close(void) · line 391

Quoted 9 lines
libselinux/src/sestatus.c +0 -2
@@ -388,7 +388,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);

I believe this is correct. selinux_stats_fd is not assigned when mmap() doesn't return MAP_FAILED

Quoted 9 lines
Quoted 4 lines
libselinux/src/sestatus.c +0 -1
 }
--

2.30.0

Nack, the fd in the mmap of the status page and the selinux_status_fd (avc mount) are different fd's.
The selinux_status_fd is for the AVC netlink socket fallback. If you drop those hunks I'd take the patch.