Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp881696pxb; Wed, 27 Oct 2021 14:23:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPpAQlXAKwmDFv26uHIu7RWJVH3JCfb5Qi2i8u3laI5aZ5o4JRzkSjwMzHX/0toKnpisj3 X-Received: by 2002:a17:906:58c1:: with SMTP id e1mr34198ejs.327.1635369812124; Wed, 27 Oct 2021 14:23:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635369812; cv=none; d=google.com; s=arc-20160816; b=YlpwpocSHnyyuVhXjwoJj7f16q+a0Gst5DrnXRy6vW8FV3oieUztJ2VRjjcGDgLtnb rbbcfXP1tiPYjkF+NdibG0IFVlY6ArX+dZANMpgOn+w5FqeK2C9j0A10vVuF3adD0W0E zAPB2FV65BF/JEBCMphXdsu0EL8R7fkpo2OiNSgcFJ7z9xySnqKpe37HJLtegTnTG2zO G6zPjH5EtSRdugY6t+SaNP5nUQT4gJZYkyaTDOj8V+RadmpRdgcOR+co7W1AIFoXFqpa ZTXIMGbBw1W+8oJfTYNaKSmgoxF4ZcqvV08ZU/6bAdOZocb+B1FXQKMCdkSDf+wgbM8j ZyMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=FXR+7/0tIrfqJJEc5+wVl2jOVnlWntX8HhrBDUs0vXo=; b=KtM+UN9JDkSzHrO1007ohUvJEEH0w0L4foskunuDsQ/sJWp55psF2o93VKDCbcpfeN dhfYpsB0sjJO0BMhh/GljvoP3Dix6jbNMYhXE1qR3UPUVnc+cpidzBX/VjV9mdCdCLJ/ mn88y0pfGfbGXqqNa7u3s8oVnF6NtWpcDhrUJKbzzlN8wdm2cjDpdnzXwT7NYhQezDvI eA6MImQKW4iBI4jgMRXX5YbqJgDukalF1SHb+rd/dujVsW1crLvHC7zPF1v7EbV/G/1r jlDfC8kKLWsErC+8z8ajU6r34MUpvIeB1mPktD2hSaoQguyKAqRWqhf2qH7Xlpo3I0gt Q2/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fAgpILfQ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y9si1098008edi.598.2021.10.27.14.22.56; Wed, 27 Oct 2021 14:23:32 -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=k20201202 header.b=fAgpILfQ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234129AbhJ0JI1 (ORCPT + 97 others); Wed, 27 Oct 2021 05:08:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:33728 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232132AbhJ0JI0 (ORCPT ); Wed, 27 Oct 2021 05:08:26 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2199461039; Wed, 27 Oct 2021 09:06:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635325561; bh=hBNFBmosy980W50NwGoL+Bn47zg/HLrLHxp0wsfh1TM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fAgpILfQAHf+tHoZMPMLWTYoqWLaBWiBeFTxJNucFW7qiin6IWgtWHEYUpG7tY9wE QmrfdpyaZLm+e/WXJzAwzhJDbD6NoQVzI0S2E9XNqKrLDli9vntedVqPJ/rwmB629q +xZ0w2sOLHRSyj34lzybXbQOBbI5ZkJIMZOalf71JP6YYqSB2+nF0ezrakHF9IRlhK i98CR6XhkZ9Bs+PA5yIUX1w4ly4WlwEehBeHoi5OWCCtUF1a+f7UivX8AuCUPqfxsx lC1bLjascYh7hAoPUqEoUnnxuFOFwPS+ZcihignJ1HSeB7nfPCWUW+Cfx8ey7uLrWp NYtAOtUZxlj9w== Received: from johan by xi.lan with local (Exim 4.94.2) (envelope-from ) id 1mfesT-00038K-BI; Wed, 27 Oct 2021 11:05:45 +0200 Date: Wed, 27 Oct 2021 11:05:45 +0200 From: Johan Hovold To: Ian Abbott Cc: H Hartley Sweeten , Greg Kroah-Hartman , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 2/5] comedi: dt9812: fix DMA buffers on stack Message-ID: References: <20211025114532.4599-1-johan@kernel.org> <20211025114532.4599-3-johan@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 26, 2021 at 03:27:13PM +0100, Ian Abbott wrote: > On 25/10/2021 12:45, Johan Hovold wrote: > > USB transfer buffers are typically mapped for DMA and must not be > > allocated on the stack or transfers will fail. > > > > Allocate proper transfer buffers in the various command helpers and > > return an error on short transfers instead of acting on random stack > > data. > > > > Note that this also fixes a stack info leak on systems where DMA is not > > used as 32 bytes are always sent to the device regardless of how short > > the command is. > > > > Fixes: 63274cd7d38a ("Staging: comedi: add usb dt9812 driver") > > Cc: stable@vger.kernel.org # 2.6.29 > > Signed-off-by: Johan Hovold > > --- > > drivers/comedi/drivers/dt9812.c | 109 ++++++++++++++++++++++++-------- > > 1 file changed, 82 insertions(+), 27 deletions(-) > > > > diff --git a/drivers/comedi/drivers/dt9812.c b/drivers/comedi/drivers/dt9812.c > > index 634f57730c1e..f15c306f2d06 100644 > > --- a/drivers/comedi/drivers/dt9812.c > > +++ b/drivers/comedi/drivers/dt9812.c > > @@ -32,6 +32,7 @@ > > #include > > #include > > #include > > +#include > > #include > > > > #include "../comedi_usb.h" > > @@ -237,22 +238,41 @@ static int dt9812_read_info(struct comedi_device *dev, > > { > > struct usb_device *usb = comedi_to_usb_dev(dev); > > struct dt9812_private *devpriv = dev->private; > > - struct dt9812_usb_cmd cmd; > > + struct dt9812_usb_cmd *cmd; > > int count, ret; > > + u8 *tbuf; > > > > - cmd.cmd = cpu_to_le32(DT9812_R_FLASH_DATA); > > - cmd.u.flash_data_info.address = > > + cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); > > + if (!cmd) > > + return -ENOMEM; > > + > > + cmd->cmd = cpu_to_le32(DT9812_R_FLASH_DATA); > > + cmd->u.flash_data_info.address = > > cpu_to_le16(DT9812_DIAGS_BOARD_INFO_ADDR + offset); > > - cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size); > > + cmd->u.flash_data_info.numbytes = cpu_to_le16(buf_size); > > > > /* DT9812 only responds to 32 byte writes!! */ > > ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, devpriv->cmd_wr.addr), > > - &cmd, 32, &count, DT9812_USB_TIMEOUT); > > + cmd, sizeof(*cmd), &count, DT9812_USB_TIMEOUT); > > + kfree(cmd); > > if (ret) > > return ret; > > > > - return usb_bulk_msg(usb, usb_rcvbulkpipe(usb, devpriv->cmd_rd.addr), > > - buf, buf_size, &count, DT9812_USB_TIMEOUT); > > + tbuf = kmalloc(buf_size, GFP_KERNEL); > > + if (!tbuf) > > + return -ENOMEM; > > + > > + ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, devpriv->cmd_rd.addr), > > + tbuf, buf_size, &count, DT9812_USB_TIMEOUT); > > + if (!ret) { > > + if (count == buf_size) > > + memcpy(buf, tbuf, buf_size); > > + else > > + ret = -EREMOTEIO; > > + } > > + kfree(tbuf); > > + > > + return ret; > > } > > I suggest doing all the allocations up front so it doesn't leave an > unread reply message in the unlikely event that the tbuf allocation > fails. (It could even allocate a single buffer for both the command and > the reply since they are not needed at the same time.) These small allocations will currently never fail, but if they ever were to, there are other allocations done in the I/O path which would have the same effect if they failed. And if this ever happens, you certainly have bigger problems than worrying about the state of this device. :) That said, I'll see if I can reuse a single buffer without things getting too messy. Johan