Skip to content

Commit 3af8360

Browse files
committed
net: UdpSocket: add notes about trusting sender addresses
1 parent 1609edd commit 3af8360

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tokio/src/net/udp.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,15 @@ impl UdpSocket {
954954
/// When there is no pending data, `Err(io::ErrorKind::WouldBlock)` is
955955
/// returned. This function is usually paired with `readable()`.
956956
///
957+
/// # Notes
958+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
959+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
960+
/// Because UDP is stateless and does not validate the origin of a packet,
961+
/// the attacker does not need to be able to intercept traffic in order to interfere.
962+
/// It is important to be aware of this when designing your application-level protocol.
963+
///
964+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
965+
///
957966
/// # Examples
958967
///
959968
/// ```no_run
@@ -1177,6 +1186,15 @@ impl UdpSocket {
11771186
/// Ok(())
11781187
/// }
11791188
/// ```
1189+
///
1190+
/// # Notes
1191+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1192+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1193+
/// Because UDP is stateless and does not validate the origin of a packet,
1194+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1195+
/// It is important to be aware of this when designing your application-level protocol.
1196+
///
1197+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
11801198
pub async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
11811199
self.io
11821200
.registration()
@@ -1201,6 +1219,15 @@ impl UdpSocket {
12011219
/// # Errors
12021220
///
12031221
/// This function may encounter any standard I/O error except `WouldBlock`.
1222+
///
1223+
/// # Notes
1224+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1225+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1226+
/// Because UDP is stateless and does not validate the origin of a packet,
1227+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1228+
/// It is important to be aware of this when designing your application-level protocol.
1229+
///
1230+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
12041231
pub fn poll_recv_from(
12051232
&self,
12061233
cx: &mut Context<'_>,
@@ -1233,6 +1260,16 @@ impl UdpSocket {
12331260
/// When there is no pending data, `Err(io::ErrorKind::WouldBlock)` is
12341261
/// returned. This function is usually paired with `readable()`.
12351262
///
1263+
/// # Notes
1264+
///
1265+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1266+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1267+
/// Because UDP is stateless and does not validate the origin of a packet,
1268+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1269+
/// It is important to be aware of this when designing your application-level protocol.
1270+
///
1271+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
1272+
///
12361273
/// # Examples
12371274
///
12381275
/// ```no_run
@@ -1336,6 +1373,12 @@ impl UdpSocket {
13361373
/// If you're merely interested in learning the sender of the data at the head of the queue,
13371374
/// try [`peek_sender`].
13381375
///
1376+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1377+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1378+
/// Because UDP is stateless and does not validate the origin of a packet,
1379+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1380+
/// It is important to be aware of this when designing your application-level protocol.
1381+
///
13391382
/// # Examples
13401383
///
13411384
/// ```no_run
@@ -1356,6 +1399,7 @@ impl UdpSocket {
13561399
/// ```
13571400
///
13581401
/// [`peek_sender`]: method@Self::peek_sender
1402+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
13591403
pub async fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
13601404
self.io
13611405
.registration()
@@ -1383,6 +1427,12 @@ impl UdpSocket {
13831427
/// If you're merely interested in learning the sender of the data at the head of the queue,
13841428
/// try [`poll_peek_sender`].
13851429
///
1430+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1431+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1432+
/// Because UDP is stateless and does not validate the origin of a packet,
1433+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1434+
/// It is important to be aware of this when designing your application-level protocol.
1435+
///
13861436
/// # Return value
13871437
///
13881438
/// The function returns:
@@ -1396,6 +1446,7 @@ impl UdpSocket {
13961446
/// This function may encounter any standard I/O error except `WouldBlock`.
13971447
///
13981448
/// [`poll_peek_sender`]: method@Self::poll_peek_sender
1449+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
13991450
pub fn poll_peek_from(
14001451
&self,
14011452
cx: &mut Context<'_>,
@@ -1438,7 +1489,14 @@ impl UdpSocket {
14381489
/// If you're merely interested in learning the sender of the data at the head of the queue,
14391490
/// try [`try_peek_sender`].
14401491
///
1492+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1493+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1494+
/// Because UDP is stateless and does not validate the origin of a packet,
1495+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1496+
/// It is important to be aware of this when designing your application-level protocol.
1497+
///
14411498
/// [`try_peek_sender`]: method@Self::try_peek_sender
1499+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14421500
pub fn try_peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
14431501
self.io
14441502
.registration()
@@ -1450,7 +1508,14 @@ impl UdpSocket {
14501508
/// This is equivalent to calling [`peek_from`] with a zero-sized buffer,
14511509
/// but suppresses the `WSAEMSGSIZE` error on Windows and the "invalid argument" error on macOS.
14521510
///
1511+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1512+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1513+
/// Because UDP is stateless and does not validate the origin of a packet,
1514+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1515+
/// It is important to be aware of this when designing your application-level protocol.
1516+
///
14531517
/// [`peek_from`]: method@Self::peek_from
1518+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14541519
pub async fn peek_sender(&self) -> io::Result<SocketAddr> {
14551520
self.io
14561521
.registration()
@@ -1470,7 +1535,14 @@ impl UdpSocket {
14701535
/// `Waker` from the `Context` passed to the most recent call will be scheduled to
14711536
/// receive a wakeup.
14721537
///
1538+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1539+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1540+
/// Because UDP is stateless and does not validate the origin of a packet,
1541+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1542+
/// It is important to be aware of this when designing your application-level protocol.
1543+
///
14731544
/// [`poll_peek_from`]: method@Self::poll_peek_from
1545+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14741546
pub fn poll_peek_sender(&self, cx: &mut Context<'_>) -> Poll<io::Result<SocketAddr>> {
14751547
self.io
14761548
.registration()
@@ -1481,6 +1553,14 @@ impl UdpSocket {
14811553
///
14821554
/// When there is no pending data, `Err(io::ErrorKind::WouldBlock)` is
14831555
/// returned. This function is usually paired with `readable()`.
1556+
///
1557+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1558+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1559+
/// Because UDP is stateless and does not validate the origin of a packet,
1560+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1561+
/// It is important to be aware of this when designing your application-level protocol.
1562+
///
1563+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14841564
pub fn try_peek_sender(&self) -> io::Result<SocketAddr> {
14851565
self.io
14861566
.registration()

0 commit comments

Comments
 (0)