The C Standard, 220.127.116.11, discusses the layout of structure fields. It specifies that non-bit-field members are aligned in an implementation-defined manner and that there may be padding within or at the end of a structure. Furthermore, initializing the members of the structure does not guarantee initialization of the padding bytes. The C Standard, 18.104.22.168, paragraph 6 [ISO/IEC 9899:2011], states
When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values.
Additionally, the storage units in which a bit-field resides may also have padding bits. For an object with automatic storage duration, these padding bits do not take on specific values and can contribute to leaking sensitive information.
When passing a pointer to a structure across a trust boundary to a different trusted domain, the programmer must ensure that the padding bytes and bit-field storage unit padding bits of such a structure do not contain sensitive information.
Noncompliant Code Example
This noncompliant code example runs in kernel space and copies data from
arg to user space. However, padding bytes may be used within the structure, for example, to ensure the proper alignment of the structure members. These padding bytes may contain sensitive information, which may then be leaked when the data is copied to user space.
Noncompliant Code Example (
The padding bytes can be explicitly initialized by calling
However, a conforming compiler is free to implement
arg.b = 2 by setting the low-order bits of a register to 2, leaving the high-order bits unchanged and containing sensitive information. Then the platform copies all register bits into memory, leaving sensitive information in the padding bits. Consequently, this implementation could leak the high-order bits from the register to a user.
This compliant solution serializes the structure data before copying it to an untrusted context:
This code ensures that no uninitialized padding bytes are copied to unprivileged users. The structure copied to user space is now a packed structure and the
copy_to_user() function would need to unpack it to recreate the original padded structure.
Compliant Solution (Padding Bytes)
Padding bytes can be explicitly declared as fields within the structure. This solution is not portable, however, because it depends on the implementation and target memory architecture. The following solution is specific to the x86-32 architecture:
The C Standard
static_assert() macro accepts a constant expression and an error message. The expression is evaluated at compile time and, if false, the compilation is terminated and the error message is output. (See DCL03-C. Use a static assertion to test the value of a constant expression for more details.) The explicit insertion of the padding bytes into the
struct should ensure that no additional padding bytes are added by the compiler and consequently both static assertions should be true. However, it is necessary to validate these assumptions to ensure that the solution is correct for a particular implementation.
Compliant Solution (Structure Packing—GCC)
GCC allows specifying declaration attributes using the keyword
__attribute__((__packed__)). When this attribute is present, the compiler will not add padding bytes for memory alignment unless an explicit alignment specifier for a structure member requires the introduction of padding bytes.
Compliant Solution (Structure Packing—Microsoft Visual Studio)
Microsoft Visual Studio supports
#pragma pack() to suppress padding bytes [MSDN]. The compiler adds padding bytes for memory alignment, depending on the current packing mode, but still honors the alignment specified by
__declspec(align()). In this compliant solution, the packing mode is set to 1 in an attempt to ensure all fields are given adjacent offsets:
pack pragma takes effect at the first
struct declaration after the pragma is seen.
Noncompliant Code Example
This noncompliant code example also runs in kernel space and copies data from
struct test to user space. However, padding bits will be used within the structure due to the bit-field member lengths not adding up to the number of bits in an
unsigned object. Further, there is an unnamed bit-field that causes no further bit-fields to be packed into the same storage unit. These padding bits may contain sensitive information, which may then be leaked when the data is copied to user space. For instance, the uninitialized bits may contain a sensitive kernel space pointer value that can be trivially reconstructed by an attacker in user space.
Padding bits can be explicitly declared, allowing the programmer to specify the value of those bits. When explicitly declaring all of the padding bits, any unnamed bit-fields of length
0 must be removed from the structure because the explicit padding bits ensure that no further bit-fields will be packed into the same storage unit.
This solution is not portable, however, because it depends on the implementation and target memory architecture. The explicit insertion of padding bits into the
struct should ensure that no additional padding bits are added by the compiler. However, it is still necessary to validate these assumptions to ensure that the solution is correct for a particular implementation. For instance, the DEC Alpha is an example of a 64-bit architecture with 32-bit integers that allocates 64 bits to a storage unit.
In addition, this solution assumes that there are no integer padding bits in an
unsigned int. The portable version of the width calculation from INT35-C. Use correct integer precisions cannot be used because the bit-field width must be an integer constant expression.
From this situation, it can be seen that special care must be taken because no solution to the bit-field padding issue will be 100% portable.
Padding units might contain sensitive data because the C Standard allows any padding to take unspecified values. A pointer to such a structure could be passed to other functions, causing information leakage.
|Axivion Bauhaus Suite|
|CertC-DCL39||Detects composite structures with padding, in particular those passed to trust boundary routines.|
A pointer to a structure should not be passed to a function that can copy data to the user space
|CERT C: Rule DCL39-C||Checks for information leak via structure padding (rule partially covered)|
Numerous vulnerabilities in the Linux Kernel have resulted from violations of this rule. CVE-2010-4083 describes a vulnerability in which the
semctl() system call allows unprivileged users to read uninitialized kernel stack memory because various fields of a
semid_ds struct declared on the stack are not altered or zeroed before being copied back to the user.
CVE-2010-3881 describes a vulnerability in which structure padding and reserved fields in certain data structures in
QEMU-KVM were not initialized properly before being copied to user space. A privileged host user with access to
/dev/kvm could use this flaw to leak kernel stack memory to user space.
CVE-2010-3477 describes a kernel information leak in
act_police where incorrectly initialized structures in the traffic-control dump code may allow the disclosure of kernel memory to user space applications.
Key here (explains table format and definitions)
|CERT C Secure Coding Standard||DCL03-C. Use a static assertion to test the value of a constant expression||Prior to 2018-01-12: CERT: Unspecified Relationship|
|[ISO/IEC 9899:2011]||22.214.171.124, "General"|
126.96.36.199, "Structure and Union Specifiers"