Skip to content

Truncating a channel which is currently written can create invalid ciphertext #269

Closed

Description

@infeo

Summary

Library version: 2.7.1

Truncating a channel which is currently written to can create invalid ciphertext/unreadable file.

Description

In our test suite exists a test, which truncates a file while another thread writes data to the same file:

public void testConcurrentWriteAndTruncate() throws IOException {
AtomicBoolean keepWriting = new AtomicBoolean(true);
ByteBuffer buf = ByteBuffer.wrap("the quick brown fox jumps over the lazy dog".getBytes(StandardCharsets.UTF_8));
var executor = Executors.newCachedThreadPool();
try (FileChannel writingChannel = FileChannel.open(file, WRITE, CREATE)) {
executor.submit(() -> {
while (keepWriting.get()) {
try {
writingChannel.write(buf);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
buf.flip();
}
});
try (FileChannel truncatingChannel = FileChannel.open(file, WRITE, TRUNCATE_EXISTING)) {
keepWriting.set(false);
}
executor.shutdown();
}
Assertions.assertDoesNotThrow(() -> {
try (FileChannel readingChannel = FileChannel.open(file, READ)) {
var dst = ByteBuffer.allocate(buf.capacity());
readingChannel.read(dst);
}
});
}

This test fails in very rare cases, e.g. in CI run 12694328432. If the logs are not available anymore, you can download them here

You can trigger the failure with higher probability, if you add a new line after line 634 with Thread.sleep(2000);.

Activity

  1. added this to the next milestone on Jan 10, 2025
  2. self-assigned this
    on Jan 10, 2025
  3. infeo commented on Jan 10, 2025

    @infeo
    MemberAuthor

    I'm pretty sure we have a synchronization issue here. When opening the truncating channel, we directly work on the ciphertextchannel and only evict stale chunks:

    if (options.truncateExisting()) {
    chunkCache.invalidateStale();
    ciphertextFileChannel.truncate(cryptor.fileHeaderCryptor().headerSize());
    fileSize.set(0);
    }

    Looking into the truncate method of the cleartext channel, there are way more things done:

    protected void truncateLocked(long newSize) throws IOException {
    Preconditions.checkArgument(newSize >= 0);
    if (newSize < fileSize.get()) {
    int cleartextChunkSize = cryptor.fileContentCryptor().cleartextChunkSize();
    long indexOfLastChunk = (newSize + cleartextChunkSize - 1) / cleartextChunkSize - 1;
    int sizeOfIncompleteChunk = (int) (newSize % cleartextChunkSize); // known to fit in int, because cleartextChunkSize is int
    if (sizeOfIncompleteChunk > 0) {
    try (var chunk = chunkCache.getChunk(indexOfLastChunk)) {
    chunk.data().limit(sizeOfIncompleteChunk);
    chunk.dirty().set(true);
    }
    }
    long ciphertextFileSize = cryptor.fileHeaderCryptor().headerSize() + cryptor.fileContentCryptor().ciphertextSize(newSize);
    chunkCache.flush();
    chunkCache.invalidateStale(); // make sure no chunks _after_ newSize exist that would otherwise be written during the next cache eviction
    ciphertextFileChannel.truncate(ciphertextFileSize);
    position = min(newSize, position);
    fileSize.set(newSize);
    lastModified.set(Instant.now());
    }
    }

  4. added a commit that references this issue on Jan 10, 2025
    1aab041
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions