Skip to content

Commit 7a6d5d7

Browse files
committed
<fix>[kvm]: use CAS remove to fix TOCTOU race in orphaned skip VM cleanup
Resolves: ZSTAC-80821 Change-Id: I59284c4e69f5d2ee357b1836b7c243200e30949a
1 parent 8c8ed73 commit 7a6d5d7

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

plugin/kvm/src/main/java/org/zstack/kvm/KvmVmSyncPingTask.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public boolean isVmDoNotNeedToTrace(String vmUuid) {
497497
return true;
498498
} else {
499499
// Expired, clean up
500-
orphanedSkipVms.remove(vmUuid);
500+
orphanedSkipVms.remove(vmUuid, orphanedAt);
501501
logger.info(String.format("orphaned skip entry for VM[uuid:%s] expired after %d minutes, resuming trace",
502502
vmUuid, ORPHAN_TTL_MS / 60000));
503503
}
@@ -513,11 +513,9 @@ private void cleanupExpiredOrphanedSkipVms() {
513513
}
514514

515515
long now = System.currentTimeMillis();
516-
Iterator<Map.Entry<String, Long>> it = orphanedSkipVms.entrySet().iterator();
517-
while (it.hasNext()) {
518-
Map.Entry<String, Long> entry = it.next();
516+
for (Map.Entry<String, Long> entry : orphanedSkipVms.entrySet()) {
519517
if (now - entry.getValue() >= ORPHAN_TTL_MS) {
520-
it.remove();
518+
orphanedSkipVms.remove(entry.getKey(), entry.getValue());
521519
logger.info(String.format("cleaned up expired orphaned skip entry for VM[uuid:%s]", entry.getKey()));
522520
}
523521
}

0 commit comments

Comments
 (0)