Skip to content

Commit 2116189

Browse files
committed
Use sockaddr_storage instead of sockaddr to prevent stack smashing
1 parent 90214fa commit 2116189

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

uvloop/dns.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,17 @@ cdef __static_getaddrinfo_pyaddr(object host, object port,
160160
int proto, int flags):
161161

162162
cdef:
163-
system.sockaddr addr
163+
system.sockaddr_storage addr
164164

165165
try:
166-
(af, type, proto) = __static_getaddrinfo(host, port, family, type,
167-
proto, &addr)
166+
(af, type, proto) = __static_getaddrinfo(
167+
host, port, family, type,
168+
proto, <system.sockaddr*>&addr)
168169
except LookupError:
169170
return
170171

171172
try:
172-
pyaddr = __convert_sockaddr_to_pyaddr(&addr)
173+
pyaddr = __convert_sockaddr_to_pyaddr(<system.sockaddr*>&addr)
173174
except:
174175
return
175176

uvloop/handles/udp.pxd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ cdef class UDPTransport(UVBaseTransport):
44
int _family
55

66
bint _address_set
7-
system.sockaddr _address
7+
system.sockaddr_storage _address
88
object _cached_py_address
99

1010
cdef _init(self, Loop loop, unsigned int family)
1111

12-
cdef _set_remote_address(self, system.sockaddr address)
12+
cdef _set_remote_address(self, system.sockaddr* addr,
13+
size_t addr_len)
1314

1415
cdef _bind(self, system.sockaddr* addr, bint reuse_addr)
1516
cdef _open(self, int family, int sockfd)

uvloop/handles/udp.pyx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cdef class _UDPSendContext:
1313

1414
bint closed
1515

16-
system.sockaddr addr
16+
system.sockaddr_storage addr
1717

1818
cdef close(self):
1919
if self.closed:
@@ -81,9 +81,10 @@ cdef class UDPTransport(UVBaseTransport):
8181

8282
self._finish_init()
8383

84-
cdef _set_remote_address(self, system.sockaddr address):
84+
cdef _set_remote_address(self, system.sockaddr* addr,
85+
size_t addr_len):
86+
memcpy(&self._address, addr, addr_len)
8587
self._address_set = 1
86-
self._address = address
8788

8889
cdef _open(self, int family, int sockfd):
8990
if family in (uv.AF_INET, uv.AF_INET6):
@@ -199,7 +200,7 @@ cdef class UDPTransport(UVBaseTransport):
199200
if self._address_set and addr is not None:
200201
if self._cached_py_address is None:
201202
self._cached_py_address = __convert_sockaddr_to_pyaddr(
202-
&self._address)
203+
<system.sockaddr*>&self._address)
203204

204205
if self._cached_py_address != addr:
205206
raise ValueError('Invalid address: must be None or %s' %
@@ -216,7 +217,8 @@ cdef class UDPTransport(UVBaseTransport):
216217
raise RuntimeError(
217218
'undable to perform send operation: no address')
218219
else:
219-
__convert_pyaddr_to_sockaddr(self._family, addr, &ctx.addr)
220+
__convert_pyaddr_to_sockaddr(self._family, addr,
221+
<system.sockaddr*>&ctx.addr)
220222
except:
221223
ctx.close()
222224
raise
@@ -225,7 +227,7 @@ cdef class UDPTransport(UVBaseTransport):
225227
<uv.uv_udp_t*>self._handle,
226228
&ctx.uv_buf,
227229
1,
228-
&ctx.addr,
230+
<system.sockaddr*>&ctx.addr,
229231
__uv_udp_on_send)
230232

231233
if err < 0:

uvloop/loop.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from .includes.python cimport PyMem_Malloc, PyMem_Free, \
1414
PyErr_SetInterrupt
1515

1616
from libc.stdint cimport uint64_t
17-
from libc.string cimport memset, strerror
17+
from libc.string cimport memset, strerror, memcpy
1818
from libc cimport errno
1919

2020
from cpython cimport PyObject
@@ -1307,9 +1307,9 @@ cdef class Loop:
13071307
system.addrinfo *lai_iter = NULL
13081308

13091309
system.addrinfo rai_static
1310-
system.sockaddr rai_addr_static
1310+
system.sockaddr_storage rai_addr_static
13111311
system.addrinfo lai_static
1312-
system.sockaddr lai_addr_static
1312+
system.sockaddr_storage lai_addr_static
13131313

13141314
object app_protocol
13151315
object protocol
@@ -1340,7 +1340,7 @@ cdef class Loop:
13401340
try:
13411341
__static_getaddrinfo(
13421342
host, port, family, uv.SOCK_STREAM,
1343-
proto, &rai_addr_static)
1343+
proto, <system.sockaddr*>&rai_addr_static)
13441344
except LookupError:
13451345
f1 = self._getaddrinfo(
13461346
host, port, family,
@@ -1349,7 +1349,7 @@ cdef class Loop:
13491349

13501350
fs.append(f1)
13511351
else:
1352-
rai_static.ai_addr = &rai_addr_static
1352+
rai_static.ai_addr = <system.sockaddr*>&rai_addr_static
13531353
rai_static.ai_next = NULL
13541354
rai = &rai_static
13551355

@@ -1363,7 +1363,7 @@ cdef class Loop:
13631363
__static_getaddrinfo(
13641364
local_addr[0], local_addr[1],
13651365
family, uv.SOCK_STREAM,
1366-
proto, &lai_addr_static)
1366+
proto, <system.sockaddr*>&lai_addr_static)
13671367
except LookupError:
13681368
f2 = self._getaddrinfo(
13691369
local_addr[0], local_addr[1], family,
@@ -1372,7 +1372,7 @@ cdef class Loop:
13721372

13731373
fs.append(f2)
13741374
else:
1375-
lai_static.ai_addr = &lai_addr_static
1375+
lai_static.ai_addr = <system.sockaddr*>&lai_addr_static
13761376
lai_static.ai_next = NULL
13771377
lai = &rai_static
13781378

@@ -2209,7 +2209,7 @@ cdef class Loop:
22092209
udp = UDPTransport.__new__(UDPTransport)
22102210
rai = (<AddrInfo>rads).data
22112211
udp._init(self, rai.ai_family)
2212-
udp._set_remote_address(rai.ai_addr[0])
2212+
udp._set_remote_address(rai.ai_addr, rai.ai_addrlen)
22132213
else:
22142214
if family not in (uv.AF_INET, uv.AF_INET6):
22152215
raise ValueError('unexpected address family')
@@ -2253,14 +2253,14 @@ cdef class Loop:
22532253
if rai.ai_protocol != lai.ai_protocol:
22542254
rai = rai.ai_next
22552255
continue
2256-
udp._set_remote_address(rai.ai_addr[0])
2256+
udp._set_remote_address(rai.ai_addr, rai.ai_addrlen)
22572257
break
22582258
else:
22592259
raise OSError(
22602260
'could not bind to remote_addr {}'.format(
22612261
remote_addr))
22622262

2263-
udp._set_remote_address(rai.ai_addr[0])
2263+
udp._set_remote_address(rai.ai_addr, rai.ai_addrlen)
22642264

22652265
if allow_broadcast:
22662266
udp._set_broadcast(1)

0 commit comments

Comments
 (0)