forked from spotbugs/spotbugs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule PERM_SUPER_NOT_CALLED_IN_GETPERMISSIONS (spotbugs#1537)
* Add new rule PERM_SUPER_NOT_CALLED_IN_GETPERMISSIONS SEI CERT rule SEC07-J requires that the superclass's getPermissions() is called when writing a custom class loader. This method is defined in class java.net.URLClassLoader. Thus any class inheriting directly or indirectly form URLClassLoader and overriding its getPermissions() method must call the superclass's getPermissions() method in this method. It is not enoguh to just call it but the the return value (an instance of java.security.PermissionCollection) must be initialized by calling the superclass's method instead of creating a new instance from scratch. This patch adds a new detector to check this rule. * Debug printouts removed and other fixes * Header comment fixed from FindBugs to SpotBugs * Updated based on the review comments by @KengoTODA * Updated according to the comments of @KengoTODA * Grammar fix * Checking for all ancestors instead of the direct superclass * Minor fix * Changelog updated after the merge * Update spotbugs/src/main/java/edu/umd/cs/findbugs/detect/PermissionsSuper.java Co-authored-by: Kengo TODA <[email protected]> * Merge `master` into `PermissionsSuper` Co-authored-by: Kengo TODA <[email protected]>
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/PermissionsSuper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* SpotBugs - Find bugs in Java programs | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
package edu.umd.cs.findbugs.detect; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.apache.bcel.Const; | ||
import org.apache.bcel.Repository; | ||
import org.apache.bcel.classfile.Code; | ||
import org.apache.bcel.classfile.JavaClass; | ||
import org.apache.bcel.classfile.Method; | ||
|
||
import edu.umd.cs.findbugs.BugAccumulator; | ||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.ba.AnalysisContext; | ||
import edu.umd.cs.findbugs.ba.XMethod; | ||
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector; | ||
|
||
public class PermissionsSuper extends OpcodeStackDetector { | ||
private boolean checkClass = false; | ||
private boolean checkMethod = false; | ||
|
||
private final BugAccumulator bugAccumulator; | ||
|
||
public PermissionsSuper(BugReporter bugReporter) { | ||
this.bugAccumulator = new BugAccumulator(bugReporter); | ||
} | ||
|
||
@Override | ||
public void visit(JavaClass obj) { | ||
checkClass = false; | ||
try { | ||
checkClass = obj.instanceOf(Repository.lookupClass("java.security.SecureClassLoader")); | ||
} catch (ClassNotFoundException e) { | ||
AnalysisContext.reportMissingClass(e); | ||
} | ||
super.visit(obj); | ||
} | ||
|
||
@Override | ||
public void visitAfter(JavaClass obj) { | ||
bugAccumulator.reportAccumulatedBugs(); | ||
} | ||
|
||
@Override | ||
public void visit(Method met) { | ||
checkMethod = checkClass && | ||
"(Ljava/security/CodeSource;)Ljava/security/PermissionCollection;" | ||
.equals(met.getSignature()); | ||
super.visit(met); | ||
} | ||
|
||
@Override | ||
public void visit(Code obj) { | ||
if (checkMethod) { | ||
super.visit(obj); | ||
} | ||
} | ||
|
||
@Override | ||
public void sawOpcode(int seen) { | ||
if (seen == Const.ARETURN) { | ||
XMethod origin = stack.getStackItem(0).getReturnValueOf(); | ||
if (origin != null) { | ||
try { | ||
if (Arrays.stream(getThisClass().getSuperClasses()) | ||
.anyMatch(cls -> cls.getClassName().equals(origin.getClassName())) && | ||
getMethod().getName().equals(origin.getName()) && | ||
getMethod().getSignature().equals(origin.getSignature())) { | ||
return; | ||
} | ||
} catch (ClassNotFoundException e) { | ||
AnalysisContext.reportMissingClass(e); | ||
} | ||
} | ||
bugAccumulator.accumulateBug(new BugInstance(this, | ||
"PERM_SUPER_NOT_CALLED_IN_GETPERMISSIONS", NORMAL_PRIORITY) | ||
.addClassAndMethod(this), this); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
spotbugsTestCases/src/java/PermissionsSuperNegativeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.security.CodeSource; | ||
import java.security.PermissionCollection; | ||
|
||
import edu.umd.cs.findbugs.annotations.NoWarning; | ||
|
||
public class PermissionsSuperNegativeTest extends MyClassLoader { | ||
PermissionsSuperNegativeTest(URL[] urls) { | ||
super(urls); | ||
} | ||
|
||
@NoWarning("PERM") | ||
protected PermissionCollection getPermissions(CodeSource cs) { | ||
PermissionCollection pc = super.getPermissions(cs); | ||
pc.add(new RuntimePermission("exitVM")); | ||
return pc; | ||
} | ||
} | ||
|
||
class MyClassLoader extends URLClassLoader { | ||
MyClassLoader(URL[] urls) { | ||
super(urls); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.security.CodeSource; | ||
import java.security.PermissionCollection; | ||
import java.security.Permissions; | ||
|
||
import edu.umd.cs.findbugs.annotations.ExpectWarning; | ||
|
||
class PermissionsSuperTest extends URLClassLoader { | ||
PermissionsSuperTest(URL[] urls) { | ||
super(urls); | ||
} | ||
|
||
@ExpectWarning("PERM") | ||
protected PermissionCollection getPermissions(CodeSource cs) { | ||
PermissionCollection pc = new Permissions(); | ||
pc.add(new RuntimePermission("exitVM")); | ||
return pc; | ||
} | ||
} |