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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <binder/Binder.h>
#include <binder/Parcel.h>
#include <binder/RpcServer.h>
#include <binder/RpcTlsTestUtils.h>
#include <binder/RpcTransport.h>
#include <binder/RpcTransportRaw.h>
#include <binder/RpcTransportTls.h>
#include <fuzzer/FuzzedDataProvider.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <sys/resource.h>
#include <sys/un.h>
namespace android {
static const std::string kSock = std::string(getenv("TMPDIR") ?: "/tmp") +
"/binderRpcFuzzerSocket_" + std::to_string(getpid());
class SomeBinder : public BBinder {
status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0) {
(void)flags;
if ((code & 1) == 0) {
sp<IBinder> binder;
(void)data.readStrongBinder(&binder);
if (binder != nullptr) {
(void)binder->pingBinder();
}
}
if ((code & 2) == 0) {
(void)data.readInt32();
}
if ((code & 4) == 0) {
(void)reply->writeStrongBinder(sp<BBinder>::make());
}
return OK;
}
};
int passwordCallback(char* buf, int size, int /*rwflag*/, void* /*u*/) {
constexpr const char pass[] = "xxxx"; // See create_certs.sh
if (size <= 0) return 0;
int numCopy = std::min<int>(size, sizeof(pass));
(void)memcpy(buf, pass, numCopy);
return numCopy;
}
struct ServerAuth {
bssl::UniquePtr<EVP_PKEY> pkey;
bssl::UniquePtr<X509> cert;
};
// Use pre-configured keys because runtime generated keys / certificates are not
// deterministic, and the algorithm is time consuming.
ServerAuth readServerKeyAndCert() {
ServerAuth ret;
auto keyPath = android::base::GetExecutableDirectory() + "/data/server.key";
bssl::UniquePtr<BIO> keyBio(BIO_new_file(keyPath.c_str(), "r"));
ret.pkey.reset(PEM_read_bio_PrivateKey(keyBio.get(), nullptr, passwordCallback, nullptr));
CHECK_NE(ret.pkey.get(), nullptr);
auto certPath = android::base::GetExecutableDirectory() + "/data/server.crt";
bssl::UniquePtr<BIO> certBio(BIO_new_file(certPath.c_str(), "r"));
ret.cert.reset(PEM_read_bio_X509(certBio.get(), nullptr, nullptr, nullptr));
CHECK_NE(ret.cert.get(), nullptr);
return ret;
}
std::unique_ptr<RpcAuth> createServerRpcAuth() {
static auto sAuth = readServerKeyAndCert();
CHECK(EVP_PKEY_up_ref(sAuth.pkey.get()));
bssl::UniquePtr<EVP_PKEY> pkey(sAuth.pkey.get());
CHECK(X509_up_ref(sAuth.cert.get()));
bssl::UniquePtr<X509> cert(sAuth.cert.get());
return std::make_unique<RpcAuthPreSigned>(std::move(pkey), std::move(cert));
}
std::unique_ptr<RpcTransportCtxFactory> makeTransportCtxFactory(FuzzedDataProvider* provider) {
bool isTls = provider->ConsumeBool();
if (!isTls) {
return RpcTransportCtxFactoryRaw::make();
}
status_t verifyStatus = provider->ConsumeIntegral<status_t>();
auto verifier = std::make_shared<RpcCertificateVerifierNoOp>(verifyStatus);
return RpcTransportCtxFactoryTls::make(verifier, createServerRpcAuth());
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size > 50000) return 0;
FuzzedDataProvider provider(data, size);
RAND_reset_for_fuzzing();
unlink(kSock.c_str());
sp<RpcServer> server = RpcServer::make(makeTransportCtxFactory(&provider));
server->setRootObject(sp<SomeBinder>::make());
CHECK_EQ(OK, server->setupUnixDomainServer(kSock.c_str()));
std::thread serverThread([=] { (void)server->join(); });
sockaddr_un addr{
.sun_family = AF_UNIX,
};
CHECK_LT(kSock.size(), sizeof(addr.sun_path));
memcpy(&addr.sun_path, kSock.c_str(), kSock.size());
std::vector<base::unique_fd> connections;
bool hangupBeforeShutdown = provider.ConsumeBool();
// b/260736889 - limit arbitrarily, due to thread resource exhaustion, which currently
// aborts. Servers should consider RpcServer::setConnectionFilter instead.
constexpr size_t kMaxConnections = 10;
while (provider.remaining_bytes() > 0) {
if (connections.empty() ||
(connections.size() < kMaxConnections && provider.ConsumeBool())) {
base::unique_fd fd(TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)));
CHECK_NE(fd.get(), -1);
CHECK_EQ(0,
TEMP_FAILURE_RETRY(
connect(fd.get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))))
<< strerror(errno);
connections.push_back(std::move(fd));
} else {
size_t idx = provider.ConsumeIntegralInRange<size_t>(0, connections.size() - 1);
if (provider.ConsumeBool()) {
std::string writeData = provider.ConsumeRandomLengthString();
ssize_t size = TEMP_FAILURE_RETRY(send(connections.at(idx).get(), writeData.data(),
writeData.size(), MSG_NOSIGNAL));
CHECK(errno == EPIPE || size == writeData.size())
<< size << " " << writeData.size() << " " << strerror(errno);
} else {
connections.erase(connections.begin() + idx); // hang up
}
}
}
if (hangupBeforeShutdown) {
connections.clear();
while (!server->listSessions().empty() || server->numUninitializedSessions()) {
// wait for all threads to finish processing existing information
usleep(1);
}
}
while (server->hasActiveRequests()) {
usleep(10);
}
while (!server->shutdown()) usleep(1);
serverThread.join();
return 0;
}
} // namespace android
|