1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
Derby - Class org.apache.derbyTesting.functionTests.util.SecurityCheck
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import junit.framework.Assert;
/**
* Code to aid in checking the Security of Derby.
* This initial implementation only handles the emebdded code.
* Future work could expand to the client driver and network server.
*/
public class SecurityCheck {
/**
* List of classes in the public api for the embedded engine.
*/
private static final String[] EMBEDDED_PUBLIC_API =
{
"org.apache.derby.jdbc.EmbeddedDriver",
"org.apache.derby.jdbc.EmbeddedDataSource",
"org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource",
"org.apache.derby.jdbc.EmbeddedXADataSource",
"org.apache.derby.authentication.UserAuthenticator",
};
/**
* Is the passed in class part of the declared public api.
* Currently only the emebdded public api
* @param c class to be checked
* @return true if the class is part of the public api, false otherwise.
*/
private static boolean isPublicApi(Class c)
{
for (int i = 0; i < EMBEDDED_PUBLIC_API.length; i++)
{
if (EMBEDDED_PUBLIC_API[i].equals(c.getName()))
return true;
}
return false;
}
/**
* Map of all classes that have been inspected.
* The key is the class name, if the value is null then
* the class is being inspected. Otherwise the value is
* a String description of the risks.
*/
private static final SortedMap<String, String> allInspectedClasses =
new TreeMap<String, String>();
/**
* Perform security analysis of the public api for the embedded engine.
* Prints a report to System.out on completion.
* @throws ClassNotFoundException
*/
public static void checkEmbeddedPublicApi() throws ClassNotFoundException
{
System.out.println("SecurityCheck: embedded public api classes");
allInspectedClasses.clear();
for (int i = 0; i < EMBEDDED_PUBLIC_API.length; i++)
SecurityCheck.inspectClass(EMBEDDED_PUBLIC_API[i]);
SecurityCheck.report(true);
}
/**
* Produce a report on System.out of all inspected classes
* that have risks associated with them.
*
*/
public static void report()
{
SecurityCheck.report(false);
}
/**
* Produce a report on System.out of all inspected classes
* that have risks associated with them. If reportClear is
* true then additionally all classes that have been inspected
* will be returned.
*
*/
private static void report(boolean reportClear)
{
synchronized (allInspectedClasses)
{
for (Map.Entry<String, String> entry : allInspectedClasses.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
if (value == null)
{
if (reportClear)
System.out.println("CLEAR: " + key);
}
else
{
System.out.print(value);
}
}
}
}
/**
* Inspect a class for security risks. No output is generated
* by this call, the caller must call report() to obtain the risks.
* @param className
* @throws ClassNotFoundException
*/
public static void inspectClass(String className) throws ClassNotFoundException
{
SecurityCheck.inspectClass(Class.forName(className), null);
}
/**
* Inspect the class of the passed in Object for security risks.
* This inspects, at this level only, the actual type of
* the object, not the declared type. E.g. for DriverManager.getConnection
* the declared type is java.sql.Connection which has no security risks,
* but the implementation type returned may have many.
*
* <code>
* Connection conn = DriverManager.getConnection(url);
* // will inspect the implementation call, eg. EmbedConnection30
* SecurityManager.inspect(conn);
* </code>
* No output is generated by this call,
* the caller must call report() to obtain the risks.
* @param o Obejct to be inspected
* @param declared the declared type of the object.
*/
public static void assertSourceSecurity(Object o, String declared)
{
Assert.assertNotNull(o);
Assert.assertTrue(SecurityCheck.inspectClass(o.getClass(), declared));
}
/**
* Inspect a Derby class for security risks. This includes following potential
* references exposed through the class.
* <P>
* Risks looked at:
* <UL>
* <LI> public constructors in non-public class -
* No justification for the constructor to be public.
* <LI> public constructors in non-final class and non-sealed package -
* Allows the class to be sub-classes through a injected class in
* the same package.
* <LI> public non-final field - Allows any one with a handle to
* the object to change the field.
* </UL>
* <P>
* The type of any public field or return type of any public method
* is also inspected. The assumption is that if such a field or method
* exists they have the potential to be called and return a valid object.
* <P>
* Note that this inspection is through the declared type of exposed
* references, not the actual runtime type. The actual runtime type
* might expose other classes that should be inspected.
* @param c the class to inspect
* @throws ClassNotFoundException
*/
private static boolean inspectClass(Class c, String declared)
{
if (!c.getName().startsWith("org.apache.derby."))
return false;
// Initial focus on embedded engine
if (c.getName().startsWith("org.apache.derby.client."))
return false;
synchronized (allInspectedClasses)
{
if (allInspectedClasses.containsKey(c.getName()))
return true;
allInspectedClasses.put(c.getName(), null);
StringBuffer sb = new StringBuffer();
sb.append("Class ");
sb.append(c.getName());
sb.append('\n');
if (declared != null)
{
allInspectedClasses.put(declared, "Checked class declared as: " + declared + "\n");
}
boolean isPublicApi = SecurityCheck.isPublicApi(c);
boolean hasIssues = false;
boolean isSealed = c.getPackage().isSealed();
boolean isFinal = Modifier.isFinal(c.getModifiers());
boolean isPublic = Modifier.isPublic(c.getModifiers());
boolean isAbstract = Modifier.isAbstract(c.getModifiers());
Constructor[] constructors = c.getConstructors();
boolean hasPublicConstructor = constructors.length != 0;
if (hasPublicConstructor && !isPublic)
{
hasIssues = true;
// No reason for a public constructor in a non-public class
sb.append("..public constructors in non-public class\n");
// class can be sub-classed even though it is not public
if (!isFinal && !isSealed)
sb.append("..public constructors in non-final class and non-sealed package\n");
}
if (hasPublicConstructor && isPublic)
{
// TODO: Need to work on these checks.
if (!isPublicApi)
{
//hasIssues = true;
// anyone can create instances of this class
//sb.append("..public constructors in public class\n");
}
// and anyone can sub-class this class
if (!isFinal)
{
//hasIssues = true;
//sb.append("..public constructors in public non-final class\n");
}
}
for (int i = 0; i < constructors.length; i++)
{
if (hasIssues)
{
sb.append("..public constructor: ");
sb.append(constructors[i].toString());
sb.append('\n');
}
}
Field[] fields = c.getFields();
for (int i = 0; i < fields.length; i++)
{
Field f = fields[i];
boolean isStatic = Modifier.isStatic(f.getModifiers());
Class fieldType = f.getType();
SecurityCheck.inspectClass(fieldType, null);
if (Modifier.isFinal(f.getModifiers()))
{
// TODO: Should this be a concern if non-static?
continue;
}
hasIssues = true;
sb.append("..public non-final field: ");
sb.append(f.toString());
sb.append('\n');
}
Method[] methods = c.getMethods();
for (int i = 0; i < methods.length; i++)
{
Method m = methods[i];
Class methodType = m.getReturnType();
if (SecurityCheck.inspectClass(methodType, null))
{
// method returns a class of interest to us.
// just a method returning a public api
if (SecurityCheck.isPublicApi(methodType))
continue;
/*
* Not sure this is a vaild risk.
hasIssues = true;
sb.append("..public method returning non-public api class: ");
sb.append(m.toString());
sb.append("\n");
*/
}
}
if (hasIssues)
allInspectedClasses.put(c.getName(), sb.toString());
}
return true;
}
}
|