﻿{"id":211,"date":"2009-08-22T19:50:08","date_gmt":"2009-08-22T19:50:08","guid":{"rendered":""},"modified":"2011-11-18T17:02:25","modified_gmt":"2011-11-18T09:02:25","slug":"211","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/211.html","title":{"rendered":"Memcached \u7684\u6f0f\u6d1e"},"content":{"rendered":"<p><span style=\"font-size: x-small;\">\u5728\u73b0\u5728\u7684\u7f51\u7edc<span class=\"t_tag\" onclick=\"tagshow(event)\">\u67b6\u6784<\/span>\u4e2d\uff0c\u5927\u578b\u7f51\u7ad9\u4e00\u822c\u90fd\u4f1a\u4e0a Memcached \u7528\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u964d\u4f4e\u9759\u6001\u6570\u636e\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/span><\/p>\n<p>\u4eca\u5929\u770b\u5230\u4e00\u4e2amemcached\u7684\u6574\u6570\u6ea2\u51fa<\/p>\n<p><a href=\"http:\/\/cve.mitre.org\/cgi-bin\/cvename.cgi?name=CVE-2009-2415\" target=\"_blank\"><span style=\"font-size: x-small;\">http:\/\/cve.mitre.org\/cgi-bin\/cvename.cgi?name=CVE-2009-2415<\/span><\/a><\/p>\n<p><span style=\"font-size: x-small;\">\u63cf\u8ff0\u5982\u4e0b\uff1a<br \/>\nMultiple integer overflows in memcached 1.1.12 and 1.2.2 allow remote attackers to execute arbitrary code via vectors involving length attributes that trigger heap-based buffer overflows. <\/span><\/p>\n<p>\u6211\u7ffb\u4e86\u4e0b diff\uff0c\u4e3b\u8981\u662f\u51e0\u4e2a\u6570\u636e\u7c7b\u578b\u90fd\u7528\u7684\u662f int, \u6240\u4ee5patch \u5c31\u662f\u6539\u6210 size_t, \u5373 unsigned int<br \/>\n\u6bd4\u5982\uff1a<br \/>\n\/* data for the swallow state *\/<br \/>\n&#8211;\u00a0 \u00a0 int\u00a0 \u00a0 sbytes;\u00a0 \u00a0 \/* how many bytes to swallow *\/<br \/>\n+\u00a0 \u00a0 size_t\u00a0 \u00a0 sbytes;\u00a0 \u00a0 \/* how many bytes to swallow *\/<\/p>\n<p>&#8230;..<\/p>\n<p>@@ -514,11 +515,12 @@<br \/>\nchar key[251];<br \/>\nint flags;<br \/>\ntime_t expire;<br \/>\n&#8211;\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0int len, res;<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0int res;<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0size_t len;<br \/>\nitem *it;<\/p>\n<p>&#8211;\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0res = sscanf(command, &#8220;%*s %250s %u %ld %d<br \/>\n&#8220;, key, &amp;flags, &amp;expire, &amp;len);<br \/>\n&#8211;\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0if (res!=4 || strlen(key)==0 ) {<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0res = sscanf(command, &#8220;%*s %250s %u %ld %zu<br \/>\n&#8220;, key, &amp;flags, &amp;expire, &amp;len);<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0if (res!=4 || strlen(key)==0 || SIZE_MAX &#8211; 2 &lt; len) {<br \/>\nout_string(c, &#8220;CLIENT_ERROR bad command line format&#8221;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>&#8230;&#8230;<\/p>\n<p>@@ -528,6 +530,11 @@<br \/>\nout_string(c, &#8220;SERVER_ERROR out of memory&#8221;);<br \/>\n\/* swallow the data line *\/<br \/>\nc-&gt;write_and_go = conn_swallow;<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0if(SIZE_MAX &#8211; 2 &lt; len){<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 out_string(c, &#8220;SERVER_ERROR out of memory&#8221;);<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 return;<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0}<br \/>\n+<br \/>\nc-&gt;sbytes = len+2;<br \/>\nreturn;<br \/>\n}<\/p>\n<p>&#8230;&#8230;<\/p>\n<p>@@ -41,13 +42,21 @@<br \/>\n}<\/p>\n<p>-item *item_alloc(char *key, int flags, time_t exptime, int nbytes) {<br \/>\n&#8211;\u00a0 \u00a0 int ntotal, len;<br \/>\n+item *item_alloc(char *key, int flags, time_t exptime, size_t nbytes) {<br \/>\n+\u00a0 \u00a0 size_t ntotal, len;<br \/>\nitem *it;<br \/>\nunsigned int id;<\/p>\n<p>len = strlen(key) + 1; if(len % 4) len += 4 &#8211; (len % 4);<br \/>\n&#8211;\u00a0 \u00a0 ntotal = sizeof(item) + len + nbytes;<br \/>\n+<br \/>\n+\u00a0 \u00a0 if(SIZE_MAX &#8211; sizeof(item) &gt; len){<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0ntotal = sizeof(item) + len;<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0if(SIZE_MAX &#8211; ntotal &gt; nbytes)<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0ntotal += nbytes;<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0else<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0return 0;<br \/>\n+\u00a0 \u00a0 } else<br \/>\n+\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0return 0;<\/p>\n<p>id = slabs_clsid(ntotal);<br \/>\nif (id == 0)<\/p>\n<p>&#8230;&#8230;<\/p>\n<p>@@ -95,7 +104,7 @@<br \/>\nit-&gt;it_flags = 0;<br \/>\nit-&gt;nkey = len;<br \/>\nit-&gt;nbytes = nbytes;<br \/>\n&#8211;\u00a0 \u00a0 strcpy(ITEM_key(it), key);<br \/>\n+\u00a0 \u00a0 strncpy(ITEM_key(it), key, len);<br \/>\nit-&gt;exptime = exptime;<br \/>\nit-&gt;flags = flags;<br \/>\nreturn it;<\/p>\n<p>\u5728\u9ad8\u7248\u672cOS\u4e0a\u5229\u7528\u5806\u6ea2\u51fa\u8fd8\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\uff0c\u5229\u7528\u5931\u8d25\u5f88\u53ef\u80fd\u4f1acrash\u6389 memcached \u670d\u52a1\u3002\u5bf9\u4e8e\u7f51\u7ad9\u6765\u8bf4\uff0cmemcached crash\u6389\u4e5f\u4f1a\u662f\u4e00\u4ef6\u5f88\u75db\u82e6\u7684\u4e8b\u60c5\u3002<\/p>\n<p>\u8981\u4ece\u5916\u90e8\u5229\u7528 memcached\u7684\u6f0f\u6d1e\u7684\u8bdd\uff0c\u9700\u8981\u80fd\u4ece\u5916\u90e8\u4f20\u53c2\u6570\u8fdb\u5165memcached\u3002\u56e0\u4e3a\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u5f80memcached\u5185\u5b58\u5165 key value \u5bf9\uff0c\u5982\u679ckey \u6216\u8005 value \u80fd\u591f\u7531\u7528\u6237\u8f93\u5165\u63a7\u5236\uff0c\u5c31\u6709\u5b9e\u65bd\u653b\u51fb\u7684\u53ef\u80fd\u3002<\/p>\n<p>\u5728\u73b0\u5b9e\u73af\u5883\u4e2d\uff0c\u7eaf\u9ed1\u76d2\u7684\u6e17\u900f\u4e0b\u8fd8\u662f\u6bd4\u8f83\u56f0\u96be\u7684\uff0c\u56e0\u4e3a\u4e0d\u77e5\u9053\u7a0b\u5e8f\u7684\u903b\u8f91\u662f\u4ec0\u4e48\u6837\u7684\uff0c\u4e0d\u77e5\u9053\u54ea\u4e9b\u4e1c\u897f\u5b58\u5165\u4e86memcached\uff0c\u4e0d\u77e5\u9053\u591a\u957f\u65f6\u95f4\u5237\u65b0\u4e00\u6b21cache\u3002<\/p>\n<p>\u968f\u540e\u6211\u7a0d\u5fae\u67e5\u4e86\u4e0b memcached \u7684\u6f0f\u6d1e\uff0c\u53d1\u73b0\u4e3b\u8981\u4ee5\u5806\u6ea2\u51fa\u4e3a\u4e3b\uff0c\u8fd8\u662f\u9646\u7eed\u5b58\u5728\u4e00\u4e9b\u6f0f\u6d1e\u7684\uff0c\u4f46\u662f\u611f\u89c9\u7814\u7a76\u8fd9\u5757\u7684\u4eba\u4e0d\u591a\uff0c\u53ef\u80fd\u548c\u5b89\u5168\u4eba\u5458\u63a5\u89e6memcached\u4e0d\u591a\u6709\u5173\u7cfb\u3002\u800c\u5728\u5b98\u65b9\u7f51\u7ad9\u4e0a\uff0c\u4e5f\u53ea\u662f\u8f7b\u63cf\u6de1\u5199\u7684\u63cf\u8ff0\u4e86\u51e0\u53e5\u66f4\u65b0\uff0c\u6ca1\u6709\u4efb\u4f55\u7ec6\u8282\u548c\u5206\u6790\u3002<\/p>\n<p>\u5b89\u5168\u4eba\u5458\u548c\u751f\u4ea7\u5b9e\u9645\u6709\u70b9\u8131\u8282\uff0c\u73b0\u5728\u6d41\u884c\u67b6\u6784\u91cc\u7528\u5230\u7684\u5f88\u591a\u4e1c\u897f\u7814\u7a76\u5176\u5b89\u5168\u7684\u4eba\u90fd\u8fd8\u4e0d\u591a\uff0c\u4e3b\u8981\u662f\u7531\u4e8e\u5b89\u5168\u7814\u7a76\u4eba\u5458\u7684\u77e5\u8bc6\u9762\u548c\u6240\u5904\u73af\u5883\u9020\u6210\u7684\uff0c\u5f88\u591a\u4f01\u4e1a\u7ea7\u5e94\u7528\u5b89\u5168\u4eba\u5458\u6ca1\u6709\u73af\u5883\u63a5\u89e6\u5230\u3002\u4e5f\u8bb8\u4ee5\u540e\u4f1a\u6162\u6162\u6539\u53d8\u8fd9\u79cd\u73b0\u72b6\u5427.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u73b0\u5728\u7684\u7f51\u7edc\u67b6\u6784\u4e2d\uff0c\u5927\u578b\u7f51\u7ad9\u4e00\u822c\u90fd\u4f1a\u4e0a Memcached \u7528\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u964d\u4f4e\u9759 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/211.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[112],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","tag-memcached"],"views":789,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}