src/jdk.management.agent/windows/native/libmanagement_agent/FileSystemImpl.c
author cgo
Thu, 28 Nov 2019 16:11:44 +0100
changeset 59308 70f49f08c9e6
parent 47613 af241e3e5a13
permissions -rw-r--r--
8234894: [TESTBUG] TestEliminateLocksOffCrash fails for client VMs due to Unrecognized VM option EliminateLocks Reviewed-by: thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 9027
diff changeset
     2
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jni_util.h"
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    32
#include "jdk_internal_agent_FileSystemImpl.h"
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    33
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    34
JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    35
{
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    36
    JNIEnv* env;
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    37
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    38
    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    39
        return JNI_EVERSION; /* JNI version not supported */
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    40
    }
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    41
47613
af241e3e5a13 8188052: JNI FindClass needs to specify the class loading context used for library lifecycle hooks
mchung
parents: 47216
diff changeset
    42
    return JNI_VERSION_10;
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    43
}
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Access mask to represent any file access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define ANY_ACCESS (FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Returns JNI_TRUE if the specified file is on a file system that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * persistent ACLs (On NTFS file systems returns true, on FAT32 file systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * returns false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static jboolean isSecuritySupported(JNIEnv* env, const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    char* root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    char* p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    BOOL res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    DWORD dwMaxComponentLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    DWORD dwFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    char fsName[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    DWORD fsNameLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Get root directory. Assume that files are absolute paths. For UNCs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * the slash after the share name is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    root = strdup(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (*root == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * \\server\share\file ==> \\server\share\
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int slashskip = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        p = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        while ((*p == '\\') && (slashskip > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            char* p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            p2 = strchr(p, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if ((p2 == NULL) || (*p2 != '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                JNU_ThrowIOException(env, "Malformed UNC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            p = p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            slashskip--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (slashskip != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            JNU_ThrowIOException(env, "Malformed UNC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        p = strchr(root, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (p == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            JNU_ThrowIOException(env, "Absolute filename not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Get the volume information - this gives us the file system file and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * also tells us if the file system supports persistent ACLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    fsNameLength = sizeof(fsName)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    res = GetVolumeInformation(root,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                               NULL,        // address of name of the volume, can be NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                               0,           // length of volume name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                               NULL,        // address of volume serial number, can be NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                               &dwMaxComponentLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                               &dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                               fsName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                               fsNameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        JNU_ThrowIOExceptionWithLastError(env, "GetVolumeInformation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    free(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    return (dwFlags & FS_PERSISTENT_ACLS) ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Returns the security descriptor for a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
static SECURITY_DESCRIPTOR* getFileSecurityDescriptor(JNIEnv* env, const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    SECURITY_DESCRIPTOR* sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    DWORD len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    SECURITY_INFORMATION info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   141
    GetFileSecurityA(path, info , 0, 0, &len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        JNU_ThrowIOExceptionWithLastError(env, "GetFileSecurity failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    sd = (SECURITY_DESCRIPTOR *)malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    if (sd == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    } else {
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   150
        if (!(*GetFileSecurityA)(path, info, sd, len, &len)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            JNU_ThrowIOExceptionWithLastError(env, "GetFileSecurity failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            free(sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Returns pointer to the SID identifying the owner of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
static SID* getFileOwner(JNIEnv* env, SECURITY_DESCRIPTOR* sd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    SID* owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    BOOL defaulted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   167
    if (!GetSecurityDescriptorOwner(sd, &owner, &defaulted)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        JNU_ThrowIOExceptionWithLastError(env, "GetSecurityDescriptorOwner failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * Returns pointer discretionary access-control list (ACL) from the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * descriptor of the specified file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
static ACL* getFileDACL(JNIEnv* env, SECURITY_DESCRIPTOR* sd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    ACL *acl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    int defaulted, present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   182
    if (!GetSecurityDescriptorDacl(sd, &present, &acl, &defaulted)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        JNU_ThrowIOExceptionWithLastError(env, "GetSecurityDescriptorDacl failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    if (!present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        JNU_ThrowInternalError(env, "Security descriptor does not contain a DACL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    return acl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * Returns JNI_TRUE if the specified owner is the only SID will access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
static jboolean isAccessUserOnly(JNIEnv* env, SID* owner, ACL* acl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    ACL_SIZE_INFORMATION acl_size_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * If there's no DACL then there's no access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    if (acl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Get the ACE count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   211
    if (!GetAclInformation(acl, (void *) &acl_size_info, sizeof(acl_size_info),
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   212
                           AclSizeInformation)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        JNU_ThrowIOExceptionWithLastError(env, "GetAclInformation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Iterate over the ACEs. For each "allow" type check that the SID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * matches the owner, and check that the access is read only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    for (i = 0; i < acl_size_info.AceCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        void* ace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        ACCESS_ALLOWED_ACE *access;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        SID* sid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   226
        if (!GetAce(acl, i, &ace)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            JNU_ThrowIOExceptionWithLastError(env, "GetAce failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (((ACCESS_ALLOWED_ACE *)ace)->Header.AceType != ACCESS_ALLOWED_ACE_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        access = (ACCESS_ALLOWED_ACE *)ace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        sid = (SID *) &access->SidStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (!EqualSid(owner, sid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             * If the ACE allows any access then the file is not secure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (access->Mask & ANY_ACCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
/*
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
   249
 * Class:     jdk_internal_agent_FileSystemImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * Method:    init0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 */
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
   253
JNIEXPORT void JNICALL Java_jdk_internal_agent_FileSystemImpl_init0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
  (JNIEnv *env, jclass ignored)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
{
9027
540bd52f1881 7030249: Eliminate use of LoadLibrary and other clean-ups
alanb
parents: 5506
diff changeset
   256
        /* nothing to do */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
/*
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
   260
 * Class:     jdk_internal_agent_FileSystemImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * Method:    isSecuritySupported0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * Signature: (Ljava/lang/String;)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 */
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
   264
JNIEXPORT jboolean JNICALL Java_jdk_internal_agent_FileSystemImpl_isSecuritySupported0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
  (JNIEnv *env, jclass ignored, jstring str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    jboolean res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    const char* path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    path = JNU_GetStringPlatformChars(env, str, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    if (path != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        res = isSecuritySupported(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            JNU_ReleaseStringPlatformChars(env, str, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        /* exception thrown - doesn't matter what we return */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
/*
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
   286
 * Class:     jdk_internal_agent_FileSystemImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * Method:    isAccessUserOnly0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * Signature: (Ljava/lang/String;)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 */
43494
11801b2ff456 8173608: Separate JDK management agent from java.management module
mchung
parents: 25859
diff changeset
   290
JNIEXPORT jboolean JNICALL Java_jdk_internal_agent_FileSystemImpl_isAccessUserOnly0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  (JNIEnv *env, jclass ignored, jstring str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    jboolean res = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    const char* path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    path = JNU_GetStringPlatformChars(env, str, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    if (path != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * From the security descriptor get the file owner and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * DACL. Then check if anybody but the owner has access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        SECURITY_DESCRIPTOR* sd = getFileSecurityDescriptor(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (sd != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            SID *owner = getFileOwner(env, sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (owner != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                ACL* acl = getFileDACL(env, sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if (acl != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    res = isAccessUserOnly(env, owner, acl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                     * If acl is NULL it means that an exception was thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                     * or there is "all acess" to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    res = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            free(sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            JNU_ReleaseStringPlatformChars(env, str, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
}