CSC 1029 Week 10 Integer Vulnerabilities PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
This document covers integral data types and vulnerabilities in C/C++. It explains concepts like integer overflow, type conversions, and implicit/explicit castings. The document is likely part of a course, and the information is intended for undergraduate students of computer science.
Full Transcript
CSC 1029 INTEGRAL DATA TYPES AND VULNERABILITIES OBJECTIVES AGENDA: WEEK 10 Learn the basics of integral types in C/C++, 1. Computer Integers: Integrals including signed and unsigned 2. Fundamental Data Types Understand how integral v...
CSC 1029 INTEGRAL DATA TYPES AND VULNERABILITIES OBJECTIVES AGENDA: WEEK 10 Learn the basics of integral types in C/C++, 1. Computer Integers: Integrals including signed and unsigned 2. Fundamental Data Types Understand how integral values are stored 3. char and wchar_t in memory using bits 4. Integers: Signed and Unsigned Recognize the concept of integer overflow, 5. Integer Overflow its causes, and its potential consequences in both signed and unsigned types. 6. Type Conversion Explore implicit and explicit type 7. Integral Vulnerabilities conversions, and how they impact your 8. Data Type conversion functions code. 9. TODO Learn when and how to use casting. 10. Resources for Help Identify the issues that arise when mixing signed and unsigned types. WHY WE USE DATA TYPES IN C++ We need data types to inform the Operating System program what type of data we are handling. o Based on the type of data, it will allocate memory in bytes in the main memory for the particular data types Variables are the name given to the memory location where we store the data. The space allocated for any variable in the function's stack frame is done by subtracting from the stack pointer enough bytes to reserve the space required of the data type. COMPUTER INTEGERS Computer integers are not the same set of numbers as mathematical integers Computer integers are finite NOT infinite What happens when integer calculations result in numbers outside that set? Set carry or overflow flag in CPU Throw an exception Convert integer type to higher precision Saturation (remain at maximum/minimum value) Wrap from max to min or min to max value Depends on the language and hardware C++ FUNDAMENTAL DATA TYPES (CPLUSPLUS) Click to add text SIZEOF (CPPREFERENCE) In C++, sizeof is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof can also be used to determine the size of arrays. An array is a contiguous sequence of memory; therefore, an array of 5 ints will total 20 bytes of memory, because 5 elements * 4 bytes = 20 Pitfall – Do NOT apply the sizeof to an array argument passed into a function! WHY??? CHAR AND WCHAR The standard ASCII table defines 128 character codes (from 0 to 127), 7 bits of the 8 bit byte. The first 32 codes are control codes (non-printable), and the remaining 96 character codes are representable characters; digits, letters, symbols The char data type in C++ represents ASCII code Unicode (UTF-8 encoding) is used to store all other character sets that cannot be stored in 1 byte of memory after the 128 ascii characters. UTF-8 uses 1 to 4 bytes of memory Unicode provides a unique number for every character, no matter what the platform, program, or language The wchar_t data type in C++ represents Unicode INTEGRAL MEMORY (CPLUSPLUS) char (1 byte) 27 26 25 24 23 22 21 2 0 128 64 32 16 8 4 2 1 short (2 bytes) 215 28 27 20 32768 256 128 1 int (4 bytes) 231 224 223 216 215 28 27 20 2.1bil 16.7mil 8.3mil 65536 32768 256 128 1 SIGNED AND UNSIGNED TYPES Chars and Integers in C++ Type Typical Typical Range are either signed or Bit unsigned. Width For each signed type there is char 1 byte -127 to 127 or 0 to 255 an equivalent unsigned type. unsigned char 1 byte 0 to 255 Signed chars and integers are used to represent signed char 1 byte -127 to 127 positive and negative values int 4 bytes -2147483648 to Unsigned chars and integer 2147483647 values range from zero to a unsigned int 4 bytes 0 to 4294967295 maximum that depends on signed int 4 bytes -2147483648 to the size of the type 2147483647 https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm INTEGER OVERFLOW OR UNDERFLOW Minimum and 127 -127 maximum values for a type depend on the type’s 126 -126 representation signedness the number of allocated bits 1 -1 0 SEI CERT: ENSURE INTS DO NOT WRAP Recommendation Severity Likelihood Remediation Priority Level Cost INT30-C High Likely High P9 L2 Risk Assessment: Integer wrap can lead to buffer overflows and the execution of arbitrary code by an attacker INTEGER OVERFLOW (CPLUSPLUS) Overflow is a where operations on 2 numbers exceeds the maximum (or goes below the minimum) value the data type can have. For example, an unhandled arithmetic overflow in the engine steering software was the primary cause of the crash of the maiden flight of the Ariane 5 rocket. The software had been considered bug-free since it had been used in many previous flights; but those used smaller rockets which generated smaller accelerations than Ariane 5's. TYPE CONVERSIONS (CPLUSPLUS) short Int long double Implicit Type Conversion Automatically performed when a value is copied to a compatible type The only type conversion guaranteed safe is to a wider type of the same signedness double long Int short Explicit Type Casting Many conversions, specifically those that imply a different interpretation of the value and/or lose memory space, require an explicit conversion, known in C++ as type-casting. Avoid conversions that result in loss of data or loss of sign DATA TYPE Converting CONVERTING char to integral: atoi | atol FUNCTIONS Do NOT use the NON-ANSI Converting data from standardized itoa std::string to or ltoa functions! integral types or vice- versa. StringStream is std::stoi another option in std::stol C++ to / from std::to_string fundamental types std::to_wstring to std::string EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 10 Content Module in D2L to 100% WHAT'S COMING UP NEXT...WEEK 11 QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie o By Appointment (both on-campus and remote via Zoom) o Drop-In Times Available (on-campus) Email: [email protected] RRCC On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help