ipc: make sure userspace communication frees wgdevice

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-12-02 19:19:38 +01:00
parent 207aeed010
commit faa55d8b19
2 changed files with 16 additions and 11 deletions

View File

@ -303,8 +303,12 @@ static int userspace_get_device(struct wgdevice **out, const char *iface)
return -errno; return -errno;
f = userspace_interface_file(iface); f = userspace_interface_file(iface);
if (!f) if (!f) {
return -errno; ret = -errno;
free(dev);
*out = NULL;
return ret;
}
fprintf(f, "get=1\n\n"); fprintf(f, "get=1\n\n");
fflush(f); fflush(f);
@ -314,11 +318,8 @@ static int userspace_get_device(struct wgdevice **out, const char *iface)
while (getline(&key, &line_buffer_len, f) > 0) { while (getline(&key, &line_buffer_len, f) > 0) {
line_len = strlen(key); line_len = strlen(key);
if (line_len == 1 && key[0] == '\n') { if (line_len == 1 && key[0] == '\n')
free(key); goto err;
fclose(f);
return ret;
}
value = strchr(key, '='); value = strchr(key, '=');
if (!value || line_len == 0 || key[line_len - 1] != '\n') if (!value || line_len == 0 || key[line_len - 1] != '\n')
break; break;
@ -382,7 +383,7 @@ static int userspace_get_device(struct wgdevice **out, const char *iface)
*end++ = '\0'; *end++ = '\0';
} }
if (getaddrinfo(begin, end, &hints, &resolved) != 0) { if (getaddrinfo(begin, end, &hints, &resolved) != 0) {
errno = ENETUNREACH; ret = ENETUNREACH;
goto err; goto err;
} }
if ((resolved->ai_family == AF_INET && resolved->ai_addrlen == sizeof(struct sockaddr_in)) || if ((resolved->ai_family == AF_INET && resolved->ai_addrlen == sizeof(struct sockaddr_in)) ||
@ -437,8 +438,10 @@ static int userspace_get_device(struct wgdevice **out, const char *iface)
ret = -EPROTO; ret = -EPROTO;
err: err:
free(key); free(key);
free_wgdevice(dev); if (ret) {
*out = NULL; free_wgdevice(dev);
*out = NULL;
}
fclose(f); fclose(f);
errno = -ret; errno = -ret;
return ret; return ret;

View File

@ -45,8 +45,10 @@ static bool sync_conf(struct wgdevice *file)
return false; return false;
} }
if (!runtime->first_peer) if (!runtime->first_peer) {
free_wgdevice(runtime);
return true; return true;
}
file->flags &= ~WGDEVICE_REPLACE_PEERS; file->flags &= ~WGDEVICE_REPLACE_PEERS;