Skip to content

Commit fa1a390

Browse files
committed
Add setsockopt and getsockopt safe rules
1 parent 0faa092 commit fa1a390

3 files changed

Lines changed: 2020 additions & 12 deletions

File tree

‎rules/socket/tgt_refcount.rs‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,126 @@ fn f17(a0: i32, a1: i32) -> i32 {
218218
}
219219
}
220220
}
221+
222+
fn f7(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: u32) -> i32 {
223+
let __res = match (a1, a2) {
224+
(::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => {
225+
let __v = a3.reinterpret_cast::<i32>().read() != 0;
226+
FdRegistry::with_fd(a0, |__fd| {
227+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpNoDelay, &__v)
228+
})
229+
}
230+
(::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => {
231+
let __v = a3.reinterpret_cast::<i32>().read() != 0;
232+
FdRegistry::with_fd(a0, |__fd| {
233+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::KeepAlive, &__v)
234+
})
235+
}
236+
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => {
237+
let __v = a3.reinterpret_cast::<u32>().read();
238+
FdRegistry::with_fd(a0, |__fd| {
239+
nix::sys::socket::setsockopt(
240+
&__fd,
241+
nix::sys::socket::sockopt::TcpKeepInterval,
242+
&__v,
243+
)
244+
})
245+
}
246+
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => {
247+
let __v = a3.reinterpret_cast::<u32>().read();
248+
FdRegistry::with_fd(a0, |__fd| {
249+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepCount, &__v)
250+
})
251+
}
252+
(::libc::IPPROTO_IP, ::libc::IP_TOS) => {
253+
let __v = a3.reinterpret_cast::<i32>().read();
254+
FdRegistry::with_fd(a0, |__fd| {
255+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv4Tos, &__v)
256+
})
257+
}
258+
(::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => {
259+
let __v = a3.reinterpret_cast::<i32>().read();
260+
FdRegistry::with_fd(a0, |__fd| {
261+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv6TClass, &__v)
262+
})
263+
}
264+
#[cfg(target_os = "linux")]
265+
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => {
266+
let __v = a3.reinterpret_cast::<u32>().read();
267+
FdRegistry::with_fd(a0, |__fd| {
268+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepIdle, &__v)
269+
})
270+
}
271+
#[cfg(target_os = "linux")]
272+
(::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => {
273+
let __v = ::std::ffi::OsString::from(a3.reinterpret_cast::<u8>().to_rust_string());
274+
FdRegistry::with_fd(a0, |__fd| {
275+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::BindToDevice, &__v)
276+
})
277+
}
278+
#[cfg(target_os = "linux")]
279+
(::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => {
280+
let __v = a3.reinterpret_cast::<i32>().read() != 0;
281+
FdRegistry::with_fd(a0, |__fd| {
282+
nix::sys::socket::setsockopt(
283+
&__fd,
284+
nix::sys::socket::sockopt::IpBindAddressNoPort,
285+
&__v,
286+
)
287+
})
288+
}
289+
#[cfg(target_os = "linux")]
290+
(::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => {
291+
let __v = a3.reinterpret_cast::<i32>().read() != 0;
292+
FdRegistry::with_fd(a0, |__fd| {
293+
nix::sys::socket::setsockopt(
294+
&__fd,
295+
nix::sys::socket::sockopt::TcpFastOpenConnect,
296+
&__v,
297+
)
298+
})
299+
}
300+
#[cfg(target_os = "linux")]
301+
(::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => {
302+
let __v = a3.reinterpret_cast::<i32>().read();
303+
FdRegistry::with_fd(a0, |__fd| {
304+
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Priority, &__v)
305+
})
306+
}
307+
(__l, __o) => panic!(
308+
"setsockopt: unsupported option (level={}, optname={})",
309+
__l, __o
310+
),
311+
};
312+
match __res {
313+
Ok(()) => 0,
314+
Err(__e) => {
315+
libcc2rs::cpp2rust_errno().write(__e as i32);
316+
-1
317+
}
318+
}
319+
}
320+
321+
fn f8(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: Ptr<u32>) -> i32 {
322+
match (a1, a2) {
323+
(::libc::SOL_SOCKET, ::libc::SO_ERROR) => {
324+
match FdRegistry::with_fd(a0, |__fd| {
325+
nix::sys::socket::getsockopt(&__fd, nix::sys::socket::sockopt::SocketError)
326+
}) {
327+
Ok(__err) => {
328+
a3.reinterpret_cast::<i32>().write(__err);
329+
a4.write(::std::mem::size_of::<i32>() as u32);
330+
0
331+
}
332+
Err(__e) => {
333+
libcc2rs::cpp2rust_errno().write(__e as i32);
334+
-1
335+
}
336+
}
337+
}
338+
(__l, __o) => panic!(
339+
"getsockopt: unsupported option (level={}, optname={})",
340+
__l, __o
341+
),
342+
}
343+
}

0 commit comments

Comments
 (0)