Skip to content

Commit 104115a

Browse files
saschahauerrichardweinberger
authored andcommitted
ubifs: Add hashes and HMACs to default filesystem
This patch calculates the necessary hashes and HMACs for the default filesystem so that the dynamically created default fs can be authenticated. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent e158e02 commit 104115a

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

fs/ubifs/sb.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static int create_default_filesystem(struct ubifs_info *c)
8787
__le64 tmp_le64;
8888
__le32 tmp_le32;
8989
struct timespec64 ts;
90+
u8 hash[UBIFS_HASH_ARR_SZ];
9091
u8 hash_lpt[UBIFS_HASH_ARR_SZ];
9192

9293
/* Some functions called from here depend on the @c->key_len filed */
@@ -177,6 +178,16 @@ static int create_default_filesystem(struct ubifs_info *c)
177178
sup_flags |= UBIFS_FLG_BIGLPT;
178179
sup_flags |= UBIFS_FLG_DOUBLE_HASH;
179180

181+
if (ubifs_authenticated(c)) {
182+
sup_flags |= UBIFS_FLG_AUTHENTICATION;
183+
sup->hash_algo = cpu_to_le16(c->auth_hash_algo);
184+
err = ubifs_hmac_wkm(c, sup->hmac_wkm);
185+
if (err)
186+
goto out;
187+
} else {
188+
sup->hash_algo = 0xffff;
189+
}
190+
180191
sup->ch.node_type = UBIFS_SB_NODE;
181192
sup->key_hash = UBIFS_KEY_HASH_R5;
182193
sup->flags = cpu_to_le32(sup_flags);
@@ -235,6 +246,7 @@ static int create_default_filesystem(struct ubifs_info *c)
235246
mst->empty_lebs = cpu_to_le32(main_lebs - 2);
236247
mst->idx_lebs = cpu_to_le32(1);
237248
mst->leb_cnt = cpu_to_le32(c->leb_cnt);
249+
ubifs_copy_hash(c, hash_lpt, mst->hash_lpt);
238250

239251
/* Calculate lprops statistics */
240252
tmp64 = main_bytes;
@@ -307,25 +319,33 @@ static int create_default_filesystem(struct ubifs_info *c)
307319

308320
cs->ch.node_type = UBIFS_CS_NODE;
309321

310-
err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0);
322+
err = ubifs_write_node_hmac(c, sup, UBIFS_SB_NODE_SZ, 0, 0,
323+
offsetof(struct ubifs_sb_node, hmac));
311324
if (err)
312325
goto out;
313326

314-
err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0);
327+
err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
328+
main_first + DEFAULT_DATA_LEB, 0);
315329
if (err)
316330
goto out;
317331

318-
err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
319-
0);
332+
ubifs_node_calc_hash(c, ino, hash);
333+
ubifs_copy_hash(c, hash, ubifs_branch_hash(c, br));
334+
335+
err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
320336
if (err)
321337
goto out;
322338

323-
err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
339+
ubifs_node_calc_hash(c, idx, hash);
340+
ubifs_copy_hash(c, hash, mst->hash_root_idx);
341+
342+
err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
343+
offsetof(struct ubifs_mst_node, hmac));
324344
if (err)
325345
goto out;
326346

327-
err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
328-
main_first + DEFAULT_DATA_LEB, 0);
347+
err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
348+
0, offsetof(struct ubifs_mst_node, hmac));
329349
if (err)
330350
goto out;
331351

0 commit comments

Comments
 (0)