java获取jvm的api,java – 为JVM插件寻找安全的Sandbox API

我找到了’System.exec(‘rm -rf *’)’问题的一个非常简单的解决方案:

package de.unkrig.commons.lang.security;

import java.security.AccessControlContext;

import java.security.Permission;

import java.security.Permissions;

import java.security.ProtectionDomain;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import java.util.WeakHashMap;

import de.unkrig.commons.nullanalysis.Nullable;

/**

* This class establishes a security manager that confines the permissions for code executed through specific classes,

* which may be specified by class, class name and/or class loader.

*

* To 'execute through a class' means that the execution stack includes the class. E.g., if a method of class {@code A}

* invokes a method of class {@code B}, which then invokes a method of class {@code C}, and all three classes were

* previously {@link #confine(Class, Permissions) confined}, then for all actions that are executed by class {@code C}

* the intersection of the three {@link Permissions} apply.

*

* Once the permissions for a class, class name or class loader are confined, they cannot be changed; this prevents any

* attempts (e.g. of the confined class itself) to release the confinement.

*

* Code example:

*

 
 

* Runnable unprivileged = new Runnable() {

* public void run() {

* System.getProperty("user.dir");

* }

* };

*

* // Run without confinement.

* unprivileged.run(); // Works fine.

*

* // Set the most strict permissions.

* Sandbox.confine(unprivileged.getClass(), new Permissions());

* unprivileged.run(); // Throws a SecurityException.

*

* // Attempt to change the permissions.

* {

* Permissions permissions = new Permissions();

* permissions.add(new AllPermission());

* Sandbox.confine(unprivileged.getClass(), permissions); // Throws a SecurityException.

* }

* unprivileged.run();

*

*/

public final

class Sandbox {

private Sandbox() {}

private static final Map, AccessControlContext>

CHECKED_CLASSES = Collections.synchronizedMap(new WeakHashMap, AccessControlContext>());

private static final Map

CHECKED_CLASS_NAMES = Collections.synchronizedMap(new HashMap());

private static final Map

CHECKED_CLASS_LOADERS = Collections.synchronizedMap(new WeakHashMap());

static {

// Install our custom security manager.

if (System.getSecurityManager() != null) {

throw new ExceptionInInitializerError("There's already a security manager set");

}

System.setSecurityManager(new SecurityManager() {

@Override public void

checkPermission(@Nullable Permission perm) {

assert perm != null;

for (Class> clasS : this.getClassContext()) {

// Check if an ACC was set for the class.

{

AccessControlContext acc = Sandbox.CHECKED_CLASSES.get(clasS);

if (acc != null) acc.checkPermission(perm);

}

// Check if an ACC was set for the class name.

{

AccessControlContext acc = Sandbox.CHECKED_CLASS_NAMES.get(clasS.getName());

if (acc != null) acc.checkPermission(perm);

}

// Check if an ACC was set for the class loader.

{

AccessControlContext acc = Sandbox.CHECKED_CLASS_LOADERS.get(clasS.getClassLoader());

if (acc != null) acc.checkPermission(perm);

}

}

}

});

}

// --------------------------

/**

* All future actions that are executed through the given {@code clasS} will be checked against the given {@code

* accessControlContext}.

*

* @throws SecurityException Permissions are already confined for the {@code clasS}

*/

public static void

confine(Class> clasS, AccessControlContext accessControlContext) {

if (Sandbox.CHECKED_CLASSES.containsKey(clasS)) {

throw new SecurityException("Attempt to change the access control context for '" + clasS + "'");

}

Sandbox.CHECKED_CLASSES.put(clasS, accessControlContext);

}

/**

* All future actions that are executed through the given {@code clasS} will be checked against the given {@code

* protectionDomain}.

*

* @throws SecurityException Permissions are already confined for the {@code clasS}

*/

public static void

confine(Class> clasS, ProtectionDomain protectionDomain) {

Sandbox.confine(

clasS,

new AccessControlContext(new ProtectionDomain[] { protectionDomain })

);

}

/**

* All future actions that are executed through the given {@code clasS} will be checked against the given {@code

* permissions}.

*

* @throws SecurityException Permissions are already confined for the {@code clasS}

*/

public static void

confine(Class> clasS, Permissions permissions) {

Sandbox.confine(clasS, new ProtectionDomain(null, permissions));

}

// Code for 'CHECKED_CLASS_NAMES' and 'CHECKED_CLASS_LOADERS' omitted here.

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值