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 for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <4eaae372-c312-6a77-f57f-dffc5f9aff02@linuxfoundation.org>
Date:   Mon, 17 Apr 2023 15:09:12 -0600
From:   Shuah Khan <skhan@...uxfoundation.org>
To:     Hao Zeng <zenghao@...inos.cn>
Cc:     trenn@...e.com, shuah@...nel.org, linux-pm@...r.kernel.org,
        linux-kernel@...r.kernel.org,
        Shuah Khan <skhan@...uxfoundation.org>
Subject: Re: [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled()

On 4/17/23 01:56, Hao Zeng wrote:
> The sysfs_get_enabled() opened file processor not closed,
> may cause a file handle leak.
> Putting error handling and resource cleanup code together
> makes the code easy to maintain and read.
> Removed the unnecessary else if branch from the original
> function, as it should return an error in cases other than '0'.
> 
> Signed-off-by: Hao Zeng <zenghao@...inos.cn>
> Suggested-by: Shuah Khan <skhan@...uxfoundation.org>
> ---
>   tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++--------
>   1 file changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c
> index 0ce29ee4c2e4..f0334a5f1acf 100644
> --- a/tools/power/cpupower/lib/powercap.c
> +++ b/tools/power/cpupower/lib/powercap.c
> @@ -40,25 +40,31 @@ static int sysfs_get_enabled(char *path, int *mode)
>   {
>   	int fd;
>   	char yes_no;
> +	int ret = 0;
>   
>   	*mode = 0;
>   
>   	fd = open(path, O_RDONLY);
> -	if (fd == -1)
> -		return -1;
> +	if (fd == -1) {
> +		ret = -1;
> +		goto out;
> +	}
>   
>   	if (read(fd, &yes_no, 1) != 1) {
> -		close(fd);
> -		return -1;
> +		ret = -1;
> +		goto out_close;
>   	}
>   
>   	if (yes_no == '1') {
>   		*mode = 1;
> -		return 0;

You can just add goto out_close here

> -	} else if (yes_no == '0') {
> -		return 0;

Keep == '0' check and add goto out_close here
> +	} else if (yes_no != '0') {

This can be just else with the above change.

> +		ret = -1;
> +		goto out_close;
>   	}
> -	return -1;
> +out_close:
> +	close(fd);
> +out:
> +	return ret;
>   }
>   
>   int powercap_get_enabled(int *mode)

thanks,
-- Shuah

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ