01.07.2016 Views

SEI CERT C Coding Standard

tqcylJ

tqcylJ

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Declarations and Initialization (DCL) - DCL39-C. Avoid information leakage when passing a structure across a trust boundary<br />

struct test {<br />

int a;<br />

char b;<br />

int c;<br />

};<br />

#pragma pack(pop)<br />

/* Safely copy bytes to user space */<br />

extern int copy_to_user(void *dest, void *src, size_t size);<br />

void do_stuff(void *usr_buf) {<br />

struct test arg = {1, 2, 3};<br />

copy_to_user(usr_buf, &arg, sizeof(arg));<br />

}<br />

The pack pragma takes effect at the first struct declaration after the pragma is seen.<br />

3.6.7 Noncompliant Code Example<br />

This noncompliant code example also runs in kernel space and copies data from struct test to<br />

user space. However, padding bits will be used within the structure due to the bit-field member<br />

lengths not adding up to the number of bits in an unsigned object. Further, there is an unnamed<br />

bit-field that causes no further bit-fields to be packed into the same storage unit. These padding<br />

bits may contain sensitive information, which may then be leaked when the data is copied to user<br />

space. For instance, the uninitialized bits may contain a sensitive kernel space pointer value that<br />

can be trivially reconstructed by an attacker in user space.<br />

#include <br />

struct test {<br />

unsigned a : 1;<br />

unsigned : 0;<br />

unsigned b : 4;<br />

};<br />

/* Safely copy bytes to user space */<br />

extern int copy_to_user(void *dest, void *src, size_t size);<br />

void do_stuff(void *usr_buf) {<br />

struct test arg = { .a = 1, .b = 10 };<br />

copy_to_user(usr_buf, &arg, sizeof(arg));<br />

}<br />

However, compilers are free to implement the initialization of arg.a and arg.b by setting the<br />

low byte of a 32-bit register to the value specified, leaving the high bytes unchanged and storing<br />

all 32 bits of the register into memory. This implementation could leak the high-order bytes resident<br />

in the register to a user.<br />

<strong>SEI</strong> <strong>CERT</strong> C <strong>Coding</strong> <strong>Standard</strong>: Rules for Developing Safe, Reliable, and Secure Systems 57<br />

Software Engineering Institute | Carnegie Mellon University<br />

[DISTRIBUTION STATEMENT A] Approved for public release and unlimited distribution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!