大风起兮云飞扬
在现在的网络架构中, 大型网站一般都会上 Memcached 用于提高性能, 降低静态数据的响应速度。
今天看到一个memcached的整数溢出
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-2415
描述如下:
Multiple 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.
我翻了下 diff, 主要是几个数据类型都用的是 int, 所以patch 就是改成 size_t, 即 unsigned int
比如:
/* data for the swallow state */
- int sbytes; /* how many bytes to swallow */
+ size_t sbytes; /* how many bytes to swallow */
.....
@@ -514,11 +515,12 @@
char key[251];
int flags;
time_t expire;
- int len, res;
+ int res;
+ size_t len;
item *it;
- res = sscanf(command, "%*s %250s %u %ld %d\n", key, &flags, &expire, &len);
- if (res!=4 || strlen(key)==0 ) {
+ res = sscanf(command, "%*s %250s %u %ld %zu\n", key, &flags, &expire, &len);
+ if (res!=4 || strlen(key)==0 || SIZE_MAX - 2 < len) {
out_string(c, "CLIENT_ERROR bad command line format");
return;
}
......
@@ -528,6 +530,11 @@
out_string(c, "SERVER_ERROR out of memory");
/* swallow the data line */
c->write_and_go = conn_swallow;
+ if(SIZE_MAX - 2 < len){
+ out_string(c, "SERVER_ERROR out of memory");
+ return;
+ }
+
c->sbytes = len+2;
return;
}
......
@@ -41,13 +42,21 @@
}
-item *item_alloc(char *key, int flags, time_t exptime, int nbytes) {
- int ntotal, len;
+item *item_alloc(char *key, int flags, time_t exptime, size_t nbytes) {
+ size_t ntotal, len;
item *it;
unsigned int id;
len = strlen(key) + 1; if(len % 4) len += 4 - (len % 4);
- ntotal = sizeof(item) + len + nbytes;
+
+ if(SIZE_MAX - sizeof(item) > len){
+ ntotal = sizeof(item) + len;
+ if(SIZE_MAX - ntotal > nbytes)
+ ntotal += nbytes;
+ else
+ return 0;
+ } else
+ return 0;
id = slabs_clsid(ntotal);
if (id == 0)
......
@@ -95,7 +104,7 @@
it->it_flags = 0;
it->nkey = len;
it->nbytes = nbytes;
- strcpy(ITEM_key(it), key);
+ strncpy(ITEM_key(it), key, len);
it->exptime = exptime;
it->flags = flags;
return it;
在高版本OS上利用堆溢出还是比较麻烦的, 利用失败很可能会crash掉 memcached 服务。 对于网站来说, memcached crash掉也会是一件很痛苦的事情。
要从外部利用 memcached的漏洞的话, 需要能从外部传参数进入memcached。 因为程序可能会往memcached内存入 key value 对, 如果key 或者 value 能够由用户输入控制, 就有实施攻击的可能。
在现实环境中, 纯黑盒的渗透下还是比较困难的, 因为不知道程序的逻辑是什么样的, 不知道哪些东西存入了memcached, 不知道多长时间刷新一次cache。
随后我稍微查了下 memcached 的漏洞, 发现主要以堆溢出为主, 还是陆续存在一些漏洞的, 但是感觉研究这块的人不多, 可能和安全人员接触memcached不多有关系。 而在官方网站上, 也只是轻描淡写的描述了几句更新, 没有任何细节和分析。
安全人员和生产实际有点脱节, 现在流行架构里用到的很多东西研究其安全的人都还不多, 主要是由于安全研究人员的知识面和所处环境造成的, 很多企业级应用安全人员没有环境接触到。 也许以后会慢慢改变这种现状吧.











