Skip to contentSkip to conversation

w1: therm: Fix off-by-one buffer overflow in alarms_store

PATCH Superseded Thorsten Blum 3 participants 7 messages Nov 11, 2025 – Dec 16, 2025

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 w1: therm: Fix off-by-one buffer overflow in alarms_store

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
Compile-tested only.

Changes in v4:
- Use simple_strtoll because kstrtoint also parses long long internally
- Return -ERANGE in addition to -EINVAL to match kstrtoint's behavior
- Remove any changes unrelated to fixing the buffer overflow (Krzysztof) while maintaining the same behavior and return values as before
- Link to v3: https://lore.kernel.org/lkml/[email protected]/

Changes in v3:
- Add integer range check for 'temp' to match kstrtoint() behavior
- Explicitly cast 'temp' to int when calling int_to_short()
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Fix buffer overflow instead of truncating the copy using strscpy()
- Parse buffer directly using simple_strtol() as suggested by David
- Update patch subject and description
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/w1/slaves/w1_therm.c | 64 ++++++++++++------------------------
 1 file changed, 21 insertions(+), 43 deletions(-)
drivers/w1/slaves/w1_therm.c +21 -43
index 9ccedb3264fb..5707fa34e804 100644
@@ -1836,55 +1836,36 @@ static ssize_t alarms_store(struct device *device,
 	struct w1_slave *sl = dev_to_w1_slave(device);
 	struct therm_info info;
 	u8 new_config_register[3];	/* array of data to be written */
-	int temp, ret;
-	char *token = NULL;
+	long long temp;
+	int ret = 0;
 	s8 tl, th;	/* 1 byte per value + temp ring order */
-	char *p_args, *orig;
-
-	p_args = orig = kmalloc(size, GFP_KERNEL);
-	/* Safe string copys as buf is const */
-	if (!p_args) {
-		dev_warn(device,
-			"%s: error unable to allocate memory %d\n",
-			__func__, -ENOMEM);
-		return size;
-	}
-	strcpy(p_args, buf);
-
-	/* Split string using space char */
-	token = strsep(&p_args, " ");
-
-	if (!token)	{
-		dev_info(device,
-			"%s: error parsing args %d\n", __func__, -EINVAL);
-		goto free_m;
-	}
-
-	/* Convert 1st entry to int */
-	ret = kstrtoint (token, 10, &temp);
+	const char *p = buf;
+	char *endp;
+
+	temp = simple_strtoll(p, &endp, 10);
+	if (p == endp || *endp != ' ')
+		ret = -EINVAL;
+	else if (temp < INT_MIN || temp > INT_MAX)
+		ret = -ERANGE;
 	if (ret) {
 		dev_info(device,
 			"%s: error parsing args %d\n", __func__, ret);
-		goto free_m;
+		goto err;
 	}
 
 	tl = int_to_short(temp);
 
-	/* Split string using space char */
-	token = strsep(&p_args, " ");
-	if (!token)	{
-		dev_info(device,
-			"%s: error parsing args %d\n", __func__, -EINVAL);
-		goto free_m;
-	}
-	/* Convert 2nd entry to int */
-	ret = kstrtoint (token, 10, &temp);
+	p = endp + 1;
+	temp = simple_strtoll(p, &endp, 10);
+	if (p == endp)
+		ret = -EINVAL;
+	else if (temp < INT_MIN || temp > INT_MAX)
+		ret = -ERANGE;
 	if (ret) {
 		dev_info(device,
 			"%s: error parsing args %d\n", __func__, ret);
-		goto free_m;
+		goto err;
 	}
-
 	/* Prepare to cast to short by eliminating out of range values */
 	th = int_to_short(temp);
 
@@ -1905,7 +1886,7 @@ static ssize_t alarms_store(struct device *device,
 		dev_info(device,
 			"%s: error reading from the slave device %d\n",
 			__func__, ret);
-		goto free_m;
+		goto err;
 	}
 
 	/* Write data in the device RAM */
@@ -1913,7 +1894,7 @@ static ssize_t alarms_store(struct device *device,
 		dev_info(device,
 			"%s: Device not supported by the driver %d\n",
 			__func__, -ENODEV);
-		goto free_m;
+		goto err;
 	}
 
 	ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
@@ -1922,10 +1903,7 @@ static ssize_t alarms_store(struct device *device,
 			"%s: error writing to the slave device %d\n",
 			__func__, ret);
 
-free_m:
-	/* free allocated memory */
-	kfree(orig);
-
+err:
 	return size;
 }
 
Signature · 1 line
2.51.1
#1 lore raw

Hi Krzysztof,

Quoted 36 lines · Thorsten Blum

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
Compile-tested only.

Changes in v4:
- Use simple_strtoll because kstrtoint also parses long long internally
- Return -ERANGE in addition to -EINVAL to match kstrtoint's behavior
- Remove any changes unrelated to fixing the buffer overflow (Krzysztof) while maintaining the same behavior and return values as before
- Link to v3: https://lore.kernel.org/lkml/[email protected]/

Changes in v3:
- Add integer range check for 'temp' to match kstrtoint() behavior
- Explicitly cast 'temp' to int when calling int_to_short()
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Fix buffer overflow instead of truncating the copy using strscpy()
- Parse buffer directly using simple_strtol() as suggested by David
- Update patch subject and description
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
---
drivers/w1/slaves/w1_therm.c | 64 ++++++++++++------------------------

Quoted 2 lines
1 file changed, 21 insertions(+), 43 deletions(-)

[...]

Could you take another look at v4?

Thanks,
Thorsten

#2 lore raw
In reply to Thorsten Blum
Quoted 68 lines · Thorsten Blum

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
Compile-tested only.

Changes in v4:
- Use simple_strtoll because kstrtoint also parses long long internally
- Return -ERANGE in addition to -EINVAL to match kstrtoint's behavior
- Remove any changes unrelated to fixing the buffer overflow (Krzysztof) while maintaining the same behavior and return values as before
- Link to v3: https://lore.kernel.org/lkml/[email protected]/

Changes in v3:
- Add integer range check for 'temp' to match kstrtoint() behavior
- Explicitly cast 'temp' to int when calling int_to_short()
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Fix buffer overflow instead of truncating the copy using strscpy()
- Parse buffer directly using simple_strtol() as suggested by David
- Update patch subject and description
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/w1/slaves/w1_therm.c | 64 ++++++++++++------------------------
 1 file changed, 21 insertions(+), 43 deletions(-)
drivers/w1/slaves/w1_therm.c +2 -19
index 9ccedb3264fb..5707fa34e804 100644
@@ -1836,55 +1836,36 @@ static ssize_t alarms_store(struct device *device,
 	struct w1_slave *sl = dev_to_w1_slave(device);
 	struct therm_info info;
 	u8 new_config_register[3];	/* array of data to be written */
-	int temp, ret;
-	char *token = NULL;
+	long long temp;
+	int ret = 0;
 	s8 tl, th;	/* 1 byte per value + temp ring order */
-	char *p_args, *orig;
-
-	p_args = orig = kmalloc(size, GFP_KERNEL);
-	/* Safe string copys as buf is const */
-	if (!p_args) {
-		dev_warn(device,
-			"%s: error unable to allocate memory %d\n",
-			__func__, -ENOMEM);
-		return size;
-	}
-	strcpy(p_args, buf);
-
-	/* Split string using space char */
-	token = strsep(&p_args, " ");
-
-	if (!token)	{
-		dev_info(device,

drivers/w1/slaves/w1_therm.c static ssize_t alarms_store(struct device *device, · line 1845

Quoted 10 lines
drivers/w1/slaves/w1_therm.c +4 -6
-			"%s: error parsing args %d\n", __func__, -EINVAL);
-		goto free_m;
-	}
-
-	/* Convert 1st entry to int */
-	ret = kstrtoint (token, 10, &temp);
+	const char *p = buf;
+	char *endp;
+
+	temp = simple_strtoll(p, &endp, 10);

Why using this, instead of explicitly encouraged kstrtoll()?

drivers/w1/slaves/w1_therm.c static ssize_t alarms_store(struct device *device, · line 1853

Quoted 9 lines
drivers/w1/slaves/w1_therm.c +5 -1
+	if (p == endp || *endp != ' ')
+		ret = -EINVAL;
+	else if (temp < INT_MIN || temp > INT_MAX)
+		ret = -ERANGE;
 	if (ret) {
 		dev_info(device,
 			"%s: error parsing args %d\n", __func__, ret);
-		goto free_m;
+		goto err;

So this is just return size.

Best regards,
Krzysztof

#3 lore raw
Quoted 73 lines · On Tue, 16 Dec 2025 08:11:13 +0100 Krzysztof Kozlowski <[email protected]> wrote:
Quoted 72 lines · Thorsten Blum

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
Compile-tested only.

Changes in v4:
- Use simple_strtoll because kstrtoint also parses long long internally
- Return -ERANGE in addition to -EINVAL to match kstrtoint's behavior
- Remove any changes unrelated to fixing the buffer overflow (Krzysztof) while maintaining the same behavior and return values as before
- Link to v3: https://lore.kernel.org/lkml/[email protected]/

Changes in v3:
- Add integer range check for 'temp' to match kstrtoint() behavior
- Explicitly cast 'temp' to int when calling int_to_short()
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Fix buffer overflow instead of truncating the copy using strscpy()
- Parse buffer directly using simple_strtol() as suggested by David
- Update patch subject and description
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/w1/slaves/w1_therm.c | 64 ++++++++++++------------------------
 1 file changed, 21 insertions(+), 43 deletions(-)
drivers/w1/slaves/w1_therm.c +2 -23
index 9ccedb3264fb..5707fa34e804 100644
@@ -1836,55 +1836,36 @@ static ssize_t alarms_store(struct device *device,
 	struct w1_slave *sl = dev_to_w1_slave(device);
 	struct therm_info info;
 	u8 new_config_register[3];	/* array of data to be written */
-	int temp, ret;
-	char *token = NULL;
+	long long temp;
+	int ret = 0;
 	s8 tl, th;	/* 1 byte per value + temp ring order */
-	char *p_args, *orig;
-
-	p_args = orig = kmalloc(size, GFP_KERNEL);
-	/* Safe string copys as buf is const */
-	if (!p_args) {
-		dev_warn(device,
-			"%s: error unable to allocate memory %d\n",
-			__func__, -ENOMEM);
-		return size;
-	}
-	strcpy(p_args, buf);
-
-	/* Split string using space char */
-	token = strsep(&p_args, " ");
-
-	if (!token)	{
-		dev_info(device,
-			"%s: error parsing args %d\n", __func__, -EINVAL);
-		goto free_m;
-	}
-
Quoted 8 lines

drivers/w1/slaves/w1_therm.c static ssize_t alarms_store(struct device *device, · line 1845

Quoted 6 lines
drivers/w1/slaves/w1_therm.c +4 -2
-	/* Convert 1st entry to int */
-	ret = kstrtoint (token, 10, &temp);
+	const char *p = buf;
+	char *endp;
+
+	temp = simple_strtoll(p, &endp, 10);  

Why using this, instead of explicitly encouraged kstrtoll()?

Because the code needs to look at the terminating character. The kstrtoxxx() family only support buffers that contain a single value. While they return an indication of 'overflow' they are useless for more general parameter parsing.

The simple_strtoxxx() could detect overflow and then set 'endp' to the digit that make the value too big - which should give an error provided the callers checks the separator.

I don't know the full history of these functions...

David

Quoted 16 lines

drivers/w1/slaves/w1_therm.c static ssize_t alarms_store(struct device *device, · line 1853

Quoted 10 lines
drivers/w1/slaves/w1_therm.c +5 -1
+	if (p == endp || *endp != ' ')
+		ret = -EINVAL;
+	else if (temp < INT_MIN || temp > INT_MAX)
+		ret = -ERANGE;
 	if (ret) {
 		dev_info(device,
 			"%s: error parsing args %d\n", __func__, ret);
-		goto free_m;
+		goto err;  

So this is just return size.

Best regards,
Krzysztof

#5 lore raw
In reply to Krzysztof Kozlowski
Quoted 18 lines · Krzysztof Kozlowski
Quoted 17 lines · Thorsten Blum

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
[...]

Quoted 11 lines

Review

Quoted 9 lines
drivers/w1/slaves/w1_therm.c +4 -0
+	if (p == endp || *endp != ' ')
+		ret = -EINVAL;
+	else if (temp < INT_MIN || temp > INT_MAX)
+		ret = -ERANGE;
	if (ret) {
		dev_info(device,
			"%s: error parsing args %d\n", __func__, ret);
(quoted diff) +1 -1
-		goto free_m;
+		goto err;

So this is just return size.

Yes, all 'goto err' could be replaced with 'return size'. I only renamed the label to keep the changes minimal.

#4 lore raw
In reply to David Laight
Quoted 85 lines · David Laight
Quoted 73 lines · On Tue, 16 Dec 2025 08:11:13 +0100 Krzysztof Kozlowski <[email protected]> wrote:
Quoted 72 lines · Thorsten Blum

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
Compile-tested only.

Changes in v4:
- Use simple_strtoll because kstrtoint also parses long long internally
- Return -ERANGE in addition to -EINVAL to match kstrtoint's behavior
- Remove any changes unrelated to fixing the buffer overflow (Krzysztof) while maintaining the same behavior and return values as before
- Link to v3: https://lore.kernel.org/lkml/[email protected]/

Changes in v3:
- Add integer range check for 'temp' to match kstrtoint() behavior
- Explicitly cast 'temp' to int when calling int_to_short()
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Fix buffer overflow instead of truncating the copy using strscpy()
- Parse buffer directly using simple_strtol() as suggested by David
- Update patch subject and description
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/w1/slaves/w1_therm.c | 64 ++++++++++++------------------------
 1 file changed, 21 insertions(+), 43 deletions(-)
drivers/w1/slaves/w1_therm.c +2 -23
index 9ccedb3264fb..5707fa34e804 100644
@@ -1836,55 +1836,36 @@ static ssize_t alarms_store(struct device *device,
 	struct w1_slave *sl = dev_to_w1_slave(device);
 	struct therm_info info;
 	u8 new_config_register[3];	/* array of data to be written */
-	int temp, ret;
-	char *token = NULL;
+	long long temp;
+	int ret = 0;
 	s8 tl, th;	/* 1 byte per value + temp ring order */
-	char *p_args, *orig;
-
-	p_args = orig = kmalloc(size, GFP_KERNEL);
-	/* Safe string copys as buf is const */
-	if (!p_args) {
-		dev_warn(device,
-			"%s: error unable to allocate memory %d\n",
-			__func__, -ENOMEM);
-		return size;
-	}
-	strcpy(p_args, buf);
-
-	/* Split string using space char */
-	token = strsep(&p_args, " ");
-
-	if (!token)	{
-		dev_info(device,
-			"%s: error parsing args %d\n", __func__, -EINVAL);
-		goto free_m;
-	}
-
Quoted 8 lines

drivers/w1/slaves/w1_therm.c static ssize_t alarms_store(struct device *device, · line 1845

Quoted 6 lines
drivers/w1/slaves/w1_therm.c +4 -2
-	/* Convert 1st entry to int */
-	ret = kstrtoint (token, 10, &temp);
+	const char *p = buf;
+	char *endp;
+
+	temp = simple_strtoll(p, &endp, 10);  

Why using this, instead of explicitly encouraged kstrtoll()?

Quoted 8 lines

Because the code needs to look at the terminating character. The kstrtoxxx() family only support buffers that contain a single value. While they return an indication of 'overflow' they are useless for more general parameter parsing.

The simple_strtoxxx() could detect overflow and then set 'endp' to the digit that make the value too big - which should give an error provided the callers checks the separator.

Yes, there are two values here, so obviously this is right.

Best regards,
Krzysztof

#6 lore raw
In reply to Thorsten Blum
Quoted 31 lines · Thorsten Blum
Quoted 18 lines · Krzysztof Kozlowski
Quoted 17 lines · Thorsten Blum

The sysfs buffer passed to alarms_store() is allocated with 'size + 1' bytes and a NUL terminator is appended. However, the 'size' argument does not account for this extra byte. The original code then allocated 'size' bytes and used strcpy() to copy 'buf', which always writes one byte past the allocated buffer since strcpy() copies until the NUL terminator at index 'size'.

Fix this by parsing the 'buf' parameter directly using simple_strtoll() without allocating any intermediate memory or string copying. This removes the overflow while simplifying the code.

Fixes e2c94d6f5720 ("w1_therm: adding alarm sysfs entry")

---
[...]

Quoted 11 lines

Review

Quoted 9 lines
drivers/w1/slaves/w1_therm.c +4 -0
+	if (p == endp || *endp != ' ')
+		ret = -EINVAL;
+	else if (temp < INT_MIN || temp > INT_MAX)
+		ret = -ERANGE;
	if (ret) {
		dev_info(device,
			"%s: error parsing args %d\n", __func__, ret);
(quoted diff) +1 -1
-		goto free_m;
+		goto err;

So this is just return size.

Quoted 2 lines

Yes, all 'goto err' could be replaced with 'return size'. I only renamed the label to keep the changes minimal.

You do not write commits to have minimal changes. That's not the goal. You organize commits in logical chunks doing one thing and doing it correctly. Empty goto label is not correct, thus should not stay.

Best regards,
Krzysztof