inline-encryption.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =================
  3. Inline Encryption
  4. =================
  5. Background
  6. ==========
  7. Inline encryption hardware sits logically between memory and the disk, and can
  8. en/decrypt data as it goes in/out of the disk. Inline encryption hardware has a
  9. fixed number of "keyslots" - slots into which encryption contexts (i.e. the
  10. encryption key, encryption algorithm, data unit size) can be programmed by the
  11. kernel at any time. Each request sent to the disk can be tagged with the index
  12. of a keyslot (and also a data unit number to act as an encryption tweak), and
  13. the inline encryption hardware will en/decrypt the data in the request with the
  14. encryption context programmed into that keyslot. This is very different from
  15. full disk encryption solutions like self encrypting drives/TCG OPAL/ATA
  16. Security standards, since with inline encryption, any block on disk could be
  17. encrypted with any encryption context the kernel chooses.
  18. Objective
  19. =========
  20. We want to support inline encryption (IE) in the kernel.
  21. To allow for testing, we also want a crypto API fallback when actual
  22. IE hardware is absent. We also want IE to work with layered devices
  23. like dm and loopback (i.e. we want to be able to use the IE hardware
  24. of the underlying devices if present, or else fall back to crypto API
  25. en/decryption).
  26. Constraints and notes
  27. =====================
  28. - IE hardware has a limited number of "keyslots" that can be programmed
  29. with an encryption context (key, algorithm, data unit size, etc.) at any time.
  30. One can specify a keyslot in a data request made to the device, and the
  31. device will en/decrypt the data using the encryption context programmed into
  32. that specified keyslot. When possible, we want to make multiple requests with
  33. the same encryption context share the same keyslot.
  34. - We need a way for upper layers like filesystems to specify an encryption
  35. context to use for en/decrypting a struct bio, and a device driver (like UFS)
  36. needs to be able to use that encryption context when it processes the bio.
  37. - We need a way for device drivers to expose their inline encryption
  38. capabilities in a unified way to the upper layers.
  39. Design
  40. ======
  41. We add a struct bio_crypt_ctx to struct bio that can
  42. represent an encryption context, because we need to be able to pass this
  43. encryption context from the upper layers (like the fs layer) to the
  44. device driver to act upon.
  45. While IE hardware works on the notion of keyslots, the FS layer has no
  46. knowledge of keyslots - it simply wants to specify an encryption context to
  47. use while en/decrypting a bio.
  48. We introduce a keyslot manager (KSM) that handles the translation from
  49. encryption contexts specified by the FS to keyslots on the IE hardware.
  50. This KSM also serves as the way IE hardware can expose its capabilities to
  51. upper layers. The generic mode of operation is: each device driver that wants
  52. to support IE will construct a KSM and set it up in its struct request_queue.
  53. Upper layers that want to use IE on this device can then use this KSM in
  54. the device's struct request_queue to translate an encryption context into
  55. a keyslot. The presence of the KSM in the request queue shall be used to mean
  56. that the device supports IE.
  57. The KSM uses refcounts to track which keyslots are idle (either they have no
  58. encryption context programmed, or there are no in-flight struct bios
  59. referencing that keyslot). When a new encryption context needs a keyslot, it
  60. tries to find a keyslot that has already been programmed with the same
  61. encryption context, and if there is no such keyslot, it evicts the least
  62. recently used idle keyslot and programs the new encryption context into that
  63. one. If no idle keyslots are available, then the caller will sleep until there
  64. is at least one.
  65. blk-mq changes, other block layer changes and blk-crypto-fallback
  66. =================================================================
  67. We add a pointer to a ``bi_crypt_context`` and ``keyslot`` to
  68. struct request. These will be referred to as the ``crypto fields``
  69. for the request. This ``keyslot`` is the keyslot into which the
  70. ``bi_crypt_context`` has been programmed in the KSM of the ``request_queue``
  71. that this request is being sent to.
  72. We introduce ``block/blk-crypto-fallback.c``, which allows upper layers to remain
  73. blissfully unaware of whether or not real inline encryption hardware is present
  74. underneath. When a bio is submitted with a target ``request_queue`` that doesn't
  75. support the encryption context specified with the bio, the block layer will
  76. en/decrypt the bio with the blk-crypto-fallback.
  77. If the bio is a ``WRITE`` bio, a bounce bio is allocated, and the data in the bio
  78. is encrypted stored in the bounce bio - blk-mq will then proceed to process the
  79. bounce bio as if it were not encrypted at all (except when blk-integrity is
  80. concerned). ``blk-crypto-fallback`` sets the bounce bio's ``bi_end_io`` to an
  81. internal function that cleans up the bounce bio and ends the original bio.
  82. If the bio is a ``READ`` bio, the bio's ``bi_end_io`` (and also ``bi_private``)
  83. is saved and overwritten by ``blk-crypto-fallback`` to
  84. ``bio_crypto_fallback_decrypt_bio``. The bio's ``bi_crypt_context`` is also
  85. overwritten with ``NULL``, so that to the rest of the stack, the bio looks
  86. as if it was a regular bio that never had an encryption context specified.
  87. ``bio_crypto_fallback_decrypt_bio`` will decrypt the bio, restore the original
  88. ``bi_end_io`` (and also ``bi_private``) and end the bio again.
  89. Regardless of whether real inline encryption hardware is used or the
  90. blk-crypto-fallback is used, the ciphertext written to disk (and hence the
  91. on-disk format of data) will be the same (assuming the hardware's implementation
  92. of the algorithm being used adheres to spec and functions correctly).
  93. If a ``request queue``'s inline encryption hardware claimed to support the
  94. encryption context specified with a bio, then it will not be handled by the
  95. ``blk-crypto-fallback``. We will eventually reach a point in blk-mq when a
  96. struct request needs to be allocated for that bio. At that point,
  97. blk-mq tries to program the encryption context into the ``request_queue``'s
  98. keyslot_manager, and obtain a keyslot, which it stores in its newly added
  99. ``keyslot`` field. This keyslot is released when the request is completed.
  100. When the first bio is added to a request, ``blk_crypto_rq_bio_prep`` is called,
  101. which sets the request's ``crypt_ctx`` to a copy of the bio's
  102. ``bi_crypt_context``. bio_crypt_do_front_merge is called whenever a subsequent
  103. bio is merged to the front of the request, which updates the ``crypt_ctx`` of
  104. the request so that it matches the newly merged bio's ``bi_crypt_context``. In particular, the request keeps a copy of the ``bi_crypt_context`` of the first
  105. bio in its bio-list (blk-mq needs to be careful to maintain this invariant
  106. during bio and request merges).
  107. To make it possible for inline encryption to work with request queue based
  108. layered devices, when a request is cloned, its ``crypto fields`` are cloned as
  109. well. When the cloned request is submitted, blk-mq programs the
  110. ``bi_crypt_context`` of the request into the clone's request_queue's keyslot
  111. manager, and stores the returned keyslot in the clone's ``keyslot``.
  112. API presented to users of the block layer
  113. =========================================
  114. ``struct blk_crypto_key`` represents a crypto key (the raw key, size of the
  115. key, the crypto algorithm to use, the data unit size to use, and the number of
  116. bytes required to represent data unit numbers that will be specified with the
  117. ``bi_crypt_context``).
  118. ``blk_crypto_init_key`` allows upper layers to initialize such a
  119. ``blk_crypto_key``.
  120. ``bio_crypt_set_ctx`` should be called on any bio that a user of
  121. the block layer wants en/decrypted via inline encryption (or the
  122. blk-crypto-fallback, if hardware support isn't available for the desired
  123. crypto configuration). This function takes the ``blk_crypto_key`` and the
  124. data unit number (DUN) to use when en/decrypting the bio.
  125. ``blk_crypto_config_supported`` allows upper layers to query whether or not the
  126. an encryption context passed to request queue can be handled by blk-crypto
  127. (either by real inline encryption hardware, or by the blk-crypto-fallback).
  128. This is useful e.g. when blk-crypto-fallback is disabled, and the upper layer
  129. wants to use an algorithm that may not supported by hardware - this function
  130. lets the upper layer know ahead of time that the algorithm isn't supported,
  131. and the upper layer can fallback to something else if appropriate.
  132. ``blk_crypto_start_using_key`` - Upper layers must call this function on
  133. ``blk_crypto_key`` and a ``request_queue`` before using the key with any bio
  134. headed for that ``request_queue``. This function ensures that either the
  135. hardware supports the key's crypto settings, or the crypto API fallback has
  136. transforms for the needed mode allocated and ready to go. Note that this
  137. function may allocate an ``skcipher``, and must not be called from the data
  138. path, since allocating ``skciphers`` from the data path can deadlock.
  139. ``blk_crypto_evict_key`` *must* be called by upper layers before a
  140. ``blk_crypto_key`` is freed. Further, it *must* only be called only once
  141. there are no more in-flight requests that use that ``blk_crypto_key``.
  142. ``blk_crypto_evict_key`` will ensure that a key is removed from any keyslots in
  143. inline encryption hardware that the key might have been programmed into (or the blk-crypto-fallback).
  144. API presented to device drivers
  145. ===============================
  146. A :c:type:``struct blk_keyslot_manager`` should be set up by device drivers in
  147. the ``request_queue`` of the device. The device driver needs to call
  148. ``blk_ksm_init`` (or its resource-managed variant ``devm_blk_ksm_init``) on the
  149. ``blk_keyslot_manager``, while specifying the number of keyslots supported by
  150. the hardware.
  151. The device driver also needs to tell the KSM how to actually manipulate the
  152. IE hardware in the device to do things like programming the crypto key into
  153. the IE hardware into a particular keyslot. All this is achieved through the
  154. struct blk_ksm_ll_ops field in the KSM that the device driver
  155. must fill up after initing the ``blk_keyslot_manager``.
  156. The KSM also handles runtime power management for the device when applicable
  157. (e.g. when it wants to program a crypto key into the IE hardware, the device
  158. must be runtime powered on) - so the device driver must also set the ``dev``
  159. field in the ksm to point to the `struct device` for the KSM to use for runtime
  160. power management.
  161. ``blk_ksm_reprogram_all_keys`` can be called by device drivers if the device
  162. needs each and every of its keyslots to be reprogrammed with the key it
  163. "should have" at the point in time when the function is called. This is useful
  164. e.g. if a device loses all its keys on runtime power down/up.
  165. If the driver used ``blk_ksm_init`` instead of ``devm_blk_ksm_init``, then
  166. ``blk_ksm_destroy`` should be called to free up all resources used by a
  167. ``blk_keyslot_manager`` once it is no longer needed.
  168. Layered Devices
  169. ===============
  170. Request queue based layered devices like dm-rq that wish to support IE need to
  171. create their own keyslot manager for their request queue, and expose whatever
  172. functionality they choose. When a layered device wants to pass a clone of that
  173. request to another ``request_queue``, blk-crypto will initialize and prepare the
  174. clone as necessary - see ``blk_crypto_insert_cloned_request`` in
  175. ``blk-crypto.c``.
  176. Future Optimizations for layered devices
  177. ========================================
  178. Creating a keyslot manager for a layered device uses up memory for each
  179. keyslot, and in general, a layered device merely passes the request on to a
  180. "child" device, so the keyslots in the layered device itself are completely
  181. unused, and don't need any refcounting or keyslot programming. We can instead
  182. define a new type of KSM; the "passthrough KSM", that layered devices can use
  183. to advertise an unlimited number of keyslots, and support for any encryption
  184. algorithms they choose, while not actually using any memory for each keyslot.
  185. Another use case for the "passthrough KSM" is for IE devices that do not have a
  186. limited number of keyslots.
  187. Interaction between inline encryption and blk integrity
  188. =======================================================
  189. At the time of this patch, there is no real hardware that supports both these
  190. features. However, these features do interact with each other, and it's not
  191. completely trivial to make them both work together properly. In particular,
  192. when a WRITE bio wants to use inline encryption on a device that supports both
  193. features, the bio will have an encryption context specified, after which
  194. its integrity information is calculated (using the plaintext data, since
  195. the encryption will happen while data is being written), and the data and
  196. integrity info is sent to the device. Obviously, the integrity info must be
  197. verified before the data is encrypted. After the data is encrypted, the device
  198. must not store the integrity info that it received with the plaintext data
  199. since that might reveal information about the plaintext data. As such, it must
  200. re-generate the integrity info from the ciphertext data and store that on disk
  201. instead. Another issue with storing the integrity info of the plaintext data is
  202. that it changes the on disk format depending on whether hardware inline
  203. encryption support is present or the kernel crypto API fallback is used (since
  204. if the fallback is used, the device will receive the integrity info of the
  205. ciphertext, not that of the plaintext).
  206. Because there isn't any real hardware yet, it seems prudent to assume that
  207. hardware implementations might not implement both features together correctly,
  208. and disallow the combination for now. Whenever a device supports integrity, the
  209. kernel will pretend that the device does not support hardware inline encryption
  210. (by essentially setting the keyslot manager in the request_queue of the device
  211. to NULL). When the crypto API fallback is enabled, this means that all bios with
  212. and encryption context will use the fallback, and IO will complete as usual.
  213. When the fallback is disabled, a bio with an encryption context will be failed.