From patchwork Sun Jul 23 07:54:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Ma X-Patchwork-Id: 13323095 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71D7C1103 for ; Sun, 23 Jul 2023 07:55:21 +0000 (UTC) Received: from zg8tmtyylji0my4xnjqumte4.icoremail.net (zg8tmtyylji0my4xnjqumte4.icoremail.net [162.243.164.118]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EE51DB1 for ; Sun, 23 Jul 2023 00:55:19 -0700 (PDT) Received: from localhost.localdomain (unknown [39.174.92.167]) by mail-app3 (Coremail) with SMTP id cC_KCgDX3w_O3LxkGlx_Cw--.18759S4; Sun, 23 Jul 2023 15:54:54 +0800 (CST) From: Lin Ma To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, ast@kernel.org, martin.lau@kernel.org, yhs@fb.com, andrii@kernel.org, void@manifault.com, houtao1@huawei.com, laoar.shao@gmail.com, inwardvessel@gmail.com, kuniyu@amazon.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Lin Ma Subject: [PATCH v1] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing Date: Sun, 23 Jul 2023 15:54:52 +0800 Message-Id: <20230723075452.3711158-1-linma@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: cC_KCgDX3w_O3LxkGlx_Cw--.18759S4 X-Coremail-Antispam: 1UD129KBjvdXoWrZw43WF17XFyDXr4UGFyrXrb_yoWDtrg_ua 1UXa48Z3WjgFWUX3W5Gay3Xr1xKr15ZFn5C3s8tFW7Kws0vay8XF48ArZIvFy7Gr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PF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: X-Patchwork-Delegate: kuba@kernel.org The nla_for_each_nested parsing in function bpf_sk_storage_diag_alloc does not check the length of the nested attribute. This can lead to an out-of-attribute read and allow a malformed nlattr (e.g., length 0) to be viewed as a 4 byte integer. This patch adds additional check before the execution of nla_get_u32 to make sure the attribute has enough length. Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage") Signed-off-by: Lin Ma --- net/core/bpf_sk_storage.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index d4172534dfa8..6f1afbb394a6 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -511,6 +511,11 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) if (nla_type(nla) != SK_DIAG_BPF_STORAGE_REQ_MAP_FD) continue; + if (nla_len(nla) < sizeof(map_fd)) { + err = -EINVAL; + goto err_free; + } + map_fd = nla_get_u32(nla); map = bpf_map_get(map_fd); if (IS_ERR(map)) {