fix: resource leakage when loading library using dlopen
PATCH 1/1 fix: resource leakage when loading library using dlopen
dlclose is used to close the handle returned by
dlopen.
--- libxtables/xtables.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
libxtables/xtables.c +4 -1
@@ -561,6 +561,7 @@ static void *load_extension(const char *search_path, const char *af_prefix,void *ptr =NULL ;struct stat sb;char path[256 ]; +void *handle =NULL ; do { next = strchr(dir, ':');
@@ -579,7 +580,8 @@ static void *load_extension(const char *search_path, const char *af_prefix, strerror(errno));return NULL ; } -if (dlopen (path,RTLD_NOW ) ==NULL ) { + handle =dlopen (path,RTLD_NOW ); +if (handle ==NULL ) {fprintf (stderr,"%s: %s\n" , path,dlerror ());break ; }
@@ -590,6 +592,7 @@ static void *load_extension(const char *search_path, const char *af_prefix, ptr =xtables_find_match (name,XTF_DONT_LOAD ,NULL ); +dlclose (handle);if (ptr !=NULL )return ptr;
Signature · 12 lines
1.9.1 --------------------------------------------------------------------- Intel Finland Oy Registered Address: PL 281, 00181 Helsinki Business Identity Code: 0357606 - 4 Domiciled in Helsinki This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 11 lines · Imran Zaman
libxtables/xtables.c +2
-1@@ -579,7 +580,8 @@ static void *load_extension(const char *search_path, const char *af_prefix, strerror(errno));return NULL ; } -if (dlopen (path,RTLD_NOW ) ==NULL ) { + handle =dlopen (path,RTLD_NOW ); +if (handle ==NULL ) {fprintf (stderr,"%s: %s\n" , path,dlerror ());break ; }
libxtables/xtables.c static void *load_extension(const char *search_path, const char *af_prefix, · line 598
Quoted 7 lines
libxtables/xtables.c +1
-0@@ -590,6 +592,7 @@ static void *load_extension(const char *search_path, const char *af_prefix, ptr =xtables_find_match (name,XTF_DONT_LOAD ,NULL ); +dlclose (handle);if (ptr !=NULL )return ptr;
Seriously? If you close the handle, then ptr->init will point to invalid memory and the thing should crash.
Aah. ookey.. so it needs to be moved two lines below..
What about the caller of the function? is it getting closed at any stage?
________________________________________
From: Jan Engelhardt [[email protected]]
Sent: 07 September 2015 13:41
To: Zaman, Imran
Cc: [email protected]
Subject: Re: [PATCH v1 1/1] fix: resource leakage when loading library using dlopen
Quoted 11 lines · Imran Zaman
libxtables/xtables.c +2
-1@@ -579,7 +580,8 @@ static void *load_extension(const char *search_path, const char *af_prefix, strerror(errno));return NULL ; } -if (dlopen (path,RTLD_NOW ) ==NULL ) { + handle =dlopen (path,RTLD_NOW ); +if (handle ==NULL ) {fprintf (stderr,"%s: %s\n" , path,dlerror ());break ; }
libxtables/xtables.c static void *load_extension(const char *search_path, const char *af_prefix, · line 598
Quoted 7 lines
libxtables/xtables.c +1
-0@@ -590,6 +592,7 @@ static void *load_extension(const char *search_path, const char *af_prefix, ptr =xtables_find_match (name,XTF_DONT_LOAD ,NULL ); +dlclose (handle);if (ptr !=NULL )return ptr;
Seriously? If you close the handle, then ptr->init will point to invalid memory and the thing should crash.
---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki
Business Identity Code: 0357606 - 4
Domiciled in Helsinki
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 1 line · Imran
Aah. ookey.. so it needs to be moved two lines below..
At which point it's almost useless, because the program will end if the requested extension cannot be loaded.
Quoted 1 line
What about the caller of the function? is it getting closed at any stage?
Implicitly at program exit.
It's not pretty valgrind-wise, but implicit resource freeing seems a not-uncommon practice for some programs.
Well! I tend to disagree here..
Any resource should be freed immediately if it is not in use.. function does not know how all the variables on heap be handled outside of the function, if those are not dealt internally unless returned in all cases to the function caller.. Besides, static analysis of the code is reporting resource leak at this code block and it should be fixed for the same reason and will be more clearer for callers of this function. Freeing resource immediately once it is not in use is normal practice and must be followed in all cases as how I see it :)
Do you know when is 'ptr' get free'd and at which point in code? sorry i m not very familiar with all iptables codebase, so if you can guide me I can sure come up with proper fix for it..
thanks
________________________________________
From: Jan Engelhardt [[email protected]]
Sent: 07 September 2015 16:08
To: Zaman, Imran
Cc: [email protected]
Subject: RE: [PATCH v1 1/1] fix: resource leakage when loading library using dlopen
Quoted 1 line · Imran
Aah. ookey.. so it needs to be moved two lines below..
At which point it's almost useless, because the program will end if the requested extension cannot be loaded.
Quoted 1 line
What about the caller of the function? is it getting closed at any stage?
Implicitly at program exit.
It's not pretty valgrind-wise, but implicit resource freeing seems a not-uncommon practice for some programs.
---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki Business Identity Code: 0357606 - 4
Domiciled in Helsinki
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 3 lines · Imran
Well! I tend to disagree here..
Any resource should be freed immediately if it is not in use..
Yet they are in use until xtables-main.c:78 or so.
Quoted 1 line
Do you know when is 'ptr' get free'd and at which point in code?
xtables-main.c:93: exit(!ret)
You meant xtables.c as I couldnt find xtables-main.c or am I looking at wrong repo/branch
________________________________________
From: Jan Engelhardt [[email protected]]
Sent: 08 September 2015 11:55
To: Zaman, Imran
Cc: [email protected]
Subject: RE: [PATCH v1 1/1] fix: resource leakage when loading library using dlopen
Quoted 3 lines · Imran
Well! I tend to disagree here..
Any resource should be freed immediately if it is not in use..
Yet they are in use until xtables-main.c:78 or so.
Quoted 1 line
Do you know when is 'ptr' get free'd and at which point in code?
xtables-main.c:93: exit(!ret)
---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki
Business Identity Code: 0357606 - 4
Domiciled in Helsinki
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 1 line · Imran
You meant
xtables.cas I couldnt findxtables-main.cor am I looking at wrong repo/branch
xtables-standalone.c, iptables-standalone.c and ip6tables-standalone.c they are.
Quoted 8 lines
Quoted 2 lines
Well! I tend to disagree here..
Any resource should be freed immediately if it is not in use..Yet they are in use until xtables-main.c:78 or so.
Quoted 1 line
Do you know when is 'ptr' get free'd and at which point in code?
xtables-main.c:93: exit(!ret)
Pablo Neira Ayuso
maintainerQuoted 7 lines · Jan Engelhardt
Quoted 3 lines · Imran
Well! I tend to disagree here..
Any resource should be freed immediately if it is not in use..
Yet they are in use until xtables-main.c:78 or so.
This lazy resource approach is sloppy. It's a good practise to release things before leaving the program. By releasing things when we don't need them anymore, we reduce the noise in valgrind and similar tools so it becomes more easily to detect more significant problems in the software.
Imran, please fix your patch to avoid the crash that was indicated in first place and resubmit, thanks.