Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnixListener::bind with abstract unix socket path has an extra \0 prefix #6837

Closed
zonyitoo opened this issue Sep 11, 2024 · 2 comments · Fixed by #6838
Closed

UnixListener::bind with abstract unix socket path has an extra \0 prefix #6837

zonyitoo opened this issue Sep 11, 2024 · 2 comments · Fixed by #6838
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-net Module: tokio/net

Comments

@zonyitoo
Copy link
Contributor

zonyitoo commented Sep 11, 2024

Version

v1.40.0

Platform
Linux VM-66-53-centos 5.4.241-1-tlinux4-0017.12 #1 SMP Fri Aug 2 14:51:21 CST 2024 x86_64 x86_64 x86_64 GNU/Linux

Description

Example code:

let abstract_path = "\0/tmp/mesh/business/mmmeshexample";
let listener = UnixListener::bind(abstract_path).unwrap();

It should create a socket listening to @/tmp/mesh/business/mmmeshexample, but with netstat -alpn, we can see:

unix  2      [ ACC ]     STREAM     LISTENING     2483935  229872/target/debug  @@/tmp/mesh/business/mmmeshexampl

Obviously the sun_path that passed to bind() syscall has an extra \0 prefix and the length of the whole sockaddr_un was missing 1 byte.

This bug couldn't be reproduced in v1.26.0. After looked deep into the code, I found the bug was quite obvious:

if os_str_bytes.starts_with(b"\0") {
StdSocketAddr::from_abstract_name(os_str_bytes)?

It checks the path from parameter, if it has a prefix \0 then calls StdSocketAddr::from_abstract_name.

https://github.com/rust-lang/rust/blob/5a2dd7d4f3210629e65879aeecbe643ba3b86bb4/library/std/src/os/unix/net/addr.rs#L269-L293

SocketAddr::from_abstract_name assumes the name in parameter doesn't have a prefix \0.

The same bug was also in UnixStream::connect:

if os_str_bytes.starts_with(b"\0") {
StdSocketAddr::from_abstract_name(os_str_bytes)?

@zonyitoo zonyitoo added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Sep 11, 2024
@zonyitoo
Copy link
Contributor Author

zonyitoo commented Sep 11, 2024

The bug was introduced from this commit: b2ea40b by @mox692 , please consider submit a fix.

One possible and straight forward solution:

    StdSocketAddr::from_abstract_name(&os_str_bytes[1..])?

@mox692 mox692 added the M-net Module: tokio/net label Sep 11, 2024
@mox692
Copy link
Member

mox692 commented Sep 11, 2024

Thanks for the report, I'll submit a fix for this soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-net Module: tokio/net
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants