Skip to contentSkip to conversation

tc: Fix descriptor leak in get_qdisc_kind()

PATCH In review Maks Mishin 3 participants 3 messages Feb 7, 2024 – Feb 8, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tc: Fix descriptor leak in get_qdisc_kind()

Add closure of fd dlh and fix incorrect comparison of dlh with NULL.

Found by RASU JSC

---
 tc/tc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tc/tc.c +4 -1
index 575157a8..162700b2 100644
@@ -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) {
 		/* 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);
+
 	if (q == NULL)
 		goto noexist;
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 3 lines · Maks Mishin

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.

Quoted 10 lines
---
 tc/tc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tc/tc.c +0 -0
index 575157a8..162700b2 100644

tc/tc.c struct qdisc_util *get_qdisc_kind(const char *str) · line 115

Quoted 5 lines
tc/tc.c +1 -1
@@ -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.

tc/tc.c struct qdisc_util *get_qdisc_kind(const char *str) · line 128

Quoted 9 lines
tc/tc.c +2 -0
 		/* 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.

Quoted 3 lines
tc/tc.c +1 -0
+
 	if (q == NULL)
 		goto noexist;
#2 lore raw
In reply to Maks Mishin
Quoted 23 lines · On Thu, 8 Feb 2024 00:00:06 +0300 Maks Mishin <[email protected]> wrote:

Add closure of fd dlh and fix incorrect comparison of dlh with NULL.

Found by RASU JSC

---
 tc/tc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tc/tc.c +1 -1
index 575157a8..162700b2 100644
@@ -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) {
 		/* look in current binary, only open once */
 		dlh = BODY;
 		if (dlh == NULL) {

tc/tc.c struct qdisc_util *get_qdisc_kind(const char *str) · line 132

Quoted 9 lines
tc/tc.c +3 -0
@@ -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);
+
 	if (q == NULL)
 		goto noexist;
 

This does make sense, but the patch method is messy. Please don't modify code outside scope of the fix being addressed. And since the check for dlh being NULL is already done above, the conditional there is irrelevant.

Also, this change will break the code. If you read carefully in the comments, there is a hint. The idea is that if the qdisc is not in the external file, then it does an internal dlopen() and it wants to keep it open in the variable BODY.

Your change would close dlh which since it same as BODY would close the internal handle. That would cause next call to get_qdisc_kind() to look at an invalid handle (BODY).

I appreciate your efforts, but you need to read deeper. This is 30 year old code and the leak may not be important or be intentional.