From 9701f8e27ac8d70f616930f977ee40b309911288 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Sat, 9 Sep 2023 06:48:39 +0000
Subject: [PATCH 144/145] !2178 clean path for fpath and verify chain id Merge
 pull request !2178 from zhongtao/image

---
 src/daemon/modules/image/oci/oci_load.c       | 30 +++++++++++++++++--
 .../modules/image/oci/registry/registry.c     |  2 +-
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/daemon/modules/image/oci/oci_load.c b/src/daemon/modules/image/oci/oci_load.c
index 7dfc5cb6..3fc8cfb8 100644
--- a/src/daemon/modules/image/oci/oci_load.c
+++ b/src/daemon/modules/image/oci/oci_load.c
@@ -27,8 +27,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <linux/limits.h>
 
 #include "utils.h"
+#include "path.h"
 #include "isula_libutils/log.h"
 #include "util_archive.h"
 #include "storage.h"
@@ -703,6 +705,9 @@ static int oci_load_set_layers_info(load_image_t *im, const image_manifest_items
     }
 
     for (; i < conf->rootfs->diff_ids_len; i++) {
+        char *fpath = NULL;
+        char cleanpath[PATH_MAX] = { 0 };
+
         im->layers[i] = util_common_calloc_s(sizeof(load_layer_blob_t));
         if (im->layers[i] == NULL) {
             ERROR("Out of memory");
@@ -710,12 +715,31 @@ static int oci_load_set_layers_info(load_image_t *im, const image_manifest_items
             goto out;
         }
 
-        im->layers[i]->fpath = util_path_join(dstdir, manifest->layers[i]);
-        if (im->layers[i]->fpath == NULL) {
-            ERROR("Path join failed");
+        fpath = util_path_join(dstdir, manifest->layers[i]);
+        if (fpath == NULL) {
+            ERROR("Failed to join path");
+            ret = -1;
+            goto out;
+        }
+
+        if (util_clean_path(fpath, cleanpath, sizeof(cleanpath)) == NULL) {
+            ERROR("Failed to clean path for %s", fpath);
+            free(fpath);
+            ret = -1;
+            goto out;
+        }
+
+        free(fpath);
+
+        // verify whether the prefix of the path is dstdir to prevent illegal directories
+        if (strncmp(cleanpath, dstdir, strlen(dstdir)) != 0) {
+            ERROR("Illegal directory: %s", cleanpath);
             ret = -1;
             goto out;
         }
+
+        im->layers[i]->fpath = util_strdup_s(cleanpath);
+
         // The format is sha256:xxx
         im->layers[i]->chain_id = oci_load_calc_chain_id(parent_chain_id_sha256, conf->rootfs->diff_ids[i]);
         if (im->layers[i]->chain_id == NULL) {
diff --git a/src/daemon/modules/image/oci/registry/registry.c b/src/daemon/modules/image/oci/registry/registry.c
index 2cf79fb9..d586d1c7 100644
--- a/src/daemon/modules/image/oci/registry/registry.c
+++ b/src/daemon/modules/image/oci/registry/registry.c
@@ -594,7 +594,7 @@ static int register_layer(pull_descriptor *desc, size_t i)
         return 0;
     }
 
-    id = util_without_sha256_prefix(desc->layers[i].chain_id);
+    id = oci_image_id_from_digest(desc->layers[i].chain_id);
     if (id == NULL) {
         ERROR("layer %zu have NULL digest for image %s", i, desc->image_name);
         return -1;
-- 
2.40.1

