src/jdk.dynalink/share/classes/jdk/dynalink/SecureLookupSupplier.java
author mbaesken
Tue, 02 Jul 2019 13:45:08 +0200
changeset 55556 19d0b382f086
parent 47216 71c04702a3d5
permissions -rw-r--r--
8226238: Improve error output and fix elf issues in os::dll_load Reviewed-by: clanger, mdoerr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     1
/*
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     4
 *
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    10
 *
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    15
 * accompanied this code).
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    16
 *
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    20
 *
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    23
 * questions.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    24
 */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    25
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    26
package jdk.dynalink;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    27
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    28
import java.lang.invoke.MethodHandles;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    29
import java.lang.invoke.MethodHandles.Lookup;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    30
import java.util.Objects;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    31
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    32
/**
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    33
 * Provides security-checked access to a {@code MethodHandles.Lookup} object.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    34
 * See {@link #getLookup()} for details.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    35
 */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    36
public class SecureLookupSupplier {
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    37
    /**
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    38
     * The name of a runtime permission required to successfully invoke the
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    39
     * {@link #getLookup()} method.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    40
     */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    41
    public static final String GET_LOOKUP_PERMISSION_NAME = "dynalink.getLookup";
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    42
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    43
    private static final RuntimePermission GET_LOOKUP_PERMISSION = new RuntimePermission(SecureLookupSupplier.GET_LOOKUP_PERMISSION_NAME);
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    44
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    45
    private final MethodHandles.Lookup lookup;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    46
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    47
    /**
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    48
     * Creates a new secure lookup supplier, securing the passed lookup.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    49
     * @param lookup the lookup to secure. Can not be null.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    50
     * @throws NullPointerException if null is passed.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    51
     */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    52
    public SecureLookupSupplier(final MethodHandles.Lookup lookup) {
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    53
        this.lookup = Objects.requireNonNull(lookup, "lookup");
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    54
    }
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    55
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    56
    /**
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    57
     * Returns the lookup secured by this {@code SecureLookupSupplier}.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    58
     * @return the lookup secured by this {@code SecureLookupSupplier}.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    59
     * @throws SecurityException if the secured lookup isn't the
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    60
     * {@link MethodHandles#publicLookup()}, and a security manager is present,
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    61
     * and a check for {@code RuntimePermission("dynalink.getLookup")} fails.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    62
     */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    63
    public final Lookup getLookup() {
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    64
        final SecurityManager sm = System.getSecurityManager();
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    65
        if (sm != null && lookup != MethodHandles.publicLookup()) {
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    66
            sm.checkPermission(GET_LOOKUP_PERMISSION);
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    67
        }
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    68
        return lookup;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    69
    }
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    70
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    71
    /**
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    72
     * Returns the value of {@link #getLookup()} without a security check. Can
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    73
     * be used by subclasses to access the lookup quickly.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    74
     * @return same as returned value of {@link #getLookup()}.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    75
     */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    76
    protected final Lookup getLookupPrivileged() {
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    77
        return lookup;
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    78
    }
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
diff changeset
    79
}