forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/fs/ubifs/tnc.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: Adrian Hunter
208 * Artem Bityutskiy (Битюцкий Артём)
....@@ -35,7 +23,7 @@
3523 #include "ubifs.h"
3624
3725 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
38
- int len, int lnum, int offs);
26
+ struct ubifs_zbranch *zbr);
3927 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
4028 struct ubifs_zbranch *zbr, void *node);
4129
....@@ -372,7 +360,6 @@
372360 /**
373361 * lnc_free - remove a leaf node from the leaf node cache.
374362 * @zbr: zbranch of leaf node
375
- * @node: leaf node
376363 */
377364 static void lnc_free(struct ubifs_zbranch *zbr)
378365 {
....@@ -433,9 +420,7 @@
433420 * @c: UBIFS file-system description object
434421 * @buf: buffer to read to
435422 * @type: node type
436
- * @len: node length (not aligned)
437
- * @lnum: LEB number of node to read
438
- * @offs: offset of node to read
423
+ * @zbr: the zbranch describing the node to read
439424 *
440425 * This function tries to read a node of known type and length, checks it and
441426 * stores it in @buf. This function returns %1 if a node is present and %0 if
....@@ -453,8 +438,11 @@
453438 * journal nodes may potentially be corrupted, so checking is required.
454439 */
455440 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
456
- int len, int lnum, int offs)
441
+ struct ubifs_zbranch *zbr)
457442 {
443
+ int len = zbr->len;
444
+ int lnum = zbr->lnum;
445
+ int offs = zbr->offs;
458446 int err, node_len;
459447 struct ubifs_ch *ch = buf;
460448 uint32_t crc, node_crc;
....@@ -478,14 +466,19 @@
478466 if (node_len != len)
479467 return 0;
480468
481
- if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting &&
482
- !c->remounting_rw)
483
- return 1;
469
+ if (type != UBIFS_DATA_NODE || !c->no_chk_data_crc || c->mounting ||
470
+ c->remounting_rw) {
471
+ crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
472
+ node_crc = le32_to_cpu(ch->crc);
473
+ if (crc != node_crc)
474
+ return 0;
475
+ }
484476
485
- crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
486
- node_crc = le32_to_cpu(ch->crc);
487
- if (crc != node_crc)
477
+ err = ubifs_node_check_hash(c, buf, zbr->hash);
478
+ if (err) {
479
+ ubifs_bad_hash(c, buf, zbr->hash, lnum, offs);
488480 return 0;
481
+ }
489482
490483 return 1;
491484 }
....@@ -507,8 +500,7 @@
507500
508501 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
509502
510
- ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
511
- zbr->offs);
503
+ ret = try_read_node(c, node, key_type(c, key), zbr);
512504 if (ret == 1) {
513505 union ubifs_key node_key;
514506 struct ubifs_dent_node *dent = node;
....@@ -899,7 +891,7 @@
899891 int adding)
900892 {
901893 struct ubifs_znode *o_znode = NULL, *znode = *zn;
902
- int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
894
+ int o_n, err, cmp, unsure = 0, nn = *n;
903895
904896 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
905897 if (unlikely(cmp < 0))
....@@ -1521,8 +1513,8 @@
15211513 */
15221514 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
15231515 {
1524
- int n, err = 0, lnum = -1, uninitialized_var(offs);
1525
- int uninitialized_var(len);
1516
+ int n, err = 0, lnum = -1, offs;
1517
+ int len;
15261518 unsigned int block = key_block(c, &bu->key);
15271519 struct ubifs_znode *znode;
15281520
....@@ -1711,6 +1703,12 @@
17111703 if (err) {
17121704 ubifs_err(c, "expected node type %d", UBIFS_DATA_NODE);
17131705 goto out;
1706
+ }
1707
+
1708
+ err = ubifs_node_check_hash(c, buf, zbr->hash);
1709
+ if (err) {
1710
+ ubifs_bad_hash(c, buf, zbr->hash, zbr->lnum, zbr->offs);
1711
+ return err;
17141712 }
17151713
17161714 len = le32_to_cpu(ch->len);
....@@ -2266,13 +2264,14 @@
22662264 * @lnum: LEB number of node
22672265 * @offs: node offset
22682266 * @len: node length
2267
+ * @hash: The hash over the node
22692268 *
22702269 * This function adds a node with key @key to TNC. The node may be new or it may
22712270 * obsolete some existing one. Returns %0 on success or negative error code on
22722271 * failure.
22732272 */
22742273 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2275
- int offs, int len)
2274
+ int offs, int len, const u8 *hash)
22762275 {
22772276 int found, n, err = 0;
22782277 struct ubifs_znode *znode;
....@@ -2287,6 +2286,7 @@
22872286 zbr.lnum = lnum;
22882287 zbr.offs = offs;
22892288 zbr.len = len;
2289
+ ubifs_copy_hash(c, hash, zbr.hash);
22902290 key_copy(c, key, &zbr.key);
22912291 err = tnc_insert(c, znode, &zbr, n + 1);
22922292 } else if (found == 1) {
....@@ -2297,6 +2297,7 @@
22972297 zbr->lnum = lnum;
22982298 zbr->offs = offs;
22992299 zbr->len = len;
2300
+ ubifs_copy_hash(c, hash, zbr->hash);
23002301 } else
23012302 err = found;
23022303 if (!err)
....@@ -2398,13 +2399,14 @@
23982399 * @lnum: LEB number of node
23992400 * @offs: node offset
24002401 * @len: node length
2402
+ * @hash: The hash over the node
24012403 * @nm: node name
24022404 *
24032405 * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
24042406 * may have collisions, like directory entry keys.
24052407 */
24062408 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2407
- int lnum, int offs, int len,
2409
+ int lnum, int offs, int len, const u8 *hash,
24082410 const struct fscrypt_name *nm)
24092411 {
24102412 int found, n, err = 0;
....@@ -2447,6 +2449,7 @@
24472449 zbr->lnum = lnum;
24482450 zbr->offs = offs;
24492451 zbr->len = len;
2452
+ ubifs_copy_hash(c, hash, zbr->hash);
24502453 goto out_unlock;
24512454 }
24522455 }
....@@ -2458,6 +2461,7 @@
24582461 zbr.lnum = lnum;
24592462 zbr.offs = offs;
24602463 zbr.len = len;
2464
+ ubifs_copy_hash(c, hash, zbr.hash);
24612465 key_copy(c, key, &zbr.key);
24622466 err = tnc_insert(c, znode, &zbr, n + 1);
24632467 if (err)
....@@ -2880,6 +2884,7 @@
28802884 err = PTR_ERR(xent);
28812885 if (err == -ENOENT)
28822886 break;
2887
+ kfree(pxent);
28832888 return err;
28842889 }
28852890
....@@ -2893,6 +2898,7 @@
28932898 fname_len(&nm) = le16_to_cpu(xent->nlen);
28942899 err = ubifs_tnc_remove_nm(c, &key1, &nm);
28952900 if (err) {
2901
+ kfree(pxent);
28962902 kfree(xent);
28972903 return err;
28982904 }
....@@ -2901,6 +2907,7 @@
29012907 highest_ino_key(c, &key2, xattr_inum);
29022908 err = ubifs_tnc_remove_range(c, &key1, &key2);
29032909 if (err) {
2910
+ kfree(pxent);
29042911 kfree(xent);
29052912 return err;
29062913 }
....@@ -3461,7 +3468,7 @@
34613468 /**
34623469 * dbg_check_inode_size - check if inode size is correct.
34633470 * @c: UBIFS file-system description object
3464
- * @inum: inode number
3471
+ * @inode: inode to check
34653472 * @size: inode size
34663473 *
34673474 * This function makes sure that the inode size (@size) is correct and it does