chapter 2: Representing and manipulating information
C allows conversion between unsigned and signed. The rule is that the underlying bit representation is not changed. Generally, most numbers are signed by default.For example, when declaring a
constant such as 12345 or 0x1A2B, the value is considered signed. Adding character ‘U’ or ‘u’ as a suffix creates an unsigned constant, e.g., 12345U or 0x1A2Bu.
When printing numeric values with printf, the directives %d, %u, and %x are used to print a number as a signed decimal, an unsigned decimal, and in hexadecimal format, respectively. Note that printf does not make use of any type information, and so it is possible to print a value of type int with directive %u and
a value of type unsigned with directive %d. For example, consider the following code:
int x = -1;
unsigned u = 2147483648; /* 2 to the 31st */
printf("x = %u = %d\n", x, x);
printf("u = %u = %d\n", u, u);
When run on a 32-bit machine, it prints the following:
x = 4294967295 = -1
u = 2147483648 = -2147483648
buger from:
Suppose, however, that some malicious programmer writes code that calls copy_from_kernel with
a negative value of maxlen. Then the minimum computation on line 16 will compute this value for len,
which will then be passed as the parameter n to memcpy. Note, however, that parameter n is declared as
having data type size_t. This data type is declared (via typedef) in the library file stdio.h. Typically it is defined to be unsigned int on 32-bit machines. Since argument n is unsigned, memcpy will treat it as a very large, positive number and attempt to copy that many bytes from the kernel region to the user’s buffer. Copying that many bytes (at least 231) will not actually work, because the program will encounter invalid addresses in the process, but the program could read regions of the kernel memory for which it is not authorized.
We can see that this problem arises due to the mismatch between data types: in one place the
length parameter is signed; in another place it is unsigned. Such mismatches can be a source of bugs
and, as this example shows, can even lead to security vulnerabilities. Fortunately, there were no reported cases where a programmer had exploited the vulnerability in FreeBSD. They issued a security advisory, “FreeBSD-SA-02:38.signed-error,” advising system administrators on how to apply a patch that would remove the vulnerability. The bug can be fixed by declaring parameter maxlen to copy_from_kernel
to be of type size_t, to be consistent with parameter n of memcpy. We should also declare local variable len and the return value to be of type size_t.
chapter 2: Representing and manipulating information的更多相关文章
- 《CSAPP》读书杂记 - Chapter 2. Representing and Manipulating Information
1. 一段查看地址内容的代码 代码: #include <stdio.h> typedef unsigned char *byte_pointer; void show_bytes(byt ...
- Chap 2 Representing and Manipulating Information (CS:APP)
-------------------------------------------------------------------------------------------------- A ...
- MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
- 六星经典CSAPP笔记(2)信息的操作和表示
2.Representing and Manipulating Information 本章从二进制.字长.字节序,一直讲到布尔代数.位运算,最后无符号.有符号整数.浮点数的表示和运算.诚然有些地方的 ...
- 【CSAPP笔记】1. 位、字节、整型
<Computer Systems a Programmer's Perspective>,机械工业出版社.中文译名<深入理解计算机系统>.作者:(美)Randal E.Bry ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...
- Modules you should know in Python Libray
前两天被问到常用的python lib和module有哪些?最常用的那几个,其他的一下子竟然回答不上.想想也是,一般情况下,遇到一个问题,在网上一搜,顺着线索找到可用的例子,然后基本没有怎么深究.结果 ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...
随机推荐
- iOS语音识别,语音播报,文字变语音播报,语音变文字
首先使用的是科大讯飞的sdk 1.语音识别部分 AppDelegate.m #import "AppDelegate.h" #import <iflyMSC/iflyMSC. ...
- C语言--对数组地址的解析
在C编程中,我们进程会用到数组,这看起来很简单,因为,数组就是存储相同类型元素的集合嘛,不过,当你还没考虑到数组的地址问题时,一切都是简单的,如果你接触了数组中的地址概念,也许你会改变你的想法. 下面 ...
- Java 之 StringTokenizer
class StringTokenizer Object to: break a string into tokens. Contructs StringTokenizer(String str) ...
- 第三章 Android绘图机制与处理技巧
1.屏幕尺寸信息 屏幕大小:屏幕对角线长度,单位“寸”:分辨率:手机屏幕像素点个数,例如720x1280分辨率:PPI(Pixels Per Inch):即DPI(Dots Per Inch),它是对 ...
- EF中的自动追踪与代理
自动追踪 EF框架会自动追踪实体的变化(通过比较实体的当前值与原始值). 默认情况下,以下方法会自动触发实体变化的追踪 DbSet.Find DbSet.Local DbSet.Remove DbSe ...
- C# winform DataTable 批量数据处理 增、删、改 .
1.批量新增,采用高效的SqlBulkCopy SqlBulkCopy DTS = new System.Data.SqlClient.SqlBulkCopy(con); DTS.NotifyAfte ...
- angularjs某些指令在外部作用域继承并创建新的子作用域引申出的“值复制”与“引用复制”的问题
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- [Leetcode][021] Merge Two Sorted Lists (Java)
题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 ...
- sql 查找数据库中某字符串所在的表及字段
declare @str varchar(100) set @str='是否严格控制' --要搜索的字符串 declare @s varchar(8 ...
- php之类,对象(四)加载类及练习题
一.加载类:1.命名类文件的时候每个单词首字母大写,后面缀上.class.php eg: Info.class.php 在写编码时定义类名首字母大写,定义变量名小写 eg:class Ren { pu ...