[vold] Add argument verification to IncFS methods

+ Get rid of an extra string copy in path validation function

Bug: 152349257
Test: atest vold_tests
Change-Id: I03a8cab0dd6abd7d5c9dcbbc2acb651e818e6cd8
Merged-In: I03a8cab0dd6abd7d5c9dcbbc2acb651e818e6cd8
This commit is contained in:
Yurii Zubrytskyi 2020-03-24 16:23:51 -07:00
parent d05bcc8070
commit 9e7482ece1
6 changed files with 248 additions and 110 deletions

View file

@ -130,6 +130,7 @@ cc_library_static {
"ScryptParameters.cpp", "ScryptParameters.cpp",
"Utils.cpp", "Utils.cpp",
"VoldNativeService.cpp", "VoldNativeService.cpp",
"VoldNativeServiceValidation.cpp",
"VoldUtil.cpp", "VoldUtil.cpp",
"VolumeManager.cpp", "VolumeManager.cpp",
"cryptfs.cpp", "cryptfs.cpp",

View file

@ -26,6 +26,7 @@
#include "MetadataCrypt.h" #include "MetadataCrypt.h"
#include "MoveStorage.h" #include "MoveStorage.h"
#include "Process.h" #include "Process.h"
#include "VoldNativeServiceValidation.h"
#include "VoldUtil.h" #include "VoldUtil.h"
#include "VolumeManager.h" #include "VolumeManager.h"
#include "cryptfs.h" #include "cryptfs.h"
@ -45,6 +46,7 @@
using android::base::StringPrintf; using android::base::StringPrintf;
using std::endl; using std::endl;
using namespace std::literals;
namespace android { namespace android {
namespace vold { namespace vold {
@ -53,14 +55,6 @@ namespace {
constexpr const char* kDump = "android.permission.DUMP"; constexpr const char* kDump = "android.permission.DUMP";
static binder::Status ok() {
return binder::Status::ok();
}
static binder::Status exception(uint32_t code, const std::string& msg) {
return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
}
static binder::Status error(const std::string& msg) { static binder::Status error(const std::string& msg) {
PLOG(ERROR) << msg; PLOG(ERROR) << msg;
return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
@ -82,77 +76,9 @@ static binder::Status translateBool(bool status) {
} }
} }
binder::Status checkPermission(const char* permission) {
pid_t pid;
uid_t uid;
if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
reinterpret_cast<int32_t*>(&uid))) {
return ok();
} else {
return exception(binder::Status::EX_SECURITY,
StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
}
}
binder::Status checkUidOrRoot(uid_t expectedUid) {
uid_t uid = IPCThreadState::self()->getCallingUid();
if (uid == expectedUid || uid == AID_ROOT) {
return ok();
} else {
return exception(binder::Status::EX_SECURITY,
StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
}
}
binder::Status checkArgumentId(const std::string& id) {
if (id.empty()) {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
}
for (const char& c : id) {
if (!std::isalnum(c) && c != ':' && c != ',') {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("ID %s is malformed", id.c_str()));
}
}
return ok();
}
binder::Status checkArgumentPath(const std::string& path) {
if (path.empty()) {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
}
if (path[0] != '/') {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Path %s is relative", path.c_str()));
}
if ((path + '/').find("/../") != std::string::npos) {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Path %s is shady", path.c_str()));
}
for (const char& c : path) {
if (c == '\0' || c == '\n') {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Path %s is malformed", path.c_str()));
}
}
return ok();
}
binder::Status checkArgumentHex(const std::string& hex) {
// Empty hex strings are allowed
for (const char& c : hex) {
if (!std::isxdigit(c) && c != ':' && c != '-') {
return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Hex %s is malformed", hex.c_str()));
}
}
return ok();
}
#define ENFORCE_SYSTEM_OR_ROOT \ #define ENFORCE_SYSTEM_OR_ROOT \
{ \ { \
binder::Status status = checkUidOrRoot(AID_SYSTEM); \ binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
if (!status.isOk()) { \ if (!status.isOk()) { \
return status; \ return status; \
} \ } \
@ -160,7 +86,7 @@ binder::Status checkArgumentHex(const std::string& hex) {
#define CHECK_ARGUMENT_ID(id) \ #define CHECK_ARGUMENT_ID(id) \
{ \ { \
binder::Status status = checkArgumentId((id)); \ binder::Status status = CheckArgumentId((id)); \
if (!status.isOk()) { \ if (!status.isOk()) { \
return status; \ return status; \
} \ } \
@ -168,7 +94,7 @@ binder::Status checkArgumentHex(const std::string& hex) {
#define CHECK_ARGUMENT_PATH(path) \ #define CHECK_ARGUMENT_PATH(path) \
{ \ { \
binder::Status status = checkArgumentPath((path)); \ binder::Status status = CheckArgumentPath((path)); \
if (!status.isOk()) { \ if (!status.isOk()) { \
return status; \ return status; \
} \ } \
@ -176,7 +102,7 @@ binder::Status checkArgumentHex(const std::string& hex) {
#define CHECK_ARGUMENT_HEX(hex) \ #define CHECK_ARGUMENT_HEX(hex) \
{ \ { \
binder::Status status = checkArgumentHex((hex)); \ binder::Status status = CheckArgumentHex((hex)); \
if (!status.isOk()) { \ if (!status.isOk()) { \
return status; \ return status; \
} \ } \
@ -206,7 +132,7 @@ status_t VoldNativeService::start() {
status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) { status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
const binder::Status dump_permission = checkPermission(kDump); const binder::Status dump_permission = CheckPermission(kDump);
if (!dump_permission.isOk()) { if (!dump_permission.isOk()) {
out << dump_permission.toString8() << endl; out << dump_permission.toString8() << endl;
return PERMISSION_DENIED; return PERMISSION_DENIED;
@ -214,7 +140,6 @@ status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
ACQUIRE_LOCK; ACQUIRE_LOCK;
out << "vold is happy!" << endl; out << "vold is happy!" << endl;
out.flush();
return NO_ERROR; return NO_ERROR;
} }
@ -224,7 +149,7 @@ binder::Status VoldNativeService::setListener(
ACQUIRE_LOCK; ACQUIRE_LOCK;
VolumeManager::Instance()->setListener(listener); VolumeManager::Instance()->setListener(listener);
return ok(); return Ok();
} }
binder::Status VoldNativeService::monitor() { binder::Status VoldNativeService::monitor() {
@ -234,7 +159,7 @@ binder::Status VoldNativeService::monitor() {
{ ACQUIRE_LOCK; } { ACQUIRE_LOCK; }
{ ACQUIRE_CRYPT_LOCK; } { ACQUIRE_CRYPT_LOCK; }
return ok(); return Ok();
} }
binder::Status VoldNativeService::reset() { binder::Status VoldNativeService::reset() {
@ -281,12 +206,12 @@ binder::Status VoldNativeService::onUserStopped(int32_t userId) {
binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames, binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
const std::vector<int32_t>& appIds) { const std::vector<int32_t>& appIds) {
return ok(); return Ok();
} }
binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds, binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
const std::vector<std::string>& sandboxIds) { const std::vector<std::string>& sandboxIds) {
return ok(); return Ok();
} }
binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) { binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
@ -398,7 +323,7 @@ static binder::Status pathForVolId(const std::string& volId, std::string* path)
return error("Volume " + volId + " missing path"); return error("Volume " + volId + " missing path");
} }
} }
return ok(); return Ok();
} }
binder::Status VoldNativeService::benchmark( binder::Status VoldNativeService::benchmark(
@ -412,7 +337,7 @@ binder::Status VoldNativeService::benchmark(
if (!status.isOk()) return status; if (!status.isOk()) return status;
std::thread([=]() { android::vold::Benchmark(path, listener); }).detach(); std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::checkEncryption(const std::string& volId) { binder::Status VoldNativeService::checkEncryption(const std::string& volId) {
@ -443,7 +368,7 @@ binder::Status VoldNativeService::moveStorage(
} }
std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach(); std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) { binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
@ -510,7 +435,7 @@ binder::Status VoldNativeService::fstrim(
ACQUIRE_LOCK; ACQUIRE_LOCK;
std::thread([=]() { android::vold::Trim(listener); }).detach(); std::thread([=]() { android::vold::Trim(listener); }).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::runIdleMaint( binder::Status VoldNativeService::runIdleMaint(
@ -519,7 +444,7 @@ binder::Status VoldNativeService::runIdleMaint(
ACQUIRE_LOCK; ACQUIRE_LOCK;
std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach(); std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::abortIdleMaint( binder::Status VoldNativeService::abortIdleMaint(
@ -528,7 +453,7 @@ binder::Status VoldNativeService::abortIdleMaint(
ACQUIRE_LOCK; ACQUIRE_LOCK;
std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach(); std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId, binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
@ -560,7 +485,7 @@ binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId,
} }
*_aidl_return = android::base::unique_fd(fd); *_aidl_return = android::base::unique_fd(fd);
return ok(); return Ok();
} }
binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) { binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
@ -577,7 +502,7 @@ binder::Status VoldNativeService::fdeRestart() {
// Spawn as thread so init can issue commands back to vold without // Spawn as thread so init can issue commands back to vold without
// causing deadlock, usually as a result of prep_data_fs. // causing deadlock, usually as a result of prep_data_fs.
std::thread(&cryptfs_restart).detach(); std::thread(&cryptfs_restart).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) { binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
@ -585,7 +510,7 @@ binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
ACQUIRE_CRYPT_LOCK; ACQUIRE_CRYPT_LOCK;
*_aidl_return = cryptfs_crypto_complete(); *_aidl_return = cryptfs_crypto_complete();
return ok(); return Ok();
} }
static int fdeEnableInternal(int32_t passwordType, const std::string& password, static int fdeEnableInternal(int32_t passwordType, const std::string& password,
@ -625,7 +550,7 @@ binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::str
// Spawn as thread so init can issue commands back to vold without // Spawn as thread so init can issue commands back to vold without
// causing deadlock, usually as a result of prep_data_fs. // causing deadlock, usually as a result of prep_data_fs.
std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach(); std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach();
return ok(); return Ok();
} }
binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType, binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
@ -652,7 +577,7 @@ binder::Status VoldNativeService::fdeGetField(const std::string& key, std::strin
return error(StringPrintf("Failed to read field %s", key.c_str())); return error(StringPrintf("Failed to read field %s", key.c_str()));
} else { } else {
*_aidl_return = buf; *_aidl_return = buf;
return ok(); return Ok();
} }
} }
@ -668,7 +593,7 @@ binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
ACQUIRE_CRYPT_LOCK; ACQUIRE_CRYPT_LOCK;
*_aidl_return = cryptfs_get_password_type(); *_aidl_return = cryptfs_get_password_type();
return ok(); return Ok();
} }
binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) { binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
@ -679,7 +604,7 @@ binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
if (res != nullptr) { if (res != nullptr) {
*_aidl_return = res; *_aidl_return = res;
} }
return ok(); return Ok();
} }
binder::Status VoldNativeService::fdeClearPassword() { binder::Status VoldNativeService::fdeClearPassword() {
@ -687,7 +612,7 @@ binder::Status VoldNativeService::fdeClearPassword() {
ACQUIRE_CRYPT_LOCK; ACQUIRE_CRYPT_LOCK;
cryptfs_clear_password(); cryptfs_clear_password();
return ok(); return Ok();
} }
binder::Status VoldNativeService::fbeEnable() { binder::Status VoldNativeService::fbeEnable() {
@ -706,7 +631,7 @@ binder::Status VoldNativeService::mountDefaultEncrypted() {
// causing deadlock, usually as a result of prep_data_fs. // causing deadlock, usually as a result of prep_data_fs.
std::thread(&cryptfs_mount_default_encrypted).detach(); std::thread(&cryptfs_mount_default_encrypted).detach();
} }
return ok(); return Ok();
} }
binder::Status VoldNativeService::initUser0() { binder::Status VoldNativeService::initUser0() {
@ -721,7 +646,7 @@ binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
ACQUIRE_CRYPT_LOCK; ACQUIRE_CRYPT_LOCK;
*_aidl_return = cryptfs_isConvertibleToFBE() != 0; *_aidl_return = cryptfs_isConvertibleToFBE() != 0;
return ok(); return Ok();
} }
binder::Status VoldNativeService::mountFstab(const std::string& blkDevice, binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
@ -821,13 +746,13 @@ binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::st
binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName, binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
int32_t appId, const std::string& sandboxId, int32_t appId, const std::string& sandboxId,
int32_t userId) { int32_t userId) {
return ok(); return Ok();
} }
binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName, binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
const std::string& sandboxId, const std::string& sandboxId,
int32_t userId) { int32_t userId) {
return ok(); return Ok();
} }
binder::Status VoldNativeService::startCheckpoint(int32_t retry) { binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
@ -842,7 +767,7 @@ binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
ACQUIRE_LOCK; ACQUIRE_LOCK;
*_aidl_return = cp_needsRollback(); *_aidl_return = cp_needsRollback();
return ok(); return Ok();
} }
binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) { binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
@ -850,7 +775,7 @@ binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
ACQUIRE_LOCK; ACQUIRE_LOCK;
*_aidl_return = cp_needsCheckpoint(); *_aidl_return = cp_needsCheckpoint();
return ok(); return Ok();
} }
binder::Status VoldNativeService::commitChanges() { binder::Status VoldNativeService::commitChanges() {
@ -895,7 +820,7 @@ binder::Status VoldNativeService::abortChanges(const std::string& message, bool
ACQUIRE_LOCK; ACQUIRE_LOCK;
cp_abortChanges(message, retry); cp_abortChanges(message, retry);
return ok(); return Ok();
} }
binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) { binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
@ -924,17 +849,23 @@ binder::Status VoldNativeService::resetCheckpoint() {
ACQUIRE_LOCK; ACQUIRE_LOCK;
cp_resetCheckpoint(); cp_resetCheckpoint();
return ok(); return Ok();
} }
binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) { binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
ENFORCE_SYSTEM_OR_ROOT;
*_aidl_return = IncFs_IsEnabled(); *_aidl_return = IncFs_IsEnabled();
return ok(); return Ok();
} }
binder::Status VoldNativeService::mountIncFs( binder::Status VoldNativeService::mountIncFs(
const std::string& backingPath, const std::string& targetDir, int32_t flags, const std::string& backingPath, const std::string& targetDir, int32_t flags,
::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) { ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
ENFORCE_SYSTEM_OR_ROOT;
CHECK_ARGUMENT_PATH(backingPath);
CHECK_ARGUMENT_PATH(targetDir);
auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(), auto result = IncFs_Mount(backingPath.c_str(), targetDir.c_str(),
{.flags = IncFsMountFlags(flags), {.flags = IncFsMountFlags(flags),
.defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS, .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
@ -948,15 +879,22 @@ binder::Status VoldNativeService::mountIncFs(
if (result.logs >= 0) { if (result.logs >= 0) {
_aidl_return->log.emplace(unique_fd(result.logs)); _aidl_return->log.emplace(unique_fd(result.logs));
} }
return ok(); return Ok();
} }
binder::Status VoldNativeService::unmountIncFs(const std::string& dir) { binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
ENFORCE_SYSTEM_OR_ROOT;
CHECK_ARGUMENT_PATH(dir);
return translate(IncFs_Unmount(dir.c_str())); return translate(IncFs_Unmount(dir.c_str()));
} }
binder::Status VoldNativeService::bindMount(const std::string& sourceDir, binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
const std::string& targetDir) { const std::string& targetDir) {
ENFORCE_SYSTEM_OR_ROOT;
CHECK_ARGUMENT_PATH(sourceDir);
CHECK_ARGUMENT_PATH(targetDir);
return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str())); return translate(IncFs_BindMount(sourceDir.c_str(), targetDir.c_str()));
} }

View file

@ -0,0 +1,109 @@
/*
* Copyright (C) 2020 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 "VoldNativeServiceValidation.h"
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <private/android_filesystem_config.h>
#include <cctype>
#include <string_view>
using android::base::StringPrintf;
using namespace std::literals;
namespace android::vold {
binder::Status Ok() {
return binder::Status::ok();
}
binder::Status Exception(uint32_t code, const std::string& msg) {
return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
}
binder::Status CheckPermission(const char* permission) {
pid_t pid;
uid_t uid;
if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
reinterpret_cast<int32_t*>(&uid))) {
return Ok();
} else {
return Exception(binder::Status::EX_SECURITY,
StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
}
}
binder::Status CheckUidOrRoot(uid_t expectedUid) {
uid_t uid = IPCThreadState::self()->getCallingUid();
if (uid == expectedUid || uid == AID_ROOT) {
return Ok();
} else {
return Exception(binder::Status::EX_SECURITY,
StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
}
}
binder::Status CheckArgumentId(const std::string& id) {
if (id.empty()) {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
}
for (const char& c : id) {
if (!std::isalnum(c) && c != ':' && c != ',') {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("ID %s is malformed", id.c_str()));
}
}
return Ok();
}
binder::Status CheckArgumentPath(const std::string& path) {
if (path.empty()) {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
}
if (path[0] != '/') {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Path %s is relative", path.c_str()));
}
if (path.find("/../"sv) != path.npos || android::base::EndsWith(path, "/.."sv)) {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Path %s is shady", path.c_str()));
}
for (const char& c : path) {
if (c == '\0' || c == '\n') {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Path %s is malformed", path.c_str()));
}
}
return Ok();
}
binder::Status CheckArgumentHex(const std::string& hex) {
// Empty hex strings are allowed
for (const char& c : hex) {
if (!std::isxdigit(c) && c != ':' && c != '-') {
return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
StringPrintf("Hex %s is malformed", hex.c_str()));
}
}
return Ok();
}
} // namespace android::vold

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2020 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.
*/
#pragma once
#include <binder/Status.h>
#include <string>
#include <stdint.h>
#include <sys/types.h>
namespace android::vold {
binder::Status Ok();
binder::Status Exception(uint32_t code, const std::string& msg);
binder::Status CheckPermission(const char* permission);
binder::Status CheckUidOrRoot(uid_t expectedUid);
binder::Status CheckArgumentId(const std::string& id);
binder::Status CheckArgumentPath(const std::string& path);
binder::Status CheckArgumentHex(const std::string& hex);
} // namespace android::vold

View file

@ -7,7 +7,9 @@ cc_test {
srcs: [ srcs: [
"Utils_test.cpp", "Utils_test.cpp",
"VoldNativeServiceValidation_test.cpp",
"cryptfs_test.cpp", "cryptfs_test.cpp",
], ],
static_libs: ["libvold"], static_libs: ["libvold"],
shared_libs: ["libbinder"]
} }

View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 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 "../VoldNativeServiceValidation.h"
#include <gtest/gtest.h>
#include <string_view>
using namespace std::literals;
namespace android::vold {
class VoldNativeServiceValidationTest : public testing::Test {};
TEST_F(VoldNativeServiceValidationTest, CheckArgumentPathTest) {
EXPECT_TRUE(CheckArgumentPath("/").isOk());
EXPECT_TRUE(CheckArgumentPath("/1/2").isOk());
EXPECT_TRUE(CheckArgumentPath("/1/2/").isOk());
EXPECT_TRUE(CheckArgumentPath("/1/2/./3").isOk());
EXPECT_TRUE(CheckArgumentPath("/1/2/./3/.").isOk());
EXPECT_TRUE(CheckArgumentPath(
"/very long path with some/ spaces and quite/ uncommon names /in\1 it/")
.isOk());
EXPECT_FALSE(CheckArgumentPath("").isOk());
EXPECT_FALSE(CheckArgumentPath("relative/path").isOk());
EXPECT_FALSE(CheckArgumentPath("../data/..").isOk());
EXPECT_FALSE(CheckArgumentPath("/../data/..").isOk());
EXPECT_FALSE(CheckArgumentPath("/data/../system").isOk());
EXPECT_FALSE(CheckArgumentPath("/data/..trick/../system").isOk());
EXPECT_FALSE(CheckArgumentPath("/data/..").isOk());
EXPECT_FALSE(CheckArgumentPath("/data/././../apex").isOk());
EXPECT_FALSE(CheckArgumentPath(std::string("/data/strange\0one"sv)).isOk());
EXPECT_FALSE(CheckArgumentPath(std::string("/data/strange\ntwo"sv)).isOk());
}
} // namespace android::vold