security.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. .. _borgcrypto:
  8. Cryptography in Borg
  9. ====================
  10. Attack model
  11. ------------
  12. The attack model of Borg is that the environment of the client process
  13. (e.g. ``borg create``) is trusted and the repository (server) is not. The
  14. attacker has any and all access to the repository, including interactive
  15. manipulation (man-in-the-middle) for remote repositories.
  16. Furthermore the client environment is assumed to be persistent across
  17. attacks (practically this means that the security database cannot be
  18. deleted between attacks).
  19. Under these circumstances Borg guarantees that the attacker cannot
  20. 1. modify the data of any archive without the client detecting the change
  21. 2. rename, remove or add an archive without the client detecting the change
  22. 3. recover plain-text data
  23. 4. recover definite (heuristics based on access patterns are possible)
  24. structural information such as the object graph (which archives
  25. refer to what chunks)
  26. The attacker can always impose a denial of service per definition (he could
  27. forbid connections to the repository, or delete it entirely).
  28. .. _security_structural_auth:
  29. Structural Authentication
  30. -------------------------
  31. Borg is fundamentally based on an object graph structure (see :ref:`internals`),
  32. where the root object is called the manifest.
  33. Borg follows the `Horton principle`_, which states that
  34. not only the message must be authenticated, but also its meaning (often
  35. expressed through context), because every object used is referenced by a
  36. parent object through its object ID up to the manifest. The object ID in
  37. Borg is a MAC of the object's plaintext, therefore this ensures that
  38. an attacker cannot change the context of an object without forging the MAC.
  39. In other words, the object ID itself only authenticates the plaintext of the
  40. object and not its context or meaning. The latter is established by a different
  41. object referring to an object ID, thereby assigning a particular meaning to
  42. an object. For example, an archive item contains a list of object IDs that
  43. represent packed file metadata. On their own it's not clear that these objects
  44. would represent what they do, but by the archive item referring to them
  45. in a particular part of its own data structure assigns this meaning.
  46. This results in a directed acyclic graph of authentication from the manifest
  47. to the data chunks of individual files.
  48. .. _tam_description:
  49. .. rubric:: Authenticating the manifest
  50. Since the manifest has a fixed ID (000...000) the aforementioned authentication
  51. does not apply to it, indeed, cannot apply to it; it is impossible to authenticate
  52. the root node of a DAG through its edges, since the root node has no incoming edges.
  53. With the scheme as described so far an attacker could easily replace the manifest,
  54. therefore Borg includes a tertiary authentication mechanism (TAM) that is applied
  55. to the manifest since version 1.0.9 (see :ref:`tam_vuln`).
  56. TAM works by deriving a separate key through HKDF_ from the other encryption and
  57. authentication keys and calculating the HMAC of the metadata to authenticate [#]_::
  58. # RANDOM(n) returns n random bytes
  59. salt = RANDOM(64)
  60. ikm = id_key || enc_key || enc_hmac_key
  61. # *context* depends on the operation, for manifest authentication it is
  62. # the ASCII string "borg-metadata-authentication-manifest".
  63. tam_key = HKDF-SHA-512(ikm, salt, context)
  64. # *data* is a dict-like structure
  65. data[hmac] = zeroes
  66. packed = pack(data)
  67. data[hmac] = HMAC(tam_key, packed)
  68. packed_authenticated = pack(data)
  69. Since an attacker cannot gain access to this key and also cannot make the
  70. client authenticate arbitrary data using this mechanism, the attacker is unable
  71. to forge the authentication.
  72. This effectively 'anchors' the manifest to the key, which is controlled by the
  73. client, thereby anchoring the entire DAG, making it impossible for an attacker
  74. to add, remove or modify any part of the DAG without Borg being able to detect
  75. the tampering.
  76. Note that when using BORG_PASSPHRASE the attacker cannot swap the *entire*
  77. repository against a new repository with e.g. repokey mode and no passphrase,
  78. because Borg will abort access when BORG_PASSPRHASE is incorrect.
  79. However, interactively a user might not notice this kind of attack
  80. immediately, if she assumes that the reason for the absent passphrase
  81. prompt is a set BORG_PASSPHRASE. See issue :issue:`2169` for details.
  82. .. [#] The reason why the authentication tag is stored in the packed
  83. data itself is that older Borg versions can still read the
  84. manifest this way, while a changed layout would have broken
  85. compatibility.
  86. Encryption
  87. ----------
  88. Encryption is currently based on the Encrypt-then-MAC construction,
  89. which is generally seen as the most robust way to create an authenticated
  90. encryption scheme from encryption and message authentication primitives.
  91. Every operation (encryption, MAC / authentication, chunk ID derivation)
  92. uses independent, random keys generated by `os.urandom`_ [#]_.
  93. Borg does not support unauthenticated encryption -- only authenticated encryption
  94. schemes are supported. No unauthenticated encryption schemes will be added
  95. in the future.
  96. Depending on the chosen mode (see :ref:`borg_init`) different primitives are used:
  97. - The actual encryption is currently always AES-256 in CTR mode. The
  98. counter is added in plaintext, since it is needed for decryption,
  99. and is also tracked locally on the client to avoid counter reuse.
  100. - The authentication primitive is either HMAC-SHA-256 or BLAKE2b-256
  101. in a keyed mode. HMAC-SHA-256 uses 256 bit keys, while BLAKE2b-256
  102. uses 512 bit keys.
  103. The latter is secure not only because BLAKE2b itself is not
  104. susceptible to `length extension`_, but also since it truncates the
  105. hash output from 512 bits to 256 bits, which would make the
  106. construction safe even if BLAKE2b were broken regarding length
  107. extension or similar attacks.
  108. - The primitive used for authentication is always the same primitive
  109. that is used for deriving the chunk ID, but they are always
  110. used with independent keys.
  111. Encryption::
  112. id = AUTHENTICATOR(id_key, data)
  113. compressed = compress(data)
  114. iv = reserve_iv()
  115. encrypted = AES-256-CTR(enc_key, 8-null-bytes || iv, compressed)
  116. authenticated = type-byte || AUTHENTICATOR(enc_hmac_key, encrypted) || iv || encrypted
  117. Decryption::
  118. # Given: input *authenticated* data, possibly a *chunk-id* to assert
  119. type-byte, mac, iv, encrypted = SPLIT(authenticated)
  120. ASSERT(type-byte is correct)
  121. ASSERT( CONSTANT-TIME-COMPARISON( mac, AUTHENTICATOR(enc_hmac_key, encrypted) ) )
  122. decrypted = AES-256-CTR(enc_key, 8-null-bytes || iv, encrypted)
  123. decompressed = decompress(decrypted)
  124. ASSERT( CONSTANT-TIME-COMPARISON( chunk-id, AUTHENTICATOR(id_key, decompressed) ) )
  125. The client needs to track which counter values have been used, since
  126. encrypting a chunk requires a starting counter value and no two chunks
  127. may have overlapping counter ranges (otherwise the bitwise XOR of the
  128. overlapping plaintexts is revealed).
  129. The client does not directly track the counter value, because it
  130. changes often (with each encrypted chunk), instead it commits a
  131. "reservation" to the security database and the repository by taking
  132. the current counter value and adding 4 GiB / 16 bytes (the block size)
  133. to the counter. Thus the client only needs to commit a new reservation
  134. every few gigabytes of encrypted data.
  135. This mechanism also avoids reusing counter values in case the client
  136. crashes or the connection to the repository is severed, since any
  137. reservation would have been committed to both the security database
  138. and the repository before any data is encrypted. Borg uses its
  139. standard mechanism (SaveFile) to ensure that reservations are durable
  140. (on most hardware / storage systems), therefore a crash of the
  141. client's host would not impact tracking of reservations.
  142. However, this design is not infallible, and requires synchronization
  143. between clients, which is handled through the repository. Therefore in
  144. a multiple-client scenario a repository can trick a client into
  145. reusing counter values by ignoring counter reservations and replaying
  146. the manifest (which will fail if the client has seen a more recent
  147. manifest or has a more recent nonce reservation). If the repository is
  148. untrusted, but a trusted synchronization channel exists between
  149. clients, the security database could be synchronized between them over
  150. said trusted channel. This is not part of Borgs functionality.
  151. .. [#] Using the :ref:`borg key migrate-to-repokey <borg_key_migrate-to-repokey>`
  152. command a user can convert repositories created using Attic in "passphrase"
  153. mode to "repokey" mode. In this case the keys were directly derived from
  154. the user's passphrase at some point using PBKDF2.
  155. Borg does not support "passphrase" mode otherwise any more.
  156. .. _key_encryption:
  157. Offline key security
  158. --------------------
  159. Borg cannot secure the key material while it is running, because the keys
  160. are needed in plain to decrypt/encrypt repository objects.
  161. For offline storage of the encryption keys they are encrypted with a
  162. user-chosen passphrase.
  163. A 256 bit key encryption key (KEK) is derived from the passphrase
  164. using PBKDF2-HMAC-SHA256 with a random 256 bit salt which is then used
  165. to Encrypt-*and*-MAC (unlike the Encrypt-*then*-MAC approach used
  166. otherwise) a packed representation of the keys with AES-256-CTR with a
  167. constant initialization vector of 0. A HMAC-SHA256 of the plaintext is
  168. generated using the same KEK and is stored alongside the ciphertext,
  169. which is converted to base64 in its entirety.
  170. This base64 blob (commonly referred to as *keyblob*) is then stored in
  171. the key file or in the repository config (keyfile and repokey modes
  172. respectively).
  173. This scheme, and specifically the use of a constant IV with the CTR
  174. mode, is secure because an identical passphrase will result in a
  175. different derived KEK for every key encryption due to the salt.
  176. The use of Encrypt-and-MAC instead of Encrypt-then-MAC is seen as
  177. uncritical (but not ideal) here, since it is combined with AES-CTR mode,
  178. which is not vulnerable to padding attacks.
  179. .. seealso::
  180. Refer to the :ref:`key_files` section for details on the format.
  181. Refer to issue :issue:`747` for suggested improvements of the encryption
  182. scheme and password-based key derivation.
  183. Implementations used
  184. --------------------
  185. We do not implement cryptographic primitives ourselves, but rely
  186. on widely used libraries providing them:
  187. - AES-CTR and HMAC-SHA-256 from OpenSSL 1.0 / 1.1 are used,
  188. which is also linked into the static binaries we provide.
  189. We think this is not an additional risk, since we don't ever
  190. use OpenSSL's networking, TLS or X.509 code, but only their
  191. primitives implemented in libcrypto.
  192. - SHA-256 and SHA-512 from Python's hashlib_ standard library module are used.
  193. Borg requires a Python built with OpenSSL support (due to PBKDF2), therefore
  194. these functions are delegated to OpenSSL by Python.
  195. - HMAC, PBKDF2 and a constant-time comparison from Python's hmac_ standard
  196. library module is used. While the HMAC implementation is written in Python,
  197. the PBKDF2 implementation is provided by OpenSSL. The constant-time comparison
  198. (``compare_digest``) is written in C and part of Python.
  199. - BLAKE2b is either provided by the system's libb2, an official implementation,
  200. or a bundled copy of the BLAKE2 reference implementation (written in C).
  201. Implemented cryptographic constructions are:
  202. - Encrypt-then-MAC based on AES-256-CTR and either HMAC-SHA-256
  203. or keyed BLAKE2b256 as described above under Encryption_.
  204. - Encrypt-and-MAC based on AES-256-CTR and HMAC-SHA-256
  205. as described above under `Offline key security`_.
  206. - HKDF_-SHA-512
  207. .. _Horton principle: https://en.wikipedia.org/wiki/Horton_Principle
  208. .. _HKDF: https://tools.ietf.org/html/rfc5869
  209. .. _length extension: https://en.wikipedia.org/wiki/Length_extension_attack
  210. .. _hashlib: https://docs.python.org/3/library/hashlib.html
  211. .. _hmac: https://docs.python.org/3/library/hmac.html
  212. .. _os.urandom: https://docs.python.org/3/library/os.html#os.urandom
  213. Remote RPC protocol security
  214. ============================
  215. .. note:: This section could be further expanded / detailed.
  216. The RPC protocol is fundamentally based on msgpack'd messages exchanged
  217. over an encrypted SSH channel (the system's SSH client is used for this
  218. by piping data from/to it).
  219. This means that the authorization and transport security properties
  220. are inherited from SSH and the configuration of the SSH client and the
  221. SSH server -- Borg RPC does not contain *any* networking
  222. code. Networking is done by the SSH client running in a separate
  223. process, Borg only communicates over the standard pipes (stdout,
  224. stderr and stdin) with this process. This also means that Borg doesn't
  225. have to directly use a SSH client (or SSH at all). For example,
  226. ``sudo`` or ``qrexec`` could be used as an intermediary.
  227. By using the system's SSH client and not implementing a
  228. (cryptographic) network protocol Borg sidesteps many security issues
  229. that would normally impact distributing statically linked / standalone
  230. binaries.
  231. The remainder of this section will focus on the security of the RPC
  232. protocol within Borg.
  233. The assumed worst-case a server can inflict to a client is a
  234. denial of repository service.
  235. The situation were a server can create a general DoS on the client
  236. should be avoided, but might be possible by e.g. forcing the client to
  237. allocate large amounts of memory to decode large messages (or messages
  238. that merely indicate a large amount of data follows). The RPC protocol
  239. code uses a limited msgpack Unpacker to prohibit this.
  240. We believe that other kinds of attacks, especially critical vulnerabilities
  241. like remote code execution are inhibited by the design of the protocol:
  242. 1. The server cannot send requests to the client on its own accord,
  243. it only can send responses. This avoids "unexpected inversion of control"
  244. issues.
  245. 2. msgpack serialization does not allow embedding or referencing code that
  246. is automatically executed. Incoming messages are unpacked by the msgpack
  247. unpacker into native Python data structures (like tuples and dictionaries),
  248. which are then passed to the rest of the program.
  249. Additional verification of the correct form of the responses could be implemented.
  250. 3. Remote errors are presented in two forms:
  251. 1. A simple plain-text *stderr* channel. A prefix string indicates the kind of message
  252. (e.g. WARNING, INFO, ERROR), which is used to suppress it according to the
  253. log level selected in the client.
  254. A server can send arbitrary log messages, which may confuse a user. However,
  255. log messages are only processed when server requests are in progress, therefore
  256. the server cannot interfere / confuse with security critical dialogue like
  257. the password prompt.
  258. 2. Server-side exceptions passed over the main data channel. These follow the
  259. general pattern of server-sent responses and are sent instead of response data
  260. for a request.
  261. The msgpack implementation used (msgpack-python) has a good security track record,
  262. a large test suite and no issues found by fuzzing. It is based on the msgpack-c implementation,
  263. sharing the unpacking engine and some support code. msgpack-c has a good track record as well.
  264. Some issues [#]_ in the past were located in code not included in msgpack-python.
  265. Borg does not use msgpack-c.
  266. .. [#] - `MessagePack fuzzing <https://blog.gypsyengineer.com/fun/msgpack-fuzzing.html>`_
  267. - `Fixed integer overflow and EXT size problem <https://github.com/msgpack/msgpack-c/pull/547>`_
  268. - `Fixed array and map size overflow <https://github.com/msgpack/msgpack-c/pull/550>`_
  269. Using OpenSSL
  270. =============
  271. Borg uses the OpenSSL library for most cryptography (see `Implementations used`_ above).
  272. OpenSSL is bundled with static releases, thus the bundled copy is not updated with system
  273. updates.
  274. OpenSSL is a large and complex piece of software and has had its share of vulnerabilities,
  275. however, it is important to note that Borg links against ``libcrypto`` **not** ``libssl``.
  276. libcrypto is the low-level cryptography part of OpenSSL,
  277. while libssl implements TLS and related protocols.
  278. The latter is not used by Borg (cf. `Remote RPC protocol security`_, Borg itself does not implement
  279. any network access) and historically contained most vulnerabilities, especially critical ones.
  280. The static binaries released by the project contain neither libssl nor the Python ssl/_ssl modules.