Skip to content

Commit a384b47

Browse files
saschahauerrichardweinberger
authored andcommitted
ubifs: Create functions to embed a HMAC in a node
With authentication support some nodes (master node, super block node) get a HMAC embedded into them. This patch adds functions to prepare and write such a node. The difficulty is that besides the HMAC the nodes also have a CRC which must stay valid. This means we first have to initialize all fields in the node, then calculate the HMAC (not covering the CRC) and finally calculate the CRC. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 49525e5 commit a384b47

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

fs/ubifs/io.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,39 @@ void ubifs_crc_node(struct ubifs_info *c, void *node, int len)
394394
ch->crc = cpu_to_le32(crc);
395395
}
396396

397+
/**
398+
* ubifs_prepare_node_hmac - prepare node to be written to flash.
399+
* @c: UBIFS file-system description object
400+
* @node: the node to pad
401+
* @len: node length
402+
* @hmac_offs: offset of the HMAC in the node
403+
* @pad: if the buffer has to be padded
404+
*
405+
* This function prepares node at @node to be written to the media - it
406+
* calculates node CRC, fills the common header, and adds proper padding up to
407+
* the next minimum I/O unit if @pad is not zero. if @hmac_offs is positive then
408+
* a HMAC is inserted into the node at the given offset.
409+
*
410+
* This function returns 0 for success or a negative error code otherwise.
411+
*/
412+
int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
413+
int hmac_offs, int pad)
414+
{
415+
int err;
416+
417+
ubifs_init_node(c, node, len, pad);
418+
419+
if (hmac_offs > 0) {
420+
err = ubifs_node_insert_hmac(c, node, len, hmac_offs);
421+
if (err)
422+
return err;
423+
}
424+
425+
ubifs_crc_node(c, node, len);
426+
427+
return 0;
428+
}
429+
397430
/**
398431
* ubifs_prepare_node - prepare node to be written to flash.
399432
* @c: UBIFS file-system description object
@@ -407,8 +440,11 @@ void ubifs_crc_node(struct ubifs_info *c, void *node, int len)
407440
*/
408441
void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
409442
{
410-
ubifs_init_node(c, node, len, pad);
411-
ubifs_crc_node(c, node, len);
443+
/*
444+
* Deliberately ignore return value since this function can only fail
445+
* when a hmac offset is given.
446+
*/
447+
ubifs_prepare_node_hmac(c, node, len, 0, pad);
412448
}
413449

414450
/**
@@ -861,21 +897,22 @@ int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
861897
}
862898

863899
/**
864-
* ubifs_write_node - write node to the media.
900+
* ubifs_write_node_hmac - write node to the media.
865901
* @c: UBIFS file-system description object
866902
* @buf: the node to write
867903
* @len: node length
868904
* @lnum: logical eraseblock number
869905
* @offs: offset within the logical eraseblock
906+
* @hmac_offs: offset of the HMAC within the node
870907
*
871908
* This function automatically fills node magic number, assigns sequence
872909
* number, and calculates node CRC checksum. The length of the @buf buffer has
873910
* to be aligned to the minimal I/O unit size. This function automatically
874911
* appends padding node and padding bytes if needed. Returns zero in case of
875912
* success and a negative error code in case of failure.
876913
*/
877-
int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
878-
int offs)
914+
int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
915+
int offs, int hmac_offs)
879916
{
880917
int err, buf_len = ALIGN(len, c->min_io_size);
881918

@@ -890,14 +927,37 @@ int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
890927
if (c->ro_error)
891928
return -EROFS;
892929

893-
ubifs_prepare_node(c, buf, len, 1);
930+
err = ubifs_prepare_node_hmac(c, buf, len, hmac_offs, 1);
931+
if (err)
932+
return err;
933+
894934
err = ubifs_leb_write(c, lnum, buf, offs, buf_len);
895935
if (err)
896936
ubifs_dump_node(c, buf);
897937

898938
return err;
899939
}
900940

941+
/**
942+
* ubifs_write_node - write node to the media.
943+
* @c: UBIFS file-system description object
944+
* @buf: the node to write
945+
* @len: node length
946+
* @lnum: logical eraseblock number
947+
* @offs: offset within the logical eraseblock
948+
*
949+
* This function automatically fills node magic number, assigns sequence
950+
* number, and calculates node CRC checksum. The length of the @buf buffer has
951+
* to be aligned to the minimal I/O unit size. This function automatically
952+
* appends padding node and padding bytes if needed. Returns zero in case of
953+
* success and a negative error code in case of failure.
954+
*/
955+
int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
956+
int offs)
957+
{
958+
return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1);
959+
}
960+
901961
/**
902962
* ubifs_read_node_wbuf - read node from the media or write-buffer.
903963
* @wbuf: wbuf to check for un-written data

fs/ubifs/ubifs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,11 +1710,15 @@ int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
17101710
int lnum, int offs);
17111711
int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
17121712
int offs);
1713+
int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
1714+
int offs, int hmac_offs);
17131715
int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
17141716
int offs, int quiet, int must_chk_crc);
17151717
void ubifs_init_node(struct ubifs_info *c, void *buf, int len, int pad);
17161718
void ubifs_crc_node(struct ubifs_info *c, void *buf, int len);
17171719
void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
1720+
int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
1721+
int hmac_offs, int pad);
17181722
void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
17191723
int ubifs_io_init(struct ubifs_info *c);
17201724
void ubifs_pad(const struct ubifs_info *c, void *buf, int pad);

0 commit comments

Comments
 (0)