Received: by 10.223.164.221 with SMTP id h29csp4783165wrb; Fri, 20 Oct 2017 00:41:09 -0700 (PDT) X-Google-Smtp-Source: ABhQp+Q2DJ/QWslXA8ni3XzX8A0X8yECUq9UBhSDfsVz4J1CdXrW4JpDFFDJAQ8GyQbWRZULnOHB X-Received: by 10.99.177.75 with SMTP id g11mr3686028pgp.326.1508485269042; Fri, 20 Oct 2017 00:41:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508485269; cv=none; d=google.com; s=arc-20160816; b=eUTEcXVEDXAYX98YUJLpidW/1KrsynjtLul4BLxrWTparj0vc1oA0LKmRvGyj7V6Kz jBrH3RpVajhxeTTz+W+kNqhka2LJw80NgNkkGEeqrnQJrEzh+M0pTmtLIa4aqM0oVw2a Emn3jcedsn3h9m+fdkLd/PZIVsPOAUeEoR7/SxdHRGvUpzC39FTwggS1k01WVJcokXXy d0yRkAdys1yj8RYveEB9sWaD8euUioiGV3P0hNhZxKfKTVdRNCJim+K0l9DppzMDHW3F U89TDnd1nv2a5AJiVCGwoVlwuYioLKAh6oZgdpdaEIH6i2Yh5qoFIcnIsQRSYB06WNAI M5xg== 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=ji7GOc89azyjuXLJKF0QL6MXNbvMvOJLqiUZUH5umLo=; b=gtyBamRjdZ+fEQE2ljKLqflGBkK1RuaBZsgg2cNZTIhHwewFkfsRZeCaDky2/AEPdt sdvkIDcG46/O5M49NyX4VkrzojDldI42X6nZdWGHIwDbf4WjI7hGTyhx+9UscCqaRhnz CeVJMkQmGjou1x8IdWhhGVwnhT43AgLXcM9K/iZsktdwAh6M4s+Rhm1TDAypFvtg2m5i tXFKguAQsbqFwve9iPGX+vKXGCQ1T5XXRbLpYQ1ds82+YS7iEOppwGp93YfOuuynRKVm 8NsYuThBGGZiop+FlYvqe5YZF/PBAKOVJ8KkwR4q9Wvh63S39d5yx30WuIAhaWTLA8Pb ukrQ== 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 k8si334675pgn.524.2017.10.20.00.40.55; Fri, 20 Oct 2017 00:41:08 -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 S1752454AbdJTHjA (ORCPT + 99 others); Fri, 20 Oct 2017 03:39:00 -0400 Received: from mga01.intel.com ([192.55.52.88]:21160 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752170AbdJTHi5 (ORCPT ); Fri, 20 Oct 2017 03:38:57 -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:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,405,1503385200"; d="scan'208";a="1233064279" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by fmsmga002.fm.intel.com with ESMTP; 20 Oct 2017 00:37:50 -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 3/4] dm: convert dm_dev_internal.count from atomic_t to refcount_t Date: Fri, 20 Oct 2017 10:37:38 +0300 Message-Id: <1508485059-21881-4-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 dm_dev_internal.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-table.c | 6 +++--- drivers/md/dm.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c index ef7b8f2..fc7d240 100644 --- a/drivers/md/dm-table.c +++ b/drivers/md/dm-table.c @@ -451,15 +451,15 @@ int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode, return r; } - atomic_set(&dd->count, 0); + refcount_set(&dd->count, 1); list_add(&dd->list, &t->devices); } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) { r = upgrade_mode(dd, mode, t->md); if (r) return r; + refcount_inc(&dd->count); } - atomic_inc(&dd->count); *result = dd->dm_dev; return 0; @@ -515,7 +515,7 @@ void dm_put_device(struct dm_target *ti, struct dm_dev *d) dm_device_name(ti->table->md), d->name); return; } - if (atomic_dec_and_test(&dd->count)) { + if (refcount_dec_and_test(&dd->count)) { dm_put_table_device(ti->table->md, d); list_del(&dd->list); kfree(dd); diff --git a/drivers/md/dm.h b/drivers/md/dm.h index 38c84c0..36399bb8 100644 --- a/drivers/md/dm.h +++ b/drivers/md/dm.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "dm-stats.h" @@ -38,7 +39,7 @@ */ struct dm_dev_internal { struct list_head list; - atomic_t count; + refcount_t count; struct dm_dev *dm_dev; }; -- 2.7.4 From 1592382568842608352@xxx Wed Feb 14 13:19:17 +0000 2018 X-GM-THRID: 1585214450413094369 X-Gmail-Labels: Inbox,Category Forums,Downloaded_2018-02