Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp49079pxk; Tue, 15 Sep 2020 17:26:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7VIS+9p78cppkooT5q3e5iil2P9RFTMqG7/T0xYY75j97QW1yjNzr4GtOgqbbEdEcgPnV X-Received: by 2002:aa7:de91:: with SMTP id j17mr25401376edv.85.1600215981565; Tue, 15 Sep 2020 17:26:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600215981; cv=none; d=google.com; s=arc-20160816; b=JYkbo12tgDTHVFRGL0D1Ei2rCarVyr/hvJJjyFtWchOfjzKGCGCRykiLVmSTNeqa70 u9TyOQ6pWaKRy62ux3v+86jbp2idIaduXYs+4FTmWrMVDNiTPl+9KEvNRVsaGYgWh8Ld l57jzND6ozEWfMHnRTwt2Cp+V6llpCIGXc2yMvAbGToy3HcHKy+V3+ljgFEq4HUCr2Tb f+Ed9C2vWUp3MYzQ+xswOO7D2E3d89vmMn87T1+x/ERFf3WggFSkG0Qnfbf9hJRCxXj6 ye0gMxv2ouQ9SfH+U8149xYjJAUDkpAHYzqNqDtREJ+aq31MHaakC9JtIGMZQdpdd4zY MnkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=cE5cE4OsPwozn+Z0zY9uRpw2JhsRZILiSO1gnLlIZ/g=; b=Y3l5iu9BUDLgSfYsxuvP9FZPAXMZ3APFaUMNwT1DFA/VZUVl7vi/pNJN/h/T21nIcn 1e0Tkji1m/bJGIkmj0WvsMJDfEIliKACsuW9EIu6XVFawLUHMRl5WPE0TPG4VSXB2iOF eQ43CZxoSpAuMIpJ0PbG7LweqIhlM+9V9vB19CRrGsUP5eRn0VGBvAqSrcfp/iE5vE2H ZDAjDxB2Dd4ll55JyTcqB5C1FvN42i8zS67EHwzmV4Kt8eHqqQECtJvcT0UCAEZV+7s0 TO0jrDbGPfRhUAYtciZyiERt63eBmMo1f7Vf/7nBtrLDJOI6OuYiKj4x48kIw9JvC385 qBWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cltSkuJh; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y4si3024499edp.279.2020.09.15.17.25.59; Tue, 15 Sep 2020 17:26:21 -0700 (PDT) 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=@kernel.org header.s=default header.b=cltSkuJh; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727091AbgIPAXW (ORCPT + 99 others); Tue, 15 Sep 2020 20:23:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:34582 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726528AbgIOOU7 (ORCPT ); Tue, 15 Sep 2020 10:20:59 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 44F3C22243; Tue, 15 Sep 2020 14:16:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600179407; bh=P7jqaOAApyzqqrUZrsopvtD81Sgcku1U3iZX28GTxMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cltSkuJhXP4A04I5MK3QWzCV12oKXbXtKV7awMY1uscPi3uvpWZpYyDqKHeMLyIcj +M03alyYwiFyshU6cIwCEDV2g9LhdCiDoSoVYXskWiOSQCOZ0I2ouDuGyVzs53XmpJ jH7b8omxSY0Gq0t/kib48xyJSqIGRGdKnJ+E53ak= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Gregor Boirie , Linus Walleij , Jonathan Cameron , Andy Shevchenko , Stable@vger.kernel.org Subject: [PATCH 4.19 48/78] iio:magnetometer:ak8975 Fix alignment and data leak issues. Date: Tue, 15 Sep 2020 16:13:13 +0200 Message-Id: <20200915140635.983651965@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200915140633.552502750@linuxfoundation.org> References: <20200915140633.552502750@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jonathan Cameron commit 02ad21cefbac4d89ac443866f25b90449527737b upstream. One of a class of bugs pointed out by Lars in a recent review. iio_push_to_buffers_with_timestamp assumes the buffer used is aligned to the size of the timestamp (8 bytes). This is not guaranteed in this driver which uses an array of smaller elements on the stack. As Lars also noted this anti pattern can involve a leak of data to userspace and that indeed can happen here. We close both issues by moving to a suitable structure in the iio_priv() data. This data is allocated with kzalloc so no data can leak apart from previous readings. The explicit alignment of ts is not necessary in this case as by coincidence the padding will end up the same, however I consider it to make the code less fragile and have included it. Fixes: bc11ca4a0b84 ("iio:magnetometer:ak8975: triggered buffer support") Reported-by: Lars-Peter Clausen Cc: Gregor Boirie Cc: Linus Walleij Signed-off-by: Jonathan Cameron Reviewed-by: Andy Shevchenko Cc: Signed-off-by: Greg Kroah-Hartman --- drivers/iio/magnetometer/ak8975.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) --- a/drivers/iio/magnetometer/ak8975.c +++ b/drivers/iio/magnetometer/ak8975.c @@ -381,6 +381,12 @@ struct ak8975_data { struct iio_mount_matrix orientation; struct regulator *vdd; struct regulator *vid; + + /* Ensure natural alignment of timestamp */ + struct { + s16 channels[3]; + s64 ts __aligned(8); + } scan; }; /* Enable attached power regulator if any. */ @@ -815,7 +821,6 @@ static void ak8975_fill_buffer(struct ii const struct i2c_client *client = data->client; const struct ak_def *def = data->def; int ret; - s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */ __le16 fval[3]; mutex_lock(&data->lock); @@ -838,12 +843,13 @@ static void ak8975_fill_buffer(struct ii mutex_unlock(&data->lock); /* Clamp to valid range. */ - buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range); - buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range); - buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range); + data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range); + data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range); + data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range); - iio_push_to_buffers_with_timestamp(indio_dev, buff, + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, iio_get_time_ns(indio_dev)); + return; unlock: