security.asciidoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. [[modules-scripting-security]]
  2. === Scripting and security
  3. While Elasticsearch contributors make every effort to prevent scripts from
  4. running amok, security is something best done in
  5. https://en.wikipedia.org/wiki/Defense_in_depth_(computing)[layers] because
  6. all software has bugs and it is important to minimize the risk of failure in
  7. any security layer. Find below rules of thumb for how to keep Elasticsearch
  8. from being a vulnerability.
  9. [float]
  10. === Do not run as root
  11. First and foremost, never run Elasticsearch as the `root` user as this would
  12. allow any successful effort to circumvent the other security layers to do
  13. *anything* on your server. Elasticsearch will refuse to start if it detects
  14. that it is running as `root` but this is so important that it is worth double
  15. and triple checking.
  16. [float]
  17. === Do not expose Elasticsearch directly to users
  18. Do not expose Elasticsearch directly to users, instead have an application
  19. make requests on behalf of users. If this is not possible, have an application
  20. to sanitize requests from users. If *that* is not possible then have some
  21. mechanism to track which users did what. Understand that it is quite possible
  22. to write a <<search, `_search`>> that overwhelms Elasticsearch and brings down
  23. the cluster. All such searches should be considered bugs and the Elasticsearch
  24. contributors make an effort to prevent this but they are still possible.
  25. [float]
  26. === Do not expose Elasticsearch directly to the Internet
  27. Do not expose Elasticsearch to the Internet, instead have an application
  28. make requests on behalf of the Internet. Do not entertain the thought of having
  29. an application "sanitize" requests to Elasticsearch. Understand that it is
  30. possible for a sufficiently determined malicious user to write searches that
  31. overwhelm the Elasticsearch cluster and bring it down. For example:
  32. Good:
  33. * Users type text into a search box and the text is sent directly to a
  34. <<query-dsl-match-query>>, <<query-dsl-match-query-phrase>>,
  35. <<query-dsl-simple-query-string-query>>, or any of the <<search-suggesters>>.
  36. * Running a script with any of the above queries that was written as part of
  37. the application development process.
  38. * Running a script with `params` provided by users.
  39. * User actions makes documents with a fixed structure.
  40. Bad:
  41. * Users can write arbitrary scripts, queries, `_search` requests.
  42. * User actions make documents with structure defined by users.
  43. [float]
  44. [[modules-scripting-security-do-no-weaken]]
  45. === Do not weaken script security settings
  46. By default Elasticsearch will run inline, stored, and filesystem scripts for
  47. the builtin languages, namely the scripting language Painless, the template
  48. language Mustache, and the expression language Expressions. These *ought* to be
  49. safe to expose to trusted users and to your application servers because they
  50. have strong security sandboxes. The Elasticsearch committers do not support any
  51. non-sandboxed scripting languages and using any would be a poor choice because:
  52. 1. This drops a layer of security, leaving only Elasticsearch's builtin
  53. <<modules-scripting-other-layers, security layers>>.
  54. 2. Non-sandboxed scripts have unchecked access to Elasticsearch's internals and
  55. can cause all kinds of trouble if misused.
  56. [float]
  57. [[modules-scripting-other-layers]]
  58. === Other security layers
  59. In addition to user privileges and script sandboxing Elasticsearch uses the
  60. http://www.oracle.com/technetwork/java/seccodeguide-139067.html[Java Security Manager]
  61. and native security tools as additional layers of security.
  62. As part of its startup sequence Elasticsearch enables the Java Security Manager
  63. which limits the actions that can be taken by portions of the code. Painless
  64. uses this to limit the actions that generated Painless scripts can take,
  65. preventing them from being able to do things like write files and listen to
  66. sockets.
  67. Elasticsearch uses
  68. https://en.wikipedia.org/wiki/Seccomp[seccomp] in Linux,
  69. https://www.chromium.org/developers/design-documents/sandbox/osx-sandboxing-design[Seatbelt]
  70. in macOS, and
  71. https://msdn.microsoft.com/en-us/library/windows/desktop/ms684147[ActiveProcessLimit]
  72. on Windows to prevent Elasticsearch from forking or executing other processes.
  73. Below this we describe the security settings for scripts and how you can
  74. change from the defaults described above. You should be very, very careful
  75. when allowing more than the defaults. Any extra permissions weakens the total
  76. security of the Elasticsearch deployment.
  77. [[security-script-source]]
  78. [float]
  79. === Script source settings
  80. Which scripts Elasticsearch will execute where is controlled by settings
  81. starting with `scripts.`. The simplest settings allow scripts to be enabled
  82. or disabled based on where they are stored. For example:
  83. [source,yaml]
  84. -----------------------------------
  85. script.inline: false <1>
  86. script.stored: false <2>
  87. script.file: true <3>
  88. -----------------------------------
  89. <1> Refuse to run scripts provided inline in the API.
  90. <2> Refuse to run scripts stored using the API.
  91. <3> Run scripts found on the filesystem in `/etc/elasticsearch/scripts`
  92. (rpm or deb) or `config/scripts` (zip or tar).
  93. NOTE: These settings override the defaults mentioned
  94. <<modules-scripting-security-do-no-weaken, above>>. Recreating the defaults
  95. requires more fine grained settings described <<security-script-fine, below>>.
  96. [[security-script-context]]
  97. [float]
  98. === Script context settings
  99. Scripting may also be enabled or disabled in different contexts in the
  100. Elasticsearch API. The supported contexts are:
  101. [horizontal]
  102. `aggs`:: Aggregations
  103. `search`:: Search api, Percolator API and Suggester API
  104. `update`:: Update api
  105. `plugin`:: Any plugin that makes use of scripts under the generic `plugin` category
  106. Plugins can also define custom operations that they use scripts for instead
  107. of using the generic `plugin` category. Those operations can be referred to
  108. in the following form: `${pluginName}_${operation}`.
  109. The following example disables scripting for `update` and `plugin` operations,
  110. regardless of the script source or language. Scripts can still be executed
  111. as part of `aggregations`, `search` and plugins execution though, as the above
  112. defaults still get applied.
  113. [source,yaml]
  114. -----------------------------------
  115. script.update: false
  116. script.plugin: false
  117. -----------------------------------
  118. [[security-script-fine]]
  119. [float]
  120. === Fine-grained script settings
  121. First, the high-level script settings described above are applied in order
  122. (context settings have precedence over source settings). Then fine-grained
  123. settings which include the script language take precedence over any high-level
  124. settings. They have two forms:
  125. [source,yaml]
  126. ------------------------
  127. script.engine.{lang}.{inline|file|stored}.{context}: true|false
  128. ------------------------
  129. And
  130. [source,yaml]
  131. ------------------------
  132. script.engine.{lang}.{inline|file|stored}: true|false
  133. ------------------------
  134. For example:
  135. [source,yaml]
  136. -----------------------------------
  137. script.inline: false <1>
  138. script.stored: false <1>
  139. script.file: false <1>
  140. script.engine.painless.inline: true <2>
  141. script.engine.painless.stored.search: true <3>
  142. script.engine.painless.stored.aggs: true <3>
  143. script.engine.mustache.stored.search: true <4>
  144. -----------------------------------
  145. <1> Disable all scripting from any source.
  146. <2> Allow inline Painless scripts for all operations.
  147. <3> Allow stored Painless scripts to be used for search and aggregations.
  148. <4> Allow stored Mustache templates to be used for search.