From patchwork Thu Dec 15 19:19:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9476799 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 6774660826 for ; Thu, 15 Dec 2016 19:42:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 55A892877A for ; Thu, 15 Dec 2016 19:42:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4A1B128833; Thu, 15 Dec 2016 19:42:04 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 D3B4B2877D for ; Thu, 15 Dec 2016 19:42:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753065AbcLOTly (ORCPT ); Thu, 15 Dec 2016 14:41:54 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:33273 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753815AbcLOTlw (ORCPT ); Thu, 15 Dec 2016 14:41:52 -0500 Received: by mail-pf0-f195.google.com with SMTP id 144so3272328pfv.0; Thu, 15 Dec 2016 11:41:51 -0800 (PST) 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=PgxqdfE8Zi8yvnBjTv64UWDCTjfB5Gavv7DXVoZWmRs=; b=FnNJYge408YHkoia1NdLmQVYNG2cJBS6ZLFrm5uQ4EkpR6SGyPE98wtFCJnwEnZ0o4 aNithoZ9xeiya+dQAiRiWdWrfqN/R4pyNjc+A6068TBfCaDj8WNvMunuTosYlH8j/+ZX lUDz/i9tlr1+DkKFidSa0PohLeMuDhH3OXX5qcK8K7+sq8xfgRX2xo4yNWGZIsbwYe8G 93HgKWwe9m0wf4czDI6WBgo04h5S2y/TZeXLC8X+vvjr2A7DTLSTGozgH8KAi+j9SslQ GQEQx3X0vPPEhtbarR0hhBvSkX19/G0+chxUqIKa3GcgdnhilsrA+NLTP/Iu6XNO5kQ8 Wwrg== 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=PgxqdfE8Zi8yvnBjTv64UWDCTjfB5Gavv7DXVoZWmRs=; b=r/EzGdSVXrNnAkiyEW4nAdOVjC/einYT8hbZU3u3Hov88dW9QpllB5ch8DQJ/hxaBL Okfy4dUW71qy26NMPF73aCQ69VBzySN50J6bT172a4fyWpthL9qaDFJSIKoYdTDKRdmB /g7IQLzT5ePTvSI3nSevz+kfH5INLrPkg1pZ7avD8IYwuR9lwfB5wE/YC+mmyl6aT9Ni +pY5dBd2phXbET5WtG2bW9TTh4FLjZwtY4OXlH43LrrenkXVp+V23+zQ+97IDNKUaSmP zaMSWju0mE6NWC2SCfL8grxNOosbT+rg2bn5IqKReuROGy95NGyuhXD2AeXYQ2avFP1o nzDQ== X-Gm-Message-State: AKaTC01cSCYoAAPkXT5HpMndTPkPvAEMt4x0Gycds83ex9MCl8SRv3UczwHg0tHbEmNHMQ== X-Received: by 10.99.138.75 with SMTP id y72mr4853509pgd.90.1481829853382; Thu, 15 Dec 2016 11:24:13 -0800 (PST) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.119.30.131]) by smtp.gmail.com with ESMTPSA id c22sm6390976pgn.12.2016.12.15.11.24.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 15 Dec 2016 11:24:12 -0800 (PST) From: Eric Biggers To: linux-fsdevel@vger.kernel.org Cc: "Theodore Y . Ts'o" , Jaegeuk Kim , linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Richard Weinberger , David Gstir , Eric Biggers Subject: [PATCH 3/3] fscrypt: consolidate fscrypt_has_permitted_context() checks Date: Thu, 15 Dec 2016 11:19:44 -0800 Message-Id: <1481829584-50218-3-git-send-email-ebiggers3@gmail.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1481829584-50218-1-git-send-email-ebiggers3@gmail.com> References: <1481829584-50218-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-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Now that fscrypt_has_permitted_context() compares the fscrypt_context rather than the fscrypt_info when needed, it is no longer necessary to delay fscrypt_has_permitted_context() from ->lookup() to ->open() for regular files, as introduced in commit ff978b09f973 ("ext4 crypto: move context consistency check to ext4_file_open()"). Therefore the check in ->open(), along with the dget_parent() hack, can be removed. It's also no longer necessary to check the file type before calling fscrypt_has_permitted_context(). This patch makes these changes for both ext4 and f2fs. Signed-off-by: Eric Biggers --- fs/ext4/file.c | 12 ------------ fs/ext4/namei.c | 10 ++-------- fs/f2fs/file.c | 15 +++++---------- fs/f2fs/namei.c | 7 ++----- 4 files changed, 9 insertions(+), 35 deletions(-) diff --git a/fs/ext4/file.c b/fs/ext4/file.c index b5f1844..2123cd8 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -398,7 +398,6 @@ static int ext4_file_open(struct inode * inode, struct file * filp) struct super_block *sb = inode->i_sb; struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); struct vfsmount *mnt = filp->f_path.mnt; - struct dentry *dir; struct path path; char buf[64], *cp; int ret; @@ -443,17 +442,6 @@ static int ext4_file_open(struct inode * inode, struct file * filp) return -ENOKEY; } - dir = dget_parent(file_dentry(filp)); - if (ext4_encrypted_inode(d_inode(dir)) && - !fscrypt_has_permitted_context(d_inode(dir), inode)) { - ext4_warning(inode->i_sb, - "Inconsistent encryption contexts: %lu/%lu", - (unsigned long) d_inode(dir)->i_ino, - (unsigned long) inode->i_ino); - dput(dir); - return -EPERM; - } - dput(dir); /* * Set up the jbd2_inode if we are opening the inode for * writing and the journal is present diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index eadba91..eb8b064 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1612,17 +1612,11 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi return ERR_PTR(-EFSCORRUPTED); } if (!IS_ERR(inode) && ext4_encrypted_inode(dir) && - (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && !fscrypt_has_permitted_context(dir, inode)) { - int nokey = ext4_encrypted_inode(inode) && - !fscrypt_has_encryption_key(inode); - iput(inode); - if (nokey) - return ERR_PTR(-ENOKEY); ext4_warning(inode->i_sb, "Inconsistent encryption contexts: %lu/%lu", - (unsigned long) dir->i_ino, - (unsigned long) inode->i_ino); + dir->i_ino, inode->i_ino); + iput(inode); return ERR_PTR(-EPERM); } } diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 49f10dc..381d39b 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -443,23 +443,18 @@ static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) static int f2fs_file_open(struct inode *inode, struct file *filp) { int ret = generic_file_open(inode, filp); - struct dentry *dir; - if (!ret && f2fs_encrypted_inode(inode)) { + if (ret) + return ret; + + if (f2fs_encrypted_inode(inode)) { ret = fscrypt_get_encryption_info(inode); if (ret) return -EACCES; if (!fscrypt_has_encryption_key(inode)) return -ENOKEY; } - dir = dget_parent(file_dentry(filp)); - if (f2fs_encrypted_inode(d_inode(dir)) && - !fscrypt_has_permitted_context(d_inode(dir), inode)) { - dput(dir); - return -EPERM; - } - dput(dir); - return ret; + return 0; } int truncate_data_blocks_range(struct dnode_of_data *dn, int count) diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c index db33b56..980f783 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -322,11 +322,8 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, goto err_out; } if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) && - (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && - !fscrypt_has_permitted_context(dir, inode)) { - bool nokey = f2fs_encrypted_inode(inode) && - !fscrypt_has_encryption_key(inode); - err = nokey ? -ENOKEY : -EPERM; + !fscrypt_has_permitted_context(dir, inode)) { + err = -EPERM; goto err_out; } return d_splice_alias(inode, dentry);