C Language Part1
C Language Part1
Institute for
Cybersecurity
Recap
Feb 14 - 2023
• Mark Dowd, John McDonald, Justin Schuh. The Art of Software Security
Assessment: Identifying and Preventing Software Vulnerabilities. Addison-Wesley
Professional, (2006).
• https://www.open-std.org/jtc1/sc22/wg14/www/standards
Canadian
Institute for
Cybersecurity
Outline
v A code reviewer examining an application binary at the assembly level can see
explicitly how data is stored and manipulated as well as the exact implications of an
operation on a piece of data. However, when you’re reviewing an application at the
source code level, some details are abstracted and less obvious.
Canadian
Institute for
Programming Language Issues Cybersecurity
- Review the basics of C types—specifically, their storage sizes, value ranges, and
representations.
- binary encoding, twos complement arithmetic, and byte order conventions,
• Each standard type has a corresponding unsigned type that takes the same amount of
storage.
Canadian
Institute for
Data Storage Overview Cybersecurity
• Floating types—There are three real floating types and three complex types. The real
floating types are float, double, and long double. The three complex types are float
_Complex, double_Complex, and long double _Complex.
• Bit fields—A bit field is a specific number of bits in an object. Bit fields can be signed
or unsigned, depending on their declaration. If no sign type specifier is given, the sign
of the bit field is implementation dependent.
Canadian
Institute for
Data Storage Overview Cybersecurity
Bit fields might be unfamiliar to some programmers, as they usually aren’t present outside
network code or low-level code. Here’s a brief example of a bit field:
- The other integer types have certain ranges of values they are required to
be able to represent, and they must maintain certain relationships with
each other (long can’t be smaller than short, for example), but otherwise,
their implementation largely depends on their architecture and compiler.
id refers to a 4-bit unsigned variable, and tflag and rflag refer to single bits. ack
is a 2-bit variable, seqnum is an 8-bit variable, and code is a 16-bit variable
Canadian
Institute for
Data Storage Overview Cybersecurity
Most programming languages have the concept of signed and unsigned integers.
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Data Storage Overview Cybersecurity
Most programming languages have the concept of signed and unsigned integers.
• Unsigned integer values are encoded in pure binary form, which is a base-two numbering
system.
• Each bit is a 1 or 0, indicating whether the power of two that the bit’s position represents is
contributing to the number’s total value..
The C standard give three possible arithmetic schemes for integers and, therefore, three
possible interpretations for the sign bit:
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2218.htm#biblio-c11
Canadian
Institute for
Cybersecurity
v Ones complement:
- Again, the sign bit is 1 if the number is negative and 0 if the number is positive.
- Positive values can be read directly from the value bits. However, negative values can’t be read
directly;
- the whole number must be negated first. In ones complement, a number is negated by inverting
all its bits. To find the value of a negative number, you have to invert its bits.
v Twos complement: The sign bit is 1 if the number is negative and 0 if the number is positive.
- You can read positive values directly from the value bits, but you can’t read negative values
directly;
- you have to negate the whole number first.
- In twos complement, a number is negated by inverting all the bits and then adding one.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2218.htm#biblio-c11
Canadian
Binary Encoding Institute for
Cybersecurity
6 0 1 1 0 -6 1 0 0 1
1
Compliment
me to get 1 0 0 1
the negative 2’s Complement
1
number by
handshaking
1 0 1 0
https://www.exploringbinary.com/twos-complement-converter/
Canadian
Institute for
Cybersecurity
Q. Find Two’s-complement of 15 ?
Q. 1101 0110 is unknown negative number ? How can you find the value of it.
Canadian
Binary Encoding Institute for
Cybersecurity
Canadian
Binary Encoding Institute for
Cybersecurity
-2(n-1) to +2(n-1) -1
-2(4-1) – 1 to +2(4-1) – 1
-2(3) – 1 to +2(3) – 1
-7 to +7
+0 or 00002
-0 or 10002.
• Integers are usually represented internally by using twos complement, especially in modern
computers.
- As mentioned, twos complement encodes positive values in standard binary encoding.
- The range of positive values that can be represented is based on the number of value bits.
- A two’s complement 8-bit signed integer has 7 value bits and 1 sign bit.
- It can represent the positive values 0 to 127 in the 7 value bits.
- All negative values represented with twos complement encoding require the sign bit to be set.
- The values from -128 to -1 can be represented in the value bits when the sign bit is set, thus
allowing the 8-bit signed integer to represent -128 to 127.
0 to 1111111
Canadian
Binary Encoding Institute for
Cybersecurity
Canadian
Institute for
Data Storage Overview Cybersecurity
• We’ve learned that C’s basic integer types have minimum and maximum
possible
• We can explore what can happen when you attempt to traverse these
boundaries
We have two unsigned integer types each with the value of:
= 4,294,967,360
https://www.calculatorsoup.com/calculators/math/modulo-calculator.php
https://www.binaryhexconverter.com/hex-to-decimal-converter
- the size of the result is truncated to a size that fits into the available process
register width.
- when an integer overflow occurs, the value may wrap to result in a small or
negative number.
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Arithmetic Boundary Conditions
Unsigned int x, y ;
x=0
y = x-1
Canadian
Institute for
Arithmetic Boundary Conditions Cybersecurity
(unsigned integers)
Conditions(unsigned integers)
#include <stdio.h>
int main()
{
int x,y,z;
printf("Please enter x:"); 100000 * 200000 = 20000000000
scanf("%d",&x);
printf("Please enter y: ");
scanf("%d",&y); 100 1010 1000 0001 0111 1100 1000 0000 0000 = 35 bits
z = x*y;
printf("%d",z);
}
Canadian
Institute for
Cybersecurity
Canadian
Institute for
Cybersecurity
Unsigned Integer Boundaries
Real-world vulnerability
nresp unsigned integer is user controlled, and its purpose
is to tell the server how many responses to expect.
Canadian
Institute for
Cybersecurity
Unsigned Integer Boundaries
Ø If users specify nresp value that is large enough, a numeric overflow could
occur, and the result of the multiplication could end up being a small
number.
Canadian
Institute for
Cybersecurity
Unsigned Integer Boundaries
• 0x40000020 à 1073741856
= 100000080 % 100000000
• 0x80 à 128 bytes are allocated
• For loop attempts to retrieve 0x40000020 strings from the packet! This turned out to be
a critical remotely exploitable vulnerability.
• A clever attacker might be able to leverage this overflow to take control of the
application, depending on the low-level details of the process’s runtime environment.
Canadian
Institute for
Cybersecurity
Unsigned Integer Boundaries
(unsigned integers)
• A numeric overflow or underflow that occurs early in a block of code can lead to a
subtle series of cascading faults; not only is the result of a single arithmetic operation
tainted, but every subsequent operation using that tainted result introduces a point
where an attacker might have unexpected influence.
Reason : execution of a data conversion from 64-bit floating point to 16-bit signed
integer value. The floating point number which was converted had a value greater than
what could be represented by a 16-bit signed integer. - internal SRI* software
exception (altitude information )
source: http://www-users.math.umn.edu/~arnold/disasters/ariane.html
Canadian
Institute for
Cybersecurity
Signed Integer Boundaries
• 2147483632
• 256
adding the left side with ones, the negative sign and the
value of the original number are maintained.
Canadian
Institute for
Cybersecurity
Value-preserving??
• The unsigned int can't hold any of the negative values a signed int can
represent.
Canadian
Institute for
Cybersecurity
https://www.rapidtables.com/convert/number/decimal-to-binary.html
Canadian
Institute for
Cybersecurity
• The bit pattern is left alone, and the value is interpreted in the context of the
new type