Skip to content

Commit

Permalink
fixes #1079 Use after free panic in tcp_dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Dec 31, 2019
1 parent 092a24e commit 3c6ecbd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/platform/posix/posix_resolv_gai.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef struct resolv_item resolv_item;
struct resolv_item {
int family;
int passive;
const char * name;
char * name;
int proto;
int socktype;
uint16_t port;
Expand All @@ -67,6 +67,7 @@ resolv_cancel(nni_aio *aio, void *arg, int rv)
// so we can just discard everything.
nni_aio_list_remove(aio);
nni_mtx_unlock(&resolv_mtx);
nni_strfree(item->name);
NNI_FREE_STRUCT(item);
} else {
// This case indicates the resolver is still processing our
Expand Down Expand Up @@ -253,9 +254,18 @@ resolv_ip(const char *host, const char *serv, int passive, int family,
return;
}

// NB: host and serv must remain valid until this is completed.
// NB: must remain valid until this is completed. So we have to
// make our own copy.

if (host == NULL) {
item->name = NULL;
} else if ((item->name = nni_strdup(host)) == NULL) {
NNI_FREE_STRUCT(item);
nni_aio_finish_error(aio, NNG_ENOMEM);
return;
}

memset(&item->sa, 0, sizeof(item->sa));
item->name = host;
item->proto = proto;
item->aio = aio;
item->family = fam;
Expand All @@ -272,6 +282,7 @@ resolv_ip(const char *host, const char *serv, int passive, int family,
}
if (rv != 0) {
nni_mtx_unlock(&resolv_mtx);
nni_strfree(item->name);
NNI_FREE_STRUCT(item);
nni_aio_finish_error(aio, rv);
return;
Expand Down Expand Up @@ -332,6 +343,7 @@ resolv_worker(void *unused)
nni_aio_set_sockaddr(aio, &item->sa);
nni_aio_finish(aio, rv, 0);
}
nni_strfree(item->name);
NNI_FREE_STRUCT(item);
}
nni_mtx_unlock(&resolv_mtx);
Expand Down
14 changes: 12 additions & 2 deletions src/platform/windows/win_resolv.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef struct resolv_item resolv_item;
struct resolv_item {
int family;
int passive;
const char * name;
char * name;
int proto;
int socktype;
uint16_t port;
Expand All @@ -60,6 +60,7 @@ resolv_cancel(nni_aio *aio, void *arg, int rv)
// so we can just discard everything.
nni_aio_list_remove(aio);
nni_mtx_unlock(&resolv_mtx);
nni_strfree(item->name);
NNI_FREE_STRUCT(item);
} else {
// Resolver still working, so just unlink our AIO to
Expand Down Expand Up @@ -225,9 +226,16 @@ resolv_ip(const char *host, const char *serv, int passive, int family,
nni_aio_finish_error(aio, NNG_ENOMEM);
return;
}
if (host == NULL) {
item->name = NULL;
} else if ((item->name = nni_strdup(host)) == NULL) {
nni_aio_finish_error(aio, NNG_ENOMEM);
NNI_FREE_STRUCT(item);
return;
}

memset(&item->sa, 0, sizeof(item->sa));
item->passive = passive;
item->name = host;
item->proto = proto;
item->aio = aio;
item->family = fam;
Expand All @@ -243,6 +251,7 @@ resolv_ip(const char *host, const char *serv, int passive, int family,
}
if (rv != 0) {
nni_mtx_unlock(&resolv_mtx);
nni_strfree(item->name);
NNI_FREE_STRUCT(item);
nni_aio_finish_error(aio, rv);
return;
Expand Down Expand Up @@ -301,6 +310,7 @@ resolv_worker(void *notused)

nni_aio_finish(aio, rv, 0);
}
nni_strfree(item->name);
NNI_FREE_STRUCT(item);
}
nni_mtx_unlock(&resolv_mtx);
Expand Down
4 changes: 2 additions & 2 deletions src/supplemental/tcp/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ tcp_dialer_free(void *arg)
nni_tcp_dialer_close(d->d);
nni_tcp_dialer_fini(d->d);
}
nni_strfree(d->host);
nni_strfree(d->port);
nni_aio_fini(d->resaio);
nni_aio_fini(d->conaio);
nni_mtx_fini(&d->mtx);
nni_strfree(d->host);
nni_strfree(d->port);
NNI_FREE_STRUCT(d);
}

Expand Down

0 comments on commit 3c6ecbd

Please sign in to comment.