Vulnerability: The driver_override implementation in drivers/base/platform.c in the Linux kernel before 4.12.1 allows local users to gain privileges by leveraging a race condition between a read operation and a store operation that involve different overrides.
Vulnerability ID: CVE-2017-12146
CWE: CWE-362
Severity score: 10
Project: https://github.com/torvalds/linux
Published: 2017-09-08
File: drivers/base/platform.c
Commit message: driver core: platform: fix race condition with driver_override

The driver_override implementation is susceptible to race condition when
different threads are reading vs storing a different driver override.
Add locking to avoid race condition.

Fixes: 3d713e0e382e ("driver core: platform: add device binding path 'driver_override'")
Cc: stable@vger.kernel.org
Signed-off-by: Adrian Salido <salidoa@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Commit author: Adrian Salido

Vulnerable version (before fix):
 static ssize_t driver_override_store(struct device *dev, 				     const char *buf, size_t count) { 	struct platform_device *pdev = to_platform_device(dev); 	char *driver_override, *old = pdev->driver_override, *cp;  	if (count > PATH_MAX) 		return -EINVAL; static ssize_t driver_override_store(struct device *dev, 	if (cp) 		*cp = '\0';  	if (strlen(driver_override)) { 		pdev->driver_override = driver_override; 	} else { 		kfree(driver_override); 		pdev->driver_override = NULL; 	}  	kfree(old);  static ssize_t driver_override_show(struct device *dev, 				    struct device_attribute *attr, char *buf) { 	struct platform_device *pdev = to_platform_device(dev);  	return sprintf(buf, "%s\n", pdev->driver_override); } static DEVICE_ATTR_RW(driver_override); 

Fixed version (after fix):
 static ssize_t driver_override_store(struct device *dev, 				     const char *buf, size_t count) { 	struct platform_device *pdev = to_platform_device(dev); 	char *driver_override, *old, *cp;  	if (count > PATH_MAX) 		return -EINVAL; static ssize_t driver_override_store(struct device *dev, 	if (cp) 		*cp = '\0';  	device_lock(dev); 	old = pdev->driver_override; 	if (strlen(driver_override)) { 		pdev->driver_override = driver_override; 	} else { 		kfree(driver_override); 		pdev->driver_override = NULL; 	} 	device_unlock(dev);  	kfree(old);  static ssize_t driver_override_show(struct device *dev, 				    struct device_attribute *attr, char *buf) { 	struct platform_device *pdev = to_platform_device(dev); 	ssize_t len;  	device_lock(dev); 	len = sprintf(buf, "%s\n", pdev->driver_override); 	device_unlock(dev); 	return len; } static DEVICE_ATTR_RW(driver_override); 

Patch:
patch: @@ -866,7 +866,7 @@ static ssize_t driver_override_store(struct device *dev,
 				     const char *buf, size_t count)
 {
 	struct platform_device *pdev = to_platform_device(dev);
-	char *driver_override, *old = pdev->driver_override, *cp;
+	char *driver_override, *old, *cp;
 
 	if (count > PATH_MAX)
 		return -EINVAL;
@@ -879,12 +879,15 @@ static ssize_t driver_override_store(struct device *dev,
 	if (cp)
 		*cp = '\0';
 
+	device_lock(dev);
+	old = pdev->driver_override;
 	if (strlen(driver_override)) {
 		pdev->driver_override = driver_override;
 	} else {
 		kfree(driver_override);
 		pdev->driver_override = NULL;
 	}
+	device_unlock(dev);
 
 	kfree(old);
 
@@ -895,8 +898,12 @@ static ssize_t driver_override_show(struct device *dev,
 				    struct device_attribute *attr, char *buf)
 {
 	struct platform_device *pdev = to_platform_device(dev);
+	ssize_t len;
 
-	return sprintf(buf, "%s\n", pdev->driver_override);
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", pdev->driver_override);
+	device_unlock(dev);
+	return len;
 }
 static DEVICE_ATTR_RW(driver_override);
path: drivers/base/platform.c