From cbbe8c229211a56d12058b930bf8a25201be2f23 Mon Sep 17 00:00:00 2001
From: Michael Ellerman <mpe@ellerman.id.au>
Date: Wed, 10 Jan 2018 23:36:43 +0530
Subject: [PATCH 11/26] UBUNTU: SAUCE: rfi-flush: Move the logic to avoid a
 redo into the sysfs code

CVE-2017-5754

rfi_flush_enable() includes a check to see if we're already
enabled (or disabled), and in that case does nothing.

But that means calling setup_rfi_flush() a 2nd time doesn't actually
work, which is a bit confusing.

Move that check into the sysfs code, where it really belongs.

Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
---
 arch/powerpc/kernel/setup_64.c | 3 ---
 arch/powerpc/kernel/sysfs.c    | 9 +++++++--
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/kernel/setup_64.c b/arch/powerpc/kernel/setup_64.c
index fce5831..f52190f 100644
--- a/arch/powerpc/kernel/setup_64.c
+++ b/arch/powerpc/kernel/setup_64.c
@@ -763,9 +763,6 @@ static void do_nothing(void *unused)
 
 void rfi_flush_enable(bool enable)
 {
-	if (rfi_flush == enable)
-		return;
-
 	if (enable) {
 		do_rfi_flush_fixups(enabled_flush_types);
 		on_each_cpu(do_nothing, NULL, 1);
diff --git a/arch/powerpc/kernel/sysfs.c b/arch/powerpc/kernel/sysfs.c
index 66025932..2832afd 100644
--- a/arch/powerpc/kernel/sysfs.c
+++ b/arch/powerpc/kernel/sysfs.c
@@ -194,6 +194,7 @@ static ssize_t __used store_rfi_flush(struct device *dev,
 		struct device_attribute *attr, const char *buf,
 		size_t count)
 {
+	bool enable;
 	int val;
 	int ret = 0;
 
@@ -202,12 +203,16 @@ static ssize_t __used store_rfi_flush(struct device *dev,
 		return -EINVAL;
 
 	if (val == 1)
-		rfi_flush_enable(true);
+		enable = true;
 	else if (val == 0)
-		rfi_flush_enable(false);
+		enable = false;
 	else
 		return -EINVAL;
 
+	/* Only do anything if we're changing state */
+	if (enable != rfi_flush)
+		rfi_flush_enable(enable);
+
 	return count;
 }
 
-- 
2.7.4

