From patchwork Fri Oct 20 10:41:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Reshetova, Elena" X-Patchwork-Id: 828550 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=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-cifs-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yJMvT3ZcXz9t6J for ; Fri, 20 Oct 2017 21:47:49 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752772AbdJTKrs (ORCPT ); Fri, 20 Oct 2017 06:47:48 -0400 Received: from mga04.intel.com ([192.55.52.120]:33453 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752657AbdJTKrr (ORCPT ); Fri, 20 Oct 2017 06:47:47 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2017 03:47:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,405,1503385200"; d="scan'208";a="1208005027" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by fmsmga001.fm.intel.com with ESMTP; 20 Oct 2017 03:47:44 -0700 From: Elena Reshetova To: linux-cifs@vger.kernel.org Cc: linux-kernel@vger.kernel.org, sfrench@samba.org, samba-technical@lists.samba.org, peterz@infradead.org, keescook@chromium.org, Elena Reshetova Subject: [PATCH] fs, cifs: convert tcon_link.tl_count from atomic_t to refcount_t Date: Fri, 20 Oct 2017 13:41:50 +0300 Message-Id: <1508496110-2292-1-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable tcon_link.tl_count is used as pure reference counter. Convert it to refcount_t and fix up the operations. Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova Reviewed-by: Aurelien Aptel --- fs/cifs/cifsglob.h | 5 +++-- fs/cifs/connect.c | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/cifs/cifsglob.h b/fs/cifs/cifsglob.h index de5b2e1..18963bf 100644 --- a/fs/cifs/cifsglob.h +++ b/fs/cifs/cifsglob.h @@ -28,6 +28,7 @@ #include "cifsacl.h" #include #include +#include #include #include "smb2pdu.h" @@ -974,7 +975,7 @@ struct tcon_link { #define TCON_LINK_PENDING 1 #define TCON_LINK_IN_TREE 2 unsigned long tl_time; - atomic_t tl_count; + refcount_t tl_count; struct cifs_tcon *tl_tcon; }; @@ -992,7 +993,7 @@ static inline struct tcon_link * cifs_get_tlink(struct tcon_link *tlink) { if (tlink && !IS_ERR(tlink)) - atomic_inc(&tlink->tl_count); + refcount_inc(&tlink->tl_count); return tlink; } diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c index 0bfc228..f861152 100644 --- a/fs/cifs/connect.c +++ b/fs/cifs/connect.c @@ -2868,7 +2868,7 @@ cifs_put_tlink(struct tcon_link *tlink) if (!tlink || IS_ERR(tlink)) return; - if (!atomic_dec_and_test(&tlink->tl_count) || + if (!refcount_dec_and_test(&tlink->tl_count) || test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) { tlink->tl_time = jiffies; return; @@ -4328,7 +4328,7 @@ cifs_sb_tlink(struct cifs_sb_info *cifs_sb) newtlink->tl_tcon = ERR_PTR(-EACCES); set_bit(TCON_LINK_PENDING, &newtlink->tl_flags); set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags); - cifs_get_tlink(newtlink); + refcount_set(&newtlink->tl_count, 1); spin_lock(&cifs_sb->tlink_tree_lock); /* was one inserted after previous search? */ @@ -4406,11 +4406,11 @@ cifs_prune_tlinks(struct work_struct *work) tlink = rb_entry(tmp, struct tcon_link, tl_rbnode); if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) || - atomic_read(&tlink->tl_count) != 0 || + refcount_read(&tlink->tl_count) != 0 || time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies)) continue; - cifs_get_tlink(tlink); + refcount_set(&tlink->tl_count, 1); clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); rb_erase(tmp, root);