xref: /linux/Documentation/filesystems/ubifs-authentication.rst (revision b1cce98493a095925fb51be045ccf6e08edb4aa0)
1688f118eSMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
2688f118eSMauro Carvalho Chehab
309f4c750SMauro Carvalho Chehab.. UBIFS Authentication
409f4c750SMauro Carvalho Chehab.. sigma star gmbh
509f4c750SMauro Carvalho Chehab.. 2018
609f4c750SMauro Carvalho Chehab
77eec6756SJonathan Neuschäfer============================
87eec6756SJonathan NeuschäferUBIFS Authentication Support
97eec6756SJonathan Neuschäfer============================
107eec6756SJonathan Neuschäfer
1109f4c750SMauro Carvalho ChehabIntroduction
1209f4c750SMauro Carvalho Chehab============
13e453fa60SSascha Hauer
14e453fa60SSascha HauerUBIFS utilizes the fscrypt framework to provide confidentiality for file
15e453fa60SSascha Hauercontents and file names. This prevents attacks where an attacker is able to
16e453fa60SSascha Hauerread contents of the filesystem on a single point in time. A classic example
17e453fa60SSascha Haueris a lost smartphone where the attacker is unable to read personal data stored
18e453fa60SSascha Haueron the device without the filesystem decryption key.
19e453fa60SSascha Hauer
20e453fa60SSascha HauerAt the current state, UBIFS encryption however does not prevent attacks where
21e453fa60SSascha Hauerthe attacker is able to modify the filesystem contents and the user uses the
22e453fa60SSascha Hauerdevice afterwards. In such a scenario an attacker can modify filesystem
23e453fa60SSascha Hauercontents arbitrarily without the user noticing. One example is to modify a
24e453fa60SSascha Hauerbinary to perform a malicious action when executed [DMC-CBC-ATTACK]. Since
25e453fa60SSascha Hauermost of the filesystem metadata of UBIFS is stored in plain, this makes it
26e453fa60SSascha Hauerfairly easy to swap files and replace their contents.
27e453fa60SSascha Hauer
28e453fa60SSascha HauerOther full disk encryption systems like dm-crypt cover all filesystem metadata,
29e453fa60SSascha Hauerwhich makes such kinds of attacks more complicated, but not impossible.
30e453fa60SSascha HauerEspecially, if the attacker is given access to the device multiple points in
31e453fa60SSascha Hauertime. For dm-crypt and other filesystems that build upon the Linux block IO
32e453fa60SSascha Hauerlayer, the dm-integrity or dm-verity subsystems [DM-INTEGRITY, DM-VERITY]
33e453fa60SSascha Hauercan be used to get full data authentication at the block layer.
34e453fa60SSascha HauerThese can also be combined with dm-crypt [CRYPTSETUP2].
35e453fa60SSascha Hauer
36e453fa60SSascha HauerThis document describes an approach to get file contents _and_ full metadata
37e453fa60SSascha Hauerauthentication for UBIFS. Since UBIFS uses fscrypt for file contents and file
38e453fa60SSascha Hauername encryption, the authentication system could be tied into fscrypt such that
39e453fa60SSascha Hauerexisting features like key derivation can be utilized. It should however also
40e453fa60SSascha Hauerbe possible to use UBIFS authentication without using encryption.
41e453fa60SSascha Hauer
42e453fa60SSascha Hauer
4309f4c750SMauro Carvalho ChehabMTD, UBI & UBIFS
4409f4c750SMauro Carvalho Chehab----------------
45e453fa60SSascha Hauer
46e453fa60SSascha HauerOn Linux, the MTD (Memory Technology Devices) subsystem provides a uniform
47e453fa60SSascha Hauerinterface to access raw flash devices. One of the more prominent subsystems that
48e453fa60SSascha Hauerwork on top of MTD is UBI (Unsorted Block Images). It provides volume management
49e453fa60SSascha Hauerfor flash devices and is thus somewhat similar to LVM for block devices. In
50e453fa60SSascha Haueraddition, it deals with flash-specific wear-leveling and transparent I/O error
51e453fa60SSascha Hauerhandling. UBI offers logical erase blocks (LEBs) to the layers on top of it
52e453fa60SSascha Hauerand maps them transparently to physical erase blocks (PEBs) on the flash.
53e453fa60SSascha Hauer
54e453fa60SSascha HauerUBIFS is a filesystem for raw flash which operates on top of UBI. Thus, wear
55e453fa60SSascha Hauerleveling and some flash specifics are left to UBI, while UBIFS focuses on
56e453fa60SSascha Hauerscalability, performance and recoverability.
57e453fa60SSascha Hauer
5809f4c750SMauro Carvalho Chehab::
59e453fa60SSascha Hauer
60e453fa60SSascha Hauer	+------------+ +*******+ +-----------+ +-----+
61e453fa60SSascha Hauer	|            | * UBIFS * | UBI-BLOCK | | ... |
62e453fa60SSascha Hauer	| JFFS/JFFS2 | +*******+ +-----------+ +-----+
63e453fa60SSascha Hauer	|            | +-----------------------------+ +-----------+ +-----+
64e453fa60SSascha Hauer	|            | |              UBI            | | MTD-BLOCK | | ... |
65e453fa60SSascha Hauer	+------------+ +-----------------------------+ +-----------+ +-----+
66e453fa60SSascha Hauer	+------------------------------------------------------------------+
67e453fa60SSascha Hauer	|                  MEMORY TECHNOLOGY DEVICES (MTD)                 |
68e453fa60SSascha Hauer	+------------------------------------------------------------------+
69e453fa60SSascha Hauer	+-----------------------------+ +--------------------------+ +-----+
70e453fa60SSascha Hauer	|         NAND DRIVERS        | |        NOR DRIVERS       | | ... |
71e453fa60SSascha Hauer	+-----------------------------+ +--------------------------+ +-----+
72e453fa60SSascha Hauer
73e453fa60SSascha Hauer            Figure 1: Linux kernel subsystems for dealing with raw flash
74e453fa60SSascha Hauer
75e453fa60SSascha Hauer
76e453fa60SSascha Hauer
77e453fa60SSascha HauerInternally, UBIFS maintains multiple data structures which are persisted on
78e453fa60SSascha Hauerthe flash:
79e453fa60SSascha Hauer
80e453fa60SSascha Hauer- *Index*: an on-flash B+ tree where the leaf nodes contain filesystem data
81e453fa60SSascha Hauer- *Journal*: an additional data structure to collect FS changes before updating
82e453fa60SSascha Hauer  the on-flash index and reduce flash wear.
83e453fa60SSascha Hauer- *Tree Node Cache (TNC)*: an in-memory B+ tree that reflects the current FS
84e453fa60SSascha Hauer  state to avoid frequent flash reads. It is basically the in-memory
85e453fa60SSascha Hauer  representation of the index, but contains additional attributes.
86e453fa60SSascha Hauer- *LEB property tree (LPT)*: an on-flash B+ tree for free space accounting per
87e453fa60SSascha Hauer  UBI LEB.
88e453fa60SSascha Hauer
89e453fa60SSascha HauerIn the remainder of this section we will cover the on-flash UBIFS data
90e453fa60SSascha Hauerstructures in more detail. The TNC is of less importance here since it is never
91e453fa60SSascha Hauerpersisted onto the flash directly. More details on UBIFS can also be found in
92e453fa60SSascha Hauer[UBIFS-WP].
93e453fa60SSascha Hauer
94e453fa60SSascha Hauer
9509f4c750SMauro Carvalho ChehabUBIFS Index & Tree Node Cache
9609f4c750SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97e453fa60SSascha Hauer
98e453fa60SSascha HauerBasic on-flash UBIFS entities are called *nodes*. UBIFS knows different types
99688f118eSMauro Carvalho Chehabof nodes. Eg. data nodes (``struct ubifs_data_node``) which store chunks of file
100688f118eSMauro Carvalho Chehabcontents or inode nodes (``struct ubifs_ino_node``) which represent VFS inodes.
101688f118eSMauro Carvalho ChehabAlmost all types of nodes share a common header (``ubifs_ch``) containing basic
102e453fa60SSascha Hauerinformation like node type, node length, a sequence number, etc. (see
103688f118eSMauro Carvalho Chehab``fs/ubifs/ubifs-media.h`` in kernel source). Exceptions are entries of the LPT
104e453fa60SSascha Hauerand some less important node types like padding nodes which are used to pad
105e453fa60SSascha Hauerunusable content at the end of LEBs.
106e453fa60SSascha Hauer
107e453fa60SSascha HauerTo avoid re-writing the whole B+ tree on every single change, it is implemented
108e453fa60SSascha Haueras *wandering tree*, where only the changed nodes are re-written and previous
109e453fa60SSascha Hauerversions of them are obsoleted without erasing them right away. As a result,
110e453fa60SSascha Hauerthe index is not stored in a single place on the flash, but *wanders* around
111e453fa60SSascha Hauerand there are obsolete parts on the flash as long as the LEB containing them is
112e453fa60SSascha Hauernot reused by UBIFS. To find the most recent version of the index, UBIFS stores
113e453fa60SSascha Hauera special node called *master node* into UBI LEB 1 which always points to the
114e453fa60SSascha Hauermost recent root node of the UBIFS index. For recoverability, the master node
115e453fa60SSascha Haueris additionally duplicated to LEB 2. Mounting UBIFS is thus a simple read of
116e453fa60SSascha HauerLEB 1 and 2 to get the current master node and from there get the location of
117e453fa60SSascha Hauerthe most recent on-flash index.
118e453fa60SSascha Hauer
119e453fa60SSascha HauerThe TNC is the in-memory representation of the on-flash index. It contains some
120e453fa60SSascha Haueradditional runtime attributes per node which are not persisted. One of these is
121e453fa60SSascha Hauera dirty-flag which marks nodes that have to be persisted the next time the
122e453fa60SSascha Hauerindex is written onto the flash. The TNC acts as a write-back cache and all
123e453fa60SSascha Hauermodifications of the on-flash index are done through the TNC. Like other caches,
124e453fa60SSascha Hauerthe TNC does not have to mirror the full index into memory, but reads parts of
125e453fa60SSascha Hauerit from flash whenever needed. A *commit* is the UBIFS operation of updating the
126e453fa60SSascha Haueron-flash filesystem structures like the index. On every commit, the TNC nodes
127e453fa60SSascha Hauermarked as dirty are written to the flash to update the persisted index.
128e453fa60SSascha Hauer
129e453fa60SSascha Hauer
13009f4c750SMauro Carvalho ChehabJournal
13109f4c750SMauro Carvalho Chehab~~~~~~~
132e453fa60SSascha Hauer
133d56b699dSBjorn HelgaasTo avoid wearing out the flash, the index is only persisted (*committed*) when
13409f4c750SMauro Carvalho Chehabcertain conditions are met (eg. ``fsync(2)``). The journal is used to record
135e453fa60SSascha Hauerany changes (in form of inode nodes, data nodes etc.) between commits
136e453fa60SSascha Hauerof the index. During mount, the journal is read from the flash and replayed
137e453fa60SSascha Haueronto the TNC (which will be created on-demand from the on-flash index).
138e453fa60SSascha Hauer
139e453fa60SSascha HauerUBIFS reserves a bunch of LEBs just for the journal called *log area*. The
140e453fa60SSascha Haueramount of log area LEBs is configured on filesystem creation (using
14109f4c750SMauro Carvalho Chehab``mkfs.ubifs``) and stored in the superblock node. The log area contains only
142e453fa60SSascha Hauertwo types of nodes: *reference nodes* and *commit start nodes*. A commit start
143e453fa60SSascha Hauernode is written whenever an index commit is performed. Reference nodes are
144e453fa60SSascha Hauerwritten on every journal update. Each reference node points to the position of
145e453fa60SSascha Hauerother nodes (inode nodes, data nodes etc.) on the flash that are part of this
146e453fa60SSascha Hauerjournal entry. These nodes are called *buds* and describe the actual filesystem
147e453fa60SSascha Hauerchanges including their data.
148e453fa60SSascha Hauer
149e453fa60SSascha HauerThe log area is maintained as a ring. Whenever the journal is almost full,
150e453fa60SSascha Hauera commit is initiated. This also writes a commit start node so that during
151e453fa60SSascha Hauermount, UBIFS will seek for the most recent commit start node and just replay
152e453fa60SSascha Hauerevery reference node after that. Every reference node before the commit start
153e453fa60SSascha Hauernode will be ignored as they are already part of the on-flash index.
154e453fa60SSascha Hauer
155e453fa60SSascha HauerWhen writing a journal entry, UBIFS first ensures that enough space is
156e453fa60SSascha Haueravailable to write the reference node and buds part of this entry. Then, the
157e453fa60SSascha Hauerreference node is written and afterwards the buds describing the file changes.
158e453fa60SSascha HauerOn replay, UBIFS will record every reference node and inspect the location of
159e453fa60SSascha Hauerthe referenced LEBs to discover the buds. If these are corrupt or missing,
160e453fa60SSascha HauerUBIFS will attempt to recover them by re-reading the LEB. This is however only
161e453fa60SSascha Hauerdone for the last referenced LEB of the journal. Only this can become corrupt
162e453fa60SSascha Hauerbecause of a power cut. If the recovery fails, UBIFS will not mount. An error
163e453fa60SSascha Hauerfor every other LEB will directly cause UBIFS to fail the mount operation.
164e453fa60SSascha Hauer
16509f4c750SMauro Carvalho Chehab::
166e453fa60SSascha Hauer
167e453fa60SSascha Hauer       | ----    LOG AREA     ---- | ----------    MAIN AREA    ------------ |
168e453fa60SSascha Hauer
169e453fa60SSascha Hauer        -----+------+-----+--------+----   ------+-----+-----+---------------
170e453fa60SSascha Hauer        \    |      |     |        |   /  /      |     |     |               \
171e453fa60SSascha Hauer        / CS |  REF | REF |        |   \  \ DENT | INO | INO |               /
172e453fa60SSascha Hauer        \    |      |     |        |   /  /      |     |     |               \
173e453fa60SSascha Hauer         ----+------+-----+--------+---   -------+-----+-----+----------------
174e453fa60SSascha Hauer                 |     |                  ^            ^
175e453fa60SSascha Hauer                 |     |                  |            |
176e453fa60SSascha Hauer                 +------------------------+            |
177e453fa60SSascha Hauer                       |                               |
178e453fa60SSascha Hauer                       +-------------------------------+
179e453fa60SSascha Hauer
180e453fa60SSascha Hauer
181e453fa60SSascha Hauer                Figure 2: UBIFS flash layout of log area with commit start nodes
182e453fa60SSascha Hauer                          (CS) and reference nodes (REF) pointing to main area
183e453fa60SSascha Hauer                          containing their buds
184e453fa60SSascha Hauer
185e453fa60SSascha Hauer
18609f4c750SMauro Carvalho ChehabLEB Property Tree/Table
18709f4c750SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~
188e453fa60SSascha Hauer
189e453fa60SSascha HauerThe LEB property tree is used to store per-LEB information. This includes the
19009f4c750SMauro Carvalho ChehabLEB type and amount of free and *dirty* (old, obsolete content) space [1]_ on
191e453fa60SSascha Hauerthe LEB. The type is important, because UBIFS never mixes index nodes with data
192e453fa60SSascha Hauernodes on a single LEB and thus each LEB has a specific purpose. This again is
193e453fa60SSascha Haueruseful for free space calculations. See [UBIFS-WP] for more details.
194e453fa60SSascha Hauer
195e453fa60SSascha HauerThe LEB property tree again is a B+ tree, but it is much smaller than the
196e453fa60SSascha Hauerindex. Due to its smaller size it is always written as one chunk on every
197e453fa60SSascha Hauercommit. Thus, saving the LPT is an atomic operation.
198e453fa60SSascha Hauer
199e453fa60SSascha Hauer
20009f4c750SMauro Carvalho Chehab.. [1] Since LEBs can only be appended and never overwritten, there is a
201e453fa60SSascha Hauer   difference between free space ie. the remaining space left on the LEB to be
202e453fa60SSascha Hauer   written to without erasing it and previously written content that is obsolete
203e453fa60SSascha Hauer   but can't be overwritten without erasing the full LEB.
204e453fa60SSascha Hauer
205e453fa60SSascha Hauer
20609f4c750SMauro Carvalho ChehabUBIFS Authentication
20709f4c750SMauro Carvalho Chehab====================
208e453fa60SSascha Hauer
209e453fa60SSascha HauerThis chapter introduces UBIFS authentication which enables UBIFS to verify
210e453fa60SSascha Hauerthe authenticity and integrity of metadata and file contents stored on flash.
211e453fa60SSascha Hauer
212e453fa60SSascha Hauer
21309f4c750SMauro Carvalho ChehabThreat Model
21409f4c750SMauro Carvalho Chehab------------
215e453fa60SSascha Hauer
216e453fa60SSascha HauerUBIFS authentication enables detection of offline data modification. While it
217e453fa60SSascha Hauerdoes not prevent it, it enables (trusted) code to check the integrity and
218e453fa60SSascha Hauerauthenticity of on-flash file contents and filesystem metadata. This covers
219e453fa60SSascha Hauerattacks where file contents are swapped.
220e453fa60SSascha Hauer
221e453fa60SSascha HauerUBIFS authentication will not protect against rollback of full flash contents.
222e453fa60SSascha HauerIe. an attacker can still dump the flash and restore it at a later time without
223e453fa60SSascha Hauerdetection. It will also not protect against partial rollback of individual
224e453fa60SSascha Hauerindex commits. That means that an attacker is able to partially undo changes.
225e453fa60SSascha HauerThis is possible because UBIFS does not immediately overwrites obsolete
226e453fa60SSascha Hauerversions of the index tree or the journal, but instead marks them as obsolete
227e453fa60SSascha Hauerand garbage collection erases them at a later time. An attacker can use this by
228e453fa60SSascha Hauererasing parts of the current tree and restoring old versions that are still on
229e453fa60SSascha Hauerthe flash and have not yet been erased. This is possible, because every commit
230e453fa60SSascha Hauerwill always write a new version of the index root node and the master node
231e453fa60SSascha Hauerwithout overwriting the previous version. This is further helped by the
232e453fa60SSascha Hauerwear-leveling operations of UBI which copies contents from one physical
233e453fa60SSascha Hauereraseblock to another and does not atomically erase the first eraseblock.
234e453fa60SSascha Hauer
235e453fa60SSascha HauerUBIFS authentication does not cover attacks where an attacker is able to
236e453fa60SSascha Hauerexecute code on the device after the authentication key was provided.
237e453fa60SSascha HauerAdditional measures like secure boot and trusted boot have to be taken to
238e453fa60SSascha Hauerensure that only trusted code is executed on a device.
239e453fa60SSascha Hauer
240e453fa60SSascha Hauer
24109f4c750SMauro Carvalho ChehabAuthentication
24209f4c750SMauro Carvalho Chehab--------------
243e453fa60SSascha Hauer
244e453fa60SSascha HauerTo be able to fully trust data read from flash, all UBIFS data structures
245e453fa60SSascha Hauerstored on flash are authenticated. That is:
246e453fa60SSascha Hauer
247e453fa60SSascha Hauer- The index which includes file contents, file metadata like extended
248e453fa60SSascha Hauer  attributes, file length etc.
249e453fa60SSascha Hauer- The journal which also contains file contents and metadata by recording changes
250e453fa60SSascha Hauer  to the filesystem
251e453fa60SSascha Hauer- The LPT which stores UBI LEB metadata which UBIFS uses for free space accounting
252e453fa60SSascha Hauer
253e453fa60SSascha Hauer
25409f4c750SMauro Carvalho ChehabIndex Authentication
25509f4c750SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~
256e453fa60SSascha Hauer
257e453fa60SSascha HauerThrough UBIFS' concept of a wandering tree, it already takes care of only
258e453fa60SSascha Hauerupdating and persisting changed parts from leaf node up to the root node
259e453fa60SSascha Hauerof the full B+ tree. This enables us to augment the index nodes of the tree
260e453fa60SSascha Hauerwith a hash over each node's child nodes. As a result, the index basically also
261e453fa60SSascha Hauera Merkle tree. Since the leaf nodes of the index contain the actual filesystem
262e453fa60SSascha Hauerdata, the hashes of their parent index nodes thus cover all the file contents
263e453fa60SSascha Hauerand file metadata. When a file changes, the UBIFS index is updated accordingly
264e453fa60SSascha Hauerfrom the leaf nodes up to the root node including the master node. This process
265e453fa60SSascha Hauercan be hooked to recompute the hash only for each changed node at the same time.
266e453fa60SSascha HauerWhenever a file is read, UBIFS can verify the hashes from each leaf node up to
267e453fa60SSascha Hauerthe root node to ensure the node's integrity.
268e453fa60SSascha Hauer
269e453fa60SSascha HauerTo ensure the authenticity of the whole index, the UBIFS master node stores a
270e453fa60SSascha Hauerkeyed hash (HMAC) over its own contents and a hash of the root node of the index
271e453fa60SSascha Hauertree. As mentioned above, the master node is always written to the flash whenever
272e453fa60SSascha Hauerthe index is persisted (ie. on index commit).
273e453fa60SSascha Hauer
274e453fa60SSascha HauerUsing this approach only UBIFS index nodes and the master node are changed to
275e453fa60SSascha Hauerinclude a hash. All other types of nodes will remain unchanged. This reduces
276e453fa60SSascha Hauerthe storage overhead which is precious for users of UBIFS (ie. embedded
277e453fa60SSascha Hauerdevices).
278e453fa60SSascha Hauer
27909f4c750SMauro Carvalho Chehab::
280e453fa60SSascha Hauer
281e453fa60SSascha Hauer                             +---------------+
282e453fa60SSascha Hauer                             |  Master Node  |
283e453fa60SSascha Hauer                             |    (hash)     |
284e453fa60SSascha Hauer                             +---------------+
285e453fa60SSascha Hauer                                     |
286e453fa60SSascha Hauer                                     v
287e453fa60SSascha Hauer                            +-------------------+
288e453fa60SSascha Hauer                            |  Index Node #1    |
289e453fa60SSascha Hauer                            |                   |
290e453fa60SSascha Hauer                            | branch0   branchn |
291e453fa60SSascha Hauer                            | (hash)    (hash)  |
292e453fa60SSascha Hauer                            +-------------------+
293e453fa60SSascha Hauer                               |    ...   |  (fanout: 8)
294e453fa60SSascha Hauer                               |          |
295e453fa60SSascha Hauer                       +-------+          +------+
296e453fa60SSascha Hauer                       |                         |
297e453fa60SSascha Hauer                       v                         v
298e453fa60SSascha Hauer            +-------------------+       +-------------------+
299e453fa60SSascha Hauer            |  Index Node #2    |       |  Index Node #3    |
300e453fa60SSascha Hauer            |                   |       |                   |
301e453fa60SSascha Hauer            | branch0   branchn |       | branch0   branchn |
302e453fa60SSascha Hauer            | (hash)    (hash)  |       | (hash)    (hash)  |
303e453fa60SSascha Hauer            +-------------------+       +-------------------+
304e453fa60SSascha Hauer                 |   ...                     |   ...   |
305e453fa60SSascha Hauer                 v                           v         v
306e453fa60SSascha Hauer               +-----------+         +----------+  +-----------+
307e453fa60SSascha Hauer               | Data Node |         | INO Node |  | DENT Node |
308e453fa60SSascha Hauer               +-----------+         +----------+  +-----------+
309e453fa60SSascha Hauer
310e453fa60SSascha Hauer
311e453fa60SSascha Hauer           Figure 3: Coverage areas of index node hash and master node HMAC
312e453fa60SSascha Hauer
313e453fa60SSascha Hauer
314e453fa60SSascha Hauer
315e453fa60SSascha HauerThe most important part for robustness and power-cut safety is to atomically
316e453fa60SSascha Hauerpersist the hash and file contents. Here the existing UBIFS logic for how
317e453fa60SSascha Hauerchanged nodes are persisted is already designed for this purpose such that
318e453fa60SSascha HauerUBIFS can safely recover if a power-cut occurs while persisting. Adding
319e453fa60SSascha Hauerhashes to index nodes does not change this since each hash will be persisted
320e453fa60SSascha Haueratomically together with its respective node.
321e453fa60SSascha Hauer
322e453fa60SSascha Hauer
32309f4c750SMauro Carvalho ChehabJournal Authentication
32409f4c750SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~
325e453fa60SSascha Hauer
326e453fa60SSascha HauerThe journal is authenticated too. Since the journal is continuously written
327e453fa60SSascha Hauerit is necessary to also add authentication information frequently to the
328e453fa60SSascha Hauerjournal so that in case of a powercut not too much data can't be authenticated.
329e453fa60SSascha HauerThis is done by creating a continuous hash beginning from the commit start node
330e453fa60SSascha Hauerover the previous reference nodes, the current reference node, and the bud
331e453fa60SSascha Hauernodes. From time to time whenever it is suitable authentication nodes are added
332e453fa60SSascha Hauerbetween the bud nodes. This new node type contains a HMAC over the current state
333e453fa60SSascha Hauerof the hash chain. That way a journal can be authenticated up to the last
334e453fa60SSascha Hauerauthentication node. The tail of the journal which may not have a authentication
335e453fa60SSascha Hauernode cannot be authenticated and is skipped during journal replay.
336e453fa60SSascha Hauer
33709f4c750SMauro Carvalho ChehabWe get this picture for journal authentication::
338e453fa60SSascha Hauer
339e453fa60SSascha Hauer    ,,,,,,,,
340e453fa60SSascha Hauer    ,......,...........................................
341e453fa60SSascha Hauer    ,. CS  ,               hash1.----.           hash2.----.
342e453fa60SSascha Hauer    ,.  |  ,                    .    |hmac            .    |hmac
343e453fa60SSascha Hauer    ,.  v  ,                    .    v                .    v
344e453fa60SSascha Hauer    ,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
345e453fa60SSascha Hauer    ,..|...,...........................................
346e453fa60SSascha Hauer    ,  |   ,
347e453fa60SSascha Hauer    ,  |   ,,,,,,,,,,,,,,,
348e453fa60SSascha Hauer    .  |            hash3,----.
349e453fa60SSascha Hauer    ,  |                 ,    |hmac
350e453fa60SSascha Hauer    ,  v                 ,    v
351e453fa60SSascha Hauer    , REF#1 -> bud -> bud,-> auth ...
352e453fa60SSascha Hauer    ,,,|,,,,,,,,,,,,,,,,,,
353e453fa60SSascha Hauer       v
354e453fa60SSascha Hauer      REF#2 -> ...
355e453fa60SSascha Hauer       |
356e453fa60SSascha Hauer       V
357e453fa60SSascha Hauer      ...
358e453fa60SSascha Hauer
359e453fa60SSascha HauerSince the hash also includes the reference nodes an attacker cannot reorder or
360e453fa60SSascha Hauerskip any journal heads for replay. An attacker can only remove bud nodes or
361e453fa60SSascha Hauerreference nodes from the end of the journal, effectively rewinding the
362e453fa60SSascha Hauerfilesystem at maximum back to the last commit.
363e453fa60SSascha Hauer
364e453fa60SSascha HauerThe location of the log area is stored in the master node. Since the master
365e453fa60SSascha Hauernode is authenticated with a HMAC as described above, it is not possible to
366e453fa60SSascha Hauertamper with that without detection. The size of the log area is specified when
367e453fa60SSascha Hauerthe filesystem is created using `mkfs.ubifs` and stored in the superblock node.
368e453fa60SSascha HauerTo avoid tampering with this and other values stored there, a HMAC is added to
369e453fa60SSascha Hauerthe superblock struct. The superblock node is stored in LEB 0 and is only
370e453fa60SSascha Hauermodified on feature flag or similar changes, but never on file changes.
371e453fa60SSascha Hauer
372e453fa60SSascha Hauer
37309f4c750SMauro Carvalho ChehabLPT Authentication
37409f4c750SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~
375e453fa60SSascha Hauer
376e453fa60SSascha HauerThe location of the LPT root node on the flash is stored in the UBIFS master
377e453fa60SSascha Hauernode. Since the LPT is written and read atomically on every commit, there is
378e453fa60SSascha Hauerno need to authenticate individual nodes of the tree. It suffices to
379e453fa60SSascha Hauerprotect the integrity of the full LPT by a simple hash stored in the master
380e453fa60SSascha Hauernode. Since the master node itself is authenticated, the LPTs authenticity can
381e453fa60SSascha Hauerbe verified by verifying the authenticity of the master node and comparing the
382e453fa60SSascha HauerLTP hash stored there with the hash computed from the read on-flash LPT.
383e453fa60SSascha Hauer
384e453fa60SSascha Hauer
38509f4c750SMauro Carvalho ChehabKey Management
38609f4c750SMauro Carvalho Chehab--------------
387e453fa60SSascha Hauer
388e453fa60SSascha HauerFor simplicity, UBIFS authentication uses a single key to compute the HMACs
389e453fa60SSascha Hauerof superblock, master, commit start and reference nodes. This key has to be
390e453fa60SSascha Haueravailable on creation of the filesystem (`mkfs.ubifs`) to authenticate the
391e453fa60SSascha Hauersuperblock node. Further, it has to be available on mount of the filesystem
392e453fa60SSascha Hauerto verify authenticated nodes and generate new HMACs for changes.
393e453fa60SSascha Hauer
394e453fa60SSascha HauerUBIFS authentication is intended to operate side-by-side with UBIFS encryption
395e453fa60SSascha Hauer(fscrypt) to provide confidentiality and authenticity. Since UBIFS encryption
396e453fa60SSascha Hauerhas a different approach of encryption policies per directory, there can be
397e453fa60SSascha Hauermultiple fscrypt master keys and there might be folders without encryption.
398e453fa60SSascha HauerUBIFS authentication on the other hand has an all-or-nothing approach in the
399e453fa60SSascha Hauersense that it either authenticates everything of the filesystem or nothing.
400e453fa60SSascha HauerBecause of this and because UBIFS authentication should also be usable without
401e453fa60SSascha Hauerencryption, it does not share the same master key with fscrypt, but manages
402e453fa60SSascha Hauera dedicated authentication key.
403e453fa60SSascha Hauer
404e453fa60SSascha HauerThe API for providing the authentication key has yet to be defined, but the
405e453fa60SSascha Hauerkey can eg. be provided by userspace through a keyring similar to the way it
406e453fa60SSascha Haueris currently done in fscrypt. It should however be noted that the current
407e453fa60SSascha Hauerfscrypt approach has shown its flaws and the userspace API will eventually
408e453fa60SSascha Hauerchange [FSCRYPT-POLICY2].
409e453fa60SSascha Hauer
410e453fa60SSascha HauerNevertheless, it will be possible for a user to provide a single passphrase
411e453fa60SSascha Haueror key in userspace that covers UBIFS authentication and encryption. This can
412e453fa60SSascha Hauerbe solved by the corresponding userspace tools which derive a second key for
413e453fa60SSascha Hauerauthentication in addition to the derived fscrypt master key used for
414e453fa60SSascha Hauerencryption.
415e453fa60SSascha Hauer
416e453fa60SSascha HauerTo be able to check if the proper key is available on mount, the UBIFS
417e453fa60SSascha Hauersuperblock node will additionally store a hash of the authentication key. This
418e453fa60SSascha Hauerapproach is similar to the approach proposed for fscrypt encryption policy v2
419e453fa60SSascha Hauer[FSCRYPT-POLICY2].
420e453fa60SSascha Hauer
421e453fa60SSascha Hauer
42209f4c750SMauro Carvalho ChehabFuture Extensions
42309f4c750SMauro Carvalho Chehab=================
424e453fa60SSascha Hauer
425e453fa60SSascha HauerIn certain cases where a vendor wants to provide an authenticated filesystem
426e453fa60SSascha Hauerimage to customers, it should be possible to do so without sharing the secret
427e453fa60SSascha HauerUBIFS authentication key. Instead, in addition the each HMAC a digital
428e453fa60SSascha Hauersignature could be stored where the vendor shares the public key alongside the
429e453fa60SSascha Hauerfilesystem image. In case this filesystem has to be modified afterwards,
430e453fa60SSascha HauerUBIFS can exchange all digital signatures with HMACs on first mount similar
431e453fa60SSascha Hauerto the way the IMA/EVM subsystem deals with such situations. The HMAC key
432e453fa60SSascha Hauerwill then have to be provided beforehand in the normal way.
433e453fa60SSascha Hauer
434e453fa60SSascha Hauer
43509f4c750SMauro Carvalho ChehabReferences
43609f4c750SMauro Carvalho Chehab==========
437e453fa60SSascha Hauer
438c69f22f2SAlexander A. Klimov[CRYPTSETUP2]        https://www.saout.de/pipermail/dm-crypt/2017-November/005745.html
439e453fa60SSascha Hauer
440c69f22f2SAlexander A. Klimov[DMC-CBC-ATTACK]     https://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/
441e453fa60SSascha Hauer
442f0ba4377SMauro Carvalho Chehab[DM-INTEGRITY]       https://www.kernel.org/doc/Documentation/device-mapper/dm-integrity.rst
443e453fa60SSascha Hauer
444f0ba4377SMauro Carvalho Chehab[DM-VERITY]          https://www.kernel.org/doc/Documentation/device-mapper/verity.rst
445e453fa60SSascha Hauer
446*5194439dSBagas Sanjaya[FSCRYPT-POLICY2]    https://lore.kernel.org/r/20171023214058.128121-1-ebiggers3@gmail.com/
447e453fa60SSascha Hauer
448e453fa60SSascha Hauer[UBIFS-WP]           http://www.linux-mtd.infradead.org/doc/ubifs_whitepaper.pdf
449