security.asciidoc 9.5 KB

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