From patchwork Sun Apr 3 05:22:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 8732911 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 316FFC0554 for ; Sun, 3 Apr 2016 05:24:44 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 57DD72022D for ; Sun, 3 Apr 2016 05:24:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6BD7C20251 for ; Sun, 3 Apr 2016 05:24:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752243AbcDCFY2 (ORCPT ); Sun, 3 Apr 2016 01:24:28 -0400 Received: from mail-ig0-f195.google.com ([209.85.213.195]:35612 "EHLO mail-ig0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752576AbcDCFXs (ORCPT ); Sun, 3 Apr 2016 01:23:48 -0400 Received: by mail-ig0-f195.google.com with SMTP id ya17so7659344igc.2; Sat, 02 Apr 2016 22:23:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OuP4odgV7XeIKI0Ehwra7EdZblcy/vH/9aifqPLWC40=; b=YvjwaD7L2AidUJ31I/hDIN8Efdd65bPHM6ZY+RjY9cgL/bC8ywsdlEaaAIgwn9DWr2 BSv/+0SsWkck2eTV29rkXc+NITyvcZPOV1+BN5JXteWa3LmWhGqYZtVlKvO21JI8SsLY TIbRDLr/DXXsRt3XYAbQB8cQLm35YN19u2Gkmn271M/P4fZvNWGjPFDgYnGDI1OkvWLc FnW304Yu/EexFj1VZRv+3FENBlwZn1K/aJRMI0//lK9OoOfJTiWYSiaqI/sJ1KXyagHr W7mjpFg4/bjdKHG0ApBHTbzddRI5hrWPSt5w8YeuorOjGDr+py0u89XJZtCw7kakOzt1 NrUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OuP4odgV7XeIKI0Ehwra7EdZblcy/vH/9aifqPLWC40=; b=ID9nhIVCAF5lhoE5RLEg2M3blud2L2rbFnkFBhrxYdQUScIyRhAoO80mWPgufsELQK LVm3kcM+UeDmoKhgKaYdxaFu0zSQBfgymUfeq9LPvaGjAhOk5SQOlUdJ/MSIXnPvWErY QyZRQ/xQ5AEAI8LJVM+TRcD9kcB3dE3ggOI+LwbYUzefFyKxBsCQwYXDWfSK7rsWLDMl z3sQvZB7oVXi6q9+0++xy+k/Mj1TMUI5RQ514Snv4boIdzkCe3Wgm+cV4sa91m4eZjmn /pJhM4TiWqoQrHLYsEt+1y1MfVZqe38lL5rbNfw3Pi0qEuXqDWVtWNkT7zarVwhE+s5A wJJg== X-Gm-Message-State: AD7BkJIkpInCDI2Bf8xkrnxn2JEy7VWAlr0xJUYljFR6a51COUB3HVXdrSz+8MEkFtnD+g== X-Received: by 10.50.97.71 with SMTP id dy7mr1575111igb.0.1459661027292; Sat, 02 Apr 2016 22:23:47 -0700 (PDT) Received: from localhost.localdomain (c-24-7-245-123.hsd1.mn.comcast.net. [24.7.245.123]) by smtp.gmail.com with ESMTPSA id je6sm2914954igb.15.2016.04.02.22.23.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 02 Apr 2016 22:23:46 -0700 (PDT) From: Eric Biggers To: linux-fsdevel@vger.kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, jaegeuk@kernel.org, tytso@mit.edu, mhalcrow@google.com, Eric Biggers Subject: [PATCH 13/13] fscrypto: improve error handling in fscrypt_set_policy() Date: Sun, 3 Apr 2016 00:22:04 -0500 Message-Id: <1459660924-2960-14-git-send-email-ebiggers3@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1459660924-2960-1-git-send-email-ebiggers3@gmail.com> References: <1459660924-2960-1-git-send-email-ebiggers3@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In some cases, the previous code did not correctly propagate errors to the caller. Also, only one call to ->get_context() is required. Signed-off-by: Eric Biggers --- fs/crypto/policy.c | 64 +++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index 6a767e6..83421fe 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c @@ -13,37 +13,17 @@ #include #include -static bool inode_has_encryption_context(struct inode *inode) -{ - if (!inode->i_sb->s_cop->get_context) - return false; - return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0); -} - -/* - * check whether the policy is consistent with the encryption context - * for the inode - */ -static bool is_encryption_context_consistent_with_policy(struct inode *inode, +static bool is_encryption_context_consistent_with_policy( + const struct fscrypt_context *ctx, const struct fscrypt_policy *policy) { - struct fscrypt_context ctx; - int res; - - if (!inode->i_sb->s_cop->get_context) - return false; - - res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); - if (res != sizeof(ctx)) - return false; - - return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor, - FS_KEY_DESCRIPTOR_SIZE) == 0 && - (ctx.flags == policy->flags) && - (ctx.contents_encryption_mode == - policy->contents_encryption_mode) && - (ctx.filenames_encryption_mode == - policy->filenames_encryption_mode)); + return memcmp(ctx->master_key_descriptor, policy->master_key_descriptor, + FS_KEY_DESCRIPTOR_SIZE) == 0 && + (ctx->flags == policy->flags) && + (ctx->contents_encryption_mode == + policy->contents_encryption_mode) && + (ctx->filenames_encryption_mode == + policy->filenames_encryption_mode); } static int create_encryption_context_from_policy(struct inode *inode, @@ -96,6 +76,7 @@ static int create_encryption_context_from_policy(struct inode *inode, int fscrypt_set_policy(struct file *file, const struct fscrypt_policy *policy) { struct inode *inode = file_inode(file); + struct fscrypt_context existing; int ret; if (!inode_owner_or_capable(inode)) @@ -110,7 +91,25 @@ int fscrypt_set_policy(struct file *file, const struct fscrypt_policy *policy) inode_lock(inode); - if (!inode_has_encryption_context(inode)) { + ret = -ENODATA; + if (inode->i_sb->s_cop->get_context) { + ret = inode->i_sb->s_cop->get_context(inode, &existing, + sizeof(existing)); + } + if (ret != -ENODATA) { + /* An existing policy cannot be changed. However, an attempt to + * set an identical policy will succeed. */ + if (ret == sizeof(existing) && + is_encryption_context_consistent_with_policy(&existing, + policy)) { + ret = 0; + } else if (ret >= 0 || ret == -ERANGE) { + printk(KERN_WARNING + "%s: Policy inconsistent with encryption context\n", + __func__); + ret = -EINVAL; + } + } else { /* A new policy may only be set on an empty directory or an * empty regular file. */ ret = -EINVAL; @@ -130,11 +129,6 @@ int fscrypt_set_policy(struct file *file, const struct fscrypt_policy *policy) ret = create_encryption_context_from_policy(inode, policy); } - } else if (!is_encryption_context_consistent_with_policy(inode, policy)) { - printk(KERN_WARNING - "%s: Policy inconsistent with encryption context\n", - __func__); - ret = -EINVAL; } inode_unlock(inode); mnt_drop_write_file(file);