Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsSecurity.java
32288 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import static sun.nio.fs.WindowsNativeDispatcher.*;28import static sun.nio.fs.WindowsConstants.*;2930/**31* Security related utility methods.32*/3334class WindowsSecurity {35private WindowsSecurity() { }3637// opens process token for given access38private static long openProcessToken(int access) {39try {40return OpenProcessToken(GetCurrentProcess(), access);41} catch (WindowsException x) {42return 0L;43}44}4546/**47* Returns the access token for this process with TOKEN_DUPLICATE access48*/49static final long processTokenWithDuplicateAccess =50openProcessToken(TOKEN_DUPLICATE);5152/**53* Returns the access token for this process with TOKEN_QUERY access54*/55static final long processTokenWithQueryAccess =56openProcessToken(TOKEN_QUERY);5758/**59* Returned by enablePrivilege when code may require a given privilege.60* The drop method should be invoked after the operation completes so as61* to revert the privilege.62*/63static interface Privilege {64void drop();65}6667/**68* Attempts to enable the given privilege for this method.69*/70static Privilege enablePrivilege(String priv) {71final long pLuid;72try {73pLuid = LookupPrivilegeValue(priv);74} catch (WindowsException x) {75// indicates bug in caller76throw new AssertionError(x);77}7879long hToken = 0L;80boolean impersontating = false;81boolean elevated = false;82try {83hToken = OpenThreadToken(GetCurrentThread(),84TOKEN_ADJUST_PRIVILEGES, false);85if (hToken == 0L && processTokenWithDuplicateAccess != 0L) {86hToken = DuplicateTokenEx(processTokenWithDuplicateAccess,87(TOKEN_ADJUST_PRIVILEGES|TOKEN_IMPERSONATE));88SetThreadToken(0L, hToken);89impersontating = true;90}9192if (hToken != 0L) {93AdjustTokenPrivileges(hToken, pLuid, SE_PRIVILEGE_ENABLED);94elevated = true;95}96} catch (WindowsException x) {97// nothing to do, privilege not enabled98}99100final long token = hToken;101final boolean stopImpersontating = impersontating;102final boolean needToRevert = elevated;103104return () -> {105try {106if (token != 0L) {107try {108if (stopImpersontating)109SetThreadToken(0L, 0L);110else if (needToRevert)111AdjustTokenPrivileges(token, pLuid, 0);112} catch (WindowsException x) {113// should not happen114throw new AssertionError(x);115} finally {116CloseHandle(token);117}118}119} finally {120LocalFree(pLuid);121}122};123}124125/**126* Check the access right against the securityInfo in the current thread.127*/128static boolean checkAccessMask(long securityInfo, int accessMask,129int genericRead, int genericWrite, int genericExecute, int genericAll)130throws WindowsException131{132int privilegies = TOKEN_QUERY;133long hToken = OpenThreadToken(GetCurrentThread(), privilegies, false);134if (hToken == 0L && processTokenWithDuplicateAccess != 0L)135hToken = DuplicateTokenEx(processTokenWithDuplicateAccess,136privilegies);137138boolean hasRight = false;139if (hToken != 0L) {140try {141hasRight = AccessCheck(hToken, securityInfo, accessMask,142genericRead, genericWrite, genericExecute, genericAll);143} finally {144CloseHandle(hToken);145}146}147return hasRight;148}149150}151152153