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: 829689 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=65.50.211.133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="BI7kk1hL"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="mOtC1K5u"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.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 3yLVQD0TNGz9sPr for ; Tue, 24 Oct 2017 08:48:20 +1100 (AEDT) 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:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: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=zTxryStZyQaGOKNprXdhQJY4gSzsTx7/tOxsagFQVyk=; b=BI7kk1hLkM77BXX3M/P7Zm7iKT jmK9Rf0FznNKErA7KluqTPRZcey4j586P7+KvCjzT/j4vT0P24Q0Mk+T7kcObCCRQs8HAh9Cy6oX1 cwyjtVX4WhcNU/fIabLpFzOqcawJQ787nIyOiIvUhqtrTxwRrwypruLsvxUgD4c3Ukg3qcKxz5G2k BqaGqzCb7UofVHijMYfKmjKOE7iMiQrFAwM0ISeL1m/jxl9rWar7eqoeiifa0i/zUhB1TipVY63zz tpIMgwPYeBypWupVctErK2vPgcb4Qf0nVntPgkUyAggFpSqM+4JSoED0M1AGESF3vFmAreYmN7a33 4XaUWiUw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e6kZv-0002Eu-Cz; Mon, 23 Oct 2017 21:48:11 +0000 Received: from mail-io0-x244.google.com ([2607:f8b0:4001:c06::244]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e6kUr-0004lT-Oa for linux-mtd@lists.infradead.org; Mon, 23 Oct 2017 21:43:13 +0000 Received: by mail-io0-x244.google.com with SMTP id 134so21710681ioo.0 for ; 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=plldar5XPeov/p7Q/Wu98mrCTm/7mp6Xqape2OVyOcXN4B6oxVJ8nWelm3YvUNhYx3 2wD6rHMaWRacmnqVCyGdnHaiYVGEa6PwwxqHIflwK+KzEe/zBR3sU2GbXhoHGyuxheQf Vqr6j5Fvqv+7ePZRKLtC+BhYAmnqTnwgo2+y6aA8TfKg6Eup8UCS5lpzf5hveXxQikSa fnAcWfTyXeoUKr/OG9jfDRH6g/F/gd6J+ta/ex9oCOW7WryshT1U65KpZxyV7DN8z2Tt qo7MXyxbMEO42dhn6xXhB0+A0cByyaRFAFblta40kprTtHNzduS7iNO35ucQ/Dgm0j/k a1TA== X-Gm-Message-State: AMCzsaU+CntvDuCql9CJGztZZhKyxBS6kaIVkEmE957W+oMZxBFgJw1z dYxcHvUA6lRlP3i2X9VkS9E= 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 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171023_144258_665438_EC5D1172 X-CRM114-Status: GOOD ( 16.23 ) X-Spam-Score: -1.8 (-) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-1.8 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (ebiggers3[at]gmail.com) 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (ebiggers3[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -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_AU Message has a valid DKIM or DK signature from author's domain 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: Ryo Hashimoto , Gwendal Grignou , "Theodore Y . Ts'o" , Eric Biggers , linux-api@vger.kernel.org, Nick Desaulniers , linux-f2fs-devel@lists.sourceforge.net, keyrings@vger.kernel.org, linux-mtd@lists.infradead.org, Michael Halcrow , Sarthak Kukreti , linux-fsdevel@vger.kernel.org, Jaegeuk Kim , linux-ext4@vger.kernel.org MIME-Version: 1.0 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 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,