Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

ncrypt: serialize no-overwrite finalizes on a lock file, unique temp per finalization

mikey9210c986531b3b
diff --git a/patches/ncrypt-persisted-keys.patch b/patches/ncrypt-persisted-keys.patch
index e14b6b2d4b6..0e0228f44b9 100644
--- a/patches/ncrypt-persisted-keys.patch
+++ b/patches/ncrypt-persisted-keys.patch
@@ -63,7 +63,7 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
#include <stdlib.h>
#include "ntstatus.h"
-@@ -126,6 +127,275 @@
+@@ -126,6 +127,273 @@
return ERROR_SUCCESS;
}
@@ -77,6 +77,8 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
+
+#define KEY_STORE_MAGIC 0x314b4a53
+
++static LONG key_store_seq;
++
+#ifndef NCRYPT_OVERWRITE_KEY_FLAG
+#define NCRYPT_OVERWRITE_KEY_FLAG 0x00000080
+#endif
@@ -170,11 +172,13 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
+
+ if (!(blob_type = blob_type_for_algid(algid))) return NTE_NOT_SUPPORTED;
+ if (!(path = key_store_path(name))) return NTE_INTERNAL_ERROR;
-+ /* Write next to the final name and rename over it, so a crash or a full
-+ * disk mid-write cannot leave a truncated key behind: that would fail to
-+ * open (NTE_BAD_KEYSET) yet count as existing (NTE_EXISTS) forever. */
-+ if (!(tmp = malloc((lstrlenW(path) + 24) * sizeof(WCHAR)))) { ret = NTE_NO_MEMORY; goto done; }
-+ swprintf(tmp, lstrlenW(path) + 24, L"%s.%u.tmp", path, (unsigned int)GetCurrentProcessId());
++ /* Write next to the final name under a name unique to this finalization
++ * and rename into place, so a crash or a full disk mid-write cannot leave
++ * a truncated key behind (it would fail to open yet count as existing),
++ * and two finalizations of one name never share a temp file. */
++ if (!(tmp = malloc((lstrlenW(path) + 40) * sizeof(WCHAR)))) { ret = NTE_NO_MEMORY; goto done; }
++ swprintf(tmp, lstrlenW(path) + 40, L"%s.%u-%u.tmp", path, (unsigned int)GetCurrentProcessId(),
++ (unsigned int)InterlockedIncrement(&key_store_seq)); /* unique per finalization, threads included */
+
+ status = BCryptExportKey(bcrypt_key, NULL, blob_type, NULL, 0, &blob_len, 0);
+ if (status != STATUS_SUCCESS) goto done;
@@ -274,50 +278,44 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
+/* Move a finished temp file onto the key's final name.
+ *
+ * With NCRYPT_OVERWRITE_KEY_FLAG this is a plain replacing rename. Without it
-+ * the rename must not replace a key another process finalized since the
++ * the file must not replace a key another finalizer installed since the
+ * creation-time check: two launches racing to create the same key would
+ * otherwise both succeed and the second would silently discard the first's
-+ * identity. A no-replace rename is atomic, so the only question is what to do
-+ * when something is already there. Checking it in place and then replacing
-+ * would be a window in which a third party can install a good key we then
-+ * destroy; instead the file is first moved aside under a name only this
-+ * process uses (atomic, exclusive), examined there, and put back if it turns
-+ * out to be a readable key. Only a damaged file is discarded, after which the
-+ * no-replace rename is tried once more. */
++ * identity. Deciding that needs a look at what is at the destination, and the
++ * look and the rename have to be one step, so finalizers of a name serialize
++ * on <path>.lock, opened with no sharing and delete-on-close: whoever holds
++ * it can examine the destination, discard a damaged file, and install its
++ * own, with nobody else able to install in between. The lock file disappears
++ * when its handle closes, including on process death. */
+static SECURITY_STATUS install_key_file(const WCHAR *tmp, const WCHAR *path, BOOL overwrite)
+{
-+ WCHAR *aside;
++ SECURITY_STATUS ret = ERROR_SUCCESS;
++ HANDLE lock = INVALID_HANDLE_VALUE;
++ WCHAR *lockname;
+ int attempt;
+
+ if (overwrite)
+ return MoveFileExW(tmp, path, MOVEFILE_REPLACE_EXISTING) ? ERROR_SUCCESS : NTE_INTERNAL_ERROR;
+
-+ for (attempt = 0; attempt < 2; attempt++)
++ if (!(lockname = malloc((lstrlenW(path) + 6) * sizeof(WCHAR)))) return NTE_NO_MEMORY;
++ lstrcpyW(lockname, path);
++ lstrcatW(lockname, L".lock");
++ for (attempt = 0; attempt < 200; attempt++) /* up to ~2 s */
+ {
-+ if (MoveFileExW(tmp, path, 0)) return ERROR_SUCCESS;
-+ if (GetLastError() != ERROR_ALREADY_EXISTS) return NTE_INTERNAL_ERROR;
-+
-+ if (!(aside = malloc((lstrlenW(path) + 24) * sizeof(WCHAR)))) return NTE_NO_MEMORY;
-+ swprintf(aside, lstrlenW(path) + 24, L"%s.%u.aside", path, (unsigned int)GetCurrentProcessId());
-+ if (!MoveFileExW(path, aside, 0))
-+ {
-+ DWORD err = GetLastError();
-+ free(aside);
-+ if (err == ERROR_FILE_NOT_FOUND) continue; /* vanished under us: try the rename again */
-+ return NTE_INTERNAL_ERROR;
-+ }
-+ if (key_file_readable(aside))
-+ {
-+ /* A real key won the race. Put it back; if that fails another
-+ * finalizer installed one meanwhile, and that one stands. */
-+ if (!MoveFileExW(aside, path, 0)) DeleteFileW(aside);
-+ free(aside);
-+ return NTE_EXISTS;
-+ }
-+ DeleteFileW(aside);
-+ free(aside);
++ lock = CreateFileW(lockname, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
++ FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
++ if (lock != INVALID_HANDLE_VALUE) break;
++ if (GetLastError() != ERROR_SHARING_VIOLATION && GetLastError() != ERROR_ACCESS_DENIED) break;
++ Sleep(10);
+ }
-+ return NTE_EXISTS;
++ free(lockname);
++ if (lock == INVALID_HANDLE_VALUE) return NTE_INTERNAL_ERROR;
++
++ if (key_file_readable(path)) ret = NTE_EXISTS; /* a real key won the race */
++ else if (!MoveFileExW(tmp, path, MOVEFILE_REPLACE_EXISTING)) ret = NTE_INTERNAL_ERROR; /* absent or damaged: ours goes in */
++
++ CloseHandle(lock);
++ return ret;
+}
+
+/* A stored key counts as existing only if it can be read back and imported.
@@ -339,7 +337,7 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
static struct object *create_key_object(enum algid algid, NCRYPT_PROV_HANDLE provider)
{
NCRYPT_SUPPORTED_LENGTHS supported_lengths = {512, 16384, 8, 1024};
-@@ -150,6 +420,26 @@
+@@ -150,6 +418,26 @@
set_object_property(object, BCRYPT_SIGNATURE_LENGTH, (BYTE *)&dw_value, sizeof(dw_value));
break;
@@ -366,7 +364,7 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
default:
ERR("Invalid algid %#x\n", algid);
return NULL;
-@@ -175,7 +465,7 @@
+@@ -175,7 +463,7 @@
if (!provider) return NTE_INVALID_HANDLE;
if (!algid) return HRESULT_FROM_WIN32(RPC_X_NULL_REF_POINTER);
@@ -375,10 +373,11 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
if (!lstrcmpiW(algid, BCRYPT_RSA_ALGORITHM))
{
-@@ -199,12 +489,40 @@
+@@ -198,6 +486,25 @@
+
set_object_property(object, NCRYPT_LENGTH_PROPERTY, (BYTE *)&default_bitlen, sizeof(default_bitlen));
set_object_property(object, BCRYPT_PUBLIC_KEY_LENGTH, (BYTE *)&default_bitlen, sizeof(default_bitlen));
- }
++ }
+ else if (!lstrcmpiW(algid, BCRYPT_ECDSA_P256_ALGORITHM) || !lstrcmpiW(algid, BCRYPT_ECDSA_P384_ALGORITHM))
+ {
+ enum algid id = !lstrcmpiW(algid, BCRYPT_ECDSA_P256_ALGORITHM) ? ECDSA_P256 : ECDSA_P384;
@@ -397,10 +396,10 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
+
+ set_object_property(object, NCRYPT_LENGTH_PROPERTY, (BYTE *)&bitlen, sizeof(bitlen));
+ set_object_property(object, BCRYPT_PUBLIC_KEY_LENGTH, (BYTE *)&bitlen, sizeof(bitlen));
-+ }
+ }
else
{
- FIXME("Algorithm not handled %s\n", wine_dbgstr_w(algid));
+@@ -205,6 +512,15 @@
return NTE_NOT_SUPPORTED;
}
@@ -416,7 +415,7 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
*key = (NCRYPT_KEY_HANDLE)object;
return ERROR_SUCCESS;
}
-@@ -219,8 +537,25 @@
+@@ -219,8 +535,25 @@
SECURITY_STATUS WINAPI NCryptDeleteKey(NCRYPT_KEY_HANDLE key, DWORD flags)
{
@@ -444,7 +443,7 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
}
SECURITY_STATUS WINAPI NCryptEncrypt(NCRYPT_KEY_HANDLE key, BYTE *input, DWORD insize, void *padding,
-@@ -277,11 +612,16 @@
+@@ -277,11 +610,16 @@
if (!(prop = get_object_property(object, NCRYPT_LENGTH_PROPERTY))) return NTE_INVALID_HANDLE;
key_length = *(DWORD *)prop->value;
@@ -465,12 +464,10 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
}
status = BCryptFinalizeKeyPair(object->key.bcrypt_key, 0);
-@@ -289,6 +629,20 @@
- {
- ERR("Error finalizing key pair\n");
+@@ -291,6 +629,20 @@
return map_ntstatus(status);
-+ }
-+
+ }
+
+ if (object->key.name)
+ {
+ /* Report a storage failure rather than hiding it: a caller told its
@@ -483,10 +480,12 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
+ WARN("could not store key %s\n", wine_dbgstr_w(object->key.name));
+ return ret;
+ }
- }
-
++ }
++
return ERROR_SUCCESS;
-@@ -302,6 +656,8 @@
+ }
+
+@@ -302,6 +654,8 @@
static SECURITY_STATUS free_key_object(struct key *key)
{
@@ -495,7 +494,7 @@ taken from its blob so a 2048 bit RSA key does not come back claiming 1024.
return map_ntstatus( BCryptDestroyKey(key->bcrypt_key) );
}
-@@ -512,8 +868,43 @@
+@@ -512,8 +866,43 @@
SECURITY_STATUS WINAPI NCryptOpenKey(NCRYPT_PROV_HANDLE provider, NCRYPT_KEY_HANDLE *key,
const WCHAR *name, DWORD keyspec, DWORD flags)
{