1. Homepage
  2. Exam
  3. [2019] INFR11098 SECURE PROGRAMMING - Final Exam - Q1 Integer Conversion

[2019] INFR11098 SECURE PROGRAMMING - Final Exam - Q1 Integer Conversion

This question has been solved
Engage in a Conversation

1. This question is about integer conversions and their impact on software security. Recall that when casting from a small datatype to a larger one, conversion may be a zero extension, where higher bits are zeroed, or a sign extension, where the top bit (the sign bit in two’s complement) is copied into higher bits. The picture below shows sign extension of 32-bit integers to 64-bit integers: CourseNana.COM

  CourseNana.COM

(a) Give two reasons why sign extension is a concern for secure programming. [2 marks] CourseNana.COM

(b) For the byte 0xF0, show two 32-bit conversions: zero extended to unsigned int, and sign-extended to int. State the corresponding decimal values. [2 marks] CourseNana.COM

(c) Consider the following C language functions. For each function, explain whether or not you consider it has a sign-related vulnerability, and if there is one, how to fix it CourseNana.COM

  CourseNana.COM

i. int readnet(int socketfd){ CourseNana.COM

            int length; CourseNana.COM

            char buffer[1024]; CourseNana.COM

            length = get_user_length(socketfd); CourseNana.COM

            if (length > 1024) { CourseNana.COM

            error(“Input size too large\n”); return -1; CourseNana.COM

} else { CourseNana.COM

            error(“Format error\n”); return -1; CourseNana.COM

} CourseNana.COM

return 0; CourseNana.COM

} CourseNana.COM

  CourseNana.COM

ii. char *readnet(int socketfd) { CourseNana.COM

            char* buffer; CourseNana.COM

  CourseNana.COM

            if (!(buffer = (char*) malloc(MAXCHARS))) { CourseNana.COM

            dia(“Couldn’t malloc\n”); CourseNana.COM

} CourseNana.COM

  CourseNana.COM

int length = get_user_length(socketfd); CourseNana.COM

if (length < 0 || length + 1 >= MAXCHARS) { CourseNana.COM

            free(buf); die(“Bad input size\n”); CourseNana.COM

} CourseNana.COM

if (read(socketfd, buffer, length) <= 0) { CourseNana.COM

            free(buf); die(“Format error\n”); CourseNana.COM

} CourseNana.COM

return buf; CourseNana.COM

} CourseNana.COM

  CourseNana.COM

iii. static const char table[UCHAR_MAX] = {‘a’, ‘b’ /* more … */}; CourseNana.COM

ptrdiff_t first_not_in_table(const char *c_str) { CourseNana.COM

            for (const char *s = c_str; *s; ++s) { CourseNana.COM

                        if (table[(unsigned int) * s] != *s){ CourseNana.COM

            return s - c_str; CourseNana.COM

} CourseNana.COM

} CourseNana.COM

return -1; CourseNana.COM

} CourseNana.COM

(d) A static analysis tool to help avoid sign extension vulnerabilities is being designed. It works on an intermediate representation (IR) with registers of different sizes. Arithmetic and logic operations require operands of equal bitsize and explicit operations for sign extension, SXTB, and zero extension, ZXTB, extend an argument register from b < B to B bits. CourseNana.COM

  CourseNana.COM

The analysis uses types to track the upper and sign bits, shown below: CourseNana.COM

A         arbitrary contents in upper B - b bits CourseNana.COM

Z          zero in upper B - b bits CourseNana.COM

S          upper B - b bits are equal to the b-value sign bit CourseNana.COM

ZS        both upper B - b bits and b-value sign bit are zero CourseNana.COM

  CourseNana.COM

Type checking rules for the IR track the signed status of registers, approximately but as closely as possible. Memory locations are not tracked so are assumed to have type A. To simplify, we may consider the fixed case when b = 16, B = 32. Here is a partial table of typing rules: CourseNana.COM

  CourseNana.COM

ZXT A = Z CourseNana.COM

ZXT Z = ? CourseNana.COM

ZXT S = Z CourseNana.COM

ZXT ZS = ZS CourseNana.COM

  CourseNana.COM

SXT A = S CourseNana.COM

SXT Z = ? CourseNana.COM

SXT S = S CourseNana.COM

SXT ZS = ZS CourseNana.COM

  CourseNana.COM

Z + Z = A CourseNana.COM

Z + S = ? CourseNana.COM

ZS + ZS = Z CourseNana.COM

ZS - ZS = ? CourseNana.COM

S / S = S CourseNana.COM

  CourseNana.COM

Z AND  A = ? CourseNana.COM

ZS OR ZS = ZS CourseNana.COM

ZS OR ZS = ZS CourseNana.COM

S LRS A = ? CourseNana.COM

S SRS A = S CourseNana.COM

  CourseNana.COM

(LRS denotes logical right shift, SRS denotes arithmetic right shift). CourseNana.COM

Answer the following questions concerning this security analysis. CourseNana.COM

i.          Give an example value that has type Z but not S. [1 mark] CourseNana.COM

ii.         Register R16 is an 16-bit register containing 0x40FF loaded from memory. What types are given to SXT32R16 and ZXT32R16? [2 marks] CourseNana.COM

iii.        The typing rules can be used to spot “odd” operations in code. Explain two examples of typing rules which might arise from faulty code. [2 marks] CourseNana.COM

iv.        What other type of programming vulnerability do you think an analysis like this might be able to warn about or rule out? [1 mark] CourseNana.COM

v.         Complete the table given above by filling in the ? cases (in your answers, please write the complete rule). [6 marks] CourseNana.COM

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
UNIVERSITY OF EDINBURGH代写,INFR11098代写,SECURE PROGRAMMING代写,UNIVERSITY OF EDINBURGH代编,INFR11098代编,SECURE PROGRAMMING代编,UNIVERSITY OF EDINBURGH代考,INFR11098代考,SECURE PROGRAMMING代考,UNIVERSITY OF EDINBURGHhelp,INFR11098help,SECURE PROGRAMMINGhelp,UNIVERSITY OF EDINBURGH作业代写,INFR11098作业代写,SECURE PROGRAMMING作业代写,UNIVERSITY OF EDINBURGH编程代写,INFR11098编程代写,SECURE PROGRAMMING编程代写,UNIVERSITY OF EDINBURGHprogramming help,INFR11098programming help,SECURE PROGRAMMINGprogramming help,UNIVERSITY OF EDINBURGHassignment help,INFR11098assignment help,SECURE PROGRAMMINGassignment help,UNIVERSITY OF EDINBURGHsolution,INFR11098solution,SECURE PROGRAMMINGsolution,