From patchwork Wed May 26 12:39:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 12281811 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3EDDC4708B for ; Wed, 26 May 2021 14:16:07 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6434E61026 for ; Wed, 26 May 2021 14:16:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6434E61026 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=jtT9miiVORytNaZ2xoDiApzNl+NhPQugpzd7xOfRDgQ=; b=RmsTIbwD7yzmr0 FFcFyLAlaVM1Ym1wEdz1ry4kx+w2UZiyCD+3NZjyXbEPlJ4J373vVnni7idPpKzxbXondJIxrZn3h 0Pl3mhdhdxjtp3WUjUgnYGG8ztNWzsta9k0Q8R/mzr1QPgJ9YmNP9U48aGY3fudMfuSoubeNQCUEO w5bjj954AJrOjl/CztvMFe4kVg9xcYPZXDl8SxHF6ce0qEaMM87xx47NAHT0DTA03w62UHCu3IJWJ a9j0rIRErksB8i9d/U4RHBg3rbo2NLC6IMSp2N1MbRyVcza2epJh/XCfQ0Ge+5ESnf73qs7mh3Y1O UT5MIlzuV0/TtzrtO66Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lluIA-00ElRE-Gh; Wed, 26 May 2021 14:13:52 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1llsp9-00E4mC-L6 for linux-arm-kernel@lists.infradead.org; Wed, 26 May 2021 12:39:48 +0000 Received: from mail-vs1-f71.google.com ([209.85.217.71]) by youngberry.canonical.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1llsp6-00007m-Gm for linux-arm-kernel@lists.infradead.org; Wed, 26 May 2021 12:39:44 +0000 Received: by mail-vs1-f71.google.com with SMTP id y13-20020a67d20d0000b02902354e7b1beaso186775vsi.5 for ; Wed, 26 May 2021 05:39:44 -0700 (PDT) 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=gN2u9zXldM21EHt67+sMR5HlACJwh4tvgZXmS6a8P94=; b=H2G/Mz6TxLU/6yeLedWOITnBMMO403J2C0pbg+BzYzavk84ks2VaU+ZzARVL8zFfJ2 8CHlQKdU6J0ZXzbz96DJI57KNIBhW2PYA+EBcrHQKhPmWkhcw83MTu1HwuxyLfzM6g+3 cmTJftv8cVnsP3fCbXXMu+Xih3hLYEonIOPdx5Q1Ktc2QKvHiKIDK3jtJ3ckyOW8tonz Swlrq/giNKBSt2BAP33kZfafHcGIreQEtszqgAeCU7UhGxXK7eK8hz02Rdbu1rkVl5gF PZkXWYwQoK5pHcFrhCOU4ezdgT7YFz5Re1qAAi/nG6O7TNbhhAOc92d/CiuOO0rSyBl8 Awvg== X-Gm-Message-State: AOAM533LGvYpR5GGHZRMyI5PyKXMeuULxy38fmZEoAb5ZKl26B8dXjLN jq5L2vS0liU7YRAJr2xapca60aib/5V9FC/FI4WuqqgvPPPH+qDOq2+VVpm+l2S1lH9zBILLT50 QeI8xzQ2Hx1PrWNc45gJlfORCCbbV1pqgHlUAj9Lws1WXECZZXoMZ X-Received: by 2002:ab0:5acf:: with SMTP id x15mr32137317uae.132.1622032783581; Wed, 26 May 2021 05:39:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/SfZLr/bmF5flVczWQ9oLy5YTXK6WvySQPDkCj5uV/aFtLdOjVhbvV6qeYmmthiflkK7Q7Q== X-Received: by 2002:ab0:5acf:: with SMTP id x15mr32137298uae.132.1622032783424; Wed, 26 May 2021 05:39:43 -0700 (PDT) Received: from localhost.localdomain ([45.237.48.4]) by smtp.gmail.com with ESMTPSA id n77sm1693475vke.24.2021.05.26.05.39.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 May 2021 05:39:42 -0700 (PDT) From: Krzysztof Kozlowski To: Krzysztof Kozlowski , linux-i2c@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Sylwester Nawrocki , Bartlomiej Zolnierkiewicz , Tomasz Figa Subject: [RESEND PATCH] i2c: s3c2410: fix possible NULL pointer deref on read message after write Date: Wed, 26 May 2021 08:39:37 -0400 Message-Id: <20210526123937.22958-1-krzysztof.kozlowski@canonical.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210526_053947_734403_E043DC43 X-CRM114-Status: GOOD ( 16.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Interrupt handler processes multiple message write requests one after another, till the driver message queue is drained. However if driver encounters a read message without preceding START, it stops the I2C transfer as it is an invalid condition for the controller. At least the comment describes a requirement "the controller forces us to send a new START when we change direction". This stop results in clearing the message queue (i2c->msg = NULL). The code however immediately jumped back to label "retry_write" which dereferenced the "i2c->msg" making it a possible NULL pointer dereference. The Coverity analysis: 1. Condition !is_msgend(i2c), taking false branch. if (!is_msgend(i2c)) { 2. Condition !is_lastmsg(i2c), taking true branch. } else if (!is_lastmsg(i2c)) { 3. Condition i2c->msg->flags & 1, taking true branch. if (i2c->msg->flags & I2C_M_RD) { 4. write_zero_model: Passing i2c to s3c24xx_i2c_stop, which sets i2c->msg to NULL. s3c24xx_i2c_stop(i2c, -EINVAL); 5. Jumping to label retry_write. goto retry_write; 6. var_deref_model: Passing i2c to is_msgend, which dereferences null i2c->msg. if (!is_msgend(i2c)) {" All previous calls to s3c24xx_i2c_stop() in this interrupt service routine are followed by jumping to end of function (acknowledging the interrupt and returning). This seems a reasonable choice also here since message buffer was entirely emptied. Addresses-Coverity: Explicit null dereferenced Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Krzysztof Kozlowski --- Reason for resend - the patch did not get necessary testing however I think it is correct, therefore resending without "RFT". --- drivers/i2c/busses/i2c-s3c2410.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/i2c/busses/i2c-s3c2410.c b/drivers/i2c/busses/i2c-s3c2410.c index ab928613afba..4d82761e1585 100644 --- a/drivers/i2c/busses/i2c-s3c2410.c +++ b/drivers/i2c/busses/i2c-s3c2410.c @@ -480,7 +480,10 @@ static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) * forces us to send a new START * when we change direction */ + dev_dbg(i2c->dev, + "missing START before write->read\n"); s3c24xx_i2c_stop(i2c, -EINVAL); + break; } goto retry_write;