|
12 | 12 |
|
13 | 13 | import java.io.FilePermission;
|
14 | 14 | import java.lang.reflect.Method;
|
| 15 | +import java.net.NetPermission; |
15 | 16 | import java.nio.file.OpenOption;
|
16 | 17 | import java.nio.file.Path;
|
17 | 18 | import java.nio.file.Paths;
|
@@ -71,59 +72,70 @@ public static void intercept(@Advice.AllArguments Object[] args, @Advice.Origin
|
71 | 72 | boolean isMutating = name.equals("move") || name.equals("write") || name.startsWith("create");
|
72 | 73 | final boolean isDelete = isMutating == false ? name.startsWith("delete") : false;
|
73 | 74 |
|
74 |
| - String targetFilePath = null; |
75 |
| - if (isMutating == false && isDelete == false) { |
76 |
| - if (name.equals("newByteChannel") == true || name.equals("open") == true) { |
77 |
| - if (args.length > 1 && args[1] instanceof OpenOption[] opts) { |
78 |
| - for (final OpenOption opt : opts) { |
79 |
| - if (opt != StandardOpenOption.READ) { |
80 |
| - isMutating = true; |
81 |
| - break; |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - } |
86 |
| - } else if (name.equals("copy") == true) { |
87 |
| - if (args.length > 1 && args[1] instanceof String pathStr) { |
88 |
| - targetFilePath = Paths.get(pathStr).toAbsolutePath().toString(); |
89 |
| - } else if (args.length > 1 && args[1] instanceof Path path) { |
90 |
| - targetFilePath = path.toAbsolutePath().toString(); |
| 75 | + // This is Windows implementation of UNIX Domain Sockets (close) |
| 76 | + if (isDelete == true |
| 77 | + && walker.getCallerClass().getName().equalsIgnoreCase("sun.nio.ch.PipeImpl$Initializer$LoopbackConnector") == true) { |
| 78 | + final NetPermission permission = new NetPermission("accessUnixDomainSocket"); |
| 79 | + for (ProtectionDomain domain : callers) { |
| 80 | + if (!policy.implies(domain, permission)) { |
| 81 | + throw new SecurityException("Denied access to: " + filePath + ", domain " + domain); |
91 | 82 | }
|
92 | 83 | }
|
93 |
| - } |
| 84 | + } else { |
| 85 | + String targetFilePath = null; |
| 86 | + if (isMutating == false && isDelete == false) { |
| 87 | + if (name.equals("newByteChannel") == true || name.equals("open") == true) { |
| 88 | + if (args.length > 1 && args[1] instanceof OpenOption[] opts) { |
| 89 | + for (final OpenOption opt : opts) { |
| 90 | + if (opt != StandardOpenOption.READ) { |
| 91 | + isMutating = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
94 | 95 |
|
95 |
| - // Check each permission separately |
96 |
| - for (final ProtectionDomain domain : callers) { |
97 |
| - // Handle FileChannel.open() separately to check read/write permissions properly |
98 |
| - if (method.getName().equals("open")) { |
99 |
| - if (isMutating == true && !policy.implies(domain, new FilePermission(filePath, "read,write"))) { |
100 |
| - throw new SecurityException("Denied OPEN (read/write) access to file: " + filePath + ", domain: " + domain); |
101 |
| - } else if (!policy.implies(domain, new FilePermission(filePath, "read"))) { |
102 |
| - throw new SecurityException("Denied OPEN (read) access to file: " + filePath + ", domain: " + domain); |
| 96 | + } |
| 97 | + } else if (name.equals("copy") == true) { |
| 98 | + if (args.length > 1 && args[1] instanceof String pathStr) { |
| 99 | + targetFilePath = Paths.get(pathStr).toAbsolutePath().toString(); |
| 100 | + } else if (args.length > 1 && args[1] instanceof Path path) { |
| 101 | + targetFilePath = path.toAbsolutePath().toString(); |
| 102 | + } |
103 | 103 | }
|
104 | 104 | }
|
105 | 105 |
|
106 |
| - // Handle Files.copy() separately to check read/write permissions properly |
107 |
| - if (method.getName().equals("copy")) { |
108 |
| - if (!policy.implies(domain, new FilePermission(filePath, "read"))) { |
109 |
| - throw new SecurityException("Denied COPY (read) access to file: " + filePath + ", domain: " + domain); |
| 106 | + // Check each permission separately |
| 107 | + for (final ProtectionDomain domain : callers) { |
| 108 | + // Handle FileChannel.open() separately to check read/write permissions properly |
| 109 | + if (method.getName().equals("open")) { |
| 110 | + if (isMutating == true && !policy.implies(domain, new FilePermission(filePath, "read,write"))) { |
| 111 | + throw new SecurityException("Denied OPEN (read/write) access to file: " + filePath + ", domain: " + domain); |
| 112 | + } else if (!policy.implies(domain, new FilePermission(filePath, "read"))) { |
| 113 | + throw new SecurityException("Denied OPEN (read) access to file: " + filePath + ", domain: " + domain); |
| 114 | + } |
110 | 115 | }
|
111 | 116 |
|
112 |
| - if (targetFilePath != null) { |
113 |
| - if (!policy.implies(domain, new FilePermission(targetFilePath, "write"))) { |
114 |
| - throw new SecurityException("Denied COPY (write) access to file: " + targetFilePath + ", domain: " + domain); |
| 117 | + // Handle Files.copy() separately to check read/write permissions properly |
| 118 | + if (method.getName().equals("copy")) { |
| 119 | + if (!policy.implies(domain, new FilePermission(filePath, "read"))) { |
| 120 | + throw new SecurityException("Denied COPY (read) access to file: " + filePath + ", domain: " + domain); |
| 121 | + } |
| 122 | + |
| 123 | + if (targetFilePath != null) { |
| 124 | + if (!policy.implies(domain, new FilePermission(targetFilePath, "write"))) { |
| 125 | + throw new SecurityException("Denied COPY (write) access to file: " + targetFilePath + ", domain: " + domain); |
| 126 | + } |
115 | 127 | }
|
116 | 128 | }
|
117 |
| - } |
118 | 129 |
|
119 |
| - // File mutating operations |
120 |
| - if (isMutating && !policy.implies(domain, new FilePermission(filePath, "write"))) { |
121 |
| - throw new SecurityException("Denied WRITE access to file: " + filePath + ", domain: " + domain); |
122 |
| - } |
| 130 | + // File mutating operations |
| 131 | + if (isMutating && !policy.implies(domain, new FilePermission(filePath, "write"))) { |
| 132 | + throw new SecurityException("Denied WRITE access to file: " + filePath + ", domain: " + domain); |
| 133 | + } |
123 | 134 |
|
124 |
| - // File deletion operations |
125 |
| - if (isDelete && !policy.implies(domain, new FilePermission(filePath, "delete"))) { |
126 |
| - throw new SecurityException("Denied DELETE access to file: " + filePath + ", domain: " + domain); |
| 135 | + // File deletion operations |
| 136 | + if (isDelete && !policy.implies(domain, new FilePermission(filePath, "delete"))) { |
| 137 | + throw new SecurityException("Denied DELETE access to file: " + filePath + ", domain: " + domain); |
| 138 | + } |
127 | 139 | }
|
128 | 140 | }
|
129 | 141 | }
|
|
0 commit comments