Skip to main content

starnix_core/bpf/
fs.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// TODO(https://github.com/rust-lang/rust/issues/39371): remove
6#![allow(non_upper_case_globals)]
7
8use crate::bpf::syscalls::BpfTypeFormat;
9use crate::bpf::{BpfMapHandle, ProgramHandle};
10use crate::mm::memory::MemoryObject;
11use crate::mm::{DesiredAddress, MappingOptions, ProtectionFlags};
12use crate::security::{self, PermissionFlags};
13use crate::task::{
14    CurrentTask, EventHandler, SignalHandler, SignalHandlerInner, Task, WaitCanceler, Waiter,
15};
16use crate::vfs::buffers::{InputBuffer, OutputBuffer};
17use crate::vfs::{
18    AccessCheck, CacheMode, FdNumber, FileObject, FileOps, FileSystem, FileSystemHandle,
19    FileSystemOps, FileSystemOptions, FsNode, FsNodeHandle, FsNodeInfo, FsNodeOps, FsStr,
20    MemoryDirectoryFile, MemoryXattrStorage, NamespaceNode, RenameContext, XattrStorage as _,
21    default_mmap, fileops_impl_nonseekable, fileops_impl_noop_sync, fs_node_impl_not_dir,
22    fs_node_impl_xattr_delegate,
23};
24use bstr::BStr;
25use ebpf_api::RINGBUF_SIGNAL;
26use starnix_logging::track_stub;
27use starnix_types::vfs::default_statfs;
28use starnix_uapi::auth::FsCred;
29use starnix_uapi::device_id::DeviceId;
30use starnix_uapi::errors::Errno;
31use starnix_uapi::file_mode::{FileMode, mode};
32use starnix_uapi::open_flags::OpenFlags;
33use starnix_uapi::user_address::UserAddress;
34use starnix_uapi::vfs::FdEvents;
35use starnix_uapi::{BPF_FS_MAGIC, bpf_map_type_BPF_MAP_TYPE_RINGBUF, errno, error, statfs};
36use std::sync::Arc;
37
38/// A reference to a BPF object that can be stored in either an FD or an entry in the /sys/fs/bpf
39/// filesystem.
40#[derive(Debug, Clone)]
41pub enum BpfHandle {
42    Program(ProgramHandle),
43
44    // Stub used to fake loading of programs of unknown types.
45    ProgramStub(u32),
46
47    Map(BpfMapHandle),
48    BpfTypeFormat(Arc<BpfTypeFormat>),
49}
50
51impl BpfHandle {
52    pub fn as_map(&self) -> Result<&BpfMapHandle, Errno> {
53        match self {
54            Self::Map(map) => Ok(map),
55            _ => error!(EINVAL),
56        }
57    }
58    pub fn as_program(&self) -> Result<&ProgramHandle, Errno> {
59        match self {
60            Self::Program(program) => Ok(program),
61            _ => error!(EINVAL),
62        }
63    }
64
65    pub fn into_program(self) -> Result<ProgramHandle, Errno> {
66        match self {
67            Self::Program(program) => Ok(program),
68            _ => error!(EINVAL),
69        }
70    }
71
72    pub fn type_name(&self) -> &'static str {
73        match self {
74            Self::Map(_) => "bpf-map",
75            Self::Program(_) | Self::ProgramStub(_) => "bpf-prog",
76            Self::BpfTypeFormat(_) => "bpf-type",
77        }
78    }
79
80    /// Performs security-related checks when opening a BPF map. If
81    /// `permission_flags` is `None`, then they are inferred from the map's
82    /// schema. `permission_flags` is ignored for programs.
83    pub(super) fn security_check_open_fd(
84        &self,
85        current_task: &CurrentTask,
86        permission_flags: Option<PermissionFlags>,
87    ) -> Result<(), Errno> {
88        match self {
89            Self::Map(bpf_map) => security::check_bpf_map_access(
90                current_task,
91                &bpf_map.security_state,
92                permission_flags.unwrap_or_else(|| bpf_map.schema.flags.into()),
93            ),
94            Self::Program(program) => {
95                security::check_bpf_prog_access(current_task, &program.security_state)
96            }
97            _ => Ok(()),
98        }
99    }
100}
101
102impl From<ProgramHandle> for BpfHandle {
103    fn from(program: ProgramHandle) -> Self {
104        Self::Program(program)
105    }
106}
107
108impl From<BpfMapHandle> for BpfHandle {
109    fn from(map: BpfMapHandle) -> Self {
110        Self::Map(map)
111    }
112}
113
114impl From<BpfTypeFormat> for BpfHandle {
115    fn from(format: BpfTypeFormat) -> Self {
116        Self::BpfTypeFormat(Arc::new(format))
117    }
118}
119
120impl FileOps for BpfHandle {
121    fileops_impl_nonseekable!();
122    fileops_impl_noop_sync!();
123    fn read(
124        &self,
125        _file: &FileObject,
126        _current_task: &crate::task::CurrentTask,
127        _offset: usize,
128        _data: &mut dyn OutputBuffer,
129    ) -> Result<usize, Errno> {
130        track_stub!(TODO("https://fxbug.dev/322874229"), "bpf handle read");
131        error!(EINVAL)
132    }
133    fn write(
134        &self,
135        _file: &FileObject,
136        _current_task: &crate::task::CurrentTask,
137        _offset: usize,
138        _data: &mut dyn InputBuffer,
139    ) -> Result<usize, Errno> {
140        track_stub!(TODO("https://fxbug.dev/322873841"), "bpf handle write");
141        error!(EINVAL)
142    }
143
144    fn get_memory(
145        &self,
146        _file: &FileObject,
147        _current_task: &CurrentTask,
148        length: Option<usize>,
149        prot: ProtectionFlags,
150    ) -> Result<Arc<MemoryObject>, Errno> {
151        // Because of the specific condition needed to map this object, the size must be known.
152        let length = length.ok_or_else(|| errno!(EINVAL))?;
153
154        // This cannot be mapped executable.
155        if prot.contains(ProtectionFlags::EXEC) {
156            return error!(EPERM);
157        }
158
159        self.as_map()?.get_memory(length, prot)
160    }
161
162    fn mmap(
163        &self,
164        file: &FileObject,
165        current_task: &CurrentTask,
166        addr: DesiredAddress,
167        memory_offset: u64,
168        length: usize,
169        prot_flags: ProtectionFlags,
170        options: MappingOptions,
171    ) -> Result<UserAddress, Errno> {
172        let BpfHandle::Map(bpf_map) = &self else {
173            return error!(EINVAL);
174        };
175        security::check_bpf_map_access(
176            current_task,
177            &bpf_map.security_state,
178            PermissionFlags::READ | PermissionFlags::WRITE,
179        )?;
180        let options = options | MappingOptions::DONT_EXPAND;
181        default_mmap(file, current_task, addr, memory_offset, length, prot_flags, options)
182    }
183
184    fn wait_async(
185        &self,
186        _file: &FileObject,
187        _current_task: &CurrentTask,
188        waiter: &Waiter,
189        events: FdEvents,
190        handler: EventHandler,
191    ) -> Option<WaitCanceler> {
192        let BpfHandle::Map(bpf_map) = self else {
193            return None;
194        };
195
196        // Only ringbuffers can be polled for POLLIN.
197        if bpf_map.schema.map_type != bpf_map_type_BPF_MAP_TYPE_RINGBUF
198            || !events.contains(FdEvents::POLLIN)
199        {
200            return Some(WaitCanceler::new_noop());
201        }
202
203        let handler = SignalHandler {
204            inner: SignalHandlerInner::ZxHandle(|signals| {
205                if signals.contains(RINGBUF_SIGNAL) { FdEvents::POLLIN } else { FdEvents::empty() }
206            }),
207            event_handler: handler,
208            err_code: None,
209        };
210
211        // Reset the signal before waiting. The case when the ring buffer already has some data
212        // is handled by the caller: it should call `query_events` after starting the waiter.
213        bpf_map
214            .vmo()
215            .as_handle_ref()
216            .signal(RINGBUF_SIGNAL, zx::Signals::empty())
217            .expect("Failed to set signal or a ring buffer VMO");
218
219        let canceler = waiter
220            .wake_on_zircon_signals(&bpf_map.vmo().as_handle_ref(), RINGBUF_SIGNAL, handler)
221            .expect("Failed to wait for signals on ringbuf VMO");
222        Some(WaitCanceler::new_port(canceler))
223    }
224
225    fn query_events(
226        &self,
227        _file: &FileObject,
228        _current_task: &CurrentTask,
229    ) -> Result<FdEvents, Errno> {
230        match self {
231            Self::Map(map) => {
232                let events = match map.can_read() {
233                    Some(true) => FdEvents::POLLIN,
234                    Some(false) => FdEvents::empty(),
235                    None => FdEvents::POLLERR,
236                };
237                Ok(events)
238            }
239            _ => error!(EPERM),
240        }
241    }
242}
243
244pub fn get_bpf_object(task: &Task, fd: FdNumber) -> Result<BpfHandle, Errno> {
245    Ok((*task.files()?.get(fd)?.downcast_file::<BpfHandle>().ok_or_else(|| errno!(EBADF))?).clone())
246}
247pub struct BpfFs;
248impl BpfFs {
249    pub fn new_fs(
250        current_task: &CurrentTask,
251        options: FileSystemOptions,
252    ) -> Result<FileSystemHandle, Errno> {
253        let kernel = current_task.kernel();
254        let fs = FileSystem::new(kernel, CacheMode::Permanent, BpfFs, options)?;
255        let root_ino = fs.allocate_ino();
256        fs.create_root_with_info(
257            root_ino,
258            BpfFsDir::new(),
259            FsNodeInfo::new(mode!(IFDIR, 0o777) | FileMode::ISVTX, FsCred::root()),
260        );
261        Ok(fs)
262    }
263}
264
265impl FileSystemOps for BpfFs {
266    fn statfs(&self, _fs: &FileSystem, _current_task: &CurrentTask) -> Result<statfs, Errno> {
267        Ok(default_statfs(BPF_FS_MAGIC))
268    }
269    fn name(&self) -> &'static FsStr {
270        "bpf".into()
271    }
272
273    fn rename(
274        &self,
275        _fs: &FileSystem,
276        _current_task: &CurrentTask,
277        _context: &mut RenameContext<'_>,
278        _old_name: &FsStr,
279        _new_name: &FsStr,
280    ) -> Result<(), Errno> {
281        Ok(())
282    }
283}
284
285pub struct BpfFsDir {
286    xattrs: MemoryXattrStorage,
287}
288
289impl BpfFsDir {
290    fn new() -> Self {
291        Self { xattrs: MemoryXattrStorage::default() }
292    }
293
294    pub fn register_pin(
295        &self,
296        current_task: &CurrentTask,
297        node: &NamespaceNode,
298        name: &FsStr,
299        object: BpfHandle,
300    ) -> Result<(), Errno> {
301        node.entry.create_entry(current_task, &node.mount, name, |dir, _mount, _name| {
302            Ok(dir.fs().create_node_and_allocate_node_id(
303                BpfFsObject::new(object),
304                FsNodeInfo::new(mode!(IFREG, 0o600), current_task.current_fscred()),
305            ))
306        })?;
307        Ok(())
308    }
309}
310
311impl FsNodeOps for BpfFsDir {
312    fs_node_impl_xattr_delegate!(self, self.xattrs);
313
314    fn create_file_ops(
315        &self,
316        _node: &FsNode,
317        _current_task: &CurrentTask,
318        _flags: OpenFlags,
319    ) -> Result<Box<dyn FileOps>, Errno> {
320        Ok(Box::new(MemoryDirectoryFile::new()))
321    }
322
323    fn mkdir(
324        &self,
325        node: &FsNode,
326        _current_task: &CurrentTask,
327        _name: &FsStr,
328        mode: FileMode,
329        owner: FsCred,
330    ) -> Result<FsNodeHandle, Errno> {
331        Ok(node.fs().create_node_and_allocate_node_id(
332            BpfFsDir::new(),
333            FsNodeInfo::new(mode | FileMode::ISVTX, owner),
334        ))
335    }
336
337    fn mknod(
338        &self,
339        _node: &FsNode,
340        _current_task: &CurrentTask,
341        _name: &FsStr,
342        _mode: FileMode,
343        _dev: DeviceId,
344        _owner: FsCred,
345    ) -> Result<FsNodeHandle, Errno> {
346        error!(EPERM)
347    }
348
349    fn create_symlink(
350        &self,
351        _node: &FsNode,
352        _current_task: &CurrentTask,
353        _name: &FsStr,
354        _target: &FsStr,
355        _owner: FsCred,
356    ) -> Result<FsNodeHandle, Errno> {
357        error!(EPERM)
358    }
359
360    fn link(
361        &self,
362        _node: &FsNode,
363        _current_task: &CurrentTask,
364        _name: &FsStr,
365        _child: &FsNodeHandle,
366    ) -> Result<(), Errno> {
367        Ok(())
368    }
369
370    fn unlink(
371        &self,
372        _node: &FsNode,
373        _current_task: &CurrentTask,
374        _name: &FsStr,
375        _child: &FsNodeHandle,
376    ) -> Result<(), Errno> {
377        Ok(())
378    }
379}
380
381pub struct BpfFsObject {
382    pub handle: BpfHandle,
383    xattrs: MemoryXattrStorage,
384}
385
386impl BpfFsObject {
387    fn new(handle: BpfHandle) -> Self {
388        Self { handle, xattrs: MemoryXattrStorage::default() }
389    }
390}
391
392impl FsNodeOps for BpfFsObject {
393    fs_node_impl_not_dir!();
394    fs_node_impl_xattr_delegate!(self, self.xattrs);
395
396    fn create_file_ops(
397        &self,
398        _node: &FsNode,
399        _current_task: &CurrentTask,
400        _flags: OpenFlags,
401    ) -> Result<Box<dyn FileOps>, Errno> {
402        error!(EIO)
403    }
404}
405
406/// Resolves a pinned BPF object from a path, returning the underlying handle.
407/// Performs DAC and MAC checks using the specified `open_flags `. Also updates
408/// atime unless `NOATIME` flag is set.
409pub fn resolve_pinned_bpf_object(
410    current_task: &CurrentTask,
411    path: &BStr,
412    open_flags: OpenFlags,
413) -> Result<BpfHandle, Errno> {
414    let node = current_task.lookup_path_from_root(path.as_ref())?;
415
416    let permission_flags = PermissionFlags::from(open_flags);
417    node.check_access(current_task, AccessCheck::for_access(permission_flags))?;
418
419    let object = node.entry.node.downcast_ops::<BpfFsObject>().ok_or_else(|| errno!(EPERM))?;
420    object.handle.security_check_open_fd(current_task, Some(permission_flags))?;
421
422    if !open_flags.contains(OpenFlags::NOATIME) {
423        node.update_atime();
424    }
425
426    Ok(object.handle.clone())
427}