From 8cb7e2c65ea86ee4c2c0845b2cff0defd2b7b1a6 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Fri, 7 Oct 2022 17:31:43 -0700 Subject: [PATCH] Add Advapi32Util#isCurrentProcessElevated and associated types Signed-off-by: Daniel Widdis --- CHANGES.md | 1 + .../sun/jna/platform/win32/Advapi32Util.java | 24 +++++++++++++++++++ .../src/com/sun/jna/platform/win32/WinNT.java | 8 +++++++ .../jna/platform/win32/Advapi32UtilTest.java | 12 ++++++++++ 4 files changed, 45 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 398b72a3b4..2d900ee918 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ Features -------- * [#1454](https://github.com/java-native-access/jna/pull/1454): Add `c.s.j.p.win32.Psapi.QueryWorkingSetEx` and associated Types - [@crain-32](https://github.com/Crain-32). * [#1459](https://github.com/java-native-access/jna/pull/1459): Add `VirtualLock` and `VirtualUnlock` in `c.s.j.p.win32.Kernel32` - [@matthiasblaesing](https://github.com/matthiasblaesing). +* [#1471](https://github.com/java-native-access/jna/pull/1471): Add `c.s.j.p.win32.Advapi32Util#isCurrentProcessElevated` and associated Types - [@dbwiddis](https://github.com/dbwiddis). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java index ddc1dc67d2..26dd22d256 100755 --- a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java @@ -86,6 +86,7 @@ import com.sun.jna.platform.win32.WinNT.SECURITY_IMPERSONATION_LEVEL; import com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES; import com.sun.jna.platform.win32.WinNT.SID_NAME_USE; +import com.sun.jna.platform.win32.WinNT.TOKEN_ELEVATION; import com.sun.jna.platform.win32.WinNT.TOKEN_TYPE; import com.sun.jna.platform.win32.WinReg.HKEY; import com.sun.jna.platform.win32.WinReg.HKEYByReference; @@ -3414,4 +3415,27 @@ private HANDLE getThreadToken() throws Win32Exception { return phThreadToken.getValue(); } } + + /** + * Reports whether the current process is running with elevated permissions. + * + * @return true if the current process has elevated perissions, false otherwise. + */ + public static boolean isCurrentProcessElevated() { + HANDLEByReference hToken = new HANDLEByReference(); + IntByReference returnLength = new IntByReference(); + if (Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_QUERY, + hToken)) { + try { + TOKEN_ELEVATION elevation = new TOKEN_ELEVATION(); + if (Advapi32.INSTANCE.GetTokenInformation(hToken.getValue(), + WinNT.TOKEN_INFORMATION_CLASS.TokenElevation, elevation, elevation.size(), returnLength)) { + return elevation.TokenIsElevated > 0; + } + } finally { + Kernel32.INSTANCE.CloseHandle(hToken.getValue()); + } + } + return false; + } } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java index f31f0335c1..639dfcf2b1 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java @@ -4415,4 +4415,12 @@ public IO_COUNTERS(Pointer memory) { public int EVENT_MODIFY_STATE = 0x0002; public int EVENT_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3); + + /** + * The TOKEN_ELEVATION structure indicates whether a token has elevated privileges. + */ + @FieldOrder({ "TokenIsElevated" }) + class TOKEN_ELEVATION extends Structure { + public int TokenIsElevated; + } } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java b/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java index 2297d2c380..39c19eacce 100755 --- a/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java @@ -915,6 +915,18 @@ public void testPrivilege() { } } + /** + * Test TOKEN_ELEVATION structure + */ + public void testIsCurrentProcessElevated() { + // This is either true if we're elevated or false otherwise. Just exercising the function. + try { + Advapi32Util.isCurrentProcessElevated(); + } catch (Exception ex) { + fail("Encountered unknown exception - " + ex.getMessage()); + } + } + private File createTempFile() throws Exception{ String filePath = System.getProperty("java.io.tmpdir") + System.nanoTime() + ".text";