博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Magic. Part 5: SecurityManager
阅读量:6842 次
发布时间:2019-06-26

本文共 3669 字,大约阅读时间需要 12 分钟。

hot3.png

Pouring a bit light on SecurityManager and its use cases.

Intro

You can do a terrible things in java using sun.misc.Unsafe class. Some really creepy examples were discussed in 

 is a guard, which could help to prevent some sensitive actions (io, net, reflection, access etc.)

SecurityManager manager = System.getSecurityManager();if (manager != null) {    manager.checkAction(action);}

If action is not allowed SecurityExeption occurs.

Use Case

Now its a time to write some code.

Assume we are building online grader, a system which accepts some java code, runs it, gets results and verify that results are correct. Such graders are useful for computer science courses in MOOC platforms like coursera, udacity, etc.

Obviously, running untrusted code is unsafe, so we need to make sure code submitter does not break/compromise whole grader system. For example such sumbitter could read passwords and modify grading entry in database. Or even worse, it could fill out the whole file system, RAM or consume all threads and prevent grading for other submitters.

SecurityManager solves these issues.

Extend it and override needed policies, specifying what is allowed and what is not.

class MySecurityManager extends SecurityManager {    @Override    public void checkRead(FileDescriptor fd) {        throw new SecurityException("File reading is not allowed");    }    @Override    public void checkWrite(FileDescriptor fd) {        throw new SecurityException("File writing is not allowed");    }    @Override    public void checkConnect(String host, int port) {        throw new SecurityException("Socket connections are not allowed");    }}

You can set such security manager in runtime using:

System.setSecurityManager(new MySecurityManager());

Note: setSecurityManager is controlled by security manager as well.

If some restricted action is executed, SecurityException occurs.

Inspect methods from SecurityManager which starts with check prefix. There are plenty of checks JVM may run before your code.

Though, security manager is useful tool for configuring access to subsystems and prevent untrusted code from doing a terrible things, some actions are not controlled by security manager.

Memory Allocation

(un)fortunately, memory allocation is not controlled by a programmer and the same way SecurityManagercan't restrict object creation. If you need validate that untrusted code fulfills memory requirements, execute it in a separate JVM and give it maximum amount of memory java -Xmx128m. If memory requirements are broken OutOfMemory occurs, but as long as this was executed in another JVM, this won't affect grader.

For more accurate memory management you need to attach instrumentation agent to a java process.

Threads

There is no way to limit number of threads spawned by a java process. If only ExecutorService responsible for thread creation, then introduce limit by using ExecutorService.newFixedThreadPool(limit) inside the code and make this as a convention.

Otherwise you need to write a custom agent that tracks number of active threads. Such functionality available in some proprietary java agents.

Timeouts

To make sure that program finishes in specific time period, use some external tool for setting a timeout for it. For linux it's a , see a related discussion on 

Libraries

You can prevent whole package usage by SecurityManager.checkPackageAccess. The same way you can prevent usage of some external libraries or products from whole organisations. But if you want prevent usage of some specific method, like java.lang.Math.min(), you probably need to manually scan java source file and detect such call.

转载于:https://my.oschina.net/u/1469495/blog/717916

你可能感兴趣的文章
关于Eclipse平台的使用和开发第一个SWT程序
查看>>
STL笔记(3) copy()之绝版应用
查看>>
安卓开发环境搭建
查看>>
如果我可以重新学习iOS开发(转)
查看>>
mysql存储过程和游标以及if-else,while典型实例
查看>>
Web性能优化:图片优化
查看>>
使用JS或jQuery模拟鼠标点击a标签事件代码
查看>>
【BZOJ】1225: [HNOI2001] 求正整数
查看>>
RTB业务知识之1-原生广告
查看>>
iOS9和Xcode7
查看>>
Python核心编程笔记----注释
查看>>
Android进程间通信(IPC)机制Binder简介和学习计划
查看>>
android application简要类(一)
查看>>
Android通过HTTP协议实现上传文件数据
查看>>
JAMA:Java矩阵包
查看>>
HAL打开驱动失败
查看>>
Androids含文档erver结束(工具包 Httputils)两
查看>>
Java眼中的XML--文件读取--1 应用DOM方式解析XML
查看>>
CSS3的媒体查询(Media Queries)与移动设备显示尺寸大全
查看>>
git 基本操作
查看>>