File: broker_command.h

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 (120 lines) | stat: -rw-r--r-- 4,141 bytes parent folder | download | duplicates (9)
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
// 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.

#ifndef SANDBOX_LINUX_SYSCALL_BROKER_BROKER_COMMAND_H_
#define SANDBOX_LINUX_SYSCALL_BROKER_BROKER_COMMAND_H_

#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>

#include <bitset>
#include <initializer_list>
#include <utility>

namespace sandbox {
namespace syscall_broker {

class BrokerPermissionList;

// Some flags are local to the current process and cannot be sent over a Unix
// socket. They need special treatment from the client.
// O_CLOEXEC is tricky because in theory another thread could call execve()
// before special treatment is made on the client, so a client needs to call
// recvmsg(2) with MSG_CMSG_CLOEXEC.
// To make things worse, there are two CLOEXEC related flags, FD_CLOEXEC (see
// F_GETFD in fcntl(2)) and O_CLOEXEC (see F_GETFL in fcntl(2)). O_CLOEXEC
// doesn't affect the semantics on execve(), it's merely a note that the
// descriptor was originally opened with O_CLOEXEC as a flag. And it is sent
// over unix sockets just fine, so a receiver that would (incorrectly) look at
// O_CLOEXEC instead of FD_CLOEXEC may be tricked in thinking that the file
// descriptor will or won't be closed on execve().
constexpr int kCurrentProcessOpenFlagsMask = O_CLOEXEC;

enum BrokerCommand {
  COMMAND_INVALID = 0,
  COMMAND_ACCESS,
  COMMAND_MKDIR,
  COMMAND_OPEN,
  COMMAND_READLINK,
  COMMAND_RENAME,
  COMMAND_RMDIR,
  COMMAND_STAT,
  COMMAND_STAT64,
  COMMAND_UNLINK,
  COMMAND_INOTIFY_ADD_WATCH,

  // NOTE: update when adding new commands.
  COMMAND_MAX = COMMAND_INOTIFY_ADD_WATCH
};

using BrokerCommandSet = std::bitset<COMMAND_MAX + 1>;

// Helper function since std::bitset lacks an initializer list constructor.
inline BrokerCommandSet MakeBrokerCommandSet(
    const std::initializer_list<BrokerCommand>& args) {
  BrokerCommandSet result;
  for (const auto& arg : args)
    result.set(arg);
  return result;
}

// Helper functions to perform the same permissions test on either side
// (client or broker process) of a broker IPC command. The implementations
// must be safe when called from an async signal handler.
// They all return nullptr when permission checks fail.
[[nodiscard]] const char* CommandAccessIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename,
    int requested_mode  // e.g. F_OK, R_OK, W_OK.
);

[[nodiscard]] const char* CommandMkdirIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename);

[[nodiscard]] std::pair<const char*, bool> CommandOpenIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename,
    int requested_flags  // e.g. O_RDONLY, O_RDWR.
);

[[nodiscard]] const char* CommandReadlinkIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename);

[[nodiscard]] std::pair<const char*, const char*> CommandRenameIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* old_filename,
    const char* new_filename);

[[nodiscard]] const char* CommandRmdirIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename);

[[nodiscard]] const char* CommandStatIsSafe(const BrokerCommandSet& command_set,
                                            const BrokerPermissionList& policy,
                                            const char* requested_filename);

[[nodiscard]] const char* CommandUnlinkIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename);

[[nodiscard]] const char* CommandInotifyAddWatchIsSafe(
    const BrokerCommandSet& command_set,
    const BrokerPermissionList& policy,
    const char* requested_filename,
    uint32_t mask);

}  // namespace syscall_broker
}  // namespace sandbox

#endif  // SANDBOX_LINUX_SYSCALL_BROKER_BROKER_COMMAND_H_