Skip to content

Commit bfee766

Browse files
author
Eirik Bjørsnøs
committed
8344183: (zipfs) SecurityManager cleanup in the ZipFS area
Reviewed-by: mullan, lancea
1 parent 857f68c commit bfee766

File tree

3 files changed

+28
-82
lines changed

3 files changed

+28
-82
lines changed

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
import java.nio.file.*;
4545
import java.nio.file.attribute.*;
4646
import java.nio.file.spi.FileSystemProvider;
47-
import java.security.AccessController;
48-
import java.security.PrivilegedAction;
49-
import java.security.PrivilegedActionException;
50-
import java.security.PrivilegedExceptionAction;
5147
import java.util.*;
5248
import java.util.concurrent.locks.ReadWriteLock;
5349
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -82,10 +78,8 @@
8278
*/
8379
class ZipFileSystem extends FileSystem {
8480
// statics
85-
@SuppressWarnings("removal")
86-
private static final boolean isWindows = AccessController.doPrivileged(
87-
(PrivilegedAction<Boolean>)()->System.getProperty("os.name")
88-
.startsWith("Windows"));
81+
private static final boolean isWindows = System.getProperty("os.name")
82+
.startsWith("Windows");
8983
private static final byte[] ROOTPATH = new byte[] { '/' };
9084
private static final String PROPERTY_POSIX = "enablePosixFileAttributes";
9185
private static final String PROPERTY_DEFAULT_OWNER = "defaultOwner";
@@ -168,9 +162,7 @@ class ZipFileSystem extends FileSystem {
168162
}
169163
// sm and existence check
170164
zfpath.getFileSystem().provider().checkAccess(zfpath, AccessMode.READ);
171-
@SuppressWarnings("removal")
172-
boolean writeable = AccessController.doPrivileged(
173-
(PrivilegedAction<Boolean>)()->Files.isWritable(zfpath));
165+
boolean writeable = Files.isWritable(zfpath);
174166
this.readOnly = !writeable;
175167
this.zc = ZipCoder.get(nameEncoding);
176168
this.rootdir = new ZipPath(this, new byte[]{'/'});
@@ -244,23 +236,14 @@ private static boolean isTrue(Map<String, ?> env, String name) {
244236
// If not specified in env, it is the owner of the archive. If no owner can
245237
// be determined, we try to go with system property "user.name". If that's not
246238
// accessible, we return "<zipfs_default>".
247-
@SuppressWarnings("removal")
248239
private UserPrincipal initOwner(Path zfpath, Map<String, ?> env) throws IOException {
249240
Object o = env.get(PROPERTY_DEFAULT_OWNER);
250241
if (o == null) {
251242
try {
252-
PrivilegedExceptionAction<UserPrincipal> pa = ()->Files.getOwner(zfpath);
253-
return AccessController.doPrivileged(pa);
254-
} catch (UnsupportedOperationException | PrivilegedActionException e) {
255-
if (e instanceof UnsupportedOperationException ||
256-
e.getCause() instanceof NoSuchFileException)
257-
{
258-
PrivilegedAction<String> pa = ()->System.getProperty("user.name");
259-
String userName = AccessController.doPrivileged(pa);
260-
return ()->userName;
261-
} else {
262-
throw new IOException(e);
263-
}
243+
return Files.getOwner(zfpath);
244+
} catch (UnsupportedOperationException | NoSuchFileException e) {
245+
String userName = System.getProperty("user.name");
246+
return ()->userName;
264247
}
265248
}
266249
if (o instanceof String) {
@@ -282,7 +265,6 @@ private UserPrincipal initOwner(Path zfpath, Map<String, ?> env) throws IOExcept
282265
// If not specified in env, we try to determine the group of the zip archive itself.
283266
// If this is not possible/unsupported, we will return a group principal going by
284267
// the same name as the default owner.
285-
@SuppressWarnings("removal")
286268
private GroupPrincipal initGroup(Path zfpath, Map<String, ?> env) throws IOException {
287269
Object o = env.get(PROPERTY_DEFAULT_GROUP);
288270
if (o == null) {
@@ -291,16 +273,9 @@ private GroupPrincipal initGroup(Path zfpath, Map<String, ?> env) throws IOExcep
291273
if (zfpv == null) {
292274
return defaultOwner::getName;
293275
}
294-
PrivilegedExceptionAction<GroupPrincipal> pa = ()->zfpv.readAttributes().group();
295-
return AccessController.doPrivileged(pa);
296-
} catch (UnsupportedOperationException | PrivilegedActionException e) {
297-
if (e instanceof UnsupportedOperationException ||
298-
e.getCause() instanceof NoSuchFileException)
299-
{
300-
return defaultOwner::getName;
301-
} else {
302-
throw new IOException(e);
303-
}
276+
return zfpv.readAttributes().group();
277+
} catch (UnsupportedOperationException | NoSuchFileException e) {
278+
return defaultOwner::getName;
304279
}
305280
}
306281
if (o instanceof String) {
@@ -462,7 +437,6 @@ public PathMatcher getPathMatcher(String syntaxAndInput) {
462437
return (path)->pattern.matcher(path.toString()).matches();
463438
}
464439

465-
@SuppressWarnings("removal")
466440
@Override
467441
public void close() throws IOException {
468442
beginWrite();
@@ -480,13 +454,9 @@ public void close() throws IOException {
480454
}
481455
beginWrite(); // lock and sync
482456
try {
483-
AccessController.doPrivileged((PrivilegedExceptionAction<Void>)() -> {
484-
sync(); return null;
485-
});
457+
sync();
486458
ch.close(); // close the ch just in case no update
487459
// and sync didn't close the ch
488-
} catch (PrivilegedActionException e) {
489-
throw (IOException)e.getException();
490460
} finally {
491461
endWrite();
492462
}
@@ -512,10 +482,8 @@ public void close() throws IOException {
512482
synchronized (tmppaths) {
513483
for (Path p : tmppaths) {
514484
try {
515-
AccessController.doPrivileged(
516-
(PrivilegedExceptionAction<Boolean>)() -> Files.deleteIfExists(p));
517-
} catch (PrivilegedActionException e) {
518-
IOException x = (IOException)e.getException();
485+
Files.deleteIfExists(p);
486+
} catch (IOException x) {
519487
if (ioe == null)
520488
ioe = x;
521489
else

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystemProvider.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,9 +39,6 @@
3939
import java.nio.file.attribute.FileAttribute;
4040
import java.nio.file.attribute.FileAttributeView;
4141
import java.nio.file.spi.FileSystemProvider;
42-
import java.security.AccessController;
43-
import java.security.PrivilegedActionException;
44-
import java.security.PrivilegedExceptionAction;
4542
import java.util.HashMap;
4643
import java.util.Map;
4744
import java.util.Set;
@@ -317,17 +314,9 @@ public void setAttribute(Path path, String attribute,
317314
}
318315

319316
//////////////////////////////////////////////////////////////
320-
@SuppressWarnings("removal")
321317
void removeFileSystem(Path zfpath, ZipFileSystem zfs) throws IOException {
322318
synchronized (filesystems) {
323-
Path tempPath = zfpath;
324-
PrivilegedExceptionAction<Path> action = tempPath::toRealPath;
325-
try {
326-
zfpath = AccessController.doPrivileged(action);
327-
} catch (PrivilegedActionException e) {
328-
throw (IOException) e.getException();
329-
}
330-
filesystems.remove(zfpath, zfs);
319+
filesystems.remove(zfpath.toRealPath(), zfs);
331320
}
332321
}
333322
}

test/jdk/jdk/nio/zipfs/TestPosix.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
import java.nio.ByteOrder;
3030
import java.nio.file.*;
3131
import java.nio.file.attribute.*;
32-
import java.security.AccessController;
33-
import java.security.PrivilegedAction;
34-
import java.security.PrivilegedActionException;
35-
import java.security.PrivilegedExceptionAction;
3632
import java.time.Instant;
3733
import java.util.*;
3834
import java.util.concurrent.atomic.AtomicInteger;
@@ -219,35 +215,28 @@ static interface Executor {
219215

220216
private static String expectedDefaultOwner(Path zf) {
221217
try {
222-
try {
223-
PrivilegedExceptionAction<String> pa = ()->Files.getOwner(zf).getName();
224-
return AccessController.doPrivileged(pa);
225-
} catch (UnsupportedOperationException e) {
226-
// if we can't get the owner of the file, we fall back to system property user.name
227-
PrivilegedAction<String> pa = ()->System.getProperty("user.name");
228-
return AccessController.doPrivileged(pa);
229-
}
230-
} catch (PrivilegedActionException | SecurityException e) {
218+
return Files.getOwner(zf).getName();
219+
} catch (UnsupportedOperationException e) {
220+
// if we can't get the owner of the file, we fall back to system property user.name
221+
return System.getProperty("user.name");
222+
} catch (IOException e) {
231223
System.out.println("Caught " + e.getClass().getName() + "(" + e.getMessage() +
232-
") when running a privileged operation to get the default owner.");
224+
") when getting the default owner.");
233225
return null;
234226
}
235227
}
236228

237229
private static String expectedDefaultGroup(Path zf, String defaultOwner) {
238230
try {
239-
try {
240-
PosixFileAttributeView zfpv = Files.getFileAttributeView(zf, PosixFileAttributeView.class);
241-
if (zfpv == null) {
242-
return defaultOwner;
243-
}
244-
PrivilegedExceptionAction<String> pa = ()->zfpv.readAttributes().group().getName();
245-
return AccessController.doPrivileged(pa);
246-
} catch (UnsupportedOperationException e) {
231+
PosixFileAttributeView zfpv = Files.getFileAttributeView(zf, PosixFileAttributeView.class);
232+
if (zfpv == null) {
247233
return defaultOwner;
248234
}
249-
} catch (PrivilegedActionException | SecurityException e) {
250-
System.out.println("Caught an exception when running a privileged operation to get the default group.");
235+
return zfpv.readAttributes().group().getName();
236+
} catch (UnsupportedOperationException e) {
237+
return defaultOwner;
238+
} catch (IOException e) {
239+
System.out.println("Caught an exception when getting the default group.");
251240
e.printStackTrace();
252241
return null;
253242
}

0 commit comments

Comments
 (0)