CWE-1335: Incorrect Bitwise Shift of Integer

Learn about CWE-1335 (Incorrect Bitwise Shift of Integer), its security impact, exploitation methods, and prevention guidelines.

What is Incorrect Bitwise Shift of Integer?

• Overview: Incorrect Bitwise Shift of Integer (CWE-1335) occurs when an integer is shifted by a negative value or by a value greater than or equal to the number of bits in the integer. This leads to undefined or unexpected results due to differences in how various architectures and compilers handle such shifts.

• Exploitation Methods:

  • Attackers can exploit this vulnerability by crafting inputs that cause unexpected shifts, leading to unpredictable behavior in the application.
  • Common attack patterns include manipulating input data to cause buffer overflows or logical errors due to incorrect integer manipulation.

• Security Impact:

  • Direct consequences include crashes, incorrect calculations, or unexpected application behavior.
  • Potential cascading effects involve data corruption or security feature bypass.
  • Business impact could be significant, leading to data loss, unauthorized access, or downtime.

• Prevention Guidelines:

  • Specific code-level fixes include validating shift values to ensure they are within the valid range before performing any shift operations.
  • Security best practices involve using safe integer manipulation libraries and conducting thorough input validation.
  • Recommended tools and frameworks include static analysis tools that detect potential integer manipulation errors and using language-specific features to handle integer operations safely.
Corgea can automatically detect and fix Incorrect Bitwise Shift of Integer in your codebase. [Try Corgea free today](https://corgea.app).

Technical Details

Likelihood of Exploit: Not specified

Affected Languages: C, C++, C#, Java, JavaScript

Affected Technologies: Not Technology-Specific

Vulnerable Code Example

Certainly! Let's improve the code examples to ensure they are clear, realistic, and follow best practices. I'll address the issues you mentioned, including syntax highlighting, line number formatting, and explanatory comments.

#include <stdio.h>

void shiftInteger(int value, int shift) {
    // Potentially dangerous shift operation
    // If 'shift' is negative or >= 32 (assuming 32-bit integer), it results in undefined behavior
    int result = value << shift; // Vulnerable: shift amount may cause undefined behavior
    printf("Shifted Result: %d\n", result);
}

int main() {
    int value = 1;
    int shift = 32; // Vulnerable: shifts by 32 bits, which is undefined for 32-bit integers
    shiftInteger(value, shift);
    return 0;
}

How to fix Incorrect Bitwise Shift of Integer?

To fix the incorrect bitwise shift vulnerability, it's essential to validate the shift amount before performing the shift operation. The shift value should be within the valid range, specifically non-negative and less than the number of bits in the integer type. For a 32-bit integer, valid shift amounts are from 0 to 31. This prevents undefined behavior caused by shifting by negative numbers or numbers greater than or equal to the bit-width of the integer.

Fixed Code Example

#include <stdio.h>

void shiftInteger(int value, int shift) {
    // Validate shift amount to prevent undefined behavior
    if (shift < 0 || shift >= 32) {
        printf("Error: Invalid shift amount. Must be between 0 and 31.\n");
        return;
    }
    int result = value << shift; // Safe shift operation
    printf("Shifted Result: %d\n", result);
}

int main() {
    int value = 1;
    int shift = 32; // Now caught by validation as an invalid shift amount
    shiftInteger(value, shift);
    return 0;
}

In the fixed code example, we added a validation check to ensure the shift amount is within the valid range for a 32-bit integer. This prevents undefined behavior and ensures the program operates safely. The syntax highlighting is corrected, and comments are provided to clearly explain the vulnerability and the fix.

Corgea Logo

Find this vulnerability and fix it with Corgea

Scan your codebase for CWE-1335: Incorrect Bitwise Shift of Integer and get remediation guidance

Start for free and no credit card needed.