Change verification of privileges 63/255963/9
authorTomasz Swierczek <t.swierczek@samsung.com>
Thu, 25 Mar 2021 13:18:55 +0000 (14:18 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Thu, 1 Apr 2021 11:55:02 +0000 (13:55 +0200)
Now, the privileges are checked depending on the message
type received.

This implementation requires DCM daemon to receive
& deserialize the message 1st before checking client privileges.

This is required by the introduction of DCM EXT API which can be guarded
by specific privilege (or none) for each backend method.

Change-Id: Iaaae1bc220e497f2488d8d2700a5334fa14b61b6

src/dcm-daemon/dcm_session.cpp
src/dcm-daemon/dcm_session.h

index 6b7d3deb9cf89a16c5d098663455a1e223974fe4..4eac7b784205ecf6c041188c8a87cd31e110ef86 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************
  *
- * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2021 Samsung Electronics All Rights Reserved.
  *
  * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
  *
@@ -29,6 +29,8 @@
 #include "dcm_server.h"
 #include "log.h"
 
+#define DCM_DEFAULT_PRIVILEGE "http://tizen.org/privilege/devicecertificate"
+
 dcm_session::dcm_session(boost::asio::io_service& io_service,
        const std::shared_ptr<dcm_server>& server,
        std::shared_ptr<so_resolver> soResolver,
@@ -48,14 +50,9 @@ dcm_session::~dcm_session()
 
 void dcm_session::start()
 {
-       int handle = fSocket.native_handle();
        LOGD("Accepted connection with socket " << fSocket.native_handle());
 
-       if(verify_privileges(handle)) {
-               do_receive();
-       } else {
-               LOGE("Client privilege check failure. Disconnect");
-       }
+       do_receive();
 }
 
 void dcm_session::do_receive() noexcept
@@ -162,7 +159,7 @@ static inline std::string cynara_error_to_string(int error) {
        return std::string("Can't translate error");
 }
 
-bool dcm_session::verify_privileges(int handle)
+bool dcm_session::verify_privileges(int handle, const char* privilege_to_check)
 {
        int ret = 0;
        char* tmp_str;
@@ -206,15 +203,15 @@ bool dcm_session::verify_privileges(int handle)
        LOGD("Got new session from " << pid << " with user " << user.get() <<
                ", client ID " << client.get() << " and session ID " << client_session.get());
 
-       ret = cynara_check(fCynaraInstance, client.get(), client_session.get(), user.get(),
-               "http://tizen.org/privilege/devicecertificate");
+       ret = cynara_check(fCynaraInstance, client.get(), client_session.get(), user.get(), privilege_to_check);
 
        if(ret != CYNARA_API_ACCESS_ALLOWED) {
-               LOGE("Application access denied for " << pid << " - " << cynara_error_to_string(ret));
+               LOGE("Application access denied for " << pid << ", privilege: " <<
+                        privilege_to_check <<  ", error:  - " << cynara_error_to_string(ret));
                return false;
        }
 
-       LOGD("Access granted for " << pid);
+       LOGD("Access granted for " << pid << " with privilege " << privilege_to_check);
        return true;
 }
 
@@ -222,6 +219,11 @@ void dcm_session::handle_context_association(const AssociateKeyContext& message)
 {
        LOGD("Associate context");
 
+       if(!verify_privileges(fSocket.native_handle(), DCM_DEFAULT_PRIVILEGE)) {
+               LOGE("Client privilege check failure. Disconnect");
+               return;
+       }
+
        ResponseMessage msg;
        auto* contextResponse = msg.mutable_associate_context();
 
@@ -275,6 +277,11 @@ void dcm_session::handle_cert_chain(const RequestCertificateChain& message)
 {
        LOGD("Request certificate chain");
 
+       if(!verify_privileges(fSocket.native_handle(), DCM_DEFAULT_PRIVILEGE)) {
+               LOGE("Client privilege check failure. Disconnect");
+               return;
+       }
+
        ResponseMessage msg;
        auto* certificateResponse = msg.mutable_request_chain();
 
@@ -320,6 +327,11 @@ void dcm_session::handle_sign_request(const SignRequest& message)
 {
        LOGD("Request data signing");
 
+       if(!verify_privileges(fSocket.native_handle(), DCM_DEFAULT_PRIVILEGE)) {
+               LOGE("Client privilege check failure. Disconnect");
+               return;
+       }
+
        ResponseMessage msg;
        auto* signingResponse = msg.mutable_sign_data();
 
@@ -348,4 +360,4 @@ void dcm_session::handle_sign_request(const SignRequest& message)
 
        signingResponse->set_result(error);
        reply(msg);
-}
+}
\ No newline at end of file
index b621f1b1a66d0fb7bab8d77bb717f27cb5217b63..faf30a214dbe9ebf6beb0dbf27f8381ff89b4d14 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************
  *
- * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2021 Samsung Electronics All Rights Reserved.
  *
  * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
  *
@@ -55,7 +55,7 @@ private:
        void decode_message() noexcept;
        void reply(const ResponseMessage& resp) noexcept;
 
-       bool verify_privileges(int handle);
+       bool verify_privileges(int handle, const char* privilege_to_check);
 
        void handle_context_association(const AssociateKeyContext& message);
        void handle_cert_chain(const RequestCertificateChain& message);