Blame SOURCES/opencryptoki-3.16.0-7b7d83c571ceb3050969359817d4145600f14ae8.patch

2c1758
commit 7b7d83c571ceb3050969359817d4145600f14ae8
2c1758
Author: Ingo Franzki <ifranzki@linux.ibm.com>
2c1758
Date:   Fri Apr 9 17:07:31 2021 +0200
2c1758
2c1758
    Check CKF_LIBRARY_CANT_CREATE_OS_THREADS at C_Initialize
2c1758
    
2c1758
    Fail if flag CKF_LIBRARY_CANT_CREATE_OS_THREADS is set at C_Initialize,
2c1758
    and event support is enabled (this is the default). We need to use pthreads
2c1758
    for the event thread, so we can't work if CKF_LIBRARY_CANT_CREATE_OS_THREADS
2c1758
    is set. Fail with CKR_NEED_TO_CREATE_THREADS if so.
2c1758
    
2c1758
    The event support can be globally disabled using keyword 'disable-event-support'
2c1758
    in opencryptoki.conf. This disables pkcsslots to accept admin connections,
2c1758
    and it does not monitor for AP UDEV events (on s390 platform). No event
2c1758
    thread is started in the opencryptoki processes, thus we can accept if flag
2c1758
    CKF_LIBRARY_CANT_CREATE_OS_THREADS is set in that case.
2c1758
    
2c1758
    Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2c1758
2c1758
diff --git a/man/man5/opencryptoki.conf.5.in b/man/man5/opencryptoki.conf.5.in
2c1758
index 71218f79..7dc676ab 100644
2c1758
--- a/man/man5/opencryptoki.conf.5.in
2c1758
+++ b/man/man5/opencryptoki.conf.5.in
2c1758
@@ -10,8 +10,16 @@ pkcs#11 slots. At startup, the pkcsslotd daemon parses this file to
2c1758
 determine which slots will be made available.
2c1758
 
2c1758
 .SH SYNTAX
2c1758
-This file is made up of slot descriptions. Each slot description
2c1758
-is composed of a slot number, brackets and key-value pairs.
2c1758
+This file is made up of optional global definitions, and slot descriptions.
2c1758
+
2c1758
+The following global definitions are valid:
2c1758
+
2c1758
+.TP
2c1758
+.BR disable-event-support
2c1758
+If this keyword is specified the openCryptoki event support is disabled.
2c1758
+
2c1758
+.P
2c1758
+Each slot description is composed of a slot number, brackets and key-value pairs.
2c1758
 
2c1758
  slot number
2c1758
  {
2c1758
diff --git a/usr/include/slotmgr.h b/usr/include/slotmgr.h
2c1758
index e37368a5..451a8cf1 100644
2c1758
--- a/usr/include/slotmgr.h
2c1758
+++ b/usr/include/slotmgr.h
2c1758
@@ -99,6 +99,7 @@ typedef struct {
2c1758
     LW_SHM_TYPE *shm_addr;      // token specific shm address
2c1758
 } Slot_Info_t;
2c1758
 
2c1758
+#define FLAG_EVENT_SUPPORT_DISABLED   0x01
2c1758
 
2c1758
 #ifdef PKCS64
2c1758
 
2c1758
@@ -200,6 +201,7 @@ typedef struct {
2c1758
 
2c1758
 typedef struct {
2c1758
     uint8 num_slots;
2c1758
+    uint8 flags;
2c1758
     CK_INFO_64 ck_info;
2c1758
     Slot_Info_t_64 slot_info[NUMBER_SLOTS_MANAGED];
2c1758
 } Slot_Mgr_Socket_t;
2c1758
@@ -214,6 +216,7 @@ typedef struct {
2c1758
 
2c1758
 typedef struct {
2c1758
     uint8 num_slots;
2c1758
+    uint8 flags;
2c1758
     CK_INFO ck_info;
2c1758
     Slot_Info_t slot_info[NUMBER_SLOTS_MANAGED];
2c1758
 } Slot_Mgr_Socket_t;
2c1758
diff --git a/usr/lib/api/api_interface.c b/usr/lib/api/api_interface.c
2c1758
index 2873a20a..6517ca6c 100644
2c1758
--- a/usr/lib/api/api_interface.c
2c1758
+++ b/usr/lib/api/api_interface.c
2c1758
@@ -308,7 +308,8 @@ void parent_fork_after()
2c1758
         return;
2c1758
 
2c1758
     /* Restart the event thread in the parent when fork is complete */
2c1758
-    if (Anchor->event_thread == 0)
2c1758
+    if ((Anchor->SocketDataP.flags & FLAG_EVENT_SUPPORT_DISABLED) == 0 &&
2c1758
+        Anchor->event_thread == 0)
2c1758
         start_event_thread();
2c1758
 }
2c1758
 
2c1758
@@ -2752,13 +2753,7 @@ CK_RV C_Initialize(CK_VOID_PTR pVoid)
2c1758
                 goto error;
2c1758
             }
2c1758
         }
2c1758
-        // If we EVER need to create threads from this library we must
2c1758
-        // check the Flags for the Can_Create_OS_Threads flag
2c1758
-        // Right now the library DOES NOT create threads and therefore this
2c1758
-        // check is irrelavant.
2c1758
-        if (pArg->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) {
2c1758
-            TRACE_DEVEL("Can't create OS threads...This is OK\n");
2c1758
-        }
2c1758
+
2c1758
         // Since this is an initialization path, we will be verbose in the
2c1758
         // code rather than efficient.
2c1758
         //
2c1758
@@ -2848,7 +2843,21 @@ CK_RV C_Initialize(CK_VOID_PTR pVoid)
2c1758
         rc = CKR_FUNCTION_FAILED;
2c1758
         goto error_shm;
2c1758
     }
2c1758
-    // Initialize structure values
2c1758
+
2c1758
+    if (pVoid != NULL) {
2c1758
+        pArg = (CK_C_INITIALIZE_ARGS *) pVoid;
2c1758
+
2c1758
+        if ((Anchor->SocketDataP.flags & FLAG_EVENT_SUPPORT_DISABLED) == 0 &&
2c1758
+            (pArg->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) != 0) {
2c1758
+            TRACE_ERROR("Flag CKF_LIBRARY_CANT_CREATE_OS_THREADS is set and "
2c1758
+                        "event support is enabled\n");
2c1758
+            OCK_SYSLOG(LOG_ERR, "C_Initialize: Application specified that "
2c1758
+                       "library can't create OS threads. PKCS11 Module requires "
2c1758
+                       "to create threads when event support is enabled.\n");
2c1758
+            rc = CKR_NEED_TO_CREATE_THREADS;
2c1758
+            goto error;
2c1758
+        }
2c1758
+    }
2c1758
 
2c1758
     //Register with pkcsslotd
2c1758
     if (!API_Register()) {
2c1758
@@ -2867,7 +2876,8 @@ CK_RV C_Initialize(CK_VOID_PTR pVoid)
2c1758
     }
2c1758
 
2c1758
     /* Start event receiver thread */
2c1758
-    if (start_event_thread() != 0) {
2c1758
+    if ((Anchor->SocketDataP.flags & FLAG_EVENT_SUPPORT_DISABLED) == 0 &&
2c1758
+        start_event_thread() != 0) {
2c1758
         TRACE_ERROR("Failed to start event thread\n");
2c1758
 
2c1758
         // unload all the STDLL's from the application
2c1758
diff --git a/usr/lib/common/configparser.h b/usr/lib/common/configparser.h
2c1758
index 13ca648d..b3c32496 100644
2c1758
--- a/usr/lib/common/configparser.h
2c1758
+++ b/usr/lib/common/configparser.h
2c1758
@@ -35,6 +35,7 @@ typedef int  (*end_slot_f)(void *private);
2c1758
 typedef int  (*key_str_f)(void *private, int tok, const char *val);
2c1758
 typedef int  (*key_vers_f)(void *private, int tok, unsigned int vers);
2c1758
 typedef void (*eolcomment_f)(void *private, const char *comment);
2c1758
+typedef void (*disab_event_supp_f)(void *private);
2c1758
 /*
2c1758
  * Report an error.  If the error is not reported by the parser itself
2c1758
  * but via one of the parse functions, \c parsermsg will be \c NULL.
2c1758
@@ -52,6 +53,7 @@ typedef void (*error_f)(void *private, int line, const char *parsermsg);
2c1758
  */
2c1758
 struct parsefuncs {
2c1758
     ockversion_f  version;
2c1758
+    disab_event_supp_f disab_event_supp;
2c1758
     eol_f         eol;
2c1758
     begin_slot_f  begin_slot;
2c1758
     end_slot_f    end_slot;
2c1758
diff --git a/usr/lib/common/lexer.l b/usr/lib/common/lexer.l
2c1758
index b35a0b72..38cbcb70 100644
2c1758
--- a/usr/lib/common/lexer.l
2c1758
+++ b/usr/lib/common/lexer.l
2c1758
@@ -69,6 +69,7 @@ extern char *configparse_strdup(const char *s);
2c1758
 
2c1758
 version                 return OCKVERSION;
2c1758
 slot                    return SLOT;
2c1758
+disable-event-support   return DISABLE_EVENT_SUPPORT;
2c1758
 
2c1758
 [^\"= \t\n]+		{
2c1758
 			  yylval.str = configparse_strdup(yytext);
2c1758
diff --git a/usr/lib/common/parser.y b/usr/lib/common/parser.y
2c1758
index 86806fcb..40c3994d 100644
2c1758
--- a/usr/lib/common/parser.y
2c1758
+++ b/usr/lib/common/parser.y
2c1758
@@ -65,7 +65,7 @@ int lookup_keyword(const char *key);
2c1758
     int err;
2c1758
 }
2c1758
 
2c1758
-%token EQUAL DOT SLOT EOL OCKVERSION BEGIN_DEF END_DEF
2c1758
+%token EQUAL DOT SLOT EOL OCKVERSION BEGIN_DEF END_DEF DISABLE_EVENT_SUPPORT
2c1758
 %token <str> STRING
2c1758
 %token <str> KEYWORD
2c1758
 %token <num> INTEGER
2c1758
@@ -81,6 +81,7 @@ config_file:
2c1758
 
2c1758
 sections:
2c1758
 	version_def eolcomment
2c1758
+	| disable_event_support_def eolcomment
2c1758
 	| SLOT INTEGER BEGIN_DEF
2c1758
 	{
2c1758
         if (parsefuncs->begin_slot && parsefuncs->begin_slot(parsedata, $2, 0)) {
2c1758
@@ -125,6 +126,13 @@ version_def:
2c1758
         }
2c1758
         configparse_freestringsfrom($2);
2c1758
     }
2c1758
+    
2c1758
+disable_event_support_def:
2c1758
+    DISABLE_EVENT_SUPPORT
2c1758
+    {
2c1758
+        if (parsefuncs->disab_event_supp)
2c1758
+            parsefuncs->disab_event_supp(parsedata);
2c1758
+    }
2c1758
 
2c1758
 line_def:
2c1758
     STRING EQUAL TOKVERSION
2c1758
diff --git a/usr/sbin/pkcsslotd/pkcsslotd.h b/usr/sbin/pkcsslotd/pkcsslotd.h
2c1758
index d7edcb3c..1dd0bac9 100644
2c1758
--- a/usr/sbin/pkcsslotd/pkcsslotd.h
2c1758
+++ b/usr/sbin/pkcsslotd/pkcsslotd.h
2c1758
@@ -88,7 +88,7 @@ int XProcLock(void);
2c1758
 int XProcUnLock(void);
2c1758
 int CreateXProcLock(void);
2c1758
 
2c1758
-int init_socket_server();
2c1758
+int init_socket_server(int event_support_disabled);
2c1758
 int term_socket_server();
2c1758
 int init_socket_data(Slot_Mgr_Socket_t *sp);
2c1758
 int socket_connection_handler(int timeout_secs);
2c1758
diff --git a/usr/sbin/pkcsslotd/slotmgr.c b/usr/sbin/pkcsslotd/slotmgr.c
2c1758
index efbfe8fd..3b328a6c 100644
2c1758
--- a/usr/sbin/pkcsslotd/slotmgr.c
2c1758
+++ b/usr/sbin/pkcsslotd/slotmgr.c
2c1758
@@ -34,6 +34,7 @@ int shmid;
2c1758
 key_t tok;
2c1758
 Slot_Info_t_64 sinfo[NUMBER_SLOTS_MANAGED];
2c1758
 unsigned int NumberSlotsInDB = 0;
2c1758
+int event_support_disabled = 0;
2c1758
 
2c1758
 Slot_Info_t_64 *psinfo;
2c1758
 
2c1758
@@ -467,6 +468,13 @@ static int slotmgr_key_vers(void *private, int tok, unsigned int vers)
2c1758
     return 1;
2c1758
 }
2c1758
 
2c1758
+static void slotmgr_disab_event_supp(void *private)
2c1758
+{
2c1758
+    UNUSED(private);
2c1758
+
2c1758
+    event_support_disabled = 1;
2c1758
+}
2c1758
+
2c1758
 static void slotmgr_parseerror(void *private, int line, const char *parsermsg)
2c1758
 {
2c1758
     struct parse_data *d = (struct parse_data *)private;
2c1758
@@ -480,6 +488,7 @@ static struct parsefuncs slotmgr_parsefuncs = {
2c1758
     .end_slot   = slotmgr_end_slot,
2c1758
     .key_str    = slotmgr_key_str,
2c1758
     .key_vers   = slotmgr_key_vers,
2c1758
+    .disab_event_supp = slotmgr_disab_event_supp,
2c1758
     .parseerror = slotmgr_parseerror
2c1758
 };
2c1758
 
2c1758
@@ -568,7 +577,7 @@ int main(int argc, char *argv[], char *envp[])
2c1758
     if (!XProcUnLock())
2c1758
         return 4;
2c1758
 
2c1758
-    if (!init_socket_server()) {
2c1758
+    if (!init_socket_server(event_support_disabled)) {
2c1758
         DestroyMutexes();
2c1758
         DetachFromSharedMemory();
2c1758
         DestroySharedMemory();
2c1758
@@ -582,6 +591,8 @@ int main(int argc, char *argv[], char *envp[])
2c1758
         DestroySharedMemory();
2c1758
         return 6;
2c1758
     }
2c1758
+    if (event_support_disabled)
2c1758
+        socketData.flags |= FLAG_EVENT_SUPPORT_DISABLED;
2c1758
 
2c1758
     /* Create customized token directories */
2c1758
     psinfo = &socketData.slot_info[0];
2c1758
diff --git a/usr/sbin/pkcsslotd/socket_server.c b/usr/sbin/pkcsslotd/socket_server.c
2c1758
index 41408670..3aa40267 100644
2c1758
--- a/usr/sbin/pkcsslotd/socket_server.c
2c1758
+++ b/usr/sbin/pkcsslotd/socket_server.c
2c1758
@@ -139,12 +139,12 @@ struct event_info {
2c1758
 };
2c1758
 
2c1758
 static int epoll_fd = -1;
2c1758
-static struct listener_info proc_listener;
2c1758
+static struct listener_info proc_listener = { .socket = -1 };
2c1758
 static DL_NODE *proc_connections = NULL;
2c1758
-static struct listener_info admin_listener;
2c1758
+static struct listener_info admin_listener = { .socket = -1 };
2c1758
 static DL_NODE *admin_connections = NULL;
2c1758
 #ifdef WITH_LIBUDEV
2c1758
-static struct udev_mon udev_mon;
2c1758
+static struct udev_mon udev_mon = { .socket = -1 };
2c1758
 #endif
2c1758
 static DL_NODE *pending_events = NULL;
2c1758
 static unsigned long pending_events_count = 0;
2c1758
@@ -1620,6 +1620,9 @@ static void udev_mon_term(struct udev_mon *udev_mon)
2c1758
     if (udev_mon == NULL)
2c1758
         return;
2c1758
 
2c1758
+    if (udev_mon->socket < 0)
2c1758
+        return;
2c1758
+
2c1758
     epoll_ctl(epoll_fd, EPOLL_CTL_DEL, udev_mon->socket, NULL);
2c1758
     if (udev_mon->udev != NULL)
2c1758
         udev_unref(udev_mon->udev);
2c1758
@@ -1636,6 +1639,7 @@ int init_socket_data(Slot_Mgr_Socket_t *socketData)
2c1758
 {
2c1758
     unsigned int processed = 0;
2c1758
 
2c1758
+    socketData->flags = 0;
2c1758
     PopulateCKInfo(&(socketData->ck_info));
2c1758
     socketData->num_slots = NumberSlotsInDB;
2c1758
     PopulateSlotInfo(socketData->slot_info, &processed);
2c1758
@@ -1692,7 +1696,7 @@ int socket_connection_handler(int timeout_secs)
2c1758
     return TRUE;
2c1758
 }
2c1758
 
2c1758
-int init_socket_server()
2c1758
+int init_socket_server(int event_support_disabled)
2c1758
 {
2c1758
     int err;
2c1758
 
2c1758
@@ -1710,18 +1714,20 @@ int init_socket_server()
2c1758
         return FALSE;
2c1758
     }
2c1758
 
2c1758
-    if (!listener_create(ADMIN_SOCKET_FILE_PATH, &admin_listener,
2c1758
-                         admin_new_conn, NUMBER_ADMINS_ALLOWED)) {
2c1758
-        term_socket_server();
2c1758
-        return FALSE;
2c1758
-    }
2c1758
+    if (!event_support_disabled) {
2c1758
+        if (!listener_create(ADMIN_SOCKET_FILE_PATH, &admin_listener,
2c1758
+                             admin_new_conn, NUMBER_ADMINS_ALLOWED)) {
2c1758
+            term_socket_server();
2c1758
+            return FALSE;
2c1758
+        }
2c1758
 
2c1758
 #ifdef WITH_LIBUDEV
2c1758
-    if (!udev_mon_init(UDEV_SUBSYSTEM_AP, &udev_mon)) {
2c1758
-        term_socket_server();
2c1758
-        return FALSE;
2c1758
-    }
2c1758
+        if (!udev_mon_init(UDEV_SUBSYSTEM_AP, &udev_mon)) {
2c1758
+            term_socket_server();
2c1758
+            return FALSE;
2c1758
+        }
2c1758
 #endif
2c1758
+    }
2c1758
 
2c1758
     DbgLog(DL0, "%s: Socket server started", __func__);
2c1758
 
2c1758
diff --git a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
2c1758
index 7c225730..94fd1196 100644
2c1758
--- a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
2c1758
+++ b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
2c1758
@@ -2066,6 +2066,13 @@ static int parseupdate_ockversion(void *private, const char *version)
2c1758
     return 0;
2c1758
 }
2c1758
 
2c1758
+static void parseupdate_disab_event_supp(void *private)
2c1758
+{
2c1758
+    struct parseupdate *u = (struct parseupdate *)private;
2c1758
+
2c1758
+    fprintf(u->f, "disable-event-support");
2c1758
+}
2c1758
+
2c1758
 static void parseupdate_eol(void *private)
2c1758
 {
2c1758
 	struct parseupdate *u = (struct parseupdate *)private;
2c1758
@@ -2124,6 +2131,7 @@ static void parseupdate_eolcomment(void *private, const char *comment)
2c1758
 
2c1758
 static struct parsefuncs parseupdatefuncs = {
2c1758
     .version    = parseupdate_ockversion,
2c1758
+    .disab_event_supp = parseupdate_disab_event_supp,
2c1758
     .eol        = parseupdate_eol,
2c1758
     .begin_slot = parseupdate_begin_slot,
2c1758
     .end_slot   = parseupdate_end_slot,