Subject: Re: [PATCH] genl: Fix descriptor leak in get_genl_kind()

From: Donald Hunter <[email-masked]>
Date: Thu, 08 Feb 2024 13:22:27 +0000

Maks Mishin <[email-masked]> writes:

> Signed-off-by: Maks Mishin <[email-masked]>

The subject should say [PATCH iproute2] or [PATCH iproute2-next] since
it targets that project.

> ---
>  genl/genl.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/genl/genl.c b/genl/genl.c
> index 85cc73bb..74100dad 100644
> --- a/genl/genl.c
> +++ b/genl/genl.c
> @@ -71,6 +71,9 @@ static struct genl_util *get_genl_kind(const char *str)
>  	snprintf(buf, sizeof(buf), "%s_genl_util", str);
>  
>  	f = dlsym(dlh, buf);
> +	if (dlh != NULL)
> +		dlclose(dlh);

This is broken. If the earlier dlopen() actually loaded a .so then this
dlclose() will close it again, before f gets used. When f gets
dereferenced later, the program will crash. If this works at all, it is
because dlopen(NULL, ...) returns a handle to the main program, so
dlclose() doesn't unload it.

My assumption is that the author is leaving resources to be released at
program exit. It is a short-lived command line utility after all.

> +
>  	if (f == NULL)
>  		goto noexist;
>  reg: