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