Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3004918ybi; Mon, 17 Jun 2019 14:30:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqyoAl0+Vqq/WdOVCh+2nlQIHq17Kd8chxRD1V6numnFNWMNU/bj0EHnp62PzUYflrZxg9o8 X-Received: by 2002:a17:902:6a4:: with SMTP id 33mr4665078plh.338.1560807012169; Mon, 17 Jun 2019 14:30:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560807012; cv=none; d=google.com; s=arc-20160816; b=QumlynagerBhZhkYCxkxNZdE9zxYk1p1lG6/ffFJlZGIrWgzx1f+hdBZCH2kVL1g/1 9RkzOLpAeEWVjCWpzSh+XhkwQqQbgJY9AGJqiHzLGEqKuB26QYhN4TBMcP8VdJqi1XNN /UMHIUXNitNq3J/Lxwx/D/uFmYXmC/JsBWv4iI4YLkNIcZFDfF82T/ZoMpgkR+JGwVnY /YePOk6JovsLnov1HHAOV701lVIXFcqnQ22YU1ETAJNc0Bs7iK3tJ8KZssjgyGVWKv2s y7/cJGNgcPOdwH1RmNw60MvD7pnbhcqll1rr4C6/HpvlpiakK/6zVjvqMk1Zr1Z1caLG ObtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=l+1m3ftO0I2BDlLV8L/zxwKLAOsCvCg2XrHpsIWOBjg=; b=u37vOFwV1f5KoSSRQFJMyGh+ogrCvlBFKDS2bYbJ61Ltj0tBDW194E1JXDh1CFSeJF h1GV4I7iOV3Q7p+xpVadFc1JiaZazCtPuLR/Vh8IuxLUIu/Elm1eOVLWYIbXjE3NbccS nrmp+e2atRFbAX8R9A3800RoD3dhoWKIBZPxfctdiq9SO6l0gSqy0Mf7+1VF8Saq9Sx1 6LaaSkX/WrCd3tcP1wD2I39U3aVhDd1gtxNDKBtCqV31fzCt/xlEe+3bZoXGYQcxUKr/ Hs3UTVA7fteWNOqVDudya+JCnhczeBDCXSrDnws89STP8cQddUBDlDOYVm/ZZuW+AbOK 0hEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ujsMTCuF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l1si11683455pgm.391.2019.06.17.14.29.57; Mon, 17 Jun 2019 14:30:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ujsMTCuF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730502AbfFQV2O (ORCPT + 99 others); Mon, 17 Jun 2019 17:28:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:55240 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730003AbfFQV2J (ORCPT ); Mon, 17 Jun 2019 17:28:09 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 39E5D2070B; Mon, 17 Jun 2019 21:28:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560806888; bh=8UODO9q99exuxiG8KbuXciBBLwNe1N522U8G0sX0cE8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ujsMTCuFGPyKkRBHSkWXCYtmn7yhnlKEAXaOx8GcKG9q1lNVqCEkgGhGbbWTDnJ6I YHIGe+i30p22amrXtmkv8EQBDEJcYNVGmUk3LP4JmB4Ud8IcqeP0c67t7AbRCWK4GQ eZDuuE38D0+zY6MsBbZZP834qvQ+eYn33EWtLmJU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Coly Li , Rolf Fokkens , Pierre JUHEN , Shenghui Wang , Kent Overstreet , Nix , Jens Axboe Subject: [PATCH 4.14 17/53] bcache: fix stack corruption by PRECEDING_KEY() Date: Mon, 17 Jun 2019 23:10:00 +0200 Message-Id: <20190617210748.615772029@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190617210745.104187490@linuxfoundation.org> References: <20190617210745.104187490@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Coly Li commit 31b90956b124240aa8c63250243ae1a53585c5e2 upstream. Recently people report bcache code compiled with gcc9 is broken, one of the buggy behavior I observe is that two adjacent 4KB I/Os should merge into one but they don't. Finally it turns out to be a stack corruption caused by macro PRECEDING_KEY(). See how PRECEDING_KEY() is defined in bset.h, 437 #define PRECEDING_KEY(_k) \ 438 ({ \ 439 struct bkey *_ret = NULL; \ 440 \ 441 if (KEY_INODE(_k) || KEY_OFFSET(_k)) { \ 442 _ret = &KEY(KEY_INODE(_k), KEY_OFFSET(_k), 0); \ 443 \ 444 if (!_ret->low) \ 445 _ret->high--; \ 446 _ret->low--; \ 447 } \ 448 \ 449 _ret; \ 450 }) At line 442, _ret points to address of a on-stack variable combined by KEY(), the life range of this on-stack variable is in line 442-446, once _ret is returned to bch_btree_insert_key(), the returned address points to an invalid stack address and this address is overwritten in the following called bch_btree_iter_init(). Then argument 'search' of bch_btree_iter_init() points to some address inside stackframe of bch_btree_iter_init(), exact address depends on how the compiler allocates stack space. Now the stack is corrupted. Fixes: 0eacac22034c ("bcache: PRECEDING_KEY()") Signed-off-by: Coly Li Reviewed-by: Rolf Fokkens Reviewed-by: Pierre JUHEN Tested-by: Shenghui Wang Tested-by: Pierre JUHEN Cc: Kent Overstreet Cc: Nix Cc: stable@vger.kernel.org Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/md/bcache/bset.c | 16 +++++++++++++--- drivers/md/bcache/bset.h | 34 ++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 17 deletions(-) --- a/drivers/md/bcache/bset.c +++ b/drivers/md/bcache/bset.c @@ -825,12 +825,22 @@ unsigned bch_btree_insert_key(struct btr struct bset *i = bset_tree_last(b)->data; struct bkey *m, *prev = NULL; struct btree_iter iter; + struct bkey preceding_key_on_stack = ZERO_KEY; + struct bkey *preceding_key_p = &preceding_key_on_stack; BUG_ON(b->ops->is_extents && !KEY_SIZE(k)); - m = bch_btree_iter_init(b, &iter, b->ops->is_extents - ? PRECEDING_KEY(&START_KEY(k)) - : PRECEDING_KEY(k)); + /* + * If k has preceding key, preceding_key_p will be set to address + * of k's preceding key; otherwise preceding_key_p will be set + * to NULL inside preceding_key(). + */ + if (b->ops->is_extents) + preceding_key(&START_KEY(k), &preceding_key_p); + else + preceding_key(k, &preceding_key_p); + + m = bch_btree_iter_init(b, &iter, preceding_key_p); if (b->ops->insert_fixup(b, k, &iter, replace_key)) return status; --- a/drivers/md/bcache/bset.h +++ b/drivers/md/bcache/bset.h @@ -418,20 +418,26 @@ static inline bool bch_cut_back(const st return __bch_cut_back(where, k); } -#define PRECEDING_KEY(_k) \ -({ \ - struct bkey *_ret = NULL; \ - \ - if (KEY_INODE(_k) || KEY_OFFSET(_k)) { \ - _ret = &KEY(KEY_INODE(_k), KEY_OFFSET(_k), 0); \ - \ - if (!_ret->low) \ - _ret->high--; \ - _ret->low--; \ - } \ - \ - _ret; \ -}) +/* + * Pointer '*preceding_key_p' points to a memory object to store preceding + * key of k. If the preceding key does not exist, set '*preceding_key_p' to + * NULL. So the caller of preceding_key() needs to take care of memory + * which '*preceding_key_p' pointed to before calling preceding_key(). + * Currently the only caller of preceding_key() is bch_btree_insert_key(), + * and it points to an on-stack variable, so the memory release is handled + * by stackframe itself. + */ +static inline void preceding_key(struct bkey *k, struct bkey **preceding_key_p) +{ + if (KEY_INODE(k) || KEY_OFFSET(k)) { + (**preceding_key_p) = KEY(KEY_INODE(k), KEY_OFFSET(k), 0); + if (!(*preceding_key_p)->low) + (*preceding_key_p)->high--; + (*preceding_key_p)->low--; + } else { + (*preceding_key_p) = NULL; + } +} static inline bool bch_ptr_invalid(struct btree_keys *b, const struct bkey *k) {