Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8062795: (fs) Files.setPermissions requires read access when NOFOLLOW_LINKS specified #15214

Closed
wants to merge 4 commits into from

Conversation

bplb
Copy link
Member

@bplb bplb commented Aug 9, 2023

If setting POSIX permissions while not following links fails with AccessDeniedException but the path does not contain any symbolic links, then retry the operation as if the NOFOLLOW_LINKS option had not been specified.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8062795: (fs) Files.setPermissions requires read access when NOFOLLOW_LINKS specified (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15214/head:pull/15214
$ git checkout pull/15214

Update a local copy of the PR:
$ git checkout pull/15214
$ git pull https://git.openjdk.org/jdk.git pull/15214/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15214

View PR using the GUI difftool:
$ git pr show -t 15214

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15214.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 9, 2023

👋 Welcome back bpb! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 9, 2023
@openjdk
Copy link

openjdk bot commented Aug 9, 2023

@bplb The following label will be automatically applied to this pull request:

  • nio

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the nio nio-dev@openjdk.org label Aug 9, 2023
@mlbridge
Copy link

mlbridge bot commented Aug 9, 2023

Webrevs

@AlanBateman
Copy link
Contributor

I don't think this is the right thing as it may do the wrong thing when there are concurrency changes on the file system. I think the simplest is to just attempt to open the file for write access, this should work for regular files.

Not tested, but I expect something like this is what you want here.

diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java b/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
index f35541a9e19..bcab5319d8d 100644
--- a/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
+++ b/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
@@ -31,6 +31,7 @@ import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.io.IOException;
 
+import static sun.nio.fs.UnixConstants.*;
 import static sun.nio.fs.UnixNativeDispatcher.*;
 
 class UnixFileAttributeViews {
@@ -262,20 +263,45 @@ class UnixFileAttributeViews {
         // 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) {
                     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);
+            }
+
         }
 
         // chown

@bplb
Copy link
Member Author

bplb commented Aug 15, 2023

I think the simplest is to just attempt to open the file for write access [...].

This in fact works. So changed in 2e04446.

@openjdk
Copy link

openjdk bot commented Aug 16, 2023

@bplb This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8062795: (fs) Files.setPermissions requires read access when NOFOLLOW_LINKS specified

Reviewed-by: alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 98 new commits pushed to the master branch:

  • 13f6450: 8313765: Invalid CEN header (invalid zip64 extra data field size)
  • 24e896d: 8310275: Bug in assignment operator of ReservedMemoryRegion
  • 1925508: 8314144: gc/g1/ihop/TestIHOPStatic.java fails due to extra concurrent mark with -Xcomp
  • b80001d: 8314209: Wrong @SInCE tag for RandomGenerator::equiDoubles
  • ef6db5c: 8314211: Add NativeLibraryUnload event
  • 49ddb19: 8313760: [REDO] Enhance AES performance
  • d46f0fb: 8313720: C2 SuperWord: wrong result with -XX:+UseVectorCmov -XX:+UseCMoveUnconditionally
  • 38687f1: 8314262: GHA: Cut down cross-compilation sysroots deeper
  • a602624: 8314020: Print instruction blocks in byte units
  • 0b12480: 8314233: C2: assert(assertion_predicate_has_loop_opaque_node(iff)) failed: unexpected
  • ... and 88 more: https://git.openjdk.org/jdk/compare/515add88ed6c290ee90f3d6f522a22971e37a62c...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 16, 2023
@bplb
Copy link
Member Author

bplb commented Aug 16, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Aug 16, 2023

Going to push as commit 6b396da.
Since your change was applied there have been 100 commits pushed to the master branch:

  • 7b28d36: 8314330: java/foreign tests should respect vm flags when start new processes
  • b32d641: 8311943: Cleanup usages of toLowerCase() and toUpperCase() in java.base
  • 13f6450: 8313765: Invalid CEN header (invalid zip64 extra data field size)
  • 24e896d: 8310275: Bug in assignment operator of ReservedMemoryRegion
  • 1925508: 8314144: gc/g1/ihop/TestIHOPStatic.java fails due to extra concurrent mark with -Xcomp
  • b80001d: 8314209: Wrong @SInCE tag for RandomGenerator::equiDoubles
  • ef6db5c: 8314211: Add NativeLibraryUnload event
  • 49ddb19: 8313760: [REDO] Enhance AES performance
  • d46f0fb: 8313720: C2 SuperWord: wrong result with -XX:+UseVectorCmov -XX:+UseCMoveUnconditionally
  • 38687f1: 8314262: GHA: Cut down cross-compilation sysroots deeper
  • ... and 90 more: https://git.openjdk.org/jdk/compare/515add88ed6c290ee90f3d6f522a22971e37a62c...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 16, 2023
@openjdk openjdk bot closed this Aug 16, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 16, 2023
@openjdk
Copy link

openjdk bot commented Aug 16, 2023

@bplb Pushed as commit 6b396da.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@bplb bplb deleted the Files-setPermissions-8062795 branch August 22, 2023 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated nio nio-dev@openjdk.org
2 participants