From patchwork Thu Oct 4 20:30:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Garrett X-Patchwork-Id: 10626663 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 24310184E for ; Thu, 4 Oct 2018 20:30:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 189C32972C for ; Thu, 4 Oct 2018 20:30:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D31B2972F; Thu, 4 Oct 2018 20:30:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A13E72972C for ; Thu, 4 Oct 2018 20:30:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727805AbeJEDZJ (ORCPT ); Thu, 4 Oct 2018 23:25:09 -0400 Received: from mail-qt1-f201.google.com ([209.85.160.201]:49592 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727802AbeJEDZI (ORCPT ); Thu, 4 Oct 2018 23:25:08 -0400 Received: by mail-qt1-f201.google.com with SMTP id f20-v6so9677037qta.16 for ; Thu, 04 Oct 2018 13:30:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=2xzpPNyQptjlKIAzX7rJhGAFIT47yBSuCeTxyClfXio=; b=WBT2lrNK0d4AUYRzPzVSVuv8rm9DULRFWzknjovgSbUzjevTEjWNIB7M7G28pGg49X hLtRtjRe9gRoXwYZTwU6VZO+NtkHA0nS3zNCOWQfLx6yvPGF8V2MxxjFSwc3berPq9nf nzP6AoK54ZOYgn8T8Vc/R4sXttMlxjPCNEKnLe266aEwhDR3MxseTD1rrRRgr2ARPPHu H5NnIEFLu65v1yPuEivoPdC0RZT6KNL0uy6tfVaBXOAnIF85LsVOVXyU7iGjKwjawpg/ yWgJMi7G9fQuEqra/KQhoz3WBOAEJNxYI9uvEbSivbPivtjwjq/De59INeM+Yi+XQkGV Fanw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=2xzpPNyQptjlKIAzX7rJhGAFIT47yBSuCeTxyClfXio=; b=N73G6FkaJeuhehEymgakN7KFAVyHS9r5dqXUjeQzwYFcGUSam2rmq4x/5yEDZZUJEY f3p0yyKstnecXHnCKoOlCdLMfInm468JIi4DEv4QEPlrVsdO5PBz/f6Hvdi8KQ5pQ26r zFE90GX5OwnbDGmjD7gVMVvk5Qyp4xkIpg//ca+20+cq9ahCkwBQWmj4NK0Thx1CYb+a CcppeuvFEeYOvA2322YGIiqTsKZwgTx/V3T7/DBS4weY6nTnMGiGDLm4eAxyB7bXdtXD ZqvikYzvid4DtnElQKLYr8r9dCwW420iAxi6t75JL3FPIGPc5qVJ4Q4AbWAO1iJZWK7W 7QpQ== X-Gm-Message-State: ABuFfohIFLWal0wUORLExETaP6wA6qFdPET1uyl5BKZs2jQVAJsCGl6y L+E2RlX3aj0esWt4p80J2yHEXgmhmCpaV49vwaUDnQ== X-Google-Smtp-Source: ACcGV62TovjgWMyI7yIlxZpURBhiPuuOZMsWRaZ8CiMfQ0F0kcM7ZVAiRyAwgTcX4G9l7ORUv4JJNIlsYnNGN55pR932Cw== X-Received: by 2002:aed:25b0:: with SMTP id x45-v6mr6169289qtc.4.1538685013277; Thu, 04 Oct 2018 13:30:13 -0700 (PDT) Date: Thu, 4 Oct 2018 13:30:05 -0700 In-Reply-To: <20181004203007.217320-1-mjg59@google.com> Message-Id: <20181004203007.217320-2-mjg59@google.com> Mime-Version: 1.0 References: <20181004203007.217320-1-mjg59@google.com> X-Mailer: git-send-email 2.19.0.605.g01d371f741-goog Subject: [PATCH 1/3] VFS: Add a call to obtain a file's hash From: Matthew Garrett To: linux-integrity@vger.kernel.org Cc: zohar@linux.vnet.ibm.com, dmitry.kasatkin@gmail.com, miklos@szeredi.hu, linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk, Matthew Garrett Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP IMA wants to know what the hash of a file is, and currently does so by reading the entire file and generating the hash. Some filesystems may have the ability to store the hash in a secure manner resistant to offline attacks (eg, filesystem-level file signing), and in that case it's a performance win for IMA to be able to use that rather than having to re-hash everything. This patch simply adds VFS-level support for calling down to filesystems. Signed-off-by: Matthew Garrett --- fs/read_write.c | 24 ++++++++++++++++++++++++ include/linux/fs.h | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/fs/read_write.c b/fs/read_write.c index 39b4a21dd933..9ba3ce4bb838 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -2081,3 +2081,27 @@ int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same) return ret; } EXPORT_SYMBOL(vfs_dedupe_file_range); + +/** + * vfs_gethash - obtain a file's hash + * @file: file structure in question + * @hash_algo: the hash algorithm requested + * @buf: buffer to return the hash in + * @size: size allocated for the buffer by the caller + * + * This function allows filesystems that support securely storing the hash + * of a file to return it rather than forcing the kernel to recalculate it. + * Filesystems that cannot provide guarantees about the hash being resistant + * to offline attack should not implement this functionality. + * + * Returns 0 on success, -EOPNOTSUPP if the filesystem doesn't support it. + */ +int vfs_get_hash(struct file *file, enum hash_algo hash, uint8_t *buf, + size_t size) +{ + if (!file->f_op->get_hash) + return -EOPNOTSUPP; + + return file->f_op->get_hash(file, hash, buf, size); +} +EXPORT_SYMBOL(vfs_get_hash); diff --git a/include/linux/fs.h b/include/linux/fs.h index 6c0b4a1c22ff..540316cfd461 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -40,6 +40,7 @@ #include #include +#include struct backing_dev_info; struct bdi_writeback; @@ -1764,6 +1765,8 @@ struct file_operations { int (*dedupe_file_range)(struct file *, loff_t, struct file *, loff_t, u64); int (*fadvise)(struct file *, loff_t, loff_t, int); + int (*get_hash)(struct file *, enum hash_algo hash, uint8_t *buf, + size_t size); } __randomize_layout; struct inode_operations { @@ -1838,7 +1841,8 @@ extern int vfs_dedupe_file_range(struct file *file, extern int vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos, struct file *dst_file, loff_t dst_pos, u64 len); - +extern int vfs_get_hash(struct file *file, enum hash_algo hash, uint8_t *buf, + size_t size); struct super_operations { struct inode *(*alloc_inode)(struct super_block *sb);