Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3300960pxk; Mon, 21 Sep 2020 10:04:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7OcV6ruB5qTg6c/RZWIuofoI86BIbvTLCnzkgymp0WLpjgSkXkS1u9tEGKWoFbj74MBCA X-Received: by 2002:a17:906:5e59:: with SMTP id b25mr434735eju.414.1600707848800; Mon, 21 Sep 2020 10:04:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600707848; cv=none; d=google.com; s=arc-20160816; b=SDufXuTxWgeqDUys5O4pHZeWjkxhYqCEpCUQt5ZOmyocdW3d88XOn2a7gH1v9e6HxS AoOcLNidh8HwZMhASZJ6w3lKqSYhqAfMP6sz5uvbkMkHBsUY6ak2MC8uY9GuYEG6klgd eaStQjm1C2q6CGtvwTVItW3ZKULIIc5llsOA6N17W00HP3lNN4bK77YGE2ODu57l5I/Y LEUci26Z86DI9fuUy1pdnS3x0Asvnm7WHP6TyFupyUYeYuWbBhTDihy/x17OliOduVjD nJmMjCys92TiAsoGJ2xL4lwvTDO6BG0nl1O0OYc8CDKsd06htjAHbOYOslffDnRyr5cu pLSg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3wDVT34m2E3ywyAawGnj3QieIYNeSdMyYVMqML3Tsro=; b=h8omrIBkdsdSSIrBG85ghF28gbYf8V+3pjFlNUMERu7nB9CFdu+zW+mAGCCS90QYaN zCnERA8fvgWkRyDRWVFTbNg6Znx4mh1JRig8WjhoE6jT28y4VNE0iGPD53SJelrfksSZ 8yWYUsibXclWXF/gfFMDXuhuf5k1xJW408/5zcT+fVP2WDeN9KqCURWBU06CIOtFuRkk S0yiZ3YkUQq2sQ0phcRhADl9vdRFC4ZrkCKVsR9rJrBJE7yqKyuTisBWl9Ud0zSJUP4e LZSZlH9zGtaKtw0BXGmYu3rStZVik31YWxkFubYQ1wvd4bGBmsK0zNYUiy5OEaiD457B cH5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="kWZsBjo/"; 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 j7si8511465ejn.639.2020.09.21.10.03.44; Mon, 21 Sep 2020 10:04:08 -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="kWZsBjo/"; 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 S1729977AbgIURCy (ORCPT + 99 others); Mon, 21 Sep 2020 13:02:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:38590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728759AbgIUQhu (ORCPT ); Mon, 21 Sep 2020 12:37:50 -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 D11AB239D1; Mon, 21 Sep 2020 16:37:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706269; bh=zf7oB3PkR2yVlda4d6EQa8QkhzF5w3CdKqr2FqhnXlQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kWZsBjo/zDQANAfWyJ9hdtgK/nVqWrDadn0mZP0N1z5gWoiMOaNxdNlqO7OnzOwxw 2NgeSgfBEtwuGXsxUdoP9Ou9bcP300UZkoUGtttJVpKb0aZWb2wbfD+rxd0stAjupn 7cZ3qLHGm+Bjl8M7z3G5jEvOHnkYdmKoxLRwohOc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Jonathan Cameron , Andy Shevchenko , Stable@vger.kernel.org Subject: [PATCH 4.14 33/94] iio:light:max44000 Fix timestamp alignment and prevent data leak. Date: Mon, 21 Sep 2020 18:27:20 +0200 Message-Id: <20200921162037.074629662@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200921162035.541285330@linuxfoundation.org> References: <20200921162035.541285330@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jonathan Cameron commit 523628852a5f5f34a15252b2634d0498d3cfb347 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 a 16 byte 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(). This data is allocated with kzalloc so no data can leak appart from previous readings. It is necessary to force the alignment of ts to avoid the padding on x86_32 being different from 64 bit platorms (it alows for 4 bytes aligned 8 byte types. Fixes: 06ad7ea10e2b ("max44000: Initial triggered buffer support") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Reviewed-by: Andy Shevchenko Cc: Signed-off-by: Greg Kroah-Hartman --- drivers/iio/light/max44000.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) --- a/drivers/iio/light/max44000.c +++ b/drivers/iio/light/max44000.c @@ -78,6 +78,11 @@ struct max44000_data { struct mutex lock; struct regmap *regmap; + /* Ensure naturally aligned timestamp */ + struct { + u16 channels[2]; + s64 ts __aligned(8); + } scan; }; /* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */ @@ -492,7 +497,6 @@ static irqreturn_t max44000_trigger_hand struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct max44000_data *data = iio_priv(indio_dev); - u16 buf[8]; /* 2x u16 + padding + 8 bytes timestamp */ int index = 0; unsigned int regval; int ret; @@ -502,17 +506,17 @@ static irqreturn_t max44000_trigger_hand ret = max44000_read_alsval(data); if (ret < 0) goto out_unlock; - buf[index++] = ret; + data->scan.channels[index++] = ret; } if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) { ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); if (ret < 0) goto out_unlock; - buf[index] = regval; + data->scan.channels[index] = regval; } mutex_unlock(&data->lock); - iio_push_to_buffers_with_timestamp(indio_dev, buf, + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, iio_get_time_ns(indio_dev)); iio_trigger_notify_done(indio_dev->trig); return IRQ_HANDLED;