F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
SandboxedFile.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/SandboxedFile.cpp
3 // \brief Implementation of directory-sandboxed file wrapper
4 // ======================================================================
5 #include <Fw/Types/Assert.hpp>
7 #include <Os/SandboxedFile.hpp>
8 #include <cstring>
9 
10 namespace Os {
11 
12 // Fail-closed: open() returns OUTSIDE_SANDBOX until configure() is called
13 SandboxedFile::SandboxedFile() : m_file(), m_allowedDirectory(""), m_configured(false) {}
14 
16  if (m_file.isOpen()) {
17  m_file.close();
18  }
19 }
20 
21 void SandboxedFile::configure(const char* allowedDirectory) {
22  FW_ASSERT(allowedDirectory != nullptr);
23  FW_ASSERT(!m_file.isOpen());
24 
25  // Resolve the allowed directory (relative paths resolve against CWD)
26  char resolved[FilePathUtils::MAX_PATH_LENGTH];
27  const FilePathUtils::Status resolveStatus =
28  FilePathUtils::resolveFromCwd(allowedDirectory, resolved, sizeof(resolved));
29  FW_ASSERT(resolveStatus == FilePathUtils::VALID);
30 
31  // Ensure trailing '/'
33  FW_ASSERT(resolvedLen > 0);
34  FW_ASSERT(resolvedLen + 2 <= FilePathUtils::MAX_PATH_LENGTH);
35  if (resolved[resolvedLen - 1] != '/') {
36  resolved[resolvedLen] = '/';
37  resolved[resolvedLen + 1] = '\0';
38  }
39 
40  m_allowedDirectory = resolved;
41  m_configured = true;
42 }
43 
45  return m_configured;
46 }
47 
51  FW_ASSERT(path != nullptr);
52 
53  if (!m_configured) {
55  }
56 
57  // Resolve path against CWD then check containment
58  char resolvedPath[FilePathUtils::MAX_PATH_LENGTH];
59  const FilePathUtils::Status resolveStatus = FilePathUtils::resolveFromCwd(path, resolvedPath, sizeof(resolvedPath));
60  if (resolveStatus != FilePathUtils::VALID) {
62  }
63 
64  if (FilePathUtils::checkContainment(resolvedPath, m_allowedDirectory.toChar()) != FilePathUtils::VALID) {
66  }
67 
68  return m_file.open(resolvedPath, mode, overwrite);
69 }
70 
72  m_file.close();
73 }
74 
75 bool SandboxedFile::isOpen() const {
76  return m_file.isOpen();
77 }
78 
80  return m_file.size(size_result);
81 }
82 
84  return m_file.position(position_result);
85 }
86 
88  return m_file.preallocate(offset, length);
89 }
90 
92  return m_file.seek(offset, seekType);
93 }
94 
96  return m_file.flush();
97 }
98 
100  return m_file.read(buffer, size, wait);
101 }
102 
104  return m_file.read(buffer, size);
105 }
106 
108  return m_file.write(buffer, size, wait);
109 }
110 
112  return m_file.write(buffer, size);
113 }
114 
116  return m_file.calculateCrc(crc);
117 }
118 
120  return m_allowedDirectory.toChar();
121 }
122 
123 } // namespace Os
void configure(const char *allowedDirectory)
Configure the allowed sandbox directory.
Resolved path falls outside the allowed directory.
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:237
Os::FileInterface::Status flush()
Flush file contents to storage.
PlatformSizeType FwSizeType
bool isOpen() const
Check if the file is open.
Status preallocate(FwSizeType offset, FwSizeType length) override
pre-allocate file storage
Definition: File.cpp:130
Os::FileInterface::Status calculateCrc(U32 &crc)
Calculate CRC32 of the entire file.
Os::FileInterface::Status position(FwSizeType &position_result)
Get file pointer position.
Os::FileInterface::Status seek(FwSignedSizeType offset, Os::FileInterface::SeekType seekType)
Seek file pointer.
Status position(FwSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:120
Status size(FwSizeType &size_result) override
get size of currently open file
Definition: File.cpp:111
~SandboxedFile()
Destroy the SandboxedFile (closes the underlying file if open)
Status checkContainment(const char *resolvedPath, const char *allowedDirectory)
Check whether an already-resolved path is within an allowed directory.
PlatformSignedSizeType FwSignedSizeType
SandboxedFile()
Construct an unconfigured SandboxedFile.
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:50
const char * getSandboxDirectory() const
Get the configured sandbox directory.
Os::FileInterface::Status open(const char *path, Os::FileInterface::Mode mode, Os::FileInterface::OverwriteType overwrite=Os::FileInterface::NO_OVERWRITE)
Open a file, validating the path against the sandbox directory.
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:142
static constexpr FwSizeType MAX_PATH_LENGTH
Maximum supported path length for resolution buffers.
bool isConfigured() const
Check if a sandbox directory has been configured.
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:97
Status write(const U8 *buffer, FwSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:213
Os::FileInterface::Status size(FwSizeType &size_result)
Get size of currently open file.
const char * toChar() const
Convert to a C-style char*.
Os::FileInterface::Status write(const U8 *buffer, FwSizeType &size, Os::FileInterface::WaitType wait)
Write data to the file.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
Status read(U8 *buffer, FwSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:194
Status flush() override
flush file contents to storage
Definition: File.cpp:182
Path is valid and within the allowed directory.
Os::FileInterface::Status preallocate(FwSizeType offset, FwSizeType length)
Pre-allocate file storage.
Status resolveFromCwd(const char *path, char *resolvedOut, FwSizeType resolvedSize)
Resolve a path using CWD as the base for relative paths.
Os::FileInterface::Status read(U8 *buffer, FwSizeType &size, Os::FileInterface::WaitType wait)
Read data from the file.
void close()
Close the file.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
bool isOpen() const
determine if the file is open
Definition: File.cpp:105
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:32