Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp2422146rdh; Sun, 29 Oct 2023 15:55:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYNjZ+cVnZYlEU+42rV+srd/Sgz7BctmnjFo/s/80U/J/1C77s7pD5GnrkaE0Xl249KFgX X-Received: by 2002:a05:6a00:2d9d:b0:6b1:b5c4:a8b0 with SMTP id fb29-20020a056a002d9d00b006b1b5c4a8b0mr11070203pfb.23.1698620126702; Sun, 29 Oct 2023 15:55:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698620126; cv=none; d=google.com; s=arc-20160816; b=CIOXEBDzEr7fdwEsMhqPnGDmZMKlBZ4sU31wtiHictwsvgLE978FudualF10aX0NZX uYZERfe56dZsy9puNvg3PSbBcVvz6l0u8xHq8UeGW5kcPSbX0CCytp0+4bACOdK0i6kA 6lvtDeTj2gwafGzHNoZG4nA6pX62bknsLgDBi0uA7gLAmeHEp5ntjPQHTk3aw+7xhnhX eNVrQ/cZon6VdGhgUmgEvo+2RnmNFDCfj49Z8AwfTLIVm72LaK+HV6RVTowFpwTUtNUA LvV0QIEMLG7sYpNGPxfovHho/Yok1VDBOAxDLtL6QBPUxaNj0hxi/wYHm2pwelavL1/X dJ4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H5GpvciLTeIHq1nXo1nezZJ24S6FQCq6k+I15oHW1Zo=; fh=IVcZYgopwke+Bc9cmwdOT5yAIhnLPNAsBqE9TuY8J+c=; b=GhbkYVek6ySpaXNSGfqjycHHBoCsgXvhh2SI++JCE60r0nDRBhs2s2nLUCrtlXGa7P tT3HUI4OO268YR9sWc2KKpi90kA4xZZmsudRL74CNV56pK/SruV5Z/lUYYytigmfxzMm Q+4wQenI658Ads1JQZS0w7suFcyFEfgFGenE/1eCRjnbT7zy4KgyMXgoIiNiLMaXZcki RoKHsTbl9Dyme5q/WplJ8CVSMfwYOqYBHjByp6URIgluKHmDKZAJ2eQEsKsYAU0448uk rJitHT69cay4URyg0CSLYnlCU43LE8d8+Ex7AF4UvGyrIWhZeS2BKoG8p0h+RLsC8D7m gOsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jZR3W5GD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id v62-20020a638941000000b00578f23273a3si4172774pgd.738.2023.10.29.15.55.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 15:55:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jZR3W5GD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id BCE088087E08; Sun, 29 Oct 2023 15:55:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230453AbjJ2WzB (ORCPT + 99 others); Sun, 29 Oct 2023 18:55:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230509AbjJ2Wy4 (ORCPT ); Sun, 29 Oct 2023 18:54:56 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57F9DDF; Sun, 29 Oct 2023 15:54:53 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1AC6AC433C8; Sun, 29 Oct 2023 22:54:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698620092; bh=1W22RHevkgAQ2+OfQ0fUXQN8FZfqQv2sohxFqtpTjMo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jZR3W5GD9RERnRa5AkCePNrOxM4zEK9rgAIn57geen2dHmWLNykgACUa4ktF4Zzav skCpoB8JvrvtFRefFnP0Y0sMGILpJ5+ByUyC+o3RbPP5hzRWmymC/kEZoQg8BazG71 J8NJQJJmgamrP5qv6zuvxMQV5VVX8A9L/iHAh6f5SMsUjnIgARJ0XiiLvbUxwkuFiE nbeJgliivQTdnWrb9SQi5RUgOLK/+nYJco7fm/yPSYSKgPE+4AB21ya35upR61F69q LjlOPkrY7m/w8gUsAIt1CaY1jH3KwvNpcNgLWtruVg+xmqvdqgB6WcgwENQDL/CI3y qGNt1GCxcd87g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Konstantin Komarov , Sasha Levin , ntfs3@lists.linux.dev Subject: [PATCH AUTOSEL 6.5 05/52] fs/ntfs3: Add more attributes checks in mi_enum_attr() Date: Sun, 29 Oct 2023 18:52:52 -0400 Message-ID: <20231029225441.789781-5-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231029225441.789781-1-sashal@kernel.org> References: <20231029225441.789781-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.5.9 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 29 Oct 2023 15:55:20 -0700 (PDT) From: Konstantin Komarov [ Upstream commit 013ff63b649475f0ee134e2c8d0c8e65284ede50 ] Signed-off-by: Konstantin Komarov Signed-off-by: Sasha Levin --- fs/ntfs3/record.c | 68 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c index c12ebffc94da4..02cc91ed88357 100644 --- a/fs/ntfs3/record.c +++ b/fs/ntfs3/record.c @@ -193,8 +193,9 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr) { const struct MFT_REC *rec = mi->mrec; u32 used = le32_to_cpu(rec->used); - u32 t32, off, asize; + u32 t32, off, asize, prev_type; u16 t16; + u64 data_size, alloc_size, tot_size; if (!attr) { u32 total = le32_to_cpu(rec->total); @@ -213,6 +214,7 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr) if (!is_rec_inuse(rec)) return NULL; + prev_type = 0; attr = Add2Ptr(rec, off); } else { /* Check if input attr inside record. */ @@ -226,11 +228,11 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr) return NULL; } - if (off + asize < off) { - /* Overflow check. */ + /* Overflow check. */ + if (off + asize < off) return NULL; - } + prev_type = le32_to_cpu(attr->type); attr = Add2Ptr(attr, asize); off += asize; } @@ -250,7 +252,11 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr) /* 0x100 is last known attribute for now. */ t32 = le32_to_cpu(attr->type); - if ((t32 & 0xf) || (t32 > 0x100)) + if (!t32 || (t32 & 0xf) || (t32 > 0x100)) + return NULL; + + /* attributes in record must be ordered by type */ + if (t32 < prev_type) return NULL; /* Check overflow and boundary. */ @@ -259,16 +265,15 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr) /* Check size of attribute. */ if (!attr->non_res) { + /* Check resident fields. */ if (asize < SIZEOF_RESIDENT) return NULL; t16 = le16_to_cpu(attr->res.data_off); - if (t16 > asize) return NULL; - t32 = le32_to_cpu(attr->res.data_size); - if (t16 + t32 > asize) + if (t16 + le32_to_cpu(attr->res.data_size) > asize) return NULL; t32 = sizeof(short) * attr->name_len; @@ -278,21 +283,52 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr) return attr; } - /* Check some nonresident fields. */ - if (attr->name_len && - le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len > - le16_to_cpu(attr->nres.run_off)) { + /* Check nonresident fields. */ + if (attr->non_res != 1) + return NULL; + + t16 = le16_to_cpu(attr->nres.run_off); + if (t16 > asize) + return NULL; + + t32 = sizeof(short) * attr->name_len; + if (t32 && le16_to_cpu(attr->name_off) + t32 > t16) + return NULL; + + /* Check start/end vcn. */ + if (le64_to_cpu(attr->nres.svcn) > le64_to_cpu(attr->nres.evcn) + 1) + return NULL; + + data_size = le64_to_cpu(attr->nres.data_size); + if (le64_to_cpu(attr->nres.valid_size) > data_size) return NULL; - } - if (attr->nres.svcn || !is_attr_ext(attr)) { + alloc_size = le64_to_cpu(attr->nres.alloc_size); + if (data_size > alloc_size) + return NULL; + + t32 = mi->sbi->cluster_mask; + if (alloc_size & t32) + return NULL; + + if (!attr->nres.svcn && is_attr_ext(attr)) { + /* First segment of sparse/compressed attribute */ + if (asize + 8 < SIZEOF_NONRESIDENT_EX) + return NULL; + + tot_size = le64_to_cpu(attr->nres.total_size); + if (tot_size & t32) + return NULL; + + if (tot_size > alloc_size) + return NULL; + } else { if (asize + 8 < SIZEOF_NONRESIDENT) return NULL; if (attr->nres.c_unit) return NULL; - } else if (asize + 8 < SIZEOF_NONRESIDENT_EX) - return NULL; + } return attr; } -- 2.42.0