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 SandboxedFile::SandboxedFile() : m_file(), m_allowedDirectory("/"), m_configured(true) {}
13 
15  if (m_file.isOpen()) {
16  m_file.close();
17  }
18 }
19 
20 void SandboxedFile::configure(const char* allowedDirectory) {
21  FW_ASSERT(allowedDirectory != nullptr);
22  FW_ASSERT(!m_file.isOpen());
23 
24  // Resolve the allowed directory (relative paths resolve against CWD)
25  char resolved[FilePathUtils::MAX_PATH_LENGTH];
26  const FilePathUtils::Status resolveStatus =
27  FilePathUtils::resolveFromCwd(allowedDirectory, resolved, sizeof(resolved));
28  FW_ASSERT(resolveStatus == FilePathUtils::VALID);
29 
30  // Ensure trailing '/'
32  FW_ASSERT(resolvedLen > 0);
33  FW_ASSERT(resolvedLen + 2 <= FilePathUtils::MAX_PATH_LENGTH);
34  if (resolved[resolvedLen - 1] != '/') {
35  resolved[resolvedLen] = '/';
36  resolved[resolvedLen + 1] = '\0';
37  }
38 
39  m_allowedDirectory = resolved;
40  m_configured = true;
41 }
42 
44  return m_configured;
45 }
46 
50  FW_ASSERT(path != nullptr);
51 
52  if (!m_configured) {
54  }
55 
56  // Resolve path against CWD then check containment
57  char resolvedPath[FilePathUtils::MAX_PATH_LENGTH];
58  const FilePathUtils::Status resolveStatus = FilePathUtils::resolveFromCwd(path, resolvedPath, sizeof(resolvedPath));
59  if (resolveStatus != FilePathUtils::VALID) {
61  }
62 
63  if (FilePathUtils::checkContainment(resolvedPath, m_allowedDirectory.toChar()) != FilePathUtils::VALID) {
65  }
66 
67  return m_file.open(resolvedPath, mode, overwrite);
68 }
69 
71  m_file.close();
72 }
73 
74 bool SandboxedFile::isOpen() const {
75  return m_file.isOpen();
76 }
77 
79  return m_file.size(size_result);
80 }
81 
83  return m_file.position(position_result);
84 }
85 
87  return m_file.preallocate(offset, length);
88 }
89 
91  return m_file.seek(offset, seekType);
92 }
93 
95  return m_file.flush();
96 }
97 
99  return m_file.read(buffer, size, wait);
100 }
101 
103  return m_file.read(buffer, size);
104 }
105 
107  return m_file.write(buffer, size, wait);
108 }
109 
111  return m_file.write(buffer, size);
112 }
113 
115  return m_file.calculateCrc(crc);
116 }
117 
119  return m_allowedDirectory.toChar();
120 }
121 
122 } // 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:230
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:123
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:113
Status size(FwSizeType &size_result) override
get size of currently open file
Definition: File.cpp:104
~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 a SandboxedFile with default sandbox of /
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:43
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:135
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:90
Status write(const U8 *buffer, FwSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:206
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:187
Status flush() override
flush file contents to storage
Definition: File.cpp:175
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:98
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:32