Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1514249rwb; Thu, 1 Dec 2022 19:21:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf5OTLuAcMIy3jJY3Z9nN9hXb+8E4i2nx2nYZguIrggGNvCUvaSty8enzh1Dj94sFKoI8snR X-Received: by 2002:a17:90a:4a8f:b0:215:f80c:18e6 with SMTP id f15-20020a17090a4a8f00b00215f80c18e6mr80396329pjh.45.1669951319196; Thu, 01 Dec 2022 19:21:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669951319; cv=none; d=google.com; s=arc-20160816; b=Jb79spW2cLfeIiWytibMoJZmakTJ/fwjhFY3Exm9AAKc0uIPStFzZrQYRglhWbSQ4X z+yS57lGXUZjGEhxJ+2zC4Xtxn++zFEQSlsf47I0YpbIUJ3BVf604ASTIo0N5zeVsdmE cVAbZ8wmCVLVwYpnBLHVZbd1QK9AGF6QeavmxYRhlKb/k2GDdXycDyr/+OEAoQCh+6Mo ijNXnr7ADHa6dJpNhnRY3Xj6Idn69KmlIwaB0BDeFdzRcQHmvKKSMj1xIYFYf/7akxr0 LxuVoNXHJ1Z40f/2WYiPJ9xF3jOwKPpo/zl8KDbYGCQKQXGv+JtTs1a9dl2jeXhP7lKR K7JA== 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 :message-id:date:subject:cc:to:from; bh=Bl1dZRMZxSv2zktW0QtOIROWWTbjcXjH/1zNYTSDhvY=; b=IN9Fdv9MVWN54tj0R2LaF+0SIu/DEeEr0EQbJGcqPOn0wTtUFuOXqFYWvRLJOlPkS3 5BDr36JTB1aIKOUjWX3PdlGYTzzQLlZHXDkmfZDUxcB3hMbZo3iJ2gALp3/k5NDmFaka fwKSXHsBl7+K4huZEIahSVlri5tHRVUJHM555FTTT2hRNIOTxDVjxqpdl5/h06cWMmjT j04/Sm1hYq/bY+l95I7kQgaUq+Mxiq2H8b9feQ1+DnjOrLW5hwlwddYiacNSVIrmzoOg 9TUg1ClB8ntucicJIdDBd9KRE32HPWzloIMdflOerCfeeqo69SqUu9Js7acx8I+SLrIM Owow== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n2-20020a170902e54200b001893d5da3cdsi6885083plf.331.2022.12.01.19.21.48; Thu, 01 Dec 2022 19:21:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230196AbiLBDA4 (ORCPT + 82 others); Thu, 1 Dec 2022 22:00:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231367AbiLBDAz (ORCPT ); Thu, 1 Dec 2022 22:00:55 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A511D67BA; Thu, 1 Dec 2022 19:00:54 -0800 (PST) Received: from kwepemm600020.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NNd3F2wyzzRpkQ; Fri, 2 Dec 2022 11:00:09 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 2 Dec 2022 11:00:51 +0800 From: Peng Zhang To: , , , CC: , , , , , , , , ZhangPeng , Subject: [PATCH v2] hfs: Fix OOB Write in hfs_asc2mac Date: Fri, 2 Dec 2022 03:00:38 +0000 Message-ID: <20221202030038.1391945-1-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600020.china.huawei.com (7.193.23.147) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: ZhangPeng Syzbot reported a OOB Write bug: loop0: detected capacity change from 0 to 64 ================================================================== BUG: KASAN: slab-out-of-bounds in hfs_asc2mac+0x467/0x9a0 fs/hfs/trans.c:133 Write of size 1 at addr ffff88801848314e by task syz-executor391/3632 Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0x1b1/0x28e lib/dump_stack.c:106 print_address_description+0x74/0x340 mm/kasan/report.c:284 print_report+0x107/0x1f0 mm/kasan/report.c:395 kasan_report+0xcd/0x100 mm/kasan/report.c:495 hfs_asc2mac+0x467/0x9a0 fs/hfs/trans.c:133 hfs_cat_build_key+0x92/0x170 fs/hfs/catalog.c:28 hfs_lookup+0x1ab/0x2c0 fs/hfs/dir.c:31 lookup_open fs/namei.c:3391 [inline] open_last_lookups fs/namei.c:3481 [inline] path_openat+0x10e6/0x2df0 fs/namei.c:3710 do_filp_open+0x264/0x4f0 fs/namei.c:3740 If in->len is much larger than HFS_NAMELEN(31) which is the maximum length of an HFS filename, a OOB write could occur in hfs_asc2mac(). In that case, when the dst reaches the boundary, the srclen is still greater than 0, which causes a OOB write. Fix this by adding a check on dstlen in while() before writing to dst address. Fixes: 328b92278650 ("[PATCH] hfs: NLS support") Reported-by: syzbot+dc3b1cf9111ab5fe98e7@syzkaller.appspotmail.com Signed-off-by: ZhangPeng Reviewed-by: Viacheslav Dubeyko --- v2: - Add RB from Slava - Change the location of the check for dstlen --- fs/hfs/trans.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/hfs/trans.c b/fs/hfs/trans.c index 39f5e343bf4d..fdb0edb8a607 100644 --- a/fs/hfs/trans.c +++ b/fs/hfs/trans.c @@ -109,7 +109,7 @@ void hfs_asc2mac(struct super_block *sb, struct hfs_name *out, const struct qstr if (nls_io) { wchar_t ch; - while (srclen > 0) { + while (srclen > 0 && dstlen > 0) { size = nls_io->char2uni(src, srclen, &ch); if (size < 0) { ch = '?'; -- 2.25.1