Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1148012imu; Wed, 16 Jan 2019 13:37:43 -0800 (PST) X-Google-Smtp-Source: ALg8bN4tdffUnkEJ2Nkao7vQUVgtg8ocaffxfT6Nf0djD7MbINB3MGSrzA1UEAtegipmeOAJaA01 X-Received: by 2002:a63:d301:: with SMTP id b1mr10839491pgg.61.1547674663895; Wed, 16 Jan 2019 13:37:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547674663; cv=none; d=google.com; s=arc-20160816; b=hzkZzuMI3kFfmNdwqpX9RoJfc7cDdi/l7/GkyvqR9SS0ESfgAUih4ysNsArAAazve+ 3Ml+xEjShJl1AjLtN6CQUliOXCEvfFeVhvnfbj+o9wde5KRG1RQzUD3FF2ZV74FRKw89 Yz9oNjA+2X0Q98WDW9ScLQmi6R/oWUG8XTIaHM95KvOeYTpb3rqlRs7c/jExqSUqpQZH cFqP/7KFBG3ewpA/i1Y/9REAkeBgRjegO9M7awmxaJAHR5qhPSajvW3KGWyp7ClLLLL9 Yb9/yzoxb9GTTLwLDxpnCu8t856f5/CWjyFAocjANoGbbqPPp/xtPgEPs0oxmtl4X3pL 9UzA== 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=pXXWwqURwiMsZJMI4mLK7l9h3RGWw6LJwgqyHqbLAWQ=; b=uoimjdfCKSbv7fNwnUoOQpLFnsgWi2EufVXOZpMZoZogqf/3YL0DssOX+3RiOzknGG Yg3QlJTD5Kua1z+R/X+vm0bM+mJibfBTACvi00K4JBG/Z7OmmnwXe85Q0tdBtjCtFQKI HdkQDU2lMw0zp1se3rmVXgn4WEa4QY+YF+bhlKSCjIuPwJrhyGXaS/db/hG44Mr61OAC ozcjGFF5GM81jz9rLBlnEDvEhGUhQShQrZaj7xLQI0aRhNXfqODvXENfWZhVzwengqTS nef69mTCplURkO4rxG6vIs3RICXdGj+UnxYBH1uZzJNscDaV9/S4+wQNDeWE0/RtylYJ dgwg== 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 g59si3760323plb.302.2019.01.16.13.37.28; Wed, 16 Jan 2019 13:37:43 -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 S2389797AbfAPLUg (ORCPT + 99 others); Wed, 16 Jan 2019 06:20:36 -0500 Received: from mga04.intel.com ([192.55.52.120]:36577 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730420AbfAPLUg (ORCPT ); Wed, 16 Jan 2019 06:20:36 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Jan 2019 03:20:35 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,485,1539673200"; d="scan'208";a="310873778" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.68.34]) by fmsmga006.fm.intel.com with ESMTP; 16 Jan 2019 03:20:33 -0800 From: Elena Reshetova To: rostedt@goodmis.org Cc: peterz@infradead.org, mingo@redhat.com, akpm@linux-foundation.org, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, linux-kernel@vger.kernel.org, keescook@chromium.org, Elena Reshetova Subject: [PATCH] uprobes: convert uprobe.ref to refcount_t Date: Wed, 16 Jan 2019 13:20:27 +0200 Message-Id: <1547637627-29526-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 uprobe.ref 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 uprobe.ref it might make a difference in following places: - put_uprobe(): 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/uprobes.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index ad415f7..750aece 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -66,7 +66,7 @@ static struct percpu_rw_semaphore dup_mmap_sem; struct uprobe { struct rb_node rb_node; /* node in the rb tree */ - atomic_t ref; + refcount_t ref; struct rw_semaphore register_rwsem; struct rw_semaphore consumer_rwsem; struct list_head pending_list; @@ -561,13 +561,13 @@ set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long v static struct uprobe *get_uprobe(struct uprobe *uprobe) { - atomic_inc(&uprobe->ref); + refcount_inc(&uprobe->ref); return uprobe; } static void put_uprobe(struct uprobe *uprobe) { - if (atomic_dec_and_test(&uprobe->ref)) { + if (refcount_dec_and_test(&uprobe->ref)) { /* * If application munmap(exec_vma) before uprobe_unregister() * gets called, we don't get a chance to remove uprobe from @@ -658,7 +658,7 @@ static struct uprobe *__insert_uprobe(struct uprobe *uprobe) rb_link_node(&uprobe->rb_node, parent, p); rb_insert_color(&uprobe->rb_node, &uprobes_tree); /* get access + creation ref */ - atomic_set(&uprobe->ref, 2); + refcount_set(&uprobe->ref, 2); return u; } -- 2.7.4