security.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. .. somewhat surprisingly the "bash" highlighter gives nice results with
  2. the pseudo-code notation used in the "Encryption" section.
  3. .. highlight:: bash
  4. ========
  5. Security
  6. ========
  7. Cryptography in Borg
  8. ====================
  9. Attack model
  10. ------------
  11. The attack model of Borg is that the environment of the client process
  12. (e.g. ``borg create``) is trusted and the repository (server) is not. The
  13. attacker has any and all access to the repository, including interactive
  14. manipulation (man-in-the-middle) for remote repositories.
  15. Furthermore the client environment is assumed to be persistent across
  16. attacks (practically this means that the security database cannot be
  17. deleted between attacks).
  18. Under these circumstances Borg guarantees that the attacker cannot
  19. 1. modify the data of any archive without the client detecting the change
  20. 2. rename, remove or add an archive without the client detecting the change
  21. 3. recover plain-text data
  22. 4. recover definite (heuristics based on access patterns are possible)
  23. structural information such as the object graph (which archives
  24. refer to what chunks)
  25. The attacker can always impose a denial of service per definition (he could
  26. forbid connections to the repository, or delete it entirely).
  27. Structural Authentication
  28. -------------------------
  29. Borg is fundamentally based on an object graph structure (see :ref:`internals`),
  30. where the root object is called the manifest.
  31. Borg follows the `Horton principle`_, which states that
  32. not only the message must be authenticated, but also its meaning (often
  33. expressed through context), because every object used is referenced by a
  34. parent object through its object ID up to the manifest. The object ID in
  35. Borg is a MAC of the object's plaintext, therefore this ensures that
  36. an attacker cannot change the context of an object without forging the MAC.
  37. In other words, the object ID itself only authenticates the plaintext of the
  38. object and not its context or meaning. The latter is established by a different
  39. object referring to an object ID, thereby assigning a particular meaning to
  40. an object. For example, an archive item contains a list of object IDs that
  41. represent packed file metadata. On their own it's not clear that these objects
  42. would represent what they do, but by the archive item referring to them
  43. in a particular part of its own data structure assigns this meaning.
  44. This results in a directed acyclic graph of authentication from the manifest
  45. to the data chunks of individual files.
  46. .. rubric:: Authenticating the manifest
  47. Since the manifest has a fixed ID (000...000) the aforementioned authentication
  48. does not apply to it, indeed, cannot apply to it; it is impossible to authenticate
  49. the root node of a DAG through its edges, since the root node has no incoming edges.
  50. With the scheme as described so far an attacker could easily replace the manifest,
  51. therefore Borg includes a tertiary authentication mechanism (TAM) that is applied
  52. to the manifest since version 1.0.9 (see :ref:`tam_vuln`).
  53. TAM works by deriving a separate key through HKDF_ from the other encryption and
  54. authentication keys and calculating the HMAC of the metadata to authenticate [#]_::
  55. # RANDOM(n) returns n random bytes
  56. salt = RANDOM(64)
  57. ikm = id_key || enc_key || enc_hmac_key
  58. # *context* depends on the operation, for manifest authentication it is
  59. # the ASCII string "borg-metadata-authentication-manifest".
  60. tam_key = HKDF-SHA-512(ikm, salt, context)
  61. # *data* is a dict-like structure
  62. data[hmac] = zeroes
  63. packed = pack(data)
  64. data[hmac] = HMAC(tam_key, packed)
  65. packed_authenticated = pack(data)
  66. Since an attacker cannot gain access to this key and also cannot make the
  67. client authenticate arbitrary data using this mechanism, the attacker is unable
  68. to forge the authentication.
  69. This effectively 'anchors' the manifest to the key, which is controlled by the
  70. client, thereby anchoring the entire DAG, making it impossible for an attacker
  71. to add, remove or modify any part of the DAG without Borg being able to detect
  72. the tampering.
  73. Note that when using BORG_PASSPHRASE the attacker cannot swap the *entire*
  74. repository against a new repository with e.g. repokey mode and no passphrase,
  75. because Borg will abort access when BORG_PASSPRHASE is incorrect.
  76. However, interactively a user might not notice this kind of attack
  77. immediately, if she assumes that the reason for the absent passphrase
  78. prompt is a set BORG_PASSPHRASE. See issue :issue:`2169` for details.
  79. .. [#] The reason why the authentication tag is stored in the packed
  80. data itself is that older Borg versions can still read the
  81. manifest this way, while a changed layout would have broken
  82. compatibility.
  83. Encryption
  84. ----------
  85. Encryption is currently based on the Encrypt-then-MAC construction,
  86. which is generally seen as the most robust way to create an authenticated
  87. encryption scheme from encryption and message authentication primitives.
  88. Every operation (encryption, MAC / authentication, chunk ID derivation)
  89. uses independent, random keys generated by `os.urandom`_ [#]_.
  90. Borg does not support unauthenticated encryption -- only authenticated encryption
  91. schemes are supported. No unauthenticated encryption schemes will be added
  92. in the future.
  93. Depending on the chosen mode (see :ref:`borg_init`) different primitives are used:
  94. - The actual encryption is currently always AES-256 in CTR mode. The
  95. counter is added in plaintext, since it is needed for decryption,
  96. and is also tracked locally on the client to avoid counter reuse.
  97. - The authentication primitive is either HMAC-SHA-256 or BLAKE2b-256
  98. in a keyed mode. HMAC-SHA-256 uses 256 bit keys, while BLAKE2b-256
  99. uses 512 bit keys.
  100. The latter is secure not only because BLAKE2b itself is not
  101. susceptible to `length extension`_, but also since it truncates the
  102. hash output from 512 bits to 256 bits, which would make the
  103. construction safe even if BLAKE2b were broken regarding length
  104. extension or similar attacks.
  105. - The primitive used for authentication is always the same primitive
  106. that is used for deriving the chunk ID, but they are always
  107. used with independent keys.
  108. Encryption::
  109. id = AUTHENTICATOR(id_key, data)
  110. compressed = compress(data)
  111. iv = reserve_iv()
  112. encrypted = AES-256-CTR(enc_key, 8-null-bytes || iv, compressed)
  113. authenticated = type-byte || AUTHENTICATOR(enc_hmac_key, encrypted) || iv || encrypted
  114. Decryption::
  115. # Given: input *authenticated* data, possibly a *chunk-id* to assert
  116. type-byte, mac, iv, encrypted = SPLIT(authenticated)
  117. ASSERT(type-byte is correct)
  118. ASSERT( CONSTANT-TIME-COMPARISON( mac, AUTHENTICATOR(enc_hmac_key, encrypted) ) )
  119. decrypted = AES-256-CTR(enc_key, 8-null-bytes || iv, encrypted)
  120. decompressed = decompress(decrypted)
  121. ASSERT( CONSTANT-TIME-COMPARISON( chunk-id, AUTHENTICATOR(id_key, decompressed) ) )
  122. .. [#] Using the :ref:`borg key migrate-to-repokey <borg_key_migrate-to-repokey>`
  123. command a user can convert repositories created using Attic in "passphrase"
  124. mode to "repokey" mode. In this case the keys were directly derived from
  125. the user's passphrase at some point using PBKDF2.
  126. Borg does not support "passphrase" mode otherwise any more.
  127. Offline key security
  128. --------------------
  129. Borg cannot secure the key material while it is running, because the keys
  130. are needed in plain to decrypt/encrypt repository objects.
  131. For offline storage of the encryption keys they are encrypted with a
  132. user-chosen passphrase.
  133. A 256 bit key encryption key (KEK) is derived from the passphrase
  134. using PBKDF2-HMAC-SHA256 with a random 256 bit salt which is then used
  135. to Encrypt-then-MAC a packed representation of the keys with
  136. AES-256-CTR with a constant initialization vector of 0 (this is the
  137. same construction used for Encryption_ with HMAC-SHA-256).
  138. The resulting MAC is stored alongside the ciphertext, which is
  139. converted to base64 in its entirety.
  140. This base64 blob (commonly referred to as *keyblob*) is then stored in
  141. the key file or in the repository config (keyfile and repokey modes
  142. respectively).
  143. This scheme, and specifically the use of a constant IV with the CTR
  144. mode, is secure because an identical passphrase will result in a
  145. different derived KEK for every encryption due to the salt.
  146. Implementations used
  147. --------------------
  148. We do not implement cryptographic primitives ourselves, but rely
  149. on widely used libraries providing them:
  150. - AES-CTR and HMAC-SHA-256 from OpenSSL 1.0 / 1.1 are used,
  151. which is also linked into the static binaries we provide.
  152. We think this is not an additional risk, since we don't ever
  153. use OpenSSL's networking, TLS or X.509 code, but only their
  154. primitives implemented in libcrypto.
  155. - SHA-256 and SHA-512 from Python's hashlib_ standard library module are used
  156. - HMAC, PBKDF2 and a constant-time comparison from Python's hmac_ standard
  157. library module is used.
  158. - BLAKE2b is either provided by the system's libb2, an official implementation,
  159. or a bundled copy of the BLAKE2 reference implementation (written in C).
  160. Implemented cryptographic constructions are:
  161. - Encrypt-then-MAC based on AES-256-CTR and either HMAC-SHA-256
  162. or keyed BLAKE2b256 as described above under Encryption_.
  163. - HKDF_-SHA-512
  164. .. _Horton principle: https://en.wikipedia.org/wiki/Horton_Principle
  165. .. _HKDF: https://tools.ietf.org/html/rfc5869
  166. .. _length extension: https://en.wikipedia.org/wiki/Length_extension_attack
  167. .. _hashlib: https://docs.python.org/3/library/hashlib.html
  168. .. _hmac: https://docs.python.org/3/library/hmac.html
  169. .. _os.urandom: https://docs.python.org/3/library/os.html#os.urandom
  170. Remote RPC protocol security
  171. ============================
  172. .. note:: This section could be further expanded / detailed.
  173. The RPC protocol is fundamentally based on msgpack'd messages exchanged
  174. over an encrypted SSH channel (the system's SSH client is used for this
  175. by piping data from/to it).
  176. This means that the authorization and transport security properties
  177. are inherited from SSH and the configuration of the SSH client
  178. and the SSH server. Therefore the remainder of this section
  179. will focus on the security of the RPC protocol within Borg.
  180. The assumed worst-case a server can inflict to a client is a
  181. denial of repository service.
  182. The situation were a server can create a general DoS on the client
  183. should be avoided, but might be possible by e.g. forcing the client to
  184. allocate large amounts of memory to decode large messages (or messages
  185. that merely indicate a large amount of data follows). See issue
  186. :issue:`2139` for details.
  187. We believe that other kinds of attacks, especially critical vulnerabilities
  188. like remote code execution are inhibited by the design of the protocol:
  189. 1. The server cannot send requests to the client on its own accord,
  190. it only can send responses. This avoids "unexpected inversion of control"
  191. issues.
  192. 2. msgpack serialization does not allow embedding or referencing code that
  193. is automatically executed. Incoming messages are unpacked by the msgpack
  194. unpacker into native Python data structures (like tuples and dictionaries),
  195. which are then passed to the rest of the program.
  196. Additional verification of the correct form of the responses could be implemented.
  197. 3. Remote errors are presented in two forms:
  198. 1. A simple plain-text *stderr* channel. A prefix string indicates the kind of message
  199. (e.g. WARNING, INFO, ERROR), which is used to suppress it according to the
  200. log level selected in the client.
  201. A server can send arbitrary log messages, which may confuse a user. However,
  202. log messages are only processed when server requests are in progress, therefore
  203. the server cannot interfere / confuse with security critical dialogue like
  204. the password prompt.
  205. 2. Server-side exceptions passed over the main data channel. These follow the
  206. general pattern of server-sent responses and are sent instead of response data
  207. for a request.