1
0
mirror of https://github.com/opencryptoki/opencryptoki.git synced 2026-10-24 05:39:17 +03:00
Commit Graph

3874 Commits

Author SHA1 Message Date
Ingo Franzki
bf8cf2fbaa COMMON/PKCSCCA/PKCSTOK_MIGRATE: Prevent overflow when reading old token objects
Check that the decrypted data is large enough to also hold the digest.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:45 +02:00
Ingo Franzki
9cbe772cf0 PKCSCCA: Prevent integer overflow when reading token objects from disk
Check for invalid sizes and reject too small or too large sizes.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:44 +02:00
Ingo Franzki
d544af81af PKCSSTATS: Escape strings printed into JSON output
Escape Backslashes, double-quotes, or control characters in strings
used for JSON output.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:44 +02:00
Ingo Franzki
95b15b1491 PKCSSTATS: Check for NULL after memory allocation
Prevent a NULL pointer access after a failed allocation.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:44 +02:00
Ingo Franzki
d51d728862 STATISTICS/PKCSSTATS: Prevent buffer overrun when building SHM name
For incredibly long token directory names the buffer could overflow
when building the SHM name.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:43 +02:00
Ingo Franzki
97ec424766 STATISTICS/PKCSSTATS: Do not overflow statistics counters, saturate at max
When statistic counters would overflow, keep them at the maximum possible
value. Wrapping to zero is the worst possible outcome for an auditing tool,
it silently under-reports activity.

If a counter saturated, then the pkcsstatd display indicates this with
"[saturated]" instead of the counter value.

While at it also make the reset operation to operate in an per counter
atomic way. Otherwise counter increments might get lost while a reset is
performed.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:43 +02:00
Ingo Franzki
50005e090d STATISTICS/PKCSSTATS: Ensure that the full range of uids is accepted
Uids are typically unsigned ints (uid_t), but the code handles them as
signed int, possibly dropping the upper half values.

Also correct the statistics SHM file iterating matching logic.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:43 +02:00
Ingo Franzki
d32d8ac28b STATISTICS/PKCSSTATS: Prevent integer overflow on statistics SHM size
Limit the number of slot to NUMBER_SLOTS_MANAGED when calculating the
size of the statistics shared memory segment.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:43 +02:00
Ingo Franzki
4488680428 PKCSCONF: Report failure of C_InitPIN and C_SetPIN with exit code
In case C_InitPIN or C_SetPIN fails, a warning message is printed, but the
the return code is not passed back to main(), but CKR_OK, or the failure
of the subsequent C_Logout or C_CloseSession. So the exit code of pkcsconf
most likely won't indicate an error although it failed.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:42 +02:00
Ingo Franzki
f13d53517e PKCSCONF: Don't accept negative slot values
Slot numbers are from 0 up to the max number of slots supported.
A negative slot number indicates a failure of get_slot().

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:42 +02:00
Ingo Franzki
ed91c6f47e PKCSCONF: Fix integer overflows and NULL pointer checks
When getting the slot list and the mechanism list, check the number of
entries to not overflow when doing the calculation for the allocation size.

Also check if the allocation worked, and handle the situation if it failed.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:42 +02:00
Ingo Franzki
06502c9fb8 PKCSCONF: Fix unbound string copy for label
While the read logic ensures that the string read is at max 32 characters
plus a NUL byte, with or without '\n' at the end, the subsequent memcpy()
uses strlen() as length argument which is fragile. Use strnlen() with
size of the target buffer as the limiting value to ensure it will never
copy more bytes than the target buffer can hold.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:42 +02:00
Ingo Franzki
3b287144b2 COMMON: Restrict token name and config name to allowed characters
Restrict the token name (tokname) and config name (confname) that is
specified in opencryptoki.conf to only contain certain characters.
They must not be '.' or '..', and must not contain '/'.
If the confname specified an absolute path, no restriction is applied.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:41 +02:00
Ingo Franzki
02a5eed4da PKCSSLOTD: Restrict token name and config name to allowed characters
Restrict the token name (tokname) and config name (confname) that is
specified in opencryptoki.conf to only contain certain characters.
They must not be '.' or '..', and must not contain '/'.
If the confname specified an absolute path, no restriction is applied.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:41 +02:00
Ingo Franzki
27caf003cd PKCSSLOTD: Change variable 'uid' to type uid_t, not int
Should not make a difference in reality, but use the designated uid_t type.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:41 +02:00
Ingo Franzki
ba5e49edd0 PKCSSLOTD: Avoid NULL pointer dereference in slotdGenericSignalHandler
In case signal is received before the shared memory was attached, or after it
has been detached, shmp is NULL and dereferencing it causes a crash.
Ignore the signal if shmp is NULL and exit early.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:41 +02:00
Ingo Franzki
4c70031063 PKCSSLOTD: Add error checking in XProcLock()
Handle the case when flock fails. This can realistically never happen, but
it is still safer to handle the error.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:40 +02:00
Ingo Franzki
cad461fb62 PKCSSLOTD: Avoid underflow of pGlobalRWSessions in CheckForGarbage
If *pProcRWSessions is greater than *pGlobalRWSessions, then this indicates
an inconsistency. Set *pGlobalRWSessions to zero instead of subtracting
*pProcRWSessions which causes an underflow.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:40 +02:00
Ingo Franzki
fdda5c8539 PKCSSLOTD: Fix error handling of shmat()
Function shmat() returns -1 in case of an error, not NULL. Fix the check
by checking for -1.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:40 +02:00
Ingo Franzki
899173b27f PKCSSLOTD: Simplify copying slot info in PopulateSlotInfo()
Instead of copying each member separately, copy the entire struct at once.
This also fixes a bug where the string fields uses memcpy with strlen,
which does not copy the trailing NUL character. The target struct is
zeroed before, so there anyway is a NUL character, but that is subtle.

While at it remove the unused field shm_addr.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:40 +02:00
Ingo Franzki
4bd6a736f1 PKCSSLOTD: Fix NULL pointer dereference
When reading /proc/<pid>/stat the content is scanned for a closing bracket.
If no closing bracket is found strrchr() returns NULL and the subsequent
write causes a NULL pointer dereference.

Check for NULL and return an error.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:39 +02:00
Ingo Franzki
2cfcc67cfa PKCSSLOTD: Fix memory leak in error paths of compute_sha256()
The EVP_MD_CTX is not freed when any of the steps fail. Rework the code
to use a common cleanup label.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:39 +02:00
Ingo Franzki
be484acefa API: Fix trace message about pid, uid, and gid vs the real ones
It prints the real_pid 3 times and the order of the gid values is wrong.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:39 +02:00
Ingo Franzki
1a39b5487a API: Fix a race condition when opening sessions
Assign apiSessp->slotID and apiSessp->rw_session before adding the new
session to the global session list. Doing so afterwards leaves a small
window where the new session is already added to the global session list,
but these fields are not yet assigned. This could affect closing all
sessions of slot 0, incorrectly closing the newly added session
because the slot ID is not yet set and thus 0.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:38 +02:00
Ingo Franzki
58eb9cd409 API: Prevent global session counters from overflowing
Fail opening additional sessions if the global session counters would
overflow.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:38 +02:00
Ingo Franzki
7fe542b48e POLICY: Prevent unchecked bit-shift when parsing PRFs, MGFs, and KDFs
Limit the possible PRF values to 63 or 31 (dependent on the architecture),
to avoid undefined behavior with too large shift amounts.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:38 +02:00
Ingo Franzki
8d2c71c651 API: Limit the event payload size to EVENT_MAX_PAYLOAD_LENGTH
Don't accept an event with a payload larger than EVENT_MAX_PAYLOAD_LENGTH
(128K).

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:38 +02:00
Ingo Franzki
596b1209fe CONFIG: Prevent number overflow in confignode_dumpnnn
Limit the indent variables before they wrap.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:37 +02:00
Ingo Franzki
1572f64f72 CONFIG: Change line number field in ConfigBaseNode to 32 bit
Very large config files can theoretically be larger than 65535 lines long.
This would cause the line number to wrap to low values. There is no good
reason to use a short int for the line number.

Also update several printf format string to match the new type.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:37 +02:00
Ingo Franzki
b9b37e814d CONFIG: White-space fix only, for better readability
Correct the indentation of some lines, and remove the semicolon after the braces.
No functional change.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:37 +02:00
Ingo Franzki
c013b81664 CONFIG: Clarify CT_HAS_KEY_MASK usage
Update the comment to add a missing excluded type. Remove duplicate
CT_STRINGVAL from mask. Add safety checks for key being NULL in
confignode_find() and confignode_findidx().

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:37 +02:00
Ingo Franzki
248e0e8d03 POLICY: Fix NULL pointer dereference on empty config file
In case an empty config file is parsed for policy.conf or strength.conf
the version check might cause a NULL pointer dereference.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:36 +02:00
Ingo Franzki
20816cdfcc CONFIG: Add error detection for parsing values
Parsing values might return invalid data in case of number format
or strdup errors. Add error checking.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:36 +02:00
Ingo Franzki
2a60521768 PKCSHSM_MK_CHANGE: Fix memory leak on error path
When C_GetTokenInfo() fails, slots is not freed.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:36 +02:00
Ingo Franzki
862ee9bdbc PKCSHSM_MK_CHANGE: Remove dead check
The apqns parameter can not be NULL here. First, it was already used above, and
second getopt_long guarantees that options with required arguments have a
non-NULL argument.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:36 +02:00
Ingo Franzki
8c9156aa1d HSM_MK_CHANGE: Fix file name match on hsm_mk_change_op_remove()
Use a stricter prefix check when removing file that belong to the current
operation. Remove files which are named either exactly like the operation
ID, or like the ID followed by a dash. This prevents removing files who's
name start with the ID, but still do not belong to that operation.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:35 +02:00
Ingo Franzki
e6c48e6095 HSM_MK_CHANGE: Prevent reuse of already closed file descriptor
When hsm_mk_change_lock_create() fails to set permissions or the owner of the
just opened lock file, it closes the lock file, but does not set the file
descriptor to -1. This might cause subsequent calls from reusing the file
descriptor, which might now belong to another file.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:35 +02:00
Ingo Franzki
baeffbd872 HSM_MK_CHANGE: Fix memory leak on error in hsm_mk_change_token_mkvps_load()
In case hsm_mk_change_mkvps_unflatten() succeeded, but did not read the
expected amount of bytes, free the mkvp array, not only clean it.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:35 +02:00
Ingo Franzki
c73bd7778c HSM_MK_CHANGE: Prevent symlink-following in hsm_mk_change_op_remove/iterate
Using scandir and then removing the files on a full path basis is not
symlink-following safe. While the remove() as such only removes the link,
but not the target, the scandir() call might follow symlinks, if the
directory to scan is already a symlink.

Change this to use opendir_nofollow() with a readdir() loop and
unlinkat() operating relatively on the directory file handle.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:35 +02:00
Ingo Franzki
2362270216 HSM_MK_CHANGE: Fix byte ordering conversion when loading operations
On loading, convert from BE to host order.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:34 +02:00
Ingo Franzki
52496c2ec8 HSM_MK_CHANGE: Prevent integer overflows in flatten/unflatten code
Check the sizes before doing the calculations.

These checks are mostly relevant when building on 32 bit
architectures, the overflows can not happen on 64 bit
architectures. Silence compiler warnings about that
using diagnostic pragmas.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:34 +02:00
Ingo Franzki
767ad03ad6 HSM_MK_CHANGE: Check length of read data before using it
The number of read bytes must be at least large enough to hold the header,
and can not be empty.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:34 +02:00
Ingo Franzki
4e90185817 PKCSICSF: Let lookup_name()/list_tokens() receive full pages on each iteration
Reset tokenCount to MAX_RECORDS on each iteration, to be able to receive a
full page of records, not only the amount of records that the last
iteration retrieved.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:34 +02:00
Ingo Franzki
24dbc97561 PKCSICSF/ICSF: Restrict token name to allowed characters
The ICSF token name is retrieved from LPAP, and could theoretically contain
characters like '..' or '/' that could cause the config file name to be
created outside of the config directory. The token name must not be '.' or
'..', and must not contain '/'.

Create relative names for the confname field in the config entry.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:33 +02:00
Ingo Franzki
6c7800836f PKCSICSF: Don't create an inconsistent config if adding a token fails
If adding the token fails, don't add it to the configuration, but return an
error immediately. Also if adding to the configuration fails, don't just
continue but fail.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:33 +02:00
Ingo Franzki
1fc0246062 PKCSICSF: Unify mech checking
Check the flags, like it is done everywhere else, no string compare of the
mech variable.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:33 +02:00
Ingo Franzki
2d660fe73d PKCSICSF: Check RACF password length and issue user friendly error
The RACF password can only up to PIN_SIZE -1 bytes (79 bytes). Issue
a user friendly error message if it is too long.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:32 +02:00
Ingo Franzki
ec22ea8c9c PKCSICSF: Fix dead assignment
Setting the pointer parameter to NULL has no effect. Instead clear the
passed record in case no one was found.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:32 +02:00
Ingo Franzki
1b54b5d24a PKCSICSF: Fix parameter type mismatch in secure_racf_passwd()
Use CK_ULONG instead of unsigned int.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:32 +02:00
Ingo Franzki
dd379229ec PKCSICSF: Cleanse sensitive data on stack
Cleanse the randomly generated master key on the stack.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2026-08-19 08:27:32 +02:00