Received: by 10.223.164.202 with SMTP id h10csp4240552wrb; Wed, 29 Nov 2017 03:20:54 -0800 (PST) X-Google-Smtp-Source: AGs4zMYae408fIEX/qRxCpOqKCvQvTwMP5CONxvnLMCBChYeywF1dlRRFJ05LkVxXQ04DZqtNKkE X-Received: by 10.98.93.219 with SMTP id n88mr2640242pfj.215.1511954454363; Wed, 29 Nov 2017 03:20:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511954454; cv=none; d=google.com; s=arc-20160816; b=PISaxiW1Q/Kqf+ckpvhzona858rXkIWD4NUpPmmPPrmOHGNR+BE+RJnhB6irtds8DG gFbMPNEdD8OAzfMBB0iet1M10oTfR17jJEPe0cpKrlzbquz2e5sbH5T+SDt4IjPoNSEJ 96gJIoMQwHcMfquxO+S/9mMnWq3tbHcIeJwpYYPn/CNJ2UCFp+GNRYQg1gdxnWJAES29 qxpjrbWIO0Jnto/ZRz5xZG0hAr7DJJTU210PQurF5zzw/oCfCVkN3oqOoh+u+EDvsCnE wpO3DOyZ1TZzSAxJg0G5LeyUPVHIeolPhCHX/DtcYpZ/Pxf6orUtvjwNRMbzs0vKfZ4l 1a9g== 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=v48duve43McASkpHuqFMSd94GhvYJIlI3HfSlV6ORio=; b=i/h+r4sKDYstQpuQMkwnizAg8Tp6IwAKHea9LYQMwBtiGDBDWjIoN5VSb6Fm/niSdz /IlTtn3YVBNi8+0p0XK97Crv7hYLpsFK2ZYqlCdx1Xx2KjJqua8OLAUYDPHataF2LahM qvuosFiEENKH5pwRSzTqVjFWtCCmVesE6gxIbvDnHCl3rj767XhV5Motvry//DIB4r/K OPQyWAhP6cYxsXnjefLytD9y6zhRMfygFBGPK9oWRmSl6cEkHaXI4lHzrnjdZiq/HWte sVe9YO2ZHAjMs4vUn5qYQefuCTS19FUHKO1tKcfthrFB3iVfnCDeMT/ZvUyI7Aca/jtD FAVg== 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 d71si1185850pfd.88.2017.11.29.03.20.44; Wed, 29 Nov 2017 03:20:54 -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 S1754541AbdK2LSv (ORCPT + 70 others); Wed, 29 Nov 2017 06:18:51 -0500 Received: from mga05.intel.com ([192.55.52.43]:10100 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751496AbdK2LSu (ORCPT ); Wed, 29 Nov 2017 06:18:50 -0500 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Nov 2017 03:18:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,472,1505804400"; d="scan'208";a="1249851304" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by fmsmga002.fm.intel.com with ESMTP; 29 Nov 2017 03:18:47 -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] mnt_namespace: convert mnt_namespace.count from atomic_t to refcount_t Date: Wed, 29 Nov 2017 13:18:44 +0200 Message-Id: <1511954324-14414-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 mnt_namespace.count 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 mnt_namespace.count it might make a difference in following places: - put_mnt_ns(): 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/mount.h | 5 +++-- fs/namespace.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/mount.h b/fs/mount.h index f39bc9d..e28e76d 100644 --- a/fs/mount.h +++ b/fs/mount.h @@ -4,9 +4,10 @@ #include #include #include +#include struct mnt_namespace { - atomic_t count; + refcount_t count; struct ns_common ns; struct mount * root; struct list_head list; @@ -112,7 +113,7 @@ static inline void detach_mounts(struct dentry *dentry) static inline void get_mnt_ns(struct mnt_namespace *ns) { - atomic_inc(&ns->count); + refcount_inc(&ns->count); } extern seqlock_t mount_lock; diff --git a/fs/namespace.c b/fs/namespace.c index 7c421e0..848e9b0 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2895,7 +2895,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) } new_ns->ns.ops = &mntns_operations; new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); - atomic_set(&new_ns->count, 1); + refcount_set(&new_ns->count, 1); new_ns->root = NULL; INIT_LIST_HEAD(&new_ns->list); init_waitqueue_head(&new_ns->poll); @@ -3279,7 +3279,7 @@ void __init mnt_init(void) void put_mnt_ns(struct mnt_namespace *ns) { - if (!atomic_dec_and_test(&ns->count)) + if (!refcount_dec_and_test(&ns->count)) return; drop_collected_mounts(&ns->root->mnt); free_mnt_ns(ns); -- 2.7.4 From 1585213436379513089@xxx Mon Nov 27 10:09:00 +0000 2017 X-GM-THRID: 1585213436379513089 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread