From patchwork Thu May 25 02:09:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 9747489 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E089E601E9 for ; Thu, 25 May 2017 02:07:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CEF0A271BC for ; Thu, 25 May 2017 02:07:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C362A27FB7; Thu, 25 May 2017 02:07:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5299A271BC for ; Thu, 25 May 2017 02:07:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755089AbdEYCHY (ORCPT ); Wed, 24 May 2017 22:07:24 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:65110 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1755084AbdEYCHX (ORCPT ); Wed, 24 May 2017 22:07:23 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="19277947" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 25 May 2017 10:07:16 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 4347F47C6D2C for ; Thu, 25 May 2017 10:07:13 +0800 (CST) Received: from localhost.localdomain (10.167.226.129) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 25 May 2017 10:07:12 +0800 From: Su Yue To: Subject: [PATCH 1/3] btrfs: Introduce btrfs_check_namelen to avoid reading beyond boundary Date: Thu, 25 May 2017 10:09:06 +0800 Message-ID: <20170525020908.25830-1-suy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.13.0 MIME-Version: 1.0 X-Originating-IP: [10.167.226.129] X-yoursite-MailScanner-ID: 4347F47C6D2C.ABB65 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: suy.fnst@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When reading out name from inode_ref, dir_item, it's possible that corrupted name_len lead to read beyond boundary. Since there are already patches for btrfs-progs, this is for btrfs. Introduce function btrfs_check_namelen, it should be called before reading name from extent_buffer. The function compares arg @namelen with boundary then returns 'proper' namelen. Signed-off-by: Su Yue --- fs/btrfs/ctree.h | 2 ++ fs/btrfs/dir-item.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 643c70d2b2e6..f49e04e7612b 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -3074,6 +3074,8 @@ int btrfs_find_name_in_ext_backref(struct btrfs_path *path, int name_len, struct btrfs_inode_extref **extref_ret); +u16 btrfs_check_namelen(struct extent_buffer *leaf, int slot, + unsigned long start, u32 namelen); /* file-item.c */ struct btrfs_dio_private; int btrfs_del_csums(struct btrfs_trans_handle *trans, diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c index c24d615e3d7f..7af5ad8e9a3c 100644 --- a/fs/btrfs/dir-item.c +++ b/fs/btrfs/dir-item.c @@ -484,3 +484,25 @@ int verify_dir_item(struct btrfs_fs_info *fs_info, return 0; } + +/* + * Returns >0: the value @namelen after cut according item boundary + * Returns 0: on error + */ +u16 btrfs_check_namelen(struct extent_buffer *leaf, int slot, + unsigned long start, u32 namelen) +{ + u32 end; + u64 ret = namelen; + + end = btrfs_leaf_data(leaf) + btrfs_item_end_nr(leaf, slot); + + if (start > end) + return 0; + if (start + namelen > end) { + ret = end - start; + if (ret > U16_MAX) + ret = 0; + } + return ret; +}