ubifs-authentication.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. UBIFS Authentication
  3. .. sigma star gmbh
  4. .. 2018
  5. ============================
  6. UBIFS Authentication Support
  7. ============================
  8. Introduction
  9. ============
  10. UBIFS utilizes the fscrypt framework to provide confidentiality for file
  11. contents and file names. This prevents attacks where an attacker is able to
  12. read contents of the filesystem on a single point in time. A classic example
  13. is a lost smartphone where the attacker is unable to read personal data stored
  14. on the device without the filesystem decryption key.
  15. At the current state, UBIFS encryption however does not prevent attacks where
  16. the attacker is able to modify the filesystem contents and the user uses the
  17. device afterwards. In such a scenario an attacker can modify filesystem
  18. contents arbitrarily without the user noticing. One example is to modify a
  19. binary to perform a malicious action when executed [DMC-CBC-ATTACK]. Since
  20. most of the filesystem metadata of UBIFS is stored in plain, this makes it
  21. fairly easy to swap files and replace their contents.
  22. Other full disk encryption systems like dm-crypt cover all filesystem metadata,
  23. which makes such kinds of attacks more complicated, but not impossible.
  24. Especially, if the attacker is given access to the device multiple points in
  25. time. For dm-crypt and other filesystems that build upon the Linux block IO
  26. layer, the dm-integrity or dm-verity subsystems [DM-INTEGRITY, DM-VERITY]
  27. can be used to get full data authentication at the block layer.
  28. These can also be combined with dm-crypt [CRYPTSETUP2].
  29. This document describes an approach to get file contents _and_ full metadata
  30. authentication for UBIFS. Since UBIFS uses fscrypt for file contents and file
  31. name encryption, the authentication system could be tied into fscrypt such that
  32. existing features like key derivation can be utilized. It should however also
  33. be possible to use UBIFS authentication without using encryption.
  34. MTD, UBI & UBIFS
  35. ----------------
  36. On Linux, the MTD (Memory Technology Devices) subsystem provides a uniform
  37. interface to access raw flash devices. One of the more prominent subsystems that
  38. work on top of MTD is UBI (Unsorted Block Images). It provides volume management
  39. for flash devices and is thus somewhat similar to LVM for block devices. In
  40. addition, it deals with flash-specific wear-leveling and transparent I/O error
  41. handling. UBI offers logical erase blocks (LEBs) to the layers on top of it
  42. and maps them transparently to physical erase blocks (PEBs) on the flash.
  43. UBIFS is a filesystem for raw flash which operates on top of UBI. Thus, wear
  44. leveling and some flash specifics are left to UBI, while UBIFS focuses on
  45. scalability, performance and recoverability.
  46. ::
  47. +------------+ +*******+ +-----------+ +-----+
  48. | | * UBIFS * | UBI-BLOCK | | ... |
  49. | JFFS/JFFS2 | +*******+ +-----------+ +-----+
  50. | | +-----------------------------+ +-----------+ +-----+
  51. | | | UBI | | MTD-BLOCK | | ... |
  52. +------------+ +-----------------------------+ +-----------+ +-----+
  53. +------------------------------------------------------------------+
  54. | MEMORY TECHNOLOGY DEVICES (MTD) |
  55. +------------------------------------------------------------------+
  56. +-----------------------------+ +--------------------------+ +-----+
  57. | NAND DRIVERS | | NOR DRIVERS | | ... |
  58. +-----------------------------+ +--------------------------+ +-----+
  59. Figure 1: Linux kernel subsystems for dealing with raw flash
  60. Internally, UBIFS maintains multiple data structures which are persisted on
  61. the flash:
  62. - *Index*: an on-flash B+ tree where the leaf nodes contain filesystem data
  63. - *Journal*: an additional data structure to collect FS changes before updating
  64. the on-flash index and reduce flash wear.
  65. - *Tree Node Cache (TNC)*: an in-memory B+ tree that reflects the current FS
  66. state to avoid frequent flash reads. It is basically the in-memory
  67. representation of the index, but contains additional attributes.
  68. - *LEB property tree (LPT)*: an on-flash B+ tree for free space accounting per
  69. UBI LEB.
  70. In the remainder of this section we will cover the on-flash UBIFS data
  71. structures in more detail. The TNC is of less importance here since it is never
  72. persisted onto the flash directly. More details on UBIFS can also be found in
  73. [UBIFS-WP].
  74. UBIFS Index & Tree Node Cache
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. Basic on-flash UBIFS entities are called *nodes*. UBIFS knows different types
  77. of nodes. Eg. data nodes (``struct ubifs_data_node``) which store chunks of file
  78. contents or inode nodes (``struct ubifs_ino_node``) which represent VFS inodes.
  79. Almost all types of nodes share a common header (``ubifs_ch``) containing basic
  80. information like node type, node length, a sequence number, etc. (see
  81. ``fs/ubifs/ubifs-media.h`` in kernel source). Exceptions are entries of the LPT
  82. and some less important node types like padding nodes which are used to pad
  83. unusable content at the end of LEBs.
  84. To avoid re-writing the whole B+ tree on every single change, it is implemented
  85. as *wandering tree*, where only the changed nodes are re-written and previous
  86. versions of them are obsoleted without erasing them right away. As a result,
  87. the index is not stored in a single place on the flash, but *wanders* around
  88. and there are obsolete parts on the flash as long as the LEB containing them is
  89. not reused by UBIFS. To find the most recent version of the index, UBIFS stores
  90. a special node called *master node* into UBI LEB 1 which always points to the
  91. most recent root node of the UBIFS index. For recoverability, the master node
  92. is additionally duplicated to LEB 2. Mounting UBIFS is thus a simple read of
  93. LEB 1 and 2 to get the current master node and from there get the location of
  94. the most recent on-flash index.
  95. The TNC is the in-memory representation of the on-flash index. It contains some
  96. additional runtime attributes per node which are not persisted. One of these is
  97. a dirty-flag which marks nodes that have to be persisted the next time the
  98. index is written onto the flash. The TNC acts as a write-back cache and all
  99. modifications of the on-flash index are done through the TNC. Like other caches,
  100. the TNC does not have to mirror the full index into memory, but reads parts of
  101. it from flash whenever needed. A *commit* is the UBIFS operation of updating the
  102. on-flash filesystem structures like the index. On every commit, the TNC nodes
  103. marked as dirty are written to the flash to update the persisted index.
  104. Journal
  105. ~~~~~~~
  106. To avoid wearing out the flash, the index is only persisted (*commited*) when
  107. certain conditions are met (eg. ``fsync(2)``). The journal is used to record
  108. any changes (in form of inode nodes, data nodes etc.) between commits
  109. of the index. During mount, the journal is read from the flash and replayed
  110. onto the TNC (which will be created on-demand from the on-flash index).
  111. UBIFS reserves a bunch of LEBs just for the journal called *log area*. The
  112. amount of log area LEBs is configured on filesystem creation (using
  113. ``mkfs.ubifs``) and stored in the superblock node. The log area contains only
  114. two types of nodes: *reference nodes* and *commit start nodes*. A commit start
  115. node is written whenever an index commit is performed. Reference nodes are
  116. written on every journal update. Each reference node points to the position of
  117. other nodes (inode nodes, data nodes etc.) on the flash that are part of this
  118. journal entry. These nodes are called *buds* and describe the actual filesystem
  119. changes including their data.
  120. The log area is maintained as a ring. Whenever the journal is almost full,
  121. a commit is initiated. This also writes a commit start node so that during
  122. mount, UBIFS will seek for the most recent commit start node and just replay
  123. every reference node after that. Every reference node before the commit start
  124. node will be ignored as they are already part of the on-flash index.
  125. When writing a journal entry, UBIFS first ensures that enough space is
  126. available to write the reference node and buds part of this entry. Then, the
  127. reference node is written and afterwards the buds describing the file changes.
  128. On replay, UBIFS will record every reference node and inspect the location of
  129. the referenced LEBs to discover the buds. If these are corrupt or missing,
  130. UBIFS will attempt to recover them by re-reading the LEB. This is however only
  131. done for the last referenced LEB of the journal. Only this can become corrupt
  132. because of a power cut. If the recovery fails, UBIFS will not mount. An error
  133. for every other LEB will directly cause UBIFS to fail the mount operation.
  134. ::
  135. | ---- LOG AREA ---- | ---------- MAIN AREA ------------ |
  136. -----+------+-----+--------+---- ------+-----+-----+---------------
  137. \ | | | | / / | | | \
  138. / CS | REF | REF | | \ \ DENT | INO | INO | /
  139. \ | | | | / / | | | \
  140. ----+------+-----+--------+--- -------+-----+-----+----------------
  141. | | ^ ^
  142. | | | |
  143. +------------------------+ |
  144. | |
  145. +-------------------------------+
  146. Figure 2: UBIFS flash layout of log area with commit start nodes
  147. (CS) and reference nodes (REF) pointing to main area
  148. containing their buds
  149. LEB Property Tree/Table
  150. ~~~~~~~~~~~~~~~~~~~~~~~
  151. The LEB property tree is used to store per-LEB information. This includes the
  152. LEB type and amount of free and *dirty* (old, obsolete content) space [1]_ on
  153. the LEB. The type is important, because UBIFS never mixes index nodes with data
  154. nodes on a single LEB and thus each LEB has a specific purpose. This again is
  155. useful for free space calculations. See [UBIFS-WP] for more details.
  156. The LEB property tree again is a B+ tree, but it is much smaller than the
  157. index. Due to its smaller size it is always written as one chunk on every
  158. commit. Thus, saving the LPT is an atomic operation.
  159. .. [1] Since LEBs can only be appended and never overwritten, there is a
  160. difference between free space ie. the remaining space left on the LEB to be
  161. written to without erasing it and previously written content that is obsolete
  162. but can't be overwritten without erasing the full LEB.
  163. UBIFS Authentication
  164. ====================
  165. This chapter introduces UBIFS authentication which enables UBIFS to verify
  166. the authenticity and integrity of metadata and file contents stored on flash.
  167. Threat Model
  168. ------------
  169. UBIFS authentication enables detection of offline data modification. While it
  170. does not prevent it, it enables (trusted) code to check the integrity and
  171. authenticity of on-flash file contents and filesystem metadata. This covers
  172. attacks where file contents are swapped.
  173. UBIFS authentication will not protect against rollback of full flash contents.
  174. Ie. an attacker can still dump the flash and restore it at a later time without
  175. detection. It will also not protect against partial rollback of individual
  176. index commits. That means that an attacker is able to partially undo changes.
  177. This is possible because UBIFS does not immediately overwrites obsolete
  178. versions of the index tree or the journal, but instead marks them as obsolete
  179. and garbage collection erases them at a later time. An attacker can use this by
  180. erasing parts of the current tree and restoring old versions that are still on
  181. the flash and have not yet been erased. This is possible, because every commit
  182. will always write a new version of the index root node and the master node
  183. without overwriting the previous version. This is further helped by the
  184. wear-leveling operations of UBI which copies contents from one physical
  185. eraseblock to another and does not atomically erase the first eraseblock.
  186. UBIFS authentication does not cover attacks where an attacker is able to
  187. execute code on the device after the authentication key was provided.
  188. Additional measures like secure boot and trusted boot have to be taken to
  189. ensure that only trusted code is executed on a device.
  190. Authentication
  191. --------------
  192. To be able to fully trust data read from flash, all UBIFS data structures
  193. stored on flash are authenticated. That is:
  194. - The index which includes file contents, file metadata like extended
  195. attributes, file length etc.
  196. - The journal which also contains file contents and metadata by recording changes
  197. to the filesystem
  198. - The LPT which stores UBI LEB metadata which UBIFS uses for free space accounting
  199. Index Authentication
  200. ~~~~~~~~~~~~~~~~~~~~
  201. Through UBIFS' concept of a wandering tree, it already takes care of only
  202. updating and persisting changed parts from leaf node up to the root node
  203. of the full B+ tree. This enables us to augment the index nodes of the tree
  204. with a hash over each node's child nodes. As a result, the index basically also
  205. a Merkle tree. Since the leaf nodes of the index contain the actual filesystem
  206. data, the hashes of their parent index nodes thus cover all the file contents
  207. and file metadata. When a file changes, the UBIFS index is updated accordingly
  208. from the leaf nodes up to the root node including the master node. This process
  209. can be hooked to recompute the hash only for each changed node at the same time.
  210. Whenever a file is read, UBIFS can verify the hashes from each leaf node up to
  211. the root node to ensure the node's integrity.
  212. To ensure the authenticity of the whole index, the UBIFS master node stores a
  213. keyed hash (HMAC) over its own contents and a hash of the root node of the index
  214. tree. As mentioned above, the master node is always written to the flash whenever
  215. the index is persisted (ie. on index commit).
  216. Using this approach only UBIFS index nodes and the master node are changed to
  217. include a hash. All other types of nodes will remain unchanged. This reduces
  218. the storage overhead which is precious for users of UBIFS (ie. embedded
  219. devices).
  220. ::
  221. +---------------+
  222. | Master Node |
  223. | (hash) |
  224. +---------------+
  225. |
  226. v
  227. +-------------------+
  228. | Index Node #1 |
  229. | |
  230. | branch0 branchn |
  231. | (hash) (hash) |
  232. +-------------------+
  233. | ... | (fanout: 8)
  234. | |
  235. +-------+ +------+
  236. | |
  237. v v
  238. +-------------------+ +-------------------+
  239. | Index Node #2 | | Index Node #3 |
  240. | | | |
  241. | branch0 branchn | | branch0 branchn |
  242. | (hash) (hash) | | (hash) (hash) |
  243. +-------------------+ +-------------------+
  244. | ... | ... |
  245. v v v
  246. +-----------+ +----------+ +-----------+
  247. | Data Node | | INO Node | | DENT Node |
  248. +-----------+ +----------+ +-----------+
  249. Figure 3: Coverage areas of index node hash and master node HMAC
  250. The most important part for robustness and power-cut safety is to atomically
  251. persist the hash and file contents. Here the existing UBIFS logic for how
  252. changed nodes are persisted is already designed for this purpose such that
  253. UBIFS can safely recover if a power-cut occurs while persisting. Adding
  254. hashes to index nodes does not change this since each hash will be persisted
  255. atomically together with its respective node.
  256. Journal Authentication
  257. ~~~~~~~~~~~~~~~~~~~~~~
  258. The journal is authenticated too. Since the journal is continuously written
  259. it is necessary to also add authentication information frequently to the
  260. journal so that in case of a powercut not too much data can't be authenticated.
  261. This is done by creating a continuous hash beginning from the commit start node
  262. over the previous reference nodes, the current reference node, and the bud
  263. nodes. From time to time whenever it is suitable authentication nodes are added
  264. between the bud nodes. This new node type contains a HMAC over the current state
  265. of the hash chain. That way a journal can be authenticated up to the last
  266. authentication node. The tail of the journal which may not have a authentication
  267. node cannot be authenticated and is skipped during journal replay.
  268. We get this picture for journal authentication::
  269. ,,,,,,,,
  270. ,......,...........................................
  271. ,. CS , hash1.----. hash2.----.
  272. ,. | , . |hmac . |hmac
  273. ,. v , . v . v
  274. ,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
  275. ,..|...,...........................................
  276. , | ,
  277. , | ,,,,,,,,,,,,,,,
  278. . | hash3,----.
  279. , | , |hmac
  280. , v , v
  281. , REF#1 -> bud -> bud,-> auth ...
  282. ,,,|,,,,,,,,,,,,,,,,,,
  283. v
  284. REF#2 -> ...
  285. |
  286. V
  287. ...
  288. Since the hash also includes the reference nodes an attacker cannot reorder or
  289. skip any journal heads for replay. An attacker can only remove bud nodes or
  290. reference nodes from the end of the journal, effectively rewinding the
  291. filesystem at maximum back to the last commit.
  292. The location of the log area is stored in the master node. Since the master
  293. node is authenticated with a HMAC as described above, it is not possible to
  294. tamper with that without detection. The size of the log area is specified when
  295. the filesystem is created using `mkfs.ubifs` and stored in the superblock node.
  296. To avoid tampering with this and other values stored there, a HMAC is added to
  297. the superblock struct. The superblock node is stored in LEB 0 and is only
  298. modified on feature flag or similar changes, but never on file changes.
  299. LPT Authentication
  300. ~~~~~~~~~~~~~~~~~~
  301. The location of the LPT root node on the flash is stored in the UBIFS master
  302. node. Since the LPT is written and read atomically on every commit, there is
  303. no need to authenticate individual nodes of the tree. It suffices to
  304. protect the integrity of the full LPT by a simple hash stored in the master
  305. node. Since the master node itself is authenticated, the LPTs authenticity can
  306. be verified by verifying the authenticity of the master node and comparing the
  307. LTP hash stored there with the hash computed from the read on-flash LPT.
  308. Key Management
  309. --------------
  310. For simplicity, UBIFS authentication uses a single key to compute the HMACs
  311. of superblock, master, commit start and reference nodes. This key has to be
  312. available on creation of the filesystem (`mkfs.ubifs`) to authenticate the
  313. superblock node. Further, it has to be available on mount of the filesystem
  314. to verify authenticated nodes and generate new HMACs for changes.
  315. UBIFS authentication is intended to operate side-by-side with UBIFS encryption
  316. (fscrypt) to provide confidentiality and authenticity. Since UBIFS encryption
  317. has a different approach of encryption policies per directory, there can be
  318. multiple fscrypt master keys and there might be folders without encryption.
  319. UBIFS authentication on the other hand has an all-or-nothing approach in the
  320. sense that it either authenticates everything of the filesystem or nothing.
  321. Because of this and because UBIFS authentication should also be usable without
  322. encryption, it does not share the same master key with fscrypt, but manages
  323. a dedicated authentication key.
  324. The API for providing the authentication key has yet to be defined, but the
  325. key can eg. be provided by userspace through a keyring similar to the way it
  326. is currently done in fscrypt. It should however be noted that the current
  327. fscrypt approach has shown its flaws and the userspace API will eventually
  328. change [FSCRYPT-POLICY2].
  329. Nevertheless, it will be possible for a user to provide a single passphrase
  330. or key in userspace that covers UBIFS authentication and encryption. This can
  331. be solved by the corresponding userspace tools which derive a second key for
  332. authentication in addition to the derived fscrypt master key used for
  333. encryption.
  334. To be able to check if the proper key is available on mount, the UBIFS
  335. superblock node will additionally store a hash of the authentication key. This
  336. approach is similar to the approach proposed for fscrypt encryption policy v2
  337. [FSCRYPT-POLICY2].
  338. Future Extensions
  339. =================
  340. In certain cases where a vendor wants to provide an authenticated filesystem
  341. image to customers, it should be possible to do so without sharing the secret
  342. UBIFS authentication key. Instead, in addition the each HMAC a digital
  343. signature could be stored where the vendor shares the public key alongside the
  344. filesystem image. In case this filesystem has to be modified afterwards,
  345. UBIFS can exchange all digital signatures with HMACs on first mount similar
  346. to the way the IMA/EVM subsystem deals with such situations. The HMAC key
  347. will then have to be provided beforehand in the normal way.
  348. References
  349. ==========
  350. [CRYPTSETUP2] https://www.saout.de/pipermail/dm-crypt/2017-November/005745.html
  351. [DMC-CBC-ATTACK] https://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/
  352. [DM-INTEGRITY] https://www.kernel.org/doc/Documentation/device-mapper/dm-integrity.rst
  353. [DM-VERITY] https://www.kernel.org/doc/Documentation/device-mapper/verity.rst
  354. [FSCRYPT-POLICY2] https://www.spinics.net/lists/linux-ext4/msg58710.html
  355. [UBIFS-WP] http://www.linux-mtd.infradead.org/doc/ubifs_whitepaper.pdf