Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3411451imu; Mon, 28 Jan 2019 04:30:05 -0800 (PST) X-Google-Smtp-Source: ALg8bN4zaBu9OAwyllWuhDyFnsTnvkTyzkjj2wAxx4fiNKUHRq1GmFcsnzaN5sMdCatBh9oJalB4 X-Received: by 2002:a63:9402:: with SMTP id m2mr18972687pge.93.1548678605301; Mon, 28 Jan 2019 04:30:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548678605; cv=none; d=google.com; s=arc-20160816; b=tNDYCZGxlkXbYWGEjviuQ340nBWCx5SCYuMabQJ5M3NPRASsplRgWFcTJEkopVs10l zH5hfF35nX6Po1/SpP6zwLZX76+1lmZLOH52LNwVfrRTXvjE01VQ79tWhy1I8Q4ZxZHO wD9wOE9egnSlA2hajolDy2itibkDumIHHKG0q0FbAehAPK/5pm850wkVpvD6fWxV7ZXr PVaLl0ATX7ZmSaF65ecV5R5aWYXU/wukX6tn78aNL4eJAiG+2K88ZV8E+o5Dn2MGOv52 5vZ19SkkkfdhXGmj1h5QWKg56fSw0uTJlnSNfH9lPf65nR2orl7UlK8F4ykgxTLnGE59 Xnhg== 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; bh=XCE4JU//x8eXJ3AKTXEn6iXO/Ot+y6PJ/tEK6o/ewB4=; b=eI9LMwme7PEvootwNo8X/uuUi1k0CZfrHFez58iQuu0CPPNOjPCs2UeljuoC3XwORh Mx/GBhTmK6gaWi4I6WLoML6f/HmIFtRFxHT1jl+XHDQ7Yr4oz77gtrVoEk3Q2E172adL z2klZT+4J2xb19UNdSVhwlJRGNMQNcpA6HPBq9dmSkqIA73TZ1smkJTDePk2FDxvc5mz fLxubz03CFMdGH+svozDMmPZGsn3l8SZTAtvUhzfXnnJeEyV8wBdWl+MoBNfA4fR1WPQ CqMM0gQRWaD8AJSMa6v5Rsv9d94uc4W1VoSfX3xqzGOZpEI1cx7MogKGHuRZt+gfFnAD s8rg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q18si28786957pls.30.2019.01.28.04.29.50; Mon, 28 Jan 2019 04:30:05 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726905AbfA1M1w (ORCPT + 99 others); Mon, 28 Jan 2019 07:27:52 -0500 Received: from mga02.intel.com ([134.134.136.20]:21831 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726695AbfA1M1u (ORCPT ); Mon, 28 Jan 2019 07:27:50 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Jan 2019 04:27:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,534,1539673200"; d="scan'208";a="139402590" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.68.34]) by fmsmga004.fm.intel.com with ESMTP; 28 Jan 2019 04:27:47 -0800 From: Elena Reshetova To: peterz@infradead.org Cc: linux-kernel@vger.kernel.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, keescook@chromium.org, tglx@linutronix.de, Elena Reshetova Subject: [PATCH 3/3] perf/ring_buffer: convert ring_buffer.aux_refcount to refcount_t Date: Mon, 28 Jan 2019 14:27:28 +0200 Message-Id: <1548678448-24458-4-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1548678448-24458-1-git-send-email-elena.reshetova@intel.com> References: <1548678448-24458-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. Please check Documentation/core-api/refcount-vs-atomic.rst for more information. 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 ACQUIRE ordering + 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 963cee0..31ab5d7 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5463,7 +5463,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 4718de2..79c4707 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 e841d48..0416f01 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -358,7 +358,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; /* @@ -671,7 +671,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; @@ -690,7 +690,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