Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix heap-use-after-free in free_fast_fallback_getaddrinfo_entry #13231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions ext/socket/ipsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,19 @@ fast_fallback_inetsock_cleanup(VALUE v)
getaddrinfo_shared->notify = -1;

int shared_need_free = 0;
int need_free[2] = { 0, 0 };
struct addrinfo *ais[arg->family_size];
for (int i = 0; i < arg->family_size; i++) ais[i] = NULL;

rb_nativethread_lock_lock(&getaddrinfo_shared->lock);
{
for (int i = 0; i < arg->family_size; i++) {
if (arg->getaddrinfo_entries[i] && --(arg->getaddrinfo_entries[i]->refcount) == 0) {
need_free[i] = 1;
struct fast_fallback_getaddrinfo_entry *getaddrinfo_entry = arg->getaddrinfo_entries[i];

if (!getaddrinfo_entry) continue;

if (--(getaddrinfo_entry->refcount) == 0) {
ais[i] = getaddrinfo_entry->ai;
getaddrinfo_entry->ai = NULL;
}
}
if (--(getaddrinfo_shared->refcount) == 0) {
Expand All @@ -1175,9 +1181,7 @@ fast_fallback_inetsock_cleanup(VALUE v)
rb_nativethread_lock_unlock(&getaddrinfo_shared->lock);

for (int i = 0; i < arg->family_size; i++) {
if (arg->getaddrinfo_entries[i] && need_free[i]) {
free_fast_fallback_getaddrinfo_entry(&arg->getaddrinfo_entries[i]);
}
if (ais[i]) freeaddrinfo(ais[i]);
}
if (getaddrinfo_shared && shared_need_free) {
free_fast_fallback_getaddrinfo_shared(&getaddrinfo_shared);
Expand Down
22 changes: 7 additions & 15 deletions ext/socket/raddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3038,22 +3038,13 @@ free_fast_fallback_getaddrinfo_shared(struct fast_fallback_getaddrinfo_shared **
*shared = NULL;
}

void
free_fast_fallback_getaddrinfo_entry(struct fast_fallback_getaddrinfo_entry **entry)
{
if ((*entry)->ai) {
freeaddrinfo((*entry)->ai);
(*entry)->ai = NULL;
}
*entry = NULL;
}

static void *
do_fast_fallback_getaddrinfo(void *ptr)
{
struct fast_fallback_getaddrinfo_entry *entry = (struct fast_fallback_getaddrinfo_entry *)ptr;
struct fast_fallback_getaddrinfo_shared *shared = entry->shared;
int err = 0, need_free = 0, shared_need_free = 0;
int err = 0, shared_need_free = 0;
struct addrinfo *ai = NULL;

sigset_t set;
sigemptyset(&set);
Expand Down Expand Up @@ -3102,14 +3093,15 @@ do_fast_fallback_getaddrinfo(void *ptr)
entry->err = errno;
entry->has_syserr = true;
}
if (--(entry->refcount) == 0) need_free = 1;
if (--(entry->refcount) == 0) {
ai = entry->ai;
entry->ai = NULL;
}
if (--(shared->refcount) == 0) shared_need_free = 1;
}
rb_nativethread_lock_unlock(&shared->lock);

if (need_free && entry) {
free_fast_fallback_getaddrinfo_entry(&entry);
}
if (ai) freeaddrinfo(ai);
if (shared_need_free && shared) {
free_fast_fallback_getaddrinfo_shared(&shared);
}
Expand Down
Loading