Received: by 10.223.164.202 with SMTP id h10csp818922wrb; Wed, 15 Nov 2017 08:24:17 -0800 (PST) X-Google-Smtp-Source: AGs4zMazCg+VUalqPW1AgfnkR+ftbaiJfkzRmlbPi62sIRUjS3tbPtjFTiH292E9f0rs9lWQrp4n X-Received: by 10.99.172.88 with SMTP id z24mr16518404pgn.46.1510763057717; Wed, 15 Nov 2017 08:24:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510763057; cv=none; d=google.com; s=arc-20160816; b=x0EvL1zFAHGU9DtGhOAlxRLL7QlE8mAaet+RNnhxXqb56TXHjqjPl1O3wCZUdcdrVF HxtHRKkOKn4WvSDzgf97KS/KmihJRcylXhaA52sHI+Fc5qJ3fANA5I+sqYjvf1PnHrNo tyryWo0bleB9+Csch2h8aSsu2PQjz7Z2cgLZ0KBd8scPqRr9ygHtLK1JOApn8VAVYr2j pLubG9EvtejwS6odyWxKEPQOZ3VZwtyTOV8quiMiYqAbkJB0qQS+J15Gcongq+4K47b1 MXqUB+Oq8VbJxz3ZEk7tP1s13Wx0WqioR9r4UoEbtRySIxT1JjY/saepwsLIgM7zYLh3 cbFg== 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=nNf/sw0qBH/KG2PA2TPJBshFEeLfr2I8w/ysEp1GyOU=; b=V+gJac3EreMl1ydb9quM3Iw2plAcJWvd1821Qv+GUrvhR2e/jRkrYioO7nrEH4pbGp H8uzLE0eBu+Wf8LCq46nvtEpqa+E0lareJi9KqrQ/e/N6uacvk5G6Hrj0/HmHOaW9nMQ LMGeNtTkM+pELpTk9vDJbIjytJKFbnPu4PF/aP5sfadKeGdJD3ga5goAiGZmjx/EUsB3 YoDL8J6tZ6qHgTnqvYZwXedSUXmFhKIlQHLLPwV/XTcALpcxkn78uHNVCbr7nPqXU5Un oYw0qQAPpIWgnmEhyffzN5l6/dJSAXODniSjf4BzCIhS1zAUYiQwZt9MPNEGDF6TbVg4 RB0Q== 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 z33si5025755plb.35.2017.11.15.08.24.05; Wed, 15 Nov 2017 08:24:17 -0800 (PST) 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 S1758127AbdKOOJV (ORCPT + 89 others); Wed, 15 Nov 2017 09:09:21 -0500 Received: from mga03.intel.com ([134.134.136.65]:11103 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758071AbdKOOIl (ORCPT ); Wed, 15 Nov 2017 09:08:41 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Nov 2017 06:08:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,399,1505804400"; d="scan'208";a="2131176" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 15 Nov 2017 06:08:36 -0800 From: Elena Reshetova To: mingo@redhat.com Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, peterz@infradead.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk, tj@kernel.org, hannes@cmpxchg.org, lizefan@huawei.com, acme@kernel.org, alexander.shishkin@linux.intel.com, eparis@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, luto@kernel.org, keescook@chromium.org, tglx@linutronix.de, dvhart@infradead.org, ebiederm@xmission.com, linux-mm@kvack.org, axboe@kernel.dk, Elena Reshetova Subject: [PATCH 10/16] perf/ring_buffer: convert ring_buffer.aux_refcount to refcount_t Date: Wed, 15 Nov 2017 16:03:34 +0200 Message-Id: <1510754620-27088-11-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510754620-27088-1-git-send-email-elena.reshetova@intel.com> References: <1510754620-27088-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 ring_buffer.aux_refcount is used as pure reference counter. Convert it to refcount_t and fix up the operations. **Important note for maintainers: Some functions from refcount_t API defined in lib/refcount.c have different memory ordering guarantees than their atomic counterparts. The full comparison can be seen in https://lkml.org/lkml/2017/11/15/57 and it is hopefully soon in state to be merged to the documentation tree. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the ring_buffer.aux_refcount it might make a difference in following places: - perf_aux_output_begin(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart - rb_free_aux(): decrement in refcount_dec_and_test() only provides RELEASE ordering and control dependency on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova --- kernel/events/core.c | 2 +- kernel/events/internal.h | 2 +- kernel/events/ring_buffer.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 3497c6a..5f087f4 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5095,7 +5095,7 @@ static void perf_mmap_close(struct vm_area_struct *vma) /* this has to be the last one */ rb_free_aux(rb); - WARN_ON_ONCE(atomic_read(&rb->aux_refcount)); + WARN_ON_ONCE(refcount_read(&rb->aux_refcount)); mutex_unlock(&event->mmap_mutex); } diff --git a/kernel/events/internal.h b/kernel/events/internal.h index 86c5c7f..50ecf00 100644 --- a/kernel/events/internal.h +++ b/kernel/events/internal.h @@ -49,7 +49,7 @@ struct ring_buffer { atomic_t aux_mmap_count; unsigned long aux_mmap_locked; void (*free_aux)(void *); - atomic_t aux_refcount; + refcount_t aux_refcount; void **aux_pages; void *aux_priv; diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index de12d36..b29d6ce 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -357,7 +357,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle, if (!atomic_read(&rb->aux_mmap_count)) goto err; - if (!atomic_inc_not_zero(&rb->aux_refcount)) + if (!refcount_inc_not_zero(&rb->aux_refcount)) goto err; /* @@ -659,7 +659,7 @@ int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event, * we keep a refcount here to make sure either of the two can * reference them safely. */ - atomic_set(&rb->aux_refcount, 1); + refcount_set(&rb->aux_refcount, 1); rb->aux_overwrite = overwrite; rb->aux_watermark = watermark; @@ -678,7 +678,7 @@ int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event, void rb_free_aux(struct ring_buffer *rb) { - if (atomic_dec_and_test(&rb->aux_refcount)) + if (refcount_dec_and_test(&rb->aux_refcount)) __rb_free_aux(rb); } -- 2.7.4 From 1583782605081562886@xxx Sat Nov 11 15:06:33 +0000 2017 X-GM-THRID: 1583670749450643631 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread