security.asciidoc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. [[modules-scripting-security]]
  2. === Scripting and security
  3. You should never run Elasticsearch as the `root` user, as this would allow a
  4. script to access or do *anything* on your server, without limitations.
  5. You should not expose Elasticsearch directly to users, but instead have a
  6. proxy application inbetween. If you *do* intend to expose Elasticsearch
  7. directly to your users, then you have to decide whether you trust them enough
  8. to run scripts on your box or not, and apply the appropriate safety measures.
  9. [[enable-dynamic-scripting]]
  10. [float]
  11. === Enabling dynamic scripting
  12. The `script.*` settings allow for <<security-script-fine,fine-grained>>
  13. control of which script languages (e.g `groovy`, `painless`) are allowed to
  14. run in which context ( e.g. `search`, `aggs`, `update`), and where the script
  15. source is allowed to come from (i.e. `inline`, `stored`, `file`).
  16. For instance, the following setting enables `stored` `update` scripts for
  17. `groovy`:
  18. [source,yaml]
  19. ----------------
  20. script.engine.groovy.inline.update: true
  21. ----------------
  22. Less fine-grained settings exist which allow you to enable or disable scripts
  23. for all sources, all languages, or all contexts. The following settings
  24. enable `inline` and `stored` scripts for all languages in all contexts:
  25. [source,yaml]
  26. -----------------------------------
  27. script.inline: true
  28. script.stored: true
  29. -----------------------------------
  30. WARNING: The above settings mean that anybody who can send requests to your
  31. Elasticsearch instance can run whatever scripts they choose! This is a
  32. security risk and may well lead to your Elasticsearch cluster being
  33. compromised.
  34. [[security-script-source]]
  35. [float]
  36. === Script source settings
  37. Scripts may be enabled or disabled depending on their source: `inline`,
  38. `stored` in the cluster state, or from a `file` on each node in the cluster.
  39. Each of these settings takes one of these values:
  40. [horizontal]
  41. `false`:: Scripting is enabled.
  42. `true`:: Scripting is disabled.
  43. `sandbox`:: Scripting is enabled only for sandboxed languages.
  44. The default values are the following:
  45. [source,yaml]
  46. -----------------------------------
  47. script.inline: sandbox
  48. script.stored: sandbox
  49. script.file: true
  50. -----------------------------------
  51. NOTE: Global scripting settings affect the `mustache` scripting language.
  52. <<search-template,Search templates>> internally use the `mustache` language,
  53. and will still be enabled by default as the `mustache` engine is sandboxed,
  54. but they will be enabled/disabled according to fine-grained settings
  55. specified in `elasticsearch.yml`.
  56. [[security-script-context]]
  57. [float]
  58. === Script context settings
  59. Scripting may also be enabled or disabled in different contexts in the
  60. Elasticsearch API. The supported contexts are:
  61. [horizontal]
  62. `aggs`:: Aggregations
  63. `search`:: Search api, Percolator API and Suggester API
  64. `update`:: Update api
  65. `plugin`:: Any plugin that makes use of scripts under the generic `plugin` category
  66. Plugins can also define custom operations that they use scripts for instead
  67. of using the generic `plugin` category. Those operations can be referred to
  68. in the following form: `${pluginName}_${operation}`.
  69. The following example disables scripting for `update` and `plugin` operations,
  70. regardless of the script source or language. Scripts can still be executed
  71. from sandboxed languages as part of `aggregations`, `search` and plugins
  72. execution though, as the above defaults still get applied.
  73. [source,yaml]
  74. -----------------------------------
  75. script.update: false
  76. script.plugin: false
  77. -----------------------------------
  78. [[security-script-fine]]
  79. [float]
  80. === Fine-grained script settings
  81. First, the high-level script settings described above are applied in order
  82. (context settings have precedence over source settings). Then, fine-grained
  83. settings which include the script language take precedence over any high-level
  84. settings.
  85. Fine-grained settings have the form:
  86. [source,yaml]
  87. ------------------------
  88. script.engine.{lang}.{source}.{context}: true|false
  89. ------------------------
  90. For example:
  91. [source,yaml]
  92. -----------------------------------
  93. script.inline: false <1>
  94. script.stored: false <1>
  95. script.file: false <1>
  96. script.engine.groovy.stored.search: true <2>
  97. script.engine.groovy.stored.aggs: true <2>
  98. script.engine.mustache.stored.search: true <3>
  99. -----------------------------------
  100. <1> Disable all scripting from any source.
  101. <2> Allow stored Groovy scripts to be used for search and aggregations.
  102. <3> Allow stored Mustache templates to be used for search.
  103. [[java-security-manager]]
  104. [float]
  105. === Java Security Manager
  106. Elasticsearch runs with the https://docs.oracle.com/javase/tutorial/essential/environment/security.html[Java Security Manager]
  107. enabled by default. The security policy in Elasticsearch locks down the
  108. permissions granted to each class to the bare minimum required to operate.
  109. The benefit of doing this is that it severely limits the attack vectors
  110. available to a hacker.
  111. Restricting permissions is particularly important with scripting languages
  112. like Groovy and Javascript which are designed to do anything that can be done
  113. in Java itself, including writing to the file system, opening sockets to
  114. remote servers, etc.
  115. [float]
  116. === Script Classloader Whitelist
  117. Scripting languages are only allowed to load classes which appear in a
  118. hardcoded whitelist that can be found in
  119. https://github.com/elastic/elasticsearch/blob/{branch}/core/src/main/java/org/elasticsearch/script/ClassPermission.java[`org.elasticsearch.script.ClassPermission`].
  120. In a script, attempting to load a class that does not appear in the whitelist
  121. _may_ result in a `ClassNotFoundException`, for instance this script:
  122. [source,json]
  123. ------------------------------
  124. GET _search
  125. {
  126. "script_fields": {
  127. "the_hour": {
  128. "script": "use(java.math.BigInteger); new BigInteger(1)"
  129. }
  130. }
  131. }
  132. ------------------------------
  133. will return the following exception:
  134. [source,json]
  135. ------------------------------
  136. {
  137. "reason": {
  138. "type": "script_exception",
  139. "reason": "failed to run inline script [use(java.math.BigInteger); new BigInteger(1)] using lang [groovy]",
  140. "caused_by": {
  141. "type": "no_class_def_found_error",
  142. "reason": "java/math/BigInteger",
  143. "caused_by": {
  144. "type": "class_not_found_exception",
  145. "reason": "java.math.BigInteger"
  146. }
  147. }
  148. }
  149. }
  150. ------------------------------
  151. However, classloader issues may also result in more difficult to interpret
  152. exceptions. For instance, this script:
  153. [source,groovy]
  154. ------------------------------
  155. use(groovy.time.TimeCategory); new Date(123456789).format('HH')
  156. ------------------------------
  157. Returns the following exception:
  158. [source,json]
  159. ------------------------------
  160. {
  161. "reason": {
  162. "type": "script_exception",
  163. "reason": "failed to run inline script [use(groovy.time.TimeCategory); new Date(123456789).format('HH')] using lang [groovy]",
  164. "caused_by": {
  165. "type": "missing_property_exception",
  166. "reason": "No such property: groovy for class: 8d45f5c1a07a1ab5dda953234863e283a7586240"
  167. }
  168. }
  169. }
  170. ------------------------------
  171. [float]
  172. == Dealing with Java Security Manager issues
  173. If you encounter issues with the Java Security Manager, you have two options
  174. for resolving these issues:
  175. [float]
  176. === Fix the security problem
  177. The safest and most secure long term solution is to change the code causing
  178. the security issue. We recognise that this may take time to do correctly and
  179. so we provide the following two alternatives.
  180. [float]
  181. === Customising the classloader whitelist
  182. The classloader whitelist can be customised by tweaking the local Java
  183. Security Policy either:
  184. * system wide: `$JAVA_HOME/lib/security/java.policy`,
  185. * for just the `elasticsearch` user: `/home/elasticsearch/.java.policy`
  186. * by adding a system property to the <<sysconfig,es-java-opts>> configuration: `-Djava.security.policy=someURL`, or
  187. * via the `ES_JAVA_OPTS` environment variable with `-Djava.security.policy=someURL`:
  188. +
  189. [source,js]
  190. ---------------------------------
  191. export ES_JAVA_OPTS="${ES_JAVA_OPTS} -Djava.security.policy=file:///path/to/my.policy`
  192. ./bin/elasticsearch
  193. ---------------------------------
  194. Permissions may be granted at the class, package, or global level. For instance:
  195. [source,js]
  196. ----------------------------------
  197. grant {
  198. permission org.elasticsearch.script.ClassPermission "java.util.Base64"; // allow class
  199. permission org.elasticsearch.script.ClassPermission "java.util.*"; // allow package
  200. permission org.elasticsearch.script.ClassPermission "*"; // allow all (disables filtering basically)
  201. };
  202. ----------------------------------
  203. Here is an example of how to enable the `groovy.time.TimeCategory` class:
  204. [source,js]
  205. ----------------------------------
  206. grant {
  207. permission org.elasticsearch.script.ClassPermission "java.lang.Class";
  208. permission org.elasticsearch.script.ClassPermission "groovy.time.TimeCategory";
  209. };
  210. ----------------------------------
  211. [TIP]
  212. ======================================
  213. Before adding classes to the whitelist, consider the security impact that it
  214. will have on Elasticsearch. Do you really need an extra class or can your code
  215. be rewritten in a more secure way?
  216. It is quite possible that we have not whitelisted a generically useful and
  217. safe class. If you have a class that you think should be whitelisted by
  218. default, please open an issue on GitHub and we will consider the impact of
  219. doing so.
  220. ======================================
  221. See http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html for more information.