[RESEND PATCH-next] staging: rtl8723bs: replace deprecated strncpy with strscpy_pad

xueqin Luo posted 1 patch 2 years ago
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
[RESEND PATCH-next] staging: rtl8723bs: replace deprecated strncpy with strscpy_pad
Posted by xueqin Luo 2 years ago
`strncpy` is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

We should NUL-pad as there are full struct copies happening in places:
|	case NL80211_IFTYPE_MONITOR:
|                ret = rtw_cfg80211_add_monitor_if(padapter,
|						(char *)name, &ndev);
|                break;

A suitable replacement is `strscpy_pad` due to the fact that it
guarantees both NUL-termination and NUL-padding on the destination
buffer.

Additionally, replace size macro `IFNAMSIZ` with sizeof():
|	struct net_device {
|		char			name[IFNAMSIZ];
|		...

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Signed-off-by: xueqin Luo <luoxueqin@kylinos.cn>
---
 drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
index 1ff763c10064..90d521571f0e 100644
--- a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
+++ b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
@@ -2144,8 +2144,7 @@ static int rtw_cfg80211_add_monitor_if(struct adapter *padapter, char *name, str
 	}
 
 	mon_ndev->type = ARPHRD_IEEE80211_RADIOTAP;
-	strncpy(mon_ndev->name, name, IFNAMSIZ);
-	mon_ndev->name[IFNAMSIZ - 1] = 0;
+	strscpy_pad(mon_ndev->name, name, sizeof(mon_ndev->name));
 	mon_ndev->needs_free_netdev = true;
 	mon_ndev->priv_destructor = rtw_ndev_destructor;
 
-- 
2.34.1
Re: [RESEND PATCH-next] staging: rtl8723bs: replace deprecated strncpy with strscpy_pad
Posted by Dan Carpenter 2 years ago
On Wed, Dec 13, 2023 at 09:18:40AM +0800, xueqin Luo wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
> 
> We should NUL-pad as there are full struct copies happening in places:
> |	case NL80211_IFTYPE_MONITOR:
> |                ret = rtw_cfg80211_add_monitor_if(padapter,
> |						(char *)name, &ndev);
> |                break;
> 

I don't see where the full copy is...  What you're looking for is some
place that copies "mon_ndev->name" to the user.

> A suitable replacement is `strscpy_pad` due to the fact that it
> guarantees both NUL-termination and NUL-padding on the destination
> buffer.
> 
> Additionally, replace size macro `IFNAMSIZ` with sizeof():
> |	struct net_device {
> |		char			name[IFNAMSIZ];
> |		...

This would normally be the right move but IFNAMSIZ is a really standard
macro that everyone knows.

When I'm reviewing this code, I later on see a line:

	memcpy(pwdev_priv->ifname_mon, name, IFNAMSIZ + 1);

That means name must be "IFNAMSIZ + 1" characters long or it is a bug.
Please find out where name is set.  We need to know how long it is
before we can go any further.

regards,
dan carpenter