2019独角兽企业重金招聘Python工程师标准>>>

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 Java Magic. Part 5: sun.misc.Unsafe

SecurityManager 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 {@Overridepublic void checkRead(FileDescriptor fd) {throw new SecurityException("File reading is not allowed");}@Overridepublic void checkWrite(FileDescriptor fd) {throw new SecurityException("File writing is not allowed");}@Overridepublic 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 coreutils project, see a related discussion on stackoverflow

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

Java Magic. Part 5: SecurityManager相关推荐

  1. java magic number_Magic Number

    今天重学java的时候了解了magic number的相关内容 以前不知道是啥,现在将他的大概意思写下来记录一下 通常有两种情况 1.在代码中直接使用到了某个数值 int duration=1000: ...

  2. java magic number_java的class文件的magic number, cafebabe

    java的class文件的前4个字节叫做magic number, 用来识别是否为java的class文件. package pkg; import java.io.File; import java ...

  3. java magic number_Java随谈(一)魔术数字、常量和枚举

    本文适合对 Java 或 C 有一些了解的用户阅读,推荐阅读时间15分钟. 导言 写这个系列的原因? 我曾经听过一种说法,如果不了解Liunx的网络通讯,就很难理解理解Java的IO:如果不知道Jav ...

  4. java magic number_避免JDBC查询中的CheckStyle magic number错误

    我正在上课一个小组项目,我们正在尝试CheckStyle. 我对Java非常满意,但从未触及到JDBC或在此之前完成任何数据库工作. 我想知道如果有一个优雅的方式来避免在准备语句电话中出现错误的数字错 ...

  5. Java SecurityManager checkDelete()方法与示例

    SecurityManager类的checkDelete()方法 (SecurityManager Class checkDelete() method) checkDelete() method i ...

  6. Java SecurityManager getSecurityContext()方法与示例

    SecurityManager类的getSecurityContext()方法 (SecurityManager Class getSecurityContext() method) getSecur ...

  7. Java SecurityManager getThreadGroup()方法与示例

    SecurityManager类的getThreadGroup()方法 (SecurityManager Class getThreadGroup() method) getThreadGroup() ...

  8. Java SecurityManager checkAwtEventQueueAccess()方法与示例

    SecurityManager类的checkAwtEventQueueAccess()方法 (SecurityManager Class checkAwtEventQueueAccess() meth ...

  9. 认识java安全管理器SecurityManager

    1,什么是Java安全管理器? SecurityManager 在看java源码的过程中,经常会遇到如下一段代码: SecurityManager s = System.getSecurityMana ...

最新文章

  1. 读游戏之旅-我的编程感悟:难得的休闲读物
  2. MySQL的IFNULL函数
  3. 尽管普通的sql语句代码可以实现数据插入的操作,但是更好的代码应该是参数的方式:...
  4. xenserver PXE安装系统错误的解决
  5. 处理SSL certificate problem self signed certificate
  6. 协议簇:TCP 解析:TCP 数据传输
  7. 人生真是圆的,从BASIC开始的程序人生,又回到了BASIC,难道。。。。。
  8. Flutter DecoratedBox装饰容器
  9. excel2016打开需要配置解决方法
  10. 网络工程师--网络规划和设计案例分析(4)
  11. JAVA关于实现科大讯飞语音合成功能
  12. 对象存储 Bucket
  13. 逢七过,逢七坐,逢七出列。。。 。。。各种叫法都有
  14. 拿铁DHT-PHEV座舱智能程度体验笔记(超详细)
  15. windows10 android模拟器,手机windows10模拟器安卓版
  16. SAS编程|if语句/where语句/select语句
  17. M的编程备忘录之Linux——基础开发工具
  18. 植物神经紊乱会不会导致失眠
  19. BeanUtils介绍
  20. python解决数学题_python编写解决数学问题

热门文章

  1. Design Pattern----06.Creational.Singleton.Pattern (Delphi Sample)
  2. MySQL的视图、事务和索引
  3. [git]一个本地仓库,多个远程仓库
  4. 学习设计模式 - 六大基本原则之单一职责原则
  5. Nginx之HTTP过滤模块
  6. 动态参数 名称空间 作用域 作用域链 加载顺序 函数的嵌套 global nonlocal 等的用法总结...
  7. XXE漏洞检测及代码执行过程
  8. 骑行318、 2016.7.8
  9. 牛顿求平方根法(输入是double,输出也是double)
  10. 软件评测-信息安全-应用安全-资源控制-用户登录限制(中)