Received: by 10.223.164.221 with SMTP id h29csp4783032wrb; Fri, 20 Oct 2017 00:40:56 -0700 (PDT) X-Google-Smtp-Source: ABhQp+QtIQQZvVj7UuVe+8izyKw4QR9Qz9h1+evMvhn8p6oXSU08voPE5GyZsiFhbd6u90+PDaQK X-Received: by 10.98.222.2 with SMTP id h2mr4146440pfg.165.1508485256543; Fri, 20 Oct 2017 00:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508485256; cv=none; d=google.com; s=arc-20160816; b=Xxho4hdSyRffH0U+2us1pI1Zg3qRBHTdVvUvm0TVCyKuAzJfUqBRZGwnlajNbIgChi QxaFCf5u6ci3J2CL4TS7OGZqZyFyG9OUVAB/DIm9GeWdWJv6ehoxneDKjI/PqUFY5Ul6 3balhfDnoWD5xErHQycs3Mfq1km6c9XdtgfWMHjbbke739pyl/kS7TyGIWsd2WJjhtTL eHpjBKO6JiLNqZnrxEzRVBSxns38vwjVG1EOth18ZL+pIX2I8M1dUbXx0grlIh3cYVfN Yy5rN7Dga01FoSwwoXKr2WqD2NHDBt9xzkkEFH8PjUUEEi1RW1A+xqfn5yWRj6Gbwuqx zgig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=DpfQdyLF4PSd+d8xbBXs3ceKnVOys8JktsrpYfiT7ok=; b=GJmcJ5jmQmM25QZZbfWMPfusjBbHo9A7wTYceKtACew40l5du8QpR0CIILkG7RystA bp8mHu9mwY+CGK+X84nFceODFAq841UbGsoqDltzLg+RulhcYrk/BElvelpVbYufHFbF JwkGBzjL4V46N4c3C6LkZTyCcgSuIypBj/2MupMFZGNs0jLOgs55RpcHzrn1bbAyyDN0 1gNLTPTr3bCR4D93JeS9LfUkR9yFh6K4p4u7ckc7SGsy498v1L6XY1skJPsYa/Xu7PcW T0XHa/vtnkrJiBuZ+TiE3lZdIxgkJjbNvaWEgsK9Lv7v2dE9G5F8N11WTQyv0374KUrs mLsQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j6si372402pfh.329.2017.10.20.00.40.42; Fri, 20 Oct 2017 00:40:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752408AbdJTHi4 (ORCPT + 99 others); Fri, 20 Oct 2017 03:38:56 -0400 Received: from mga01.intel.com ([192.55.52.88]:44912 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751982AbdJTHio (ORCPT ); Fri, 20 Oct 2017 03:38:44 -0400 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2017 00:38:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,405,1503385200"; d="scan'208";a="1233064283" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by fmsmga002.fm.intel.com with ESMTP; 20 Oct 2017 00:37:53 -0700 From: Elena Reshetova To: dm-devel@redhat.com Cc: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, kent.overstreet@gmail.com, koverstreet@google.com, ejt@redhat.comg, snitzer@redhat.com, shli@kernel.org, agk@redhat.com, peterz@infradead.org, keescook@chromium.org, Elena Reshetova Subject: [PATCH 4/4] dm: convert table_device.count from atomic_t to refcount_t Date: Fri, 20 Oct 2017 10:37:39 +0300 Message-Id: <1508485059-21881-5-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1508485059-21881-1-git-send-email-elena.reshetova@intel.com> References: <1508485059-21881-1-git-send-email-elena.reshetova@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 table_device.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 --- drivers/md/dm.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 4be8532..be12f3f 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -24,6 +24,7 @@ #include #include #include +#include #define DM_MSG_PREFIX "core" @@ -98,7 +99,7 @@ struct dm_md_mempools { struct table_device { struct list_head list; - atomic_t count; + refcount_t count; struct dm_dev dm_dev; }; @@ -685,10 +686,11 @@ int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, format_dev_t(td->dm_dev.name, dev); - atomic_set(&td->count, 0); + refcount_set(&td->count, 1); list_add(&td->list, &md->table_devices); + } else { + refcount_inc(&td->count); } - atomic_inc(&td->count); mutex_unlock(&md->table_devices_lock); *result = &td->dm_dev; @@ -701,7 +703,7 @@ void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) struct table_device *td = container_of(d, struct table_device, dm_dev); mutex_lock(&md->table_devices_lock); - if (atomic_dec_and_test(&td->count)) { + if (refcount_dec_and_test(&td->count)) { close_table_device(td, md); list_del(&td->list); kfree(td); @@ -718,7 +720,7 @@ static void free_table_devices(struct list_head *devices) struct table_device *td = list_entry(tmp, struct table_device, list); DMWARN("dm_destroy: %s still exists with %d references", - td->dm_dev.name, atomic_read(&td->count)); + td->dm_dev.name, refcount_read(&td->count)); kfree(td); } } -- 2.7.4 From 1584745573084483550@xxx Wed Nov 22 06:12:31 +0000 2017 X-GM-THRID: 1584661685458738428 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread