Skip to content

Commit 851095a

Browse files
apply coderabbit suggestions:
* ENOTSUP for xattr names * damaged symlinks in listings
1 parent 3c82e99 commit 851095a

6 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎vfs-frontend-fuse/src/main/java/org/cryptomator/vfs/frontend/fuse/FuseVfsOperations.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ public int getxattr(String fusePath, String name, ByteBuffer value) {
434434
return size;
435435
} catch (IOException e) {
436436
return toXattrErrno(e);
437+
} catch (UnsupportedOperationException _) {
438+
return -errno.enotsup(); // e.g. a name outside the user. namespace on Linux
437439
}
438440
}
439441

@@ -448,6 +450,8 @@ public int setxattr(String fusePath, String name, ByteBuffer value, int flags) {
448450
return 0;
449451
} catch (IOException e) {
450452
return toXattrErrno(e);
453+
} catch (UnsupportedOperationException _) {
454+
return -errno.enotsup(); // e.g. a name outside the user. namespace on Linux
451455
}
452456
}
453457

@@ -471,6 +475,8 @@ public int listxattr(String fusePath, ByteBuffer list) {
471475
return size;
472476
} catch (IOException e) {
473477
return toXattrErrno(e);
478+
} catch (UnsupportedOperationException _) {
479+
return -errno.enotsup(); // e.g. a name outside the user. namespace on Linux
474480
}
475481
}
476482

@@ -481,6 +487,8 @@ public int removexattr(String fusePath, String name) {
481487
return 0;
482488
} catch (IOException e) {
483489
return toXattrErrno(e);
490+
} catch (UnsupportedOperationException _) {
491+
return -errno.enotsup(); // e.g. a name outside the user. namespace on Linux
484492
}
485493
}
486494

‎vfs-frontend-fuse/src/test/java/org/cryptomator/vfs/frontend/fuse/FuseVfsOperationsTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class FuseVfsOperationsTest {
4646
private static final int EINVAL = 22;
4747
private static final int ERANGE = 34;
4848
private static final int ENOATTR = 93;
49+
private static final int ENOTSUP = 95;
4950

5051
private VfsFileSystem fs;
5152
private Errno errno;
@@ -60,6 +61,7 @@ void setUp() {
6061
Mockito.doReturn(EINVAL).when(errno).einval();
6162
Mockito.doReturn(ERANGE).when(errno).erange();
6263
Mockito.doReturn(ENOATTR).when(errno).enoattr();
64+
Mockito.doReturn(ENOTSUP).when(errno).enotsup();
6365
Mockito.doReturn(VfsCapabilities.MINIMAL).when(fs).capabilities();
6466
ops = new FuseVfsOperations(fs, errno);
6567
}
@@ -423,6 +425,16 @@ void getxattrMissing() throws IOException {
423425
assertEquals(-ENOATTR, ops.getxattr("/f", "user.a", ByteBuffer.allocate(0)));
424426
}
425427

428+
@Test
429+
@DisplayName("an unsupported attribute name maps to ENOTSUP")
430+
void unsupportedName() throws IOException {
431+
Mockito.doThrow(new UnsupportedOperationException("not in the user namespace")).when(fs).getXattr("/f", "security.selinux");
432+
Mockito.doThrow(new UnsupportedOperationException("not in the user namespace")).when(fs).removeXattr("/f", "security.selinux");
433+
434+
assertEquals(-ENOTSUP, ops.getxattr("/f", "security.selinux", ByteBuffer.allocate(0)));
435+
assertEquals(-ENOTSUP, ops.removexattr("/f", "security.selinux"));
436+
}
437+
426438
@Test
427439
@DisplayName("setxattr maps the platform's flags to policies")
428440
void setxattrFlags() throws IOException {

‎vfs-middleware-uvf/src/main/java/org/cryptomator/vfs/middleware/uvf/UvfFileSystem.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private VfsNodeMetadata toCleartextMetadata(ResolvedPath resolved, VfsNodeMetada
396396
long size = switch (type) {
397397
case FILE -> cleartextSize(resolved, entryMetadata.size());
398398
case DIRECTORY -> entryMetadata.size();
399-
case SYMBOLIC_LINK -> symlinkTarget(resolved).getBytes(StandardCharsets.UTF_8).length;
399+
case SYMBOLIC_LINK -> symlinkTargetLength(resolved);
400400
};
401401
return new VfsNodeMetadata(
402402
type,
@@ -431,11 +431,21 @@ private VaultDirectory directoryOf(ResolvedPath resolved) throws IOException {
431431
}
432432
}
433433

434+
// a damaged link must not take the whole directory listing down with it either, so it is reported as empty; reading it still fails
435+
private long symlinkTargetLength(ResolvedPath resolved) {
436+
try {
437+
return symlinkTarget(resolved).getBytes(StandardCharsets.UTF_8).length;
438+
} catch (IOException e) { // unauthentic, malformed or oversized symlink.uvf
439+
LOG.warn("Reporting symbolic link {} as empty, its target cannot be read: {}", resolved.symlinkFilePath(), e.getMessage());
440+
return 0;
441+
}
442+
}
443+
434444
private String symlinkTarget(ResolvedPath resolved) throws IOException {
435445
try {
436446
return resolved.node().symlinkTarget(reader);
437-
} catch (AuthenticationFailedException e) {
438-
throw new IOException("Unauthentic symbolic link", e);
447+
} catch (AuthenticationFailedException | IllegalArgumentException e) { // unauthentic or malformed symlink.uvf
448+
throw new IOException("Invalid symbolic link", e);
439449
}
440450
}
441451

‎vfs-middleware-uvf/src/test/java/org/cryptomator/vfs/middleware/uvf/UvfFileSystemTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,22 @@ void roundTrip() throws IOException {
492492
assertThrows(NoSuchFileException.class, () -> fs.readNodeMetadata("/link"));
493493
}
494494

495+
@Test
496+
@DisplayName("a damaged link is listed as empty, reading it fails")
497+
void damagedLink() throws IOException {
498+
fs.createSymbolicLink("/link", "../target");
499+
var symlinkFile = ciphertext.paths().stream().filter(p -> p.endsWith("/symlink.uvf")).findFirst().orElseThrow();
500+
var garbage = new byte[128]; // long enough to be parsed, so it fails authentication rather than length validation
501+
csprng.nextBytes(garbage);
502+
ciphertext.writeFile(symlinkFile, garbage);
503+
504+
var entry = list("/").get("link");
505+
506+
assertEquals(VfsNodeType.SYMBOLIC_LINK, entry.metadata().type());
507+
assertEquals(0, entry.metadata().size());
508+
assertThrows(IOException.class, () -> fs.readSymbolicLink("/link"));
509+
}
510+
495511
@Test
496512
@DisplayName("readSymbolicLink on a file fails")
497513
void notALink() throws IOException {

‎vfs-test/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Executable wiring for manual end-to-end testing.
44

5-
`VfsTestMain <vaultDir> <mountPoint> [fuseLibraryPath]` mounts the vault in `vaultDir`. If neither `vault.uvf` nor `d/` exist there, a new password-protected vault is created first; the throwaway recovery key is only logged. Existing vault files are never overwritten. The password comes from `VFS_VAULT_PASSWORD` or a console prompt. The mount is single-threaded; on macOS it uses Cryptomator's macFUSE flags (`-oatomic_o_trunc -oauto_xattr -oauto_cache -onoappledouble -odefault_permissions`), and `VFS_MOUNT_FLAGS` appends further flags.
5+
`VfsTestMain <vaultDir> <mountPoint> [fuseLibraryPath]` mounts the vault in `vaultDir`. If neither `vault.uvf` nor `d/` exist there, a new password-protected vault is created first; the throwaway recovery key is only logged. Existing vault files are never overwritten. The password comes from `VFS_VAULT_PASSWORD` or a console prompt. The mount is single-threaded; on macOS it uses Cryptomator's macFUSE flags (`-oatomic_o_trunc -oauto_xattr -oauto_cache -onoappledouble -odefault_permissions`), and `VFS_MOUNT_FLAGS` appends further flags, split on whitespace, so a single flag cannot contain spaces.
66

77
```mermaid
88
flowchart LR

‎vfs-test/src/main/java/org/cryptomator/vfs/test/VfsTestMain.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void main(String[] args) throws Exception {
5252
var backend = new NioVfsFileSystem(vaultDir);
5353
var ciphertextLoggingMiddleware = new LoggingVfsFileSystem("ciphertext", backend);
5454
var password = readPassword();
55-
var csprng = SecureRandom.getInstanceStrong();
55+
var csprng = new SecureRandom(); // getInstanceStrong() may block on headless Linux, and the default is a CSPRNG as well
5656
boolean createNewVault = Files.notExists(vaultDir.resolve(VAULT_CONFIG_FILE)) && Files.notExists(vaultDir.resolve(VAULT_DATA_DIR));
5757

5858
var builder = Fuse.builder();

0 commit comments

Comments
 (0)