security.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. [[modules-scripting-security]]
  2. === Scripting and the Java Security Manager
  3. Elasticsearch runs with the https://docs.oracle.com/javase/tutorial/essential/environment/security.html[Java Security Manager]
  4. enabled by default. The security policy in Elasticsearch locks down the
  5. permissions granted to each class to the bare minimum required to operate.
  6. The benefit of doing this is that it severely limits the attack vectors
  7. available to a hacker.
  8. Restricting permissions is particularly important with scripting languages
  9. like Groovy and Javascript which are designed to do anything that can be done
  10. in Java itself, including writing to the file system, opening sockets to
  11. remote servers, etc.
  12. [float]
  13. === Script Classloader Whitelist
  14. Scripting languages are only allowed to load classes which appear in a
  15. hardcoded whitelist that can be found in
  16. https://github.com/elastic/elasticsearch/blob/{branch}/core/src/main/java/org/elasticsearch/script/ClassPermission.java[`org.elasticsearch.script.ClassPermission`].
  17. In a script, attempting to load a class that does not appear in the whitelist
  18. _may_ result in a `ClassNotFoundException`, for instance this script:
  19. [source,json]
  20. ------------------------------
  21. GET _search
  22. {
  23. "script_fields": {
  24. "the_hour": {
  25. "script": "use(java.math.BigInteger); new BigInteger(1)"
  26. }
  27. }
  28. }
  29. ------------------------------
  30. will return the following exception:
  31. [source,json]
  32. ------------------------------
  33. {
  34. "reason": {
  35. "type": "script_exception",
  36. "reason": "failed to run inline script [use(java.math.BigInteger); new BigInteger(1)] using lang [groovy]",
  37. "caused_by": {
  38. "type": "no_class_def_found_error",
  39. "reason": "java/math/BigInteger",
  40. "caused_by": {
  41. "type": "class_not_found_exception",
  42. "reason": "java.math.BigInteger"
  43. }
  44. }
  45. }
  46. }
  47. ------------------------------
  48. However, classloader issues may also result in more difficult to interpret
  49. exceptions. For instance, this script:
  50. [source,groovy]
  51. ------------------------------
  52. use(groovy.time.TimeCategory); new Date(123456789).format('HH')
  53. ------------------------------
  54. Returns the following exception:
  55. [source,json]
  56. ------------------------------
  57. {
  58. "reason": {
  59. "type": "script_exception",
  60. "reason": "failed to run inline script [use(groovy.time.TimeCategory); new Date(123456789).format('HH')] using lang [groovy]",
  61. "caused_by": {
  62. "type": "missing_property_exception",
  63. "reason": "No such property: groovy for class: 8d45f5c1a07a1ab5dda953234863e283a7586240"
  64. }
  65. }
  66. }
  67. ------------------------------
  68. [float]
  69. == Dealing with Java Security Manager issues
  70. If you encounter issues with the Java Security Manager, you have three options
  71. for resolving these issues:
  72. [float]
  73. === Fix the security problem
  74. The safest and most secure long term solution is to change the code causing
  75. the security issue. We recognise that this may take time to do correctly and
  76. so we provide the following two alternatives.
  77. [float]
  78. === Disable the Java Security Manager
  79. deprecated[2.2.0,The ability to disable the Java Security Manager will be removed in a future version]
  80. You can disable the Java Security Manager entirely with the
  81. `security.manager.enabled` command line flag:
  82. [source,sh]
  83. -----------------------------
  84. ./bin/elasticsearch --security.manager.enabled false
  85. -----------------------------
  86. WARNING: This disables the Security Manager entirely and makes Elasticsearch
  87. much more vulnerable to attacks! It is an option that should only be used in
  88. the most urgent of situations and for the shortest amount of time possible.
  89. Optional security is not secure at all because it **will** be disabled and
  90. leave the system vulnerable. This option will be removed in a future version.
  91. [float]
  92. === Customising the classloader whitelist
  93. The classloader whitelist can be customised by tweaking the local Java
  94. Security Policy either:
  95. * system wide: `$JAVA_HOME/lib/security/java.policy`,
  96. * for just the `elasticsearch` user: `/home/elasticsearch/.java.policy`, or
  97. * from a file specified on the command line: `-Djava.security.policy=someURL`
  98. Permissions may be granted at the class, package, or global level. For instance:
  99. [source,js]
  100. ----------------------------------
  101. grant {
  102. permission org.elasticsearch.script.ClassPermission "java.util.Base64"; // allow class
  103. permission org.elasticsearch.script.ClassPermission "java.util.*"; // allow package
  104. permission org.elasticsearch.script.ClassPermission "*"; // allow all (disables filtering basically)
  105. };
  106. ----------------------------------
  107. Here is an example of how to enable the `groovy.time.TimeCategory` class:
  108. [source,js]
  109. ----------------------------------
  110. grant {
  111. permission org.elasticsearch.script.ClassPermission "java.lang.Class";
  112. permission org.elasticsearch.script.ClassPermission "groovy.time.TimeCategory";
  113. };
  114. ----------------------------------
  115. [TIP]
  116. ======================================
  117. Before adding classes to the whitelist, consider the security impact that it
  118. will have on Elasticsearch. Do you really need an extra class or can your code
  119. be rewritten in a more secure way?
  120. It is quite possible that we have not whitelisted a generically useful and
  121. safe class. If you have a class that you think should be whitelisted by
  122. default, please open an issue on GitHub and we will consider the impact of
  123. doing so.
  124. ======================================
  125. See http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html for more information.