Description
Affects PMD Version: 7.26.0
Rules:
Description:
InsecureCryptoIv can detect cases where the IV contents are explicitly specified using an array initializer, for example:
byte[] iv = new byte[] {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f
};
IvParameterSpec ivSpec = new IvParameterSpec(iv);
However, for the case where a fixed-length array is allocated without an explicit array initializer, the rule does not report a violation:
byte[] iv = new byte[16];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
In Java, newly created arrays of primitive types are automatically initialized with their default values. For byte[], the default value of each element is 0. Therefore, in byte[] iv = new byte[16];, all 16 elements of iv are deterministically set to 0, and the value ultimately passed to IvParameterSpec is also completely determined by the source code:
{0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0}
Although these 0 values do not appear explicitly as literals in the source code, the actual contents of the array are determined by Java's default initialization semantics. Therefore, from the perspective of the rule, this case could also be worth considering for detection. The following test case demonstrates this behavior. The first case is reported by InsecureCryptoIv, whereas the second case does not produce a warning. Therefore, this case could be considered a potential false negative.
Code Sample demonstrating the issue:
// Explicitly initialized IV
package com.example;
import javax.crypto.spec.IvParameterSpec;
public class BadCase {
public static void main(String[] args) {
// Hardcoded IV using a fixed byte array
byte[] iv = new byte[] {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f
}; // [REPORTED LINE]
IvParameterSpec ivSpec = new IvParameterSpec(iv);
}
}
// Default array initialization
package com.example;
import javax.crypto.spec.IvParameterSpec;
public class BadCase_Var {
public static void main(String[] args) {
byte[] iv = new byte[16];
IvParameterSpec ivSpec = new IvParameterSpec(iv);// [should be flagged]
}
}Expected outcome:
InsecureCryptoIv could consider locally allocated primitive arrays with statically known default values when determining whether an IV is a fixed, hard-coded value. For example:
byte[] iv = new byte[16];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
could be considered by the rule because the contents of iv are deterministically known to be all zeroes. A possible improvement would be to extend the handling of ASTArrayAllocation to account for Java's default array initialization semantics. In particular, the checker could:
- Determine whether the allocated array has a statically known length;
- Determine whether its element type has a deterministic default value;
- Treat an array without an explicit initializer as having those default values;
- Propagate the resulting array contents to subsequent uses;
- Evaluate whether the resulting IV is a fixed value that should be reported by InsecureCryptoIv.
This would allow the rule to consider fixed IVs even when their constant contents arise implicitly from Java's array initialization semantics rather than from an explicit ASTArrayInitializer.
This is a false-negative case.
Note: The same applies for HardcodedCryptoKey.
Running PMD through: CLI
Activity
aoto-tech commentedon Sep 7, 2026
I’d like to work on this issue. The following is my current understanding of the possible implementation scopes:
new IvParameterSpec(new byte[16])SecureRandom.nextBytes(iv)before the array is usedI assume the practical scope is the most appropriate starting point: tracking a locally allocated, default-initialized array within the same method while avoiding a report if the array may have been modified before being passed to
IvParameterSpec.Please let me know if this matches the intended scope of the issue. If so, I can proceed with the implementation and regression tests.
ziwen-gong commentedon Sep 9, 2026
aoto-tech commentedon Sep 9, 2026
@adangel, is excluding default-initialized arrays an intentional limitation to avoid false positives?
Determining whether an array remains unchanged until use requires additional analysis logic. This could require a more fundamental revision of the existing analysis, so I am concerned about proceeding as though this were a straightforward false-negative fix.
Is this extension within PMD’s intended scope, or would it be more appropriate to document the current limitation?
adangel commentedon Sep 11, 2026
@aoto-tech The practical scope sound reasonable. I didn't look at the current impl, but if we currently intentionally skipped this case, then because you need to make sure, that no write access occurs before usage (so, that it is indeed all zeros). The assumption probably was: If the array is not defined inline (it is a variable), then the array content is probably loaded from somewhere and is not constant. But if the "loading" never happens, it's constantly zero.
Tracking assignments/usages within a single method is definitely within PMD's intended scope.
Valid (practical) cases - which should not produce a violation:
It would be good, to have a couple of "real-world" examples, because all we have are somewhat contrived...
aoto-tech commentedon Sep 11, 2026
@adangel san
Thanks, this clarifies the intended boundary.
I’ll proceed with a conservative intraprocedural implementation: a default-initialized local array will be reported only when there is no potentially mutating or escaping use before the specific IvParameterSpec construction.
Passing the array to an unknown method, SecureRandom.nextBytes, System.arraycopy as the destination, or another alias/storage location will suppress the report. Uses occurring after the IvParameterSpec construction will not affect that earlier use.
I’ll add the positive cases, the four negative cases above, and ordering-related regression tests. I also found real-world examples of both patterns in PacketProxy, jPOS, and Hyphanet/Fred, which I’ll reference in the PR description.
aoto-tech commentedon Sep 11, 2026
/take
[java] Fix InsecureCryptoIv default-initialized arrays (pmd#7015)
[-][java] InsecureCryptoIv: False negative when a hard-coded IV is constructed through default array initialization[/-][+]InsecureCryptoIv: False negative for unchanged default-initialized arrays[/+][-]InsecureCryptoIv: False negative for unchanged default-initialized arrays[/-][+][java] InsecureCryptoIv: False negative for unchanged default-initialized arrays[/+][-][java] InsecureCryptoIv: False negative for unchanged default-initialized arrays[/-][+][java] InsecureCryptoIv/HardCodedCryptoKey: False negative for unchanged default-initialized arrays[/+][java] Fix #7014/#7015: InsecureCryptoIv/HardCodedCryptoKey: Detect d…