From patchwork Thu Apr 18 23:29:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 1087901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="iDMpOOt2"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="UjP2KoLN"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44lb4P08Q8z9s3q for ; Fri, 19 Apr 2019 09:32:37 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jf2+qb70LjBJro1Atb7zf81J1ekiJP9g4TJxmPDuryA=; b=iDMpOOt2axJic6 bvZ2X6BOEg33KHEWGETSM43ngSNiIq1IBy4OuFdkinooG2xWstGNYNX+lHXRpi+h8JwbD4JTGH3/S 03jh3mKMdpTDg9VP8rx10gCQS3+scXSOgOjXpDpBFMDBQk49pa6MjxTxye/zF6kHsl91y6qU2mQcV +ClfGsx7biuit3KjWmBnw3qqPmquQn9tcL+kbSQhi9ecf3symkw3TTva7LusW1SxitvLA5QSrj5wU 6SoPge6PrftJl1m8yvweaaXYJtJq3WU6N0C4kyl6RdTeq4qCUsOYW6vH3cCGzgx4prn4i1nF2LoRY gOF4F50bcvCbT2dJAWOg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hHGW5-0000Uy-9Z; Thu, 18 Apr 2019 23:32:29 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hHGU9-0006Q6-Vv for linux-mtd@lists.infradead.org; Thu, 18 Apr 2019 23:30:40 +0000 Received: from ebiggers-linuxstation.mtv.corp.google.com (unknown [104.132.1.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6BEE2218DA; Thu, 18 Apr 2019 23:30:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555630229; bh=ij5EIlR9cfG+o5QMxm0cgi3zzQAvYhfDjiLkPcBCAak=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UjP2KoLNmeSj15jkKoQeyryKFgVFvjHFj4GK8cZ4EkiJiFFTIJlM4strm3Lx1EKi9 zsxkUms7IE61rt9LiYO95rw/GjG5qpW5J69Q8AwO0MN6DcimDvZfhMNEt+rhihOdWK Kek+xPrgpZjPMzUxZAFKJWyC769r6GW5bjl7jjus= From: Eric Biggers To: linux-fscrypt@vger.kernel.org Subject: [PATCH v5 12/16] fscrypt: require that key be added when setting a v2 encryption policy Date: Thu, 18 Apr 2019 16:29:19 -0700 Message-Id: <20190418232923.250800-13-ebiggers@kernel.org> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog In-Reply-To: <20190418232923.250800-1-ebiggers@kernel.org> References: <20190418232923.250800-1-ebiggers@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190418_163030_406434_0745F96A X-CRM114-Status: GOOD ( 17.13 ) X-Spam-Score: -5.3 (-----) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-5.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at https://www.dnswl.org/, high trust [198.145.29.99 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIMWL_WL_HIGH DKIMwl.org - Whitelisted High sender X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-ext4@vger.kernel.org, linux-api@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, keyrings@vger.kernel.org, linux-mtd@lists.infradead.org, linux-crypto@vger.kernel.org, linux-fsdevel@vger.kernel.org, Satya Tangirala , Paul Crowley Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 isn't much of a problem, but the semantics of this would be a bit weird. Thus, 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 | 3 +++ fs/crypto/keyring.c | 47 +++++++++++++++++++++++++++++++++++++ fs/crypto/policy.c | 6 +++++ 3 files changed, 56 insertions(+) diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index 5f679ba32cece..a398383ae9e34 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h @@ -411,6 +411,9 @@ extern struct key * fscrypt_find_master_key(struct super_block *sb, const struct fscrypt_key_specifier *mk_spec); +extern int fscrypt_verify_key_added(struct super_block *sb, + const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); + extern int __init fscrypt_init_keyring(void); extern void fscrypt_exit_keyring(void); diff --git a/fs/crypto/keyring.c b/fs/crypto/keyring.c index 4809ed061b332..73758c649f620 100644 --- a/fs/crypto/keyring.c +++ b/fs/crypto/keyring.c @@ -562,6 +562,53 @@ 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 with 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 this isn't much of a problem, but the semantics of this + * would be a bit weird, so 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. + * + * Note that the key may have already removed by the time this returns, but + * that's okay; we just care whether the key was there at some point. + * + * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code + */ +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.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); + + key = fscrypt_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 shrink_dcache_inode(struct inode *inode) { struct dentry *dentry; diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index b42e9e3ce8d0f..839ce67581812 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c @@ -211,6 +211,7 @@ static int set_encryption_policy(struct inode *inode, { union fscrypt_context ctx; int ctxsize; + int err; if (!fscrypt_supported_policy(policy)) return -EINVAL; @@ -229,6 +230,11 @@ static int set_encryption_policy(struct inode *inode, */ pr_warn_once("%s (pid %d) is setting deprecated 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; } ctxsize = fscrypt_new_context_from_policy(&ctx, policy);