Compare commits

...

1 Commits

Author SHA1 Message Date
Luca Bacci
e4aa58b4c0 GdkWin32: Add Utility functions 2024-10-10 15:04:24 +02:00
2 changed files with 144 additions and 0 deletions

View File

@@ -26,6 +26,7 @@
#include <io.h>
#include "gdk.h"
#include "gdkmain-win32.h"
#include "gdkdebugprivate.h"
#include "gdkkeysyms.h"
#include <glib/gi18n-lib.h>
@@ -39,6 +40,8 @@
#include <wintab.h>
#include <imm.h>
#include <stdbool.h>
/* Whether GDK initialized COM */
gboolean
gdk_win32_ensure_com (void)
@@ -106,6 +109,110 @@ gdk_win32_ensure_ole (void)
return ole_initialized;
}
/** gdk_win32_check_app_container:
*
* Check if running sandboxed in an app container
*/
bool
gdk_win32_check_app_container (void)
{
HANDLE token_handle = NULL;
DWORD is_app_container = 0;
DWORD size = sizeof (is_app_container);
bool result = false;
/* Since Windows 8: use GetCurrentProcessToken() and remove the call to CloseHandle() */
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token_handle))
{
WIN32_API_FAILED ("OpenProcessToken");
return false;
}
if (!GetTokenInformation (token_handle, TokenIsAppContainer, &is_app_container, size, &size))
WIN32_API_FAILED ("GetTokenInformation");
else if (size > 0)
result = !!is_app_container;
CloseHandle (token_handle);
return result;
}
/** gdk_win32_check_high_integrity:
*
* Check if the app is running with high integrity
*
* Code based on:
* https://devblogs.microsoft.com/oldnewthing/20221017-00/?p=107291
* https://github.com/microsoft/WindowsAppSDK/blob/main/dev/Common/Security.IntegrityLevel.h
*/
bool
gdk_win32_check_high_integrity (void)
{
HANDLE token_handle = NULL;
TOKEN_MANDATORY_LABEL integrity_level;
DWORD size = sizeof (integrity_level);
bool result = false;
/* Since Windows 8: use GetCurrentProcessToken() and remove the call to CloseHandle() */
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token_handle))
{
WIN32_API_FAILED ("OpenProcessToken");
return false;
}
memset (&integrity_level, 0, sizeof (integrity_level));
if (!GetTokenInformation (token_handle, TokenIntegrityLevel, &integrity_level, size, &size))
WIN32_API_FAILED ("GetTokenInformation");
else if (size > 0 && IsValidSid (integrity_level.Label.Sid))
{
UCHAR subauthority_count = *GetSidSubAuthorityCount (integrity_level.Label.Sid);
if (subauthority_count > 0)
{
DWORD level = *GetSidSubAuthority (integrity_level.Label.Sid, subauthority_count - 1);
result = (level >= SECURITY_MANDATORY_HIGH_RID);
}
}
CloseHandle (token_handle);
return result;
}
/** gdk_win32_check_manually_elevated:
*
* Code based on:
* https://devblogs.microsoft.com/oldnewthing/20241003-00/?p=110336
*/
bool
gdk_win32_check_manually_elevated (void)
{
HANDLE token_handle = NULL;
TOKEN_ELEVATION_TYPE elevation_type = TokenElevationTypeDefault;
DWORD size = sizeof (elevation_type);
bool result = false;
/* Since Windows 8: use GetCurrentProcessToken() and remove the call to CloseHandle() */
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token_handle))
{
WIN32_API_FAILED ("OpenProcessToken");
return false;
}
if (!GetTokenInformation (token_handle, TokenElevationType, &elevation_type, size, &size))
WIN32_API_FAILED ("GetTokenInformation");
else if (size > 0)
result = (elevation_type == TokenElevationTypeFull);
CloseHandle (token_handle);
return result;
}
void
_gdk_win32_api_failed (const char *where,
const char *api)

37
gdk/win32/gdkmain-win32.h Normal file
View File

@@ -0,0 +1,37 @@
/* GTK - The GIMP Toolkit
*
* Copyright (C) 2024 the GTK team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdbool.h>
typedef enum {
OSVersionWindows7,
OSVersionWindows8,
OSVersionWindows8_1,
OSVersionWindows10,
OSVersionWindows11,
} OSVersion;
bool gdk_win32_check_app_container (void);
bool gdk_win32_check_high_integrity (void);
bool gdk_win32_check_manually_elevated (void);
OSVersion gdk_win32_get_os_version (void);