From patchwork Mon Oct 23 21:40:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10023201 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E1FE9603D7 for ; Mon, 23 Oct 2017 21:43:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D380228921 for ; Mon, 23 Oct 2017 21:43:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C854E28938; Mon, 23 Oct 2017 21:43:11 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 5635F28921 for ; Mon, 23 Oct 2017 21:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932300AbdJWVnI (ORCPT ); Mon, 23 Oct 2017 17:43:08 -0400 Received: from mail-io0-f194.google.com ([209.85.223.194]:50629 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932221AbdJWVmi (ORCPT ); Mon, 23 Oct 2017 17:42:38 -0400 Received: by mail-io0-f194.google.com with SMTP id 97so21701422iok.7; Mon, 23 Oct 2017 14:42:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6bj+3PpKOEqnjjCfFytXcJdfqep6Tsq3tGn/CH2pnfU=; b=mOtC1K5uD1IQOUkDxmkg8LOeQg964c87AtZbg/MnR72YOTSWhPNgiIvWAoP3E5A3gy Xj7xm0j9Isa07F6HzOZyvz5v3fWGtqI1FBF+O/TLub2msFrJno9ZwtLb3KKmnycQl3gV eYcQEPqY2MXArBUjPt5WdiM89gCVnD/zqdx4ru8hotZqPhr3NOVBEFOLpDRTr7ELotIn alOkLbiBr96OnqNlsVXE4CMQm85lEFKz8HCPi6UsJzej6n5uxNGQSQq343Bs1VZqVXw9 b5nOiyyXdsTMkF4r2YqUC2P4RdDZBrg2A2ZPq11/P9L3b8wUfy9/jaF0rIWyFxF6Cvf2 qz4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6bj+3PpKOEqnjjCfFytXcJdfqep6Tsq3tGn/CH2pnfU=; b=KtZt6fN73fOAkJFKl3FCBCYB/ZpHFR5kbVapSXrj/E9QOVKjLC8NSoMFIN/EXI0T8y uAyulXInVevLPX6b6hbLWc5v1saMYmEFjoM3xEa8w3+ncCA4Wly1TSNZ4l+MYIU02QqF Rp70jksjinMLZ6tfdfLWhg7cRUHnNzZVQzEqLbo/4rX4gW7m6ykil8ZQDVVqGAmW7lEd 6lVA+9sfywueBBocEnJGhYENAUAB+ahPn1JefPrrRdORqmHiGoLUQ50xcqkffBkreQut 3hp4MrX5xWHfTJ/jkC9pxOLxXACgvPtZHGC9eyiVM8fAIf8sHHYXIX+3tvyOFWg6YNFq d3cg== X-Gm-Message-State: AMCzsaX1rD0KqIVQd0aGulQTjgtG0nXOjSIaEAr5GxUXlqCPlDodCY4K QOVW4Nhf0r9P5zxiMoI2AuRw5OHw X-Google-Smtp-Source: ABhQp+RH1Lyy/9yjcppo1NO0unSUK8en9i9rBXkfes1gjNC2HRCS9wOoUhBH+z8Uj6NbiIiqVyf2dw== X-Received: by 10.107.10.82 with SMTP id u79mr19424303ioi.252.1508794957596; Mon, 23 Oct 2017 14:42:37 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.175.88]) by smtp.gmail.com with ESMTPSA id i63sm3558482ioi.68.2017.10.23.14.42.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Oct 2017 14:42:37 -0700 (PDT) From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, linux-api@vger.kernel.org, keyrings@vger.kernel.org, "Theodore Y . Ts'o" , Jaegeuk Kim , Gwendal Grignou , Ryo Hashimoto , Sarthak Kukreti , Nick Desaulniers , Michael Halcrow , Eric Biggers Subject: [RFC PATCH 21/25] fscrypt: require that key be added when setting a v2 encryption policy Date: Mon, 23 Oct 2017 14:40:54 -0700 Message-Id: <20171023214058.128121-22-ebiggers3@gmail.com> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog In-Reply-To: <20171023214058.128121-1-ebiggers3@gmail.com> References: <20171023214058.128121-1-ebiggers3@gmail.com> 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 From: Eric Biggers By looking up the master keys in a filesystem-level keyring rather than in the calling processes' key hierarchy, it becomes possible for a user to set an encryption policy which refers to some key they don't actually know, then encrypt their files using that key. Cryptographically this shouldn't actually be a major problem; for one, every file will still be encrypted with a unique derived key, rather than with the master key directly. But to be on the safe side, enforce that a v2 encryption policy can only be set if the user has previously added the key, or has capable(CAP_FOWNER). We tolerate that this problem will continue to exist for v1 encryption policies, however; there is no way around that. Signed-off-by: Eric Biggers --- fs/crypto/fscrypt_private.h | 2 ++ fs/crypto/keyinfo.c | 42 ++++++++++++++++++++++++++++++++++++++++++ fs/crypto/policy.c | 6 ++++++ 3 files changed, 50 insertions(+) diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index d0a63086fa95..7a0d5b6c2504 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h @@ -234,6 +234,8 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx, gfp_t gfp_flags); /* keyinfo.c */ +extern int fscrypt_verify_key_added(struct super_block *sb, + const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); extern struct key_type key_type_fscrypt_mk; extern struct key_type key_type_fscrypt_mk_user; extern void __exit fscrypt_essiv_cleanup(void); diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c index 1fe44983239a..fd59f37dad10 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c @@ -851,6 +851,48 @@ int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg) } EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key); +/* + * Verify that the current user has added a master key that has the given + * identifier (returns -ENOKEY if not). This is needed to prevent a user from + * encrypting their files using some other user's key which they don't actually + * know. Cryptographically speaking, it's debatable how much of a problem this + * actually would be, but it's best to just forbid it. + * + * The system administrator (CAP_FOWNER) can override this, which should be + * enough for any use cases where encryption policies are being set using keys + * that were chosen ahead of time but aren't available at the moment. + */ +int fscrypt_verify_key_added(struct super_block *sb, + const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]) +{ + struct fscrypt_key_specifier mk_spec; + struct key *key, *mk_user; + struct fscrypt_master_key *mk; + int err; + + mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; + memcpy(mk_spec.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); + + key = find_master_key(sb, &mk_spec); + if (IS_ERR(key)) { + err = PTR_ERR(key); + goto out; + } + mk = key->payload.data[0]; + mk_user = find_master_key_user(mk); + if (IS_ERR(mk_user)) { + err = PTR_ERR(mk_user); + } else { + key_put(mk_user); + err = 0; + } + key_put(key); +out: + if (err == -ENOKEY && capable(CAP_FOWNER)) + err = 0; + return err; +} + static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk) { struct fscrypt_info *ci; diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index 27a391038f73..cfb404def9ed 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c @@ -170,6 +170,7 @@ static int set_encryption_policy(struct inode *inode, const union fscrypt_policy *policy) { union fscrypt_context ctx; + int err; if (!fscrypt_supported_policy(policy)) return -EINVAL; @@ -190,6 +191,11 @@ static int set_encryption_policy(struct inode *inode, */ pr_warn_once("%s (pid %d) is setting less secure v1 encryption policy; recommend upgrading to v2.\n", current->comm, current->pid); + } else { + err = fscrypt_verify_key_added(inode->i_sb, + policy->v2.master_key_identifier); + if (err) + return err; } return inode->i_sb->s_cop->set_context(inode, &ctx,