Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3302737pxk; Mon, 21 Sep 2020 10:06:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6qXj/mAWFyZo/QekFvDKFxDYkk7XxGM6Iq76nNmKk7WVrOFkNamS/jBwcdCipx3k3M8oW X-Received: by 2002:a50:9d0a:: with SMTP id v10mr660760ede.144.1600707979531; Mon, 21 Sep 2020 10:06:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600707979; cv=none; d=google.com; s=arc-20160816; b=nVyJpEpMle+a0KUq791WGrnJt9DpwcGvBNWvQ8gV5xuJ5TUWqAsAkYhUnK/rMhU1Ue PI+s/lDGLUV4qb6eCxy77MjYq+kKUtp/ekDtLVyUP2xrKx8gqv0k51Fat8DC8urbznZN PlZEKhRZBgHtIi5KjeHrsunKPZpNdjGtRC1fuOphB4FINRcWEozu1zWQlMl9a4MBDjzw g9djcRUDfLHnGjBb5q8PFEnGShWztPB4IeygT/XFWx9sXbkGQ1XoTM2DJ4AQwmCnt5S8 s/iPRLUzTUgx9AGJPLLoUuQxLlZMpwJ1rItdraARNf8MhzTirN0Fry8Z8cXIOEpGMelF BKww== 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=cE5cE4OsPwozn+Z0zY9uRpw2JhsRZILiSO1gnLlIZ/g=; b=g0+slE6RHAtHWu+9JynQBh0uoCelYrBWFQ5UtZLdFS7t0qrwOh+Eo7QocKIB1CP/3E Hw0J7ylkU8Rji7IZYq6ot+YpI2rjeJZ3AWyVGR+7fcdXSOUxcKMwaBeMZJkXjesG5V4h y0D7qzRQnaQKR3OXa7/zkrqJi7/21hfNDfXYhdK4/x4Sz4s4ivLv9ehkJJ4rrn7+Ufe2 Xdk1jfIj1aI5CzemVufXgn1Ly3+09zRyLJt72swzL5RX3Aq3JiHv6VisCvSH83ujpTLZ wn7g+dV2VNnZszarOcbOB10NlZPA2Tgm1pGnhvIDTi/BM1Pp2h1gDZr1NJ4QPzrAC9zo Xz8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Fb6cotHy; 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 qn24si8445708ejb.299.2020.09.21.10.05.53; Mon, 21 Sep 2020 10:06:19 -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=Fb6cotHy; 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 S1729143AbgIURCz (ORCPT + 99 others); Mon, 21 Sep 2020 13:02:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:38516 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729104AbgIUQhr (ORCPT ); Mon, 21 Sep 2020 12:37:47 -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 01E9D23998; Mon, 21 Sep 2020 16:37:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706266; bh=P7jqaOAApyzqqrUZrsopvtD81Sgcku1U3iZX28GTxMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fb6cotHyP/E4/pMs9yeLqzpiF/VzwnUSmx08LlYxsFklOl1JW7oV3+psgHQZgij6f kDCJdqPSLbYbGPQmX0GZJIjSkPcA+zHYVcE0dMDJmKRwzxDBJmvoyoTF0QAtFzzEUo LbXtnozzqeoYjKDMSLJBHai7EoLDJC3lWr16gMTQ= 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.14 32/94] iio:magnetometer:ak8975 Fix alignment and data leak issues. Date: Mon, 21 Sep 2020 18:27:19 +0200 Message-Id: <20200921162037.022542525@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 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: