From patchwork Tue May 19 05:36:41 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaegeuk Kim X-Patchwork-Id: 6433331 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 908079F38D for ; Tue, 19 May 2015 05:37:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A89672049E for ; Tue, 19 May 2015 05:37:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A6D4B2047C for ; Tue, 19 May 2015 05:36:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753449AbbESFgo (ORCPT ); Tue, 19 May 2015 01:36:44 -0400 Received: from mail.kernel.org ([198.145.29.136]:54004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752863AbbESFgo (ORCPT ); Tue, 19 May 2015 01:36:44 -0400 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 03F35204B0; Tue, 19 May 2015 05:36:43 +0000 (UTC) Received: from localhost (c-73-189-178-17.hsd1.ca.comcast.net [73.189.178.17]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 47EA2204A0; Tue, 19 May 2015 05:36:42 +0000 (UTC) From: Jaegeuk Kim To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Cc: Jaegeuk Kim Subject: [PATCH] f2fs crypto: add rwsem to avoid data races Date: Mon, 18 May 2015 22:36:41 -0700 Message-Id: <1432013801-39069-1-git-send-email-jaegeuk@kernel.org> X-Mailer: git-send-email 2.1.1 X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, 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 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 Previoulsy, fi->i_crypt_info was not covered by any lock, resulting in memory leak. This patch adds a rwsem to avoid leaking objects on i_crypt_info. Signed-off-by: Jaegeuk Kim --- fs/f2fs/crypto_key.c | 29 ++++++++++++++++++++++------- fs/f2fs/f2fs.h | 1 + fs/f2fs/super.c | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/fs/f2fs/crypto_key.c b/fs/f2fs/crypto_key.c index 8a10569..a25b164 100644 --- a/fs/f2fs/crypto_key.c +++ b/fs/f2fs/crypto_key.c @@ -87,7 +87,7 @@ out: return res; } -void f2fs_free_encryption_info(struct inode *inode) +static void _f2fs_free_encryption_info(struct inode *inode) { struct f2fs_inode_info *fi = F2FS_I(inode); struct f2fs_crypt_info *ci = fi->i_crypt_info; @@ -103,6 +103,13 @@ void f2fs_free_encryption_info(struct inode *inode) fi->i_crypt_info = NULL; } +void f2fs_free_encryption_info(struct inode *inode) +{ + down_write(&F2FS_I(inode)->crypto_rwsem); + _f2fs_free_encryption_info(inode); + up_write(&F2FS_I(inode)->crypto_rwsem); +} + int _f2fs_get_encryption_info(struct inode *inode) { struct f2fs_inode_info *fi = F2FS_I(inode); @@ -119,12 +126,13 @@ int _f2fs_get_encryption_info(struct inode *inode) if (res) return res; - if (fi->i_crypt_info) { - if (!fi->i_crypt_info->ci_keyring_key || - key_validate(fi->i_crypt_info->ci_keyring_key) == 0) - return 0; - f2fs_free_encryption_info(inode); + down_read(&fi->crypto_rwsem); + if (fi->i_crypt_info && (!fi->i_crypt_info->ci_keyring_key || + key_validate(fi->i_crypt_info->ci_keyring_key) == 0)) { + up_read(&fi->crypto_rwsem); + return 0; } + up_read(&fi->crypto_rwsem); res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, @@ -187,8 +195,11 @@ out: res = 0; kmem_cache_free(f2fs_crypt_info_cachep, crypt_info); } else { + down_write(&fi->crypto_rwsem); + _f2fs_free_encryption_info(inode); fi->i_crypt_info = crypt_info; crypt_info->ci_keyring_key = keyring_key; + up_write(&fi->crypto_rwsem); keyring_key = NULL; } if (keyring_key) @@ -199,6 +210,10 @@ out: int f2fs_has_encryption_key(struct inode *inode) { struct f2fs_inode_info *fi = F2FS_I(inode); + int ret; - return (fi->i_crypt_info != NULL); + down_read(&fi->crypto_rwsem); + ret = (fi->i_crypt_info != NULL); + up_read(&fi->crypto_rwsem); + return ret; } diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 5119167..c44d7bf 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -431,6 +431,7 @@ struct f2fs_inode_info { #ifdef CONFIG_F2FS_FS_ENCRYPTION /* Encryption params */ struct f2fs_crypt_info *i_crypt_info; + struct rw_semaphore crypto_rwsem; /* lock for crypt_info */ #endif }; diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index bbeb6d7..137d1b7 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -418,6 +418,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb) #ifdef CONFIG_F2FS_FS_ENCRYPTION fi->i_crypt_info = NULL; + init_rwsem(&fi->crypto_rwsem); #endif return &fi->vfs_inode; }