2020-10-01 08:15:33 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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
|
2020-10-01 08:15:33 +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.
|
|
|
|
|
2020-10-02 18:56:14 +08:00
|
|
|
#ifndef SAPI_LIBARCHIVE_EXAMPLES_SANDBOX_H
|
|
|
|
#define SAPI_LIBARCHIVE_EXAMPLES_SANDBOX_H
|
2020-09-10 23:22:22 +08:00
|
|
|
|
2020-09-21 21:30:43 +08:00
|
|
|
#include <asm/unistd_64.h>
|
2020-10-02 23:52:29 +08:00
|
|
|
#include <linux/fs.h>
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
#include "libarchive_sapi.sapi.h" // NOLINT(build/include)
|
2020-10-02 23:52:29 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util/bpf_helper.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2020-09-10 23:22:22 +08:00
|
|
|
|
2020-09-28 23:00:33 +08:00
|
|
|
// When creating an archive, we need read permissions on each of the
|
|
|
|
// file/directory added in the archive. Also, in order to create the archive, we
|
2020-10-01 08:15:33 +08:00
|
|
|
// map "/output" with the basename of the archive. This way, the program can
|
2020-09-28 23:00:33 +08:00
|
|
|
// create the file without having access to anything else.
|
2020-09-10 23:22:22 +08:00
|
|
|
class SapiLibarchiveSandboxCreate : public LibarchiveSandbox {
|
|
|
|
public:
|
2020-10-07 18:07:13 +08:00
|
|
|
SapiLibarchiveSandboxCreate(const std::vector<std::string>& files,
|
|
|
|
absl::string_view archive_path)
|
2020-09-21 21:30:43 +08:00
|
|
|
: files_(files), archive_path_(archive_path) {}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-09-10 23:22:22 +08:00
|
|
|
private:
|
2020-09-14 22:39:16 +08:00
|
|
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
2020-09-23 02:30:07 +08:00
|
|
|
sandbox2::PolicyBuilder*) override {
|
2020-09-21 21:30:43 +08:00
|
|
|
sandbox2::PolicyBuilder policy =
|
|
|
|
sandbox2::PolicyBuilder()
|
|
|
|
.AddDirectoryAt(archive_path_, "/output", false)
|
|
|
|
.AllowRead()
|
|
|
|
.AllowWrite()
|
|
|
|
.AllowOpen()
|
|
|
|
.AllowSystemMalloc()
|
|
|
|
.AllowGetIDs()
|
|
|
|
.AllowSafeFcntl()
|
|
|
|
.AllowStat()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowSyscalls({
|
|
|
|
__NR_futex,
|
|
|
|
__NR_lseek,
|
|
|
|
__NR_close,
|
|
|
|
__NR_gettid,
|
|
|
|
__NR_umask,
|
|
|
|
__NR_utimensat,
|
|
|
|
__NR_unlink,
|
|
|
|
__NR_mkdir,
|
|
|
|
__NR_fstatfs,
|
|
|
|
__NR_socket,
|
|
|
|
__NR_connect,
|
|
|
|
__NR_flistxattr,
|
|
|
|
__NR_recvmsg,
|
|
|
|
__NR_getdents64,
|
2020-10-02 23:52:29 +08:00
|
|
|
})
|
|
|
|
// Allow ioctl only for FS_IOC_GETFLAGS.
|
|
|
|
.AddPolicyOnSyscall(__NR_ioctl,
|
|
|
|
{ARG(1), JEQ(FS_IOC_GETFLAGS, ALLOW)});
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// We check whether the entry is a file or a directory.
|
2020-09-23 02:30:07 +08:00
|
|
|
for (const auto& i : files_) {
|
2020-09-21 23:11:10 +08:00
|
|
|
struct stat s;
|
2020-10-02 18:56:14 +08:00
|
|
|
CHECK(stat(i.c_str(), &s) == 0) << "Could not stat " << i;
|
2020-09-21 23:11:10 +08:00
|
|
|
if (S_ISDIR(s.st_mode)) {
|
|
|
|
policy = policy.AddDirectory(i);
|
|
|
|
} else {
|
|
|
|
policy = policy.AddFile(i);
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return policy.BuildOrDie();
|
2020-09-10 23:22:22 +08:00
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
|
|
|
const std::vector<std::string> files_;
|
|
|
|
absl::string_view archive_path_;
|
2020-09-14 22:39:16 +08:00
|
|
|
};
|
2020-09-10 23:22:22 +08:00
|
|
|
|
2020-09-25 23:46:06 +08:00
|
|
|
// When an archive is extracted, the generated files/directories will be placed
|
2020-09-28 23:00:33 +08:00
|
|
|
// relative to the current working directory. In order to add permissions to
|
|
|
|
// this we create a temporary directory at every extraction. Then, we change the
|
|
|
|
// directory of the sandboxed process to that directory and map it to the
|
|
|
|
// current "real" working directory. This way the contents of the archived will
|
|
|
|
// pe placed correctly without offering additional permission.
|
2020-09-10 23:22:22 +08:00
|
|
|
class SapiLibarchiveSandboxExtract : public LibarchiveSandbox {
|
|
|
|
public:
|
2020-10-07 18:07:13 +08:00
|
|
|
SapiLibarchiveSandboxExtract(absl::string_view archive_path, int do_extract,
|
|
|
|
absl::string_view tmp_dir)
|
2020-09-21 21:30:43 +08:00
|
|
|
: archive_path_(archive_path),
|
|
|
|
do_extract_(do_extract),
|
|
|
|
tmp_dir_(tmp_dir) {}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-09-10 23:22:22 +08:00
|
|
|
private:
|
2020-10-08 17:06:50 +08:00
|
|
|
void ModifyExecutor(sandbox2::Executor* executor) override {
|
2020-09-25 23:46:06 +08:00
|
|
|
// If the user only wants to list the entries in the archive, we do
|
|
|
|
// not need to worry about changing directories;
|
2020-09-14 22:39:16 +08:00
|
|
|
if (do_extract_) {
|
2020-10-02 18:56:14 +08:00
|
|
|
executor->set_cwd(std::string(tmp_dir_));
|
2020-09-10 23:22:22 +08:00
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
2020-09-23 02:30:07 +08:00
|
|
|
sandbox2::PolicyBuilder*) override {
|
2020-09-21 21:30:43 +08:00
|
|
|
sandbox2::PolicyBuilder policy = sandbox2::PolicyBuilder()
|
|
|
|
.AllowRead()
|
|
|
|
.AllowWrite()
|
|
|
|
.AllowOpen()
|
|
|
|
.AllowSystemMalloc()
|
|
|
|
.AllowGetIDs()
|
|
|
|
.AllowSafeFcntl()
|
|
|
|
.AllowStat()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowSyscalls({
|
|
|
|
__NR_futex,
|
|
|
|
__NR_lseek,
|
|
|
|
__NR_close,
|
|
|
|
__NR_gettid,
|
|
|
|
__NR_umask,
|
|
|
|
__NR_utimensat,
|
|
|
|
__NR_unlink,
|
|
|
|
__NR_mkdir,
|
|
|
|
})
|
|
|
|
.AddFile(archive_path_);
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-09-16 23:57:31 +08:00
|
|
|
if (do_extract_) {
|
2020-09-30 03:27:40 +08:00
|
|
|
// Get the real cwd and map it to the temporary directory in which
|
|
|
|
// the sandboxed process takes place().
|
2020-09-21 21:30:43 +08:00
|
|
|
std::string cwd = sandbox2::file_util::fileops::GetCWD();
|
|
|
|
policy = policy.AddDirectoryAt(cwd, tmp_dir_, false);
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
return policy.BuildOrDie();
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:30:43 +08:00
|
|
|
absl::string_view archive_path_;
|
|
|
|
absl::string_view tmp_dir_;
|
2020-09-14 22:39:16 +08:00
|
|
|
const int do_extract_;
|
|
|
|
};
|
2020-09-10 23:22:22 +08:00
|
|
|
|
2020-10-02 18:56:14 +08:00
|
|
|
#endif // SAPI_LIBARCHIVE_EXAMPLES_SANDBOX_H
|