CertC-WIN04

Consider encrypting function pointers

Required inputs: IR

If an attacker can overwrite memory containing function pointers, they may be able to execute arbitrary code. To mitigate the effects of such attacks, pointers to functions can be encrypted at runtime on the basis of some characteristics of the execution process so that only a running process will be able to decode them.  This is only required for stored function pointers stored to writable memory, including the stack.  The Microsoft SDL [Microsoft 2012] recommends encoding long-lived pointers in your code.

Noncompliant Code Example

This noncompliant code example assigns the address of the printf() function to the log_fn function pointer, which can be allocated in the stack or data segment:

int (*log_fn)(const char *, ...) = printf;
/* ... */
log_fn("foo");

If a vulnerability exists in this program that allows an attacker to overwrite the log_fn function pointer, such as a buffer overflow or arbitrary memory write, the attacker may be able to overwrite the value of printf with the location of an arbitrary function.

Compliant Solution (Windows)

Microsoft Windows provides the EncodePointer() and DecodePointer() functions that encrypt and decrypt pointers using a secret that is unique to the given process:

#include <Windows.h>

void *log_fn = EncodePointer(printf);
/* ... */
int (*fn)(const char *, ...) = (int (*)(const char *, ...))DecodePointer(log_fn);

fn("foo");

Note that DecodePointer() does not return success or failure.  If an attacker has overwritten the pointer contained in log_fn , the pointer returned will be invalid and cause your application to crash.  However, this is preferable to giving an attacker the ability to execute arbitrary code.

Risk Assessment
Recommendation Severity Likelihood Detectable Repairable Priority Level
WIN04-C High Unlikely No No P3 L3
Related Guidelines
SEI CERT C++ Coding Standard VOID MSC16-CPP. Consider encrypting function pointers
MITRE CWE CWE-311 , Missing encryption of sensitive data
CWE-319 , Cleartext Transmission of Sensitive Information
Bibliography
[ MSDN ] EncodePointer()
DecodePointer()
Microsoft Corporation 2012 Microsoft Security Development Lifecycle (SDL) – version 5.2 Phase 3: Implementation
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/microsoft-windows-win/win04-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

unencrypted_function_pointer

Consider encrypting function pointers.

None

False

Options

register_argument_count

register_argument_count : int = 0

Number of leading call arguments assumed to be passed in a register under the calling convention in effect; a bare function reference in one of those positions isn't storage of its own, unlike one spilled to the stack. The actual count depends on architecture and calling convention (e.g. 4 for the Microsoft x64 ABI, 2 for __fastcall, 0 for cdecl/stdcall), so there is no universally correct default. Arguments at or beyond this position are reported like any other assignment.