File: broker_file_permission.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (343 lines) | stat: -rw-r--r-- 11,119 bytes parent folder | download | duplicates (7)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "sandbox/linux/syscall_broker/broker_file_permission.h"

#include <fcntl.h>
#include <stddef.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>

#include <ostream>
#include <string>

#include "base/check.h"
#include "base/strings/string_util.h"
#include "sandbox/linux/syscall_broker/broker_command.h"

namespace sandbox {
namespace syscall_broker {

BrokerFilePermission::BrokerFilePermission(BrokerFilePermission&&) = default;
BrokerFilePermission& BrokerFilePermission::operator=(BrokerFilePermission&&) =
    default;
BrokerFilePermission::BrokerFilePermission(const BrokerFilePermission&) =
    default;
BrokerFilePermission& BrokerFilePermission::operator=(
    const BrokerFilePermission&) = default;

BrokerFilePermission::~BrokerFilePermission() = default;

namespace {
bool ContainsParentReference(const char* path, size_t len) {
  // No trailing /..
  if (len >= 3 && path[len - 3] == '/' && path[len - 2] == '.' &&
      path[len - 1] == '.') {
    return true;
  }
  for (size_t i = 0; i < len; i++) {
    if (path[i] == '/' && (len - i) > 3) {
      if (path[i + 1] == '.' && path[i + 2] == '.' && path[i + 3] == '/') {
        return true;
      }
    }
  }
  return false;
}
}  // namespace

bool BrokerFilePermission::ValidatePath(const char* path) {
  if (!path) {
    return false;
  }

  const size_t len = strlen(path);
  // No empty paths
  if (len == 0) {
    return false;
  }
  // Paths must be absolute and not relative
  if (path[0] != '/') {
    return false;
  }
  // No trailing / (but "/" is valid)
  if (len > 1 && path[len - 1] == '/') {
    return false;
  }
  if (ContainsParentReference(path, len)) {
    return false;
  }
  return true;
}

// Async signal safe
// Calls std::string::c_str(), strncmp and strlen. All these
// methods are async signal safe in common standard libs.
// TODO(leecam): remove dependency on std::string
bool BrokerFilePermission::MatchPath(const char* requested_filename) const {
  // Note: This recursive match will allow any path under the allowlisted
  // path, for any number of directory levels. E.g. if the allowlisted
  // path is /good/ then the following will be permitted by the policy.
  //   /good/file1
  //   /good/folder/file2
  //   /good/folder/folder2/file3
  // If an attacker could make 'folder' a symlink to ../../ they would have
  // access to the entire filesystem.
  // Allowlisting with multiple depths is useful, e.g /proc/ but
  // the system needs to ensure symlinks can not be created!
  // That said if an attacker can convert any of the absolute paths
  // to a symlink they can control any file on the system also.
  return recursive() ? base::StartsWith(requested_filename, path_)
                     : requested_filename == path_;
}

const char* BrokerFilePermission::CheckAccess(const char* requested_filename,
                                              int mode) const {
  // First, check if |mode| is existence, ability to read or ability
  // to write. We do not support X_OK.
  if (mode != F_OK && mode & ~(R_OK | W_OK))
    return nullptr;

  if (!ValidatePath(requested_filename))
    return nullptr;

  return CheckAccessInternal(requested_filename, mode);
}

const char* BrokerFilePermission::CheckAccessInternal(
    const char* requested_filename,
    int mode) const {
  if (!MatchPath(requested_filename))
    return nullptr;

  bool allowed = false;
  switch (mode) {
    case F_OK:
      allowed = allow_read() || allow_write();
      break;
    case R_OK:
      allowed = allow_read();
      break;
    case W_OK:
      allowed = allow_write();
      break;
    case R_OK | W_OK:
      allowed = allow_read() && allow_write();
      break;
    default:
      break;
  }
  if (!allowed)
    return nullptr;

  return recursive() ? requested_filename : path_.c_str();
}

std::pair<const char*, bool> BrokerFilePermission::CheckOpen(
    const char* requested_filename,
    int flags) const {
  if (!ValidatePath(requested_filename))
    return {nullptr, false};

  if (!MatchPath(requested_filename))
    return {nullptr, false};

  // First, check the access mode is valid.
  const int access_mode = flags & O_ACCMODE;
  if (access_mode != O_RDONLY && access_mode != O_WRONLY &&
      access_mode != O_RDWR) {
    return {nullptr, false};
  }

  // Check if read is allowed.
  if (!allow_read() && (access_mode == O_RDONLY || access_mode == O_RDWR)) {
    return {nullptr, false};
  }

  // Check if write is allowed.
  if (!allow_write() && (access_mode == O_WRONLY || access_mode == O_RDWR)) {
    return {nullptr, false};
  }

  // Check if file creation is allowed.
  if (!allow_create() && (flags & O_CREAT)) {
    return {nullptr, false};
  }

  // If this file is to be temporary, ensure it is created, not pre-existing.
  // See https://crbug.com/415681#c17
  if (temporary_only() && (!(flags & O_CREAT) || !(flags & O_EXCL))) {
    return {nullptr, false};
  }

  // Some flags affect the behavior of the current process. We don't support
  // them and don't allow them for now.
  if (flags & kCurrentProcessOpenFlagsMask) {
    return {nullptr, false};
  }

  // The effect of (O_RDONLY | O_TRUNC) is undefined, and in some cases it
  // actually truncates, so deny.
  if (access_mode == O_RDONLY && (flags & O_TRUNC) != 0) {
    return {nullptr, false};
  }

  // Now check that all the flags are known to us.
  const int creation_and_status_flags = flags & ~O_ACCMODE;
  const int known_flags = O_APPEND | O_ASYNC | O_CLOEXEC | O_CREAT | O_DIRECT |
                          O_DIRECTORY | O_EXCL | O_LARGEFILE | O_NOATIME |
                          O_NOCTTY | O_NOFOLLOW | O_NONBLOCK | O_NDELAY |
                          O_SYNC | O_TRUNC;

  const int unknown_flags = ~known_flags;
  const bool has_unknown_flags = creation_and_status_flags & unknown_flags;
  if (has_unknown_flags) {
    return {nullptr, false};
  }

  return {recursive() ? requested_filename : path_.c_str(), temporary_only()};
}

const char* BrokerFilePermission::CheckStatWithIntermediates(
    const char* requested_filename) const {
  if (!ValidatePath(requested_filename)) {
    return nullptr;
  }

  // Ability to access implies ability to stat().
  const char* ret = CheckAccessInternal(requested_filename, F_OK);
  if (ret) {
    return ret;
  }

  // Allow stat() on leading directories if have create or stat() permission.
  if (!(allow_create() || allow_stat_with_intermediates())) {
    return nullptr;
  }

  // |allow_stat_with_intermediates()| can match on the full path, and
  // |allow_create()| only matches a leading directory.
  if (!CheckIntermediates(
          requested_filename,
          /*can_match_full_path=*/allow_stat_with_intermediates())) {
    return nullptr;
  }

  return requested_filename;
}

const char* BrokerFilePermission::CheckInotifyAddWatchWithIntermediates(
    const char* requested_filename,
    uint32_t mask) const {
  if (!allow_inotify_add_watch_with_intermediates()) {
    return nullptr;
  }

  if (!ValidatePath(requested_filename)) {
    return nullptr;
  }

  // Allow only this exact mask as it is used by
  // base/files/file_path_watcher_inotify.cc.
  if (mask != (IN_ATTRIB | IN_CREATE | IN_DELETE | IN_CLOSE_WRITE | IN_MOVE |
               IN_ONLYDIR)) {
    return nullptr;
  }

  // If this permission is recursive and a prefix of `requested_filename`
  // matches this permission, allow. Otherwise check intermediates.
  if (!(recursive() && MatchPath(requested_filename)) &&
      !CheckIntermediates(requested_filename,
                          /*can_match_full_path=*/true)) {
    return nullptr;
  }

  return requested_filename;
}

bool BrokerFilePermission::CheckIntermediates(const char* requested_filename,
                                              bool can_match_full_path) const {
  // NOTE: ValidatePath proves requested_length != 0 and |requested_filename| is
  // absolute.
  size_t requested_length = strlen(requested_filename);
  CHECK(requested_length);
  CHECK(requested_filename[0] == '/');

  // Special case for root: only one slash, otherwise must have a second
  // slash in the right spot to avoid substring matches.
  return (requested_length == 1 && requested_filename[0] == '/') ||
         // If this permission can match the full path, compare directly to the
         // requested filename.
         (can_match_full_path && path_ == requested_filename) ||
         // Check whether |requested_filename| matches a leading directory of
         // |path_|.
         (requested_length < path_.length() &&
          memcmp(path_.c_str(), requested_filename, requested_length) == 0 &&
          path_.c_str()[requested_length] == '/');
}

const char* BrokerFilePermission::GetErrorMessageForTests() {
  return "Invalid BrokerFilePermission";
}

void BrokerFilePermission::DieOnInvalidPermission() {
  // Must have enough length for a '/'
  CHECK(path_.length() > 0) << GetErrorMessageForTests();

  // Allowlisted paths must be absolute.
  CHECK(path_[0] == '/') << GetErrorMessageForTests();

  // Don't allow temporary creation without create permission.
  if (temporary_only())
    CHECK(allow_create()) << GetErrorMessageForTests();

  // Recursive paths must have a trailing slash, absolutes must not (except
  // root).
  const char last_char = path_.back();
  if (recursive() || path_.length() == 1)
    CHECK(last_char == '/') << GetErrorMessageForTests();
  else
    CHECK(last_char != '/') << GetErrorMessageForTests();

  CHECK(!ContainsParentReference(path_.c_str(), path_.length()));
}

BrokerFilePermission::BrokerFilePermission(std::string path, uint64_t flags)
    : path_(std::move(path)), flags_(flags) {
  DieOnInvalidPermission();
}

BrokerFilePermission::BrokerFilePermission(
    std::string path,
    RecursionOption recurse_opt,
    PersistenceOption persist_opt,
    ReadPermission read_perm,
    WritePermission write_perm,
    CreatePermission create_perm,
    StatWithIntermediatesPermission stat_perm,
    InotifyAddWatchWithIntermediatesPermission inotify_perm)
    : path_(std::move(path)) {
  flags_[kRecursiveBitPos] = recurse_opt == RecursionOption::kRecursive;
  flags_[kTemporaryOnlyBitPos] =
      persist_opt == PersistenceOption::kTemporaryOnly;
  flags_[kAllowReadBitPos] = read_perm == ReadPermission::kAllowRead;
  flags_[kAllowWriteBitPos] = write_perm == WritePermission::kAllowWrite;
  flags_[kAllowCreateBitPos] = create_perm == CreatePermission::kAllowCreate;
  flags_[kAllowStatWithIntermediatesBitPos] =
      stat_perm == StatWithIntermediatesPermission::kAllowStatWithIntermediates;
  flags_[kAllowInotifyAddWatchWithIntermediates] =
      inotify_perm == InotifyAddWatchWithIntermediatesPermission::
                          kAllowInotifyAddWatchWithIntermediates;

  DieOnInvalidPermission();
}

}  // namespace syscall_broker
}  // namespace sandbox