Received: by 10.223.164.202 with SMTP id h10csp4238864wrb; Wed, 29 Nov 2017 03:19:04 -0800 (PST) X-Google-Smtp-Source: AGs4zMZOg3/QwUlxYjWxB8cPuGc1qWDW1JXEFiGVyLN+OYQS+xA3XdswV0Nv8oivX5ss3dGL3esc X-Received: by 10.98.67.78 with SMTP id q75mr2661147pfa.110.1511954344388; Wed, 29 Nov 2017 03:19:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511954344; cv=none; d=google.com; s=arc-20160816; b=XJfVibBgNGVPfoHwn8OoG/ZLbRhcF8o67MZnfylcHRm5mvorDULkbn7o/SLA4Gmh7A dE1wgqey3XZmSx9/08r9skYTrB1SW/2EdZw8LR09FIb8mTbQy2UDAYlH+Am+idADIpHP +at9m+JS15UQ5/vUIbqKZkxfqs3UoclAkOx2FQm8VXk4GLureVdRoBsT0AeYChZNrPgo FFRrGBpS309pJIn8x2IvtzNJNXpBoJ3EjNu8l4E9GxZzB+KkMATSMk5W3EPDAcCv+VMY uQ5gbqNuCsliierDY+eRQ6Bp3AoCntqNySqula8FRG0jTdgptKNWt3JOjiCc6siZ7yvn 1Emw== 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 :arc-authentication-results; bh=ep5Nbbh3AN4sKuXnrUNLV6z78QEpelZ23byXKF8h4gY=; b=KUtqBdYufwFx6CKbgkJRovG3ACuU/sBBglS8XtKwSmholmGS/WprMs5EGclrj9YwCR 55DZdYoPKE8FzpuhFFBFNoMaEleqgNDPusU0srWDRMjT2EtNOCiTUMXX2ibT9+7ddmfs Joji+s51VJKHygsRuky3/+G6kqafr1LSN4zQHWqyqnGQgb08UdQKYEdj0yV9+CoGXDI1 ZUTX3WQLDHbk3QEfepiyFHomxgFz3i19sI6pOut0oz0QA2d2sMt8fk34QCPAXvq74aAv gO13pRjb1WSEeuS5qNI4fN0xMF/doTuhCN1Ia40/qob+ptieHOTTkQmo4V3GXvY6Dysa gVQw== 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 g1si1119242pld.816.2017.11.29.03.18.54; Wed, 29 Nov 2017 03:19:04 -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 S1754582AbdK2LRU (ORCPT + 70 others); Wed, 29 Nov 2017 06:17:20 -0500 Received: from mga02.intel.com ([134.134.136.20]:25104 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752133AbdK2LRS (ORCPT ); Wed, 29 Nov 2017 06:17:18 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Nov 2017 03:17:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,472,1505804400"; d="scan'208";a="10947551" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by orsmga001.jf.intel.com with ESMTP; 29 Nov 2017 03:17:15 -0800 From: Elena Reshetova To: viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, peterz@infradead.org, keescook@chromium.org, Elena Reshetova Subject: [PATCH] userfaultfd: convert userfaultfd_ctx.refcount from atomic_t to refcount_t Date: Wed, 29 Nov 2017 13:17:07 +0200 Message-Id: <1511954227-12600-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 userfaultfd_ctx.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 userfaultfd_ctx.refcount it might make a difference in following places: - userfaultfd_ctx_get(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart - userfaultfd_ctx_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 --- fs/userfaultfd.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index ac9a4e6..b63ebcac 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -29,6 +29,7 @@ #include #include #include +#include static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly; @@ -53,7 +54,7 @@ struct userfaultfd_ctx { /* a refile sequence protected by fault_pending_wqh lock */ struct seqcount refile_seq; /* pseudo fd refcounting */ - atomic_t refcount; + refcount_t refcount; /* userfaultfd syscall flags */ unsigned int flags; /* features requested from the userspace */ @@ -138,7 +139,7 @@ static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode, */ static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx) { - if (!atomic_inc_not_zero(&ctx->refcount)) + if (!refcount_inc_not_zero(&ctx->refcount)) BUG(); } @@ -152,7 +153,7 @@ static void userfaultfd_ctx_get(struct userfaultfd_ctx *ctx) */ static void userfaultfd_ctx_put(struct userfaultfd_ctx *ctx) { - if (atomic_dec_and_test(&ctx->refcount)) { + if (refcount_dec_and_test(&ctx->refcount)) { VM_BUG_ON(spin_is_locked(&ctx->fault_pending_wqh.lock)); VM_BUG_ON(waitqueue_active(&ctx->fault_pending_wqh)); VM_BUG_ON(spin_is_locked(&ctx->fault_wqh.lock)); @@ -662,7 +663,7 @@ int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs) return -ENOMEM; } - atomic_set(&ctx->refcount, 1); + refcount_set(&ctx->refcount, 1); ctx->flags = octx->flags; ctx->state = UFFD_STATE_RUNNING; ctx->features = octx->features; @@ -1902,7 +1903,7 @@ static struct file *userfaultfd_file_create(int flags) if (!ctx) goto out; - atomic_set(&ctx->refcount, 1); + refcount_set(&ctx->refcount, 1); ctx->flags = flags; ctx->features = 0; ctx->state = UFFD_STATE_WAIT_API; -- 2.7.4 From 1589701425171756377@xxx Mon Jan 15 23:03:39 +0000 2018 X-GM-THRID: 1585202142139897134 X-Gmail-Labels: Inbox,Category Forums,Downloaded_2018-01