Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1139675imu; Wed, 16 Jan 2019 13:28:35 -0800 (PST) X-Google-Smtp-Source: ALg8bN5FougREKYgb2axI9KdvQ6xtMwH+ch6+3koMUeTHpWUF5G6MN/DgYGpmANx3DV7u1Nwrcc5 X-Received: by 2002:a63:d047:: with SMTP id s7mr10661403pgi.311.1547674115123; Wed, 16 Jan 2019 13:28:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547674115; cv=none; d=google.com; s=arc-20160816; b=nzcipVONtoUTgjywm5b0sWsCtNcPmt/demzNjLoKCGlFJZdeXnQf8G7ya+o/pj78v5 ZS2PNn1PpsTF4zPBMmEL1wRcdPsCoJL2sZkv999vkfT6uQKmxEHonKxVOkDD2eYPtK6W 6zPQSPzcNXuI9pmERcFm2c9l9lZ2BXoHNCwFrO7rCayPk7tFIJKsH79Aa9XsFlKxRSJ5 +jLOzzq4lQYwb5NcoyT3/SL/QJnR5iHKjqAbtO/c70MsSToeDvHEkeIJcaDtLwmnlYLo FKeyFknbk/6bLGUL3ZH4vi2K0FOFw0LbjFNSlhFWC5Zy9fcF1e8u1yL1IXt47WA/wOwq /dYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=8Pg17dPFqwTEFyinbS5t3q+ko2d3cepft/adLE9TsKk=; b=L4yCNoelr+CC4/zzde3zdq42MkW2ZlR39xdEcpAuXvQrWNVqOeSMkJHemotPiIj9gE YrNgFxSGGM9sR9bR9JYkIv0abP7QOlD6Va3kOFSPYW88YVyv1ErTcNaCM1K4hgvNwXzq QgGIUFqz2Dbguhkk9KUmJCdchJ/mpNp/Y2CY84uL1iAD/qHLUIq/tyI/NkJWMh7drPO8 KJq3GoaTH4jLllIiIi/fBzNo7R+4VtoqAQ1hRxTN/9rrKfxDVfd4/V6abkQD6amtwESn vjGPmBHIoRkgikFViG3S9g2XDco6GzVFrmP+da253Ne5MOx4tpp56AKiG1GaLlPUzDOV a1Rw== 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 l8si5838353pgr.345.2019.01.16.13.28.17; Wed, 16 Jan 2019 13:28:35 -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 S2391991AbfAPK1Y (ORCPT + 99 others); Wed, 16 Jan 2019 05:27:24 -0500 Received: from mga09.intel.com ([134.134.136.24]:31384 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729269AbfAPK1Y (ORCPT ); Wed, 16 Jan 2019 05:27:24 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Jan 2019 02:27:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,485,1539673200"; d="scan'208";a="118910341" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.68.34]) by orsmga003.jf.intel.com with ESMTP; 16 Jan 2019 02:27:20 -0800 From: Elena Reshetova To: akpm@linux-foundation.org Cc: aryabinin@virtuozzo.com, anders.roxell@linaro.org, mark.rutland@arm.com, dvyukov@google.com, linux-kernel@vger.kernel.org, keescook@chromium.org, peterz@infradead.org, Elena Reshetova Subject: [PATCH] kcov: convert kcov.refcount to refcount_t Date: Wed, 16 Jan 2019 12:27:09 +0200 Message-Id: <1547634429-772-1-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 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 kcov.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 kcov.refcount it might make a difference in following places: - kcov_put(): 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/kcov.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/kcov.c b/kernel/kcov.c index c2277db..051e86e 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -20,6 +20,7 @@ #include #include #include +#include #include /* Number of 64-bit words written per one comparison: */ @@ -44,7 +45,7 @@ struct kcov { * - opened file descriptor * - task with enabled coverage (we can't unwire it from another task) */ - atomic_t refcount; + refcount_t refcount; /* The lock protects mode, size, area and t. */ spinlock_t lock; enum kcov_mode mode; @@ -228,12 +229,12 @@ EXPORT_SYMBOL(__sanitizer_cov_trace_switch); static void kcov_get(struct kcov *kcov) { - atomic_inc(&kcov->refcount); + refcount_inc(&kcov->refcount); } static void kcov_put(struct kcov *kcov) { - if (atomic_dec_and_test(&kcov->refcount)) { + if (refcount_dec_and_test(&kcov->refcount)) { vfree(kcov->area); kfree(kcov); } @@ -312,7 +313,7 @@ static int kcov_open(struct inode *inode, struct file *filep) if (!kcov) return -ENOMEM; kcov->mode = KCOV_MODE_DISABLED; - atomic_set(&kcov->refcount, 1); + refcount_set(&kcov->refcount, 1); spin_lock_init(&kcov->lock); filep->private_data = kcov; return nonseekable_open(inode, filep); -- 2.7.4