security.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 two 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. === Customising the classloader whitelist
  79. The classloader whitelist can be customised by tweaking the local Java
  80. Security Policy either:
  81. * system wide: `$JAVA_HOME/lib/security/java.policy`,
  82. * for just the `elasticsearch` user: `/home/elasticsearch/.java.policy`
  83. * by adding a system property to the <<sysconfig,es-java-opts>> configuration: `-Djava.security.policy=someURL`, or
  84. * via the `ES_JAVA_OPTS` environment variable with `-Djava.security.policy=someURL`:
  85. +
  86. [source,js]
  87. ---------------------------------
  88. export ES_JAVA_OPTS="${ES_JAVA_OPTS} -Djava.security.policy=file:///path/to/my.policy`
  89. ./bin/elasticsearch
  90. ---------------------------------
  91. Permissions may be granted at the class, package, or global level. For instance:
  92. [source,js]
  93. ----------------------------------
  94. grant {
  95. permission org.elasticsearch.script.ClassPermission "java.util.Base64"; // allow class
  96. permission org.elasticsearch.script.ClassPermission "java.util.*"; // allow package
  97. permission org.elasticsearch.script.ClassPermission "*"; // allow all (disables filtering basically)
  98. };
  99. ----------------------------------
  100. Here is an example of how to enable the `groovy.time.TimeCategory` class:
  101. [source,js]
  102. ----------------------------------
  103. grant {
  104. permission org.elasticsearch.script.ClassPermission "java.lang.Class";
  105. permission org.elasticsearch.script.ClassPermission "groovy.time.TimeCategory";
  106. };
  107. ----------------------------------
  108. [TIP]
  109. ======================================
  110. Before adding classes to the whitelist, consider the security impact that it
  111. will have on Elasticsearch. Do you really need an extra class or can your code
  112. be rewritten in a more secure way?
  113. It is quite possible that we have not whitelisted a generically useful and
  114. safe class. If you have a class that you think should be whitelisted by
  115. default, please open an issue on GitHub and we will consider the impact of
  116. doing so.
  117. ======================================
  118. See http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html for more information.