From patchwork Fri Dec 2 03:00:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhangpeng (AS)" X-Patchwork-Id: 28672 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp618489wrr; Thu, 1 Dec 2022 19:14:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf44g1dmRB64EztsCUuveMqYwRo4O0zh20DM+SFZryG+MmjWUtz2/m2nl5A5MlxzQLYJPpxI X-Received: by 2002:a17:907:d412:b0:7bc:68cc:7913 with SMTP id vi18-20020a170907d41200b007bc68cc7913mr30262637ejc.589.1669950883184; Thu, 01 Dec 2022 19:14:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669950883; cv=none; d=google.com; s=arc-20160816; b=FaFnV4gVvoR2nrXYAaS7g8qIyKhKXWHr+amhWGJ5RExlWtB3zpqpZJvckL296fXRwh UDxLGx9VX+iTpGTLUg12yIjdnQ5uj+OlpOAoL8Ho+nmIW3TjrQDTb1r6nT3clpuJwOGB fdDkY0isdvHH0hCwAcTrhgjFV1FLz+2s7g9hsHCshND0NxHbYLKfzbFUJX0UxRoB5pv3 iERkWP02MyvmilUxglJA3646qWRGYJxeAta7H1UCa0khkNizjwcbbRbcxzR75ddqfTdq u9jQ7nL/UqqizOIUgrRkSzKicfflR8jDrONQOO+2Rb0DsKyTtBpFE2JvdErcVJTDEMld byGg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho8-20020a1709070e8800b0079ddb52369bsi5684368ejc.61.2022.12.01.19.14.12; Thu, 01 Dec 2022 19:14:43 -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 + 99 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751070416910035489?= X-GMAIL-MSGID: =?utf-8?q?1751070416910035489?= 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 = '?';