1 From b3b7c4795ccab5be71f080774c45bbbcc75c2aaf Mon Sep 17 00:00:00 2001
2 From: Seunghun Han <kkamagui@gmail.com>
3 Date: Tue, 6 Mar 2018 15:21:43 +0100
4 Subject: x86/MCE: Serialize sysfs changes
6 From: Seunghun Han <kkamagui@gmail.com>
8 commit b3b7c4795ccab5be71f080774c45bbbcc75c2aaf upstream.
10 The check_interval file in
12 /sys/devices/system/machinecheck/machinecheck<cpu number>
14 directory is a global timer value for MCE polling. If it is changed by one
15 CPU, mce_restart() broadcasts the event to other CPUs to delete and restart
16 the MCE polling timer and __mcheck_cpu_init_timer() reinitializes the
19 If more than one CPU writes a specific value to the check_interval file
20 concurrently, mce_timer is not protected from such concurrent accesses and
21 all kinds of explosions happen. Since only root can write to those sysfs
22 variables, the issue is not a big deal security-wise.
24 However, concurrent writes to these configuration variables is void of
25 reason so the proper thing to do is to serialize the access with a mutex.
29 - Make store_int_with_restart() use device_store_ulong() to filter out
31 - Limit min interval to 1 second
33 - Massage commit message
35 Signed-off-by: Seunghun Han <kkamagui@gmail.com>
36 Signed-off-by: Borislav Petkov <bp@suse.de>
37 Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
38 Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
39 Cc: Tony Luck <tony.luck@intel.com>
40 Cc: linux-edac <linux-edac@vger.kernel.org>
41 Cc: stable@vger.kernel.org
42 Link: http://lkml.kernel.org/r/20180302202706.9434-1-kkamagui@gmail.com
43 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
46 arch/x86/kernel/cpu/mcheck/mce.c | 22 +++++++++++++++++++++-
47 1 file changed, 21 insertions(+), 1 deletion(-)
49 --- a/arch/x86/kernel/cpu/mcheck/mce.c
50 +++ b/arch/x86/kernel/cpu/mcheck/mce.c
53 static DEFINE_MUTEX(mce_log_mutex);
55 +/* sysfs synchronization */
56 +static DEFINE_MUTEX(mce_sysfs_mutex);
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/mce.h>
61 @@ -2080,6 +2083,7 @@ static ssize_t set_ignore_ce(struct devi
62 if (kstrtou64(buf, 0, &new) < 0)
65 + mutex_lock(&mce_sysfs_mutex);
66 if (mca_cfg.ignore_ce ^ !!new) {
68 /* disable ce features */
69 @@ -2092,6 +2096,8 @@ static ssize_t set_ignore_ce(struct devi
70 on_each_cpu(mce_enable_ce, (void *)1, 1);
73 + mutex_unlock(&mce_sysfs_mutex);
78 @@ -2104,6 +2110,7 @@ static ssize_t set_cmci_disabled(struct
79 if (kstrtou64(buf, 0, &new) < 0)
82 + mutex_lock(&mce_sysfs_mutex);
83 if (mca_cfg.cmci_disabled ^ !!new) {
86 @@ -2115,6 +2122,8 @@ static ssize_t set_cmci_disabled(struct
87 on_each_cpu(mce_enable_ce, NULL, 1);
90 + mutex_unlock(&mce_sysfs_mutex);
95 @@ -2122,8 +2131,19 @@ static ssize_t store_int_with_restart(st
96 struct device_attribute *attr,
97 const char *buf, size_t size)
99 - ssize_t ret = device_store_int(s, attr, buf, size);
100 + unsigned long old_check_interval = check_interval;
101 + ssize_t ret = device_store_ulong(s, attr, buf, size);
103 + if (check_interval == old_check_interval)
106 + if (check_interval < 1)
107 + check_interval = 1;
109 + mutex_lock(&mce_sysfs_mutex);
111 + mutex_unlock(&mce_sysfs_mutex);