summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/flash/phosphor-software-manager/0008-PFR-image-HASH-verification.patch
blob: c65f8318c522d640cfd07a52ac0a26bf1a24f125 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
From ac6e0c217a1b136d82f93b691aff1acb40009f26 Mon Sep 17 00:00:00 2001
From: Vikram Bodireddy <vikram.bodireddy@linux.intel.com>
Date: Thu, 5 Dec 2019 11:55:36 +0530
Subject: [PATCH] PFR image HASH verification

This adds HASH verification on PFR images uploaded for
firmware updates

Tested: tested firmware update with good and bad HASH images.

A)
1. Upload the corrupted image for fw update.
2. Image present in /tmp/images/
-rw-r--r--    1 root     root      22969344 Jun  3 09:27
5dea710b-8b85-4065-8af7-3149ada81edf

3. Journalctl logs during image verification
Jun 03 09:27:20 intel-obmc phosphor-version-software-manager[4755]:
Firmware image HASH verification failed
Jun 03 09:27:20 intel-obmc phosphor-version-software-manager[4755]:
Error verifying uploaded image
Jun 03 09:27:20 intel-obmc phosphor-version-software-manager[4755]:
Error processing image

4. image deleted from /tmp/images/

B)
1. Upload the correct image.
POST: https://<BMC_IP>/redfish/v1/UpdateService/
      with <BMC_signed_cap> binary file
2. Image verification is success and proceeds with update.
{
  "@odata.id": "/redfish/v1/TaskService/Tasks/0",
  "@odata.type": "#Task.v1_4_3.Task",
  "Id": "0",
  "TaskState": "Running",
  "TaskStatus": "OK"
}

Change-Id: I9336980bfb74c8136690024782bfef45f6b08d56
Signed-off-by: Chalapathi Venkataramashetty <chalapathix.venkataramashetty@intel.com>

Signed-off-by: Vikram Bodireddy <vikram.bodireddy@linux.intel.com>
---
 pfr_image_manager.cpp | 150 +++++++++++++++++++++++++++++++++----------
 pfr_image_manager.hpp | 112 +++++++++++++++++++++++++++++--
 2 files changed, 222 insertions(+), 40 deletions(-)

diff --git a/pfr_image_manager.cpp b/pfr_image_manager.cpp
index 242a6ca..1a41cbe 100644
--- a/pfr_image_manager.cpp
+++ b/pfr_image_manager.cpp
@@ -5,6 +5,8 @@
 #include "version.hpp"
 #include "watch.hpp"
 
+#include <fcntl.h>
+#include <openssl/err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -15,9 +17,9 @@
 #include <algorithm>
 #include <cstring>
 #include <elog-errors.hpp>
-#include <filesystem>
 #include <fstream>
 #include <iomanip>
+#include <set>
 #include <sstream>
 #include <string>
 #include <xyz/openbmc_project/Software/Image/error.hpp>
@@ -33,12 +35,21 @@ using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
 namespace Software = phosphor::logging::xyz::openbmc_project::Software;
 
 static constexpr const uint32_t pfmPos = 2054;
+static constexpr const uint32_t block0Magic = 0xB6EAFD19;
+static constexpr const uint32_t lengthBlk0Blk1 = 1024;
 
-static int getPFRImgInfo(const std::filesystem::path imgPath, uint8_t& imgType,
-                         std::string& version)
+int Manager::verifyPFRImage(const std::filesystem::path imgPath,
+                            std::string& version, std::string& purposeString)
 {
-    struct pfrImgBlock0 block0Data;
-    uint8_t verData[2];
+    uint8_t imgType = 0;
+    uint32_t imgMagic = 0;
+    uint8_t verData[2] = {0};
+    uint32_t hashLen = 0;
+    struct pfrImgBlock0 block0Data = {};
+
+    std::string imageName;
+
+    EVP_MD_CTX* ctx;
 
     if (std::filesystem::exists(imgPath))
     {
@@ -55,17 +66,101 @@ static int getPFRImgInfo(const std::filesystem::path imgPath, uint8_t& imgType,
 
             imgFile.read(reinterpret_cast<char*>(&block0Data),
                          sizeof(block0Data));
+
+            imgMagic = block0Data.tag;
+
+            if (imgMagic != block0Magic)
+            {
+                phosphor::logging::log<phosphor::logging::level::ERR>(
+                    "Image magic number match failed",
+                    phosphor::logging::entry("IMAGEMAGIC=0x%x", imgMagic));
+                return -1;
+            }
+
             imgType = block0Data.pcType[0];
+
+            phosphor::logging::log<phosphor::logging::level::INFO>(
+                "Image Type", phosphor::logging::entry(
+                                  "IMAGETYPE=0x%x", static_cast<int>(imgType)));
+
+            if (imgType == pfrBMCUpdateCap || imgType == pfrBMCPFM)
+            {
+                imageName = "BMC";
+                purposeString =
+                    "xyz.openbmc_project.Software.Version.VersionPurpose.BMC";
+            }
+            else if (imgType == pfrPCHUpdateCap || imgType == pfrPCHPFM)
+            {
+                imageName = "BIOS";
+                purposeString =
+                    "xyz.openbmc_project.Software.Version.VersionPurpose.Host";
+            }
+            else if (imgType == pfrCPLDUpdateCap)
+            {
+                imageName = "CPLD";
+                purposeString =
+                    "xyz.openbmc_project.Software.Version.VersionPurpose.Other";
+            }
+            else
+            {
+                purposeString = "xyz.openbmc_project.Software.Version."
+                                "VersionPurpose.Unknown";
+
+                phosphor::logging::log<phosphor::logging::level::ERR>(
+                    "Unknown image type");
+                return -1;
+            }
+
             imgFile.seekg(pfmPos,
                           std::ios::beg); // Version is at 0x806 in the PFM
             imgFile.read(reinterpret_cast<char*>(&verData), sizeof(verData));
             imgFile.close();
-            version =
-                std::to_string(verData[0]) + "." + std::to_string(verData[1]);
+
+            auto size = std::filesystem::file_size(imgPath);
+
+            phosphor::logging::log<phosphor::logging::level::INFO>(
+                "Image Size", phosphor::logging::entry("IMAGESIZE=0x%x",
+                                                       static_cast<int>(size)));
+
+            // Adds all digest algorithms to the internal table
+            OpenSSL_add_all_digests();
+
+            ctx = EVP_MD_CTX_create();
+            EVP_DigestInit(ctx, EVP_sha256());
+
+            // Hash the image file and update the digest
+            auto dataPtr = mapFile(imgPath, size);
+
+            EVP_DigestUpdate(ctx, ((uint8_t*)dataPtr() + lengthBlk0Blk1),
+                             (size - lengthBlk0Blk1));
+
+            std::vector<uint8_t> digest(EVP_MD_size(EVP_sha256()));
+            std::vector<uint8_t> expectedDigest(block0Data.hash256,
+                                                &block0Data.hash256[0] + 32);
+
+            EVP_DigestFinal(ctx, digest.data(), &hashLen);
+            EVP_MD_CTX_destroy(ctx);
+
+            std::string redfishMsgID = "OpenBMC.0.1";
+
+            if (expectedDigest != digest)
+            {
+                redfishMsgID += ".GeneralFirmwareSecurityViolation";
+                sd_journal_send("MESSAGE=%s",
+                                "Firmware image HASH verification failed",
+                                "PRIORITY=%i", LOG_ERR, "REDFISH_MESSAGE_ID=%s",
+                                redfishMsgID.c_str(), "REDFISH_MESSAGE_ARGS=%s",
+                                "Image HASH check fail", NULL);
+                return -1;
+            }
+
             phosphor::logging::log<phosphor::logging::level::INFO>(
                 "PFR image",
                 phosphor::logging::entry("PCType=%d", block0Data.pcType[0]),
                 phosphor::logging::entry("VERSION=%s", version.c_str()));
+
+            version =
+                std::to_string(verData[0]) + "." + std::to_string(verData[1]);
         }
         catch (std::exception& e)
         {
@@ -79,20 +174,21 @@ static int getPFRImgInfo(const std::filesystem::path imgPath, uint8_t& imgType,
 
 int Manager::processImage(const std::string& imgFilePath)
 {
+
     std::filesystem::path imgPath(imgFilePath);
 
     if (!std::filesystem::exists(imgPath))
         return -1;
 
-    uint8_t imgType;
     int retry = 3;
     std::string ver;
     std::string purposeString;
 
-    if (0 != getPFRImgInfo(imgFilePath, imgType, ver))
+    if (0 != verifyPFRImage(imgFilePath, ver, purposeString))
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Error reading uploaded image type and version");
+            "Error verifying uploaded image");
+        std::filesystem::remove_all(imgFilePath);
         return -1;
     }
 
@@ -103,31 +198,6 @@ int Manager::processImage(const std::string& imgFilePath)
         return -1;
     }
 
-    if (imgType == pfrBMCUpdateCap)
-    {
-        purposeString =
-            "xyz.openbmc_project.Software.Version.VersionPurpose.BMC";
-    }
-    else if (imgType == pfrPCHUpdateCap)
-    {
-        purposeString =
-            "xyz.openbmc_project.Software.Version.VersionPurpose.Host";
-    }
-    else if (imgType == pfrCPLDUpdateCap)
-    {
-        purposeString =
-            "xyz.openbmc_project.Software.Version.VersionPurpose.Other";
-    }
-    else
-    {
-        purposeString =
-            "xyz.openbmc_project.Software.Version.VersionPurpose.Unknown";
-
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Unknown image type");
-        return -1;
-    }
-
     sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
         purpose = Version::VersionPurpose::Unknown;
     try
@@ -169,6 +239,7 @@ int Manager::processImage(const std::string& imgFilePath)
     std::filesystem::create_directory(imageDirPath);
 
     std::filesystem::path newFileName = imageDirPath / "image-runtime";
+
     std::filesystem::rename(imgFilePath, newFileName);
 
     // Create Version object
@@ -212,6 +283,14 @@ void Manager::erase(std::string entryId)
     this->versions.erase(entryId);
 }
 
+CustomMap Manager::mapFile(const std::filesystem::path& path, size_t size)
+{
+
+    CustomFd fd(open(path.c_str(), O_RDONLY));
+
+    return CustomMap(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd(), 0),
+                     size);
+}
 } // namespace manager
 } // namespace software
 } // namespace phosphor
diff --git a/pfr_image_manager.hpp b/pfr_image_manager.hpp
index c6ee6a4..5b7b2c3 100644
--- a/pfr_image_manager.hpp
+++ b/pfr_image_manager.hpp
@@ -1,6 +1,13 @@
 #pragma once
 #include "version.hpp"
 
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/rsa.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <filesystem>
 #include <sdbusplus/server.hpp>
 
 namespace phosphor
@@ -10,7 +17,8 @@ namespace software
 namespace manager
 {
 
-enum pfrImgPCType {
+enum pfrImgPCType
+{
     pfrCPLDUpdateCap = 0x00,
     pfrPCHPFM = 0x01,
     pfrPCHUpdateCap = 0x02,
@@ -19,15 +27,94 @@ enum pfrImgPCType {
 };
 
 /* PFR image block 0 - As defined in HAS */
-struct pfrImgBlock0 {
-    uint8_t tag[4];
+struct pfrImgBlock0
+{
+    uint32_t tag;
     uint8_t pcLength[4];
     uint8_t pcType[4];
     uint8_t reserved1[4];
     uint8_t hash256[32];
     uint8_t hash384[48];
     uint8_t reserved2[32];
-}__attribute__((packed));
+} __attribute__((packed));
+
+/** @struct CustomFd
+ *
+ *  RAII wrapper for file descriptor.
+ */
+struct CustomFd
+{
+  public:
+    CustomFd() = delete;
+    CustomFd(const CustomFd&) = delete;
+    CustomFd& operator=(const CustomFd&) = delete;
+    CustomFd(CustomFd&&) = default;
+    CustomFd& operator=(CustomFd&&) = default;
+    /** @brief Saves File descriptor and uses it to do file operation
+     *
+     *  @param[in] fd - File descriptor
+     */
+    CustomFd(int fd) : fd(fd)
+    {
+    }
+
+    ~CustomFd()
+    {
+        if (fd >= 0)
+        {
+            close(fd);
+        }
+    }
+
+    int operator()() const
+    {
+        return fd;
+    }
+
+  private:
+    /** @brief File descriptor */
+    int fd = -1;
+};
+
+/** @struct CustomMap
+ *
+ *  RAII wrapper for mmap.
+ */
+struct CustomMap
+{
+  private:
+    /** @brief starting address of the map   */
+    void* addr;
+
+    /** @brief length of the mapping   */
+    size_t length;
+
+  public:
+    CustomMap() = delete;
+    CustomMap(const CustomMap&) = delete;
+    CustomMap& operator=(const CustomMap&) = delete;
+    CustomMap(CustomMap&&) = default;
+    CustomMap& operator=(CustomMap&&) = default;
+
+    /** @brief Saves starting address of the map and
+     *         and length of the file.
+     *  @param[in]  addr - Starting address of the map
+     *  @param[in]  length - length of the map
+     */
+    CustomMap(void* addr, size_t length) : addr(addr), length(length)
+    {
+    }
+
+    ~CustomMap()
+    {
+        munmap(addr, length);
+    }
+
+    void* operator()() const
+    {
+        return addr;
+    }
+};
 
 /** @class Manager
  *  @brief Contains a map of Version dbus objects.
@@ -61,13 +148,28 @@ class Manager
     void erase(std::string entryId);
 
   private:
+    /**
+     * @brief Memory map the  file
+     * @param[in]  - file path
+     * @param[in]  - file size
+     * @param[out] - Custom Mmap address
+     */
+    CustomMap mapFile(const std::filesystem::path& path, size_t size);
+
+    /**
+     * @brief Verify the PFR image and return version and purpose
+     * @param[in]  - file path
+     * @param[out]  - version
+     * @param[out]  - purpose
+     */
+    int verifyPFRImage(const std::filesystem::path imgPath,
+                       std::string& version, std::string& purposeString);
     /** @brief Persistent map of Version dbus objects and their
      * version id */
     std::map<std::string, std::unique_ptr<Version>> versions;
 
     /** @brief Persistent sdbusplus DBus bus connection. */
     sdbusplus::bus::bus& bus;
-
 };
 
 } // namespace manager
-- 
2.17.1