Received: by 10.223.164.202 with SMTP id h10csp2025665wrb; Thu, 16 Nov 2017 08:11:56 -0800 (PST) X-Google-Smtp-Source: AGs4zMaM/c1jlzoo/pI4n/OvOZExhb96a3Ez9yWhfiudUwQ1rIQz5eBRQnDBOvAZGnDFYH9L5URd X-Received: by 10.159.252.76 with SMTP id t12mr2181527plz.323.1510848715895; Thu, 16 Nov 2017 08:11:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510848715; cv=none; d=google.com; s=arc-20160816; b=L5yVrWKE83m0ErmgKMfTmCTSez6GnUDfMD0nCeuYwO26Ivh4cxiKE7U/izrIcPTNwf 8vIiz6alZUZGXVCb3J2TK/efPa7ZY4wLqCYBUOpB7CmixOUq1uaNUKHJpZFfIF+F1nMm LurNfhYCBCQAxkT7Eu3m46DzJasVqKn1vtLyaEC9aY0IsRgmGbdTXnpZfEqg5yqxBSlD H2eDLImlRD3GScXfxcX0LmdJJwo76vK0xE6KptqqOj3qKekuwzMkdECWYPh+Br9MMGXG uLcvUzGCnZJr03pyFEJlEIsJgm/DDGdiWqzzybWtuLex7XjGJ8gsbaemjxMk2caoEnFg wYng== 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=etysg5OrobF9dPiK61LbjCr2tVqBjykRICHWb85LwYs=; b=afjdvQJpsC1h7fqXzNb2oiaDwmUGF1Td8GwU82UxTPoKj3lRwY8D4LfyXBmpdz98cN +PRRbyg8EzJglhUf+m/gDCFVRU08a/xn5i0xs0rCgiHxdcYFaRHPg7nVFJPoNvPmaBso vhrCYJrn+85pCENplXUMlOFUlgaFfcspYgYEGjgc8RTreHHmly4/3zlpc3PUvDZyE1s9 wUwyn4gYRzukdP+YR3PM/pJ3qx2RrgMfre99BsxJw88laOK0ruE+pTlvp7DCFaQX3uLS B8Cxf2zlzPBq+Vvwd9HMAeEyV8MSJVv8OOGpObzENIsZmUjPVW/alHopT3cHjdlSSex4 hUzw== 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 b5si1081421ple.716.2017.11.16.08.11.42; Thu, 16 Nov 2017 08:11:55 -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 S935183AbdKPN6U (ORCPT + 91 others); Thu, 16 Nov 2017 08:58:20 -0500 Received: from mga04.intel.com ([192.55.52.120]:26946 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935162AbdKPN55 (ORCPT ); Thu, 16 Nov 2017 08:57:57 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Nov 2017 06:08:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,399,1505804400"; d="scan'208";a="2131208" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 15 Nov 2017 06:08:41 -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 11/16] uprobes: convert uprobe.ref to refcount_t Date: Wed, 15 Nov 2017 16:03:35 +0200 Message-Id: <1510754620-27088-12-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 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 8d42d8f..3514b42 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; @@ -371,13 +371,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)) kfree(uprobe); } @@ -459,7 +459,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 From 1584244982018547325@xxx Thu Nov 16 17:35:50 +0000 2017 X-GM-THRID: 1584165208362108163 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread