Skip to contentSkip to conversation

wifi: mwifiex: Replace strlcpy with strscpy

PATCH Applied Azeem Shaikh 4 participants 7 messages Jul 10, 2023 – Jul 27, 2023 Marvell mwifiex wireless driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: mwifiex: Replace strlcpy with strscpy

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy().

Direct replacement is safe here since return value of -errno is used to check for truncation instead of sizeof(dest).

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89

---
 drivers/net/wireless/marvell/mwifiex/main.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/net/wireless/marvell/mwifiex/main.c +2 -3
index 1cd9d20cca16..8d3c4bcf9c89 100644
@@ -725,9 +725,8 @@ static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
 	 * manufacturing mode is enabled
 	 */
 	if (mfg_mode) {
-		if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
-			    sizeof(adapter->fw_name)) >=
-			    sizeof(adapter->fw_name)) {
+		if (strscpy(adapter->fw_name, MFG_FIRMWARE,
+			    sizeof(adapter->fw_name)) < 0) {
 			pr_err("%s: fw_name too long!\n", __func__);
 			return -1;
 		}
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Azeem Shaikh

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy().

Quoted 7 lines

Direct replacement is safe here since return value of -errno is used to check for truncation instead of sizeof(dest).

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89

Signature · 1 line
Kees Cook
Applied
#2 lore raw
In reply to Azeem Shaikh
Quoted 8 lines · Azeem Shaikh

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy().

[...]

Applied, thanks!

[1/1] wifi: mwifiex: Replace strlcpy with strscpy

      https://git.kernel.org/kees/c/5469fb73e96d

Best regards,

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 8 lines · Kees Cook
Quoted 7 lines · Azeem Shaikh

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy().

Quoted 6 lines
Quoted 1 line

[...]

Applied, thanks!

[1/1] wifi: mwifiex: Replace strlcpy with strscpy

      https://git.kernel.org/kees/c/5469fb73e96d

And the same question here, why are you taking wifi patches without acks? And this already fixed differently in wireless-next so our trees conflict now:

https://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git/commit/?id=caf9ead2c7d06fd7aa4cb48bd569ad61db9a0b4a

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#4 lore raw
Quoted 14 lines · Kalle Valo
Quoted 12 lines · Kees Cook
Quoted 8 lines · Azeem Shaikh

strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy().

[...]

Applied, thanks!

Quoted 8 lines
Quoted 2 lines

[1/1] wifi: mwifiex: Replace strlcpy with strscpy

      https://git.kernel.org/kees/c/5469fb73e96d

And the same question here, why are you taking wifi patches without acks? And this already fixed differently in wireless-next so our trees conflict now:

https://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git/commit/?id=caf9ead2c7d06fd7aa4cb48bd569ad61db9a0b4a

Thanks for pointing that out! I saw no feedback on Azeem's patch, so it looked like it was being ignored.

For the patch you linked to -- it's okay to have lost the overflow detection and warning?

Regardless, I will drop this from my tree.

-Kees

Signature · 1 line
Kees Cook
maintainer
#5 lore raw
Quoted 6 lines · Kees Cook
Quoted 1 line · Kalle Valo

https://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git/commit/?id=caf9ead2c7d06fd7aa4cb48bd569ad61db9a0b4a

For the patch you linked to -- it's okay to have lost the overflow detection and warning?

It's a fixed constant string being copied into a larger fixed array. It really doesn't matter functionality-wise. At best, the error check would be useful for human readers or automated tools. So I didn't bother complaining one way or another, as it took enough tries for the submitter to get things right in the first place.

Brian

#6 lore raw
Quoted 5 lines · Brian Norris
Quoted 4 lines · Kees Cook
Quoted 1 line · Kalle Valo

https://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git/commit/?id=caf9ead2c7d06fd7aa4cb48bd569ad61db9a0b4a

Quoted 8 lines
Quoted 2 lines

For the patch you linked to -- it's okay to have lost the overflow detection and warning?

It's a fixed constant string being copied into a larger fixed array. It really doesn't matter functionality-wise. At best, the error check would be useful for human readers or automated tools. So I didn't bother complaining one way or another, as it took enough tries for the submitter to get things right in the first place.

Gotcha. Thanks!

Signature · 1 line
Kees Cook