Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1329000imm; Wed, 13 Jun 2018 18:06:22 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIjrE1kCekQkzks7l7JY/PwW69lTWrq4ZG2KasA/s7IqDq/ozwpgUUgPOdaQTgrcaUQzJ0M X-Received: by 2002:a63:ba4b:: with SMTP id l11-v6mr378176pgu.295.1528938382866; Wed, 13 Jun 2018 18:06:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528938382; cv=none; d=google.com; s=arc-20160816; b=WstuDJ/JErlIcBpBsOIncfHvGS1ymEN1t1jvsm+ExyfKZ4cAx8BShQ9+zYXrJok2Xc 7DQOU0fAsOLi0kDDyaY7lJ+DGJZtxyNQwLNSj7JQmN9UxpIUHW1GsmAeRz7esTXtTSHK zPP3JMMsG1/2qBZntNQ9lYBORpNVP1jbM9VCESq19wZVOfu3xXphv6TBOB4nC+YbdA8c lY7w7cMTZ8f2gIicpc4bAupE1S8YtHyVn6gGIhsCItYXugZ6VSi7/Q1ufZBPnoN3ONSf vCTQMmnVGBQ3HKQJRj3v3csBTVD9ic2dz9EC7PyLyTheS8+tHWOq3Ri4x7hiwFdFNo8o Fp3A== 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=oxPxa+iSQ3OFatbV7BXLgrZ2W7uTCUZEOWtGslrKHAY=; b=ftr2ejCBtTADN7CrU5bBnybBHCW73L7eceF5x5ClvjdxINu11jnQsjhTVUsgx+kdvk YgRUR3F+GNgVcAXj1slUlYBmO4Uql8Rp1yf4QAiDeJyOZ+/KAkjMbuTUf5i01rX3mM+L DOYZ9lEi1FDJRxWWSBNIqrFL5z3qq8Sy4/hK8J+4iTH9yQxZTpm9G2iSRLhBRuTyGNV/ tsm9/a4JCrMb57vRKw3YBrPiJgkKFmBS7/qO9/6d3fNTpn8nKFrcKAuAzq7XhUYkDqlM m7PIeI4NeFGZ9/9H7v9aeP9Mxfnl/F06PQg116SdeSGEt+9kAzL3nLV6dflxR29vHOO9 aOvw== 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 j12-v6si3391841pgq.312.2018.06.13.18.06.06; Wed, 13 Jun 2018 18:06:22 -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 S936032AbeFNBEr (ORCPT + 99 others); Wed, 13 Jun 2018 21:04:47 -0400 Received: from relay1-d.mail.gandi.net ([217.70.183.193]:44063 "EHLO relay1-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935907AbeFNBEp (ORCPT ); Wed, 13 Jun 2018 21:04:45 -0400 X-Greylist: delayed 86791 seconds by postgrey-1.27 at vger.kernel.org; Wed, 13 Jun 2018 21:04:45 EDT 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 relay1-d.mail.gandi.net (Postfix) with ESMTPSA id D826F240003; Thu, 14 Jun 2018 01:04:44 +0000 (UTC) Date: Wed, 13 Jun 2018 21:04:38 -0400 From: Hugo Lefeuvre To: Greg Kroah-Hartman Cc: devel@driverdev.osuosl.org, Marcus Wolf , linux-kernel@vger.kernel.org, Dan Carpenter , kernelnewbies@kernelnewbies.org Subject: [PATCH v4] staging: pi433: fix race condition in pi433_ioctl Message-ID: <20180614010438.GB1974@hle-laptop.local> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="DBIVS5p969aUjpLe" 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 --DBIVS5p969aUjpLe Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 and simpler code. Signed-off-by: Hugo Lefeuvre --- Changes in v4: - Fix incorrect buffer name in memcpy. --- drivers/staging/pi433/pi433_if.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433= _if.c index b061f77dda41..94e0bfcec991 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, unsign= ed long arg) int retval =3D 0; struct pi433_instance *instance; struct pi433_device *device; + struct pi433_tx_cfg tx_cfg; void __user *argp =3D (void __user *)arg; =20 /* Check type and command number */ @@ -902,9 +903,11 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsig= ned long arg) return -EFAULT; break; case PI433_IOC_WR_TX_CFG: - if (copy_from_user(&instance->tx_cfg, argp, - sizeof(struct pi433_tx_cfg))) + if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg))) return -EFAULT; + mutex_lock(&device->tx_fifo_lock); + memcpy(&instance->tx_cfg, &tx_cfg, 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, --=20 2.17.1 --DBIVS5p969aUjpLe Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE5LpPtQuYJzvmooL3LVy48vb3khkFAlshvyAACgkQLVy48vb3 khn+qQf7BEMeWwZ5N10oascSjuRCuzDKWVMMOMq2nbQcOPPstaZXYC3oU/Ya1/0o QuwXPSFVLLqeX0u5NmgTnCpeHBI7ANNywxogoB7MnAIw6mbUE8aePNRc7vhvLgyH AC3D0pLNIqmLFHjav68PVER5i4IPHWeWBTuKiWbYMQ1JI6dE0YKjfFavk2uG2/zu JRi427DtK/XtgqCTapcBx5YHsIazlmztFIRuO2P8J/0Z+LuRcaEsQjBZmWNTDu9f MdlDxkrNL5lZJLmWpiX4QewgGLZQ+KER+0zTFlhcbjMp4+nkcUsTBAxtimI3IGp3 gckReFpEmWXT3tkT09JBJyymqIutnA== =hkYt -----END PGP SIGNATURE----- --DBIVS5p969aUjpLe--