strcpy_s与strcpy安全性的比较

本文探讨了在VC2005的CRT中,如何使用更安全的strcpy_s替换strcpy来防止缓冲区溢出等问题。strcpy_s通过接收目标缓冲区大小作为参数,能检测并避免潜在的错误,而strcpy可能会导致未定义的行为或内存破坏。新的安全函数加强了参数验证和错误处理,有助于提高程序的安全性和错误定位。
摘要由CSDN通过智能技术生成

在VC2005的CRT中,增加了一些具有更强安全性的CRT函数,例如strcpy_s, strncat_s等。

(MSDN: <Security Enhancements in the CRT >

Significant enhancements have been made to make the CRT more secure. Many CRT functions now have more secure versions. If a new secure function exists, the older, less secure version is marked as deprecated and the new version has the _s ("secure") suffix.

It should be noted that in this context, "deprecated" just means that a function's use is not recommended; it does not indicate that the function is scheduled to be removed from the CRT.

It should also be noted that the secure functions do not prevent or correct security errors; rather, they catch errors when they occur. They perform additional checks for error conditions, and in the case of an error, they invoke an error handler (see Parameter Validation).

For example, the strcpy function has no way of telling if the string that it's copying is too big for its destination buffer. However, its secure counterpart, strcpy_s, takes the size of the buffer as a parameter, so it can determine if a buffer overrun will occur. If you use strcpy_s to copy eleven characters into a ten-character buffer, that is an error on your part; strcpy_s cannot correct your mistake, but it can detect your error and inform you by invoking the invalid parameter handler.

一下是使用strcpy_s与strcpy的安全性比较

 char szBuf[2] = {0};

 strcpy_s(szBuf, 2, "12131");  //新的CRT函数
 strcpy(szBuf,  "12131");    //老的CRT函数

上述代码,明显有缓冲区溢出的问题。 使用strcpy_s函数则会抛出一个异常。而使用strcpy函数的结果则未定,因为它错误地改变了程序中其他部分的内存的数据,可能不会抛出异常但导致程序数据错误,也可能由于非法内存访问抛出异常。

使用新的增强安全的CRT函数有什么好处呢?简单地说,新的函数加强了对参数合法性的检查以及缓冲区边界的检查,如果发现错误,会返回errno或抛出异常。老版本的这些CRT函数则没有那么严格的检查与校验,如果错误地传输了参数或者缓冲区溢出,那么错误并不能被立刻发现,对于定位程序错误也带来更大困难。

以下是MSDN关于CRT安全增强的说明。

-------------------------------------------------------------------------------------------------------------

【MSDN】:

Some of the security enhancements are:

  • Parameter Validation. Parameters passed to CRT functions are validated, in both secure functions and in many preexisting versions of functions. These validations include:

    • Checking for NULL values passed to the functions,

    • Checking enumerated values for validity,

    • Checking that integral values are in valid ranges.

  • For more information, see Parameter Validation.

  • There is also a handler for invalid parameters which is accessible to the developer. When an invalid parameter is encountered, instead of asserting and exiting the application, the CRT provides a way to check these problems with the _set_invalid_parameter_handler function.

  • Sized Buffers. The secure functions require that the buffer size be passed to any function that writes to a buffer. The secure versions validate that the buffer is large enough before writing to it, helping to avoid dangerous buffer overrun errors which could allow malicious code to execute. These functions will usually return an errno type of error code and invoke the invalid parameter handler if the size of the buffer is too small. Functions which read from input buffers, such as gets, have secure versions that require you to specify a maximum size.

  • Null termination. Some functions which left potentially non terminated strings have secure versions which ensure that strings are properly null terminated.

  • Enhanced error reporting. The secure functions return error codes with more error information than was available with the preexisting functions. The secure functions and many of the preexisting functions now set errno and often return an errno code type as well, to provide better error reporting.

  • Filesystem security. Secure file I/O APIs support secure file access in the default case.

  • Windows security. Secure process APIs enforce security policies and allow ACLs to be specified.

  • Format string syntax checking. Invalid strings are now detected, for example using incorrect type field characters in printf format strings.

  • Additional security enhancements are described in the documentation for each function.

------------------------------------------------------------------------------------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值