Skip to content

Commit

Permalink
8062795: (fs) Files.setPermissions requires read access when NOFOLLOW…
Browse files Browse the repository at this point in the history
…_LINKS specified

Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed Aug 16, 2023
1 parent 7b28d36 commit 6b396da
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
46 changes: 36 additions & 10 deletions src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.TimeUnit;
import java.io.IOException;

import static sun.nio.fs.UnixConstants.*;
import static sun.nio.fs.UnixNativeDispatcher.*;

class UnixFileAttributeViews {
Expand Down Expand Up @@ -262,19 +263,44 @@ public UnixFileAttributes readAttributes() throws IOException {
// chmod
final void setMode(int mode) throws IOException {
checkWriteExtended();
try {
if (followLinks) {

if (followLinks) {
try {
chmod(file, mode);
} else {
int fd = file.openForAttributeAccess(false);
} catch (UnixException e) {
e.rethrowAsIOException(file);
}
return;
}

if (O_NOFOLLOW == 0) {
throw new IOException("NOFOLLOW_LINKS is not supported on this platform");
}

int fd = -1;
try {
fd = open(file, O_RDONLY, O_NOFOLLOW);
} catch (UnixException e1) {
if (e1.errno() == EACCES) {
// retry with write access if there is no read permission
try {
fchmod(fd, mode);
} finally {
close(fd);
fd = open(file, O_WRONLY, O_NOFOLLOW);
} catch (UnixException e2) {
e2.rethrowAsIOException(file);
}
} else {
e1.rethrowAsIOException(file);
}
} catch (UnixException x) {
x.rethrowAsIOException(file);
}

try {
try {
fchmod(fd, mode);
} finally {
close(fd);
}
} catch (UnixException e) {
e.rethrowAsIOException(file);
}
}

Expand Down
48 changes: 44 additions & 4 deletions test/jdk/java/nio/file/attribute/PosixFileAttributeView/Basic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,7 +22,7 @@
*/

/* @test
* @bug 4313887 6838333
* @bug 4313887 6838333 8062795
* @summary Unit test for java.nio.file.attribute.PosixFileAttributeView
* @library ../..
*/
Expand Down Expand Up @@ -167,9 +167,9 @@ static void permissionTests(Path dir)
Files.delete(file);
}

// create link (to file that doesn't exist) and test reading of
// permissions
if (TestUtil.supportsLinks(dir)) {
// create link (to file that doesn't exist) and test reading of
// permissions
Path link = dir.resolve("link");
System.out.format("create link %s\n", link);
Files.createSymbolicLink(link, file);
Expand All @@ -185,6 +185,46 @@ static void permissionTests(Path dir)
} finally {
Files.delete(link);
}

// test that setting permissions on paths with and without
// links succeeds when the NOFOLLOW_LINKS option is set

// ensure there are no links in the path to test
Path realDir = dir.toRealPath();

// realDir/a/b/c/d
Path leaf = realDir.resolve(Path.of("a", "b", "c", "d"));
Files.createDirectories(leaf);

// realDir/a/b/c/d/FUBAR
Path sansLinks = Files.createTempFile(leaf, "FU", "BAR");

PosixFileAttributeView sansView =
Files.getFileAttributeView(sansLinks,
PosixFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS);
sansView.setPermissions(Set.of(PosixFilePermission.OWNER_WRITE));
sansView.setPermissions(Set.of(PosixFilePermission.OWNER_WRITE));

// reinstate read permission
sansView.setPermissions(Set.of(PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE));

// lien -> realDir/a/b/c
Path lien = realDir.resolve(Path.of("a", "lien"));
Files.createSymbolicLink(lien,
realDir.resolve(Path.of("a", "b", "c")));

// lien/d/FUBAR
Path withLinks = lien.resolve(Path.of("d"),
sansLinks.getFileName());

PosixFileAttributeView withView =
Files.getFileAttributeView(withLinks,
PosixFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS);
withView.setPermissions(Set.of(PosixFilePermission.OWNER_WRITE));
withView.setPermissions(Set.of(PosixFilePermission.OWNER_WRITE));
}

System.out.println("OKAY");
Expand Down

1 comment on commit 6b396da

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.