JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
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
MisraC2025 MisraC2025:D.4.14 The validity of values received from external sources shall be checked
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.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
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
  OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A05 Injection
  OWASP-2025:A06 Insecure Design
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"

#ifdef HAVE_LDAP_H
#include "ldap.h"
#else

/* Provide minimal declarations so this can compile even if openldap
 * headers are not available.
 */
typedef struct ldap LDAP;
typedef struct ldapmsg LDAPMessage;
typedef struct berelement BerElement;
typedef struct ldapmsg LDAPMessage;
#define LBER_INT_T int
#define LBER_LEN_T long
typedef unsigned LBER_LEN_T ber_len_t;
typedef LBER_INT_T ber_int_t;
#define LDAP_SCOPE_SUBTREE		((ber_int_t) 0x0002)
#define LDAP_SUCCESS				0x00
#define LDAP_NO_LIMIT			0
typedef struct berval {
	ber_len_t	bv_len;
	char		*bv_val;
} BerValue;
typedef struct ldapcontrol {
	char *			ldctl_oid;			/* numericoid of control */
	struct berval	ldctl_value;		/* encoded value of control */
	char			ldctl_iscritical;	/* criticality */
} LDAPControl;
#define LDAP_CONST const
int
ldap_search_ext_s(
	LDAP			*ld,
	LDAP_CONST char	*base,
	int				scope,
	LDAP_CONST char	*filter,
	char			**attrs,
	int				attrsonly,
	LDAPControl		**serverctrls,
	LDAPControl		**clientctrls,
	struct timeval	*timeout,
	int				sizelimit,
	LDAPMessage		**res );
LDAPMessage *
ldap_first_message (
	LDAP *ld,
	LDAPMessage *chain );

LDAPMessage *
ldap_next_message (
	LDAP *ld,
	LDAPMessage *msg );
char *
ldap_get_dn (
	LDAP *ld,
	LDAPMessage *entry );
void
ldap_memfree (
	void* p );
char *
ldap_first_attribute (
	LDAP *ld,
	LDAPMessage *entry,
	BerElement **ber );

char *
ldap_next_attribute (
	LDAP *ld,
	LDAPMessage *entry,
	BerElement *ber );

struct berval **
ldap_get_values_len (
	LDAP *ld,
	LDAPMessage *entry,
	LDAP_CONST char *target );

#endif

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.

 

To report problems with this documentation, please visit https://support.codesecure.com/.