lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <zshes4xtyir7zrrybeqj2amrxyctwqnuvx2tqyktv4vsfstxqi@pvdq2rb5m47j>
Date: Sat, 22 Feb 2025 14:01:13 -0500
From: Seyediman Seyedarab <imandevel@...il.com>
To: ritvikfoss@...il.com
Cc: shuah@...nel.org, linux-kselftest@...r.kernel.org, 
	linux-kernel@...r.kernel.org, skhan@...uxfoundation.org, 
	linux-kernel-mentees@...ts.linuxfoundation.org
Subject: Re: [PATCH v2] selftests/mount: Close 'fd' when write fails

On 25/02/22 05:42PM, ritvikfoss@...il.com wrote:
> From: Ritvik Gupta <ritvikfoss@...il.com>
> 
> 1. Close the file descriptor when write fails.
> 2. Introduce 'close_or_die' helper function to
> reduce repetition.
> 
> Signed-off-by: Ritvik Gupta <ritvikfoss@...il.com>
> ---
> Changes in v2:
>     - Fixed formatting
> 
>  .../selftests/mount/unprivileged-remount-test.c    | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/testing/selftests/mount/unprivileged-remount-test.c b/tools/testing/selftests/mount/unprivileged-remount-test.c
> index d2917054fe3a..41d7547c781d 100644
> --- a/tools/testing/selftests/mount/unprivileged-remount-test.c
> +++ b/tools/testing/selftests/mount/unprivileged-remount-test.c
> @@ -54,6 +54,14 @@ static void die(char *fmt, ...)
>  	exit(EXIT_FAILURE);
>  }
>  
> +static void close_or_die(char *filename, int fd)
> +{
> +	if (close(fd) != 0) {
> +		die("close of %s failed: %s\n",
> +		filename, strerror(errno));
> +	}
> +}
> +
>  static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
>  {
>  	char buf[4096];
> @@ -79,6 +87,7 @@ static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list
>  	}
>  	written = write(fd, buf, buf_len);
>  	if (written != buf_len) {
> +		close_or_die(filename, fd);
>  		if (written >= 0) {
>  			die("short write to %s\n", filename);
>  		} else {
> @@ -86,10 +95,7 @@ static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list
>  				filename, strerror(errno));
>  		}
>  	}
> -	if (close(fd) != 0) {
> -		die("close of %s failed: %s\n",
> -			filename, strerror(errno));
> -	}
> +	close_or_die(filename, fd);
>  }
>  
>  static void maybe_write_file(char *filename, char *fmt, ...)
> -- 
> 2.48.1
> 
> 

Closing a file right before a process exits is redundant,
since the kernel will clean it up automatically anyway.
That said, whether doing this as a best practice is arguable.

Cheers,
Seyediman

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ