Use memcpy to avoid unaligned access 90/250790/3
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 4 Jan 2021 13:56:28 +0000 (14:56 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 4 Jan 2021 19:34:47 +0000 (20:34 +0100)
Casting unsigned char* to signalfd_siginfo* may cause an unaligned
access (see -Wcast-align). Use memcpy to avoid it.

Verify by sending SIGTERM to key-manager, observing the logs and
systemctl status. The service should stop without errors.

systemctl start central-key-manager
kill -SIGTERM `pidof key-manager`
systemctl status central-key-manager

Change-Id: I061cc2f488cba9252ed65b0d8ca22840f725a433

src/manager/main/socket-manager.cpp

index 55150fe..047b6c5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014 - 2021 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
 #include <signal.h>
 #include <errno.h>
 #include <cassert>
+#include <cstring>
 
 #include <systemd/sd-daemon.h>
 
@@ -129,23 +130,23 @@ struct SignalService : public GenericSocketService {
        {
                LogDebug("Get signal information");
 
-               if (sizeof(struct signalfd_siginfo) != event.rawBuffer.size()) {
+               signalfd_siginfo siginfo;
+               if (sizeof(siginfo) != event.rawBuffer.size()) {
                        LogError("Wrong size of signalfd_siginfo struct. Expected: "
-                                        << sizeof(signalfd_siginfo) << " Get: "
+                                        << sizeof(siginfo) << " Get: "
                                         << event.rawBuffer.size());
                        return;
                }
 
-               auto siginfo = reinterpret_cast<const signalfd_siginfo *>
-                                          (event.rawBuffer.data());
+               memcpy(&siginfo, event.rawBuffer.data(), sizeof(siginfo));
 
-               if (siginfo->ssi_signo == SIGTERM) {
+               if (siginfo.ssi_signo == SIGTERM) {
                        LogInfo("Got signal: SIGTERM");
                        m_serviceManager->MainLoopStop();
                        return;
                }
 
-               LogInfo("This should not happend. Got signal: " << siginfo->ssi_signo);
+               LogInfo("This should not happen. Got unexpected signal: " << siginfo.ssi_signo);
        }
 };