现在的位置: 首页 > 综合 > 正文

Memcached 二进制协议(BinaryProtocol) incr指令泄露内存数据的bug

2019年11月21日 ⁄ 综合 ⁄ 共 4097字 ⁄ 字号 评论关闭
文章目录

缘起

最近有个分布式限速的需求。支付宝的接口双11只允许每秒调用10次。

单机的限速,自然是用google guava的RateLimiter。

http://docs.guava-libraries.googlecode.com/git-history/master/javadoc/com/google/common/util/concurrent/RateLimiter.html

分布式的ReteLimiter,貌似没有现在的实现方案。不过用memcached或者Redis来实现一个简单的也很快。

比如上面的要求,每秒钟只允许调用10次,则按下面的流程来执行,以memcached为例:

incr alipay_ratelimiter  1 1
如果返回NOT_FOUND,则
  add alipay_ratelimiter  0  1 1
  1
  即如果alipay_ratelimiter不存在,则设置alipay_ratelimiter的值为1,过期时间为1秒。
如果incr返回不是具体的数值,则判断是否大于10,
如果大于10则要sleep等待。

上面是Memcached 文本协议的做法。因为文本协议不允许incr 设置不存在的key。

如果是二进制协议,则可以直接用incr命令设置初始值,过期时间。

memcached二进制协议的bug

上面扯远了,下面来说下memcached incr指令的bug。

在测试的时间,用XMemcached做客户端,来测试,发现有的时候,incr函数返回两个1。

于是,在命令行,用telnet来测试,结果发现有时候返回很奇怪的数据:

get alipay_ratelimiter
VALUE alipay_ratelimiter 0 22
END2446744073709551608

明显END后面跟了一些很奇怪的数据。而且返回数据的长度是22,而正确的长度应该是1。

正常的返回应该是这样的:

get alipay_ratelimiter
VALUE alipay_ratelimiter 0 4
1
END

抓包分析

开始以为是XMemcached客户端的bug,也有可能是序列化方式有问题。于是调试了下代码,没发现什么可疑的地方。

于是祭出wireshakr来抓包。发现XMemcached发出来的数据包是正常的。而且服务器的确返回了22字节的数据。

那么这个可能是Memcached本身的bug了。这个令人比较惊奇,因为Memcached本身已经开发多年,很稳定了,怎么会有这么明显的bug?

查找有问题的Memcached的版本

检查下当前的Memcahcd版本,是memcached 1.4.14。

于是去下载了最新的1.4.21版,编绎安装之后,再次测试。发现正常了。

于是到release log里查看是哪个版本修复了:

https://code.google.com/p/memcached/wiki/ReleaseNotes

发现1417版的release note里有incr相关的信息:

https://code.google.com/p/memcached/wiki/ReleaseNotes1417

Fix for incorrect length of initial value set via binary increment protocol.

查找bug发生的原因:

于是再到github上查看修改了哪些内容:

https://github.com/memcached/memcached/commit/8818bb698ea0abd5199b2792964bbc7fbe4cd845?diff=split

对比下修改内容,和查看下源代码,可以发现,其实是开发人员在为incr指令存储的数据分配内存时,没有注意边界,一下子分配了INCR_MAX_STORAGE_LEN,即24字节的内存,却没有正常地设置'\r\n'到真实数据的最后。所以当Get请求拿到数据是,会把22字节 + "\r\n"的数据返回给用户,造成了内存数据泄露。

修复前的代码:

            it = item_alloc(key, nkey, 0, realtime(req->message.body.expiration),
                            INCR_MAX_STORAGE_LEN);

            if (it != NULL) {
                snprintf(ITEM_data(it), INCR_MAX_STORAGE_LEN, "%llu",
                         (unsigned long long)req->message.body.initial);

修复后的代码:

            snprintf(tmpbuf, INCR_MAX_STORAGE_LEN, "%llu",
                (unsigned long long)req->message.body.initial);
            int res = strlen(tmpbuf);
            it = item_alloc(key, nkey, 0, realtime(req->message.body.expiration),
                            res + 2);

            if (it != NULL) {
                memcpy(ITEM_data(it), tmpbuf, res);
                memcpy(ITEM_data(it) + res, "\r\n", 2);

为什么这个bug隐藏了这么久

从测试的版本可以看到,至少从12年这个bug就存在了,从github上的代码来看,09年之前就存在了。直到13年12月才被修复。

为什么这个bug隐藏了这个久?可能是因为返回的22字节数据里中间有正确的加了\r\n,后面的才是多余的泄露数据,可能大部分解析库都以"\r\n"为分隔,从而跳过了解析到的多余的数据。比如XMemcached就能解析到。

另外,只有混用二进制协议和文本协议才可能会发现。

估计有不少服务器上运行的memcached版本都是比1.4.17要老的,比如ubuntu14默认的就是1.4.14。

这个bug的危害

不过这个bug的危害比较小,因为泄露的只有20个字节的数据。对于一些云服务指供的cache服务,即使后面是memcached做支持,也会有一个中转的路由。

窃取到有效信息的可能性很小。

其它的一些东东

wireshark设置解析memcached协议:

wireshark默认是支持解析memcached文本和二进制协议的,不过默认解析端口是11211,所以如果想要解析其它端口的包,要设置下。

在"Edit", ”Preferences“ 里,找到Memcached协议,就可以看到端口的配置了。

参考:https://ask.wireshark.org/questions/24495/memcache-and-tcp 

XMemcached的文本协议incr指令的实现:

上面说到Memcached的文本协议是不支持incr设置不存在的key的,但是XMemcached却提供了相关的函数,而且能正常运行,是为什么呢?

	/**
	 * "incr" are used to change data for some item in-place, incrementing it.
	 * The data for the item is treated as decimal representation of a 64-bit
	 * unsigned integer. If the current data value does not conform to such a
	 * representation, the commands behave as if the value were 0. Also, the
	 * item must already exist for incr to work; these commands won't pretend
	 * that a non-existent key exists with value 0; instead, it will fail.This
	 * method doesn't wait for reply.
	 * 
	 * @param key
	 *            key
	 * @param delta
	 *            increment delta
	 * @param initValue
	 *            the initial value to be added when value is not found
	 * @param timeout
	 *            operation timeout
	 * @param exp
	 *            the initial vlaue expire time, in seconds. Can be up to 30
	 *            days. After 30 days, is treated as a unix timestamp of an
	 *            exact date.
	 * @return
	 * @throws TimeoutException
	 * @throws InterruptedException
	 * @throws MemcachedException
	 */
	long incr(String key, long delta, long initValue, long timeout, int exp)
			throws TimeoutException, InterruptedException, MemcachedException;

实际上,XMemcached内部包装了incr和add指令,表明上是调用了incr但实际上是两条指令:

incr alipay_ratelimiter 1
NOT_FOUND
add alipay_ratelimiter 0 0 1
1
STORED

另外,要注意XMemcached默认是文本协议的,只有手动配置,才会使用二进制协议。

Memcached二进制协议比文本协议要快多少?

据这个演示的结果,二进制协议比文本协议要略快,第14页:

http://www.slideshare.net/tmaesaka/memcached-binary-protocol-in-a-nutshell-presentation/

参考:

https://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol 

二进制协议介绍的ppt:

抱歉!评论已关闭.