Sandbox针对Java应用程序中的恶意代码

Ala*_*ger 88 java plugins sandbox

在允许用户提交他们自己的代码以供服务器运行的模拟服务器环境中,任何用户提交的代码都可以在沙箱中运行,这与使用Applet在浏览器中不同,这显然是有利的.我希望能够利用JVM本身,而不是添加另一个VM层来隔离这些提交的组件.

使用现有的Java沙箱模型似乎可以实现这种限制,但是有一种动态方法可以仅为正在运行的应用程序的用户提交的部分启用它吗?

waq*_*qas 107

  1. 在自己的线程中运行不受信任的代码.例如,这可以防止无限循环等问题,并使未来的步骤更容易.让主线程等待线程完成,如果花费太长时间,请使用Thread.stop将其终止.不推荐使用Thread.stop,但由于不受信任的代码不能访问任何资源,因此将其删除是安全的.

  2. 在该线程上设置SecurityManager.创建一个SecurityManager的子类,它覆盖checkPermission(Permission perm),只是为除了少数几个权限之外的所有权限抛出SecurityException.这里有一个方法列表和权限:Java TM 6 SDK 中的权限.

  3. 使用自定义ClassLoader加载不受信任的代码.您的类加载器将被调用所有不受信任的代码使用的类,因此您可以执行诸如禁用对单个JDK类的访问的操作.要做的是有一个允许的JDK类的白名单.

  4. 您可能希望在单独的JVM中运行不受信任的代码.虽然前面的步骤可以使代码安全,但孤立的代码仍然可以做一件令人讨厌的事情:分配尽可能多的内存,这会导致主应用程序的可见占用空间增长.

JSR 121:应用程序隔离API规范旨在解决这个问题,但不幸的是它还没有实现.

这是一个非常详细的话题,而且我大部分时间都在写这篇文章.

但无论如何,一些不完美的,使用自己的风险,可能是错误的(伪)代码:

类加载器

class MyClassLoader extends ClassLoader {
  @Override
  public Class<?> loadClass(String name) throws ClassNotFoundException {
    if (name is white-listed JDK class) return super.loadClass(name);
    return findClass(name);
  }
  @Override
  public Class findClass(String name) {
    byte[] b = loadClassData(name);
    return defineClass(name, b, 0, b.length);
  }
  private byte[] loadClassData(String name) {
    // load the untrusted class data here
  }
}
Run Code Online (Sandbox Code Playgroud)

安全管理器

class MySecurityManager extends SecurityManager {
  private Object secret;
  public MySecurityManager(Object pass) { secret = pass; }
  private void disable(Object pass) {
    if (pass == secret) secret = null;
  }
  // ... override checkXXX method(s) here.
  // Always allow them to succeed when secret==null
}
Run Code Online (Sandbox Code Playgroud)

线

class MyIsolatedThread extends Thread {
  private Object pass = new Object();
  private MyClassLoader loader = new MyClassLoader();
  private MySecurityManager sm = new MySecurityManager(pass);
  public void run() {
    SecurityManager old = System.getSecurityManager();
    System.setSecurityManager(sm);
    runUntrustedCode();
    sm.disable(pass);
    System.setSecurityManager(old);
  }
  private void runUntrustedCode() {
    try {
      // run the custom class's main method for example:
      loader.loadClass("customclassname")
        .getMethod("main", String[].class)
        .invoke(null, new Object[]{...});
    } catch (Throwable t) {}
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这种方法的问题是当你将SecurityManager设置为System时,它不仅会影响正在运行的线程,还会影响其他线程! (8认同)
  • 除了`System.setSecurityManager(...)`将影响整个JVM这一事实,不仅是调用该方法的线程,当Java从1.0切换到1.1时,基于线程做出安全决策的想法已经放弃了.此时,无论哪个线程执行代码,都认识到不受信任的代码可以调用受信任的代码,反之亦然.没有开发者应该重复这个错误. (5认同)
  • 该代码可能需要一些工作.您无法真正防范JVM可用性.准备好杀死这个过程(可能是自动的).代码进入其他线程 - 例如终结者线程.`Thread.stop`会导致Java库代码出现问题.同样,Java库代码也需要权限.更好的是允许`SecurityManager`使用`java.security.AccessController`.类加载器应该也允许访问用户代码自己的类. (4认同)
  • 鉴于这是一个如此复杂的主题,是不是现有解决方案以安全的方式处理Java"插件"? (3认同)
  • 抱歉,thread.stop()可以被throwable捕获。您可以同时使用(thread.isAlive)Thread.stop(),但随后我可以递归调用捕获异常的函数。在我的PC上进行测试,递归函数胜过stop()。现在您有了一个垃圾线程,正在窃取CPU和资源 (2认同)

shs*_*rfy 18

显然,这样的计划引发了各种各样的安全问题.Java有一个严格的安全框架,但它并非无足轻重.不应忽视将其搞砸并让非特权用户访问重要系统组件的可能性.

除了那个警告,如果你以源代码的形式接受用户输入,你需要做的第一件事就是将它编译成Java字节码.AFIAK,这不能在本地完成,因此您需要对javac进行系统调用,并将源代码编译为磁盘上的字节码.这是一个可以作为起点的教程. 编辑:正如我在评论中所了解到的,您实际上可以使用javax.tools.JavaCompiler本地编译源代码中的Java代码

获得JVM字节码后,可以使用ClassLoader的 defineClass函数将其加载到JVM中.要为此加载的类设置安全上下文,您需要指定一个ProtectionDomain.ProtectionDomain的最小构造函数需要CodeSource和PermissionCollection.PermissionCollection是您在这里主要使用的对象 - 您可以使用它来指定加载的类具有的确切权限.这些权限最终应由JVM的AccessController强制执行.

这里有很多可能的错误点,在实现任何内容之前,你应该非常小心地完全理解所有内容.

  • 使用JDK 6的javax.tools API,Java编译非常简单. (2认同)

Lii*_*Lii 10

Java的沙箱是用于执行Java代码与一组有限的权限的文库.它可用于仅允许访问一组列入白名单的类和资源.它似乎无法限制对单个方法的访问.它使用具有自定义类加载器和安全管理器的系统来实现此目的.

我没有使用它,但它看起来设计得很好,而且记录得很好.

@waqas给出了一个非常有趣的答案,解释了如何实现这一点.但是,将这些安全关键和复杂的代码留给专家会更安全.

请注意,自2013年以来该项目尚未更新,创作者将其描述为"实验性".它的主页已经消失,但Source Forge条目仍然存在.

从项目网站改编的示例代码:

SandboxService sandboxService = SandboxServiceImpl.getInstance();

// Configure context 
SandboxContext context = new SandboxContext();
context.addClassForApplicationLoader(getClass().getName());
context.addClassPermission(AccessType.PERMIT, "java.lang.System");

// Whithout this line we get a SandboxException when touching System.out
context.addClassPermission(AccessType.PERMIT, "java.io.PrintStream");

String someValue = "Input value";

class TestEnvironment implements SandboxedEnvironment<String> {
    @Override
    public String execute() throws Exception {
        // This is untrusted code
        System.out.println(someValue);
        return "Output value";
    }
};

// Run code in sandbox. Pass arguments to generated constructor in TestEnvironment.
SandboxedCallResult<String> result = sandboxService.runSandboxed(TestEnvironment.class, 
    context, this, someValue);

System.out.println(result.get());
Run Code Online (Sandbox Code Playgroud)


Arn*_*rig 6

这是该问题的线程安全解决方案:

https://svn.code.sf.net/p/loggifier/code/trunk/de.unkrig.commons.lang/src/de/unkrig/commons/lang/security/Sandbox.java

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.
 * <p>
 * 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 <i>intersection</i> of the three {@link Permissions} apply.
 * <p>
 * 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.
 * <p>
 * Code example:
 * <pre>
 *  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();
 * </pre>
 */
public final
class Sandbox {

    private Sandbox() {}

    private static final Map<Class<?>, AccessControlContext>
    CHECKED_CLASSES = Collections.synchronizedMap(new WeakHashMap<Class<?>, AccessControlContext>());

    private static final Map<String, AccessControlContext>
    CHECKED_CLASS_NAMES = Collections.synchronizedMap(new HashMap<String, AccessControlContext>());

    private static final Map<ClassLoader, AccessControlContext>
    CHECKED_CLASS_LOADERS = Collections.synchronizedMap(new WeakHashMap<ClassLoader, AccessControlContext>());

    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.

}
Run Code Online (Sandbox Code Playgroud)

请给出意见!

CU

阿诺


alp*_*oop 5

要解决已接受的答案中的问题,即自定义SecurityManager将应用于 JVM 中的所有线程,而不是在每个线程的基础上,您可以创建一个SecurityManager可以为特定线程启用/禁用的自定义,如下所示:

import java.security.Permission;

public class SelectiveSecurityManager extends SecurityManager {

  private static final ToggleSecurityManagerPermission TOGGLE_PERMISSION = new ToggleSecurityManagerPermission();

  ThreadLocal<Boolean> enabledFlag = null;

  public SelectiveSecurityManager(final boolean enabledByDefault) {

    enabledFlag = new ThreadLocal<Boolean>() {

      @Override
      protected Boolean initialValue() {
        return enabledByDefault;
      }

      @Override
      public void set(Boolean value) {
        SecurityManager securityManager = System.getSecurityManager();
        if (securityManager != null) {
          securityManager.checkPermission(TOGGLE_PERMISSION);
        }
        super.set(value);
      }
    };
  }

  @Override
  public void checkPermission(Permission permission) {
    if (shouldCheck(permission)) {
      super.checkPermission(permission);
    }
  }

  @Override
  public void checkPermission(Permission permission, Object context) {
    if (shouldCheck(permission)) {
      super.checkPermission(permission, context);
    }
  }

  private boolean shouldCheck(Permission permission) {
    return isEnabled() || permission instanceof ToggleSecurityManagerPermission;
  }

  public void enable() {
    enabledFlag.set(true);
  }

  public void disable() {
    enabledFlag.set(false);
  }

  public boolean isEnabled() {
    return enabledFlag.get();
  }

}
Run Code Online (Sandbox Code Playgroud)

ToggleSecurirtyManagerPermission只是一个简单的实现,java.security.Permission以确保只有经过授权的代码才能启用/禁用安全管理器。它看起来像这样:

import java.security.Permission;

public class ToggleSecurityManagerPermission extends Permission {

  private static final long serialVersionUID = 4812713037565136922L;
  private static final String NAME = "ToggleSecurityManagerPermission";

  public ToggleSecurityManagerPermission() {
    super(NAME);
  }

  @Override
  public boolean implies(Permission permission) {
    return this.equals(permission);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof ToggleSecurityManagerPermission) {
      return true;
    }
    return false;
  }

  @Override
  public int hashCode() {
    return NAME.hashCode();
  }

  @Override
  public String getActions() {
    return "";
  }

}
Run Code Online (Sandbox Code Playgroud)

  • 引用您(自己的)来源:http://alphaloop.blogspot.com/2014/08/a-per-thread-java-security-manager.html 和 https://github.com/alphaloop/selective-security-manager . (4认同)