Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10649275ybi; Thu, 25 Jul 2019 03:03:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqx3FuFUi70TEAY6N90epuCOA84MkOcPHvFthbFZ6iiHw13q7O0f5oqdrZhI/VFDwwFau+Fi X-Received: by 2002:a17:902:bf09:: with SMTP id bi9mr84611940plb.143.1564048994632; Thu, 25 Jul 2019 03:03:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564048994; cv=none; d=google.com; s=arc-20160816; b=X+WFWx4lwX2AKcLsi6GmWCs5zH63NIxwmgZPZsGSp6tvITLQhbPhQv1nTPUJbsRgz6 HZzu6tGdZp0p99WyV1DQDIup39yFTSlROerF2HbrOaUuQfKf9QC7+IdUyKf9ZvntvJI1 XFFqQeoY7dai7BDECGGWxJ0asexGk2UDCeKDbD+UW5fSq9kjAeK9KFIsaMy/3T9BmNlo FYn7z606MpOAHazQ/lgFfYh7a/rqPbK3nmHaE7etA2A/RJUDM1gJZfMv1FwqP3AJwnmL TwV/Z+isFduEZTF5RNC+Y06g/IVwL6C7Inaxx1QFh+SVpQNa9rgobH9U8GwJ+qXT4Hnp DdWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=2OOipR6ExT2jNJi4jbCkya5oL8OY5qKBVN4HVC3rk40=; b=wG/gOzULsuD5+WvDumWICHVsIi7Gj4yR8CqE9ZfoeU230JoKn2TCq4JPnV37MrJp9b 340E7PieygtEsUBOcCw+xspxCIimYmzSvi8z7AU7XPzIOwgroN3p/bxSiInQ3B8pRjaz ABTgeJyKEilOQikg5Vck/UmP1vciF4i7UcM7cltQoIQV8IjKooDTv9tbNDabSE4NWPzF hXRRdtmYCDCuJk61jt7SV/51yRkrtU4Us25FF66tlT+vfM0n/2DFdAXSdanxa7142aHy jZ2AJOSwdmA6hqIyQ8IdrXObdPrb1IZdZJWT65Az3pgd1nKKTOibFzWHF3/WQevPIphR mJDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b="FzHCkuX/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p1si16000391plq.286.2019.07.25.03.02.54; Thu, 25 Jul 2019 03:03:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b="FzHCkuX/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388244AbfGYEal (ORCPT + 99 others); Thu, 25 Jul 2019 00:30:41 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:38707 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726102AbfGYEal (ORCPT ); Thu, 25 Jul 2019 00:30:41 -0400 Received: by mail-pg1-f193.google.com with SMTP id f5so13499631pgu.5; Wed, 24 Jul 2019 21:30:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=2OOipR6ExT2jNJi4jbCkya5oL8OY5qKBVN4HVC3rk40=; b=FzHCkuX/yPdx2rB65HPx3xPsnFpinwzY8xUPsFsNkqk20Pd9KYCc5MaZH8/4FR6wFc kY09+WnE+PA3hkaSW+6kt+6bFZje6pzjOciyxjiU+lglVJQStTJBJ3sV61VRB12o/p3C vogzuNi4N2XHLc5CPnNK/DfXwEbV7aUUF2Oae75AtiRF1SavbX9uV3x45t2CP+XneUWp HLaEj/3kH5bKi0h7uonSPVeEeGs+DBI/smg/WZ/OJOvMHAI/hB2Whwfidn2GQA2pmy9h x8ITDWxZEmw9mpEqKBezniQDW7OkNvxkeCOjEZ2EDWim8qeaUsl417pwYiildxpDArjw HI7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=2OOipR6ExT2jNJi4jbCkya5oL8OY5qKBVN4HVC3rk40=; b=md9B0ChXfNkBdrL5/D9KNdNryY5+VG61Ko43DI4STp42VUrc4HvmdhxLXcASEX2gTY gXw0dzP5hrlukribNsyPsY+tvDx/6+dvZCmLrBj2XBVvLD68AW933yf+bp5synhZOv5E aQlJvqpWhi8REONZQ9VNX6oDQTwp4dyNaNTIUvq9MQaxi9ISkVPwESOLtwjy3V7kXwD/ V27RL6/kV3vLi7t/oDXnvHreXmB1Dtm37gRtqkrOGM0+r28gr8TLpi3xQOn4XC9NyCom acFne6LurcFOFRwKR8DafGJ9E+kUTNWZ2h364Rstn8PrDS56VYDRqsKDtC+3DjWK+4wt 33ig== X-Gm-Message-State: APjAAAWm7eZYSnxxtPjGqZODjzv9IrZCbNu2B+J+kwoWaItMCnPHI/ef VNATtemQpN7z0SeY4ryi+txwADVC X-Received: by 2002:a17:90a:cb18:: with SMTP id z24mr39609687pjt.108.1564029040397; Wed, 24 Jul 2019 21:30:40 -0700 (PDT) Received: from localhost ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id t9sm48380995pji.18.2019.07.24.21.30.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 21:30:39 -0700 (PDT) From: Guenter Roeck To: Heikki Krogerus , Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck , Douglas Gilbert Subject: [RFC PATCH] usb: typec: tcpm: Ignore unsupported/unknown alternate mode requests Date: Wed, 24 Jul 2019 21:30:37 -0700 Message-Id: <1564029037-22929-1-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org TCPM may receive PD messages associated with unknown or unsupported alternate modes. If that happens, calls to typec_match_altmode() will return NULL. The tcpm code does not currently take this into account. This results in crashes. Unable to handle kernel NULL pointer dereference at virtual address 000001f0 pgd = 41dad9a1 [000001f0] *pgd=00000000 Internal error: Oops: 5 [#1] THUMB2 Modules linked in: tcpci tcpm CPU: 0 PID: 2338 Comm: kworker/u2:0 Not tainted 5.1.18-sama5-armv7-r2 #6 Hardware name: Atmel SAMA5 Workqueue: 2-0050 tcpm_pd_rx_handler [tcpm] PC is at typec_altmode_attention+0x0/0x14 LR is at tcpm_pd_rx_handler+0xa3b/0xda0 [tcpm] ... [] (typec_altmode_attention) from [] (tcpm_pd_rx_handler+0xa3b/0xda0 [tcpm]) [] (tcpm_pd_rx_handler [tcpm]) from [] (process_one_work+0x123/0x2a8) [] (process_one_work) from [] (worker_thread+0xbd/0x3b0) [] (worker_thread) from [] (kthread+0xcf/0xf4) [] (kthread) from [] (ret_from_fork+0x11/0x38) Ignore PD messages if the asociated alternate mode is not supported. Reported-by: Douglas Gilbert Cc: Douglas Gilbert Fixes: e9576fe8e605c ("usb: typec: tcpm: Support for Alternate Modes") Signed-off-by: Guenter Roeck --- Taking a stab at the problem. I don't really know if this is the correct fix, or even if my understanding of the problem is correct, thus marking the patch as RFC. drivers/usb/typec/tcpm/tcpm.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index 77f71f602f73..1df2844469aa 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -1096,7 +1096,8 @@ static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt, break; case CMD_ATTENTION: /* Attention command does not have response */ - typec_altmode_attention(adev, p[1]); + if (adev) + typec_altmode_attention(adev, p[1]); return 0; default: break; @@ -1148,20 +1149,26 @@ static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt, } break; case CMD_ENTER_MODE: - typec_altmode_update_active(pdev, true); - - if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { - response[0] = VDO(adev->svid, 1, CMD_EXIT_MODE); - response[0] |= VDO_OPOS(adev->mode); - return 1; + if (adev && pdev) { + typec_altmode_update_active(pdev, true); + + if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { + response[0] = VDO(adev->svid, 1, + CMD_EXIT_MODE); + response[0] |= VDO_OPOS(adev->mode); + return 1; + } } return 0; case CMD_EXIT_MODE: - typec_altmode_update_active(pdev, false); + if (adev && pdev) { + typec_altmode_update_active(pdev, false); - /* Back to USB Operation */ - WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, - NULL)); + /* Back to USB Operation */ + WARN_ON(typec_altmode_notify(adev, + TYPEC_STATE_USB, + NULL)); + } break; default: break; @@ -1171,8 +1178,10 @@ static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt, switch (cmd) { case CMD_ENTER_MODE: /* Back to USB Operation */ - WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, - NULL)); + if (adev) + WARN_ON(typec_altmode_notify(adev, + TYPEC_STATE_USB, + NULL)); break; default: break; @@ -1183,7 +1192,8 @@ static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt, } /* Informing the alternate mode drivers about everything */ - typec_altmode_vdm(adev, p[0], &p[1], cnt); + if (adev) + typec_altmode_vdm(adev, p[0], &p[1], cnt); return rlen; } -- 2.7.4