CVE-2017-12146: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') (CWE-362)
value: 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.
CWE description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
CVSS v3 base score: 7
Published: 2017-09-08T19:29Z
Repository: https://github.com/torvalds/linux
Language: C
Files changed: drivers/base/platform.c
Commit date: 2017-05-25 15:30:12 +0200
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>

Diff: diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index a102152301c832..97332d094fe23a 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -864,11 +864,11 @@ static DEVICE_ATTR_RO(modalias);
 static ssize_t driver_override_store(struct device *dev,
 				     struct device_attribute *attr,
 				     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;
 
 	driver_override = kstrndup(buf, count, GFP_KERNEL);
@@ -877,28 +877,35 @@ static ssize_t driver_override_store(struct device *dev,
 
 	cp = strchr(driver_override, '\n');
 	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);
 
 	return count;
 }
 
 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);
 
 
 static struct attribute *platform_dev_attrs[] = {

Vulnerable code: 	char *driver_override, *old = pdev->driver_override, *cp;
	return sprintf(buf, "%s\n", pdev->driver_override);
Fixed code: 	char *driver_override, *old, *cp;
	device_lock(dev);
	old = pdev->driver_override;
	device_unlock(dev);
	ssize_t len;
	device_lock(dev);
	len = sprintf(buf, "%s\n", pdev->driver_override);
	device_unlock(dev);
	return len;