Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d29179

Browse files
committedMar 7, 2023
net: UdpSocket: add notes about trusting sender addresses
1 parent 13026e4 commit 2d29179

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
 

‎tokio/src/net/udp.rs

+80
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<'_>,
@@ -1442,7 +1493,14 @@ impl UdpSocket {
14421493
/// If you're merely interested in learning the sender of the data at the head of the queue,
14431494
/// try [`try_peek_sender`].
14441495
///
1496+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1497+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1498+
/// Because UDP is stateless and does not validate the origin of a packet,
1499+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1500+
/// It is important to be aware of this when designing your application-level protocol.
1501+
///
14451502
/// [`try_peek_sender`]: method@Self::try_peek_sender
1503+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14461504
pub fn try_peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
14471505
self.io
14481506
.registration()
@@ -1454,7 +1512,14 @@ impl UdpSocket {
14541512
/// This is equivalent to calling [`peek_from`] with a zero-sized buffer,
14551513
/// but suppresses the `WSAEMSGSIZE` error on Windows and the "invalid argument" error on macOS.
14561514
///
1515+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1516+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1517+
/// Because UDP is stateless and does not validate the origin of a packet,
1518+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1519+
/// It is important to be aware of this when designing your application-level protocol.
1520+
///
14571521
/// [`peek_from`]: method@Self::peek_from
1522+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14581523
pub async fn peek_sender(&self) -> io::Result<SocketAddr> {
14591524
self.io
14601525
.registration()
@@ -1474,7 +1539,14 @@ impl UdpSocket {
14741539
/// `Waker` from the `Context` passed to the most recent call will be scheduled to
14751540
/// receive a wakeup.
14761541
///
1542+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1543+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1544+
/// Because UDP is stateless and does not validate the origin of a packet,
1545+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1546+
/// It is important to be aware of this when designing your application-level protocol.
1547+
///
14771548
/// [`poll_peek_from`]: method@Self::poll_peek_from
1549+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14781550
pub fn poll_peek_sender(&self, cx: &mut Context<'_>) -> Poll<io::Result<SocketAddr>> {
14791551
self.io
14801552
.registration()
@@ -1485,6 +1557,14 @@ impl UdpSocket {
14851557
///
14861558
/// When there is no pending data, `Err(io::ErrorKind::WouldBlock)` is
14871559
/// returned. This function is usually paired with `readable()`.
1560+
///
1561+
/// Note that the socket address **cannot** be implicitly trusted, because it is relatively
1562+
/// trivial to send a UDP datagram with a spoofed origin in a [packet injection attack].
1563+
/// Because UDP is stateless and does not validate the origin of a packet,
1564+
/// the attacker does not need to be able to intercept traffic in order to interfere.
1565+
/// It is important to be aware of this when designing your application-level protocol.
1566+
///
1567+
/// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
14881568
pub fn try_peek_sender(&self) -> io::Result<SocketAddr> {
14891569
self.io
14901570
.registration()

0 commit comments

Comments
 (0)
Please sign in to comment.