cryptomator / cryptofs

Java Filesystem Provider with integrated encryption
GNU Affero General Public License v3.0
93 stars 35 forks source link

Wrong file header used when opening two file channels #168

Closed infeo closed 1 year ago

infeo commented 1 year ago

The file header should be shared between all related OpenFileobjects (ChunkSaver, ChunkLoader and CleartextFileChannel via the file header holder to ensure consistency. But only the Chunk* classes share the header. The CleartextFileChannel, once created, uses always the same header.

This causes data corruption, if during the lifetime of the channel the file header changes. (e.g., by opening a second file channel and closing it again).

Example test code:

        @Test
        @DisplayName("Specific pattern")
        public void testPattern() throws IOException {

            try(var ch = FileChannel.open(file,READ, WRITE, CREATE_NEW)) {
                try(var ch2 = FileChannel.open(file,WRITE)) {
                }
                ch.write(ByteBuffer.wrap(new byte[]{0x1}), 0);
            }
            try(var ch = FileChannel.open(file,READ)) {
                ch.read(ByteBuffer.allocate(10),0);
            }
        }