From 8851d0b7ac8ca15fede0184bd37ee32dde4d9490 Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Tue, 14 Mar 2017 21:39:16 +0000 Subject: [PATCH] ucount: Remove the atomicity from ucount->count (CVE-2017-6874) ...and avoid an ABI change. --- debian/changelog | 2 + ...move-the-atomicity-from-ucount-count.patch | 83 +++++++++++++++++++ ...oid-abi-change-for-cve-2017-6874-fix.patch | 23 +++++ debian/patches/series | 2 + 4 files changed, 110 insertions(+) create mode 100644 debian/patches/bugfix/all/ucount-remove-the-atomicity-from-ucount-count.patch create mode 100644 debian/patches/debian/userns-avoid-abi-change-for-cve-2017-6874-fix.patch diff --git a/debian/changelog b/debian/changelog index d836abfe0..050fd6091 100644 --- a/debian/changelog +++ b/debian/changelog @@ -145,6 +145,8 @@ linux (4.9.14-1) UNRELEASED; urgency=medium * tty: n_hdlc: get rid of racy n_hdlc.tbuf (CVE-2017-2636) * [rt] Refresh one patch that had a textual conflict with 4.9.14 * Ignore ABI changes in rds and ufshcd, not useful to OOT modules + * ucount: Remove the atomicity from ucount->count (CVE-2017-6874) + * userns: Avoid ABI change for CVE-2017-6874 fix [ Salvatore Bonaccorso ] * ACPI / EC: Use busy polling mode when GPE is not enabled. diff --git a/debian/patches/bugfix/all/ucount-remove-the-atomicity-from-ucount-count.patch b/debian/patches/bugfix/all/ucount-remove-the-atomicity-from-ucount-count.patch new file mode 100644 index 000000000..0a40cfb22 --- /dev/null +++ b/debian/patches/bugfix/all/ucount-remove-the-atomicity-from-ucount-count.patch @@ -0,0 +1,83 @@ +From: "Eric W. Biederman" +Date: Sun, 5 Mar 2017 15:03:22 -0600 +Subject: ucount: Remove the atomicity from ucount->count +Origin: https://git.kernel.org/linus/040757f738e13caaa9c5078bca79aa97e11dde88 +Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2017-6874 + +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. + +Cc: stable@vger.kernel.org +Fixes: f6b2db1a3e8d ("userns: Make the count of user namespaces per user") +Reported-by: JongHwan Kim +Reported-by: Dmitry Vyukov +Reviewed-by: Andrei Vagin +Signed-off-by: "Eric W. Biederman" +--- + 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) diff --git a/debian/patches/debian/userns-avoid-abi-change-for-cve-2017-6874-fix.patch b/debian/patches/debian/userns-avoid-abi-change-for-cve-2017-6874-fix.patch new file mode 100644 index 000000000..58a6a588d --- /dev/null +++ b/debian/patches/debian/userns-avoid-abi-change-for-cve-2017-6874-fix.patch @@ -0,0 +1,23 @@ +From: Ben Hutchings +Date: Tue, 14 Mar 2017 21:35:33 +0000 +Subject: userns: Avoid ABI change for CVE-2017-6874 fix + +The type of ucounts::count changed from atomic_t to int. But they're +the same size, and it's only accessed within kernel/ucount.c, so hide +the change from genksyms. + +--- +--- a/include/linux/user_namespace.h ++++ b/include/linux/user_namespace.h +@@ -65,7 +65,11 @@ struct ucounts { + struct hlist_node node; + struct user_namespace *ns; + kuid_t uid; ++#ifdef __GENKSYMS__ ++ atomic_t count; ++#else + int count; ++#endif + atomic_t ucount[UCOUNT_COUNTS]; + }; + diff --git a/debian/patches/series b/debian/patches/series index 7156e7f2d..e2f635a5b 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -116,6 +116,7 @@ bugfix/x86/kvm-fix-page-struct-leak-in-handle_vmon.patch debian/time-mark-timer_stats-as-broken.patch bugfix/all/sctp-deny-peeloff-operation-on-asocs-with-threads-sl.patch bugfix/all/tty-n_hdlc-get-rid-of-racy-n_hdlc.patch +bugfix/all/ucount-remove-the-atomicity-from-ucount-count.patch # Fix exported symbol versions bugfix/ia64/revert-ia64-move-exports-to-definitions.patch @@ -129,6 +130,7 @@ bugfix/all/module-disable-matching-missing-version-crc.patch # ABI maintenance debian/net-avoid-abi-change-for-min_header_len.patch +debian/userns-avoid-abi-change-for-cve-2017-6874-fix.patch # Tools bug fixes bugfix/all/usbip-document-tcp-wrappers.patch