Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp699165lqc; Fri, 8 Mar 2024 09:08:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVUJlGSo1BSKtplS5Gyp8UUY5IUsUsYCK8YVe9EI1JV/HLfNItLCfq9CinezePNVS6fXmfaZ9pGUJ14tz8YBFFhZceHj+ZyE24HvTITaQ== X-Google-Smtp-Source: AGHT+IGIJFxy95DxsLn7q6SGnCt19MIBCwQ0JeYIn7xX332cwnx1crjTo63EUJDDCtIuFS7JKGua X-Received: by 2002:a05:620a:564:b0:788:3fad:6491 with SMTP id p4-20020a05620a056400b007883fad6491mr7985398qkp.63.1709917706144; Fri, 08 Mar 2024 09:08:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709917706; cv=pass; d=google.com; s=arc-20160816; b=EqjTaLpsfZupPYUfh7tIkUnMv9HmC6+Xk1X2RBrOzrZV7cAmrIir8EzHOeeworBrfi Oxqb8Ns8UjRaKiI11EY0Y1KpI/VDd0d3oIJslRXUHfk9LBphgjGnn/CWTcrI5EMzOucD G0iV18m+yN6sH2s/qOZs+iqjYjtiuYZcwOxeUaVhscNLKf9AZppvolHRCIDiF5tSnO7C H5omOuKAJxYlGskDQDrre849Dqf+K1FtHGo4N59/RBhgHjL5N549lV9jxQn5i5riXtHO gfXccuNopSKr6q4kODgUBrfAMyQLBlapZHWmWGtyE6Lrg2n6qbyemOBmHk/K9X9x9aTk Nukg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=SVSOVMM++kpSS7mrO6vPo+8T0YiZCKLh1YcyRU4PU+0=; fh=scrb9YhufrPqHzYrehJAZPm1kIM4FDccU65mCI5/QHU=; b=m6gtiETUB8/qYFyL/IYYI937tjzSlc6mKMsXHv12XORqrKs75JPr6pCV1XbJiMA6j0 6EW5g+7d4MfewPprwrUOTjfpM6KIXJCi9X6eTegpp9018TEFg1vf5KkZCq1yFBtoQZGC pQxOlHbJQfxg4RItWlj2aKBWNf/kTguksiPP42BHsbinCf+ZwRCOSiv/Pozs5iTdGX2J sDIMQx6ah3gNks39/csEwQhhZtMzIcOIQ4jr0NikpVZTqBvYicstuCDN6O7KfmCEBBFA U6w/XtORdXrmniK19eoN+L6prb8E5Orjsjshukm+nFt3jUHkxfaJ37AQgy3f2p4KvvPg YtUA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=jrWFndom; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-97353-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97353-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g14-20020a05620a40ce00b00788136b7872si17177358qko.175.2024.03.08.09.08.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 09:08:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97353-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=jrWFndom; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-97353-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97353-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id DB5C11C21FF7 for ; Fri, 8 Mar 2024 17:08:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B40EF249E3; Fri, 8 Mar 2024 17:08:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="jrWFndom" Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 549482420C; Fri, 8 Mar 2024 17:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709917701; cv=none; b=jMRRA3MnI7OEsAjeIAaecPG51cvdsl4BXhM0MlS/lf5NI5Oe4lMOIuCHDYxJ3IEfSamjfQ3JNsZj0NyIuYjXXhfUadG8Mdr0u9+FevtSRsKOYy/lLSfGzAoiRhQPw5OV0KOY2CLuNXRsvcWKQ5AyadQMEUf6W3Knewv06Z3qFyg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709917701; c=relaxed/simple; bh=yjkgaWbfvxOAOJ8/GrcE5ERV6Xdug+DVVLT7XjhOlTk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uPq9rxy8qun47y8VVcJGkH8qVck79FNRw3U5ZbLmB1FHJkjt2qB4HTJVC/alBuBIuk+3xzR//7QwO3WHxhXja5ciwxdYgO4rZ2k2ptp8gDLCrazicNy0K3SUOCXY618pNIysIg74qN51VTnt4h0CZWh2TNvRnC1wyxhSlG1lTiY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=jrWFndom; arc=none smtp.client-ip=217.70.183.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 8CE101BF208; Fri, 8 Mar 2024 17:08:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1709917690; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SVSOVMM++kpSS7mrO6vPo+8T0YiZCKLh1YcyRU4PU+0=; b=jrWFndomKXlOKifh8SJu/xxEkVdtVmr98MEjZ+7hFRthTHJKujWCykaljnvIvOdtm7Dok6 M5tjTkWxhcWVLR0ib0dwtzRbmM11eEqG5foA9pKwMy/DXOg4uUggHkmFvEaQY100sS/+Bp pYaikC+wimyQPKjSNwhgfeVMhVXpOGlVMaVzeT6+bf/kqrKuhov+Afv8A5Gw+uPjZF+ekD aAR07y5nHuo2+++FZbpPxmXO5XbPt08sNYX9ChZpYhxl/4TddBcUBnt7CB4XYeBgpboqZN OxtD+TGlSCuakbxbSCTP3XXoc69X0TmpYm1qLJ/scQ+Yp+jUu3MY8tnLiY3dfw== Date: Fri, 8 Mar 2024 18:08:10 +0100 From: Alexandre Belloni To: Nicholas Miehlbradt Cc: a.zummo@towertech.it, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] rtc: fix uninitialized read of rtc_wkalrm.time Message-ID: <2024030817081059be4ef2@mail.local> References: <20231129073647.2624497-1-nicholas@linux.ibm.com> <20240229215850a1990100@mail.local> <66a2c8b3-b1e8-4d2c-8a19-09e62099a2d7@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <66a2c8b3-b1e8-4d2c-8a19-09e62099a2d7@linux.ibm.com> X-GND-Sasl: alexandre.belloni@bootlin.com On 08/03/2024 11:53:13+1100, Nicholas Miehlbradt wrote: > > > On 1/3/2024 8:58 am, Alexandre Belloni wrote: > > Hello, > > > > On 29/11/2023 07:36:47+0000, Nicholas Miehlbradt wrote: > > > If either of the first two branches of the if statement in > > > rtc_read_alarm_internal are taken the fields of alarm->time are not > > > initialized but are subsequently read by the call to rtc_tm_to_time64. > > > > > > Refactor so that the time field is only read if the final branch of the > > > if statment which initializes the field is taken. > > > > > > > While the problem description is correct, the solution is not because > > you have no guarantee that the fields have been initialized if > > ->read_alarm returns a value different from 0 > > > > So, instead of avoiding the conversion unless the final branch is taken, > > it should be avoided as long as err != 0. > > > > But, I'm also wondering whether there is actually an issue. mktime64 > > can be fed whatever value without bugging out and the value of err will > > be part of the trace so userspace knows that it shouldn't trust the > > value. > > > > So, what is the actual issue? :) > > Thank you for your feedback. > I found this issue during my implementation of KMSAN for powerpc. The goal > with this patch is to eliminate use of undefined memory which leads to > undefined behaviour, I should have made this more clear in my original > message. You can find the KMSAN patch series here: > https://lore.kernel.org/linuxppc-dev/20231214055539.9420-1-nicholas@linux.ibm.com/ > > I can make the changes suggested and fold this patch into the next version > of my KMSAN series if that would help to add context as to why I am > submitting this patch? Please do but I must say I don't really like "defensive" programming just to please a static checker. There is no actual issue here, even if the memory is not initialized, the result will never be incorrect and the various checks that are being added everywhere are just bloating the kernel, making it slower to boot and run. Now, we even have useless CVEs that are assigned for this kind of issues... Maybe it would be better to ensure the correctness of the result rather than having a look at whether the memory was initialized or not. > > > > > Signed-off-by: Nicholas Miehlbradt > > > --- > > > drivers/rtc/interface.c | 4 +++- > > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > > > diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c > > > index 1b63111cdda2..f40e76d2fe2b 100644 > > > --- a/drivers/rtc/interface.c > > > +++ b/drivers/rtc/interface.c > > > @@ -179,6 +179,7 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, > > > struct rtc_wkalrm *alarm) > > > { > > > int err; > > > + time64_t trace_time = -1; > > > err = mutex_lock_interruptible(&rtc->ops_lock); > > > if (err) > > > @@ -201,11 +202,12 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, > > > alarm->time.tm_yday = -1; > > > alarm->time.tm_isdst = -1; > > > err = rtc->ops->read_alarm(rtc->dev.parent, alarm); > > > + trace_time = rtc_tm_to_time64(&alarm->time); > > > } > > > mutex_unlock(&rtc->ops_lock); > > > - trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); > > > + trace_rtc_read_alarm(trace_time, err); > > > return err; > > > } > > > -- > > > 2.37.2 > > > > > -- Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com