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>
6 #include <Os/SandboxedFile.hpp>
7 #include <cstring>
8 
9 namespace Os {
10 
11 // Fail-closed: open() returns OUTSIDE_SANDBOX until configure() is called
12 SandboxedFile::SandboxedFile() : m_file(), m_allowedDirectory(""), m_configured(false) {}
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) into the
25  // canonical trailing-'/' form that checkContainment requires
26  char resolved[FilePathUtils::MAX_PATH_LENGTH];
27  const FilePathUtils::Status resolveStatus =
28  FilePathUtils::resolveDirectoryFromCwd(allowedDirectory, resolved, sizeof(resolved));
29  FW_ASSERT(resolveStatus == FilePathUtils::VALID, static_cast<FwAssertArgType>(resolveStatus));
30 
31  m_allowedDirectory = resolved;
32  m_configured = true;
33 }
34 
36  return m_configured;
37 }
38 
42  FW_ASSERT(path != nullptr);
43 
44  if (!m_configured) {
46  }
47 
48  // Resolve path against CWD then check containment
49  char resolvedPath[FilePathUtils::MAX_PATH_LENGTH];
50  const FilePathUtils::Status resolveStatus = FilePathUtils::resolveFromCwd(path, resolvedPath, sizeof(resolvedPath));
51  if (resolveStatus != FilePathUtils::VALID) {
53  }
54 
55  if (FilePathUtils::checkContainment(resolvedPath, m_allowedDirectory.toChar()) != FilePathUtils::VALID) {
57  }
58 
59  return m_file.open(resolvedPath, mode, overwrite);
60 }
61 
63  m_file.close();
64 }
65 
66 bool SandboxedFile::isOpen() const {
67  return m_file.isOpen();
68 }
69 
71  return m_file.size(size_result);
72 }
73 
75  return m_file.position(position_result);
76 }
77 
79  return m_file.preallocate(offset, length);
80 }
81 
83  return m_file.seek(offset, seekType);
84 }
85 
87  return m_file.flush();
88 }
89 
91  return m_file.read(buffer, size, wait);
92 }
93 
95  return m_file.read(buffer, size);
96 }
97 
99  return m_file.write(buffer, size, wait);
100 }
101 
103  return m_file.write(buffer, size);
104 }
105 
107  return m_file.calculateCrc(crc);
108 }
109 
111  return m_allowedDirectory.toChar();
112 }
113 
114 } // 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.
Status resolveDirectoryFromCwd(const char *path, char *resolvedOut, FwSizeType resolvedSize)
Resolve a directory path from CWD into the form checkContainment expects.
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