2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// The sandbox2::util namespace provides various, uncategorized, functions
|
|
|
|
// useful for creating sandboxes.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_UTIL_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_UTIL_H_
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-12-20 21:07:40 +08:00
|
|
|
#include "absl/base/attributes.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/macros.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace sandbox2::util {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Converts an array of char* (terminated by a nullptr, like argv, or environ
|
2019-03-26 22:54:02 +08:00
|
|
|
// arrays), to an std::vector<std::string>.
|
2021-12-20 21:07:40 +08:00
|
|
|
ABSL_DEPRECATED("Use CharPtrArray(arr).ToStringVector() instead")
|
2019-03-19 00:21:48 +08:00
|
|
|
void CharPtrArrToVecString(char* const* arr, std::vector<std::string>* vec);
|
|
|
|
|
|
|
|
// Converts a vector of strings to a newly allocated array. The array is limited
|
|
|
|
// by the terminating nullptr entry (like environ or argv). It must be freed by
|
|
|
|
// the caller.
|
2021-12-20 21:07:40 +08:00
|
|
|
ABSL_DEPRECATED("Use CharPtrArray class instead")
|
2019-03-19 00:21:48 +08:00
|
|
|
const char** VecStringToCharPtrArr(const std::vector<std::string>& vec);
|
|
|
|
|
2021-12-20 21:07:40 +08:00
|
|
|
// An char ptr array limited by the terminating nullptr entry (like environ
|
|
|
|
// or argv).
|
|
|
|
class CharPtrArray {
|
|
|
|
public:
|
|
|
|
CharPtrArray(char* const* array);
|
|
|
|
static CharPtrArray FromStringVector(const std::vector<std::string>& vec);
|
|
|
|
|
|
|
|
const std::vector<const char*>& array() const { return array_; }
|
|
|
|
|
|
|
|
const char* const* data() const { return array_.data(); }
|
|
|
|
|
|
|
|
std::vector<std::string> ToStringVector() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
CharPtrArray(const std::vector<std::string>& vec);
|
|
|
|
|
|
|
|
const std::string content_;
|
|
|
|
std::vector<const char*> array_;
|
|
|
|
};
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Returns the program name (via /proc/self/comm) for a given PID.
|
|
|
|
std::string GetProgName(pid_t pid);
|
|
|
|
|
2021-04-17 03:42:41 +08:00
|
|
|
// Returns the command line (via /proc/self/cmdline) for a given PID. The
|
|
|
|
// argument separators '\0' are converted to spaces.
|
|
|
|
std::string GetCmdLine(pid_t pid);
|
|
|
|
|
|
|
|
// Returns the specified line from /proc/<pid>/status for a given PID. 'value'
|
|
|
|
// is a field name like "Threads" or "Tgid".
|
|
|
|
std::string GetProcStatusLine(int pid, const std::string& value);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Invokes a syscall, avoiding on-stack argument promotion, as it might happen
|
|
|
|
// with vararg syscall() function.
|
|
|
|
long Syscall(long sys_no, // NOLINT
|
|
|
|
uintptr_t a1 = 0, uintptr_t a2 = 0, uintptr_t a3 = 0,
|
|
|
|
uintptr_t a4 = 0, uintptr_t a5 = 0, uintptr_t a6 = 0);
|
|
|
|
|
|
|
|
// Fork based on clone() which updates glibc's PID/TID caches - Based on:
|
|
|
|
// https://chromium.googlesource.com/chromium/src/+/9eb564175dbd452196f782da2b28e3e8e79c49a5%5E!/
|
|
|
|
//
|
|
|
|
// Return values as for 'man 2 fork'.
|
|
|
|
pid_t ForkWithFlags(int flags);
|
|
|
|
|
|
|
|
// Creates a new memfd.
|
|
|
|
bool CreateMemFd(int* fd, const char* name = "buffer_file");
|
|
|
|
|
|
|
|
// Executes a the program given by argv and the specified environment and
|
|
|
|
// captures any output to stdout/stderr.
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<int> Communicate(const std::vector<std::string>& argv,
|
2020-08-28 19:49:15 +08:00
|
|
|
const std::vector<std::string>& envv,
|
|
|
|
std::string* output);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Returns signal description.
|
|
|
|
std::string GetSignalName(int signo);
|
|
|
|
|
2019-05-17 16:55:13 +08:00
|
|
|
// Returns rlimit resource name
|
|
|
|
std::string GetRlimitName(int resource);
|
|
|
|
|
2021-04-16 03:01:42 +08:00
|
|
|
// Returns ptrace event name
|
|
|
|
std::string GetPtraceEventName(int event);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Reads a path string (NUL-terminated, shorter than PATH_MAX) from another
|
|
|
|
// process memory
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> ReadCPathFromPid(pid_t pid, uintptr_t ptr);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
} // namespace sandbox2::util
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_UTIL_H_
|