Resource leak in getaddrinfo
Filip Ochnik
filip@sumologic.com
Mon Jan 20 16:10:00 GMT 2020
Hi,
I'm debugging an issue with stale resolv.conf cache in libc. I have a question about a code fragment in getaddrinfo.c which, if I understand it correctly, leaks resources.
My current understanding is that each call to __resolv_context_get should be matched by a call to __resolv_context_put, otherwise the resolv context may never be freed.
Looking at the function getaddrinfo.c:gaih_inet I see that it calls __resolv_context_get in https://sourceware.org/git/gitweb.cgi?p=glibc.git;a=blob;f=sysdeps/posix/getaddrinfo.c;h=f813d85aa39d80a9f23f95368854213ebd60b46b;hb=HEAD#l746
It is then possible to arrive at the call site of gethosts macro without calling __resolv_context_put
here:
https://sourceware.org/git/gitweb.cgi?p=glibc.git;a=blob;f=sysdeps/posix/getaddrinfo.c;h=f813d85aa39d80a9f23f95368854213ebd60b46b;hb=HEAD#l849
or here:
https://sourceware.org/git/gitweb.cgi?p=glibc.git;a=blob;f=sysdeps/posix/getaddrinfo.c;h=f813d85aa39d80a9f23f95368854213ebd60b46b;hb=HEAD#l861
Next, in gethosts, we have a lot of branches, but it's possible to arrive here
https://sourceware.org/git/gitweb.cgi?p=glibc.git;a=blob;f=sysdeps/posix/getaddrinfo.c;h=f813d85aa39d80a9f23f95368854213ebd60b46b;hb=HEAD#l291
where the code jumps to free_and_return, without calling __resolv_context_put as in other branches.
Finally, free_and_return does not call __resolv_context_put either: https://sourceware.org/git/gitweb.cgi?p=glibc.git;a=blob;f=sysdeps/posix/getaddrinfo.c;h=f813d85aa39d80a9f23f95368854213ebd60b46b;hb=HEAD#l1116
So it seems that in certain scenarios (I don't know what they are as I don't understand the code well enough) we can call __resolv_context_get without matching __resolv_context_put and thus never free the underlying context.
Am I reading this correctly? Is this a bug?
Thanks,
Filip
More information about the Libc-help
mailing list