smbd: fix FSCTL_QUERY_NETWORK_INTERFACE_INFO handling
Subsystems (2)
v2 cover smbd: fix FSCTL_QUERY_NETWORK_INTERFACE_INFO handling
From: huhai <[email protected]>
This series fixes several issues in fsctl_query_iface_info_ioctl():
incorrect link speeds, an unlocked IPv6 address-list walk, an invalid final Next offset, and incorrect IPv4/IPv6 interface reporting.
Patch 1 was previously submitted as a standalone patch; this series supersedes that submission.
---
v1: https://lore.kernel.org/all/[email protected]/
Henry Hu (4):
ksmbd: fix link speed query in fsctl_query_iface_info_ioctl()
ksmbd: fix unlocked IPv6 addr_list walk in
fsctl_query_iface_info_ioctl()
ksmbd: fix invalid Next offset in interface info replies
ksmbd: report IPv4 and IPv6 addresses independently in iface query
fs/smb/server/smb2pdu.c | 134 +++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 57 deletions(-)
base-commit: 08df884136f1c1197bab2a27814404fd329d9aac
Signature · 1 line
2.25.1
PATCH v2 1/4 ksmbd: fix link speed query in fsctl_query_iface_info_ioctl()
From: Henry Hu <[email protected]>
fsctl_query_iface_info_ioctl() used to call netdev->ethtool_ops->get_link_ksettings() directly and never checked the return value. If the callback fails, cmd.base.speed may be left uninitialized. Also, SPEED_UNKNOWN (-1) is treated as a numeric link speed and would be converted into a bogus link speed.
Use __ethtool_get_link_ksettings() to acquire the per-netdev ops lock when needed. Fall back to 1Gbps when the query fails, or when the reported speed is 0 or SPEED_UNKNOWN.
--- fs/smb/server/smb2pdu.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/smb/server/smb2pdu.c +3 -4
@@ -10467,6 +10467,7 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,struct sockaddr_storage_rsp *sockaddr_storage;unsigned int flags;unsigned long long speed; +struct ethtool_link_ksettings cmd;rtnl_lock ();for_each_netdev (&init_net, netdev) {
@@ -10501,10 +10502,8 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, nii_rsp->Next =cpu_to_le32 (152 ); nii_rsp->Reserved =0 ; -if (netdev->ethtool_ops->get_link_ksettings) { -struct ethtool_link_ksettings cmd; - - netdev->ethtool_ops->get_link_ksettings (netdev, &cmd); +if (!__ethtool_get_link_ksettings (netdev, &cmd) && + cmd.base.speed && cmd.base.speed !=SPEED_UNKNOWN ) { speed = cmd.base.speed; }else { ksmbd_debug(SMB, "%s %s\n", netdev->name,
Signature · 1 line
2.25.1
PATCH v2 2/4 ksmbd: fix unlocked IPv6 addr_list walk in fsctl_query_iface_info_ioctl()
In reply to huhaiFrom: Henry Hu <[email protected]>
fsctl_query_iface_info_ioctl() walks idev6->addr_list with a plain list_for_each_entry() while holding only rtnl_lock(). rtnl does not serialize all writers of this list: the DAD-failure path (addrconf_dad_failure -> ipv6_add_addr) can insert addresses without holding RTNL. It updates idev->addr_list with list_add_tail_rcu() under write_lock_bh(&idev->lock), so it can race with the unlocked walk.
addr_list is RCU managed, so walk it with list_for_each_entry_rcu() under rcu_read_lock(), matching the other lockless readers of the list.
--- fs/smb/server/smb2pdu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +3 -1
@@ -10545,13 +10545,15 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,if (!idev6)continue ; -list_for_each_entry (ifa, &idev6->addr_list, if_list) { +rcu_read_lock (); +list_for_each_entry_rcu (ifa, &idev6->addr_list, if_list) {if (ifa->flags & (IFA_F_TENTATIVE |IFA_F_DEPRECATED ))continue ;memcpy (ipv6_addr, ifa->addr.s6_addr,16 );break ; } +rcu_read_unlock (); sockaddr_storage->addr6.ScopeId =0 ; nbytes +=sizeof (struct network_interface_info_ioctl_rsp ); }
Signature · 1 line
2.25.1
PATCH v2 3/4 ksmbd: fix invalid Next offset in interface info replies
In reply to huhaiFrom: Henry Hu <[email protected]>
fsctl_query_iface_info_ioctl() assigns nii_rsp to the next output slot before checking whether the interface has an in_device or inet6_dev. If either lookup returns NULL, the loop continues without advancing nbytes, leaving nii_rsp pointing at an uncommitted slot. If no later entry is committed, clearing nii_rsp->Next updates that slot instead of the last returned entry, leaving the latter with a nonzero Next offset.
Fix this by using nbytes to locate the last committed entry and clear its Next field, but only when the response contains at least one entry.
--- fs/smb/server/smb2pdu.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +8 -2
@@ -10560,9 +10560,15 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, }rtnl_unlock (); -/* zero if this is last one */ -if (nii_rsp) +/* +* nii_rsp may point to an entry not included in nbytes if the IPv4 +* or IPv6 device lookup failed. Clear Next in the last returned entry. +*/ +if (nbytes >0 ) { + nii_rsp = (struct network_interface_info_ioctl_rsp *) + &rsp->Buffer[nbytes -sizeof (*nii_rsp)]; nii_rsp->Next =0 ; + } rsp->PersistentFileId =SMB2_NO_FID ; rsp->VolatileFileId =SMB2_NO_FID ;
Signature · 1 line
2.25.1
PATCH v2 4/4 ksmbd: report IPv4 and IPv6 addresses independently in iface query
In reply to huhaiFrom: Henry Hu <[email protected]>
fsctl_query_iface_info_ioctl() appends an entry before checking that a usable address was found, so it advertises unspecified-address
placeholders: an entry containing 0.0.0.0 when an in_device exists but has no primary IPv4 address, and an entry containing :: when an inet6_dev exists but its address list is empty or every address is tentative or deprecated.
The loop also couples IPv6 reporting to IPv4 success: when __in_dev_get_rtnl() returns NULL it continues to the next interface, skipping the IPv6 pass, so an interface without an in_device is dropped even if it has a usable IPv6 address.
Query IPv4 and IPv6 independently and append an entry only when an address is found. Factor out the common entry initialization and query the link speed once per interface instead of once per address family.
--- fs/smb/server/smb2pdu.c | 127 ++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 57 deletions(-)
fs/smb/server/smb2pdu.c +70 -57
@@ -10457,6 +10457,27 @@ static __be32 idev_ipv4_address(struct in_device *idev)return addr; } +static struct network_interface_info_ioctl_rsp * +ksmbd_iface_entry_init (struct smb2_ioctl_rsp *rsp,int nbytes, +struct net_device *netdev,unsigned long long speed) +{ +struct network_interface_info_ioctl_rsp *nii_rsp; + + nii_rsp = (struct network_interface_info_ioctl_rsp *)&rsp->Buffer[nbytes]; + nii_rsp->IfIndex =cpu_to_le32 (netdev->ifindex); + nii_rsp->Capability =0 ; +if (netdev->real_num_tx_queues >1 ) + nii_rsp->Capability |=RSS_CAPABLE ; +if (ksmbd_rdma_capable_netdev (netdev)) + nii_rsp->Capability |=RDMA_CAPABLE ; + nii_rsp->Next =cpu_to_le32 (152 ); + nii_rsp->Reserved =0 ; + nii_rsp->LinkSpeed =cpu_to_le64 (speed); +memset (nii_rsp->SockAddr_Storage,0 ,128 ); + +return nii_rsp; +} +static int fsctl_query_iface_info_ioctl (struct ksmbd_conn *conn,struct smb2_ioctl_rsp *rsp,unsigned int out_buf_len)
@@ -10471,7 +10492,12 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,rtnl_lock ();for_each_netdev (&init_net, netdev) { -bool ipv4_set = false; +struct inet6_ifaddr *ifa; +struct inet6_dev *idev6; +struct in_device *idev; +struct in6_addr ip6 = { }; +bool have_ip6 = false; +__be32 ip4 =0 ;if (netdev->type ==ARPHRD_LOOPBACK )continue ;
@@ -10482,25 +10508,6 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, flags =netif_get_flags (netdev);if (!(flags &IFF_RUNNING ))continue ; -ipv6_retry: -if (out_buf_len < - nbytes +sizeof (struct network_interface_info_ioctl_rsp )) { -rtnl_unlock (); -return -ENOSPC ; - } - - nii_rsp = (struct network_interface_info_ioctl_rsp *) - &rsp->Buffer[nbytes]; - nii_rsp->IfIndex =cpu_to_le32 (netdev->ifindex); - - nii_rsp->Capability =0 ; -if (netdev->real_num_tx_queues >1 ) - nii_rsp->Capability |=RSS_CAPABLE ; -if (ksmbd_rdma_capable_netdev (netdev)) - nii_rsp->Capability |=RDMA_CAPABLE ; - - nii_rsp->Next =cpu_to_le32 (152 ); - nii_rsp->Reserved =0 ;if (!__ethtool_get_link_ksettings (netdev, &cmd) && cmd.base.speed && cmd.base.speed !=SPEED_UNKNOWN ) {
@@ -10510,60 +10517,66 @@ static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, "speed is unknown, defaulting to 1Gb/sec"); speed =SPEED_1000 ; } - speed *=1000000 ; - nii_rsp->LinkSpeed =cpu_to_le64 (speed); - sockaddr_storage = (struct sockaddr_storage_rsp *) - nii_rsp->SockAddr_Storage; -memset (sockaddr_storage,0 ,128 ); +/* +* Query IPv4 and IPv6 independently; emit an entry only when a +* usable address exists, so an interface missing one family is +* still reported for the other and 0.0.0.0 / :: placeholders are +* never advertised. +*/ + idev =__in_dev_get_rtnl (netdev); +if (idev) + ip4 =idev_ipv4_address (idev); -if (!ipv4_set) { -struct in_device *idev; + idev6 =__in6_dev_get (netdev); +if (idev6) { +rcu_read_lock (); +list_for_each_entry_rcu (ifa, &idev6->addr_list, if_list) { +if (ifa->flags & (IFA_F_TENTATIVE |IFA_F_DEPRECATED )) +continue ; +memcpy (&ip6, ifa->addr.s6_addr,sizeof (ip6)); + have_ip6 = true; +break ; + } +rcu_read_unlock (); + } +if (ip4) { +if (out_buf_len < + nbytes +sizeof (struct network_interface_info_ioctl_rsp )) { +rtnl_unlock (); +return -ENOSPC ; + } + + nii_rsp =ksmbd_iface_entry_init (rsp, nbytes, netdev, speed); + sockaddr_storage = (struct sockaddr_storage_rsp *)nii_rsp->SockAddr_Storage; sockaddr_storage->Family =INTERNETWORK ; sockaddr_storage->addr4.Port =0 ; - - idev =__in_dev_get_rtnl (netdev); -if (!idev) -continue ; - sockaddr_storage->addr4.IPv4Address = -idev_ipv4_address (idev); + sockaddr_storage->addr4.IPv4Address = ip4; nbytes +=sizeof (struct network_interface_info_ioctl_rsp ); - ipv4_set = true; - goto ipv6_retry; - }else { -struct inet6_dev *idev6; -struct inet6_ifaddr *ifa; -__u8 *ipv6_addr = sockaddr_storage->addr6.IPv6Address; + } + +if (have_ip6) { +if (out_buf_len < + nbytes +sizeof (struct network_interface_info_ioctl_rsp )) { +rtnl_unlock (); +return -ENOSPC ; + } + nii_rsp =ksmbd_iface_entry_init (rsp, nbytes, netdev, speed); + sockaddr_storage = (struct sockaddr_storage_rsp *)nii_rsp->SockAddr_Storage; sockaddr_storage->Family =INTERNETWORKV6 ; sockaddr_storage->addr6.Port =0 ; sockaddr_storage->addr6.FlowInfo =0 ; - - idev6 =__in6_dev_get (netdev); -if (!idev6) -continue ; - -rcu_read_lock (); -list_for_each_entry_rcu (ifa, &idev6->addr_list, if_list) { -if (ifa->flags & (IFA_F_TENTATIVE | -IFA_F_DEPRECATED )) -continue ; -memcpy (ipv6_addr, ifa->addr.s6_addr,16 ); -break ; - } -rcu_read_unlock (); +memcpy (sockaddr_storage->addr6.IPv6Address, ip6.s6_addr,16 ); sockaddr_storage->addr6.ScopeId =0 ; nbytes +=sizeof (struct network_interface_info_ioctl_rsp ); } }rtnl_unlock (); -/* -* nii_rsp may point to an entry not included in nbytes if the IPv4 -* or IPv6 device lookup failed. Clear Next in the last returned entry. -*/ +/* Clear Next of the last committed entry to terminate the list. */ if (nbytes >0 ) { nii_rsp = (struct network_interface_info_ioctl_rsp *) &rsp->Buffer[nbytes -sizeof (*nii_rsp)];
Signature · 1 line
2.25.1
Quoted 13 lines · huhai
From: huhai <[email protected]>
This series fixes several issues in
fsctl_query_iface_info_ioctl():
incorrect link speeds, an unlocked IPv6 address-list walk, an invalid final Next offset, and incorrect IPv4/IPv6 interface reporting.Patch 1 was previously submitted as a standalone patch; this series supersedes that submission.
Quoted 6 lines
Henry Hu (4):
ksmbd: fix link speed query infsctl_query_iface_info_ioctl()
ksmbd: fix unlocked IPv6addr_listwalk in
fsctl_query_iface_info_ioctl()
ksmbd: fix invalid Next offset in interface info replies
ksmbd: report IPv4 and IPv6 addresses independently in iface query
Applied it to #ksmbd-for-next.
Thanks!