C and C++ Binaries


IO.INJ.LDAP : LDAP Injection

Summary

Potentially-tainted data is used to construct an LDAP statement.

Properties

Class Name LDAP Injection
Significance security
Mnemonic IO.INJ.LDAP
Categories
MisraC2023 MisraC2023:D.4.14 The validity of values received from external sources shall be checked
Misra2012 Misra2012:D.4.14 The validity of values received from external sources shall be checked
AUTOSARC++14 AUTOSARC++14:A27-0-1 Inputs from independent components shall be validated.
CWE CWE:90 Improper Neutralization of Special Elements used in an LDAP Query ('LDAP Injection')
CERT-C CERT-C:STR02-C Sanitize data passed to complex subsystems
DISA-6r1 DISA-6r1:V-222606 The application must validate all input.
  DISA-6r1:V-222609 The application must not be subject to input handling vulnerabilities.
DISA-5r3 DISA-5r3:V-70265 The application must validate all input.
  DISA-5r3:V-70271 The application must not be subject to input handling vulnerabilities.
DISA-4r3 DISA-4r3:V-70265 The application must validate all input.
  DISA-4r3:V-70271 The application must not be subject to input handling vulnerabilities.
DISA-3r10 DISA-3r10:V-6157 The designer will ensure the application does not contain invalid URL or path references.
  DISA-3r10:V-6164 The designer will ensure the application validates all input.
OWASP-2017 OWASP-2017:A1 Injection
OWASP-2021 OWASP-2021:A3 Injection
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="LDAP Injection"

Example

In the following example, the code constructs an LDAP query based on user input. In normal usage the user provides an ID number, then the code displays the DN fields of any LDAP records with that ID number. However, an attacker could use a malicious input such as "*" to reveal far more of the directory than the code author intended.

#include <stdlib.h>
#include <stdio.h>
#include "math.h"
#include "ldap.h"

void lookup_by_idnum(LDAP *ld ){
    char filter[256];
    char idnum[128];
    LDAPMessage *res_msg, *msg;
    BerElement *berel; 
    int retval, i;
    char *att, *dn;
    struct berval **attvals;

    printf("what is the ID number?\n");
    if (!fgets(idnum, 128, stdin)) return ;
    sprintf(filter, "(IDnum=%s)", idnum);

    retval=ldap_search_ext_s(ld, 
                             "dc=myserver,dc=example,dc=com",
                             LDAP_SCOPE_SUBTREE, 
                             filter,  NULL, 0, NULL, NULL, NULL, 
                             LDAP_NO_LIMIT, &res_msg );  // LDAP Injection Warning Here

    if (retval != LDAP_SUCCESS) { 
        for ( msg = ldap_first_message( ld, res_msg ); 
              msg != NULL; 
              msg = ldap_next_message( ld, msg ) ) {
            if (( dn = ldap_get_dn( ld, msg )) != NULL ) {
                printf( "dn: %s\n", dn );
                ldap_memfree( dn );
            }  
            for ( att = ldap_first_attribute( ld, msg, &berel );
                  att != NULL; 
                  att = ldap_next_attribute( ld, msg, berel ) ) {
                if ((attvals = ldap_get_values_len( ld, msg, att ))!= NULL ) {
                    for ( i = 0; attvals[ i ] != NULL; i++ ) {
                        printf( "%s: %s\n", att, attvals[ i ]->bv_val );

                    } 
                }
            }
        }
    }
    exit(0);
}

Triggers

CodeSonar ships with library models that allow it to recognize functions such as LDAP ldap_search_ext() that use one or more of their parameters to construct an LDAP statement. If one of these functions is called with a tainted value in one of those parameter positions, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering LDAP Injection warnings.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.