1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 01:55:22 +03:00

Merge pull request #10186 from yuwata/fix-10149

network: Fix memleaks found by test
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-09-28 14:20:12 +02:00 committed by GitHub
commit 22041e475f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 64 deletions

View File

@ -258,6 +258,8 @@ static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) {
free(option);
}
free(lease->root_path);
free(lease->timezone);
free(lease->hostname);
free(lease->domainname);
free(lease->dns);
@ -330,8 +332,7 @@ static int lease_parse_string(const uint8_t *option, size_t len, char **ret) {
if (!string)
return -ENOMEM;
free(*ret);
*ret = string;
free_and_replace(*ret, string);
}
return 0;

View File

@ -387,17 +387,10 @@ static void client_notify(sd_dhcp6_client *client, int event) {
client->callback(client, event, client->userdata);
}
static void client_set_lease(sd_dhcp6_client *client, sd_dhcp6_lease *lease) {
assert(client);
(void) sd_dhcp6_lease_unref(client->lease);
client->lease = sd_dhcp6_lease_ref(lease);
}
static int client_reset(sd_dhcp6_client *client) {
assert(client);
client_set_lease(client, NULL);
client->lease = sd_dhcp6_lease_unref(client->lease);
client->receive_message =
sd_event_source_unref(client->receive_message);
@ -844,8 +837,8 @@ static int client_parse_message(
uint8_t *optval;
be32_t iaid_lease;
if (len < offsetof(DHCP6Option, data) ||
len < offsetof(DHCP6Option, data) + be16toh(option->len))
if (len < pos + offsetof(DHCP6Option, data) ||
len < pos + offsetof(DHCP6Option, data) + be16toh(option->len))
return -ENOBUFS;
optcode = be16toh(option->code);
@ -1001,7 +994,7 @@ static int client_parse_message(
break;
}
pos += sizeof(*option) + optlen;
pos += offsetof(DHCP6Option, data) + optlen;
}
if (!clientid) {
@ -1061,8 +1054,8 @@ static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply, si
return 0;
}
client_set_lease(client, lease);
lease = NULL;
sd_dhcp6_lease_unref(client->lease);
client->lease = TAKE_PTR(lease);
return DHCP6_STATE_BOUND;
}
@ -1090,8 +1083,8 @@ static int client_receive_advertise(sd_dhcp6_client *client, DHCP6Message *adver
r = dhcp6_lease_get_preference(client->lease, &pref_lease);
if (r < 0 || pref_advertise > pref_lease) {
client_set_lease(client, lease);
lease = NULL;
sd_dhcp6_lease_unref(client->lease);
client->lease = TAKE_PTR(lease);
r = 0;
}

View File

@ -718,7 +718,7 @@ int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, voi
union in_addr_union to = {}, from = {};
RoutingPolicyRule *rule = NULL;
uint32_t fwmark = 0, table = 0;
char *iif = NULL, *oif = NULL;
const char *iif = NULL, *oif = NULL;
Manager *m = userdata;
uint16_t type;
int family;
@ -834,13 +834,13 @@ int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, voi
return 0;
}
r = sd_netlink_message_read_string(message, FRA_IIFNAME, (const char **) &iif);
r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
return 0;
}
r = sd_netlink_message_read_string(message, FRA_OIFNAME, (const char **) &oif);
r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
if (r < 0 && r != -ENODATA) {
log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
return 0;
@ -1470,9 +1470,8 @@ void manager_free(Manager *m) {
while ((pool = m->address_pools))
address_pool_free(pool);
set_free(m->rules);
set_free(m->rules_foreign);
set_free_with_destructor(m->rules, routing_policy_rule_free);
set_free_with_destructor(m->rules_foreign, routing_policy_rule_free);
set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
sd_netlink_unref(m->rtnl);

View File

@ -138,7 +138,7 @@ DHCP.UseTimezone, config_parse_bool,
DHCP.IAID, config_parse_iaid, 0, offsetof(Network, iaid)
DHCP.ListenPort, config_parse_uint16, 0, offsetof(Network, dhcp_client_port)
DHCP.RapidCommit, config_parse_bool, 0, offsetof(Network, rapid_commit)
DHCP.ForceDHCPv6PDOtherInformation, config_parse_bool, 0, offsetof(Network, dhcp6_force_pd_other_information)
DHCP.ForceDHCPv6PDOtherInformation, config_parse_bool, 0, offsetof(Network, dhcp6_force_pd_other_information)
IPv6AcceptRA.UseDNS, config_parse_bool, 0, offsetof(Network, ipv6_accept_ra_use_dns)
IPv6AcceptRA.UseDomains, config_parse_dhcp_use_domains, 0, offsetof(Network, ipv6_accept_ra_use_domains)
IPv6AcceptRA.RouteTable, config_parse_uint32, 0, offsetof(Network, ipv6_accept_ra_route_table)

View File

@ -80,8 +80,7 @@ void network_apply_anonymize_if_set(Network *network) {
network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC;
/* RFC 7844 3.10:
SHOULD NOT use the Vendor Class Identifier option */
/* NOTE: it was not initiallized to any value in network_load_one. */
network->dhcp_vendor_class_identifier = false;
network->dhcp_vendor_class_identifier = mfree(network->dhcp_vendor_class_identifier);
/* RFC7844 section 3.6.:
The client intending to protect its privacy SHOULD only request a
minimal number of options in the PRL and SHOULD also randomly shuffle
@ -201,10 +200,6 @@ static int network_load_one(Manager *manager, const char *filename) {
network->dhcp_client_identifier = DHCP_CLIENT_ID_DUID;
network->dhcp_route_table = RT_TABLE_MAIN;
network->dhcp_route_table_set = false;
/* NOTE: the following vars were not set to any default,
* even if they are commented in the man?
* These vars might be overwriten by network_apply_anonymize_if_set */
network->dhcp_vendor_class_identifier = false;
/* NOTE: from man: UseMTU=... Defaults to false*/
network->dhcp_use_mtu = false;
/* NOTE: from man: UseTimezone=... Defaults to "no".*/

View File

@ -17,12 +17,14 @@
int routing_policy_rule_new(RoutingPolicyRule **ret) {
RoutingPolicyRule *rule;
rule = new0(RoutingPolicyRule, 1);
rule = new(RoutingPolicyRule, 1);
if (!rule)
return -ENOMEM;
rule->family = AF_INET;
rule->table = RT_TABLE_MAIN;
*rule = (RoutingPolicyRule) {
.family = AF_INET,
.table = RT_TABLE_MAIN,
};
*ret = rule;
return 0;
@ -154,8 +156,8 @@ int routing_policy_rule_get(Manager *m,
uint8_t tos,
uint32_t fwmark,
uint32_t table,
char *iif,
char *oif,
const char *iif,
const char *oif,
RoutingPolicyRule **ret) {
RoutingPolicyRule rule, *existing;
@ -171,26 +173,22 @@ int routing_policy_rule_get(Manager *m,
.tos = tos,
.fwmark = fwmark,
.table = table,
.iif = iif,
.oif = oif
.iif = (char*) iif,
.oif = (char*) oif
};
if (m->rules) {
existing = set_get(m->rules, &rule);
if (existing) {
if (ret)
*ret = existing;
return 1;
}
existing = set_get(m->rules, &rule);
if (existing) {
if (ret)
*ret = existing;
return 1;
}
if (m->rules_foreign) {
existing = set_get(m->rules_foreign, &rule);
if (existing) {
if (ret)
*ret = existing;
return 1;
}
existing = set_get(m->rules_foreign, &rule);
if (existing) {
if (ret)
*ret = existing;
return 1;
}
return -ENOENT;
@ -224,15 +222,28 @@ static int routing_policy_rule_add_internal(Manager *m,
uint8_t tos,
uint32_t fwmark,
uint32_t table,
char *iif,
char *oif,
const char *_iif,
const char *_oif,
RoutingPolicyRule **ret) {
_cleanup_(routing_policy_rule_freep) RoutingPolicyRule *rule = NULL;
_cleanup_free_ char *iif = NULL, *oif = NULL;
int r;
assert_return(rules, -EINVAL);
if (_iif) {
iif = strdup(_iif);
if (!iif)
return -ENOMEM;
}
if (_oif) {
oif = strdup(_oif);
if (!oif)
return -ENOMEM;
}
r = routing_policy_rule_new(&rule);
if (r < 0)
return r;
@ -246,8 +257,8 @@ static int routing_policy_rule_add_internal(Manager *m,
rule->tos = tos;
rule->fwmark = fwmark;
rule->table = table;
rule->iif = iif;
rule->oif = oif;
rule->iif = TAKE_PTR(iif);
rule->oif = TAKE_PTR(oif);
r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops);
if (r < 0)
@ -274,8 +285,8 @@ int routing_policy_rule_add(Manager *m,
uint8_t tos,
uint32_t fwmark,
uint32_t table,
char *iif,
char *oif,
const char *iif,
const char *oif,
RoutingPolicyRule **ret) {
return routing_policy_rule_add_internal(m, &m->rules, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, ret);
@ -290,8 +301,8 @@ int routing_policy_rule_add_foreign(Manager *m,
uint8_t tos,
uint32_t fwmark,
uint32_t table,
char *iif,
char *oif,
const char *iif,
const char *oif,
RoutingPolicyRule **ret) {
return routing_policy_rule_add_internal(m, &m->rules_foreign, family, from, from_prefixlen, to, to_prefixlen, tos, fwmark, table, iif, oif, ret);
}
@ -392,9 +403,8 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
if (r < 0)
return r;
rule->section = n;
rule->section = TAKE_PTR(n);
rule->network = network;
n = NULL;
r = hashmap_put(network->rules_by_section, rule->section, rule);
if (r < 0)

View File

@ -54,11 +54,11 @@ int link_routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message
int link_routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata);
int routing_policy_rule_add(Manager *m, int family, const union in_addr_union *from, uint8_t from_prefixlen, const union in_addr_union *to, uint8_t to_prefixlen,
uint8_t tos, uint32_t fwmark, uint32_t table, char *iif, char *oif, RoutingPolicyRule **ret);
uint8_t tos, uint32_t fwmark, uint32_t table, const char *iif, const char *oif, RoutingPolicyRule **ret);
int routing_policy_rule_add_foreign(Manager *m, int family, const union in_addr_union *from, uint8_t from_prefixlen, const union in_addr_union *to, uint8_t to_prefixlen,
uint8_t tos, uint32_t fwmark, uint32_t table, char *iif, char *oif, RoutingPolicyRule **ret);
uint8_t tos, uint32_t fwmark, uint32_t table, const char *iif, const char *oif, RoutingPolicyRule **ret);
int routing_policy_rule_get(Manager *m, int family, const union in_addr_union *from, uint8_t from_prefixlen, const union in_addr_union *to, uint8_t to_prefixlen, uint8_t tos,
uint32_t fwmark, uint32_t table, char *iif, char *oif, RoutingPolicyRule **ret);
uint32_t fwmark, uint32_t table, const char *iif, const char *oif, RoutingPolicyRule **ret);
int routing_policy_rule_make_local(Manager *m, RoutingPolicyRule *rule);
int routing_policy_serialize_rules(Set *rules, FILE *f);
int routing_policy_load_rules(const char *state_file, Set **rules);