Skip to content

[java] InsecureCryptoIv/HardCodedCryptoKey: False negative for unchanged default-initialized arrays #7015

Closed

Description

@ziwen-gong

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:

  1. Determine whether the allocated array has a statically known length;
  2. Determine whether its element type has a deterministic default value;
  3. Treat an array without an explicit initializer as having those default values;
  4. Propagate the resulting array contents to subsequent uses;
  5. 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

  1. added
    a:false-negative
    on Aug 31, 2026
  2. aoto-tech commented on Sep 7, 2026

    @aoto-tech
    Contributor

    I’d like to work on this issue. The following is my current understanding of the possible implementation scopes:

    ScopeImplementationDifficultyLimitation / Concern
    Minimal Detect only direct constructions such as new IvParameterSpec(new byte[16]) Low Does not cover the variable-based example reported in this issue
    Practical Track simple local-variable assignments within the same method Medium Must account for mutations such as SecureRandom.nextBytes(iv) before the array is used
    Broad Track aliases, branches, loops, and mutations through method calls High Requires more comprehensive data-flow and potentially interprocedural analysis

    I 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.

  3. ziwen-gong commented on Sep 9, 2026

    @ziwen-gong
    Author
  4. aoto-tech commented on Sep 9, 2026

    @aoto-tech
    Contributor

    @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?

  5. adangel commented on Sep 11, 2026

    @adangel
    Member

    @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:

    package com.example;
    
    import javax.crypto.spec.IvParameterSpec;
    import java.security.SecureRandom;
    import java.util.random.RandomGenerator;
    
    public class GoodCase_Var {
        public void ok1() {
            byte[] iv = new byte[16];
            loadIvFromSomehwere(iv); // assume, iv is initialized in that method. That method might be somewhere else (not even in this file)
            IvParameterSpec ivSpec = new IvParameterSpec(iv); // not a violation
        }
    
        private void loadIvFromSomewhere(byte[] iv) {
            SecureRandom rand = new SecureRandom();
            rand.nextBytes(iv);
        }
    
        public void ok2() {
            byte[] iv = new byte[16];
            getRand().nextBytes(iv); // assume, iv is initialized properly here.
            IvParameterSpec ivSpec = new IvParameterSpec(iv); // not a violation
        }
    
        private RandomGenerator getRand() {
            return new SecureRandom();
        }
    
        public void ok3() {
            byte[] iv = new byte[16];
            new SecureRandom().nextBytes(iv); // that's definitively ok
            IvParameterSpec ivSpec = new IvParameterSpec(iv); // not a violation
        }
    
        public void ok4(byte[] cipherTextPrefixedWithIv) {
            byte[] iv = new byte[16];
            System.arraycopy(cipherTextPrefixedWithIv, 0, iv, 0, 16);
            IvParameterSpec ivSpec = new IvParameterSpec(iv); // not a violation
        }
    }

    It would be good, to have a couple of "real-world" examples, because all we have are somewhat contrived...

  6. aoto-tech commented on Sep 11, 2026

    @aoto-tech
    Contributor

    @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.

  7. aoto-tech commented on Sep 11, 2026

    @aoto-tech
    Contributor

    /take

  8. changed the title [-][java] InsecureCryptoIv: False negative when a hard-coded IV is constructed through default array initialization[/-] [+]InsecureCryptoIv: False negative for unchanged default-initialized arrays[/+] on Oct 22, 2026
  9. changed the title [-]InsecureCryptoIv: False negative for unchanged default-initialized arrays[/-] [+][java] InsecureCryptoIv: False negative for unchanged default-initialized arrays[/+] on Oct 22, 2026
  10. changed the title [-][java] InsecureCryptoIv: False negative for unchanged default-initialized arrays[/-] [+][java] InsecureCryptoIv/HardCodedCryptoKey: False negative for unchanged default-initialized arrays[/+] on Oct 23, 2026
  11. added this to the 7.29.0 milestone on Oct 23, 2026
  12. added a commit that references this issue on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions