Skip to contentSkip to conversation

restorecond: invalidate local_lock_fd properly when closing it

PATCH In review Nicolas Iooss 2 participants 2 messages Feb 28, 2021 – Mar 4, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 restorecond: invalidate local_lock_fd properly when closing it

If flock(local_lock_fd,...) fails, in function local_server(), the file descriptor to the lock file is closed but local_lock_fd is not reset to -1. This leads to server() calling end_local_server(), which closes the file descriptor again.

Fix this double-close issue by setting local_lock_fd to -1 after closing it.

This issue was found by using Facebook's Infer static analyzer.

---
 restorecond/user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
restorecond/user.c +2 -1
index a24b8407b048..47b86823ff79 100644
@@ -230,9 +230,10 @@ static int local_server(void) {
 		return -1;
 	}
 	if (flock(local_lock_fd, LOCK_EX | LOCK_NB) < 0) {
-		close(local_lock_fd);
 		if (debug_mode)
 			perror("flock");
+		close(local_lock_fd);
+		local_lock_fd = -1;
 		return -1;
 	}
 	/* watch for stdin/terminal going away */
Signature · 1 line
2.30.0
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Nicolas Iooss

If flock(local_lock_fd,...) fails, in function local_server(), the file descriptor to the lock file is closed but local_lock_fd is not reset to -1. This leads to server() calling end_local_server(), which closes the file descriptor again.

Quoted 7 lines

Fix this double-close issue by setting local_lock_fd to -1 after closing it.

This issue was found by using Facebook's Infer static analyzer.

and merged. Thanks!

Quoted 22 lines
---
 restorecond/user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
restorecond/user.c +2 -1
index a24b8407b048..47b86823ff79 100644
@@ -230,9 +230,10 @@ static int local_server(void) {
 		return -1;
 	}
 	if (flock(local_lock_fd, LOCK_EX | LOCK_NB) < 0) {
-		close(local_lock_fd);
 		if (debug_mode)
 			perror("flock");
+		close(local_lock_fd);
+		local_lock_fd = -1;
 		return -1;
 	}
 	/* watch for stdin/terminal going away */
Signature · 1 line
2.30.0