diff mbox series

[1/1] restorecond: invalidate local_lock_fd properly when closing it

Message ID 20210228084809.8387-1-nicolas.iooss@m4x.org (mailing list archive)
State Accepted
Headers show
Series [1/1] restorecond: invalidate local_lock_fd properly when closing it | expand

Commit Message

Nicolas Iooss Feb. 28, 2021, 8:48 a.m. UTC
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.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
---
 restorecond/user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Petr Lautrbach March 3, 2021, 3:08 p.m. UTC | #1
Nicolas Iooss <nicolas.iooss@m4x.org> writes:

> 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.
>
> Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
>

Acked-by: Petr Lautrbach <plautrba@redhat.com>

and merged. Thanks!

> ---
>  restorecond/user.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/restorecond/user.c b/restorecond/user.c
> index a24b8407b048..47b86823ff79 100644
> --- a/restorecond/user.c
> +++ b/restorecond/user.c
> @@ -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 */
> -- 
> 2.30.0
diff mbox series

Patch

diff --git a/restorecond/user.c b/restorecond/user.c
index a24b8407b048..47b86823ff79 100644
--- a/restorecond/user.c
+++ b/restorecond/user.c
@@ -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 */