449 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3.. UBIFS Authentication4.. sigma star gmbh5.. 20186 7============================8UBIFS Authentication Support9============================10 11Introduction12============13 14UBIFS utilizes the fscrypt framework to provide confidentiality for file15contents and file names. This prevents attacks where an attacker is able to16read contents of the filesystem on a single point in time. A classic example17is a lost smartphone where the attacker is unable to read personal data stored18on the device without the filesystem decryption key.19 20At the current state, UBIFS encryption however does not prevent attacks where21the attacker is able to modify the filesystem contents and the user uses the22device afterwards. In such a scenario an attacker can modify filesystem23contents arbitrarily without the user noticing. One example is to modify a24binary to perform a malicious action when executed [DMC-CBC-ATTACK]. Since25most of the filesystem metadata of UBIFS is stored in plain, this makes it26fairly easy to swap files and replace their contents.27 28Other full disk encryption systems like dm-crypt cover all filesystem metadata,29which makes such kinds of attacks more complicated, but not impossible.30Especially, if the attacker is given access to the device multiple points in31time. For dm-crypt and other filesystems that build upon the Linux block IO32layer, the dm-integrity or dm-verity subsystems [DM-INTEGRITY, DM-VERITY]33can be used to get full data authentication at the block layer.34These can also be combined with dm-crypt [CRYPTSETUP2].35 36This document describes an approach to get file contents _and_ full metadata37authentication for UBIFS. Since UBIFS uses fscrypt for file contents and file38name encryption, the authentication system could be tied into fscrypt such that39existing features like key derivation can be utilized. It should however also40be possible to use UBIFS authentication without using encryption.41 42 43MTD, UBI & UBIFS44----------------45 46On Linux, the MTD (Memory Technology Devices) subsystem provides a uniform47interface to access raw flash devices. One of the more prominent subsystems that48work on top of MTD is UBI (Unsorted Block Images). It provides volume management49for flash devices and is thus somewhat similar to LVM for block devices. In50addition, it deals with flash-specific wear-leveling and transparent I/O error51handling. UBI offers logical erase blocks (LEBs) to the layers on top of it52and maps them transparently to physical erase blocks (PEBs) on the flash.53 54UBIFS is a filesystem for raw flash which operates on top of UBI. Thus, wear55leveling and some flash specifics are left to UBI, while UBIFS focuses on56scalability, performance and recoverability.57 58::59 60 +------------+ +*******+ +-----------+ +-----+61 | | * UBIFS * | UBI-BLOCK | | ... |62 | JFFS/JFFS2 | +*******+ +-----------+ +-----+63 | | +-----------------------------+ +-----------+ +-----+64 | | | UBI | | MTD-BLOCK | | ... |65 +------------+ +-----------------------------+ +-----------+ +-----+66 +------------------------------------------------------------------+67 | MEMORY TECHNOLOGY DEVICES (MTD) |68 +------------------------------------------------------------------+69 +-----------------------------+ +--------------------------+ +-----+70 | NAND DRIVERS | | NOR DRIVERS | | ... |71 +-----------------------------+ +--------------------------+ +-----+72 73 Figure 1: Linux kernel subsystems for dealing with raw flash74 75 76 77Internally, UBIFS maintains multiple data structures which are persisted on78the flash:79 80- *Index*: an on-flash B+ tree where the leaf nodes contain filesystem data81- *Journal*: an additional data structure to collect FS changes before updating82 the on-flash index and reduce flash wear.83- *Tree Node Cache (TNC)*: an in-memory B+ tree that reflects the current FS84 state to avoid frequent flash reads. It is basically the in-memory85 representation of the index, but contains additional attributes.86- *LEB property tree (LPT)*: an on-flash B+ tree for free space accounting per87 UBI LEB.88 89In the remainder of this section we will cover the on-flash UBIFS data90structures in more detail. The TNC is of less importance here since it is never91persisted onto the flash directly. More details on UBIFS can also be found in92[UBIFS-WP].93 94 95UBIFS Index & Tree Node Cache96~~~~~~~~~~~~~~~~~~~~~~~~~~~~~97 98Basic on-flash UBIFS entities are called *nodes*. UBIFS knows different types99of nodes. Eg. data nodes (``struct ubifs_data_node``) which store chunks of file100contents or inode nodes (``struct ubifs_ino_node``) which represent VFS inodes.101Almost all types of nodes share a common header (``ubifs_ch``) containing basic102information like node type, node length, a sequence number, etc. (see103``fs/ubifs/ubifs-media.h`` in kernel source). Exceptions are entries of the LPT104and some less important node types like padding nodes which are used to pad105unusable content at the end of LEBs.106 107To avoid re-writing the whole B+ tree on every single change, it is implemented108as *wandering tree*, where only the changed nodes are re-written and previous109versions of them are obsoleted without erasing them right away. As a result,110the index is not stored in a single place on the flash, but *wanders* around111and there are obsolete parts on the flash as long as the LEB containing them is112not reused by UBIFS. To find the most recent version of the index, UBIFS stores113a special node called *master node* into UBI LEB 1 which always points to the114most recent root node of the UBIFS index. For recoverability, the master node115is additionally duplicated to LEB 2. Mounting UBIFS is thus a simple read of116LEB 1 and 2 to get the current master node and from there get the location of117the most recent on-flash index.118 119The TNC is the in-memory representation of the on-flash index. It contains some120additional runtime attributes per node which are not persisted. One of these is121a dirty-flag which marks nodes that have to be persisted the next time the122index is written onto the flash. The TNC acts as a write-back cache and all123modifications of the on-flash index are done through the TNC. Like other caches,124the TNC does not have to mirror the full index into memory, but reads parts of125it from flash whenever needed. A *commit* is the UBIFS operation of updating the126on-flash filesystem structures like the index. On every commit, the TNC nodes127marked as dirty are written to the flash to update the persisted index.128 129 130Journal131~~~~~~~132 133To avoid wearing out the flash, the index is only persisted (*committed*) when134certain conditions are met (eg. ``fsync(2)``). The journal is used to record135any changes (in form of inode nodes, data nodes etc.) between commits136of the index. During mount, the journal is read from the flash and replayed137onto the TNC (which will be created on-demand from the on-flash index).138 139UBIFS reserves a bunch of LEBs just for the journal called *log area*. The140amount of log area LEBs is configured on filesystem creation (using141``mkfs.ubifs``) and stored in the superblock node. The log area contains only142two types of nodes: *reference nodes* and *commit start nodes*. A commit start143node is written whenever an index commit is performed. Reference nodes are144written on every journal update. Each reference node points to the position of145other nodes (inode nodes, data nodes etc.) on the flash that are part of this146journal entry. These nodes are called *buds* and describe the actual filesystem147changes including their data.148 149The log area is maintained as a ring. Whenever the journal is almost full,150a commit is initiated. This also writes a commit start node so that during151mount, UBIFS will seek for the most recent commit start node and just replay152every reference node after that. Every reference node before the commit start153node will be ignored as they are already part of the on-flash index.154 155When writing a journal entry, UBIFS first ensures that enough space is156available to write the reference node and buds part of this entry. Then, the157reference node is written and afterwards the buds describing the file changes.158On replay, UBIFS will record every reference node and inspect the location of159the referenced LEBs to discover the buds. If these are corrupt or missing,160UBIFS will attempt to recover them by re-reading the LEB. This is however only161done for the last referenced LEB of the journal. Only this can become corrupt162because of a power cut. If the recovery fails, UBIFS will not mount. An error163for every other LEB will directly cause UBIFS to fail the mount operation.164 165::166 167 | ---- LOG AREA ---- | ---------- MAIN AREA ------------ |168 169 -----+------+-----+--------+---- ------+-----+-----+---------------170 \ | | | | / / | | | \171 / CS | REF | REF | | \ \ DENT | INO | INO | /172 \ | | | | / / | | | \173 ----+------+-----+--------+--- -------+-----+-----+----------------174 | | ^ ^175 | | | |176 +------------------------+ |177 | |178 +-------------------------------+179 180 181 Figure 2: UBIFS flash layout of log area with commit start nodes182 (CS) and reference nodes (REF) pointing to main area183 containing their buds184 185 186LEB Property Tree/Table187~~~~~~~~~~~~~~~~~~~~~~~188 189The LEB property tree is used to store per-LEB information. This includes the190LEB type and amount of free and *dirty* (old, obsolete content) space [1]_ on191the LEB. The type is important, because UBIFS never mixes index nodes with data192nodes on a single LEB and thus each LEB has a specific purpose. This again is193useful for free space calculations. See [UBIFS-WP] for more details.194 195The LEB property tree again is a B+ tree, but it is much smaller than the196index. Due to its smaller size it is always written as one chunk on every197commit. Thus, saving the LPT is an atomic operation.198 199 200.. [1] Since LEBs can only be appended and never overwritten, there is a201 difference between free space ie. the remaining space left on the LEB to be202 written to without erasing it and previously written content that is obsolete203 but can't be overwritten without erasing the full LEB.204 205 206UBIFS Authentication207====================208 209This chapter introduces UBIFS authentication which enables UBIFS to verify210the authenticity and integrity of metadata and file contents stored on flash.211 212 213Threat Model214------------215 216UBIFS authentication enables detection of offline data modification. While it217does not prevent it, it enables (trusted) code to check the integrity and218authenticity of on-flash file contents and filesystem metadata. This covers219attacks where file contents are swapped.220 221UBIFS authentication will not protect against rollback of full flash contents.222Ie. an attacker can still dump the flash and restore it at a later time without223detection. It will also not protect against partial rollback of individual224index commits. That means that an attacker is able to partially undo changes.225This is possible because UBIFS does not immediately overwrites obsolete226versions of the index tree or the journal, but instead marks them as obsolete227and garbage collection erases them at a later time. An attacker can use this by228erasing parts of the current tree and restoring old versions that are still on229the flash and have not yet been erased. This is possible, because every commit230will always write a new version of the index root node and the master node231without overwriting the previous version. This is further helped by the232wear-leveling operations of UBI which copies contents from one physical233eraseblock to another and does not atomically erase the first eraseblock.234 235UBIFS authentication does not cover attacks where an attacker is able to236execute code on the device after the authentication key was provided.237Additional measures like secure boot and trusted boot have to be taken to238ensure that only trusted code is executed on a device.239 240 241Authentication242--------------243 244To be able to fully trust data read from flash, all UBIFS data structures245stored on flash are authenticated. That is:246 247- The index which includes file contents, file metadata like extended248 attributes, file length etc.249- The journal which also contains file contents and metadata by recording changes250 to the filesystem251- The LPT which stores UBI LEB metadata which UBIFS uses for free space accounting252 253 254Index Authentication255~~~~~~~~~~~~~~~~~~~~256 257Through UBIFS' concept of a wandering tree, it already takes care of only258updating and persisting changed parts from leaf node up to the root node259of the full B+ tree. This enables us to augment the index nodes of the tree260with a hash over each node's child nodes. As a result, the index basically also261a Merkle tree. Since the leaf nodes of the index contain the actual filesystem262data, the hashes of their parent index nodes thus cover all the file contents263and file metadata. When a file changes, the UBIFS index is updated accordingly264from the leaf nodes up to the root node including the master node. This process265can be hooked to recompute the hash only for each changed node at the same time.266Whenever a file is read, UBIFS can verify the hashes from each leaf node up to267the root node to ensure the node's integrity.268 269To ensure the authenticity of the whole index, the UBIFS master node stores a270keyed hash (HMAC) over its own contents and a hash of the root node of the index271tree. As mentioned above, the master node is always written to the flash whenever272the index is persisted (ie. on index commit).273 274Using this approach only UBIFS index nodes and the master node are changed to275include a hash. All other types of nodes will remain unchanged. This reduces276the storage overhead which is precious for users of UBIFS (ie. embedded277devices).278 279::280 281 +---------------+282 | Master Node |283 | (hash) |284 +---------------+285 |286 v287 +-------------------+288 | Index Node #1 |289 | |290 | branch0 branchn |291 | (hash) (hash) |292 +-------------------+293 | ... | (fanout: 8)294 | |295 +-------+ +------+296 | |297 v v298 +-------------------+ +-------------------+299 | Index Node #2 | | Index Node #3 |300 | | | |301 | branch0 branchn | | branch0 branchn |302 | (hash) (hash) | | (hash) (hash) |303 +-------------------+ +-------------------+304 | ... | ... |305 v v v306 +-----------+ +----------+ +-----------+307 | Data Node | | INO Node | | DENT Node |308 +-----------+ +----------+ +-----------+309 310 311 Figure 3: Coverage areas of index node hash and master node HMAC312 313 314 315The most important part for robustness and power-cut safety is to atomically316persist the hash and file contents. Here the existing UBIFS logic for how317changed nodes are persisted is already designed for this purpose such that318UBIFS can safely recover if a power-cut occurs while persisting. Adding319hashes to index nodes does not change this since each hash will be persisted320atomically together with its respective node.321 322 323Journal Authentication324~~~~~~~~~~~~~~~~~~~~~~325 326The journal is authenticated too. Since the journal is continuously written327it is necessary to also add authentication information frequently to the328journal so that in case of a powercut not too much data can't be authenticated.329This is done by creating a continuous hash beginning from the commit start node330over the previous reference nodes, the current reference node, and the bud331nodes. From time to time whenever it is suitable authentication nodes are added332between the bud nodes. This new node type contains a HMAC over the current state333of the hash chain. That way a journal can be authenticated up to the last334authentication node. The tail of the journal which may not have a authentication335node cannot be authenticated and is skipped during journal replay.336 337We get this picture for journal authentication::338 339 ,,,,,,,,340 ,......,...........................................341 ,. CS , hash1.----. hash2.----.342 ,. | , . |hmac . |hmac343 ,. v , . v . v344 ,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...345 ,..|...,...........................................346 , | ,347 , | ,,,,,,,,,,,,,,,348 . | hash3,----.349 , | , |hmac350 , v , v351 , REF#1 -> bud -> bud,-> auth ...352 ,,,|,,,,,,,,,,,,,,,,,,353 v354 REF#2 -> ...355 |356 V357 ...358 359Since the hash also includes the reference nodes an attacker cannot reorder or360skip any journal heads for replay. An attacker can only remove bud nodes or361reference nodes from the end of the journal, effectively rewinding the362filesystem at maximum back to the last commit.363 364The location of the log area is stored in the master node. Since the master365node is authenticated with a HMAC as described above, it is not possible to366tamper with that without detection. The size of the log area is specified when367the filesystem is created using `mkfs.ubifs` and stored in the superblock node.368To avoid tampering with this and other values stored there, a HMAC is added to369the superblock struct. The superblock node is stored in LEB 0 and is only370modified on feature flag or similar changes, but never on file changes.371 372 373LPT Authentication374~~~~~~~~~~~~~~~~~~375 376The location of the LPT root node on the flash is stored in the UBIFS master377node. Since the LPT is written and read atomically on every commit, there is378no need to authenticate individual nodes of the tree. It suffices to379protect the integrity of the full LPT by a simple hash stored in the master380node. Since the master node itself is authenticated, the LPTs authenticity can381be verified by verifying the authenticity of the master node and comparing the382LTP hash stored there with the hash computed from the read on-flash LPT.383 384 385Key Management386--------------387 388For simplicity, UBIFS authentication uses a single key to compute the HMACs389of superblock, master, commit start and reference nodes. This key has to be390available on creation of the filesystem (`mkfs.ubifs`) to authenticate the391superblock node. Further, it has to be available on mount of the filesystem392to verify authenticated nodes and generate new HMACs for changes.393 394UBIFS authentication is intended to operate side-by-side with UBIFS encryption395(fscrypt) to provide confidentiality and authenticity. Since UBIFS encryption396has a different approach of encryption policies per directory, there can be397multiple fscrypt master keys and there might be folders without encryption.398UBIFS authentication on the other hand has an all-or-nothing approach in the399sense that it either authenticates everything of the filesystem or nothing.400Because of this and because UBIFS authentication should also be usable without401encryption, it does not share the same master key with fscrypt, but manages402a dedicated authentication key.403 404The API for providing the authentication key has yet to be defined, but the405key can eg. be provided by userspace through a keyring similar to the way it406is currently done in fscrypt. It should however be noted that the current407fscrypt approach has shown its flaws and the userspace API will eventually408change [FSCRYPT-POLICY2].409 410Nevertheless, it will be possible for a user to provide a single passphrase411or key in userspace that covers UBIFS authentication and encryption. This can412be solved by the corresponding userspace tools which derive a second key for413authentication in addition to the derived fscrypt master key used for414encryption.415 416To be able to check if the proper key is available on mount, the UBIFS417superblock node will additionally store a hash of the authentication key. This418approach is similar to the approach proposed for fscrypt encryption policy v2419[FSCRYPT-POLICY2].420 421 422Future Extensions423=================424 425In certain cases where a vendor wants to provide an authenticated filesystem426image to customers, it should be possible to do so without sharing the secret427UBIFS authentication key. Instead, in addition the each HMAC a digital428signature could be stored where the vendor shares the public key alongside the429filesystem image. In case this filesystem has to be modified afterwards,430UBIFS can exchange all digital signatures with HMACs on first mount similar431to the way the IMA/EVM subsystem deals with such situations. The HMAC key432will then have to be provided beforehand in the normal way.433 434 435References436==========437 438[CRYPTSETUP2] https://www.saout.de/pipermail/dm-crypt/2017-November/005745.html439 440[DMC-CBC-ATTACK] https://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/441 442[DM-INTEGRITY] https://www.kernel.org/doc/Documentation/device-mapper/dm-integrity.rst443 444[DM-VERITY] https://www.kernel.org/doc/Documentation/device-mapper/verity.rst445 446[FSCRYPT-POLICY2] https://www.spinics.net/lists/linux-ext4/msg58710.html447 448[UBIFS-WP] http://www.linux-mtd.infradead.org/doc/ubifs_whitepaper.pdf449