{"id":1424,"date":"2015-06-04T11:48:15","date_gmt":"2015-06-04T03:48:15","guid":{"rendered":"http:\/\/blog.dynox.cn\/?p=1424"},"modified":"2016-02-26T13:33:37","modified_gmt":"2016-02-26T05:33:37","slug":"security-enhanced-crt%e8%be%b9%e7%95%8c%e4%b8%8e%e6%bd%9c%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/blog.dynox.cn\/?p=1424","title":{"rendered":"Security-Enhanced CRT,\u8fb9\u754c\u4e0e\u6f5c\u89c4\u5219"},"content":{"rendered":"<div class=\"gruber-markdown\"><p><strong><em><\/em><\/strong><span style=\"font-size: small\">Windows Security-Enhanced CRT \u76f8\u5173\u51fd\u6570(\u5373_s\u540e\u8f8d\u51fd\u6570)\u5e76\u4e0d\u662f\u5bf9\u8001\u51fd\u6570\u7684\u76f4\u63a5\u66ff\u4ee3\uff0c\u5176\u4e2d\u4e0d\u4ec5\u6709\u66f4\u4e25\u683c\u7684\u53c2\u6570\u68c0\u67e5\uff0c\u8fd8\u4f1a\u6709buffer\u7684\u91cd\u7f6e\u64cd\u4f5c\u7b49\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u4e00\u5b9a\u8981\u4ed4\u7ec6\u9605\u8bfbMSDN\u63cf\u8ff0\u6216\u8005VC runtime\u6e90\u7801 (\u4ee5Visual Studio 2008\u4e3a\u4f8b\uff1a C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\crt\\src)\u3002<\/span><\/p>  <p><span style=\"font-size: small\">\u4e0b\u9762\u4e0d\u59a8\u6d4b\u8bd53\u4e2a\u4ee3\u7801\u7247\u6bb5\uff1a <\/span><\/p>  <p><span style=\"font-size: small\">\u6d4b\u8bd5\u4ee3\u7801\u3010C1\u3011\uff1a<\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">void main() {       <br \/>&#160;&#160;&#160; char dst1[8];        <br \/>&#160;&#160;&#160; memcpy(dst1, NULL, 0);        <br \/>&#160;&#160;&#160; memcpy(NULL, NULL, 0);        <br \/>}<\/font><\/span><\/p>  <p><span style=\"font-size: small\">\u6d4b\u8bd5\u4ee3\u7801\u3010C2\u3011:<\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">void main() {<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; char dst1[8];<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; memcpy_s(dst1, 8, NULL, 0);       <br \/>&#160;&#160;&#160; memcpy_s(NULL, 0, NULL, 0);        <br \/>&#160;&#160;&#160; char *dst2 = (char *)malloc(0);        <br \/>&#160;&#160;&#160; if (dst2) {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; memcpy_s(dst2, 0, NULL, 0);        <br \/>&#160;&#160;&#160; free(dst2);        <br \/>&#160;&#160;&#160; }        <br \/>}<\/font><\/span><\/p>  <p><span style=\"font-size: small\">\u6d4b\u8bd5\u4ee3\u7801\u3010C3\u3011:<\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">void main() {<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; char dst1[8];<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; memcpy_s(NULL, 0, dst1, 8);<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; char *dst2 = (char *)malloc(0);       <br \/>&#160;&#160;&#160; if (dst2) {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; memcpy_s(dst2, 0, dst1, 8);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; free(dst2);        <br \/>&#160;&#160;&#160; }        <br \/>}<\/font><\/span><\/p>  <p><span style=\"font-size: small\">\u8fd9\u51e0\u6bb5\u4ee3\u7801\u5b9e\u9645\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u6709\u505a\uff0c\u53ea\u662f\u53c2\u6570\u6709\u53d8\u5316\u800c\u5df2\uff0c\u4f46\u6d4b\u8bd5\u4e2d\u4f1a\u53d1\u73b0\u3010C3\u3011\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u5177\u4f53\u539f\u56e0\u5c31\u662f\u56e0\u4e3amemcpy_s\u8fdb\u884c\u4e86\u66f4\u4e25\u683c\u7684\u53c2\u6570\u68c0\u6d4b\uff0c\u53ef\u5bf9\u53c2\u8003memcpy_s\u7684\u6e90\u7801\uff0c\u5f15\u7528\u81eaVisual Studio 2008 runtime source memcpy_s.c:<\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">\/***       <br \/>*&#160; memcpy_s.c - contains memcpy_s routine        <br \/>*        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; Copyright (c) Microsoft Corporation. All rights reserved.        <br \/>*        <br \/>*&#160; Purpose:        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; memcpy_s() copies a source memory buffer to a destination buffer.        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; Overlapping buffers are not treated specially, so propagation may occur.        <br \/>*        <br \/>*******************************************************************\/<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">#include &lt;cruntime.h&gt;       <br \/>#include &lt;string.h&gt;        <br \/>#include &lt;internal.h&gt;<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">\/***       <br \/>*&#160; memcpy_s - Copy source buffer to destination buffer        <br \/>*        <br \/>*&#160; Purpose:        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; memcpy_s() copies a source memory buffer to a destination memory buffer.        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; This routine does NOT recognize overlapping buffers, and thus can lead        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; to propagation.        <br \/>*        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; For cases where propagation must be avoided, memmove_s() must be used.        <br \/>*        <br \/>*&#160; Entry:        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; void *dst = pointer to destination buffer        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; size_t sizeInBytes = size in bytes of the destination buffer        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; const void *src = pointer to source buffer        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; size_t count = number of bytes to copy        <br \/>*        <br \/>*&#160; Exit:        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; Returns 0 if everything is ok, else return the error code.        <br \/>*        <br \/>*&#160; Exceptions:        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; Input parameters are validated. Refer to the validation section of the function.        <br \/>*&#160;&#160;&#160;&#160;&#160;&#160; On error, the error code is returned and the destination buffer is zeroed.        <br \/>*        <br \/>*******************************************************************\/<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">errno_t __cdecl memcpy_s(       <br \/>&#160;&#160;&#160; void * dst,        <br \/>&#160;&#160;&#160; size_t sizeInBytes,        <br \/>&#160;&#160;&#160; const void * src,        <br \/>&#160;&#160;&#160; size_t count        <br \/>)        <br \/>{        <br \/>&#160;&#160;&#160; if (count == 0)        <br \/>&#160;&#160;&#160; {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/* nothing to do *\/        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return 0;        <br \/>&#160;&#160;&#160; }<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; \/* validation section *\/       <br \/>&#160;&#160;&#160; _VALIDATE_RETURN_ERRCODE(dst != NULL, EINVAL);        <br \/>&#160;&#160;&#160; if (src == NULL || sizeInBytes &lt; count)        <br \/>&#160;&#160;&#160; {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/* zeroes the destination buffer *\/        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; memset(dst, 0, sizeInBytes);<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; _VALIDATE_RETURN_ERRCODE(src != NULL, EINVAL);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _VALIDATE_RETURN_ERRCODE(sizeInBytes &gt;= count, ERANGE);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/* useless, but prefast is confused *\/        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return EINVAL;        <br \/>&#160;&#160;&#160; }<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\">&#160;&#160;&#160; memcpy(dst, src, count);       <br \/>&#160;&#160;&#160; return 0;        <br \/>}<\/font><\/span><\/p>  <p style=\"padding-left: 30px\"><span style=\"font-size: small\"><font size=\"1\"><\/font><\/span><\/p>  <p><span style=\"font-size: small\">\u53c2\u8003\u9605\u8bfb\uff1a     <br \/><\/span><\/p>  <h4><span style=\"font-size: small\"><span style=\"font-weight: normal\">1\uff0c <\/span><\/span><span style=\"font-size: small\"><span style=\"font-weight: normal\">_snwprintf_s\u7684\u6f5c\u89c4\u5219<\/span><\/span><span style=\"font-size: small\"><span style=\"font-weight: normal\">\uff1a <\/span><\/span><a title=\"https:\/\/blog.dynox.cn\/?p=791\" href=\"https:\/\/blog.dynox.cn\/?p=791\"><span style=\"font-size: small\"><span style=\"font-weight: normal\">https:\/\/blog.dynox.cn\/?p=791<\/span><\/span><\/a>    <br \/><span style=\"font-size: small\"><span style=\"font-weight: normal\">2,&#160;&#160; Security Features in the CRT:       <br \/><\/span><span style=\"font-weight: normal\">&#160;&#160;&#160;&#160;&#160; <a href=\"https:\/\/msdn.microsoft.com\/zh-cn\/library\/vstudio\/8ef0s5kh(v=vs.110).aspx\">https:\/\/msdn.microsoft.com\/zh-cn\/library\/vstudio\/8ef0s5kh(v=vs.110).aspx<\/a><\/span><\/span><\/h4><\/div>","protected":false},"excerpt":{"rendered":"<p>Windows Security-Enhanced CRT \u76f8\u5173\u51fd\u6570(\u5373_s\u540e\u8f8d\u51fd\u6570)\u5e76\u4e0d\u662f\u5bf9\u8001\u51fd\u6570\u7684\u76f4\u63a5\u66ff\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[557,558,356,357],"views":1942,"_links":{"self":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1424"}],"collection":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1424"}],"version-history":[{"count":7,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1424\/revisions"}],"predecessor-version":[{"id":1470,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1424\/revisions\/1470"}],"wp:attachment":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}