falco_event/types/primitive/
newtypes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::event_derive::format_type;
use crate::fields::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;
use crate::types::{BorrowDeref, Borrowed};
use std::fmt::{Debug, Formatter};

macro_rules! default_format {
    ($name:ident($repr:ty)) => {
        impl<F> Format<F> for $name
        where
            $repr: Format<F>,
        {
            fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
                self.0.format(fmt)
            }
        }
    };
}

macro_rules! newtype {
    ($(#[$attr:meta])* $name:ident($repr:ty)) => {
        $(#[$attr])*
        #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[cfg_attr(feature = "serde", serde(transparent))]
        pub struct $name(pub $repr);

        impl FromBytes<'_> for $name {
            fn from_bytes(buf: &mut &[u8]) -> FromBytesResult<Self>
            where
                Self: Sized,
            {
                Ok(Self(FromBytes::from_bytes(buf)?))
            }
        }

        impl ToBytes for $name {
            fn binary_size(&self) -> usize {
                self.0.binary_size()
            }

            fn write<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
                self.0.write(writer)
            }

            fn default_repr() -> impl ToBytes {
                <$repr>::default_repr()
            }
        }

        impl Borrowed for $name {
            type Owned = Self;
        }

        impl BorrowDeref for $name {
            type Target<'a> = $name;

            fn borrow_deref(&self) -> Self::Target<'_> {
                *self
            }
        }
    };
}

newtype!(
    /// Syscall result
    #[derive(Debug)]
    SyscallResult(i64)
);

#[cfg(target_os = "linux")]
impl<F> Format<F> for SyscallResult
where
    i64: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        if self.0 < 0 {
            let errno = nix::errno::Errno::from_raw(-self.0 as i32);
            if errno == nix::errno::Errno::UnknownErrno {
                // always format errors as decimal
                <i64 as Format<format_type::PF_DEC>>::format(&self.0, fmt)
            } else {
                write!(fmt, "{}({:?})", self.0, errno)
            }
        } else {
            self.0.format(fmt)
        }
    }
}

// not on Linux, we don't have the Linux errnos without maintaining the list ourselves
#[cfg(not(target_os = "linux"))]
impl<F> Format<F> for SyscallResult
where
    i64: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        if self.0 < 0 {
            // always format errors as decimal
            <i64 as Format<format_type::PF_DEC>>::format(&self.0, fmt)
        } else {
            self.0.format(fmt)
        }
    }
}

newtype!(
    /// A system call number
    #[derive(Debug)]
    SyscallId(u16)
);
default_format!(SyscallId(u16));

newtype!(
    /// A signal number
    #[derive(Debug)]
    SigType(u8)
);

impl<F> Format<F> for SigType
where
    u8: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        self.0.format(fmt)?;

        #[cfg(target_os = "linux")]
        {
            let sig = nix::sys::signal::Signal::try_from(self.0 as i32);
            if let Ok(sig) = sig {
                write!(fmt, "({sig:?})")?;
            }
        }

        Ok(())
    }
}

newtype!(
    /// File descriptor
    #[derive(Debug)]
    Fd(i64)
);

impl<F> Format<F> for Fd
where
    i64: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        if self.0 == -100 {
            fmt.write_str("AT_FDCWD")
        } else {
            self.0.format(fmt)
        }
    }
}

newtype!(
    /// Process or thread id
    #[derive(Debug)]
    Pid(i64)
);
default_format!(Pid(i64));

newtype!(
    /// User id
    #[derive(Debug)]
    Uid(u32)
);
default_format!(Uid(u32));

newtype!(
    /// Group id
    #[derive(Debug)]
    Gid(u32)
);
default_format!(Gid(u32));

newtype!(
    /// Signal set (bitmask of signals, only the lower 32 bits are used)
    #[derive(Debug)]
    SigSet(u32)
);

impl<F> Format<F> for SigSet
where
    SigType: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        <u32 as Format<format_type::PF_HEX>>::format(&self.0, fmt)?;
        if self.0 != 0 {
            let mut first = false;
            for sig in 0..32 {
                if (self.0 & (1 << sig)) != 0 {
                    if first {
                        write!(fmt, "(")?;
                        first = false;
                    } else {
                        write!(fmt, ",")?;
                    }
                    let sig_type = SigType(sig);
                    sig_type.format(fmt)?;
                }
            }
            write!(fmt, ")")?;
        }

        Ok(())
    }
}

newtype!(
    /// IP port number
    ///
    /// This looks unused
    #[derive(Debug)]
    Port(u16)
);
default_format!(Port(u16));

newtype!(
    /// Layer 4 protocol (tcp/udp)
    ///
    /// This looks unused
    #[derive(Debug)]
    L4Proto(u8)
);
default_format!(L4Proto(u8));

newtype!(
    /// Socket family (`PPM_AF_*`)
    ///
    /// This looks unused
    #[derive(Debug)]
    SockFamily(u8)
);
default_format!(SockFamily(u8));

newtype!(
    /// Boolean value (0/1)
    ///
    /// This looks unused
    #[derive(Debug)]
    Bool(u32)
);

impl<F> Format<F> for Bool {
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        match self.0 {
            0 => fmt.write_str("false"),
            1 => fmt.write_str("true"),
            n => write!(fmt, "true({n})"),
        }
    }
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
    use crate::types::SyscallResult;

    #[test]
    fn test_serde_newtype() {
        let val = SyscallResult(-2);
        let json = serde_json::to_string(&val).unwrap();

        assert_eq!(json, "-2");
        let val2: SyscallResult = serde_json::from_str(&json).unwrap();
        assert_eq!(val, val2);
    }
}