Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp11723257rwb; Fri, 25 Nov 2022 21:08:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf52hqDbcXfUXblv/t8H42lytz23MT1RKDe3huKrbvUveNi8P9eWo10IVDaxvuHLGCZxTqYs X-Received: by 2002:a17:906:e0d7:b0:7b9:a74b:f16a with SMTP id gl23-20020a170906e0d700b007b9a74bf16amr13980662ejb.192.1669439318237; Fri, 25 Nov 2022 21:08:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669439318; cv=none; d=google.com; s=arc-20160816; b=02ekPpl1E7xXIrqvoAt2HQVzTBCkwN2qxXkae0CCXRxgGHUkz0pnqZ0yE8uU7TZhNi qTDrB3s4muAa4b3g9bB/b5OjhgNXPTDn1CGlZGhSQv8AwAKvzOpd82Cf99ruH3Y+UfSB FkZi3s0G/3prCn9RLVPFq5QPvXAOwKV++mGWy4txiC5b6Q/dfYRE+gI3sOuZnaK1QHq+ eHRrm6zxAMZh/UzzHhlXUPZgAKLsHzsqjYTb5Cer24llP0Y7NBbPm6bWuMUSZ4+Xclx6 qTLF0Cx2iFXHFUPdnbrYcrX15NedXW0EZJBNeXSWHyjRbGSOhLOVu36VFMd2aO+dUoGm qVPQ== 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=LQtm48WndR5jjFqesrC4Ja3e/6jKf5Nd6amkJi45ST8=; b=d6QBLBNYilTMWJ6EdzCbMtdegfA5nwxTMywKoP4hlMihJu+XA1gerHDITfLXYkAyaF QdZo6X02M2j38yeFuBFPmWlBl1aE9eSAJjsoVSjs/21z0iEdhi2vu5LmFmYa8TR0VO6U gmghz9Jy0f+Bh6IyVPQCmu98+5nx/KrrNOLX9wpMtzLu+m3e9tkkj8OSYBCkhUZtf3h+ uQVPym3cpQIhNSCO36ql3PLph35FXzf5+Wo/LL/j8PF4kS+J9QGyM0K8O1GvqOnqxym2 q7Pfw+WNAlgEdECWQbEJeMg0jhX7+aNlnt30gHmlNEVOcMDo0i73MPR6Jnja52DdzPIW WOlg== 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 g20-20020a1709065d1400b007b5ce4a4360si5119804ejt.151.2022.11.25.21.08.17; Fri, 25 Nov 2022 21:08:38 -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 S230128AbiKZEg2 (ORCPT + 84 others); Fri, 25 Nov 2022 23:36:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230114AbiKZEgX (ORCPT ); Fri, 25 Nov 2022 23:36:23 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 165B440466; Fri, 25 Nov 2022 20:36:22 -0800 (PST) Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NJzSK2KMKzmW95; Sat, 26 Nov 2022 12:35:45 +0800 (CST) Received: from kwepemm600020.china.huawei.com (7.193.23.147) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 26 Nov 2022 12:36:20 +0800 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; Sat, 26 Nov 2022 12:36:19 +0800 From: Peng Zhang To: , CC: , , , , ZhangPeng , Subject: [PATCH] hfs: Fix OOB Write in hfs_asc2mac Date: Sat, 26 Nov 2022 04:36:12 +0000 Message-ID: <20221126043612.853428-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 before Writing to dst address. Fixes: 328b92278650 ("[PATCH] hfs: NLS support") Reported-by: syzbot+dc3b1cf9111ab5fe98e7@syzkaller.appspotmail.com Signed-off-by: ZhangPeng --- fs/hfs/trans.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/hfs/trans.c b/fs/hfs/trans.c index 39f5e343bf4d..886158db07b3 100644 --- a/fs/hfs/trans.c +++ b/fs/hfs/trans.c @@ -130,6 +130,8 @@ void hfs_asc2mac(struct super_block *sb, struct hfs_name *out, const struct qstr dst += size; dstlen -= size; } else { + if (dstlen == 0) + goto out; *dst++ = ch > 0xff ? '?' : ch; dstlen--; } -- 2.25.1