hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/ubifs/journal.c
....@@ -1,20 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * This file is part of UBIFS.
34 *
45 * Copyright (C) 2006-2008 Nokia Corporation.
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License version 2 as published by
8
- * the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope that it will be useful, but WITHOUT
11
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
- * more details.
14
- *
15
- * You should have received a copy of the GNU General Public License along with
16
- * this program; if not, write to the Free Software Foundation, Inc., 51
17
- * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
186 *
197 * Authors: Artem Bityutskiy (Битюцкий Артём)
208 * Adrian Hunter
....@@ -88,6 +76,12 @@
8876 static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
8977 {
9078 memset(trun->padding, 0, 12);
79
+}
80
+
81
+static void ubifs_add_auth_dirt(struct ubifs_info *c, int lnum)
82
+{
83
+ if (ubifs_authenticated(c))
84
+ ubifs_add_dirt(c, lnum, ubifs_auth_node_sz(c));
9185 }
9286
9387 /**
....@@ -228,34 +222,33 @@
228222 return err;
229223 }
230224
231
-/**
232
- * write_node - write node to a journal head.
233
- * @c: UBIFS file-system description object
234
- * @jhead: journal head
235
- * @node: node to write
236
- * @len: node length
237
- * @lnum: LEB number written is returned here
238
- * @offs: offset written is returned here
239
- *
240
- * This function writes a node to reserved space of journal head @jhead.
241
- * Returns zero in case of success and a negative error code in case of
242
- * failure.
243
- */
244
-static int write_node(struct ubifs_info *c, int jhead, void *node, int len,
245
- int *lnum, int *offs)
225
+static int ubifs_hash_nodes(struct ubifs_info *c, void *node,
226
+ int len, struct shash_desc *hash)
246227 {
247
- struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
228
+ int auth_node_size = ubifs_auth_node_sz(c);
229
+ int err;
248230
249
- ubifs_assert(c, jhead != GCHD);
231
+ while (1) {
232
+ const struct ubifs_ch *ch = node;
233
+ int nodelen = le32_to_cpu(ch->len);
250234
251
- *lnum = c->jheads[jhead].wbuf.lnum;
252
- *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
235
+ ubifs_assert(c, len >= auth_node_size);
253236
254
- dbg_jnl("jhead %s, LEB %d:%d, len %d",
255
- dbg_jhead(jhead), *lnum, *offs, len);
256
- ubifs_prepare_node(c, node, len, 0);
237
+ if (len == auth_node_size)
238
+ break;
257239
258
- return ubifs_wbuf_write_nolock(wbuf, node, len);
240
+ ubifs_assert(c, len > nodelen);
241
+ ubifs_assert(c, ch->magic == cpu_to_le32(UBIFS_NODE_MAGIC));
242
+
243
+ err = ubifs_shash_update(c, hash, (void *)node, nodelen);
244
+ if (err)
245
+ return err;
246
+
247
+ node += ALIGN(nodelen, 8);
248
+ len -= ALIGN(nodelen, 8);
249
+ }
250
+
251
+ return ubifs_prepare_auth_node(c, node, hash);
259252 }
260253
261254 /**
....@@ -268,9 +261,9 @@
268261 * @offs: offset written is returned here
269262 * @sync: non-zero if the write-buffer has to by synchronized
270263 *
271
- * This function is the same as 'write_node()' but it does not assume the
272
- * buffer it is writing is a node, so it does not prepare it (which means
273
- * initializing common header and calculating CRC).
264
+ * This function writes data to the reserved space of journal head @jhead.
265
+ * Returns zero in case of success and a negative error code in case of
266
+ * failure.
274267 */
275268 static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
276269 int *lnum, int *offs, int sync)
....@@ -284,6 +277,12 @@
284277 *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
285278 dbg_jnl("jhead %s, LEB %d:%d, len %d",
286279 dbg_jhead(jhead), *lnum, *offs, len);
280
+
281
+ if (ubifs_authenticated(c)) {
282
+ err = ubifs_hash_nodes(c, buf, len, c->jheads[jhead].log_hash);
283
+ if (err)
284
+ return err;
285
+ }
287286
288287 err = ubifs_wbuf_write_nolock(wbuf, buf, len);
289288 if (err)
....@@ -504,7 +503,7 @@
504503 static void set_dent_cookie(struct ubifs_info *c, struct ubifs_dent_node *dent)
505504 {
506505 if (c->double_hash)
507
- dent->cookie = prandom_u32();
506
+ dent->cookie = (__force __le32) prandom_u32();
508507 else
509508 dent->cookie = 0;
510509 }
....@@ -540,7 +539,7 @@
540539 const struct fscrypt_name *nm, const struct inode *inode,
541540 int deletion, int xent)
542541 {
543
- int err, dlen, ilen, len, lnum, ino_offs, dent_offs;
542
+ int err, dlen, ilen, len, lnum, ino_offs, dent_offs, orphan_added = 0;
544543 int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
545544 int last_reference = !!(deletion && inode->i_nlink == 0);
546545 struct ubifs_inode *ui = ubifs_inode(inode);
....@@ -548,6 +547,9 @@
548547 struct ubifs_dent_node *dent;
549548 struct ubifs_ino_node *ino;
550549 union ubifs_key dent_key, ino_key;
550
+ u8 hash_dent[UBIFS_HASH_ARR_SZ];
551
+ u8 hash_ino[UBIFS_HASH_ARR_SZ];
552
+ u8 hash_ino_host[UBIFS_HASH_ARR_SZ];
551553
552554 ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
553555
....@@ -570,7 +572,10 @@
570572
571573 len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
572574 /* Make sure to also account for extended attributes */
573
- len += host_ui->data_len;
575
+ if (ubifs_authenticated(c))
576
+ len += ALIGN(host_ui->data_len, 8) + ubifs_auth_node_sz(c);
577
+ else
578
+ len += host_ui->data_len;
574579
575580 dent = kzalloc(len, GFP_NOFS);
576581 if (!dent)
....@@ -602,11 +607,21 @@
602607
603608 zero_dent_node_unused(dent);
604609 ubifs_prep_grp_node(c, dent, dlen, 0);
610
+ err = ubifs_node_calc_hash(c, dent, hash_dent);
611
+ if (err)
612
+ goto out_release;
605613
606614 ino = (void *)dent + aligned_dlen;
607615 pack_inode(c, ino, inode, 0);
616
+ err = ubifs_node_calc_hash(c, ino, hash_ino);
617
+ if (err)
618
+ goto out_release;
619
+
608620 ino = (void *)ino + aligned_ilen;
609621 pack_inode(c, ino, dir, 1);
622
+ err = ubifs_node_calc_hash(c, ino, hash_ino_host);
623
+ if (err)
624
+ goto out_release;
610625
611626 if (last_reference) {
612627 err = ubifs_add_orphan(c, inode->i_ino);
....@@ -615,6 +630,7 @@
615630 goto out_finish;
616631 }
617632 ui->del_cmtno = c->cmt_no;
633
+ orphan_added = 1;
618634 }
619635
620636 err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
....@@ -628,6 +644,7 @@
628644 }
629645 release_head(c, BASEHD);
630646 kfree(dent);
647
+ ubifs_add_auth_dirt(c, lnum);
631648
632649 if (deletion) {
633650 if (fname_name(nm) == NULL)
....@@ -638,7 +655,8 @@
638655 goto out_ro;
639656 err = ubifs_add_dirt(c, lnum, dlen);
640657 } else
641
- err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen, nm);
658
+ err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen,
659
+ hash_dent, nm);
642660 if (err)
643661 goto out_ro;
644662
....@@ -650,14 +668,14 @@
650668 */
651669 ino_key_init(c, &ino_key, inode->i_ino);
652670 ino_offs = dent_offs + aligned_dlen;
653
- err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen);
671
+ err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen, hash_ino);
654672 if (err)
655673 goto out_ro;
656674
657675 ino_key_init(c, &ino_key, dir->i_ino);
658676 ino_offs += aligned_ilen;
659677 err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
660
- UBIFS_INO_NODE_SZ + host_ui->data_len);
678
+ UBIFS_INO_NODE_SZ + host_ui->data_len, hash_ino_host);
661679 if (err)
662680 goto out_ro;
663681
....@@ -685,7 +703,7 @@
685703 kfree(dent);
686704 out_ro:
687705 ubifs_ro_mode(c, err);
688
- if (last_reference)
706
+ if (orphan_added)
689707 ubifs_delete_orphan(c, inode->i_ino);
690708 finish_reservation(c);
691709 return err;
....@@ -706,10 +724,12 @@
706724 const union ubifs_key *key, const void *buf, int len)
707725 {
708726 struct ubifs_data_node *data;
709
- int err, lnum, offs, compr_type, out_len, compr_len;
727
+ int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
710728 int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
729
+ int write_len;
711730 struct ubifs_inode *ui = ubifs_inode(inode);
712
- bool encrypted = ubifs_crypt_is_encrypted(inode);
731
+ bool encrypted = IS_ENCRYPTED(inode);
732
+ u8 hash[UBIFS_HASH_ARR_SZ];
713733
714734 dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
715735 (unsigned long)key_inum(c, key), key_block(c, key), len);
....@@ -718,7 +738,9 @@
718738 if (encrypted)
719739 dlen += UBIFS_CIPHER_BLOCK_SIZE;
720740
721
- data = kmalloc(dlen, GFP_NOFS | __GFP_NOWARN);
741
+ auth_len = ubifs_auth_node_sz(c);
742
+
743
+ data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
722744 if (!data) {
723745 /*
724746 * Fall-back to the write reserve buffer. Note, we might be
....@@ -757,20 +779,33 @@
757779 }
758780
759781 dlen = UBIFS_DATA_NODE_SZ + out_len;
782
+ if (ubifs_authenticated(c))
783
+ write_len = ALIGN(dlen, 8) + auth_len;
784
+ else
785
+ write_len = dlen;
786
+
760787 data->compr_type = cpu_to_le16(compr_type);
761788
762789 /* Make reservation before allocating sequence numbers */
763
- err = make_reservation(c, DATAHD, dlen);
790
+ err = make_reservation(c, DATAHD, write_len);
764791 if (err)
765792 goto out_free;
766793
767
- err = write_node(c, DATAHD, data, dlen, &lnum, &offs);
794
+ ubifs_prepare_node(c, data, dlen, 0);
795
+ err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
768796 if (err)
769797 goto out_release;
798
+
799
+ err = ubifs_node_calc_hash(c, data, hash);
800
+ if (err)
801
+ goto out_release;
802
+
770803 ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
771804 release_head(c, DATAHD);
772805
773
- err = ubifs_tnc_add(c, key, lnum, offs, dlen);
806
+ ubifs_add_auth_dirt(c, lnum);
807
+
808
+ err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
774809 if (err)
775810 goto out_ro;
776811
....@@ -806,9 +841,12 @@
806841 int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
807842 {
808843 int err, lnum, offs;
809
- struct ubifs_ino_node *ino;
844
+ struct ubifs_ino_node *ino, *ino_start;
810845 struct ubifs_inode *ui = ubifs_inode(inode);
811
- int sync = 0, len = UBIFS_INO_NODE_SZ, last_reference = !inode->i_nlink;
846
+ int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
847
+ int last_reference = !inode->i_nlink;
848
+ int kill_xattrs = ui->xattr_cnt && last_reference;
849
+ u8 hash[UBIFS_HASH_ARR_SZ];
812850
813851 dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
814852
....@@ -817,20 +855,83 @@
817855 * need to synchronize the write-buffer either.
818856 */
819857 if (!last_reference) {
820
- len += ui->data_len;
858
+ ilen += ui->data_len;
821859 sync = IS_SYNC(inode);
860
+ } else if (kill_xattrs) {
861
+ write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
822862 }
823
- ino = kmalloc(len, GFP_NOFS);
863
+
864
+ if (ubifs_authenticated(c))
865
+ write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
866
+ else
867
+ write_len += ilen;
868
+
869
+ ino_start = ino = kmalloc(write_len, GFP_NOFS);
824870 if (!ino)
825871 return -ENOMEM;
826872
827873 /* Make reservation before allocating sequence numbers */
828
- err = make_reservation(c, BASEHD, len);
874
+ err = make_reservation(c, BASEHD, write_len);
829875 if (err)
830876 goto out_free;
831877
878
+ if (kill_xattrs) {
879
+ union ubifs_key key;
880
+ struct fscrypt_name nm = {0};
881
+ struct inode *xino;
882
+ struct ubifs_dent_node *xent, *pxent = NULL;
883
+
884
+ if (ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
885
+ err = -EPERM;
886
+ ubifs_err(c, "Cannot delete inode, it has too much xattrs!");
887
+ goto out_release;
888
+ }
889
+
890
+ lowest_xent_key(c, &key, inode->i_ino);
891
+ while (1) {
892
+ xent = ubifs_tnc_next_ent(c, &key, &nm);
893
+ if (IS_ERR(xent)) {
894
+ err = PTR_ERR(xent);
895
+ if (err == -ENOENT)
896
+ break;
897
+
898
+ kfree(pxent);
899
+ goto out_release;
900
+ }
901
+
902
+ fname_name(&nm) = xent->name;
903
+ fname_len(&nm) = le16_to_cpu(xent->nlen);
904
+
905
+ xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
906
+ if (IS_ERR(xino)) {
907
+ err = PTR_ERR(xino);
908
+ ubifs_err(c, "dead directory entry '%s', error %d",
909
+ xent->name, err);
910
+ ubifs_ro_mode(c, err);
911
+ kfree(pxent);
912
+ kfree(xent);
913
+ goto out_release;
914
+ }
915
+ ubifs_assert(c, ubifs_inode(xino)->xattr);
916
+
917
+ clear_nlink(xino);
918
+ pack_inode(c, ino, xino, 0);
919
+ ino = (void *)ino + UBIFS_INO_NODE_SZ;
920
+ iput(xino);
921
+
922
+ kfree(pxent);
923
+ pxent = xent;
924
+ key_read(c, &xent->key, &key);
925
+ }
926
+ kfree(pxent);
927
+ }
928
+
832929 pack_inode(c, ino, inode, 1);
833
- err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
930
+ err = ubifs_node_calc_hash(c, ino, hash);
931
+ if (err)
932
+ goto out_release;
933
+
934
+ err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
834935 if (err)
835936 goto out_release;
836937 if (!sync)
....@@ -843,12 +944,14 @@
843944 if (err)
844945 goto out_ro;
845946 ubifs_delete_orphan(c, inode->i_ino);
846
- err = ubifs_add_dirt(c, lnum, len);
947
+ err = ubifs_add_dirt(c, lnum, write_len);
847948 } else {
848949 union ubifs_key key;
849950
951
+ ubifs_add_auth_dirt(c, lnum);
952
+
850953 ino_key_init(c, &key, inode->i_ino);
851
- err = ubifs_tnc_add(c, &key, lnum, offs, len);
954
+ err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
852955 }
853956 if (err)
854957 goto out_ro;
....@@ -857,7 +960,7 @@
857960 spin_lock(&ui->ui_lock);
858961 ui->synced_i_size = ui->ui_size;
859962 spin_unlock(&ui->ui_lock);
860
- kfree(ino);
963
+ kfree(ino_start);
861964 return 0;
862965
863966 out_release:
....@@ -866,7 +969,7 @@
866969 ubifs_ro_mode(c, err);
867970 finish_reservation(c);
868971 out_free:
869
- kfree(ino);
972
+ kfree(ino_start);
870973 return err;
871974 }
872975
....@@ -906,8 +1009,8 @@
9061009
9071010 ubifs_assert(c, inode->i_nlink == 0);
9081011
909
- if (ui->del_cmtno != c->cmt_no)
910
- /* A commit happened for sure */
1012
+ if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
1013
+ /* A commit happened for sure or inode hosts xattrs */
9111014 return ubifs_jnl_write_inode(c, inode);
9121015
9131016 down_read(&c->commit_sem);
....@@ -958,6 +1061,10 @@
9581061 int aligned_dlen1, aligned_dlen2;
9591062 int twoparents = (fst_dir != snd_dir);
9601063 void *p;
1064
+ u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1065
+ u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1066
+ u8 hash_p1[UBIFS_HASH_ARR_SZ];
1067
+ u8 hash_p2[UBIFS_HASH_ARR_SZ];
9611068
9621069 ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
9631070 ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
....@@ -972,6 +1079,8 @@
9721079 len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
9731080 if (twoparents)
9741081 len += plen;
1082
+
1083
+ len += ubifs_auth_node_sz(c);
9751084
9761085 dent1 = kzalloc(len, GFP_NOFS);
9771086 if (!dent1)
....@@ -993,6 +1102,9 @@
9931102 set_dent_cookie(c, dent1);
9941103 zero_dent_node_unused(dent1);
9951104 ubifs_prep_grp_node(c, dent1, dlen1, 0);
1105
+ err = ubifs_node_calc_hash(c, dent1, hash_dent1);
1106
+ if (err)
1107
+ goto out_release;
9961108
9971109 /* Make new dent for 2nd entry */
9981110 dent2 = (void *)dent1 + aligned_dlen1;
....@@ -1006,14 +1118,26 @@
10061118 set_dent_cookie(c, dent2);
10071119 zero_dent_node_unused(dent2);
10081120 ubifs_prep_grp_node(c, dent2, dlen2, 0);
1121
+ err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1122
+ if (err)
1123
+ goto out_release;
10091124
10101125 p = (void *)dent2 + aligned_dlen2;
1011
- if (!twoparents)
1126
+ if (!twoparents) {
10121127 pack_inode(c, p, fst_dir, 1);
1013
- else {
1128
+ err = ubifs_node_calc_hash(c, p, hash_p1);
1129
+ if (err)
1130
+ goto out_release;
1131
+ } else {
10141132 pack_inode(c, p, fst_dir, 0);
1133
+ err = ubifs_node_calc_hash(c, p, hash_p1);
1134
+ if (err)
1135
+ goto out_release;
10151136 p += ALIGN(plen, 8);
10161137 pack_inode(c, p, snd_dir, 1);
1138
+ err = ubifs_node_calc_hash(c, p, hash_p2);
1139
+ if (err)
1140
+ goto out_release;
10171141 }
10181142
10191143 err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
....@@ -1027,28 +1151,30 @@
10271151 }
10281152 release_head(c, BASEHD);
10291153
1154
+ ubifs_add_auth_dirt(c, lnum);
1155
+
10301156 dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
1031
- err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, snd_nm);
1157
+ err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
10321158 if (err)
10331159 goto out_ro;
10341160
10351161 offs += aligned_dlen1;
10361162 dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
1037
- err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, fst_nm);
1163
+ err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
10381164 if (err)
10391165 goto out_ro;
10401166
10411167 offs += aligned_dlen2;
10421168
10431169 ino_key_init(c, &key, fst_dir->i_ino);
1044
- err = ubifs_tnc_add(c, &key, lnum, offs, plen);
1170
+ err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
10451171 if (err)
10461172 goto out_ro;
10471173
10481174 if (twoparents) {
10491175 offs += ALIGN(plen, 8);
10501176 ino_key_init(c, &key, snd_dir->i_ino);
1051
- err = ubifs_tnc_add(c, &key, lnum, offs, plen);
1177
+ err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
10521178 if (err)
10531179 goto out_ro;
10541180 }
....@@ -1096,11 +1222,16 @@
10961222 void *p;
10971223 union ubifs_key key;
10981224 struct ubifs_dent_node *dent, *dent2;
1099
- int err, dlen1, dlen2, ilen, lnum, offs, len;
1225
+ int err, dlen1, dlen2, ilen, lnum, offs, len, orphan_added = 0;
11001226 int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
11011227 int last_reference = !!(new_inode && new_inode->i_nlink == 0);
11021228 int move = (old_dir != new_dir);
1103
- struct ubifs_inode *uninitialized_var(new_ui);
1229
+ struct ubifs_inode *new_ui;
1230
+ u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
1231
+ u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
1232
+ u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
1233
+ u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1234
+ u8 hash_dent2[UBIFS_HASH_ARR_SZ];
11041235
11051236 ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
11061237 ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
....@@ -1123,6 +1254,9 @@
11231254 len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
11241255 if (move)
11251256 len += plen;
1257
+
1258
+ len += ubifs_auth_node_sz(c);
1259
+
11261260 dent = kzalloc(len, GFP_NOFS);
11271261 if (!dent)
11281262 return -ENOMEM;
....@@ -1143,6 +1277,9 @@
11431277 set_dent_cookie(c, dent);
11441278 zero_dent_node_unused(dent);
11451279 ubifs_prep_grp_node(c, dent, dlen1, 0);
1280
+ err = ubifs_node_calc_hash(c, dent, hash_dent1);
1281
+ if (err)
1282
+ goto out_release;
11461283
11471284 dent2 = (void *)dent + aligned_dlen1;
11481285 dent2->ch.node_type = UBIFS_DENT_NODE;
....@@ -1162,19 +1299,36 @@
11621299 set_dent_cookie(c, dent2);
11631300 zero_dent_node_unused(dent2);
11641301 ubifs_prep_grp_node(c, dent2, dlen2, 0);
1302
+ err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1303
+ if (err)
1304
+ goto out_release;
11651305
11661306 p = (void *)dent2 + aligned_dlen2;
11671307 if (new_inode) {
11681308 pack_inode(c, p, new_inode, 0);
1309
+ err = ubifs_node_calc_hash(c, p, hash_new_inode);
1310
+ if (err)
1311
+ goto out_release;
1312
+
11691313 p += ALIGN(ilen, 8);
11701314 }
11711315
1172
- if (!move)
1316
+ if (!move) {
11731317 pack_inode(c, p, old_dir, 1);
1174
- else {
1318
+ err = ubifs_node_calc_hash(c, p, hash_old_dir);
1319
+ if (err)
1320
+ goto out_release;
1321
+ } else {
11751322 pack_inode(c, p, old_dir, 0);
1323
+ err = ubifs_node_calc_hash(c, p, hash_old_dir);
1324
+ if (err)
1325
+ goto out_release;
1326
+
11761327 p += ALIGN(plen, 8);
11771328 pack_inode(c, p, new_dir, 1);
1329
+ err = ubifs_node_calc_hash(c, p, hash_new_dir);
1330
+ if (err)
1331
+ goto out_release;
11781332 }
11791333
11801334 if (last_reference) {
....@@ -1184,6 +1338,7 @@
11841338 goto out_finish;
11851339 }
11861340 new_ui->del_cmtno = c->cmt_no;
1341
+ orphan_added = 1;
11871342 }
11881343
11891344 err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
....@@ -1200,15 +1355,17 @@
12001355 }
12011356 release_head(c, BASEHD);
12021357
1358
+ ubifs_add_auth_dirt(c, lnum);
1359
+
12031360 dent_key_init(c, &key, new_dir->i_ino, new_nm);
1204
- err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, new_nm);
1361
+ err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
12051362 if (err)
12061363 goto out_ro;
12071364
12081365 offs += aligned_dlen1;
12091366 if (whiteout) {
12101367 dent_key_init(c, &key, old_dir->i_ino, old_nm);
1211
- err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, old_nm);
1368
+ err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
12121369 if (err)
12131370 goto out_ro;
12141371
....@@ -1227,21 +1384,21 @@
12271384 offs += aligned_dlen2;
12281385 if (new_inode) {
12291386 ino_key_init(c, &key, new_inode->i_ino);
1230
- err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
1387
+ err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
12311388 if (err)
12321389 goto out_ro;
12331390 offs += ALIGN(ilen, 8);
12341391 }
12351392
12361393 ino_key_init(c, &key, old_dir->i_ino);
1237
- err = ubifs_tnc_add(c, &key, lnum, offs, plen);
1394
+ err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
12381395 if (err)
12391396 goto out_ro;
12401397
12411398 if (move) {
12421399 offs += ALIGN(plen, 8);
12431400 ino_key_init(c, &key, new_dir->i_ino);
1244
- err = ubifs_tnc_add(c, &key, lnum, offs, plen);
1401
+ err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
12451402 if (err)
12461403 goto out_ro;
12471404 }
....@@ -1263,7 +1420,7 @@
12631420 release_head(c, BASEHD);
12641421 out_ro:
12651422 ubifs_ro_mode(c, err);
1266
- if (last_reference)
1423
+ if (orphan_added)
12671424 ubifs_delete_orphan(c, new_inode->i_ino);
12681425 out_finish:
12691426 finish_reservation(c);
....@@ -1298,7 +1455,7 @@
12981455 dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
12991456 compr_type = le16_to_cpu(dn->compr_type);
13001457
1301
- if (ubifs_crypt_is_encrypted(inode)) {
1458
+ if (IS_ENCRYPTED(inode)) {
13021459 err = ubifs_decrypt(inode, dn, &dlen, block);
13031460 if (err)
13041461 goto out;
....@@ -1314,7 +1471,7 @@
13141471 ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
13151472 }
13161473
1317
- if (ubifs_crypt_is_encrypted(inode)) {
1474
+ if (IS_ENCRYPTED(inode)) {
13181475 err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
13191476 if (err)
13201477 goto out;
....@@ -1355,11 +1512,13 @@
13551512 union ubifs_key key, to_key;
13561513 struct ubifs_ino_node *ino;
13571514 struct ubifs_trun_node *trun;
1358
- struct ubifs_data_node *uninitialized_var(dn);
1515
+ struct ubifs_data_node *dn;
13591516 int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
13601517 struct ubifs_inode *ui = ubifs_inode(inode);
13611518 ino_t inum = inode->i_ino;
13621519 unsigned int blk;
1520
+ u8 hash_ino[UBIFS_HASH_ARR_SZ];
1521
+ u8 hash_dn[UBIFS_HASH_ARR_SZ];
13631522
13641523 dbg_jnl("ino %lu, size %lld -> %lld",
13651524 (unsigned long)inum, old_size, new_size);
....@@ -1369,6 +1528,9 @@
13691528
13701529 sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
13711530 UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
1531
+
1532
+ sz += ubifs_auth_node_sz(c);
1533
+
13721534 ino = kmalloc(sz, GFP_NOFS);
13731535 if (!ino)
13741536 return -ENOMEM;
....@@ -1414,16 +1576,28 @@
14141576
14151577 /* Must make reservation before allocating sequence numbers */
14161578 len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
1417
- if (dlen)
1579
+
1580
+ if (ubifs_authenticated(c))
1581
+ len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
1582
+ else
14181583 len += dlen;
1584
+
14191585 err = make_reservation(c, BASEHD, len);
14201586 if (err)
14211587 goto out_free;
14221588
14231589 pack_inode(c, ino, inode, 0);
1590
+ err = ubifs_node_calc_hash(c, ino, hash_ino);
1591
+ if (err)
1592
+ goto out_release;
1593
+
14241594 ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1425
- if (dlen)
1595
+ if (dlen) {
14261596 ubifs_prep_grp_node(c, dn, dlen, 1);
1597
+ err = ubifs_node_calc_hash(c, dn, hash_dn);
1598
+ if (err)
1599
+ goto out_release;
1600
+ }
14271601
14281602 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
14291603 if (err)
....@@ -1432,15 +1606,17 @@
14321606 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
14331607 release_head(c, BASEHD);
14341608
1609
+ ubifs_add_auth_dirt(c, lnum);
1610
+
14351611 if (dlen) {
14361612 sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
1437
- err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
1613
+ err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
14381614 if (err)
14391615 goto out_ro;
14401616 }
14411617
14421618 ino_key_init(c, &key, inum);
1443
- err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
1619
+ err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
14441620 if (err)
14451621 goto out_ro;
14461622
....@@ -1495,12 +1671,13 @@
14951671 const struct inode *inode,
14961672 const struct fscrypt_name *nm)
14971673 {
1498
- int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
1674
+ int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
14991675 struct ubifs_dent_node *xent;
15001676 struct ubifs_ino_node *ino;
15011677 union ubifs_key xent_key, key1, key2;
15021678 int sync = IS_DIRSYNC(host);
15031679 struct ubifs_inode *host_ui = ubifs_inode(host);
1680
+ u8 hash[UBIFS_HASH_ARR_SZ];
15041681
15051682 ubifs_assert(c, inode->i_nlink == 0);
15061683 ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
....@@ -1514,12 +1691,14 @@
15141691 hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
15151692 len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
15161693
1517
- xent = kzalloc(len, GFP_NOFS);
1694
+ write_len = len + ubifs_auth_node_sz(c);
1695
+
1696
+ xent = kzalloc(write_len, GFP_NOFS);
15181697 if (!xent)
15191698 return -ENOMEM;
15201699
15211700 /* Make reservation before allocating sequence numbers */
1522
- err = make_reservation(c, BASEHD, len);
1701
+ err = make_reservation(c, BASEHD, write_len);
15231702 if (err) {
15241703 kfree(xent);
15251704 return err;
....@@ -1540,11 +1719,16 @@
15401719 pack_inode(c, ino, inode, 0);
15411720 ino = (void *)ino + UBIFS_INO_NODE_SZ;
15421721 pack_inode(c, ino, host, 1);
1722
+ err = ubifs_node_calc_hash(c, ino, hash);
1723
+ if (err)
1724
+ goto out_release;
15431725
1544
- err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
1726
+ err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
15451727 if (!sync && !err)
15461728 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
15471729 release_head(c, BASEHD);
1730
+
1731
+ ubifs_add_auth_dirt(c, lnum);
15481732 kfree(xent);
15491733 if (err)
15501734 goto out_ro;
....@@ -1572,7 +1756,7 @@
15721756
15731757 /* And update TNC with the new host inode position */
15741758 ino_key_init(c, &key1, host->i_ino);
1575
- err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
1759
+ err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
15761760 if (err)
15771761 goto out_ro;
15781762
....@@ -1583,6 +1767,9 @@
15831767 mark_inode_clean(c, host_ui);
15841768 return 0;
15851769
1770
+out_release:
1771
+ kfree(xent);
1772
+ release_head(c, BASEHD);
15861773 out_ro:
15871774 ubifs_ro_mode(c, err);
15881775 finish_reservation(c);
....@@ -1610,9 +1797,10 @@
16101797 struct ubifs_ino_node *ino;
16111798 union ubifs_key key;
16121799 int sync = IS_DIRSYNC(host);
1800
+ u8 hash_host[UBIFS_HASH_ARR_SZ];
1801
+ u8 hash[UBIFS_HASH_ARR_SZ];
16131802
16141803 dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
1615
- ubifs_assert(c, host->i_nlink > 0);
16161804 ubifs_assert(c, inode->i_nlink > 0);
16171805 ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
16181806
....@@ -1620,6 +1808,8 @@
16201808 len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
16211809 aligned_len1 = ALIGN(len1, 8);
16221810 aligned_len = aligned_len1 + ALIGN(len2, 8);
1811
+
1812
+ aligned_len += ubifs_auth_node_sz(c);
16231813
16241814 ino = kzalloc(aligned_len, GFP_NOFS);
16251815 if (!ino)
....@@ -1631,7 +1821,13 @@
16311821 goto out_free;
16321822
16331823 pack_inode(c, ino, host, 0);
1824
+ err = ubifs_node_calc_hash(c, ino, hash_host);
1825
+ if (err)
1826
+ goto out_release;
16341827 pack_inode(c, (void *)ino + aligned_len1, inode, 1);
1828
+ err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
1829
+ if (err)
1830
+ goto out_release;
16351831
16361832 err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
16371833 if (!sync && !err) {
....@@ -1644,13 +1840,15 @@
16441840 if (err)
16451841 goto out_ro;
16461842
1843
+ ubifs_add_auth_dirt(c, lnum);
1844
+
16471845 ino_key_init(c, &key, host->i_ino);
1648
- err = ubifs_tnc_add(c, &key, lnum, offs, len1);
1846
+ err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
16491847 if (err)
16501848 goto out_ro;
16511849
16521850 ino_key_init(c, &key, inode->i_ino);
1653
- err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
1851
+ err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
16541852 if (err)
16551853 goto out_ro;
16561854
....@@ -1662,6 +1860,8 @@
16621860 kfree(ino);
16631861 return 0;
16641862
1863
+out_release:
1864
+ release_head(c, BASEHD);
16651865 out_ro:
16661866 ubifs_ro_mode(c, err);
16671867 finish_reservation(c);