Blob Blame History Raw
From d1dbc25c6f424a12860295008991cd1392c888a8 Mon Sep 17 00:00:00 2001
From: Ingo Franzki <ifranzki@linux.ibm.com>
Date: Mon, 6 Jul 2020 09:56:31 +0200
Subject: [PATCH 4/5] pkcstok_migrate: Remove the token's shared memory segment

After successfully migration, remove the tokens shared memory segment.
This will be re-created on the first use of the token.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
---
 usr/sbin/pkcstok_migrate/pkcstok_migrate.c  | 38 +++++++++++++++++++++++++++++
 usr/sbin/pkcstok_migrate/pkcstok_migrate.mk |  2 +-
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
index 136c010c..46e5e57f 100644
--- a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
+++ b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
@@ -31,6 +31,7 @@
 #include <termios.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <sys/mman.h>
 #include <pkcs11types.h>
 
 #include "sw_crypt.h"
@@ -2108,6 +2109,36 @@ done:
 
 }
 
+/**
+ * Removes the token_s shared memory from /dev/shm
+ */
+static CK_RV remove_shared_memory(char *location)
+{
+    char shm_name[PATH_MAX];
+    int i, k, rc;
+
+    i = k = 0;
+    shm_name[k++] = '/';
+    if (location[i] == '/')
+        i++;
+
+    for (; location[i]; i++, k++) {
+        if (location[i] == '/')
+            shm_name[k] = '.';
+        else
+            shm_name[k] = location[i];
+    }
+    shm_name[k] = '\0';
+
+    rc = shm_unlink(shm_name);
+    if (rc != 0) {
+        warnx("shm_unlink(%s) failed, errno=%s", shm_name, strerror(errno));
+        return CKR_FUNCTION_FAILED;
+    }
+
+    return CKR_OK;
+}
+
 /**
  * Copy a file given by name from a src folder to a dst folder.
  */
@@ -2718,6 +2749,13 @@ int main(int argc, char **argv)
         goto done;
     }
 
+    /* Remove the token's shared memory */
+    ret = remove_shared_memory(data_store);
+    if (ret != CKR_OK) {
+        warnx("Failed to remove token's shared memory.");
+        goto done;
+    }
+
     /* Now insert new 'tokversion=3.12' parm in opencryptoki.conf */
     ret = update_opencryptoki_conf(slot_id, conf_dir);
     if (ret != CKR_OK) {
diff --git a/usr/sbin/pkcstok_migrate/pkcstok_migrate.mk b/usr/sbin/pkcstok_migrate/pkcstok_migrate.mk
index dc4582e5..028a383e 100644
--- a/usr/sbin/pkcstok_migrate/pkcstok_migrate.mk
+++ b/usr/sbin/pkcstok_migrate/pkcstok_migrate.mk
@@ -6,7 +6,7 @@ noinst_HEADERS += usr/include/local_types.h
 noinst_HEADERS += usr/lib/common/h_extern.h
 noinst_HEADERS += usr/lib/common/pkcs_utils.h
 
-usr_sbin_pkcstok_migrate_pkcstok_migrate_LDFLAGS = -lcrypto -ldl
+usr_sbin_pkcstok_migrate_pkcstok_migrate_LDFLAGS = -lcrypto -ldl -lrt
 
 usr_sbin_pkcstok_migrate_pkcstok_migrate_CFLAGS  =		\
 	-DSTDLL_NAME=\"pkcstok_migrate\"			\
-- 
2.16.2.windows.1