Skip to content

Commit a668020

Browse files
author
Abutalib Aghayev
authored
net: remove libc type leakage in a public API (#5191)
1 parent 01f0193 commit a668020

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

tokio/src/net/unix/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Unix domain socket utility types.
2-
32
// This module does not currently provide any public API, but it was
43
// unintentionally defined as a public module. Hide it from the documentation
54
// instead of changing it to a private module to avoid breakage.
@@ -22,3 +21,15 @@ pub(crate) use stream::UnixStream;
2221

2322
mod ucred;
2423
pub use ucred::UCred;
24+
25+
/// A type representing process and process group IDs.
26+
#[allow(non_camel_case_types)]
27+
pub type uid_t = u32;
28+
29+
/// A type representing user ID.
30+
#[allow(non_camel_case_types)]
31+
pub type gid_t = u32;
32+
33+
/// A type representing group ID.
34+
#[allow(non_camel_case_types)]
35+
pub type pid_t = i32;

tokio/src/net/unix/ucred.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
use libc::{gid_t, pid_t, uid_t};
1+
use crate::net::unix;
22

33
/// Credentials of a process.
44
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
55
pub struct UCred {
66
/// PID (process ID) of the process.
7-
pid: Option<pid_t>,
7+
pid: Option<unix::pid_t>,
88
/// UID (user ID) of the process.
9-
uid: uid_t,
9+
uid: unix::uid_t,
1010
/// GID (group ID) of the process.
11-
gid: gid_t,
11+
gid: unix::gid_t,
1212
}
1313

1414
impl UCred {
1515
/// Gets UID (user ID) of the process.
16-
pub fn uid(&self) -> uid_t {
16+
pub fn uid(&self) -> unix::uid_t {
1717
self.uid
1818
}
1919

2020
/// Gets GID (group ID) of the process.
21-
pub fn gid(&self) -> gid_t {
21+
pub fn gid(&self) -> unix::gid_t {
2222
self.gid
2323
}
2424

2525
/// Gets PID (process ID) of the process.
2626
///
2727
/// This is only implemented under Linux, Android, iOS, macOS, Solaris and
2828
/// Illumos. On other platforms this will always return `None`.
29-
pub fn pid(&self) -> Option<pid_t> {
29+
pub fn pid(&self) -> Option<unix::pid_t> {
3030
self.pid
3131
}
3232
}
@@ -48,7 +48,7 @@ pub(crate) use self::impl_solaris::get_peer_cred;
4848

4949
#[cfg(any(target_os = "linux", target_os = "android", target_os = "openbsd"))]
5050
pub(crate) mod impl_linux {
51-
use crate::net::unix::UnixStream;
51+
use crate::net::unix::{self, UnixStream};
5252

5353
use libc::{c_void, getsockopt, socklen_t, SOL_SOCKET, SO_PEERCRED};
5454
use std::{io, mem};
@@ -87,9 +87,9 @@ pub(crate) mod impl_linux {
8787
);
8888
if ret == 0 && ucred_size as usize == mem::size_of::<ucred>() {
8989
Ok(super::UCred {
90-
uid: ucred.uid,
91-
gid: ucred.gid,
92-
pid: Some(ucred.pid),
90+
uid: ucred.uid as unix::uid_t,
91+
gid: ucred.gid as unix::gid_t,
92+
pid: Some(ucred.pid as unix::pid_t),
9393
})
9494
} else {
9595
Err(io::Error::last_os_error())
@@ -100,7 +100,7 @@ pub(crate) mod impl_linux {
100100

101101
#[cfg(any(target_os = "netbsd"))]
102102
pub(crate) mod impl_netbsd {
103-
use crate::net::unix::UnixStream;
103+
use crate::net::unix::{self, UnixStream};
104104

105105
use libc::{c_void, getsockopt, socklen_t, unpcbid, LOCAL_PEEREID, SOL_SOCKET};
106106
use std::io;
@@ -129,9 +129,9 @@ pub(crate) mod impl_netbsd {
129129
);
130130
if ret == 0 && unpcbid_size as usize == size_of::<unpcbid>() {
131131
Ok(super::UCred {
132-
uid: unpcbid.unp_euid,
133-
gid: unpcbid.unp_egid,
134-
pid: Some(unpcbid.unp_pid),
132+
uid: unpcbid.unp_euid as unix::uid_t,
133+
gid: unpcbid.unp_egid as unix::gid_t,
134+
pid: Some(unpcbid.unp_pid as unix::pid_t),
135135
})
136136
} else {
137137
Err(io::Error::last_os_error())
@@ -142,7 +142,7 @@ pub(crate) mod impl_netbsd {
142142

143143
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
144144
pub(crate) mod impl_bsd {
145-
use crate::net::unix::UnixStream;
145+
use crate::net::unix::{self, UnixStream};
146146

147147
use libc::getpeereid;
148148
use std::io;
@@ -160,8 +160,8 @@ pub(crate) mod impl_bsd {
160160

161161
if ret == 0 {
162162
Ok(super::UCred {
163-
uid: uid.assume_init(),
164-
gid: gid.assume_init(),
163+
uid: uid.assume_init() as unix::uid_t,
164+
gid: gid.assume_init() as unix::gid_t,
165165
pid: None,
166166
})
167167
} else {
@@ -173,7 +173,7 @@ pub(crate) mod impl_bsd {
173173

174174
#[cfg(any(target_os = "macos", target_os = "ios"))]
175175
pub(crate) mod impl_macos {
176-
use crate::net::unix::UnixStream;
176+
use crate::net::unix::{self, UnixStream};
177177

178178
use libc::{c_void, getpeereid, getsockopt, pid_t, LOCAL_PEEREPID, SOL_LOCAL};
179179
use std::io;
@@ -207,9 +207,9 @@ pub(crate) mod impl_macos {
207207

208208
if ret == 0 {
209209
Ok(super::UCred {
210-
uid: uid.assume_init(),
211-
gid: gid.assume_init(),
212-
pid: Some(pid.assume_init()),
210+
uid: uid.assume_init() as unix::uid_t,
211+
gid: gid.assume_init() as unix::gid_t,
212+
pid: Some(pid.assume_init() as unix::pid_t),
213213
})
214214
} else {
215215
Err(io::Error::last_os_error())
@@ -220,7 +220,7 @@ pub(crate) mod impl_macos {
220220

221221
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
222222
pub(crate) mod impl_solaris {
223-
use crate::net::unix::UnixStream;
223+
use crate::net::unix::{self, UnixStream};
224224
use std::io;
225225
use std::os::unix::io::AsRawFd;
226226
use std::ptr;
@@ -240,9 +240,9 @@ pub(crate) mod impl_solaris {
240240
libc::ucred_free(cred);
241241

242242
Ok(super::UCred {
243-
uid,
244-
gid,
245-
pid: Some(pid),
243+
uid: uid as unix::uid_t,
244+
gid: gid as unix::gid_t,
245+
pid: Some(pid as unix::pid_t),
246246
})
247247
} else {
248248
Err(io::Error::last_os_error())

0 commit comments

Comments
 (0)