Received: by 10.192.165.148 with SMTP id m20csp2533482imm; Sun, 22 Apr 2018 09:01:53 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+yakYe+UX0DnKm8cvuLSUk3QlTncpXuJWB6BWfbDsTuYuxuNSfHxSW9xWa93kRdX5ursRA X-Received: by 2002:a17:902:6986:: with SMTP id l6-v6mr17735895plk.209.1524412913010; Sun, 22 Apr 2018 09:01:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524412912; cv=none; d=google.com; s=arc-20160816; b=exKS0wsKBb82GZbCwC8zFv7evmwyU0y4Xpl2P3sYpxhu9nENMFG7kIqPUxAOSKLZZc yIZ5MKi1ajqVG/9oHJ7rU0nPL3Cvo0znYD4DYtfxyxeTZFsKuq1K9QaECuJpupIAxzHK 86pzpNU61IRG3/kNTL4FDc/H8OFbbG+t/8GBatyZVWSeJThpB+5TCMBjoDl8EAP31clU wI9NtqYZ+fa+f73jNcgyrAU8U/Rd7vSJU/5IolprC3Nzdb75wP7xPZFqJFmNeyjnui2B tKq05nbAj3JGE8YxE8C/3vfxf6JHwusnQfqPZLz3usnhYh98C1d7Om9bm+VHsHECRlRc maBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=mSkKBCwbQsR4cxJ3wE5vv4Fn1C6LQunQQ2579c4Kirk=; b=UloVhWXYlLwUZls/9DPqwKAvnrjL0L4d6YZftvVUki7uuzseqwcjkWMVYOJzB+kEkn k4cQBiEKYsu4SGo1Lfzhyz07xVhAuINk2ocFxhQPkyeR0bg/VbZ8aPrI23/k0+o1V37o a0PAN1Mesr/GQWLGjuz6F+3TfLVMbDsL9zaVR89MoBTMmQZ7Ox0krP90JA1uMj6G4XQf 3W9fdmvYbsL83uUHanP6DqEgauMnv8NqHyep0Qoc06Uk3GvN1H+Qs+uJAZJiUDicklWV a7iKfk6jjz4I8nY6JGUC3sMahZTyVko74aV65Z8KgNBXLOhlvmt2IjUq86VRLjByGpBH M6RQ== ARC-Authentication-Results: i=1; mx.google.com; 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 d9si7419423pfm.226.2018.04.22.09.01.38; Sun, 22 Apr 2018 09:01:52 -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; 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 S1754468AbeDVQAQ (ORCPT + 99 others); Sun, 22 Apr 2018 12:00:16 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:46326 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753877AbeDVN6l (ORCPT ); Sun, 22 Apr 2018 09:58:41 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id F27F3CD5; Sun, 22 Apr 2018 13:58:40 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , stable@kernel.org Subject: [PATCH 4.16 098/196] ext4: add extra checks to ext4_xattr_block_get() Date: Sun, 22 Apr 2018 15:51:58 +0200 Message-Id: <20180422135109.331429279@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Theodore Ts'o commit 54dd0e0a1b255f115f8647fc6fb93273251b01b9 upstream. Add explicit checks in ext4_xattr_block_get() just in case the e_value_offs and e_value_size fields in the the xattr block are corrupted in memory after the buffer_verified bit is set on the xattr block. Signed-off-by: Theodore Ts'o Cc: stable@kernel.org Signed-off-by: Greg Kroah-Hartman --- fs/ext4/xattr.c | 26 +++++++++++++++++++------- fs/ext4/xattr.h | 11 +++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) --- a/fs/ext4/xattr.c +++ b/fs/ext4/xattr.c @@ -197,7 +197,7 @@ ext4_xattr_check_entries(struct ext4_xat while (!IS_LAST_ENTRY(entry)) { u32 size = le32_to_cpu(entry->e_value_size); - if (size > INT_MAX) + if (size > EXT4_XATTR_SIZE_MAX) return -EFSCORRUPTED; if (size != 0 && entry->e_value_inum == 0) { @@ -540,8 +540,10 @@ ext4_xattr_block_get(struct inode *inode if (error) goto cleanup; size = le32_to_cpu(entry->e_value_size); + error = -ERANGE; + if (unlikely(size > EXT4_XATTR_SIZE_MAX)) + goto cleanup; if (buffer) { - error = -ERANGE; if (size > buffer_size) goto cleanup; if (entry->e_value_inum) { @@ -550,8 +552,12 @@ ext4_xattr_block_get(struct inode *inode if (error) goto cleanup; } else { - memcpy(buffer, bh->b_data + - le16_to_cpu(entry->e_value_offs), size); + u16 offset = le16_to_cpu(entry->e_value_offs); + void *p = bh->b_data + offset; + + if (unlikely(p + size > end)) + goto cleanup; + memcpy(buffer, p, size); } } error = size; @@ -589,8 +595,10 @@ ext4_xattr_ibody_get(struct inode *inode if (error) goto cleanup; size = le32_to_cpu(entry->e_value_size); + error = -ERANGE; + if (unlikely(size > EXT4_XATTR_SIZE_MAX)) + goto cleanup; if (buffer) { - error = -ERANGE; if (size > buffer_size) goto cleanup; if (entry->e_value_inum) { @@ -599,8 +607,12 @@ ext4_xattr_ibody_get(struct inode *inode if (error) goto cleanup; } else { - memcpy(buffer, (void *)IFIRST(header) + - le16_to_cpu(entry->e_value_offs), size); + u16 offset = le16_to_cpu(entry->e_value_offs); + void *p = (void *)IFIRST(header) + offset; + + if (unlikely(p + size > end)) + goto cleanup; + memcpy(buffer, p, size); } } error = size; --- a/fs/ext4/xattr.h +++ b/fs/ext4/xattr.h @@ -71,6 +71,17 @@ struct ext4_xattr_entry { #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1)) /* + * XATTR_SIZE_MAX is currently 64k, but for the purposes of checking + * for file system consistency errors, we use a somewhat bigger value. + * This allows XATTR_SIZE_MAX to grow in the future, but by using this + * instead of INT_MAX for certain consistency checks, we don't need to + * worry about arithmetic overflows. (Actually XATTR_SIZE_MAX is + * defined in include/uapi/linux/limits.h, so changing it is going + * not going to be trivial....) + */ +#define EXT4_XATTR_SIZE_MAX (1 << 24) + +/* * The minimum size of EA value when you start storing it in an external inode * size of block - size of header - size of 1 entry - 4 null bytes */