File: sqlite_sandboxed_vfs.cc

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (214 lines) | stat: -rw-r--r-- 7,262 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/persistent_cache/sqlite/vfs/sqlite_sandboxed_vfs.h"

#include <mutex>
#include <optional>
#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/check_op.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/synchronization/lock.h"
#include "components/persistent_cache/sqlite/vfs/sqlite_database_vfs_file_set.h"
#include "sql/sandboxed_vfs.h"
#include "sql/sandboxed_vfs_file.h"
#include "third_party/sqlite/sqlite3.h"

namespace persistent_cache {

namespace {

std::once_flag g_register_vfs_once_flag;
SqliteSandboxedVfsDelegate* g_instance = nullptr;

SandboxedFile::FileType GetFileType(int sqlite_requested_type) {
  static constexpr int kTypeMask =
      SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_TRANSIENT_DB |
      SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL |
      SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_SUPER_JOURNAL | SQLITE_OPEN_WAL;

  switch (sqlite_requested_type & kTypeMask) {
    case SQLITE_OPEN_MAIN_DB:
      return SandboxedFile::FileType::kMainDb;
    case SQLITE_OPEN_TEMP_DB:
      return SandboxedFile::FileType::kTempDb;
    case SQLITE_OPEN_TRANSIENT_DB:
      return SandboxedFile::FileType::kTransientDb;
    case SQLITE_OPEN_MAIN_JOURNAL:
      return SandboxedFile::FileType::kMainJournal;
    case SQLITE_OPEN_TEMP_JOURNAL:
      return SandboxedFile::FileType::kTempJournal;
    case SQLITE_OPEN_SUBJOURNAL:
      return SandboxedFile::FileType::kSubjournal;
    case SQLITE_OPEN_SUPER_JOURNAL:
      return SandboxedFile::FileType::kSuperJournal;
    case SQLITE_OPEN_WAL:
      return SandboxedFile::FileType::kWal;
  }
  NOTREACHED();
}

}  // namespace

SqliteSandboxedVfsDelegate::SqliteSandboxedVfsDelegate() {
  CHECK(!g_instance);
  g_instance = this;
}

SqliteSandboxedVfsDelegate::~SqliteSandboxedVfsDelegate() {
  CHECK_EQ(this, g_instance);
  g_instance = nullptr;
}

// static
SqliteSandboxedVfsDelegate* SqliteSandboxedVfsDelegate::GetInstance() {
  // When requesting the the global instance the first time make sure it exists
  // and register it.
  std::call_once(g_register_vfs_once_flag, []() {
    sql::SandboxedVfs::Register(kSqliteVfsName,
                                std::make_unique<SqliteSandboxedVfsDelegate>(),
                                /*make_default=*/false);
  });
  return g_instance;
}

sql::SandboxedVfsFile* SqliteSandboxedVfsDelegate::RetrieveSandboxedVfsFile(
    base::File file,
    base::FilePath file_path,
    sql::SandboxedVfsFileType file_type,
    sql::SandboxedVfs* vfs) {
  base::AutoLock lock(files_map_lock_);
  auto it = sandboxed_files_map_.find(file_path);
  if (it == sandboxed_files_map_.end()) {
    return nullptr;
  }

  it->second->OnFileOpened(std::move(file));

  return it->second;
}

base::File SqliteSandboxedVfsDelegate::OpenFile(const base::FilePath& file_path,
                                                int sqlite_requested_flags) {
  base::AutoLock lock(files_map_lock_);

  // If `file_name` is missing in the mapping there is no file to return.
  auto it = sandboxed_files_map_.find(file_path);
  if (it == sandboxed_files_map_.end()) {
    return base::File();
  }

  auto file_type = GetFileType(sqlite_requested_flags);

  // Only the main database and its rollback journal and/or write-ahead log are
  // supported.
  CHECK(file_type == SandboxedFile::FileType::kMainDb ||
        file_type == SandboxedFile::FileType::kMainJournal ||
        file_type == SandboxedFile::FileType::kWal);

  // If `file_name` is found in the mapping return the associated file.
  return it->second->TakeUnderlyingFile(file_type);
}

int SqliteSandboxedVfsDelegate::DeleteFile(const base::FilePath& file_path,
                                           bool /*sync_dir*/) {
  base::AutoLock lock(files_map_lock_);

  // Sandboxed processes are not capable of deleting files, so deletion is
  // simulated by truncating the file to zero.
  auto it = sandboxed_files_map_.find(file_path);
  if (it != sandboxed_files_map_.end()) {
    auto& file = it->second->GetFile();
    if (!file.SetLength(0)) {
      const base::File::Error file_error = base::File::GetLastFileError();
      base::UmaHistogramExactLinear(
          base::StrCat(
              {"PersistentCache.Sqlite.",
               SqliteVfsFileSet::GetVirtualFileHistogramVariant(file_path),
               ".SetLengthError"}),
          -file_error, -base::File::FILE_ERROR_MAX);
      return SQLITE_IOERR_DELETE;
    }
    return SQLITE_OK;
  }

  return SQLITE_NOTFOUND;
}

std::optional<sql::SandboxedVfs::PathAccessInfo>
SqliteSandboxedVfsDelegate::GetPathAccess(const base::FilePath& file_path) {
  base::AutoLock lock(files_map_lock_);

  // If `file_name` is missing in the mapping there is no access to return.
  auto it = sandboxed_files_map_.find(file_path);
  if (it == sandboxed_files_map_.end()) {
    return std::nullopt;
  }

  // The files will never be received without read access.
  // Write access is conditional on the file being opened for write.
  return sql::SandboxedVfs::PathAccessInfo{
      .can_read = true,
      .can_write = it->second->access_rights() ==
                   SandboxedFile::AccessRights::kReadWrite};
}

SqliteSandboxedVfsDelegate::UnregisterRunner::UnregisterRunner(
    const SqliteVfsFileSet& vfs_file_set)
    : vfs_file_set_(vfs_file_set) {}

SqliteSandboxedVfsDelegate::UnregisterRunner::~UnregisterRunner() {
  SqliteSandboxedVfsDelegate::GetInstance()->UnregisterSandboxedFiles(
      *vfs_file_set_);
}

// static
void SqliteSandboxedVfsDelegate::UnregisterSandboxedFiles(
    const SqliteVfsFileSet& sqlite_vfs_file_set) {
  base::AutoLock lock(files_map_lock_);

  auto num_erased =
      sandboxed_files_map_.erase(sqlite_vfs_file_set.GetDbVirtualFilePath());
  CHECK_EQ(num_erased, 1U);
  num_erased = sandboxed_files_map_.erase(
      sqlite_vfs_file_set.GetJournalVirtualFilePath());
  CHECK_EQ(num_erased, 1U);
  if (sqlite_vfs_file_set.wal_journal_mode()) {
    num_erased = sandboxed_files_map_.erase(
        sqlite_vfs_file_set.GetWalJournalVirtualFilePath());
    CHECK_EQ(num_erased, 1U);
  }
}

// static
SqliteSandboxedVfsDelegate::UnregisterRunner
SqliteSandboxedVfsDelegate::RegisterSandboxedFiles(
    const SqliteVfsFileSet& sqlite_vfs_file_set) {
  base::AutoLock lock(files_map_lock_);

  auto [it, inserted] =
      sandboxed_files_map_.emplace(sqlite_vfs_file_set.GetDbVirtualFilePath(),
                                   sqlite_vfs_file_set.GetSandboxedDbFile());
  CHECK(inserted);
  std::tie(it, inserted) = sandboxed_files_map_.emplace(
      sqlite_vfs_file_set.GetJournalVirtualFilePath(),
      sqlite_vfs_file_set.GetSandboxedJournalFile());
  CHECK(inserted);
  if (sqlite_vfs_file_set.wal_journal_mode()) {
    std::tie(it, inserted) = sandboxed_files_map_.emplace(
        sqlite_vfs_file_set.GetWalJournalVirtualFilePath(),
        sqlite_vfs_file_set.GetSandboxedWalJournalFile());
    CHECK(inserted);
  }

  return UnregisterRunner(sqlite_vfs_file_set);
}

}  // namespace persistent_cache