History

ovpn: zero-initialize sockaddr before learning a floated endpoint

ovpn_peer_endpoints_update() builds the new remote endpoint in an on-stack struct sockaddr_storage that is left uninitialized. For IPv4 only sin_family/sin_addr/sin_port are written, leaving the 8-byte sin_zero padding as stack garbage (for IPv6, sin6_flowinfo is left uninitialized likewise). ovpn_peer_reset_sockaddr() -> ovpn_bind_from_sockaddr() then memcpy()s sizeof(struct sockaddr_in)/sizeof(struct sockaddr_in6) bytes - padding included - into bind->remote. That buffer is later hashed with jhash() over the same length to place the peer in the by_transp_addr table, so the garbage padding lands the floated peer in an essentially random bucket. Lockless lookups in ovpn_peer_get_by_transp_addr() build their key from a zero-initialized sockaddr_storage, compute a different bucket and fail to find the peer. This is also a plain use of uninitialized stack memory in jhash(). Build the floated endpoint with a designated initializer so the padding (sin_zero for IPv4, sin6_flowinfo for IPv6) is zeroed as part of the assignment. This keeps the padding out of the by_transp_addr hash key without memset-ing the whole sockaddr_storage on every received packet.

Antonio Quartulli committed 3 months ago

Commit 3f012bd1 parent 59aed1e1 file · +23 −8Browse files

Unified · Split

drivers/net/ovpn/peer.c+23−8
@@ -222,9 +222,16 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb) · 221 unmodified lines
222222 */
223223 local_ip = &ip_hdr(skb)->daddr;
224224 sa = (struct sockaddr_in *)&ss;
225− sa->sin_family = AF_INET;
226− sa->sin_addr.s_addr = ip_hdr(skb)->saddr;
227− sa->sin_port = udp_hdr(skb)->source;
225+ /* use a designated initializer so the sin_zero padding
226+ * is zeroed (it ends up in the by_transp_addr hash key)
227+ * without memset-ing the whole sockaddr_storage on the
228+ * RX fast path
229+ */
230+ *sa = (struct sockaddr_in) {
231+ .sin_family = AF_INET,
232+ .sin_addr.s_addr = ip_hdr(skb)->saddr,
233+ .sin_port = udp_hdr(skb)->source,
234+ };
228235 salen = sizeof(*sa);
229236 reset_cache = true;
230237 break;
@@ -250,11 +257,19 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb) · 19 unmodified lines
250257 */
251258 local_ip = &ipv6_hdr(skb)->daddr;
252259 sa6 = (struct sockaddr_in6 *)&ss;
253− sa6->sin6_family = AF_INET6;
254− sa6->sin6_addr = ipv6_hdr(skb)->saddr;
255− sa6->sin6_port = udp_hdr(skb)->source;
256− sa6->sin6_scope_id = ipv6_iface_scope_id(&ipv6_hdr(skb)->saddr,
257− skb->skb_iif);
260+ /* use a designated initializer so the sin6_flowinfo
261+ * padding is zeroed (it ends up in the by_transp_addr
262+ * hash key) without memset-ing the whole
263+ * sockaddr_storage on the RX fast path
264+ */
265+ *sa6 = (struct sockaddr_in6) {
266+ .sin6_family = AF_INET6,
267+ .sin6_addr = ipv6_hdr(skb)->saddr,
268+ .sin6_port = udp_hdr(skb)->source,
269+ .sin6_scope_id =
270+ ipv6_iface_scope_id(&ipv6_hdr(skb)->saddr,
271+ skb->skb_iif),
272+ };
258273 salen = sizeof(*sa6);
259274 reset_cache = true;
260275 break;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t