1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr;
  27 
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Objects;
  32 
  33 import jdk.jfr.internal.PlatformEventType;
  34 import jdk.jfr.internal.PlatformRecording;
  35 import jdk.jfr.internal.PrivateAccess;
  36 import jdk.jfr.internal.Type;
  37 import jdk.jfr.internal.Utils;
  38 
  39 /**
  40  * Permission for controlling access to Flight Recorder.
  41  * <p>
  42  * The following table provides a summary description of what the permission
  43  * allows, and discusses the risks of granting code the permission.
  44  *
  45  * <table class="striped">
  46  * <caption style="display:none">Table shows permission target name,
  47  *      what the permission allows, and associated risks</caption>
  48  * <thead>
  49  * <tr>
  50  * <th scope="col">Permission Target Name</th>
  51  * <th scope="col">What the Permission Allows</th>
  52  * <th scope="col">Risks of Allowing this Permission</th>
  53  * </tr>
  54  * </thead>
  55  *
  56  * <tbody>
  57  * <tr>
  58  * <th scope="row">accessFlightRecorder</th>
  59  * <td>Ability to create a Flight Recorder instance, register callbacks to
  60  * monitor Flight Recorder's life cycle and control an already existing instance
  61  * of Flight Recorder, which can record and dump runtime information, such as
  62  * stack traces, class names and data in user defined events.</td>
  63  * <td>A malicious user may be able to extract sensitive information stored in
  64  * events and interrupt Flight Recorder by installing listeners or hooks that
  65  * never finishes.</td>
  66  * </tr>
  67  * <tr>
  68  * <th scope="row">registerEvent</th>
  69  * <td>Ability to register events, write data to the Flight Recorder buffers
  70  * and execute code in a callback function for periodic events.
  71  *
  72  * <td>A malicious user may be able to write sensitive information to Flight
  73  * Recorder buffers.</td>
  74  * </tr>
  75  * </tbody>
  76  * </table>
  77  *
  78  * <p>
  79  * Programmers do not normally create {@code FlightRecorderPermission} objects
  80  * directly. Instead they are created by the security policy code based on
  81  * reading the security policy file.
  82  *
  83  * @since 9
  84  *
  85  * @see java.security.BasicPermission
  86  * @see java.security.Permission
  87  * @see java.security.Permissions
  88  * @see java.security.PermissionCollection
  89  * @see java.lang.SecurityManager
  90  *
  91  */
  92 @SuppressWarnings("serial")
  93 public final class FlightRecorderPermission extends java.security.BasicPermission {
  94 
  95     // Purpose of InternalAccess is to give classes in jdk.jfr.internal
  96     // access to package private methods in this package (jdk.jfr).
  97     //
  98     // The initialization could be done in any class in this package,
  99     // but this one was chosen because it is light weight and
 100     // lacks dependencies on other public classes.
 101     static {
 102         PrivateAccess.setPrivateAccess(new InternalAccess());
 103     }
 104 
 105     private final static class InternalAccess extends PrivateAccess {
 106 
 107         @Override
 108         public Type getType(Object o) {
 109             if (o instanceof AnnotationElement) {
 110                 return ((AnnotationElement) o).getType();
 111             }
 112             if (o instanceof EventType) {
 113                 return ((EventType) o).getType();
 114             }
 115             if (o instanceof ValueDescriptor) {
 116                 return ((ValueDescriptor) o).getType();
 117             }
 118             if (o instanceof SettingDescriptor) {
 119                 return ((SettingDescriptor) o).getType();
 120             }
 121             throw new Error("Unknown type " + o.getClass());
 122         }
 123 
 124         @Override
 125         public Configuration newConfiguration(String name, String label, String description, String provider, Map<String, String> settings, String contents) {
 126             return new Configuration(name, label, description, provider, settings, contents);
 127         }
 128 
 129         @Override
 130         public EventType newEventType(PlatformEventType platformEventType) {
 131             return new EventType(platformEventType);
 132         }
 133 
 134         @Override
 135         public AnnotationElement newAnnotation(Type annotationType, List<Object> values, boolean boot) {
 136             return new AnnotationElement(annotationType, values, boot);
 137         }
 138 
 139         @Override
 140         public ValueDescriptor newValueDescriptor(String name, Type fieldType, List<AnnotationElement> annos, int dimension, boolean constantPool, String fieldName) {
 141             return new ValueDescriptor(fieldType, name, annos, dimension, constantPool, fieldName);
 142         }
 143 
 144         @Override
 145         public PlatformRecording getPlatformRecording(Recording r) {
 146             return r.getInternal();
 147         }
 148 
 149         @Override
 150         public PlatformEventType getPlatformEventType(EventType eventType) {
 151             return eventType.getPlatformEventType();
 152         }
 153 
 154         @Override
 155         public boolean isConstantPool(ValueDescriptor v) {
 156             return v.isConstantPool();
 157         }
 158 
 159         @Override
 160         public void setAnnotations(ValueDescriptor v, List<AnnotationElement> a) {
 161             v.setAnnotations(a);
 162         }
 163 
 164         @Override
 165         public void setAnnotations(SettingDescriptor s, List<AnnotationElement> a) {
 166            s.setAnnotations(a);
 167         }
 168 
 169         @Override
 170         public String getFieldName(ValueDescriptor v) {
 171             return v.getJavaFieldName();
 172         }
 173 
 174         @Override
 175         public ValueDescriptor newValueDescriptor(Class<?> type, String name) {
 176             return new ValueDescriptor(type, name, Collections.emptyList(), true);
 177         }
 178 
 179         @Override
 180         public SettingDescriptor newSettingDescriptor(Type type, String name, String defaultValue, List<AnnotationElement> annotations) {
 181             return new SettingDescriptor(type, name, defaultValue, annotations);
 182         }
 183 
 184         @Override
 185         public boolean isUnsigned(ValueDescriptor v) {
 186             return v.isUnsigned();
 187         }
 188     }
 189 
 190     /**
 191      * Constructs a {@code FlightRecorderPermission} with the specified name.
 192      *
 193      * @param name permission name, must be either
 194      *        {@code "accessFlightRecorder"} or {@code "registerEvent"}, not
 195      *        {@code null}
 196      *
 197      * @throws IllegalArgumentException if {@code name} is empty or invalid
 198      */
 199     public FlightRecorderPermission(String name) {
 200         super(Objects.requireNonNull(name));
 201         if (!name.equals(Utils.ACCESS_FLIGHT_RECORDER) && !name.equals(Utils.REGISTER_EVENT)) {
 202             throw new IllegalArgumentException("name: " + name);
 203         }
 204     }
 205 }