Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Filesystem: ROMFS: fix open race conditions #29201

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ int AP_Filesystem_ROMFS::open(const char *fname, int flags, bool allow_absolute_
errno = EROFS;
return -1;
}

WITH_SEMAPHORE(record_sem); // search for free file record
uint8_t idx;
for (idx=0; idx<max_open_file; idx++) {
if (file[idx].data == nullptr) {
Expand All @@ -42,10 +44,6 @@ int AP_Filesystem_ROMFS::open(const char *fname, int flags, bool allow_absolute_
errno = ENFILE;
return -1;
}
if (file[idx].data != nullptr) {
errno = EBUSY;
return -1;
}
tpwrules marked this conversation as resolved.
Show resolved Hide resolved
file[idx].data = AP_ROMFS::find_decompress(fname, file[idx].size);
if (file[idx].data == nullptr) {
errno = ENOENT;
Expand All @@ -61,6 +59,8 @@ int AP_Filesystem_ROMFS::close(int fd)
errno = EBADF;
return -1;
}

WITH_SEMAPHORE(record_sem); // release file record
tpwrules marked this conversation as resolved.
Show resolved Hide resolved
AP_ROMFS::free(file[fd].data);
file[fd].data = nullptr;
return 0;
Expand Down Expand Up @@ -142,6 +142,7 @@ int AP_Filesystem_ROMFS::mkdir(const char *pathname)

void *AP_Filesystem_ROMFS::opendir(const char *pathname)
{
WITH_SEMAPHORE(record_sem); // search for free directory record
uint8_t idx;
for (idx=0; idx<max_open_dir; idx++) {
if (dir[idx].path == nullptr) {
Expand Down Expand Up @@ -216,6 +217,8 @@ int AP_Filesystem_ROMFS::closedir(void *dirp)
errno = EBADF;
return -1;
}

WITH_SEMAPHORE(record_sem); // release directory record
tpwrules marked this conversation as resolved.
Show resolved Hide resolved
free(dir[idx].path);
dir[idx].path = nullptr;
return 0;
Expand Down
5 changes: 5 additions & 0 deletions libraries/AP_Filesystem/AP_Filesystem_ROMFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#if AP_FILESYSTEM_ROMFS_ENABLED

#include <AP_HAL/Semaphores.h>

#include "AP_Filesystem_backend.h"

class AP_Filesystem_ROMFS : public AP_Filesystem_Backend
Expand Down Expand Up @@ -56,6 +58,9 @@ class AP_Filesystem_ROMFS : public AP_Filesystem_Backend
void unload_file(FileData *fd) override;

private:
// protect searching for free file/dir records when opening/closing
HAL_Semaphore record_sem;

// only allow up to 4 files at a time
static constexpr uint8_t max_open_file = 4;
static constexpr uint8_t max_open_dir = 4;
Expand Down
Loading