mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
net: leaseshelper: Refactor copying of old entries to avoid double free
When copying entries from the old lease file into the new array the old code would copy the pointer of the json object into the second array without removing it from the first. Afterwards when both arrays were freed this might lead to a crash due to access of already freed memory. Refactor the code to use the new array item stealing helper added to the json code so that the entry resides just in one array.
This commit is contained in:
parent
45d51681ce
commit
0657ed2a5c
@ -116,7 +116,6 @@ main(int argc, char **argv)
|
|||||||
long long currtime = 0;
|
long long currtime = 0;
|
||||||
long long expirytime = 0;
|
long long expirytime = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
int size = 0;
|
|
||||||
int action = -1;
|
int action = -1;
|
||||||
int pid_file_fd = -1;
|
int pid_file_fd = -1;
|
||||||
int rv = EXIT_FAILURE;
|
int rv = EXIT_FAILURE;
|
||||||
@ -270,20 +269,6 @@ main(int argc, char **argv)
|
|||||||
virLeaseActionTypeToString(action));
|
virLeaseActionTypeToString(action));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
/* Check for previous leases */
|
|
||||||
if (custom_lease_file_len) {
|
|
||||||
if (!(leases_array = virJSONValueFromString(lease_entries))) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("invalid json in file: %s, rewriting it"),
|
|
||||||
custom_lease_file);
|
|
||||||
} else {
|
|
||||||
if ((size = virJSONValueArraySize(leases_array)) < 0) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
||||||
_("couldn't fetch array of leases"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(leases_array_new = virJSONValueNewArray())) {
|
if (!(leases_array_new = virJSONValueNewArray())) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
@ -293,36 +278,58 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
currtime = (long long) time(NULL);
|
currtime = (long long) time(NULL);
|
||||||
|
|
||||||
for (i = 0; i < size; i++) {
|
/* Check for previous leases */
|
||||||
const char *ip_tmp = NULL;
|
if (custom_lease_file_len) {
|
||||||
long long expirytime_tmp = -1;
|
if (!(leases_array = virJSONValueFromString(lease_entries))) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("invalid json in file: %s, rewriting it"),
|
||||||
|
custom_lease_file);
|
||||||
|
} else {
|
||||||
|
if (!virJSONValueIsArray(leases_array)) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
|
_("couldn't fetch array of leases"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
if (!(lease_tmp = virJSONValueArrayGet(leases_array, i))) {
|
i = 0;
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
while (i < virJSONValueArraySize(leases_array)) {
|
||||||
_("failed to parse json"));
|
const char *ip_tmp = NULL;
|
||||||
goto cleanup;
|
long long expirytime_tmp = -1;
|
||||||
}
|
|
||||||
|
|
||||||
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address")) ||
|
if (!(lease_tmp = virJSONValueArrayGet(leases_array, i))) {
|
||||||
(virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", &expirytime_tmp) < 0)) {
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
_("failed to parse json"));
|
||||||
_("failed to parse json"));
|
goto cleanup;
|
||||||
goto cleanup;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Check whether lease has expired or not */
|
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address")) ||
|
||||||
if (expirytime_tmp < currtime)
|
(virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", &expirytime_tmp) < 0)) {
|
||||||
continue;
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
|
_("failed to parse json"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
/* Check whether lease has to be included or not */
|
/* Check whether lease has expired or not */
|
||||||
if (delete && STREQ(ip_tmp, ip))
|
if (expirytime_tmp < currtime) {
|
||||||
continue;
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
/* Add old lease to new array */
|
/* Check whether lease has to be included or not */
|
||||||
if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) {
|
if (delete && STREQ(ip_tmp, ip)) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
i++;
|
||||||
_("failed to create json"));
|
continue;
|
||||||
goto cleanup;
|
}
|
||||||
|
|
||||||
|
/* Move old lease to new array */
|
||||||
|
if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
|
_("failed to create json"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ignore_value(virJSONValueArraySteal(leases_array, i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user