Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [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 std::collections::HashMap; |
| 18 | use std::io; |
| 19 | use std::path::{Path, PathBuf}; |
| 20 | |
| 21 | use super::remote_file::RemoteFileEditor; |
| 22 | use super::{VirtFdService, VirtFdServiceStatus}; |
| 23 | use crate::fsverity::VerifiedFileEditor; |
| 24 | use crate::fusefs::Inode; |
| 25 | |
| 26 | const MAX_ENTRIES: u16 = 100; // Arbitrary limit |
| 27 | |
| 28 | /// A remote directory backed by a remote directory FD, where the provider/fd_server is not |
| 29 | /// trusted. |
| 30 | /// |
| 31 | /// The directory is assumed empty initially without the trust to the storage. Functionally, when |
| 32 | /// the backing storage is not clean, the fd_server can fail to create a file or directory when |
| 33 | /// there is name collision. From RemoteDirEditor's perspective of security, the creation failure |
| 34 | /// is just one of possible errors that can happen, and what matters is RemoteDirEditor maintains |
| 35 | /// the integrity itself. |
| 36 | /// |
| 37 | /// When new files are created through RemoteDirEditor, the file integrity are maintained within the |
| 38 | /// VM. Similarly, integrity (namely the list of entries) of the directory, or new directories |
| 39 | /// created within such a directory, are also maintained within the VM. A compromised fd_server or |
| 40 | /// malicious client can't affect the view to the files and directories within such a directory in |
| 41 | /// the VM. |
| 42 | pub struct RemoteDirEditor { |
| 43 | service: VirtFdService, |
| 44 | remote_dir_fd: i32, |
| 45 | |
| 46 | /// Mapping of entry names to the corresponding inode number. The actual file/directory is |
| 47 | /// stored in the global pool in fusefs. |
| 48 | entries: HashMap<PathBuf, Inode>, |
| 49 | } |
| 50 | |
| 51 | impl RemoteDirEditor { |
| 52 | pub fn new(service: VirtFdService, remote_dir_fd: i32) -> Self { |
| 53 | RemoteDirEditor { service, remote_dir_fd, entries: HashMap::new() } |
| 54 | } |
| 55 | |
| 56 | /// Returns the number of entries created. |
| 57 | pub fn number_of_entries(&self) -> u16 { |
| 58 | self.entries.len() as u16 // limited to MAX_ENTRIES |
| 59 | } |
| 60 | |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 61 | /// Creates a remote file named `basename` with corresponding `inode` at the current directory. |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 62 | pub fn create_file( |
| 63 | &mut self, |
| 64 | basename: &Path, |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 65 | inode: Inode, |
| 66 | ) -> io::Result<VerifiedFileEditor<RemoteFileEditor>> { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 67 | self.validate_argument(basename)?; |
| 68 | |
| 69 | let basename_str = |
| 70 | basename.to_str().ok_or_else(|| io::Error::from_raw_os_error(libc::EINVAL))?; |
| 71 | let new_fd = self |
| 72 | .service |
| 73 | .createFileInDirectory(self.remote_dir_fd, basename_str) |
| 74 | .map_err(into_io_error)?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 75 | |
| 76 | let new_remote_file = |
| 77 | VerifiedFileEditor::new(RemoteFileEditor::new(self.service.clone(), new_fd)); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 78 | self.entries.insert(basename.to_path_buf(), inode); |
| 79 | Ok(new_remote_file) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 82 | /// Creates a remote directory named `basename` with corresponding `inode` at the current |
| 83 | /// directory. |
| 84 | pub fn mkdir(&mut self, basename: &Path, inode: Inode) -> io::Result<RemoteDirEditor> { |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 85 | self.validate_argument(basename)?; |
| 86 | |
| 87 | let basename_str = |
| 88 | basename.to_str().ok_or_else(|| io::Error::from_raw_os_error(libc::EINVAL))?; |
| 89 | let new_fd = self |
| 90 | .service |
| 91 | .createDirectoryInDirectory(self.remote_dir_fd, basename_str) |
| 92 | .map_err(into_io_error)?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 93 | |
| 94 | let new_remote_dir = RemoteDirEditor::new(self.service.clone(), new_fd); |
Victor Hsieh | d5a5b1e | 2021-11-09 11:42:34 -0800 | [diff] [blame^] | 95 | self.entries.insert(basename.to_path_buf(), inode); |
| 96 | Ok(new_remote_dir) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// Returns the inode number of a file or directory named `name` previously created through |
| 100 | /// `RemoteDirEditor`. |
| 101 | pub fn find_inode(&self, name: &Path) -> Option<Inode> { |
| 102 | self.entries.get(name).copied() |
| 103 | } |
| 104 | |
| 105 | fn validate_argument(&self, basename: &Path) -> io::Result<()> { |
| 106 | // Kernel should only give us a basename. |
| 107 | debug_assert!(basename.parent().is_none()); |
| 108 | if self.entries.contains_key(basename) { |
| 109 | Err(io::Error::from_raw_os_error(libc::EEXIST)) |
| 110 | } else if self.entries.len() >= MAX_ENTRIES.into() { |
| 111 | Err(io::Error::from_raw_os_error(libc::EMLINK)) |
| 112 | } else { |
| 113 | Ok(()) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn into_io_error(e: VirtFdServiceStatus) -> io::Error { |
| 119 | let maybe_errno = e.service_specific_error(); |
| 120 | if maybe_errno > 0 { |
| 121 | io::Error::from_raw_os_error(maybe_errno) |
| 122 | } else { |
| 123 | io::Error::new(io::ErrorKind::Other, e.get_description()) |
| 124 | } |
| 125 | } |