From b7a9c63ff038a854a50cc3df6d900e27113208b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanislav=20Angelovi=C4=8D?= Date: Fri, 11 Feb 2022 21:53:37 +0100 Subject: [PATCH] refactor: add validity checks for names and paths (#242) --- CMakeLists.txt | 1 + src/Connection.cpp | 3 +++ src/Object.cpp | 9 ++++++++ src/Proxy.cpp | 9 ++++++++ src/Utils.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 src/Utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index be4e836..9732757 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ set(SDBUSCPP_HDR_SRCS ${SDBUSCPP_SOURCE_DIR}/Connection.h ${SDBUSCPP_SOURCE_DIR}/IConnection.h ${SDBUSCPP_SOURCE_DIR}/MessageUtils.h + ${SDBUSCPP_SOURCE_DIR}/Utils.h ${SDBUSCPP_SOURCE_DIR}/Object.h ${SDBUSCPP_SOURCE_DIR}/Proxy.h ${SDBUSCPP_SOURCE_DIR}/ScopeGuard.h diff --git a/src/Connection.cpp b/src/Connection.cpp index 5b55f14..c5488f0 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -27,6 +27,7 @@ #include "Connection.h" #include "SdBus.h" #include "MessageUtils.h" +#include "Utils.h" #include #include #include "ScopeGuard.h" @@ -71,6 +72,8 @@ Connection::~Connection() void Connection::requestName(const std::string& name) { + SDBUS_CHECK_SERVICE_NAME(name); + auto r = iface_->sd_bus_request_name(bus_.get(), name.c_str(), 0); SDBUS_THROW_ERROR_IF(r < 0, "Failed to request bus name", -r); } diff --git a/src/Object.cpp b/src/Object.cpp index f1d5575..bd961d2 100644 --- a/src/Object.cpp +++ b/src/Object.cpp @@ -33,6 +33,7 @@ #include #include "ScopeGuard.h" #include "IConnection.h" +#include "Utils.h" #include "VTableUtils.h" #include #include @@ -43,6 +44,7 @@ namespace sdbus::internal { Object::Object(sdbus::internal::IConnection& connection, std::string objectPath) : connection_(connection), objectPath_(std::move(objectPath)) { + SDBUS_CHECK_OBJECT_PATH(objectPath_); } void Object::registerMethod( const std::string& interfaceName @@ -71,6 +73,8 @@ void Object::registerMethod( const std::string& interfaceName , method_callback methodCallback , Flags flags ) { + SDBUS_CHECK_INTERFACE_NAME(interfaceName); + SDBUS_CHECK_MEMBER_NAME(methodName); SDBUS_THROW_ERROR_IF(!methodCallback, "Invalid method callback provided", EINVAL); auto& interface = getInterface(interfaceName); @@ -98,6 +102,9 @@ void Object::registerSignal( const std::string& interfaceName , const std::vector& paramNames , Flags flags ) { + SDBUS_CHECK_INTERFACE_NAME(interfaceName); + SDBUS_CHECK_MEMBER_NAME(signalName); + auto& interface = getInterface(interfaceName); InterfaceData::SignalData signalData{std::move(signature), paramNamesToString(paramNames), std::move(flags)}; @@ -127,6 +134,8 @@ void Object::registerProperty( const std::string& interfaceName , property_set_callback setCallback , Flags flags ) { + SDBUS_CHECK_INTERFACE_NAME(interfaceName); + SDBUS_CHECK_MEMBER_NAME(propertyName); SDBUS_THROW_ERROR_IF(!getCallback && !setCallback, "Invalid property callbacks provided", EINVAL); auto& interface = getInterface(interfaceName); diff --git a/src/Proxy.cpp b/src/Proxy.cpp index ad26daa..bffe648 100644 --- a/src/Proxy.cpp +++ b/src/Proxy.cpp @@ -27,6 +27,7 @@ #include "Proxy.h" #include "IConnection.h" #include "MessageUtils.h" +#include "Utils.h" #include "sdbus-c++/Message.h" #include "sdbus-c++/IConnection.h" #include "sdbus-c++/Error.h" @@ -43,6 +44,9 @@ Proxy::Proxy(sdbus::internal::IConnection& connection, std::string destination, , destination_(std::move(destination)) , objectPath_(std::move(objectPath)) { + SDBUS_CHECK_SERVICE_NAME(destination_); + SDBUS_CHECK_OBJECT_PATH(objectPath_); + // The connection is not ours only, it is owned and managed by the user and we just reference // it here, so we expect the client to manage the event loop upon this connection themselves. } @@ -54,6 +58,9 @@ Proxy::Proxy( std::unique_ptr&& connection , destination_(std::move(destination)) , objectPath_(std::move(objectPath)) { + SDBUS_CHECK_SERVICE_NAME(destination_); + SDBUS_CHECK_OBJECT_PATH(objectPath_); + // The connection is ours only, i.e. it's us who has to manage the event loop upon this connection, // in order that we get and process signals, async call replies, and other messages from D-Bus. connection_->enterEventLoopAsync(); @@ -156,6 +163,8 @@ void Proxy::registerSignalHandler( const std::string& interfaceName , const std::string& signalName , signal_handler signalHandler ) { + SDBUS_CHECK_INTERFACE_NAME(interfaceName); + SDBUS_CHECK_MEMBER_NAME(signalName); SDBUS_THROW_ERROR_IF(!signalHandler, "Invalid signal handler provided", EINVAL); auto& interface = interfaces_[interfaceName]; diff --git a/src/Utils.h b/src/Utils.h new file mode 100644 index 0000000..7d234b5 --- /dev/null +++ b/src/Utils.h @@ -0,0 +1,53 @@ +/** + * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland + * (C) 2016 - 2022 Stanislav Angelovic + * + * @file Utils.h + * + * Created on: Feb 9, 2022 + * Project: sdbus-c++ + * Description: High-level D-Bus IPC C++ library based on sd-bus + * + * This file is part of sdbus-c++. + * + * sdbus-c++ is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2.1 of the License, or + * (at your option) any later version. + * + * sdbus-c++ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with sdbus-c++. If not, see . + */ + +#ifndef SDBUS_CXX_INTERNAL_UTILS_H_ +#define SDBUS_CXX_INTERNAL_UTILS_H_ + +#include +#include + +#if LIBSYSTEMD_VERSION>=246 +#define SDBUS_CHECK_OBJECT_PATH(_PATH) \ + SDBUS_THROW_ERROR_IF(!sd_bus_object_path_is_valid(_PATH.c_str()), "Invalid object path '" + _PATH + "' provided", EINVAL) \ + /**/ +#define SDBUS_CHECK_INTERFACE_NAME(_NAME) \ + SDBUS_THROW_ERROR_IF(!sd_bus_interface_name_is_valid(_NAME.c_str()), "Invalid interface name '" + _NAME + "' provided", EINVAL) \ + /**/ +#define SDBUS_CHECK_SERVICE_NAME(_NAME) \ + SDBUS_THROW_ERROR_IF(!sd_bus_service_name_is_valid(_NAME.c_str()), "Invalid service name '" + _NAME + "' provided", EINVAL) \ + /**/ +#define SDBUS_CHECK_MEMBER_NAME(_NAME) \ + SDBUS_THROW_ERROR_IF(!sd_bus_member_name_is_valid(_NAME.c_str()), "Invalid member name '" + _NAME + "' provided", EINVAL) \ + /**/ +#else +#define SDBUS_CHECK_OBJECT_PATH(_PATH) +#define SDBUS_CHECK_INTERFACE_NAME(_NAME) +#define SDBUS_CHECK_SERVICE_NAME(_NAME) +#define SDBUS_CHECK_MEMBER_NAME(_NAME) +#endif + +#endif /* SDBUS_CXX_INTERNAL_UTILS_H_ */