2019独角兽企业重金招聘Python工程师标准>>>

Tool  nameCoverity

Brief introduction about Coverity

Prevent SQS(软件质量系统)是检测和解决C、C++、Java源代码中最严重的缺陷的领先的自动化方法。通过对您的构建环境、源代码和开发过程给出一个完整的分析,Prevent SQS建立了获得高质量软件的标准。

静态源代码分析允许我们再软件开发生命周期的早期阶段发现和修复缺陷,节省数以百万计的相关成本。Prevent SQS是业界标准,因为只有Coverity理解和掌握静态源代码分析技术所具有的严格的要求。

Common Issue and Solutions

1.Buffer not null terminated(缓冲区不以null终止)

example

 void buffer_size_example() {static char source[] = "Twenty characters!!!";char dest[10];strncpy(dest, source, strlen(dest));  //或者strncpy(dest, source,  //strlen(source)); 直接导致栈溢出。}

problem:

In the above example, a call to strncpy()generates an error because the  length of the source string is twenty characters, but the destination string can  only have a maximum of 10 characters:

Solution

一般情况下,使用strncpy时,建议将n置为dest串长度,复制完毕后,为保险起见,将 dest串最后一字符置NULL,避免发输出乱码问题。当然喽,无论是strcpy还是strncpy,保证 src串长度<dest串长度才是最重要的。

2.String not null terminated(字符串不以null结尾)

example

char *string_null_example() {        char name[1024];char *extension;string_from_net(fd, 1023, name);  // read from net, no null-terminationif (x[0] != SOME_CHAR)  {extension = process_filename(name);  // process until '\0' found}}

problem:

This example reports a defect because the name string is not null- terminated and is passed to process_filename(), which searches name until it  finds a null terminator. If name lacks a null-terminator process_filename() could potentially corrupt memory.

Solution

A quick fix for these type of defects is to null-terminate strings after  reading them in from a string null source such as string_from_net() and before  passing them to a string null sink such as process_filename().

3.Overflowed return value(返回值溢出)

example

 #include <unistd.h>#define INT_MAX 2147483647class Cell { public:int a;int *b;};void test(int x, int fd) {int y;read(fd, &y, 4);  // y is from a tainted (outside) sourceint size = y;Cell *mycell;if (size != 0) {// Overflow results from operation size * sizeof(Cell)// Overflowed value is used in memory allocation  mycell = new Cell[size];  // overflow and overflow_sink events}}

problem:

The  example has an integer overflow defect because the  integer y is from an outside (and therefore, potentially tainted) source. This value is an operator in a multiplication operation (as size), and then is used  in a sink (allocator for mycell).

Solution

应付溢出的最佳方法还是防范:充分了解数据的范围,选择恰当的变量类型。

4.Integer overflowed argument(整数参数的溢出)

example

#include <unistd.h>  #define INT_MAX 2147483647class Cell {public:int a;int *b;};void test(int x, int fd) {int y;read(fd, &y, 4);  // y is from a tainted (outside) sourceint size = y;Cell *mycell;if (size != 0) {// Overflow results from operation size * sizeof(Cell)// Overflowed value is used in memory allocation  mycell = new Cell[size];  // overflow and overflow_sink events}

problem:

The following example has an integer overflow defect because the integer y is from an outside (and therefore, potentially tainted) source.  This value is an operator in a multiplication operation (as size), and then  is used in a sink (allocator for mycell).

Solution

。。。

5.Copy into fixed size buffer(复制到固定大小的缓冲区)

example

void string_overflow_example() {        char destination_buffer[256];char source_buffer[1024];...strcpy(destination_buffer, source_buffer);}

problem:

The above example flags a defect because, for the strcpy() call,  the source string is larger than the destination string.

Solution

在往缓冲区复制数据前先对缓冲区的大小进行检测,看是否会发生缓冲区大小不够的情况。

6.Destination buffer too small(目标缓冲区太小)

example

void buffer_size_example() {    static char source[] = "Twenty characters!!!";char dest[10];strncpy(dest, source, strlen(source));}

problem:

In the above example, a call to strncpy()generates an error  because the length of the source string is twenty characters, but the  destination string can only have a maximum of 10 characters:

Solution

dest should be length checked before being passed to the copy routine.

7.Untrusted value as argument(不受信任的值作为参数)

example

void tainted_scalar_example() {int nresp = packet_get_int();if (nresp > 0) {                                response = xmalloc(nresp * sizeof(char *));    for (i = 0; i < nresp; i++) {             // tainted scalar controls loopresponse[i] = packet_get_string(NULL); // heap corruption}}}

problem:

In the above example, the tainted integer nresp, read from a packet, is  only lower-bounds checked and not upper-bounds checked. This is a defect  because a tainted expression—(nresp * sizeof(char *))— is being passed  to xmalloc(). This expression can cause an integer overflow, which can result  in a buffer overflow, denial of service, memory corruption, or other security  vulnerability.

Solution

Properly sanitize the tainted variable before use. For example, the following is not a defect because nresp's lower and upper bounds are checked before  any dangerous uses.

Such as :

#define MAX_NRESP 256
...
void tainted_scalar_example() {int nresp = packet_get_int();  if (nresp > 0 && nresp < MAX_NRESP) {                                          response = xmalloc(nresp * sizeof(char *)); for (i = 0; i < nresp; i++) {               response[i] = packet_get_string(NULL);    }}
}

8.Insecure temporary file(不安全的临时文件)

example

void secure_temp_example() {             char *tmp, *tmp2, *tmp3;char buffer[1024];tmp = mktemp(buffer);
}

problem:

The above example generates a defect because mktemp() is  insecure—it is easy to guess the name of the temporary file it creates.  Similar functions include tmpnam(), tempnam(), and tmpfile().

Solution

When using mkstemp(), remember to safely set the umask  before to restrict the resulting temporary file permissions to only the owner. Also, do not pass on the filename to another  privileged system call.  Use the returned file descriptor instead.

9.Time of check time of use ---TOCTOU(计算机系统的资料与权限等状态的检查           与使用之间)

example

void toctou_example() {            stat(logfile, &st);if (st.st_uid != getuid())return -1;open(logfile, O_RDWR);
}

problem:

This program is susceptible to a file-based race condition because the logfile binding can possibly change between the stat() and open() calls.

Solution

http://www.ibm.com/developerworks/cn/linux/l-sprace.html

转载于:https://my.oschina.net/u/813598/blog/193475

Security issue about static code checking相关推荐

  1. flickr app android,Flickr latest Android app(Version: 2.1.5) ContentProvider security issue

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 摘要:Flickr latest Android app(Version: 2.1.5) ContentProvider ...

  2. Predicting Buffer Overflow Vulnerabilities through Mining Light-Weight Static Code Attributes

    题目: Predicting Buffer Overflow Vulnerabilities through Mining Light-Weight Static Code Attributes 作者 ...

  3. [20][04][10] Fortify Static Code Analyzer 详解

    文章目录 1. Fortify Static Code Analyzer 是什么 2. 工具介绍 3. 代码安全扫描实施步骤 3.1 清理 3.2 构建 3.3 扫描 1. Fortify Stati ...

  4. IoT reigns supreme at upcoming Embedded Linux Conference

    本文转载至:http://linuxgizmos.com/linux-foundation-details-embedded-linux-conference-agenda/ 转载说明:今年的嵌入式 ...

  5. 有哪些开源C ++静态分析工具? [关闭]

    本文翻译自:What open source C++ static analysis tools are available? [closed] Java has some very good ope ...

  6. Build a web app fast: Python, HTML JavaScript resources

    转自:http://www.pixelmonkey.org/2012/06/14/web-app Wanna build a web app fast? Know a little bit about ...

  7. vulnhub靶场-Ripper

    1.靶机信息 靶机名称:Ripper 靶机难度:简单-中等 虚拟机环境:此靶机推荐使用Virtualbox搭建 目标:取得root权限+2flag 靶机地址:https://download.vuln ...

  8. 转载:2014 Top Security Tools as Voted by ToolsWatch.org Readers

    2014年十大安全工具排行榜(来自于ToolsWatch.org读者投票) 原文地址:http://www.toolswatch.org/2015/01/2014-top-security-tools ...

  9. Characterizing, exploiting, and detecting DMA code injection vulnerabilities,Eurosys2021

    Characterizing, exploiting, and detecting DMA code injection vulnerabilities in the presence of an I ...

最新文章

  1. 四、爬虫中的urllib库使用
  2. python自动化部署程序,聊聊Python自动化脚本部署服务器全流程(详细)
  3. 已知三角形三边长怎么求面积_已知三角形三边求面积的公式——海伦公式
  4. 初步认识Volatile-什么叫缓存一致性呢?
  5. 我的10年富士康故事
  6. 吴忠军 - 如何理解马云所说的月入两三万,三四万的人最幸福?
  7. 关于使用 jquery Validate 使用出现的问题
  8. php mysql 绕过_PHP中md5绕过
  9. mysql udp服务器_netty学习:UDP服务器与Spring整合(2)
  10. java 小数如何转换成百分数_初等数学33-百分数
  11. paip.php调试不能显示局部变量内容with xdebug
  12. easyui_datagrid模板代码
  13. 如何利用Django打造一款类似于链家二手房信息查询网!厉害吧!
  14. dll依赖查看工具-depends
  15. 设置android模拟器的ip地址,安卓模拟器怎么改IP地址和分辨率,怎么改机型
  16. cad2012打开后闪退_打开CAD2012出现闪退问题的解决方法
  17. 设置计算机关机时间快捷键,电脑怎么设定关机时间?
  18. Java实现微信退付款
  19. 在PyTorch训练一个epoch时,模型不能接着训练,Dataloader卡死
  20. js日期格式转换Wed Mar 22 13:38:37 CST 2017 转换 为yyyy-mm-dd

热门文章

  1. java string 连续字符_Java中字符串中连续相同字符去重方法
  2. IDEA 配置 Docker
  3. 智能车竞赛技术报告 | 节能信标组组 - 大连民族大学 - 粉红靓车队
  4. ESP-12F模块转接板测试版调试说明,下载MicroPython程序。ESP8266-12F
  5. 全国大学生智能汽车竞赛-讯飞智慧餐厅
  6. 室外声音信标可行性分析
  7. c++编译器里的字体_从C的for和Python的for聊起
  8. 未解决计算机主机与打印机,电脑无法与打印机连接 计算机网考题目2(12)
  9. 圆周率一千万亿位_圆周率已经到了十万亿位了,为什么还要计算下去?有什么意义吗?...
  10. 华为mate8怎么申请云闪付_华为消费者管培生面试问题分享