File: seatbelt.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (101 lines) | stat: -rw-r--r-- 3,423 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 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_MAC_SEATBELT_H_
#define SANDBOX_MAC_SEATBELT_H_

#include <cstdint>
#include <string>
#include <vector>

#include "sandbox/mac/seatbelt_export.h"

extern "C" {
struct sandbox_params_t;
}

namespace sandbox {

// This class exists because OS X deprecated the sandbox functions,
// and did not supply replacements that are suitable for Chrome.
// This class wraps the functions in deprecation warning supressions.
class SEATBELT_EXPORT Seatbelt {
 public:
  // Parameters stores policy key/value pairs that can be used for policy
  // compilation, independent of sandbox application.
  class Parameters {
   public:
    // Creates a valid parameter object.
    static Parameters Create();

    // Creates an null parameter object. Calling Set() on this object is
    // undefined.
    Parameters();

    Parameters(Parameters&&);
    Parameters& operator=(Parameters&&);

    Parameters(const Parameters&) = delete;
    Parameters& operator=(const Parameters&) = delete;

    ~Parameters();

    // Sets a key/value pair. Duplicate keys are not permitted. Both strings
    // must outlive this object.
    bool Set(const char* key, const char* value);

    sandbox_params_t* params() const { return params_; }

   private:
    sandbox_params_t* params_ = nullptr;
  };

  // Initializes the specified sandbox profile. Returns true on success with
  // the sandbox applied; otherwise, returns false and outputs the error in
  // `error`.
  static bool Init(const char* profile, uint64_t flags, std::string* error);

  // Initializes the specified sandbox profile and passes the parameters to the
  // `profile`. `parameters` is a null terminated list containing key,value
  // pairs in sequence. [key1,val1,key2,val2,nullptr]. Returns true on success
  // with the sandbox applied; otherwise, returns false and outputs the
  // error in `error`.
  static bool InitWithParams(const std::string& profile,
                             uint64_t flags,
                             const std::vector<std::string>& parameters,
                             std::string* error);

  // Compiles a profile string, with optional parameters, into binary
  // representation. Returns true on success with the result of compilation
  // stored in `compiled_profile`. On error, returns false with a message
  // stored in the optional `error` parameter.
  static bool Compile(const char* profile,
                      const Parameters& params,
                      std::string& compiled_profile,
                      std::string* error);

  // Applies a compiled binary sandbox profile to the current process. Returns
  // true on success; on failure, returns false with a message stored in
  // the optional `error` parameter.
  static bool ApplyCompiledProfile(const std::string& profile,
                                   std::string* error);

  // Frees an error buffer allocated from libsandbox.dylib routines.
  static void FreeError(char* errorbuf);

  // Returns whether or not the process is currently sandboxed.
  static bool IsSandboxed();

  static const char* kProfilePureComputation;

  Seatbelt(const Seatbelt& other) = delete;
  Seatbelt& operator=(const Seatbelt& other) = delete;

 private:
  Seatbelt();
};

}  // sandbox

#endif  // SANDBOX_MAC_SEATBELT_H_