Security Features in the CRT
Many old CRT functions have newer, more secure versions. If a secure function exists, the older, less secure version is marked as deprecated and the new version has the
_s ("secure") suffix.
In this context, "deprecated" means using the function's isn't recommended. It doesn't mean the function is scheduled to be removed from the CRT.
The secure functions don't prevent or correct security errors. Instead, they catch errors when they occur. They do additional checks for error conditions. If there is an error, they invoke an error handler (see Parameter Validation).
For example, the
strcpy function can't tell if the string it's copying is too large for the destination buffer. 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 11 characters into a 10 character buffer, that is an error on your part;
strcpy_s can't correct your mistake. But it can detect your error and inform you by invoking the invalid parameter handler.
Eliminating deprecation warnings
There are several ways to eliminate deprecation warnings for the older, less secure functions. The simplest is simply to define
_CRT_SECURE_NO_WARNINGS or use the warning pragma. Either will disable deprecation warnings, but the security issues that caused the warnings still exist. It's better to leave deprecation warnings enabled and take advantage of the new CRT security features.
In C++, the easiest way to do that is to use Secure Template Overloads. This will eliminate deprecation warnings, in many cases, by replacing calls to deprecated functions with calls to secure versions of those functions. For example, consider this deprecated call to
char szBuf; strcpy(szBuf, "test"); // warning: deprecated
_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES as 1 eliminates the warning by changing the
strcpy call to
strcpy_s, which prevents buffer overruns. For more information, see Secure Template Overloads.
For those deprecated functions without secure template overloads, you should definitely consider manually updating your code to use the secure versions.
Another source of deprecation warnings, unrelated to security, is the POSIX functions. Replace POSIX function names with their standard equivalents (for example, change access to _access), or disable POSIX-related deprecation warnings by defining
_CRT_NONSTDC_NO_WARNINGS. For more information, see Compatibility.
Additional Security Features
Some of the security features include:
Parameter Validation. Secure functions, and many of their unsecure counterparts, validate parameters. Validation may include:
- Checking for NULL values.
- Checking enumerated values for validity.
- Checking that integral values are in valid ranges.
For more information, see Parameter Validation.
A handler for invalid parameters is also accessible to the developer. When a function encounters an invalid parameter, instead of asserting and exiting the application, the CRT allows you to check these problems via _set_invalid_parameter_handler, _set_thread_local_invalid_parameter_handler.
Sized Buffers. You must pass the buffer size to any secure function that writes to a buffer. The secure versions validate that the buffer is large enough before writing to it. Which helps avoid dangerous buffer overrun errors that could allow malicious code to execute. These functions usually return an
errnoerror code and invoke the invalid parameter handler if the size of the buffer is too small. Functions that read from input buffers, such as
gets, have secure versions that require you to specify a maximum size.
Null termination. Some functions that 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 pre-existing functions. The secure functions and many of the pre-existing functions now set
errnoand often return an
errnocode 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 detected, for example, using incorrect type field characters in