Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | use anyhow::Result; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 18 | use log::{debug, error, warn}; |
| 19 | use std::collections::{btree_map, BTreeMap}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 20 | use std::convert::TryFrom; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 21 | use std::ffi::{CStr, OsStr}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 22 | use std::fs::OpenOptions; |
| 23 | use std::io; |
| 24 | use std::mem::MaybeUninit; |
| 25 | use std::option::Option; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 26 | use std::os::unix::{ffi::OsStrExt, io::AsRawFd}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 27 | use std::path::Path; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 28 | use std::sync::{Arc, Mutex}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 29 | use std::time::Duration; |
| 30 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 31 | use fuse::filesystem::{ |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 32 | Context, DirEntry, DirectoryIterator, Entry, FileSystem, FsOptions, GetxattrReply, |
| 33 | SetattrValid, ZeroCopyReader, ZeroCopyWriter, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 34 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 35 | use fuse::mount::MountOption; |
| 36 | |
Victor Hsieh | ac4f3f4 | 2021-02-26 12:35:58 -0800 | [diff] [blame] | 37 | use crate::common::{divide_roundup, ChunkedSizeIter, CHUNK_SIZE}; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 38 | use crate::file::{ |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 39 | RandomWrite, ReadByChunk, RemoteDirEditor, RemoteFileEditor, RemoteFileReader, |
| 40 | RemoteMerkleTreeReader, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 41 | }; |
| 42 | use crate::fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 43 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 44 | const DEFAULT_METADATA_TIMEOUT: std::time::Duration = Duration::from_secs(5); |
| 45 | |
| 46 | pub type Inode = u64; |
| 47 | type Handle = u64; |
| 48 | |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 49 | /// `FileConfig` defines the file type supported by AuthFS. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 50 | pub enum FileConfig { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 51 | /// A file type that is verified against fs-verity signature (thus read-only). The file is |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 52 | /// served from a remote server. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 53 | VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 54 | reader: VerifiedFileReader<RemoteFileReader, RemoteMerkleTreeReader>, |
| 55 | file_size: u64, |
| 56 | }, |
| 57 | /// A file type that is a read-only passthrough from a file on a remote serrver. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 58 | UnverifiedReadonly { reader: RemoteFileReader, file_size: u64 }, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 59 | /// A file type that is initially empty, and the content is stored on a remote server. File |
| 60 | /// integrity is guaranteed with private Merkle tree. |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 61 | VerifiedNew { editor: VerifiedFileEditor<RemoteFileEditor> }, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 62 | /// A directory type that is initially empty. One can create new file (`VerifiedNew`) and new |
| 63 | /// directory (`VerifiedNewDirectory` itself) with integrity guaranteed within the VM. |
| 64 | VerifiedNewDirectory { dir: RemoteDirEditor }, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | struct AuthFs { |
| 68 | /// Store `FileConfig`s using the `Inode` number as the search index. |
| 69 | /// |
| 70 | /// For further optimization to minimize the search cost, since Inode is integer, we may |
| 71 | /// consider storing them in a Vec if we can guarantee that the numbers are small and |
| 72 | /// consecutive. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 73 | file_pool: Arc<Mutex<BTreeMap<Inode, FileConfig>>>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 74 | |
| 75 | /// Maximum bytes in the write transaction to the FUSE device. This limits the maximum size to |
| 76 | /// a read request (including FUSE protocol overhead). |
| 77 | max_write: u32, |
| 78 | } |
| 79 | |
| 80 | impl AuthFs { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 81 | pub fn new(file_pool: Arc<Mutex<BTreeMap<Inode, FileConfig>>>, max_write: u32) -> AuthFs { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 82 | AuthFs { file_pool, max_write } |
| 83 | } |
| 84 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 85 | /// Handles the file associated with `inode` if found. This function returns whatever |
| 86 | /// `handle_fn` returns. |
| 87 | fn handle_file<F, R>(&self, inode: &Inode, handle_fn: F) -> io::Result<R> |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 88 | where |
| 89 | F: FnOnce(&FileConfig) -> io::Result<R>, |
| 90 | { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 91 | let file_pool = self.file_pool.lock().unwrap(); |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 92 | let config = |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 93 | file_pool.get(inode).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?; |
| 94 | handle_fn(config) |
| 95 | } |
| 96 | |
| 97 | /// Inserts a new inode and corresponding `FileConfig` created by `create_fn` to the file pool, |
| 98 | /// then returns the new inode number. |
| 99 | fn insert_new_inode<F>(&self, inode: &Inode, create_fn: F) -> io::Result<Inode> |
| 100 | where |
| 101 | F: FnOnce(&mut FileConfig) -> io::Result<(Inode, FileConfig)>, |
| 102 | { |
| 103 | let mut file_pool = self.file_pool.lock().unwrap(); |
| 104 | let mut config = |
| 105 | file_pool.get_mut(inode).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?; |
| 106 | let (new_inode, new_file_config) = create_fn(&mut config)?; |
| 107 | if let btree_map::Entry::Vacant(entry) = file_pool.entry(new_inode) { |
| 108 | entry.insert(new_file_config); |
| 109 | Ok(new_inode) |
| 110 | } else { |
| 111 | // We can't assume fd_server is trusted, so the returned FD may collide with existing |
| 112 | // one, even when we are creating a new file. Do not override an existing FD. In terms |
| 113 | // of security, it is better to "leak" the file created earlier, than returning an |
| 114 | // existing inode as a new file. |
| 115 | error!("Inode {} already exists, do not override", new_inode); |
| 116 | Err(io::Error::from_raw_os_error(libc::EIO)) |
| 117 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | fn check_access_mode(flags: u32, mode: libc::c_int) -> io::Result<()> { |
| 122 | if (flags & libc::O_ACCMODE as u32) == mode as u32 { |
| 123 | Ok(()) |
| 124 | } else { |
| 125 | Err(io::Error::from_raw_os_error(libc::EACCES)) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | cfg_if::cfg_if! { |
| 130 | if #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 131 | fn blk_size() -> libc::c_int { CHUNK_SIZE as libc::c_int } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 132 | } else { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 133 | fn blk_size() -> libc::c_long { CHUNK_SIZE as libc::c_long } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 137 | #[allow(clippy::enum_variant_names)] |
| 138 | enum AccessMode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 139 | ReadOnly, |
| 140 | ReadWrite, |
| 141 | } |
| 142 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 143 | fn create_stat( |
| 144 | ino: libc::ino_t, |
| 145 | file_size: u64, |
| 146 | access_mode: AccessMode, |
| 147 | ) -> io::Result<libc::stat64> { |
| 148 | // SAFETY: stat64 is a plan C struct without pointer. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 149 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 150 | |
| 151 | st.st_ino = ino; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 152 | st.st_mode = match access_mode { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 153 | // Until needed, let's just grant the owner access. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 154 | // TODO(205169366): Implement mode properly. |
| 155 | AccessMode::ReadOnly => libc::S_IFREG | libc::S_IRUSR, |
| 156 | AccessMode::ReadWrite => libc::S_IFREG | libc::S_IRUSR | libc::S_IWUSR, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 157 | }; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 158 | st.st_nlink = 1; |
| 159 | st.st_uid = 0; |
| 160 | st.st_gid = 0; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 161 | st.st_size = libc::off64_t::try_from(file_size) |
| 162 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 163 | st.st_blksize = blk_size(); |
| 164 | // Per man stat(2), st_blocks is "Number of 512B blocks allocated". |
| 165 | st.st_blocks = libc::c_longlong::try_from(divide_roundup(file_size, 512)) |
| 166 | .map_err(|_| io::Error::from_raw_os_error(libc::EFBIG))?; |
| 167 | Ok(st) |
| 168 | } |
| 169 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 170 | fn create_dir_stat(ino: libc::ino_t, file_number: u16) -> io::Result<libc::stat64> { |
| 171 | // SAFETY: stat64 is a plan C struct without pointer. |
| 172 | let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() }; |
| 173 | |
| 174 | st.st_ino = ino; |
| 175 | // TODO(205169366): Implement mode properly. |
| 176 | st.st_mode = libc::S_IFDIR |
| 177 | | libc::S_IXUSR |
| 178 | | libc::S_IWUSR |
| 179 | | libc::S_IRUSR |
| 180 | | libc::S_IXGRP |
| 181 | | libc::S_IXOTH; |
| 182 | |
| 183 | // 2 extra for . and .. |
| 184 | st.st_nlink = file_number |
| 185 | .checked_add(2) |
| 186 | .ok_or_else(|| io::Error::from_raw_os_error(libc::EOVERFLOW))? |
| 187 | .into(); |
| 188 | |
| 189 | st.st_uid = 0; |
| 190 | st.st_gid = 0; |
| 191 | Ok(st) |
| 192 | } |
| 193 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 194 | fn offset_to_chunk_index(offset: u64) -> u64 { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 195 | offset / CHUNK_SIZE |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 198 | fn read_chunks<W: io::Write, T: ReadByChunk>( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 199 | mut w: W, |
| 200 | file: &T, |
| 201 | file_size: u64, |
| 202 | offset: u64, |
| 203 | size: u32, |
| 204 | ) -> io::Result<usize> { |
| 205 | let remaining = file_size.saturating_sub(offset); |
| 206 | let size_to_read = std::cmp::min(size as usize, remaining as usize); |
Victor Hsieh | ac4f3f4 | 2021-02-26 12:35:58 -0800 | [diff] [blame] | 207 | let total = ChunkedSizeIter::new(size_to_read, offset, CHUNK_SIZE as usize).try_fold( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 208 | 0, |
| 209 | |total, (current_offset, planned_data_size)| { |
| 210 | // TODO(victorhsieh): There might be a non-trivial way to avoid this copy. For example, |
| 211 | // instead of accepting a buffer, the writer could expose the final destination buffer |
| 212 | // for the reader to write to. It might not be generally applicable though, e.g. with |
| 213 | // virtio transport, the buffer may not be continuous. |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 214 | let mut buf = [0u8; CHUNK_SIZE as usize]; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 215 | let read_size = file.read_chunk(offset_to_chunk_index(current_offset), &mut buf)?; |
| 216 | if read_size < planned_data_size { |
| 217 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 218 | } |
| 219 | |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 220 | let begin = (current_offset % CHUNK_SIZE) as usize; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 221 | let end = begin + planned_data_size; |
| 222 | let s = w.write(&buf[begin..end])?; |
| 223 | if s != planned_data_size { |
| 224 | return Err(io::Error::from_raw_os_error(libc::EIO)); |
| 225 | } |
| 226 | Ok(total + s) |
| 227 | }, |
| 228 | )?; |
| 229 | |
| 230 | Ok(total) |
| 231 | } |
| 232 | |
| 233 | // No need to support enumerating directory entries. |
| 234 | struct EmptyDirectoryIterator {} |
| 235 | |
| 236 | impl DirectoryIterator for EmptyDirectoryIterator { |
| 237 | fn next(&mut self) -> Option<DirEntry> { |
| 238 | None |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | impl FileSystem for AuthFs { |
| 243 | type Inode = Inode; |
| 244 | type Handle = Handle; |
| 245 | type DirIter = EmptyDirectoryIterator; |
| 246 | |
| 247 | fn max_buffer_size(&self) -> u32 { |
| 248 | self.max_write |
| 249 | } |
| 250 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 251 | fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> { |
| 252 | // Enable writeback cache for better performance especially since our bandwidth to the |
| 253 | // backend service is limited. |
| 254 | Ok(FsOptions::WRITEBACK_CACHE) |
| 255 | } |
| 256 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 257 | fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> { |
| 258 | // TODO(victorhsieh): convert root directory (inode == 1) to a readonly directory. Right |
| 259 | // now, it's the (global) inode pool, so all inodes can be accessed from root. |
| 260 | if parent == 1 { |
| 261 | // Only accept file name that looks like an integrer. Files in the pool are simply |
| 262 | // exposed by their inode number. Also, there is currently no directory structure. |
| 263 | let num = name.to_str().map_err(|_| io::Error::from_raw_os_error(libc::EINVAL))?; |
| 264 | // Normally, `lookup` is required to increase a reference count for the inode (while |
| 265 | // `forget` will decrease it). It is not necessary here since the files are configured |
| 266 | // to be static. |
| 267 | let inode = |
| 268 | num.parse::<Inode>().map_err(|_| io::Error::from_raw_os_error(libc::ENOENT))?; |
| 269 | let st = self.handle_file(&inode, |config| match config { |
| 270 | FileConfig::UnverifiedReadonly { file_size, .. } |
| 271 | | FileConfig::VerifiedReadonly { file_size, .. } => { |
| 272 | create_stat(inode, *file_size, AccessMode::ReadOnly) |
| 273 | } |
| 274 | FileConfig::VerifiedNew { editor } => { |
| 275 | create_stat(inode, editor.size(), AccessMode::ReadWrite) |
| 276 | } |
| 277 | FileConfig::VerifiedNewDirectory { dir } => { |
| 278 | create_dir_stat(inode, dir.number_of_entries()) |
| 279 | } |
| 280 | })?; |
| 281 | Ok(Entry { |
| 282 | inode, |
| 283 | generation: 0, |
| 284 | attr: st, |
| 285 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 286 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 287 | }) |
| 288 | } else { |
| 289 | let inode = self.handle_file(&parent, |config| match config { |
| 290 | FileConfig::VerifiedNewDirectory { dir } => { |
| 291 | let path: &Path = cstr_to_path(name); |
| 292 | dir.find_inode(path).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT)) |
| 293 | } |
| 294 | _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)), |
| 295 | })?; |
| 296 | let st = self.handle_file(&inode, |config| match config { |
| 297 | FileConfig::VerifiedNew { editor } => { |
| 298 | create_stat(inode, editor.size(), AccessMode::ReadWrite) |
| 299 | } |
| 300 | FileConfig::VerifiedNewDirectory { dir } => { |
| 301 | create_dir_stat(inode, dir.number_of_entries()) |
| 302 | } |
| 303 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 304 | })?; |
| 305 | Ok(Entry { |
| 306 | inode, |
| 307 | generation: 0, |
| 308 | attr: st, |
| 309 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 310 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 311 | }) |
| 312 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | fn getattr( |
| 316 | &self, |
| 317 | _ctx: Context, |
| 318 | inode: Inode, |
| 319 | _handle: Option<Handle>, |
| 320 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 321 | self.handle_file(&inode, |config| { |
| 322 | Ok(( |
| 323 | match config { |
| 324 | FileConfig::UnverifiedReadonly { file_size, .. } |
| 325 | | FileConfig::VerifiedReadonly { file_size, .. } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 326 | create_stat(inode, *file_size, AccessMode::ReadOnly)? |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 327 | } |
| 328 | FileConfig::VerifiedNew { editor } => { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 329 | create_stat(inode, editor.size(), AccessMode::ReadWrite)? |
| 330 | } |
| 331 | FileConfig::VerifiedNewDirectory { dir } => { |
| 332 | create_dir_stat(inode, dir.number_of_entries())? |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 333 | } |
| 334 | }, |
| 335 | DEFAULT_METADATA_TIMEOUT, |
| 336 | )) |
| 337 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | fn open( |
| 341 | &self, |
| 342 | _ctx: Context, |
| 343 | inode: Self::Inode, |
| 344 | flags: u32, |
| 345 | ) -> io::Result<(Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 346 | // Since file handle is not really used in later operations (which use Inode directly), |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 347 | // return None as the handle. |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 348 | self.handle_file(&inode, |config| { |
| 349 | match config { |
| 350 | FileConfig::VerifiedReadonly { .. } | FileConfig::UnverifiedReadonly { .. } => { |
| 351 | check_access_mode(flags, libc::O_RDONLY)?; |
| 352 | } |
| 353 | FileConfig::VerifiedNew { .. } => { |
| 354 | // No need to check access modes since all the modes are allowed to the |
| 355 | // read-writable file. |
| 356 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 357 | FileConfig::VerifiedNewDirectory { .. } => { |
| 358 | // TODO(victorhsieh): implement when needed. |
| 359 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 360 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 361 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 362 | // Always cache the file content. There is currently no need to support direct I/O or |
| 363 | // avoid the cache buffer. Memory mapping is only possible with cache enabled. |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 364 | Ok((None, fuse::sys::OpenOptions::KEEP_CACHE)) |
| 365 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 368 | fn create( |
| 369 | &self, |
| 370 | _ctx: Context, |
| 371 | parent: Self::Inode, |
| 372 | name: &CStr, |
| 373 | _mode: u32, |
| 374 | _flags: u32, |
| 375 | _umask: u32, |
| 376 | ) -> io::Result<(Entry, Option<Self::Handle>, fuse::sys::OpenOptions)> { |
| 377 | // TODO(205169366): Implement mode properly. |
| 378 | // TODO(205172873): handle O_TRUNC and O_EXCL properly. |
| 379 | let new_inode = self.insert_new_inode(&parent, |config| match config { |
| 380 | FileConfig::VerifiedNewDirectory { dir } => { |
| 381 | let basename: &Path = cstr_to_path(name); |
| 382 | if dir.find_inode(basename).is_some() { |
| 383 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
| 384 | } |
| 385 | let (new_inode, new_file) = dir.create_file(basename)?; |
| 386 | Ok((new_inode, FileConfig::VerifiedNew { editor: new_file })) |
| 387 | } |
| 388 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 389 | })?; |
| 390 | |
| 391 | Ok(( |
| 392 | Entry { |
| 393 | inode: new_inode, |
| 394 | generation: 0, |
| 395 | attr: create_stat(new_inode, /* file_size */ 0, AccessMode::ReadWrite)?, |
| 396 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 397 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 398 | }, |
| 399 | // See also `open`. |
| 400 | /* handle */ None, |
| 401 | fuse::sys::OpenOptions::KEEP_CACHE, |
| 402 | )) |
| 403 | } |
| 404 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 405 | fn read<W: io::Write + ZeroCopyWriter>( |
| 406 | &self, |
| 407 | _ctx: Context, |
| 408 | inode: Inode, |
| 409 | _handle: Handle, |
| 410 | w: W, |
| 411 | size: u32, |
| 412 | offset: u64, |
| 413 | _lock_owner: Option<u64>, |
| 414 | _flags: u32, |
| 415 | ) -> io::Result<usize> { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 416 | self.handle_file(&inode, |config| { |
| 417 | match config { |
| 418 | FileConfig::VerifiedReadonly { reader, file_size } => { |
| 419 | read_chunks(w, reader, *file_size, offset, size) |
| 420 | } |
| 421 | FileConfig::UnverifiedReadonly { reader, file_size } => { |
| 422 | read_chunks(w, reader, *file_size, offset, size) |
| 423 | } |
| 424 | FileConfig::VerifiedNew { editor } => { |
| 425 | // Note that with FsOptions::WRITEBACK_CACHE, it's possible for the kernel to |
| 426 | // request a read even if the file is open with O_WRONLY. |
| 427 | read_chunks(w, editor, editor.size(), offset, size) |
| 428 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 429 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 430 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 431 | }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | fn write<R: io::Read + ZeroCopyReader>( |
| 435 | &self, |
| 436 | _ctx: Context, |
| 437 | inode: Self::Inode, |
| 438 | _handle: Self::Handle, |
| 439 | mut r: R, |
| 440 | size: u32, |
| 441 | offset: u64, |
| 442 | _lock_owner: Option<u64>, |
| 443 | _delayed_write: bool, |
| 444 | _flags: u32, |
| 445 | ) -> io::Result<usize> { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 446 | self.handle_file(&inode, |config| match config { |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 447 | FileConfig::VerifiedNew { editor } => { |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 448 | let mut buf = vec![0; size as usize]; |
| 449 | r.read_exact(&mut buf)?; |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 450 | editor.write_at(&buf, offset) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 451 | } |
| 452 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 453 | }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 454 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 455 | |
| 456 | fn setattr( |
| 457 | &self, |
| 458 | _ctx: Context, |
| 459 | inode: Inode, |
| 460 | attr: libc::stat64, |
| 461 | _handle: Option<Handle>, |
| 462 | valid: SetattrValid, |
| 463 | ) -> io::Result<(libc::stat64, Duration)> { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 464 | self.handle_file(&inode, |config| { |
| 465 | match config { |
| 466 | FileConfig::VerifiedNew { editor } => { |
| 467 | // Initialize the default stat. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 468 | let mut new_attr = create_stat(inode, editor.size(), AccessMode::ReadWrite)?; |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 469 | // `valid` indicates what fields in `attr` are valid. Update to return correctly. |
| 470 | if valid.contains(SetattrValid::SIZE) { |
| 471 | // st_size is i64, but the cast should be safe since kernel should not give a |
| 472 | // negative size. |
| 473 | debug_assert!(attr.st_size >= 0); |
| 474 | new_attr.st_size = attr.st_size; |
| 475 | editor.resize(attr.st_size as u64)?; |
| 476 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 477 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 478 | if valid.contains(SetattrValid::MODE) { |
| 479 | warn!("Changing st_mode is not currently supported"); |
| 480 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 481 | } |
| 482 | if valid.contains(SetattrValid::UID) { |
| 483 | warn!("Changing st_uid is not currently supported"); |
| 484 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 485 | } |
| 486 | if valid.contains(SetattrValid::GID) { |
| 487 | warn!("Changing st_gid is not currently supported"); |
| 488 | return Err(io::Error::from_raw_os_error(libc::ENOSYS)); |
| 489 | } |
| 490 | if valid.contains(SetattrValid::CTIME) { |
| 491 | debug!( |
| 492 | "Ignoring ctime change as authfs does not maintain timestamp currently" |
| 493 | ); |
| 494 | } |
| 495 | if valid.intersects(SetattrValid::ATIME | SetattrValid::ATIME_NOW) { |
| 496 | debug!( |
| 497 | "Ignoring atime change as authfs does not maintain timestamp currently" |
| 498 | ); |
| 499 | } |
| 500 | if valid.intersects(SetattrValid::MTIME | SetattrValid::MTIME_NOW) { |
| 501 | debug!( |
| 502 | "Ignoring mtime change as authfs does not maintain timestamp currently" |
| 503 | ); |
| 504 | } |
| 505 | Ok((new_attr, DEFAULT_METADATA_TIMEOUT)) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 506 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 507 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 508 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 509 | }) |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 510 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 511 | |
| 512 | fn getxattr( |
| 513 | &self, |
| 514 | _ctx: Context, |
| 515 | inode: Self::Inode, |
| 516 | name: &CStr, |
| 517 | size: u32, |
| 518 | ) -> io::Result<GetxattrReply> { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 519 | self.handle_file(&inode, |config| { |
| 520 | match config { |
| 521 | FileConfig::VerifiedNew { editor } => { |
| 522 | // FUSE ioctl is limited, thus we can't implement fs-verity ioctls without a kernel |
| 523 | // change (see b/196635431). Until it's possible, use xattr to expose what we need |
| 524 | // as an authfs specific API. |
| 525 | if name != CStr::from_bytes_with_nul(b"authfs.fsverity.digest\0").unwrap() { |
| 526 | return Err(io::Error::from_raw_os_error(libc::ENODATA)); |
| 527 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 528 | |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 529 | if size == 0 { |
| 530 | // Per protocol, when size is 0, return the value size. |
| 531 | Ok(GetxattrReply::Count(editor.get_fsverity_digest_size() as u32)) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 532 | } else { |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 533 | let digest = editor.calculate_fsverity_digest()?; |
| 534 | if digest.len() > size as usize { |
| 535 | Err(io::Error::from_raw_os_error(libc::ERANGE)) |
| 536 | } else { |
| 537 | Ok(GetxattrReply::Value(digest.to_vec())) |
| 538 | } |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 541 | _ => Err(io::Error::from_raw_os_error(libc::ENODATA)), |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 542 | } |
Victor Hsieh | c85e4ef | 2021-10-18 15:28:53 -0700 | [diff] [blame] | 543 | }) |
Victor Hsieh | 71f1003 | 2021-08-13 11:24:02 -0700 | [diff] [blame] | 544 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 545 | |
| 546 | fn mkdir( |
| 547 | &self, |
| 548 | _ctx: Context, |
| 549 | parent: Self::Inode, |
| 550 | name: &CStr, |
| 551 | _mode: u32, |
| 552 | _umask: u32, |
| 553 | ) -> io::Result<Entry> { |
| 554 | // TODO(205169366): Implement mode properly. |
| 555 | let new_inode = self.insert_new_inode(&parent, |config| match config { |
| 556 | FileConfig::VerifiedNewDirectory { dir } => { |
| 557 | let basename: &Path = cstr_to_path(name); |
| 558 | if dir.find_inode(basename).is_some() { |
| 559 | return Err(io::Error::from_raw_os_error(libc::EEXIST)); |
| 560 | } |
| 561 | let (new_inode, new_dir) = dir.mkdir(basename)?; |
| 562 | Ok((new_inode, FileConfig::VerifiedNewDirectory { dir: new_dir })) |
| 563 | } |
| 564 | _ => Err(io::Error::from_raw_os_error(libc::EBADF)), |
| 565 | })?; |
| 566 | |
| 567 | Ok(Entry { |
| 568 | inode: new_inode, |
| 569 | generation: 0, |
| 570 | attr: create_dir_stat(new_inode, /* file_number */ 0)?, |
| 571 | entry_timeout: DEFAULT_METADATA_TIMEOUT, |
| 572 | attr_timeout: DEFAULT_METADATA_TIMEOUT, |
| 573 | }) |
| 574 | } |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /// Mount and start the FUSE instance. This requires CAP_SYS_ADMIN. |
| 578 | pub fn loop_forever( |
| 579 | file_pool: BTreeMap<Inode, FileConfig>, |
| 580 | mountpoint: &Path, |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 581 | extra_options: &Option<String>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 582 | ) -> Result<(), fuse::Error> { |
| 583 | let max_read: u32 = 65536; |
| 584 | let max_write: u32 = 65536; |
| 585 | let dev_fuse = OpenOptions::new() |
| 586 | .read(true) |
| 587 | .write(true) |
| 588 | .open("/dev/fuse") |
| 589 | .expect("Failed to open /dev/fuse"); |
| 590 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 591 | let mut mount_options = vec![ |
| 592 | MountOption::FD(dev_fuse.as_raw_fd()), |
| 593 | MountOption::RootMode(libc::S_IFDIR | libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH), |
| 594 | MountOption::AllowOther, |
| 595 | MountOption::UserId(0), |
| 596 | MountOption::GroupId(0), |
| 597 | MountOption::MaxRead(max_read), |
| 598 | ]; |
| 599 | if let Some(value) = extra_options { |
| 600 | mount_options.push(MountOption::Extra(value)); |
| 601 | } |
| 602 | |
| 603 | fuse::mount(mountpoint, "authfs", libc::MS_NOSUID | libc::MS_NODEV, &mount_options) |
| 604 | .expect("Failed to mount fuse"); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 605 | |
| 606 | fuse::worker::start_message_loop( |
| 607 | dev_fuse, |
| 608 | max_write, |
| 609 | max_read, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 610 | AuthFs::new(Arc::new(Mutex::new(file_pool)), max_write), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 611 | ) |
| 612 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 613 | |
| 614 | fn cstr_to_path(cstr: &CStr) -> &Path { |
| 615 | OsStr::from_bytes(cstr.to_bytes()).as_ref() |
| 616 | } |