1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // The sandbox2::util namespace provides various, uncategorized, functions
16 // useful for creating sandboxes.
17
18 #ifndef SANDBOXED_API_SANDBOX2_UTIL_H_
19 #define SANDBOXED_API_SANDBOX2_UTIL_H_
20
21 #include <sys/types.h>
22
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26
27 #include "absl/base/attributes.h"
28 #include "absl/base/macros.h"
29 #include "absl/status/statusor.h"
30
31 namespace sandbox2::util {
32
33 void DumpCoverageData();
34
35 // An char ptr array limited by the terminating nullptr entry (like environ
36 // or argv).
37 class CharPtrArray {
38 public:
39 CharPtrArray(char* const* array);
40 static CharPtrArray FromStringVector(const std::vector<std::string>& vec);
41
array()42 const std::vector<const char*>& array() const { return array_; }
43
data()44 const char* const* data() const { return array_.data(); }
45
46 std::vector<std::string> ToStringVector() const;
47
48 private:
49 CharPtrArray(const std::vector<std::string>& vec);
50
51 const std::string content_;
52 std::vector<const char*> array_;
53 };
54
55 // Converts an array of char* (terminated by a nullptr, like argv, or environ
56 // arrays), to an std::vector<std::string>.
57 ABSL_DEPRECATED("Use CharPtrArray(arr).ToStringVector() instead")
CharPtrArrToVecString(char * const * arr,std::vector<std::string> * vec)58 inline void CharPtrArrToVecString(char* const* arr,
59 std::vector<std::string>* vec) {
60 *vec = sandbox2::util::CharPtrArray(arr).ToStringVector();
61 }
62
63 // Returns the program name (via /proc/self/comm) for a given PID.
64 std::string GetProgName(pid_t pid);
65
66 // Returns the command line (via /proc/self/cmdline) for a given PID. The
67 // argument separators '\0' are converted to spaces.
68 std::string GetCmdLine(pid_t pid);
69
70 // Returns the specified line from /proc/<pid>/status for a given PID. 'value'
71 // is a field name like "Threads" or "Tgid".
72 std::string GetProcStatusLine(int pid, const std::string& value);
73
74 // Invokes a syscall, avoiding on-stack argument promotion, as it might happen
75 // with vararg syscall() function.
76 long Syscall(long sys_no, // NOLINT
77 uintptr_t a1 = 0, uintptr_t a2 = 0, uintptr_t a3 = 0,
78 uintptr_t a4 = 0, uintptr_t a5 = 0, uintptr_t a6 = 0);
79
80 // Fork based on clone() which updates glibc's PID/TID caches - Based on:
81 // https://chromium.googlesource.com/chromium/src/+/9eb564175dbd452196f782da2b28e3e8e79c49a5%5E!/
82 //
83 // Return values as for 'man 2 fork'.
84 pid_t ForkWithFlags(int flags);
85
86 // Creates a new memfd.
87 bool CreateMemFd(int* fd, const char* name = "buffer_file");
88
89 // Executes a the program given by argv and the specified environment and
90 // captures any output to stdout/stderr.
91 absl::StatusOr<int> Communicate(const std::vector<std::string>& argv,
92 const std::vector<std::string>& envv,
93 std::string* output);
94
95 // Returns signal description.
96 std::string GetSignalName(int signo);
97
98 // Returns rlimit resource name
99 std::string GetRlimitName(int resource);
100
101 // Returns ptrace event name
102 std::string GetPtraceEventName(int event);
103
104 // Reads a path string (NUL-terminated, shorter than PATH_MAX) from another
105 // process memory
106 absl::StatusOr<std::string> ReadCPathFromPid(pid_t pid, uintptr_t ptr);
107
108 // Wrapper for execveat(2).
109 int Execveat(int dirfd, const char* pathname, const char* const argv[],
110 const char* const envp[], int flags, uintptr_t extra_arg = 0);
111
112 } // namespace sandbox2::util
113
114 #endif // SANDBOXED_API_SANDBOX2_UTIL_H_
115