aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/filesystems/ubifs-authentication.md426
-rw-r--r--Documentation/filesystems/ubifs.txt7
-rw-r--r--drivers/mtd/ubi/attach.c1
-rw-r--r--drivers/mtd/ubi/build.c2
-rw-r--r--fs/ubifs/Kconfig11
-rw-r--r--fs/ubifs/Makefile1
-rw-r--r--fs/ubifs/auth.c502
-rw-r--r--fs/ubifs/debug.c6
-rw-r--r--fs/ubifs/gc.c49
-rw-r--r--fs/ubifs/io.c110
-rw-r--r--fs/ubifs/journal.c289
-rw-r--r--fs/ubifs/log.c24
-rw-r--r--fs/ubifs/lpt.c184
-rw-r--r--fs/ubifs/lpt_commit.c44
-rw-r--r--fs/ubifs/master.c64
-rw-r--r--fs/ubifs/misc.h5
-rw-r--r--fs/ubifs/recovery.c120
-rw-r--r--fs/ubifs/replay.c177
-rw-r--r--fs/ubifs/sb.c209
-rw-r--r--fs/ubifs/super.c91
-rw-r--r--fs/ubifs/tnc.c36
-rw-r--r--fs/ubifs/tnc_commit.c27
-rw-r--r--fs/ubifs/tnc_misc.c26
-rw-r--r--fs/ubifs/ubifs-media.h46
-rw-r--r--fs/ubifs/ubifs.h253
25 files changed, 2418 insertions, 292 deletions
diff --git a/Documentation/filesystems/ubifs-authentication.md b/Documentation/filesystems/ubifs-authentication.md
new file mode 100644
index 000000000000..028b3e2e25f9
--- /dev/null
+++ b/Documentation/filesystems/ubifs-authentication.md
@@ -0,0 +1,426 @@
1% UBIFS Authentication
2% sigma star gmbh
3% 2018
4
5# Introduction
6
7UBIFS utilizes the fscrypt framework to provide confidentiality for file
8contents and file names. This prevents attacks where an attacker is able to
9read contents of the filesystem on a single point in time. A classic example
10is a lost smartphone where the attacker is unable to read personal data stored
11on the device without the filesystem decryption key.
12
13At the current state, UBIFS encryption however does not prevent attacks where
14the attacker is able to modify the filesystem contents and the user uses the
15device afterwards. In such a scenario an attacker can modify filesystem
16contents arbitrarily without the user noticing. One example is to modify a
17binary to perform a malicious action when executed [DMC-CBC-ATTACK]. Since
18most of the filesystem metadata of UBIFS is stored in plain, this makes it
19fairly easy to swap files and replace their contents.
20
21Other full disk encryption systems like dm-crypt cover all filesystem metadata,
22which makes such kinds of attacks more complicated, but not impossible.
23Especially, if the attacker is given access to the device multiple points in
24time. For dm-crypt and other filesystems that build upon the Linux block IO
25layer, the dm-integrity or dm-verity subsystems [DM-INTEGRITY, DM-VERITY]
26can be used to get full data authentication at the block layer.
27These can also be combined with dm-crypt [CRYPTSETUP2].
28
29This document describes an approach to get file contents _and_ full metadata
30authentication for UBIFS. Since UBIFS uses fscrypt for file contents and file
31name encryption, the authentication system could be tied into fscrypt such that
32existing features like key derivation can be utilized. It should however also
33be possible to use UBIFS authentication without using encryption.
34
35
36## MTD, UBI & UBIFS
37
38On Linux, the MTD (Memory Technology Devices) subsystem provides a uniform
39interface to access raw flash devices. One of the more prominent subsystems that
40work on top of MTD is UBI (Unsorted Block Images). It provides volume management
41for flash devices and is thus somewhat similar to LVM for block devices. In
42addition, it deals with flash-specific wear-leveling and transparent I/O error
43handling. UBI offers logical erase blocks (LEBs) to the layers on top of it
44and maps them transparently to physical erase blocks (PEBs) on the flash.
45
46UBIFS is a filesystem for raw flash which operates on top of UBI. Thus, wear
47leveling and some flash specifics are left to UBI, while UBIFS focuses on
48scalability, performance and recoverability.
49
50
51
52 +------------+ +*******+ +-----------+ +-----+
53 | | * UBIFS * | UBI-BLOCK | | ... |
54 | JFFS/JFFS2 | +*******+ +-----------+ +-----+
55 | | +-----------------------------+ +-----------+ +-----+
56 | | | UBI | | MTD-BLOCK | | ... |
57 +------------+ +-----------------------------+ +-----------+ +-----+
58 +------------------------------------------------------------------+
59 | MEMORY TECHNOLOGY DEVICES (MTD) |
60 +------------------------------------------------------------------+
61 +-----------------------------+ +--------------------------+ +-----+
62 | NAND DRIVERS | | NOR DRIVERS | | ... |
63 +-----------------------------+ +--------------------------+ +-----+
64
65 Figure 1: Linux kernel subsystems for dealing with raw flash
66
67
68
69Internally, UBIFS maintains multiple data structures which are persisted on
70the flash:
71
72- *Index*: an on-flash B+ tree where the leaf nodes contain filesystem data
73- *Journal*: an additional data structure to collect FS changes before updating
74 the on-flash index and reduce flash wear.
75- *Tree Node Cache (TNC)*: an in-memory B+ tree that reflects the current FS
76 state to avoid frequent flash reads. It is basically the in-memory
77 representation of the index, but contains additional attributes.
78- *LEB property tree (LPT)*: an on-flash B+ tree for free space accounting per
79 UBI LEB.
80
81In the remainder of this section we will cover the on-flash UBIFS data
82structures in more detail. The TNC is of less importance here since it is never
83persisted onto the flash directly. More details on UBIFS can also be found in
84[UBIFS-WP].
85
86
87### UBIFS Index & Tree Node Cache
88
89Basic on-flash UBIFS entities are called *nodes*. UBIFS knows different types
90of nodes. Eg. data nodes (`struct ubifs_data_node`) which store chunks of file
91contents or inode nodes (`struct ubifs_ino_node`) which represent VFS inodes.
92Almost all types of nodes share a common header (`ubifs_ch`) containing basic
93information like node type, node length, a sequence number, etc. (see
94`fs/ubifs/ubifs-media.h`in kernel source). Exceptions are entries of the LPT
95and some less important node types like padding nodes which are used to pad
96unusable content at the end of LEBs.
97
98To avoid re-writing the whole B+ tree on every single change, it is implemented
99as *wandering tree*, where only the changed nodes are re-written and previous
100versions of them are obsoleted without erasing them right away. As a result,
101the index is not stored in a single place on the flash, but *wanders* around
102and there are obsolete parts on the flash as long as the LEB containing them is
103not reused by UBIFS. To find the most recent version of the index, UBIFS stores
104a special node called *master node* into UBI LEB 1 which always points to the
105most recent root node of the UBIFS index. For recoverability, the master node
106is additionally duplicated to LEB 2. Mounting UBIFS is thus a simple read of
107LEB 1 and 2 to get the current master node and from there get the location of
108the most recent on-flash index.
109
110The TNC is the in-memory representation of the on-flash index. It contains some
111additional runtime attributes per node which are not persisted. One of these is
112a dirty-flag which marks nodes that have to be persisted the next time the
113index is written onto the flash. The TNC acts as a write-back cache and all
114modifications of the on-flash index are done through the TNC. Like other caches,
115the TNC does not have to mirror the full index into memory, but reads parts of
116it from flash whenever needed. A *commit* is the UBIFS operation of updating the
117on-flash filesystem structures like the index. On every commit, the TNC nodes
118marked as dirty are written to the flash to update the persisted index.
119
120
121### Journal
122
123To avoid wearing out the flash, the index is only persisted (*commited*) when
124certain conditions are met (eg. `fsync(2)`). The journal is used to record
125any changes (in form of inode nodes, data nodes etc.) between commits
126of the index. During mount, the journal is read from the flash and replayed
127onto the TNC (which will be created on-demand from the on-flash index).
128
129UBIFS reserves a bunch of LEBs just for the journal called *log area*. The
130amount of log area LEBs is configured on filesystem creation (using
131`mkfs.ubifs`) and stored in the superblock node. The log area contains only
132two types of nodes: *reference nodes* and *commit start nodes*. A commit start
133node is written whenever an index commit is performed. Reference nodes are
134written on every journal update. Each reference node points to the position of
135other nodes (inode nodes, data nodes etc.) on the flash that are part of this
136journal entry. These nodes are called *buds* and describe the actual filesystem
137changes including their data.
138
139The log area is maintained as a ring. Whenever the journal is almost full,
140a commit is initiated. This also writes a commit start node so that during
141mount, UBIFS will seek for the most recent commit start node and just replay
142every reference node after that. Every reference node before the commit start
143node will be ignored as they are already part of the on-flash index.
144
145When writing a journal entry, UBIFS first ensures that enough space is
146available to write the reference node and buds part of this entry. Then, the
147reference node is written and afterwards the buds describing the file changes.
148On replay, UBIFS will record every reference node and inspect the location of
149the referenced LEBs to discover the buds. If these are corrupt or missing,
150UBIFS will attempt to recover them by re-reading the LEB. This is however only
151done for the last referenced LEB of the journal. Only this can become corrupt
152because of a power cut. If the recovery fails, UBIFS will not mount. An error
153for every other LEB will directly cause UBIFS to fail the mount operation.
154
155