|
| 1 | +/** Provides classes to reason about vulnerabilites related to content URIs. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.java.frameworks.android.Android |
| 6 | + |
| 7 | +/** A URI that gets resolved by a `ContentResolver`. */ |
| 8 | +abstract class ContentUriResolutionSink extends DataFlow::Node { |
| 9 | + /** Gets the call node that resolves this URI. */ |
| 10 | + abstract DataFlow::Node getCallNode(); |
| 11 | +} |
| 12 | + |
| 13 | +/** A sanitizer for content URIs. */ |
| 14 | +abstract class ContentUriResolutionSanitizer extends DataFlow::Node { } |
| 15 | + |
| 16 | +/** |
| 17 | + * A unit class for adding additional taint steps to configurations related to |
| 18 | + * content URI resolution vulnerabilities. |
| 19 | + */ |
| 20 | +abstract class ContentUriResolutionAdditionalTaintStep extends Unit { |
| 21 | + /** Holds if the step from `node1` to `node2` should be considered an additional taint step. */ |
| 22 | + abstract predicate step(DataFlow::Node node1, DataFlow::Node node2); |
| 23 | +} |
| 24 | + |
| 25 | +/** The URI argument of a call to a `ContentResolver` URI-opening method. */ |
| 26 | +private class DefaultContentUriResolutionSink extends ContentUriResolutionSink { |
| 27 | + DefaultContentUriResolutionSink() { |
| 28 | + exists(MethodAccess ma | |
| 29 | + ma.getMethod() instanceof UriOpeningContentResolverMethod and |
| 30 | + this.asExpr() = ma.getAnArgument() and |
| 31 | + this.getType().(RefType).hasQualifiedName("android.net", "Uri") |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + /** Gets the call node of this argument. */ |
| 36 | + override DataFlow::Node getCallNode() { |
| 37 | + result = DataFlow::exprNode(this.asExpr().(Argument).getCall()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +private class UninterestingTypeSanitizer extends ContentUriResolutionSanitizer { |
| 42 | + UninterestingTypeSanitizer() { |
| 43 | + this.getType() instanceof BoxedType or |
| 44 | + this.getType() instanceof PrimitiveType or |
| 45 | + this.getType() instanceof NumberType |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +private class FilenameOnlySanitizer extends ContentUriResolutionSanitizer { |
| 50 | + FilenameOnlySanitizer() { |
| 51 | + exists(Method m | this.asExpr().(MethodAccess).getMethod() = m | |
| 52 | + m.hasQualifiedName("java.io", "File", "getName") or |
| 53 | + m.hasQualifiedName("kotlin.io", "FilesKt", ["getNameWithoutExtension", "getExtension"]) or |
| 54 | + m.hasQualifiedName("org.apache.commons.io", "FilenameUtils", "getName") |
| 55 | + ) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * A `ContentUriResolutionSink` that flows to an image-decoding function. |
| 61 | + * Such functions raise exceptions when the input is not a valid image, |
| 62 | + * which prevents accessing arbitrary non-image files. |
| 63 | + */ |
| 64 | +private class DecodedAsAnImageSanitizer extends ContentUriResolutionSanitizer { |
| 65 | + DecodedAsAnImageSanitizer() { |
| 66 | + exists(Argument decodeArg, MethodAccess decode | |
| 67 | + decode.getArgument(0) = decodeArg and |
| 68 | + decode |
| 69 | + .getMethod() |
| 70 | + .hasQualifiedName("android.graphics", "BitmapFactory", |
| 71 | + [ |
| 72 | + "decodeByteArray", "decodeFile", "decodeFileDescriptor", "decodeResource", |
| 73 | + "decodeStream" |
| 74 | + ]) |
| 75 | + | |
| 76 | + DataFlow::localFlow(this.(ContentUriResolutionSink).getCallNode(), |
| 77 | + DataFlow::exprNode(decodeArg)) |
| 78 | + ) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** A `ContentResolver` method that resolves a URI. */ |
| 83 | +private class UriOpeningContentResolverMethod extends Method { |
| 84 | + UriOpeningContentResolverMethod() { |
| 85 | + this.hasName([ |
| 86 | + "openInputStream", "openOutputStream", "openAssetFile", "openAssetFileDescriptor", |
| 87 | + "openFile", "openFileDescriptor", "openTypedAssetFile", "openTypedAssetFileDescriptor", |
| 88 | + ]) and |
| 89 | + this.getDeclaringType() instanceof AndroidContentResolver |
| 90 | + } |
| 91 | +} |
0 commit comments