-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEZH8oZUiU471FcZm+ONu9yGCSaT4FAmQtPbUACgkQONu9yGCS aT462xAAhgh6J/KB4thj31ULLDPkX3zEuTLKIBlLK617NkKHF9k0XA6oAo9A2Fyy t/MfXJvjmmL0kxsWqmoir0ZrPMifgdAK5hoxjXfvjWYtlYi3k0CXqXlg4YQ9Xalp VU3O0RRli3KQxKK3u1PhnMMui7+l3pMELza3pUvyhCxRJx3K9loXkbrFZqdOvXEV QuZ0ugKaxEwWnwStqIzIAUw+jt/13TwPrVQC6cBjkeOOItw2kNw1SPzrjptfHahG M8fApzAKEgZPa49gDw95hZLawt4Acf5suITLgktBtzniFbj8c5A7jaYMFnaKVv3/ 1zUhDu6VYZ5UfLzwYoLnmZ08vWVCTi8r28MJ/f1UdkPlhH9T6blos5RdGB9+4Al8 17KmOPSXLjzs36cSJFaj521earJSrcwvhsc/sc0ENk0U3CO1d0JkqZKClD2QRt82 z4yOlkd8j7SbpMgLdwwKbn0PqlK9YddCH7vXNCeMu9thA+Zjy7Z1zCWzENrh8btt EcQls3VfHSue9avVhkb5THlhEjY8Pe4/x061YWCYqzamIg5/9xjmYTE8mJdXQVxs zr2wgDikAfXHM440/yQgCiAYLT+gB7ewef+ubbhWVwMDviu8vTWlPAiLqnR7TUAp CHvypmojDa6iLVnLGvPmIZTkChGCj0x3u7b5VDBJmlt/DLi8amw= =Y+Jp -----END PGP SIGNATURE----- Merge 5.4.240 into android11-5.4-lts Changes in 5.4.240 net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf() power: supply: da9150: Fix use after free bug in da9150_charger_remove due to race condition iavf: fix inverted Rx hash condition leading to disabled hash iavf: fix non-tunneled IPv6 UDP packet type and hashing intel/igbvf: free irq on the error path in igbvf_request_msix() igbvf: Regard vf reset nack as success i2c: imx-lpi2c: check only for enabled interrupt flags scsi: scsi_dh_alua: Fix memleak for 'qdata' in alua_activate() net: usb: smsc95xx: Limit packet length to skb->len qed/qed_sriov: guard against NULL derefs from qed_iov_get_vf_info xirc2ps_cs: Fix use after free bug in xirc2ps_detach net: qcom/emac: Fix use after free bug in emac_remove due to race condition net/ps3_gelic_net: Fix RX sk_buff length net/ps3_gelic_net: Use dma_mapping_error keys: Do not cache key in task struct if key is requested from kernel thread bpf: Adjust insufficient default bpf_jit_limit net/mlx5: Read the TC mapping of all priorities on ETS query atm: idt77252: fix kmemleak when rmmod idt77252 erspan: do not use skb_mac_header() in ndo_start_xmit() net/sonic: use dma_mapping_error() for error check nvme-tcp: fix nvme_tcp_term_pdu to match spec hvc/xen: prevent concurrent accesses to the shared ring net: mdio: thunder: Add missing fwnode_handle_put() Bluetooth: btqcomsmd: Fix command timeout after setting BD address Bluetooth: btsdio: fix use after free bug in btsdio_remove due to unfinished work platform/chrome: cros_ec_chardev: fix kernel data leak from ioctl hwmon (it87): Fix voltage scaling for chips with 10.9mV ADCs scsi: qla2xxx: Perform lockless command completion in abort path uas: Add US_FL_NO_REPORT_OPCODES for JMicron JMS583Gen 2 thunderbolt: Use const qualifier for `ring_interrupt_index` riscv: Bump COMMAND_LINE_SIZE value to 1024 ca8210: fix mac_len negative array access m68k: Only force 030 bus error if PC not in exception table selftests/bpf: check that modifier resolves after pointer scsi: target: iscsi: Fix an error message in iscsi_check_key() scsi: ufs: core: Add soft dependency on governor_simpleondemand scsi: lpfc: Avoid usage of list iterator variable after loop net: usb: cdc_mbim: avoid altsetting toggling for Telit FE990 net: usb: qmi_wwan: add Telit 0x1080 composition sh: sanitize the flags on sigreturn cifs: empty interface list when server doesn't support query interfaces scsi: core: Add BLIST_SKIP_VPD_PAGES for SKhynix H28U74301AMR usb: gadget: u_audio: don't let userspace block driver unbind fsverity: Remove WQ_UNBOUND from fsverity read workqueue igb: revert rtnl_lock() that causes deadlock dm thin: fix deadlock when swapping to thin device usb: cdns3: Fix issue with using incorrect PCI device function usb: chipdea: core: fix return -EINVAL if request role is the same with current role usb: chipidea: core: fix possible concurrent when switch role wifi: mac80211: fix qos on mesh interfaces nilfs2: fix kernel-infoleak in nilfs_ioctl_wrap_copy() i2c: xgene-slimpro: Fix out-of-bounds bug in xgene_slimpro_i2c_xfer() dm stats: check for and propagate alloc_percpu failure dm crypt: add cond_resched() to dmcrypt_write() sched/fair: sanitize vruntime of entity being placed sched/fair: Sanitize vruntime of entity being migrated tun: avoid double free in tun_free_netdev ocfs2: fix data corruption after failed write fsverity: don't drop pagecache at end of FS_IOC_ENABLE_VERITY bus: imx-weim: fix branch condition evaluates to a garbage value md: avoid signed overflow in slot_store() ALSA: asihpi: check pao in control_message() ALSA: hda/ca0132: fixup buffer overrun at tuning_ctl_set() fbdev: tgafb: Fix potential divide by zero sched_getaffinity: don't assume 'cpumask_size()' is fully initialized fbdev: nvidia: Fix potential divide by zero fbdev: intelfb: Fix potential divide by zero fbdev: lxfb: Fix potential divide by zero fbdev: au1200fb: Fix potential divide by zero ca8210: Fix unsigned mac_len comparison with zero in ca8210_skb_tx() dma-mapping: drop the dev argument to arch_sync_dma_for_* mips: bmips: BCM6358: disable RAC flush for TP1 mtd: rawnand: meson: invalidate cache on polling ECC bit scsi: megaraid_sas: Fix crash after a double completion ptp_qoriq: fix memory leak in probe() regulator: fix spelling mistake "Cant" -> "Can't" regulator: Handle deferred clk net/net_failover: fix txq exceeding warning can: bcm: bcm_tx_setup(): fix KMSAN uninit-value in vfs_write s390/vfio-ap: fix memory leak in vfio_ap device driver i40e: fix registers dump after run ethtool adapter self test bnxt_en: Fix typo in PCI id to device description string mapping net: dsa: mv88e6xxx: Enable IGMP snooping on user ports only net: mvneta: make tx buffer array agnostic pinctrl: ocelot: Fix alt mode for ocelot Input: alps - fix compatibility with -funsigned-char Input: focaltech - use explicitly signed char type cifs: prevent infinite recursion in CIFSGetDFSRefer() cifs: fix DFS traversal oops without CONFIG_CIFS_DFS_UPCALL Input: goodix - add Lenovo Yoga Book X90F to nine_bytes_report DMI table xen/netback: don't do grant copy across page boundary pinctrl: at91-pio4: fix domain name assignment NFSv4: Fix hangs when recovering open state after a server reboot ALSA: hda/conexant: Partial revert of a quirk for Lenovo ALSA: usb-audio: Fix regression on detection of Roland VS-100 drm/etnaviv: fix reference leak when mmaping imported buffer s390/uaccess: add missing earlyclobber annotations to __clear_user() btrfs: scan device in non-exclusive mode ext4: fix kernel BUG in 'ext4_write_inline_data_end()' net_sched: add __rcu annotation to netdev->qdisc net: sched: fix race condition in qdisc_graft() firmware: arm_scmi: Fix device node validation for mailbox transport gfs2: Always check inode size of inline inodes Linux 5.4.240 Change-Id: Ibe603c6cdf434feacfd91e87ba359dc544223a21 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
302 lines
9.5 KiB
C
302 lines
9.5 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* fs/verity/verify.c: data verification functions, i.e. hooks for ->readpages()
|
|
*
|
|
* Copyright 2019 Google LLC
|
|
*/
|
|
|
|
#include "fsverity_private.h"
|
|
|
|
#include <crypto/hash.h>
|
|
#include <linux/bio.h>
|
|
#include <linux/ratelimit.h>
|
|
|
|
static struct workqueue_struct *fsverity_read_workqueue;
|
|
|
|
/**
|
|
* hash_at_level() - compute the location of the block's hash at the given level
|
|
*
|
|
* @params: (in) the Merkle tree parameters
|
|
* @dindex: (in) the index of the data block being verified
|
|
* @level: (in) the level of hash we want (0 is leaf level)
|
|
* @hindex: (out) the index of the hash block containing the wanted hash
|
|
* @hoffset: (out) the byte offset to the wanted hash within the hash block
|
|
*/
|
|
static void hash_at_level(const struct merkle_tree_params *params,
|
|
pgoff_t dindex, unsigned int level, pgoff_t *hindex,
|
|
unsigned int *hoffset)
|
|
{
|
|
pgoff_t position;
|
|
|
|
/* Offset of the hash within the level's region, in hashes */
|
|
position = dindex >> (level * params->log_arity);
|
|
|
|
/* Index of the hash block in the tree overall */
|
|
*hindex = params->level_start[level] + (position >> params->log_arity);
|
|
|
|
/* Offset of the wanted hash (in bytes) within the hash block */
|
|
*hoffset = (position & ((1 << params->log_arity) - 1)) <<
|
|
(params->log_blocksize - params->log_arity);
|
|
}
|
|
|
|
/* Extract a hash from a hash page */
|
|
static void extract_hash(struct page *hpage, unsigned int hoffset,
|
|
unsigned int hsize, u8 *out)
|
|
{
|
|
void *virt = kmap_atomic(hpage);
|
|
|
|
memcpy(out, virt + hoffset, hsize);
|
|
kunmap_atomic(virt);
|
|
}
|
|
|
|
static inline int cmp_hashes(const struct fsverity_info *vi,
|
|
const u8 *want_hash, const u8 *real_hash,
|
|
pgoff_t index, int level)
|
|
{
|
|
const unsigned int hsize = vi->tree_params.digest_size;
|
|
|
|
if (memcmp(want_hash, real_hash, hsize) == 0)
|
|
return 0;
|
|
|
|
fsverity_err(vi->inode,
|
|
"FILE CORRUPTED! index=%lu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
|
|
index, level,
|
|
vi->tree_params.hash_alg->name, hsize, want_hash,
|
|
vi->tree_params.hash_alg->name, hsize, real_hash);
|
|
return -EBADMSG;
|
|
}
|
|
|
|
/*
|
|
* Verify a single data page against the file's Merkle tree.
|
|
*
|
|
* In principle, we need to verify the entire path to the root node. However,
|
|
* for efficiency the filesystem may cache the hash pages. Therefore we need
|
|
* only ascend the tree until an already-verified page is seen, as indicated by
|
|
* the PageChecked bit being set; then verify the path to that page.
|
|
*
|
|
* This code currently only supports the case where the verity block size is
|
|
* equal to PAGE_SIZE. Doing otherwise would be possible but tricky, since we
|
|
* wouldn't be able to use the PageChecked bit.
|
|
*
|
|
* Note that multiple processes may race to verify a hash page and mark it
|
|
* Checked, but it doesn't matter; the result will be the same either way.
|
|
*
|
|
* Return: true if the page is valid, else false.
|
|
*/
|
|
static bool verify_page(struct inode *inode, const struct fsverity_info *vi,
|
|
struct ahash_request *req, struct page *data_page,
|
|
unsigned long level0_ra_pages)
|
|
{
|
|
const struct merkle_tree_params *params = &vi->tree_params;
|
|
const unsigned int hsize = params->digest_size;
|
|
const pgoff_t index = data_page->index;
|
|
int level;
|
|
u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
|
|
const u8 *want_hash;
|
|
u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
|
|
struct page *hpages[FS_VERITY_MAX_LEVELS];
|
|
unsigned int hoffsets[FS_VERITY_MAX_LEVELS];
|
|
int err;
|
|
|
|
if (WARN_ON_ONCE(!PageLocked(data_page) || PageUptodate(data_page)))
|
|
return false;
|
|
|
|
pr_debug_ratelimited("Verifying data page %lu...\n", index);
|
|
|
|
/*
|
|
* Starting at the leaf level, ascend the tree saving hash pages along
|
|
* the way until we find a verified hash page, indicated by PageChecked;
|
|
* or until we reach the root.
|
|
*/
|
|
for (level = 0; level < params->num_levels; level++) {
|
|
pgoff_t hindex;
|
|
unsigned int hoffset;
|
|
struct page *hpage;
|
|
|
|
hash_at_level(params, index, level, &hindex, &hoffset);
|
|
|
|
pr_debug_ratelimited("Level %d: hindex=%lu, hoffset=%u\n",
|
|
level, hindex, hoffset);
|
|
|
|
hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode, hindex,
|
|
level == 0 ? level0_ra_pages : 0);
|
|
if (IS_ERR(hpage)) {
|
|
err = PTR_ERR(hpage);
|
|
fsverity_err(inode,
|
|
"Error %d reading Merkle tree page %lu",
|
|
err, hindex);
|
|
goto out;
|
|
}
|
|
|
|
if (PageChecked(hpage)) {
|
|
extract_hash(hpage, hoffset, hsize, _want_hash);
|
|
want_hash = _want_hash;
|
|
put_page(hpage);
|
|
pr_debug_ratelimited("Hash page already checked, want %s:%*phN\n",
|
|
params->hash_alg->name,
|
|
hsize, want_hash);
|
|
goto descend;
|
|
}
|
|
pr_debug_ratelimited("Hash page not yet checked\n");
|
|
hpages[level] = hpage;
|
|
hoffsets[level] = hoffset;
|
|
}
|
|
|
|
want_hash = vi->root_hash;
|
|
pr_debug("Want root hash: %s:%*phN\n",
|
|
params->hash_alg->name, hsize, want_hash);
|
|
descend:
|
|
/* Descend the tree verifying hash pages */
|
|
for (; level > 0; level--) {
|
|
struct page *hpage = hpages[level - 1];
|
|
unsigned int hoffset = hoffsets[level - 1];
|
|
|
|
err = fsverity_hash_page(params, inode, req, hpage, real_hash);
|
|
if (err)
|
|
goto out;
|
|
err = cmp_hashes(vi, want_hash, real_hash, index, level - 1);
|
|
if (err)
|
|
goto out;
|
|
SetPageChecked(hpage);
|
|
extract_hash(hpage, hoffset, hsize, _want_hash);
|
|
want_hash = _want_hash;
|
|
put_page(hpage);
|
|
pr_debug("Verified hash page at level %d, now want %s:%*phN\n",
|
|
level - 1, params->hash_alg->name, hsize, want_hash);
|
|
}
|
|
|
|
/* Finally, verify the data page */
|
|
err = fsverity_hash_page(params, inode, req, data_page, real_hash);
|
|
if (err)
|
|
goto out;
|
|
err = cmp_hashes(vi, want_hash, real_hash, index, -1);
|
|
out:
|
|
for (; level > 0; level--)
|
|
put_page(hpages[level - 1]);
|
|
|
|
return err == 0;
|
|
}
|
|
|
|
/**
|
|
* fsverity_verify_page() - verify a data page
|
|
* @page: the page to verity
|
|
*
|
|
* Verify a page that has just been read from a verity file. The page must be a
|
|
* pagecache page that is still locked and not yet uptodate.
|
|
*
|
|
* Return: true if the page is valid, else false.
|
|
*/
|
|
bool fsverity_verify_page(struct page *page)
|
|
{
|
|
struct inode *inode = page->mapping->host;
|
|
const struct fsverity_info *vi = inode->i_verity_info;
|
|
struct ahash_request *req;
|
|
bool valid;
|
|
|
|
/* This allocation never fails, since it's mempool-backed. */
|
|
req = fsverity_alloc_hash_request(vi->tree_params.hash_alg, GFP_NOFS);
|
|
|
|
valid = verify_page(inode, vi, req, page, 0);
|
|
|
|
fsverity_free_hash_request(vi->tree_params.hash_alg, req);
|
|
|
|
return valid;
|
|
}
|
|
EXPORT_SYMBOL_GPL(fsverity_verify_page);
|
|
|
|
#ifdef CONFIG_BLOCK
|
|
/**
|
|
* fsverity_verify_bio() - verify a 'read' bio that has just completed
|
|
* @bio: the bio to verify
|
|
*
|
|
* Verify a set of pages that have just been read from a verity file. The pages
|
|
* must be pagecache pages that are still locked and not yet uptodate. Pages
|
|
* that fail verification are set to the Error state. Verification is skipped
|
|
* for pages already in the Error state, e.g. due to fscrypt decryption failure.
|
|
*
|
|
* This is a helper function for use by the ->readpages() method of filesystems
|
|
* that issue bios to read data directly into the page cache. Filesystems that
|
|
* populate the page cache without issuing bios (e.g. non block-based
|
|
* filesystems) must instead call fsverity_verify_page() directly on each page.
|
|
* All filesystems must also call fsverity_verify_page() on holes.
|
|
*/
|
|
void fsverity_verify_bio(struct bio *bio)
|
|
{
|
|
struct inode *inode = bio_first_page_all(bio)->mapping->host;
|
|
const struct fsverity_info *vi = inode->i_verity_info;
|
|
const struct merkle_tree_params *params = &vi->tree_params;
|
|
struct ahash_request *req;
|
|
struct bio_vec *bv;
|
|
struct bvec_iter_all iter_all;
|
|
unsigned long max_ra_pages = 0;
|
|
|
|
/* This allocation never fails, since it's mempool-backed. */
|
|
req = fsverity_alloc_hash_request(params->hash_alg, GFP_NOFS);
|
|
|
|
if (bio->bi_opf & REQ_RAHEAD) {
|
|
/*
|
|
* If this bio is for data readahead, then we also do readahead
|
|
* of the first (largest) level of the Merkle tree. Namely,
|
|
* when a Merkle tree page is read, we also try to piggy-back on
|
|
* some additional pages -- up to 1/4 the number of data pages.
|
|
*
|
|
* This improves sequential read performance, as it greatly
|
|
* reduces the number of I/O requests made to the Merkle tree.
|
|
*/
|
|
bio_for_each_segment_all(bv, bio, iter_all)
|
|
max_ra_pages++;
|
|
max_ra_pages /= 4;
|
|
}
|
|
|
|
bio_for_each_segment_all(bv, bio, iter_all) {
|
|
struct page *page = bv->bv_page;
|
|
unsigned long level0_index = page->index >> params->log_arity;
|
|
unsigned long level0_ra_pages =
|
|
min(max_ra_pages, params->level0_blocks - level0_index);
|
|
|
|
if (!PageError(page) &&
|
|
!verify_page(inode, vi, req, page, level0_ra_pages))
|
|
SetPageError(page);
|
|
}
|
|
|
|
fsverity_free_hash_request(params->hash_alg, req);
|
|
}
|
|
EXPORT_SYMBOL_GPL(fsverity_verify_bio);
|
|
#endif /* CONFIG_BLOCK */
|
|
|
|
/**
|
|
* fsverity_enqueue_verify_work() - enqueue work on the fs-verity workqueue
|
|
* @work: the work to enqueue
|
|
*
|
|
* Enqueue verification work for asynchronous processing.
|
|
*/
|
|
void fsverity_enqueue_verify_work(struct work_struct *work)
|
|
{
|
|
queue_work(fsverity_read_workqueue, work);
|
|
}
|
|
EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
|
|
|
|
int __init fsverity_init_workqueue(void)
|
|
{
|
|
/*
|
|
* Use a high-priority workqueue to prioritize verification work, which
|
|
* blocks reads from completing, over regular application tasks.
|
|
*
|
|
* For performance reasons, don't use an unbound workqueue. Using an
|
|
* unbound workqueue for crypto operations causes excessive scheduler
|
|
* latency on ARM64.
|
|
*/
|
|
fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
|
|
WQ_HIGHPRI,
|
|
num_online_cpus());
|
|
if (!fsverity_read_workqueue)
|
|
return -ENOMEM;
|
|
return 0;
|
|
}
|
|
|
|
void __init fsverity_exit_workqueue(void)
|
|
{
|
|
destroy_workqueue(fsverity_read_workqueue);
|
|
fsverity_read_workqueue = NULL;
|
|
}
|