0% found this document useful (0 votes)
10 views3 pages

Memory Safety Vulnerabilities in C

The document discusses memory safety vulnerabilities like format string vulnerabilities and integer overflows. It provides examples of code vulnerable to these issues and asks questions to assess understanding. Potential attacks and fixes are also explained.

Uploaded by

Agus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Memory Safety Vulnerabilities in C

The document discusses memory safety vulnerabilities like format string vulnerabilities and integer overflows. It provides examples of code vulnerable to these issues and asks questions to assess understanding. Potential attacks and fixes are also explained.

Uploaded by

Agus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SC3010 Computer Security

Tutorial 2 – Memory Safety Vulnerability

1. Circle the correct answers in the following questions.


1) Which statement is false about the format string vulnerability?

A. Attacker can abuse the format string “%d” to cause confidentiality violation.
B. Attacker can abuse the format string “%i” to cause integrity violation.
C. Attacker can abuse the format string “%s” to cause availability violation.
D. Attacker can abuse the format string “%x” to cause confidentiality violation.

2) Which statements are true about Cross-Site Scripting (XSS) attack?


(i) XSS can target static web applications without users’ input.
(ii) When XSS is exploited, the malicious commands are executed on the victim’s local
computer, instead of the web server.
(iii) In the stored XSS attack, the attacker does not need to connect to the victim computer.
(iv) In the reflected XSS attack, the malicious command can exist in the web server for a
long time.

A. (i) and (iii)


B. (i) and (iv)
C. (ii) and (iii)
D. (ii) and (iv)

3) In a C program, let an unsigned int variable x = UINT_MAX. What will be the result when
we calculate x ++?

A. 0
B. UINT_MAX
C. INT_MAX
D. INT_MIN

2. Answer the following questions.


1) What is the root cause of format string vulnerability? What are the possible consequences?

2) How to prevent integer overflow vulnerabilities?

3) What is the scripting vulnerability?

3. Consider the following fragment of a C program. The program has a vulnerability that would allow
an attacker to cause the program to disclose the content of the variable “secret” at runtime. We
assume that the attacker has no access to the exact implementation of the ‘get_secret()’
function so the attack has to work regardless of how the function ‘get_secret()’ is implemented.
1) Explain how the attack mentioned above works. You do not need to produce the exact
input to the program that would trigger the attack. It is sufficient to explain the strategy
of the attack. Explain why the attack works.

2) The vulnerability above can be fixed by modifying just one statement in the program
without changing its functionality. Show which statement you should modify and how you
would modify it to fix the vulnerability. Show the C code of the proposed solution

int main(int argc, char* argv[]) {


int uid1 = x12345;
int secret = get_secret();
int uid2 = x56789;
char str[256];
if (argc < 2)
return 1;
strncpy(str, argv[1], 255);
str[255] = ‘\0’;
printf(“Welcome”);
printf(str);

return 0;
}

4. You are developing a web service, which accepts the email title ‘title’ and body ‘body’ from
users, and forwards them to fake-addr@[Link]. This is achieved by the following program.
Identify the security problems in this piece of program

void send_mail(char* body, char* title) {


FILE* mail_stdin;
char buf[512];
sprintf(buf, "mail -s \“Subject: %s\" fake-addr@[Link]", title);
mail_stdin = popen(buf, "w");
fprintf(mail_stdin, body);
pclose(mail_stdin);
}

5. The following program implements a function in a network socket: ‘get_two_vars’. It receives


two packets, and concatenates the data into a buffer. Use an example to show this program has
integer overflow vulnerability. Note the first integer in the received buffer from ‘recv’ denotes
the size of the buffer.
int get_two_vars(int sock, char *out, int len){
char buf1[512], buf2[512];
int size1, size2;
int size;

if(recv(sock, buf1, sizeof(buf1), 0) < 0)


return -1;
if(recv(sock, buf2, sizeof(buf2), 0) < 0)
return -1;

memcpy(&size1, buf1, sizeof(int));


memcpy(&size2, buf2, sizeof(int));
size = size1 + size2;
if(size > len)
return -1;
memcpy(out, buf1, size1);
memcpy(out + size1, buf2, size2);
return size;
}

Common questions

Powered by AI

The primary security implication of a format string vulnerability is that it allows an attacker to execute arbitrary code or cause a program crash by manipulating the memory. This vulnerability occurs when user inputs are incorrectly used in functions like printf without appropriate format specifiers, leading to potential leaks of sensitive data. To prevent this vulnerability, developers should explicitly define format strings, avoiding user input directly in them, such as using printf("%s", user_input) instead of printf(user_input), and employ functions that provide bounds checking like snprintf .

Preventative measures against Cross-Site Scripting (XSS) attacks include input validation and output encoding. Input validation involves sanitizing user inputs to eliminate scripts or special characters that could be malicious. Output encoding means ensuring that any data sent to the browser is encoded correctly to display as text, not executable code. Additional measures include Content Security Policy (CSP) to restrict resources the user agent can load, and implementing secure cookies with HTTPOnly and Secure flags, and using frameworks that automatically guard against XSS, like AngularJS .

An attacker exploits a format string vulnerability with "%x" format to read stack data, potentially exposing confidential information like memory addresses or sensitive plain text data. Using "%x", an attacker can instruct the program to output stack contents, revealing data unintentionally stored in memory areas accessible through stack access. This technique allows attackers to explore memory layout and access data normally protected by the program .

A stored XSS attack involves injecting malicious scripts into a web application where they are stored on the server, such as in databases, and then served to users without further validation, compromising multiple user sessions over time. In contrast, a reflected XSS attack involves reflecting the malicious script off a web server via a request requiring user interaction, typically affecting the user who clicked the crafted link. Stored XSS is more persistent as the script resides on the server, whereas reflected XSS typically requires user-initiated interaction .

Integer overflow vulnerabilities occur when arithmetic operations exceed the maximum size that a data type can store, causing a wrap-around effect. In C networking socket functions, this can manifest during buffer size calculations. For instance, when calculating total sizes for buffers received from a socket in 'get_two_vars', adding two large integers representing buffer sizes can overflow, leading to incorrect allocation causing buffer overflow. Mitigation involves explicitly checking for overflow before any arithmetic operation or using safe arithmetic libraries that detect and prevent overflows, such as using 'if (__builtin_add_overflow(size1, size2, &size))' instead of 'size1 + size2' .

Input validation is critical to prevent vulnerabilities in C programs handling network data since it guards against malformed input that could exploit weaknesses like buffer overflows or injection sites. Effective strategies include defining clear specifications on expected input, validating size limits before operations, and employing character whitelist or regex to validate content structure. By verifying and cleansing inputs from network sources, programmers can reduce the potential attack surface exploited by attackers to compromise memory or execute unauthorized commands .

Incrementing UINT_MAX, which represents the maximum value an unsigned integer can hold, causes a wrap-around back to 0 because the fixed-size integer's bit pattern resets, exceeding its binary capacity without overflow detection. This wrap-around can have security implications if unanticipated, such as converting large counters into small ones, breaking algorithm assumptions, and leading potentially to incorrect logic paths or resource mismanagement in critical systems .

A buffer overflow can occur in a C program when a function that doesn't check input size, such as strcpy or sprintf, is used incorrectly. For example, if a fixed-size buffer of 256 bytes is used to store user input through strcpy(buffer, userInput), and the user provides more than 256 bytes, the overflow causes adjacent memory corruption with possible execution of arbitrary code. This is preventable by using safer functions like strncpy or snprintf, which allows specifying the maximum number of bytes to copy, thereby preventing overflows .

Using popen() for executing command-line programs like mail in security-sensitive functions can lead to command injection if user input is not sanitized. This happens when user-supplied data contains termination or injection permission, leading to arbitrary shell command execution. A secure alternative is to use library functions designed for email sending, such as SMTP libraries where email components are composed through safer, API-based interactions rather than shell commands, thus circumventing shell execution completely .

Using strncpy in C provides some safety over strcpy by allowing a length argument to prevent overflowing the destination buffer. However, if the source string is the same size as or longer than this argument, strncpy does not null-terminate the destination unless explicitly done by the programmer, potentially causing undefined behavior if the programmer later tries to read from the resultant string as null-terminated. Therefore, it is crucial to manually null-terminate or use safer functions that automatically handle this, ensuring correct string termination .

You might also like