* 'upstream-f2fs-stable-linux-4.19.y' of https://android.googlesource.com/kernel/common: (560 commits) f2fs: reset wait_ms to default if any of the victims have been selected f2fs: fix some format WARNING in debug.c and sysfs.c f2fs: don't call f2fs_issue_discard_timeout() when discard_cmd_cnt is 0 in f2fs_put_super() f2fs: fix iostat parameter for discard f2fs: Fix spelling mistake in label: free_bio_enrty_cache -> free_bio_entry_cache f2fs: avoid build warnining in extent_cache f2fs: add block_age-based extent cache f2fs: allocate the extent_cache by default f2fs: refactor extent_cache to support for read and more f2fs: remove unnecessary __init_extent_tree f2fs: move internal functions into extent_cache.c f2fs: specify extent cache for read explicitly f2fs: introduce f2fs_is_readonly() for readability f2fs: remove F2FS_SET_FEATURE() and F2FS_CLEAR_FEATURE() macro f2fs: do some cleanup for f2fs module init MAINTAINERS: Add f2fs bug tracker link f2fs: remove the unused flush argument to change_curseg f2fs: open code allocate_segment_by_default f2fs: remove struct segment_allocation default_salloc_ops f2fs: introduce discard_urgent_util sysfs node ... Conflicts: fs/crypto/hooks.c fs/ext4/ioctl.c fs/ext4/namei.c fs/f2fs/checkpoint.c fs/f2fs/data.c fs/f2fs/dir.c fs/f2fs/f2fs.h fs/f2fs/file.c fs/f2fs/gc.c fs/f2fs/hash.c fs/f2fs/inline.c fs/f2fs/namei.c fs/f2fs/node.c fs/f2fs/node.h fs/f2fs/recovery.c fs/f2fs/segment.c fs/f2fs/segment.h fs/f2fs/super.c fs/f2fs/sysfs.c fs/f2fs/xattr.c fs/libfs.c fs/ubifs/dir.c fs/unicode/utf8-core.c fs/verity/enable.c fs/verity/signature.c include/linux/fs.h include/linux/fscrypt.h include/uapi/linux/fsverity.h Change-Id: I555b2ac03d0bc864b8993a006994c68c0f4f8c41
204 lines
5.7 KiB
C
204 lines
5.7 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Verification of builtin signatures
|
|
*
|
|
* Copyright 2019 Google LLC
|
|
*/
|
|
|
|
#include "fsverity_private.h"
|
|
|
|
#include <linux/cred.h>
|
|
#include <linux/key.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/verification.h>
|
|
|
|
/*
|
|
* /proc/sys/fs/verity/require_signatures
|
|
* If 1, all verity files must have a valid builtin signature.
|
|
*/
|
|
static int fsverity_require_signatures;
|
|
|
|
/*
|
|
* Keyring that contains the trusted X.509 certificates.
|
|
*
|
|
* Only root (kuid=0) can modify this. Also, root may use
|
|
* keyctl_restrict_keyring() to prevent any more additions.
|
|
*/
|
|
static struct key *fsverity_keyring;
|
|
|
|
/**
|
|
* fsverity_verify_signature() - check a verity file's signature
|
|
* @vi: the file's fsverity_info
|
|
* @signature: the file's built-in signature
|
|
* @sig_size: size of signature in bytes, or 0 if no signature
|
|
*
|
|
* If the file includes a signature of its fs-verity file digest, verify it
|
|
* against the certificates in the fs-verity keyring.
|
|
*
|
|
* Return: 0 on success (signature valid or not required); -errno on failure
|
|
*/
|
|
int fsverity_verify_signature(const struct fsverity_info *vi,
|
|
const u8 *signature, size_t sig_size)
|
|
{
|
|
unsigned int digest_algorithm =
|
|
vi->tree_params.hash_alg - fsverity_hash_algs;
|
|
|
|
return __fsverity_verify_signature(vi->inode, signature, sig_size,
|
|
vi->file_digest, digest_algorithm);
|
|
}
|
|
|
|
/**
|
|
* __fsverity_verify_signature() - check a verity file's signature
|
|
* @inode: the file's inode
|
|
* @signature: the file's signature
|
|
* @sig_size: size of @signature. Can be 0 if there is no signature
|
|
* @file_digest: the file's digest
|
|
* @digest_algorithm: the digest algorithm used
|
|
*
|
|
* Takes the file's digest and optional signature and verifies the signature
|
|
* against the digest and the fs-verity keyring if appropriate
|
|
*
|
|
* Return: 0 on success (signature valid or not required); -errno on failure
|
|
*/
|
|
int __fsverity_verify_signature(const struct inode *inode, const u8 *signature,
|
|
u32 sig_size, const u8 *file_digest,
|
|
unsigned int digest_algorithm)
|
|
{
|
|
struct fsverity_formatted_digest *d;
|
|
struct fsverity_hash_alg *hash_alg = fsverity_get_hash_alg(inode,
|
|
digest_algorithm);
|
|
int err;
|
|
|
|
if (IS_ERR(hash_alg))
|
|
return PTR_ERR(hash_alg);
|
|
|
|
if (sig_size == 0) {
|
|
if (fsverity_require_signatures) {
|
|
fsverity_err(inode,
|
|
"require_signatures=1, rejecting unsigned file!");
|
|
return -EPERM;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
|
|
/*
|
|
* The ".fs-verity" keyring is empty, due to builtin signatures
|
|
* being supported by the kernel but not actually being used.
|
|
* In this case, verify_pkcs7_signature() would always return an
|
|
* error, usually ENOKEY. It could also be EBADMSG if the
|
|
* PKCS#7 is malformed, but that isn't very important to
|
|
* distinguish. So, just skip to ENOKEY to avoid the attack
|
|
* surface of the PKCS#7 parser, which would otherwise be
|
|
* reachable by any task able to execute FS_IOC_ENABLE_VERITY.
|
|
*/
|
|
fsverity_err(inode,
|
|
"fs-verity keyring is empty, rejecting signed file!");
|
|
return -ENOKEY;
|
|
}
|
|
|
|
d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
|
|
if (!d)
|
|
return -ENOMEM;
|
|
memcpy(d->magic, "FSVerity", 8);
|
|
d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
|
|
d->digest_size = cpu_to_le16(hash_alg->digest_size);
|
|
memcpy(d->digest, file_digest, hash_alg->digest_size);
|
|
|
|
err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
|
|
signature, sig_size, fsverity_keyring,
|
|
VERIFYING_UNSPECIFIED_SIGNATURE,
|
|
NULL, NULL);
|
|
kfree(d);
|
|
|
|
if (err) {
|
|
if (err == -ENOKEY)
|
|
fsverity_err(inode,
|
|
"File's signing cert isn't in the fs-verity keyring");
|
|
else if (err == -EKEYREJECTED)
|
|
fsverity_err(inode, "Incorrect file signature");
|
|
else if (err == -EBADMSG)
|
|
fsverity_err(inode, "Malformed file signature");
|
|
else
|
|
fsverity_err(inode, "Error %d verifying file signature",
|
|
err);
|
|
return err;
|
|
}
|
|
|
|
pr_debug("Valid signature for file digest %s:%*phN\n",
|
|
hash_alg->name, hash_alg->digest_size, file_digest);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(__fsverity_verify_signature);
|
|
|
|
#ifdef CONFIG_SYSCTL
|
|
static struct ctl_table_header *fsverity_sysctl_header;
|
|
|
|
static const struct ctl_path fsverity_sysctl_path[] = {
|
|
{ .procname = "fs", },
|
|
{ .procname = "verity", },
|
|
{ }
|
|
};
|
|
|
|
/* shared constants to be used in various sysctls */
|
|
static int sysctl_vals[] = { 0, 1, INT_MAX };
|
|
|
|
#define SYSCTL_ZERO ((void *)&sysctl_vals[0])
|
|
#define SYSCTL_ONE ((void *)&sysctl_vals[1])
|
|
#define SYSCTL_INT_MAX ((void *)&sysctl_vals[2])
|
|
|
|
static struct ctl_table fsverity_sysctl_table[] = {
|
|
{
|
|
.procname = "require_signatures",
|
|
.data = &fsverity_require_signatures,
|
|
.maxlen = sizeof(int),
|
|
.mode = 0644,
|
|
.proc_handler = proc_dointvec_minmax,
|
|
.extra1 = SYSCTL_ZERO,
|
|
.extra2 = SYSCTL_ONE,
|
|
},
|
|
{ }
|
|
};
|
|
|
|
static int __init fsverity_sysctl_init(void)
|
|
{
|
|
fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
|
|
fsverity_sysctl_table);
|
|
if (!fsverity_sysctl_header) {
|
|
pr_err("sysctl registration failed!\n");
|
|
return -ENOMEM;
|
|
}
|
|
return 0;
|
|
}
|
|
#else /* !CONFIG_SYSCTL */
|
|
static inline int __init fsverity_sysctl_init(void)
|
|
{
|
|
return 0;
|
|
}
|
|
#endif /* !CONFIG_SYSCTL */
|
|
|
|
int __init fsverity_init_signature(void)
|
|
{
|
|
struct key *ring;
|
|
int err;
|
|
|
|
ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
|
|
current_cred(), KEY_POS_SEARCH |
|
|
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
|
|
KEY_USR_SEARCH | KEY_USR_SETATTR,
|
|
KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
|
|
if (IS_ERR(ring))
|
|
return PTR_ERR(ring);
|
|
|
|
err = fsverity_sysctl_init();
|
|
if (err)
|
|
goto err_put_ring;
|
|
|
|
fsverity_keyring = ring;
|
|
return 0;
|
|
|
|
err_put_ring:
|
|
key_put(ring);
|
|
return err;
|
|
}
|