Skip to content

Commit a81897c

Browse files
committed
[Entitlements] Add a check for filesystem mismatch (elastic#123744)
1 parent 6b55ea8 commit a81897c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.entitlement.runtime.policy;
1111

12+
import org.elasticsearch.core.Strings;
1213
import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement;
1314
import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode;
1415
import org.elasticsearch.logging.LogManager;
@@ -202,6 +203,7 @@ static String normalizePath(Path path) {
202203
}
203204

204205
private boolean checkPath(String path, String[] paths) {
206+
logger.trace(() -> Strings.format("checking [%s] against [%s]", path, String.join(",", paths)));
205207
if (paths.length == 0) {
206208
return false;
207209
}
@@ -219,6 +221,7 @@ private boolean checkPath(String path, String[] paths) {
219221
}
220222

221223
private static boolean isParent(String maybeParent, String path) {
224+
logger.trace(() -> Strings.format("checking isParent [%s] for [%s]", maybeParent, path));
222225
return path.startsWith(maybeParent) && path.startsWith(FILE_SEPARATOR, maybeParent.length());
223226
}
224227

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.entitlement.runtime.policy;
1111

12+
import org.elasticsearch.core.PathUtils;
1213
import org.elasticsearch.core.Strings;
1314
import org.elasticsearch.core.SuppressForbidden;
1415
import org.elasticsearch.entitlement.instrumentation.InstrumentationService;
@@ -61,6 +62,8 @@ public class PolicyManager {
6162
static final String SERVER_COMPONENT_NAME = "(server)";
6263
static final String APM_AGENT_COMPONENT_NAME = "(APM agent)";
6364

65+
static final Class<?> DEFAULT_FILESYSTEM_CLASS = PathUtils.getDefaultFileSystem().getClass();
66+
6467
/**
6568
* @param componentName the plugin name; or else one of the special component names
6669
* like {@link #SERVER_COMPONENT_NAME} or {@link #APM_AGENT_COMPONENT_NAME}.
@@ -305,7 +308,26 @@ public void checkFileRead(Class<?> callerClass, File file) {
305308
checkFileRead(callerClass, file.toPath());
306309
}
307310

311+
private static boolean isPathOnDefaultFilesystem(Path path) {
312+
var pathFileSystemClass = path.getFileSystem().getClass();
313+
if (path.getFileSystem().getClass() != DEFAULT_FILESYSTEM_CLASS) {
314+
logger.trace(
315+
() -> Strings.format(
316+
"File entitlement trivially allowed: path [%s] is for a different FileSystem class [%s], default is [%s]",
317+
path.toString(),
318+
pathFileSystemClass.getName(),
319+
DEFAULT_FILESYSTEM_CLASS.getName()
320+
)
321+
);
322+
return false;
323+
}
324+
return true;
325+
}
326+
308327
public void checkFileRead(Class<?> callerClass, Path path) {
328+
if (isPathOnDefaultFilesystem(path) == false) {
329+
return;
330+
}
309331
var requestingClass = requestingClass(callerClass);
310332
if (isTriviallyAllowed(requestingClass)) {
311333
return;
@@ -332,6 +354,9 @@ public void checkFileWrite(Class<?> callerClass, File file) {
332354
}
333355

334356
public void checkFileWrite(Class<?> callerClass, Path path) {
357+
if (isPathOnDefaultFilesystem(path) == false) {
358+
return;
359+
}
335360
var requestingClass = requestingClass(callerClass);
336361
if (isTriviallyAllowed(requestingClass)) {
337362
return;

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlement.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ static boolean isAbsolutePath(String path) {
108108
// Unix/BSD absolute
109109
return true;
110110
}
111-
112111
return isWindowsAbsolutePath(path);
113112
}
114113

0 commit comments

Comments
 (0)