Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/crates/net/src/loopback_owner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ mod tests {
drop(client);
}

// The command line's question: the server's end, before the server accepts it.
#[cfg(target_os = "linux")]
#[test]
fn a_connection_waiting_to_be_accepted_is_its_own() {
let listener = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
let client = std::net::TcpStream::connect(listener.local_addr().unwrap()).unwrap();

assert!(loopback_peer_is_ours(client.peer_addr().unwrap(), client.local_addr().unwrap()).unwrap());
}

#[cfg(not(any(target_os = "linux", windows)))]
#[test]
fn other_systems_name_no_owner() {
Expand Down
87 changes: 74 additions & 13 deletions core/crates/net/src/loopback_owner/proc_net_tcp.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
//! Who owns the other end of a loopback TCP connection, read from the
//! kernel's socket table as proc(5) describes `/proc/net/tcp`.
// Who owns the other end of a loopback TCP connection, from the socket table proc(5) describes as /proc/net/tcp.

use std::net::{Ipv4Addr, SocketAddrV4};

/// The table lists each socket from its own side, so the peer's row has
/// the peer as its local address and `local` as its remote one.
const ESTABLISHED: &str = "01";
const SYN_RECV: &str = "03";
const LISTEN: &str = "0A";

struct Row<'a> {
from: SocketAddrV4,
to: SocketAddrV4,
state: &'a str,
uid: u32,
inode: u64,
}

// Each row is from its socket's side, so the peer's has the peer as its local address and `local` as its remote.
pub(super) fn owner(table: &str, peer: SocketAddrV4, local: SocketAddrV4) -> Option<u32> {
table.lines().skip(1).find_map(|line| {
let rows = || table.lines().skip(1).filter_map(Row::parse);
let connection = rows().find(|row| row.from == peer && row.to == local)?;
if connection.inode != 0 {
return Some(connection.uid);
}
// A closing socket also has no inode, and another user's could share its address with a later listener.
if connection.state != ESTABLISHED && connection.state != SYN_RECV {
return None;
}
// No process holds it, as while it waits to be accepted: the listener's. Before 6.17 it is listed as root's.
rows()
.find(|row| row.state == LISTEN && row.from == peer)
.map(|listener| listener.uid)
}

impl<'a> Row<'a> {
// `sl local_address rem_address st tx_queue:rx_queue tr:tm->when retrnsmt uid timeout inode ...`
fn parse(line: &'a str) -> Option<Self> {
let mut fields = line.split_ascii_whitespace();
let _slot = fields.next()?;
let from = parse_address(fields.next()?)?;
let to = parse_address(fields.next()?)?;
if from != peer || to != local {
return None;
}
let uid = fields.nth(4)?;
uid.parse().ok()
})
let state = fields.next()?;
let uid = fields.nth(3)?.parse().ok()?;
let inode = fields.nth(1)?.parse().ok()?;
Some(Self {
from,
to,
state,
uid,
inode,
})
}
}

/// `0100007F:1F90`: the address as the kernel's 32-bit value in host
/// byte order, then the port, both in hex.
// `0100007F:1F90`: the address as the kernel's 32-bit value in host byte order, then the port, both in hex.
fn parse_address(field: &str) -> Option<SocketAddrV4> {
let (address, port) = field.split_once(':')?;
let address = u32::from_str_radix(address, 16).ok()?;
Expand Down Expand Up @@ -49,4 +80,34 @@ mod tests {
assert_eq!(owner(&table, loopback(50_000), loopback(8080)), Some(1234));
assert_eq!(owner(&table, loopback(50_001), loopback(8080)), None);
}

// As kernel 6.8 lists them for uid 2001 before the server accepts: the server's end is root's, with no inode.
#[test]
fn a_connection_waiting_to_be_accepted_belongs_to_its_listener() {
let hex = |address: SocketAddrV4| {
format!(
"{:08X}:{:04X}",
u32::from_ne_bytes(address.ip().octets()),
address.port()
)
};
let row = |from: &str, to: &str, state: &str, uid: u32, inode: u64| {
format!(" 0: {from} {to} {state} 00000000:00000000 00:00000000 00000000 {uid:>5} 0 {inode}\n")
};
let (server, client) = (loopback(0x8EA7), loopback(0xBC88));
let (server_hex, client_hex) = (hex(server), hex(client));
let connection = row(&client_hex, &server_hex, "01", 2001, 540) + &row(&server_hex, &client_hex, "01", 0, 0);
let listener = |uid| row(&server_hex, "00000000:0000", "0A", uid, 539);
let table = |listener: &str| format!(" sl local_address rem_address st\n{listener}{connection}");

assert_eq!(owner(&table(&listener(2001)), server, client), Some(2001));
assert_eq!(owner(&table(&listener(1234)), server, client), Some(1234));
assert_eq!(owner(&table(""), server, client), None);

// Another user's client that sent data and closed, at an address a listener of ours took later.
let closed = row(&client_hex, &server_hex, "05", 0, 0);
let ours = row(&client_hex, "00000000:0000", "0A", 2001, 541);
let reused = format!(" sl local_address rem_address st\n{ours}{closed}");
assert_eq!(owner(&reused, client, server), None);
}
}
1 change: 1 addition & 0 deletions linux/changelog.d/4311.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `tablepro` command no longer refuses the running app as another program's server on Linux kernels before 6.17.
Loading