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


Groups > linux.kernel > #1602464

[PATCH 4.9 17/44] ucount: Remove the atomicity from ucount->count

From Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Newsgroups linux.kernel
Subject [PATCH 4.9 17/44] ucount: Remove the atomicity from ucount->count
Date 2017-03-16 16:10 +0100
Message-ID <tlFcn-97-41@gated-at.bofh.it> (permalink)
References <tlEJj-84L-19@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Eric W. Biederman <ebiederm@xmission.com>

commit 040757f738e13caaa9c5078bca79aa97e11dde88 upstream.

Always increment/decrement ucount->count under the ucounts_lock.  The
increments are there already and moving the decrements there means the
locking logic of the code is simpler.  This simplification in the
locking logic fixes a race between put_ucounts and get_ucounts that
could result in a use-after-free because the count could go zero then
be found by get_ucounts and then be freed by put_ucounts.

A bug presumably this one was found by a combination of syzkaller and
KASAN.  JongWhan Kim reported the syzkaller failure and Dmitry Vyukov
spotted the race in the code.

Fixes: f6b2db1a3e8d ("userns: Make the count of user namespaces per user")
Reported-by: JongHwan Kim <zzoru007@gmail.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Andrei Vagin <avagin@gmail.com>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 include/linux/user_namespace.h |    2 +-
 kernel/ucount.c                |   18 +++++++++++-------
 2 files changed, 12 insertions(+), 8 deletions(-)

--- a/include/linux/user_namespace.h
+++ b/include/linux/user_namespace.h
@@ -65,7 +65,7 @@ struct ucounts {
 	struct hlist_node node;
 	struct user_namespace *ns;
 	kuid_t uid;
-	atomic_t count;
+	int count;
 	atomic_t ucount[UCOUNT_COUNTS];
 };
 
--- a/kernel/ucount.c
+++ b/kernel/ucount.c
@@ -139,7 +139,7 @@ static struct ucounts *get_ucounts(struc
 
 		new->ns = ns;
 		new->uid = uid;
-		atomic_set(&new->count, 0);
+		new->count = 0;
 
 		spin_lock_irq(&ucounts_lock);
 		ucounts = find_ucounts(ns, uid, hashent);
@@ -150,8 +150,10 @@ static struct ucounts *get_ucounts(struc
 			ucounts = new;
 		}
 	}
-	if (!atomic_add_unless(&ucounts->count, 1, INT_MAX))
+	if (ucounts->count == INT_MAX)
 		ucounts = NULL;
+	else
+		ucounts->count += 1;
 	spin_unlock_irq(&ucounts_lock);
 	return ucounts;
 }
@@ -160,13 +162,15 @@ static void put_ucounts(struct ucounts *
 {
 	unsigned long flags;
 
-	if (atomic_dec_and_test(&ucounts->count)) {
-		spin_lock_irqsave(&ucounts_lock, flags);
+	spin_lock_irqsave(&ucounts_lock, flags);
+	ucounts->count -= 1;
+	if (!ucounts->count)
 		hlist_del_init(&ucounts->node);
-		spin_unlock_irqrestore(&ucounts_lock, flags);
+	else
+		ucounts = NULL;
+	spin_unlock_irqrestore(&ucounts_lock, flags);
 
-		kfree(ucounts);
-	}
+	kfree(ucounts);
 }
 
 static inline bool atomic_inc_below(atomic_t *v, int u)

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


Thread

[PATCH 4.9 00/44] 4.9.16-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:00 +0100
  [PATCH 4.9 42/44] [media] rc: raw decoder for keymap protocol is not loaded on register Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:00 +0100
  [PATCH 4.9 31/44] usb: host: xhci-plat: Fix timeout on removal of hot pluggable xhci controllers Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:00 +0100
  [PATCH 4.9 25/44] usb: dwc3: gadget: make Set Endpoint Configuration macros safe Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:00 +0100
  [PATCH 4.9 27/44] usb: ohci-at91: Do not drop unhandled USB suspend control requests Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:00 +0100
  [PATCH 4.9 23/44] PCI: Prevent VPD access for QLogic ISP2722 Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:00 +0100
  [PATCH 4.9 18/44] efi/arm: Fix boot crash with CONFIG_CPUMASK_OFFSTACK=y Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 17/44] ucount: Remove the atomicity from ucount->count Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 16/44] tracing: Add #undef to fix compile error Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 15/44] cpmac: remove hopeless #warning Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 22/44] powerpc/xics: Work around limitations of OPAL XICS priority handling Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 10/44] mtd: pmcmsp: use kstrndup instead of kmalloc+strncpy Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 24/44] usb: gadget: dummy_hcd: clear usb_gadget region before registration Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 12/44] MIPS: ralink: Remove unused timer functions Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 14/44] bcm63xx_enet: avoid uninitialized variable warning Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 11/44] MIPS: ralink: Cosmetic change to prom_init(). Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  [PATCH 4.9 02/44] USB: serial: digi_acceleport: fix OOB-event processing Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-03-16 16:10 +0100
  Re: [PATCH 4.9 00/44] 4.9.16-stable review Shuah Khan <shuahkh@osg.samsung.com> - 2017-03-16 20:40 +0100
  Re: [PATCH 4.9 00/44] 4.9.16-stable review Guenter Roeck <linux@roeck-us.net> - 2017-03-16 23:40 +0100

csiph-web