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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/var_int.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/rpcchannel.h"
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
Fd::~Fd() {
|
|
|
|
if (GetFreeRPCChannel() && GetRemoteFd() >= 0 && own_remote_) {
|
|
|
|
this->CloseRemoteFd(GetFreeRPCChannel()).IgnoreError();
|
|
|
|
}
|
|
|
|
if (GetValue() >= 0 && own_local_) {
|
|
|
|
CloseLocalFd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Fd::CloseRemoteFd(RPCChannel* rpc_channel) {
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(rpc_channel->Close(GetRemoteFd()));
|
|
|
|
|
|
|
|
SetRemoteFd(-1);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fd::CloseLocalFd() {
|
|
|
|
if (GetValue() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (close(GetValue()) != 0) {
|
|
|
|
PLOG(WARNING) << "close(" << GetValue() << ") failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
SetValue(-1);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Fd::TransferToSandboxee(RPCChannel* rpc_channel, pid_t /* pid */) {
|
2019-03-19 00:21:48 +08:00
|
|
|
int remote_fd;
|
|
|
|
|
|
|
|
SetFreeRPCChannel(rpc_channel);
|
|
|
|
OwnRemoteFd(true);
|
|
|
|
|
|
|
|
if (GetValue() < 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Cannot transfer FD: Local FD not valid");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetRemoteFd() >= 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Cannot transfer FD: Sandboxee already has a valid FD");
|
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_RETURN_IF_ERROR(rpc_channel->SendFD(GetValue(), &remote_fd));
|
|
|
|
SetRemoteFd(remote_fd);
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Fd::TransferFromSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t /* pid */) {
|
|
|
|
int local_fd;
|
|
|
|
|
|
|
|
SetFreeRPCChannel(rpc_channel);
|
|
|
|
OwnRemoteFd(false);
|
|
|
|
|
|
|
|
if (GetValue()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Cannot transfer FD back: Our FD is already valid");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetRemoteFd() < 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Cannot transfer FD back: Sandboxee has no valid FD");
|
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_RETURN_IF_ERROR(rpc_channel->RecvFD(GetRemoteFd(), &local_fd));
|
|
|
|
SetValue(local_fd);
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|