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 ...
随机推荐
- Cache-control
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...
- 局域网指定 IP 地址后无法上网的问题
子网掩码.默认网关.DNS 与局域网设置有关,建议指定前先 运行 cmd -> ipconfig /all 查看一下自动获取的信息. 另外留意指定IP 后需打开高级设置 -> WINS,勾 ...
- openwrt time sycronize
三行命令搞定这个. opkg update opkg install ntpclient ntpclient -s -c 0 -h ntp.sjtu.edu.cn 最后把这个 放到 rc.local ...
- Ngnix安装
一.pcre安装 yum install pcre 或 https://sourceforge.net/projects/pcre/files/pcre/8.37/ 手动下载后上传至linux 1.y ...
- canvas-画七巧板
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 手贱随手在Linux敲了 as 命令,出不来了
手贱随手在Linux敲了 as 命令,出不了命令,问问度娘吧,得到下列资料 as命令 GNU组织推出的一款汇编语言编译器,它支持多种不同类型的处理器.语法as(选项)(参数)选项-ac:忽略失败条 ...
- C# List<T>转为 DataTable
// remove "this" if not on C# 3.0 / .NET 3.5 public static System.Data.DataTable ConvertTo ...
- QueryFilter与SpatialFilter - 浅谈
我们知道,GIS不仅仅有属性查询,还有空间查询.而 QueryFilter 对应于 属性查询,而 SpatialFilter 对应于 空间查询.
- 打开较大存储量的.sql文件时,出现SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问
1. “消息 15281,级别 16,状态 1,过程 xp_cmdshell,第 1 行SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访 ...
- MVC 4.0项目部署在IIS上无法浏览的解决方案
本文属于原创,转载请标明出处! MVC 4.0发布后部署到IIS上可能出现无法浏览的问题,浏览器报403的错误. 解决方法是:只需在web.config配置文件里的<system.webServ ...