security.asciidoc 9.5 KB

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