Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1085068lqo; Thu, 9 May 2024 04:31:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdzaze6iuqCxwyI6RBFzlePU6u4TYU49WZZmWdZI8gdK2D9qt8OTbqt0AGGq5uhdij1R789sOzUU4KOc6+3KcZYRSYp4NO8UlJHycIdg== X-Google-Smtp-Source: AGHT+IHh7x4ejnp4kB125vWo063FrE3lMJYQnUj7jSsOAT4fpa4nKJsOFJPv1BIRmxELLnzSHW83 X-Received: by 2002:a17:906:1284:b0:a59:c52b:9936 with SMTP id a640c23a62f3a-a59fb955a22mr330725966b.26.1715254287269; Thu, 09 May 2024 04:31:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715254287; cv=pass; d=google.com; s=arc-20160816; b=Ofgg8K0jrzHYK+Ph/eF9md9JMCCd8GmoMSRUn5jB6S0acAjzBDRvIuX1xh1Ww3M15d fImT1lHlZM/CyBAwxHFyzvPrO+hHEm5f1vmJhC3cRYN74ySom5QzxWBTEai9/r+30mPi 1YIY7PAm/BLlhJaa8YaSaQnxaWe7zKGy6V5CXBa2QB0BN2h4o80HqsUSLjk+Z971SiKT OI3JH0opH71jLzKgxe0Q6/pNTAhIg741SIdY79OHWZaG8P5wqjIAzNnzF50+Xsi66zZ6 oS//sg3Vp87Bm8XgSCEL7Ur+yUyiwVkuO/qqf5cuw8dm8FUVTXNG8uqGe8sd0Puk18py X2Lg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=kZ1oYNvf/jlHzYagQ7dAZFEVJALyTuqemntwu0w6jKw=; fh=CI1MislCB8I7r1QIEB2N7dtNtZYw4HaK2RnASIh2xfg=; b=YKAt4iMVudSTICFJ8+j73ZZcoRRD+cuYPxfleiHHPlVqw5A8rvt9DFmns5KlBsG5lm GgU/ZVnhZeduhjDlE3TajogCSQ/PjyMX/6XuQY/TS+GQhjt1wt+ztKZeL4PVfzpDkoUN FVwP8b0ebirpFU0typvvuIwp0o97umWFzIhDkn4XkND/o1e/CsommOflLx/wzZZ5P90q iLEA/dZ62bZKG09WgvFmVXgfBUXz6CC3WMfimrsgFtnhrJHOyuNM4J9IKEUpwBzuOrTM 53Qxs2YamOS+FDAK+JKc2ON8CUhuHHh76D2JdPYRrlVN+qzLUpM3VVCk4hhJYkzzAS5e ByOQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CgcvKIg8; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-174446-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174446-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17be6cf8si68243066b.814.2024.05.09.04.31.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 04:31:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174446-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CgcvKIg8; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-174446-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174446-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CC9811F22307 for ; Thu, 9 May 2024 11:31:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7625B1311A2; Thu, 9 May 2024 11:31:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CgcvKIg8" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 C0DDB12FF93 for ; Thu, 9 May 2024 11:31:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715254279; cv=none; b=aa6hELWuI9q3WumG8w5HcpOLwkXsaA0/G7guEih8ZX/DLTYC72s1HykxIYZUfCLWKzv3IkE+8yIvendxotk839mizXsZGsUFRQxmbdAbzfybTA7/3Ct3fFlapNEbif6tp9I3sWsc02QJcumEYogJx02jAiUTw5csbnNzTC91Q7k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715254279; c=relaxed/simple; bh=KbSJqTXIym8X1LZZSbDqHy2oc5PTnuYaFn9rlqkiybc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=XhE7u3TlM/Pf7L5S3XKeGUgwB9hS7IdQoLZ6aoQbLh1UkSHr5qMAKtKA+TNnSzzXnb3SyfEZTxHMtEJk0QG++AGv1mZ1EcYQkv+lEKrfmqDOvGGE4Ln6wwsJuReeNgCkmXK5r/5dVkcbwx/V51Vvc05uLmcLvrIk0cKxz6Usp3E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=CgcvKIg8; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715254276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=kZ1oYNvf/jlHzYagQ7dAZFEVJALyTuqemntwu0w6jKw=; b=CgcvKIg8TkwcLe8NWm7mS+8ORrGWGXJa1T+i7a2s9zpruDBKKynVz2w7RpOLXJdC64G1Jw pA5DKiIlYJgmvA9kbmfdUy4DtEYIO83Lh3tn8s8t6v4dI1hwW0yrzJ/qpWTm1yhED7f7zz b9VRAwaqrGBFcxLO9QokgEFr0z4bEzY= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-367-5WV20ux4MQmwWP1g_MQsmA-1; Thu, 09 May 2024 07:31:15 -0400 X-MC-Unique: 5WV20ux4MQmwWP1g_MQsmA-1 Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-5cf35636346so628930a12.3 for ; Thu, 09 May 2024 04:31:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715254274; x=1715859074; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kZ1oYNvf/jlHzYagQ7dAZFEVJALyTuqemntwu0w6jKw=; b=YUIvLmZXnZIIrg31mHuOF4+BNcH3pAKNdKiuRl5lptNOvU7GHy5LORPdM5p0ML/gma w9/Uh6Rf8go1nDm4nmasDZOLQuSfXsfnUZzODOvGtDveHEvHlBkq7IhgYQTVTp6C9s19 mAqFVRMkKG9Gilqy1oI+8V4aYms8tJR49aEMmG8Q6jnDLXCeYaNTNoGTg2opU8EFYUGJ 0E0hbm6OVNBMxq/Prg2wjj3vTQQS64rn+yUsFnZ9Q9ZAYWtzLAP1MdsfI9IbWq/SfW+J cMKYXAIX1/cJIc5F7IzSqWYDmxIFRmaamV9BzPY3yERlZFozVhTattYBz91/1HK/Mn1+ n0PA== X-Forwarded-Encrypted: i=1; AJvYcCUgqERFnSXMZaQbDpDq3n98ql3gvWlU+Tf9R/deKzsTMW5xGgzxvoZ+MWjQkzrX576dQ3UYOCCqxFWFzFfHbaOIMewN2f2rIUHtq4wh X-Gm-Message-State: AOJu0YxZSh0P+rVZQG32VIig0dPhRrNAayQzSeJ8o5xjA8kN6Ak/gQH7 nOyj4eTAPBRnvJzetxgdyWhp/F6KeSLuiJdqOjtlbxS90k5kzrGfvXwekKTB3qksECnBAZERYTB ZOtKjjeWg1FznT0UD6gdV2EVqStMEBugT4OZmin8VrmzfuI7XddAC153rhzieFg== X-Received: by 2002:a05:6a20:9494:b0:1af:c0f9:b155 with SMTP id adf61e73a8af0-1afc8d8eb58mr5402213637.38.1715254274439; Thu, 09 May 2024 04:31:14 -0700 (PDT) X-Received: by 2002:a05:6a20:9494:b0:1af:c0f9:b155 with SMTP id adf61e73a8af0-1afc8d8eb58mr5402187637.38.1715254274006; Thu, 09 May 2024 04:31:14 -0700 (PDT) Received: from zeus.elecom ([240b:10:83a2:bd00:6e35:f2f5:2e21:ae3a]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c2567ccsm12023495ad.301.2024.05.09.04.31.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 04:31:13 -0700 (PDT) From: Ryosuke Yasuoka To: krzk@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org Cc: Ryosuke Yasuoka , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, syoshida@redhat.com, syzbot+d7b4dc6cd50410152534@syzkaller.appspotmail.com Subject: [PATCH net v4] nfc: nci: Fix uninit-value in nci_rx_work Date: Thu, 9 May 2024 20:30:33 +0900 Message-ID: <20240509113036.362290-1-ryasuoka@redhat.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit syzbot reported the following uninit-value access issue [1] nci_rx_work() parses received packet from ndev->rx_q. It should be validated header size, payload size and total packet size before processing the packet. If an invalid packet is detected, it should be silently discarded. Fixes: d24b03535e5e ("nfc: nci: Fix uninit-value in nci_dev_up and nci_ntf_packet") Reported-and-tested-by: syzbot+d7b4dc6cd50410152534@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=d7b4dc6cd50410152534 [1] Signed-off-by: Ryosuke Yasuoka --- v4 - v3 patch uses goto statement and it makes codes complicated. So this patch simply calls kfree_skb inside loop and remove goto statement. - [2] inserted kcov_remote_stop() to fix kcov check. However, as we discuss about my v3 patch [3], it should not exit the for statement and should continue processing subsequent packets. This patch removes them and simply insert continue statement. [2] https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/commit/?id=19e35f24750d v3 https://lore.kernel.org/netdev/20240502082323.250739-1-ryasuoka@redhat.com/T/ - As Simon pointed out, the valid packets will reach invalid_pkt_free and kfree_skb(skb) after being handled correctly in switch statement. It can lead to double free issues, which is not intended. So this patch uses "continue" instead of "break" in switch statement. - In the current implementation, once zero payload size is detected, the for statement exits. It should continue processing subsequent packets. So this patch just frees skb in invalid_pkt_free when the invalid packets are detected. [3] v2 https://lore.kernel.org/lkml/20240428134525.GW516117@kernel.org/T/ - The v1 patch only checked whether skb->len is zero. This patch also checks header size, payload size and total packet size. v1 https://lore.kernel.org/linux-kernel/CANn89iJrQevxPFLCj2P=U+XSisYD0jqrUQpa=zWMXTjj5+RriA@mail.gmail.com/T/ net/nfc/nci/core.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c index b133dc55304c..0aaff30cb68f 100644 --- a/net/nfc/nci/core.c +++ b/net/nfc/nci/core.c @@ -1463,6 +1463,16 @@ int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode, ndev->ops->n_core_ops); } +static bool nci_valid_size(struct sk_buff *skb, unsigned int header_size) +{ + if (skb->len < header_size || + !nci_plen(skb->data) || + skb->len < header_size + nci_plen(skb->data)) { + return false; + } + return true; +} + /* ---- NCI TX Data worker thread ---- */ static void nci_tx_work(struct work_struct *work) @@ -1516,24 +1526,32 @@ static void nci_rx_work(struct work_struct *work) nfc_send_to_raw_sock(ndev->nfc_dev, skb, RAW_PAYLOAD_NCI, NFC_DIRECTION_RX); - if (!nci_plen(skb->data)) { + if (!skb->len) { kfree_skb(skb); - kcov_remote_stop(); - break; + continue; } /* Process frame */ switch (nci_mt(skb->data)) { case NCI_MT_RSP_PKT: - nci_rsp_packet(ndev, skb); + if (nci_valid_size(skb, NCI_CTRL_HDR_SIZE)) + nci_rsp_packet(ndev, skb); + else + kfree_skb(skb); break; case NCI_MT_NTF_PKT: - nci_ntf_packet(ndev, skb); + if (nci_valid_size(skb, NCI_CTRL_HDR_SIZE)) + nci_ntf_packet(ndev, skb); + else + kfree_skb(skb); break; case NCI_MT_DATA_PKT: - nci_rx_data_packet(ndev, skb); + if (nci_valid_size(skb, NCI_DATA_HDR_SIZE)) + nci_rx_data_packet(ndev, skb); + else + kfree_skb(skb); break; default: -- 2.44.0