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

feat(builtins): add builtin SocketPair #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 15 additions & 18 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
//! Built-in [`RuleSet`](crate::RuleSet)s

pub mod basic;
pub mod danger_zone;
pub mod network;
pub mod pipes;
pub mod systemio;
pub mod time;

pub use self::{
basic::BasicCapabilities,
network::{Networking, Netlink, SocketPair},
systemio::SystemIO,
time::Time,
};

/// A struct whose purpose is to make you read the documentation for the function you're calling.
/// If you're reading this, go read the documentation for the function that is returning this
/// object.
Expand All @@ -16,23 +30,6 @@ impl<T> YesReally<T> {

/// Make a [`YesReally`].
pub fn new(inner: T) -> YesReally<T> {
YesReally {
inner,
}
YesReally { inner }
}
}

pub mod basic;
pub use basic::BasicCapabilities;

pub mod systemio;
pub use systemio::SystemIO;

pub mod network;
pub use network::Networking;

pub mod time;
pub use time::Time;

pub mod danger_zone;
pub mod pipes;
15 changes: 10 additions & 5 deletions src/builtins/network.rs → src/builtins/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
//! Contains a [`RuleSet`] for allowing networking-related syscalls.

use std::collections::{HashMap, HashSet};
pub mod netlink;
pub mod socket_pair;

use syscalls::Sysno;
use {
super::YesReally,
crate::{RuleSet, SeccompRule},
std::collections::{HashMap, HashSet},
syscalls::Sysno,
};

use super::YesReally;
use crate::{SeccompRule, RuleSet};
pub use self::{netlink::Netlink, socket_pair::SocketPair};

// TODO: make bind calls conditional on the DGRAM/UNIX/STREAM flag in each function

Expand Down Expand Up @@ -205,7 +210,7 @@ impl Networking {
self.custom.entry(Sysno::socket)
.or_insert_with(Vec::new)
.push(rule);

self.allowed.extend(&[Sysno::connect]);
self.allowed.extend(NET_IO_SYSCALLS);
self.allowed.extend(NET_READ_SYSCALLS);
Expand Down
62 changes: 62 additions & 0 deletions src/builtins/network/netlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Allow netlink-sockets.

use {
crate::{
RuleSet, SeccompArgumentFilter as Filter, SeccompRule as Rule,
SeccompilerComparator as Comparator,
},
std::collections::HashMap,
syscalls::Sysno,
};

/// Allow the syscall `socket` to open a netlink-socket.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, PartialOrd)]
#[must_use]
pub struct Netlink;

impl RuleSet for Netlink {
fn simple_rules(&self) -> Vec<Sysno> {
Vec::default()
}

#[allow(clippy::as_conversions)]
fn conditional_rules(&self) -> HashMap<Sysno, Vec<Rule>> {
/// `AF_NETLINK` as `u64`.
const AF_NETLINK: u64 = libc::AF_NETLINK as u64;

/// `SOCK_RAW` as `u64`.
const SOCK_RAW: u64 = libc::SOCK_RAW as u64;

let rule = Rule::new(Sysno::socket)
.and_condition(Filter::new(0, Comparator::MaskedEq(AF_NETLINK), AF_NETLINK))
.and_condition(Filter::new(1, Comparator::MaskedEq(SOCK_RAW), SOCK_RAW));
HashMap::from([(Sysno::socket, Vec::from([rule]))])
}

fn name(&self) -> &'static str {
"Netlink"
}
}

#[cfg(test)]
mod tests {
use {super::Netlink, crate::RuleSet as _, syscalls::Sysno};

#[test]
fn name() {
assert_eq!(Netlink.name(), "Netlink");
}

#[test]
fn simple_rules() {
let rules = Netlink.simple_rules();
assert!(rules.is_empty());
}

#[test]
fn conditional_rules() {
let rules = Netlink.conditional_rules();
assert_eq!(rules.len(), 1);
assert!(rules.contains_key(&Sysno::socket));
}
}
40 changes: 40 additions & 0 deletions src/builtins/network/socket_pair.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Allow `sys_socketpair`.

use {crate::RuleSet, syscalls::Sysno};

/// Allow the syscall `socketpair` to create a pair of connected sockets.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, PartialOrd)]
#[must_use]
pub struct SocketPair;

impl RuleSet for SocketPair {
fn simple_rules(&self) -> Vec<Sysno> {
Vec::from([Sysno::socketpair])
}

fn name(&self) -> &'static str {
"SocketPair"
}
}

#[cfg(test)]
mod tests {
use {super::SocketPair, crate::RuleSet as _, syscalls::Sysno};

#[test]
fn name() {
assert_eq!(SocketPair.name(), "SocketPair");
}

#[test]
fn simple_rules() {
let rules = SocketPair.simple_rules();
assert_eq!(rules.len(), 1);
assert!(rules.contains(&Sysno::socketpair));
}

#[test]
fn conditional_rules() {
assert!(SocketPair.conditional_rules().is_empty());
}
}
Loading