Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp74887imm; Tue, 12 Jun 2018 18:49:21 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ5WCYA1EJ68/RXkUuSszMcA1wIUB6rucNefsH8iAHdAGCs/rgRpi6eXRoE2k9BRKt/I6v0 X-Received: by 2002:a63:ac11:: with SMTP id v17-v6mr2376768pge.274.1528854561235; Tue, 12 Jun 2018 18:49:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528854561; cv=none; d=google.com; s=arc-20160816; b=tkEMIHOWEAOa0fqAZMXCwCMqr9jFUkprUkScAUFqI2iiUM/Y9gPCp9SGPUqyyjClI+ pS9vW54h48Zb24svG8X/xjx0+tPJhgL5NeZSM6qbFW9Yn6intspRMiimKnrZ2W+CnQxq syes/oUDTHhIOzz0lcP3wwvVhvuDQu0tdXqXHyGK5/p7PjSKmaoLWKqMyR57KfKfmmzx nflG52twe42sMgafwHNrhcV9SffoSgGmC50xr330Cf3Jrxymfzlq+a8j1yrU4dtDZ2la Tj/Zb/ibPnbiNvrRI7MTtgow9t0eFEorMdBPg8Dif31WR7BnnFnpCRFLWY50/UcksxKQ LJyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date :arc-authentication-results; bh=JRso5xQbSBH0bArojD5Y4d23kofBvvUCRfFAqDbz0j8=; b=0DNWrKThMQytaYyFYHRxg4ERH2IUJzXbNL3IbAgmAbzwWWi7xNG0T0+4CsJjgJVn+M l3vxMcp9ReHv1pmPJqKmonFzTR99SBKshIdTnrp84yRWQvhmz57q1LCBU4OqJ9Islmzy /FljUV7qNgsgp3Ne6pQu+sdw+mPrY+0YspIKKYTwU2EZCkW35FGbwN9nBeEsC8fOSTsE Wp8IBFvJjkkERmAvesctuBTvohN0e6wD735DkCZZ58cxM4ay/JtWmcZ7a6AQitQfyZKl HVK974GK/nKLXDbK2+G3NqGZwS4o1LPDg7XWsfFdVkFOOzO2GBl1eqxHCHJJPngguMb1 DGoA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d2-v6si1216561pge.342.2018.06.12.18.49.06; Tue, 12 Jun 2018 18:49:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934303AbeFMBrv (ORCPT + 99 others); Tue, 12 Jun 2018 21:47:51 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:53973 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932985AbeFMBrv (ORCPT ); Tue, 12 Jun 2018 21:47:51 -0400 X-Originating-IP: 70.80.172.8 Received: from localhost (modemcable008.172-80-70.mc.videotron.ca [70.80.172.8]) (Authenticated sender: hle@owl.eu.com) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id BDDB460014; Wed, 13 Jun 2018 01:47:45 +0000 (UTC) Date: Tue, 12 Jun 2018 21:47:41 -0400 From: Hugo Lefeuvre To: Greg Kroah-Hartman Cc: devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Dan Carpenter , Marcus Wolf Subject: [PATCH v2] staging: pi433: fix race condition in pi433_ioctl Message-ID: <20180613014741.GC2265@hle-laptop.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.0 (2018-05-17) X-Spam-Level: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the PI433_IOC_WR_TX_CFG case in pi433_ioctl, instance->tx_cfg is modified via copy_from_user(&instance->tx_cfg, argp, sizeof(struct pi433_tx_cfg))) without any kind of synchronization. In the case where two threads would execute this same command concurrently the tx_cfg field might enter in an inconsistent state. Additionally: if ioctl(PI433_IOC_WR_TX_CFG) and write() execute concurrently the tx config might be modified while it is being copied to the fifo, resulting in potential data corruption. Fix: Get instance->tx_cfg_lock before modifying tx config in the PI433_IOC_WR_TX_CFG case in pi433_ioctl. Also, do not copy data directly from user space to instance->tx_cfg. Instead use a temporary buffer allowing future checks for correctness of copied data. Signed-off-by: Hugo Lefeuvre --- Changes in v2: - Use device->tx_fifo_lock instead of introducing a new lock in instance. - Do not copy data directly from user space to instance->tx_cfg, instead use a temporary buffer allowing future checks for correctness of copied data. --- drivers/staging/pi433/pi433_if.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c index b061f77dda41..3ec1ed01d04b 100644 --- a/drivers/staging/pi433/pi433_if.c +++ b/drivers/staging/pi433/pi433_if.c @@ -880,6 +880,7 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) int retval = 0; struct pi433_instance *instance; struct pi433_device *device; + struct pi433_tx_cfg tx_cfg_buffer; void __user *argp = (void __user *)arg; /* Check type and command number */ @@ -902,9 +903,15 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return -EFAULT; break; case PI433_IOC_WR_TX_CFG: - if (copy_from_user(&instance->tx_cfg, argp, - sizeof(struct pi433_tx_cfg))) + /* do not modify tx config while it is being copied to fifo */ + mutex_lock(&device->tx_fifo_lock); + if (copy_from_user(&tx_cfg_buffer, argp, + sizeof(struct pi433_tx_cfg))) { + mutex_unlock(&device->tx_fifo_lock); return -EFAULT; + } + memcpy(&instance->tx_cfg, &tx_cfg_buffer, sizeof(struct pi433_tx_cfg)); + mutex_unlock(&device->tx_fifo_lock); break; case PI433_IOC_RD_RX_CFG: if (copy_to_user(argp, &device->rx_cfg, -- 2.17.1