From patchwork Fri Jul 26 22:41:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 1137687 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="aiTSoxqK"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="J1Ec19Lu"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45wPQ503Hqz9s3l for ; Sat, 27 Jul 2019 08:48:45 +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=vqR8yyouiviSz+KUiqGUlqfJB9mcyB0EgEVPb6qB1TE=; b=aiTSoxqK8mYBcP bVRxV5Ebs2siXmhUJ3tCXBuOn7oGbouSbTugqPWTJEIBGw3g/0vXnjr43tJzoico9LfpKyTxQBw4x QjyPZF6fy2xrtajWL50KIu0i/LwB07mDGZwYhUah6jClxeUQl10xm+Em7RFnw11JLn5le1arNYGzD RZlj53ttO1JibQgZ/7clGbv6iLUSmfvUXSFGYrXbyO/njMz4amaxhsjKO2UA5hnnyHeSikZBs+3xL A8aqSber8CoumGbFGx1/Jlo+gN/VHxzxQAhpW6aC2fjZByh5ysOeNfZFwidzN6MQzktHUt9wkp01c TXdv9eDMQgI15Oy7bENw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hr90i-0008A8-Oj; Fri, 26 Jul 2019 22:48:24 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hr8yP-0005cN-RF for linux-mtd@lists.infradead.org; Fri, 26 Jul 2019 22:46:05 +0000 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (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 4445422CD3; Fri, 26 Jul 2019 22:46:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564181161; bh=/pgwWqWauoV13Wb69Dw/cWy9U2yjT7ueDPpVC9x+FpY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J1Ec19LumkIBbEia8is06GJU5ld6UTgAKEmZsmBmnHOOWfn5lVJY5Fo6a1j2OEjZN 6qKVg6oqFs1Hg5PfDrexFGgVLOEvoXWiVOnsNqAbfE7iVXkp6njUb2HvIGQLtlbXkE FJ+7rzLYbp0zkY46CMGuyfwDRkELIP4nZJ4O8Uus= From: Eric Biggers To: linux-fscrypt@vger.kernel.org Subject: [PATCH v7 12/16] fscrypt: require that key be added when setting a v2 encryption policy Date: Fri, 26 Jul 2019 15:41:37 -0700 Message-Id: <20190726224141.14044-13-ebiggers@kernel.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190726224141.14044-1-ebiggers@kernel.org> References: <20190726224141.14044-1-ebiggers@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190726_154601_953021_C2534C0B X-CRM114-Status: GOOD ( 16.88 ) X-Spam-Score: -5.2 (-----) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-5.2 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_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 DKIMWL_WL_HIGH DKIMwl.org - Whitelisted High sender X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Satya Tangirala , 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, linux-ext4@vger.kernel.org, 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 Reviewed-by: Theodore Ts'o --- 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 2445b0fc4f01c..8d61f410aa677 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h @@ -408,6 +408,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); /* keysetup.c */ diff --git a/fs/crypto/keyring.c b/fs/crypto/keyring.c index 307533d4d7c51..258409d0c7409 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 0141d338c1fdb..60de9048ed4d1 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c @@ -233,6 +233,7 @@ static int set_encryption_policy(struct inode *inode, { union fscrypt_context ctx; int ctxsize; + int err; if (!fscrypt_supported_policy(policy, inode)) return -EINVAL; @@ -251,6 +252,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);