From: Darrick J. Wong Date: Tue, 4 Nov 2014 16:43:08 +0000 (-0500) Subject: libext2fs: don't memcpy identical pointers when writing a cache block X-Git-Tag: v1.42.13~24 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=8d5324c43f51ac7dc797501cf94270a1c339cb5a;p=tools%2Fe2fsprogs.git libext2fs: don't memcpy identical pointers when writing a cache block Sami Liedes found a scenario where we could memcpy incorrectly: If a block read fails during an e2fsck run, the UNIX IO manager will call the io->read_error routine with a pointer to the internal block cache. The e2fsck read error handler immediately tries to write the buffer back out to disk(!), at which point the block write code will try to copy the buffer contents back into the block cache. Normally this is fine, but not when the write buffer is the cache itself! So, plumb in a trivial check for this condition. A more thorough solution would pass a duplicated buffer to the IO error handlers, but I don't know if that happens frequently enough to be worth the extra point of failure. Signed-off-by: Darrick J. Wong Reported-by: Sami Liedes Signed-off-by: Theodore Ts'o --- diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c index eb39b28..23f22e3 100644 --- a/lib/ext2fs/unix_io.c +++ b/lib/ext2fs/unix_io.c @@ -820,7 +820,8 @@ static errcode_t unix_write_blk64(io_channel channel, unsigned long long block, cache = reuse; reuse_cache(channel, data, cache, block); } - memcpy(cache->buf, cp, channel->block_size); + if (cache->buf != cp) + memcpy(cache->buf, cp, channel->block_size); cache->dirty = !writethrough; count--; block++;