Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp3014149pxx; Sun, 1 Nov 2020 19:21:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzup2ZDc8oZrxCzuyOmo0r104tsUYaFlIa+0ijJ4qgnaOpxBwlr7IvPvpsgiddI0SWH+tOP X-Received: by 2002:aa7:d890:: with SMTP id u16mr14699431edq.159.1604287270530; Sun, 01 Nov 2020 19:21:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604287270; cv=none; d=google.com; s=arc-20160816; b=Rv6qtE04fSKXQEaNR9MtwFE/KpGGZa3IXxOmkuP/d0R+Jt1p+oFmu/86XCwN6A3Z8Y /zmO2f47E7lLBmHMXe/aN0/xdwhsC+ishkuXzA/0mLRbnJExgJ7HNA7eAyzX/+6LMY9t DRSCKE6ikqryIYSi2ftaKIkbyzurxR89dEq6/Syrb7PSBAnyZj0U5dXwC1w5EsgoewUA ujf4IqavAv29fMZ4rQEUjbptV0CwvkZ9C5xIncFBPbM3K4U8AQCjptjCQhtiie44668p qWzBSsh9pyy4cvEaOe4X7TS0RBINwjCYApLnia8nDZPBvTed652Z8hSAIKJwMcKh78Sk gqWQ== 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:dkim-signature; bh=fwNRxDnv+UtvNXDA29/weyHf8Kk1GunaCJV8fEWqD00=; b=xieKKICSDOsdZduG6M9DbO5/d09KKZOo9Y7g5DuS8I/u9D6Mi9lbCUsWExEqTtI+Wu KRBDfxlz7CS0pngLooMpXy19bUvR4SrPIFJrtvEICyNhtpwY2Vc36+mL8P+ICTsYEwrl PXDo+xeTjoVI49tNTbwZc+2A55tnQP04usuzCgqhgcXmeaukIMnw2iJlX2NNbrFh7bdF 0kdR5DA4bqpYVjPvA7se+KTdmRkxNGa/iLprwC7g9lEinX3/3u4CjPXw0FiRCyOAWrug qH5XGVoru9XJa6L3eN3qspEv8blUOg33qt0SaTZ2b9z4O9ThyEKTVDuSjpbLFkUFD5tm FFBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=moDwNx4L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h2si9215993ejd.547.2020.11.01.19.20.47; Sun, 01 Nov 2020 19:21:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=moDwNx4L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727770AbgKBDRX (ORCPT + 99 others); Sun, 1 Nov 2020 22:17:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727450AbgKBDRW (ORCPT ); Sun, 1 Nov 2020 22:17:22 -0500 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 683D2C0617A6; Sun, 1 Nov 2020 19:17:22 -0800 (PST) Received: by mail-pg1-x542.google.com with SMTP id k9so7882046pgt.9; Sun, 01 Nov 2020 19:17:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fwNRxDnv+UtvNXDA29/weyHf8Kk1GunaCJV8fEWqD00=; b=moDwNx4LCqS6nBHsZeSVmXTK7hi5PDEiX/2Ee5/gjcBkkr8ffzRYKmAzS/r94rU8jv wqlHqNRKB8TfixhOD9Y2hAFZUorR0jTtVSYmB7j9nwlr4fHz5skVdWqamOY6fImILhIi 1i2g9iAvjHe261r6LvUli9CB6uxQsQMWTvzsikIiF3CTG42fZFIMXfcOjRdSwKaR2sIH aV0BfClvm24o8PIQsGPk4qhvOVsRByL8MWKTceTkyp2wTJ52YhNcZtXLXGaeESsyJJgI NlN2BKveQDh9wpwFtgbRgtrNF8HCh9LT4ok2un/jDeR3Znj3dvCnwHWyuUR8d/fqy7lu Dr/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fwNRxDnv+UtvNXDA29/weyHf8Kk1GunaCJV8fEWqD00=; b=eZz9iPkR74DtUNB7PhjGZoHZi1Ta24Yn6aAKp/xS9OdSmH//pbbtfKqAKdZTqxxFw6 eQEofCAj7CQwr4x3CAOaGSbMnSwFZ9q5emvycph7ObhgOAOrIcl2Xu4gq168yzC74gxd myUuxs4eaUyR2VKjbHjrteM3gjgOg1/0btR0OFlR4HMKe929YOVy+BR6FDN+tK/6umFz l/O7ev5levOwnRJWm2OWvVCaOZIOesCNZsD3KxQFtTn4HK4ohaT25CiR7vKN51IQVysn /+b/UBCemdvDkRMwA0GW34Yjer2MR7nob9ytUeL/fEMh4+KXE6L7HRz7JdaZJ5aP1L1F 8Fdw== X-Gm-Message-State: AOAM533uZBEWTeTNJONI61eCLvqE/fyhUYFYN9OpYo76caAKQLLctl6R bNxJsEaOR1yuAznFd8IMLeWwrruIFRESxKB3RP8= X-Received: by 2002:a62:dd56:0:b029:155:8165:c6c2 with SMTP id w83-20020a62dd560000b02901558165c6c2mr19488885pff.3.1604287041702; Sun, 01 Nov 2020 19:17:21 -0800 (PST) Received: from localhost.localdomain ([49.207.221.93]) by smtp.gmail.com with ESMTPSA id o22sm10997076pgb.83.2020.11.01.19.17.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Nov 2020 19:17:20 -0800 (PST) From: Anant Thazhemadam To: Oliver Hartkopp , Marc Kleine-Budde , "David S . Miller" , Jakub Kicinski Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, Anant Thazhemadam , syzbot+9bcb0c9409066696d3aa@syzkaller.appspotmail.com Subject: [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv() Date: Mon, 2 Nov 2020 08:43:26 +0530 Message-Id: <20201102031326.430048-1-anant.thazhemadam@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In canfd_rcv(), cfd->len is uninitialized when skb->len = 0, and this uninitialized cfd->len is accessed nonetheless by pr_warn_once(). Fix this uninitialized variable access by checking cfd->len's validity condition (cfd->len > CANFD_MAX_DLEN) separately after the skb->len's condition is checked, and appropriately modify the log messages that are generated as well. In case either of the required conditions fail, the skb is freed and NET_RX_DROP is returned, same as before. Reported-by: syzbot+9bcb0c9409066696d3aa@syzkaller.appspotmail.com Tested-by: Anant Thazhemadam Signed-off-by: Anant Thazhemadam --- This patch was locally tested using the reproducer and .config file generated by syzbot. net/can/af_can.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/net/can/af_can.c b/net/can/af_can.c index ea29a6d97ef5..1b9f2e50f065 100644 --- a/net/can/af_can.c +++ b/net/can/af_can.c @@ -694,16 +694,25 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev, { struct canfd_frame *cfd = (struct canfd_frame *)skb->data; - if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU || - cfd->len > CANFD_MAX_DLEN)) { - pr_warn_once("PF_CAN: dropped non conform CAN FD skbuf: dev type %d, len %d, datalen %d\n", + if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) { + pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n", + dev->type, skb->len); + goto free_skb; + } + + // This check is made separately since cfd->len would be uninitialized if skb->len = 0. + else if (unlikely(cfd->len > CANFD_MAX_DLEN)) { + pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d, datalen %d\n", dev->type, skb->len, cfd->len); - kfree_skb(skb); - return NET_RX_DROP; + goto free_skb; } can_receive(skb, dev); return NET_RX_SUCCESS; + +free_skb: + kfree_skb(skb); + return NET_RX_DROP; } /* af_can protocol functions */ -- 2.25.1