Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1690063

[PATCH 14/15] futex: convert futex_pi_state.refcount to refcount_t

From Elena Reshetova <elena.reshetova@intel.com>
Newsgroups linux.kernel
Subject [PATCH 14/15] futex: convert futex_pi_state.refcount to refcount_t
Date 2017-07-18 12:40 +0200
Message-ID <u4y56-1ed-35@gated-at.bofh.it> (permalink)
References <u4y55-1ed-29@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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 futex_pi_state.refcount is used as pure
reference counter. Convert it to refcount_t and fix up
the operations.

Suggested-by: Kees Cook <keescook@chromium.org>
Reviewed-by: David Windsor <dwindsor@gmail.com>
Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
 kernel/futex.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 16dbe4c..1cc7641 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -67,6 +67,7 @@
 #include <linux/freezer.h>
 #include <linux/bootmem.h>
 #include <linux/fault-inject.h>
+#include <linux/refcount.h>
 
 #include <asm/futex.h>
 
@@ -209,7 +210,7 @@ struct futex_pi_state {
 	struct rt_mutex pi_mutex;
 
 	struct task_struct *owner;
-	atomic_t refcount;
+	refcount_t refcount;
 
 	union futex_key key;
 } __randomize_layout;
@@ -794,7 +795,7 @@ static int refill_pi_state_cache(void)
 	INIT_LIST_HEAD(&pi_state->list);
 	/* pi_mutex gets initialized later */
 	pi_state->owner = NULL;
-	atomic_set(&pi_state->refcount, 1);
+	refcount_set(&pi_state->refcount, 1);
 	pi_state->key = FUTEX_KEY_INIT;
 
 	current->pi_state_cache = pi_state;
@@ -814,7 +815,7 @@ static struct futex_pi_state *alloc_pi_state(void)
 
 static void get_pi_state(struct futex_pi_state *pi_state)
 {
-	WARN_ON_ONCE(!atomic_inc_not_zero(&pi_state->refcount));
+	refcount_inc(&pi_state->refcount);
 }
 
 /*
@@ -828,7 +829,7 @@ static void put_pi_state(struct futex_pi_state *pi_state)
 	if (!pi_state)
 		return;
 
-	if (!atomic_dec_and_test(&pi_state->refcount))
+	if (!refcount_dec_and_test(&pi_state->refcount))
 		return;
 
 	/*
@@ -852,7 +853,7 @@ static void put_pi_state(struct futex_pi_state *pi_state)
 		 * refcount is at 0 - put it back to 1.
 		 */
 		pi_state->owner = NULL;
-		atomic_set(&pi_state->refcount, 1);
+		refcount_set(&pi_state->refcount, 1);
 		current->pi_state_cache = pi_state;
 	}
 }
@@ -1046,7 +1047,7 @@ static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
 	 * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
 	 * free pi_state before we can take a reference ourselves.
 	 */
-	WARN_ON(!atomic_read(&pi_state->refcount));
+	WARN_ON(!refcount_read(&pi_state->refcount));
 
 	/*
 	 * Now that we have a pi_state, we can acquire wait_lock
-- 
2.7.4

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 00/15] v4 kernel core pieces refcount conversions Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 14/15] futex: convert futex_pi_state.refcount to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 03/15] sched: convert user_struct.__count to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 12/15] groups: convert group_info.usage to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 11/15] nsproxy: convert nsproxy.count to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 13/15] creds: convert cred.usage to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 10/15] uprobes: convert uprobe.ref to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 04/15] sched: convert numa_group.refcount to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 06/15] sched/task_struct: convert task_struct.stack_refcount to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:40 +0200
  [PATCH 09/15] perf/ring_buffer: convert ring_buffer.aux_refcount to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:50 +0200
  [PATCH 07/15] perf: convert perf_event_context.refcount to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:50 +0200
  [PATCH 08/15] perf/ring_buffer: convert ring_buffer.refcount to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:50 +0200
  [PATCH 05/15] sched/task_struct: convert task_struct.usage to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:50 +0200
  [PATCH 02/15] sched: convert signal_struct.sigcnt to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:50 +0200
  [PATCH 01/15] sched: convert sighand_struct.count to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-07-18 12:50 +0200

csiph-web