lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  PHC 
Open Source and information security mailing list archives
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [day] [month] [year] [list]
Date:   Wed, 29 Nov 2017 13:18:44 +0200
From:   Elena Reshetova <>
        Elena Reshetova <>
Subject: [PATCH] mnt_namespace: convert mnt_namespace.count from atomic_t to refcount_t

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
The full comparison can be seen in 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 <linux/poll.h>
 #include <linux/ns_common.h>
 #include <linux/fs_pin.h>
+#include <linux/refcount.h>
 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;
@@ -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))

Powered by blists - more mailing lists