Subject: Re: [PATCH] tc: Fix descriptor leak in get_qdisc_kind()

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

Maks Mishin <[email-masked]> writes:

> Add closure of fd `dlh` and fix incorrect comparison of `dlh` with NULL.
>
> Found by RASU JSC

What is this tool? It seems to be giving you bad advice.

> Signed-off-by: Maks Mishin <[email-masked]>
> ---
>  tc/tc.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tc/tc.c b/tc/tc.c
> index 575157a8..162700b2 100644
> --- a/tc/tc.c
> +++ b/tc/tc.c
> @@ -112,7 +112,7 @@ struct qdisc_util *get_qdisc_kind(const char *str)
>  
>  	snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
>  	dlh = dlopen(buf, RTLD_LAZY);
> -	if (!dlh) {
> +	if (dlh != NULL) {

if (!dlh) seems to be the preferred style in the iproute2 codebase, with
2000+ occurrences.

>  		/* look in current binary, only open once */
>  		dlh = BODY;
>  		if (dlh == NULL) {
> @@ -124,6 +124,9 @@ struct qdisc_util *get_qdisc_kind(const char *str)
>  
>  	snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
>  	q = dlsym(dlh, buf);
> +	if (dlh != NULL)
> +		dlclose(dlh);

Incorrect placement of dlclose() before sym q gets dereferenced.

> +
>  	if (q == NULL)
>  		goto noexist;