All of lore.kernel.org
 help / color / mirror / Atom feed
From: Demi Marie Obenour <demi@invisiblethingslab.com>
To: Alasdair Kergon <agk@redhat.com>,
	Mike Snitzer <snitzer@kernel.org>,
	dm-devel@redhat.com
Cc: Demi Marie Obenour <demi@invisiblethingslab.com>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 4/6] device-mapper: Avoid double-fetch of version
Date: Thu,  1 Jun 2023 17:24:53 -0400	[thread overview]
Message-ID: <20230601212456.1533-5-demi@invisiblethingslab.com> (raw)
In-Reply-To: <20230601212456.1533-1-demi@invisiblethingslab.com>

The version is fetched once in check_version(), which then does some
validation and then overwrites the version in userspace with the API
version supported by the kernel.  copy_params() then fetches the version
from userspace *again*, and this time no validation is done.  The result
is that the kernel's version number is completely controllable by
userspace, provided that userspace can win a race condition.

Fix this flaw by not copying the version back to the kernel the second
time.  This is not exploitable as the version is not further used in the
kernel.  However, it could become a problem if future patches start
relying on the version field.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Demi Marie Obenour <demi@invisiblethingslab.com>
---
 drivers/md/dm-ioctl.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
index da6ca26b51d0953df380582bb3a51c2ec22c27cb..fd46b249f6f856c49752063fc49d720e95df0525 100644
--- a/drivers/md/dm-ioctl.c
+++ b/drivers/md/dm-ioctl.c
@@ -1873,12 +1873,13 @@ static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
  * As well as checking the version compatibility this always
  * copies the kernel interface version out.
  */
-static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
+static int check_version(unsigned int cmd, struct dm_ioctl __user *user,
+			 struct dm_ioctl *kernel_params)
 {
-	uint32_t version[3];
 	int r = 0;
+	uint32_t *version = kernel_params->version;
 
-	if (copy_from_user(version, user->version, sizeof(version)))
+	if (copy_from_user(version, user->version, sizeof(user->version)))
 		return -EFAULT;
 
 	if ((version[0] != DM_VERSION_MAJOR) ||
@@ -1922,7 +1923,10 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kern
 	const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
 	unsigned int noio_flag;
 
-	if (copy_from_user(param_kernel, user, minimum_data_size))
+	/* Version has been copied from userspace already, avoid TOCTOU */
+	if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version),
+			   (char __user *)user + sizeof(param_kernel->version),
+			   minimum_data_size - sizeof(param_kernel->version)))
 		return -EFAULT;
 
 	if (param_kernel->data_size < minimum_data_size) {
@@ -2034,7 +2038,7 @@ static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us
 	 * Check the interface version passed in.  This also
 	 * writes out the kernel's interface version.
 	 */
-	r = check_version(cmd, user);
+	r = check_version(cmd, user, &param_kernel);
 	if (r)
 		return r;
 
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab


WARNING: multiple messages have this Message-ID (diff)
From: Demi Marie Obenour <demi@invisiblethingslab.com>
To: Alasdair Kergon <agk@redhat.com>,
	Mike Snitzer <snitzer@kernel.org>,
	dm-devel@redhat.com
Cc: Demi Marie Obenour <demi@invisiblethingslab.com>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [dm-devel] [PATCH 4/6] device-mapper: Avoid double-fetch of version
Date: Thu,  1 Jun 2023 17:24:53 -0400	[thread overview]
Message-ID: <20230601212456.1533-5-demi@invisiblethingslab.com> (raw)
In-Reply-To: <20230601212456.1533-1-demi@invisiblethingslab.com>

The version is fetched once in check_version(), which then does some
validation and then overwrites the version in userspace with the API
version supported by the kernel.  copy_params() then fetches the version
from userspace *again*, and this time no validation is done.  The result
is that the kernel's version number is completely controllable by
userspace, provided that userspace can win a race condition.

Fix this flaw by not copying the version back to the kernel the second
time.  This is not exploitable as the version is not further used in the
kernel.  However, it could become a problem if future patches start
relying on the version field.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Demi Marie Obenour <demi@invisiblethingslab.com>
---
 drivers/md/dm-ioctl.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c
index da6ca26b51d0953df380582bb3a51c2ec22c27cb..fd46b249f6f856c49752063fc49d720e95df0525 100644
--- a/drivers/md/dm-ioctl.c
+++ b/drivers/md/dm-ioctl.c
@@ -1873,12 +1873,13 @@ static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
  * As well as checking the version compatibility this always
  * copies the kernel interface version out.
  */
-static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
+static int check_version(unsigned int cmd, struct dm_ioctl __user *user,
+			 struct dm_ioctl *kernel_params)
 {
-	uint32_t version[3];
 	int r = 0;
+	uint32_t *version = kernel_params->version;
 
-	if (copy_from_user(version, user->version, sizeof(version)))
+	if (copy_from_user(version, user->version, sizeof(user->version)))
 		return -EFAULT;
 
 	if ((version[0] != DM_VERSION_MAJOR) ||
@@ -1922,7 +1923,10 @@ static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kern
 	const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
 	unsigned int noio_flag;
 
-	if (copy_from_user(param_kernel, user, minimum_data_size))
+	/* Version has been copied from userspace already, avoid TOCTOU */
+	if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version),
+			   (char __user *)user + sizeof(param_kernel->version),
+			   minimum_data_size - sizeof(param_kernel->version)))
 		return -EFAULT;
 
 	if (param_kernel->data_size < minimum_data_size) {
@@ -2034,7 +2038,7 @@ static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *us
 	 * Check the interface version passed in.  This also
 	 * writes out the kernel's interface version.
 	 */
-	r = check_version(cmd, user);
+	r = check_version(cmd, user, &param_kernel);
 	if (r)
 		return r;
 
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


  parent reply	other threads:[~2023-06-01 21:25 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-01 21:24 [PATCH 0/6] Several device-mapper fixes Demi Marie Obenour
2023-06-01 21:24 ` [dm-devel] " Demi Marie Obenour
2023-06-01 21:24 ` [PATCH 1/6] device-mapper: Check that target specs are sufficiently aligned Demi Marie Obenour
2023-06-01 21:24   ` [dm-devel] " Demi Marie Obenour
2023-06-01 21:24 ` [PATCH 2/6] device-mapper: Avoid pointer arithmetic overflow Demi Marie Obenour
2023-06-01 21:24   ` [dm-devel] " Demi Marie Obenour
2023-06-01 21:24 ` [PATCH 3/6] device-mapper: structs and parameter strings must not overlap Demi Marie Obenour
2023-06-01 21:24   ` [dm-devel] " Demi Marie Obenour
2023-06-01 21:24 ` Demi Marie Obenour [this message]
2023-06-01 21:24   ` [dm-devel] [PATCH 4/6] device-mapper: Avoid double-fetch of version Demi Marie Obenour
2023-06-03  7:40   ` kernel test robot
2023-06-03  7:40     ` [dm-devel] " kernel test robot
2023-06-03 14:21     ` Demi Marie Obenour
2023-06-03 14:21       ` [dm-devel] " Demi Marie Obenour
2023-06-01 21:24 ` [PATCH 5/6] device-mapper: Refuse to create device named "control" Demi Marie Obenour
2023-06-01 21:24   ` [dm-devel] " Demi Marie Obenour
2023-06-01 21:24 ` [PATCH 6/6] device-mapper: "." and ".." are not valid symlink names Demi Marie Obenour
2023-06-01 21:24   ` [dm-devel] " Demi Marie Obenour
2023-06-03 14:52 ` [PATCH v2 0/6] Several device-mapper fixes Demi Marie Obenour
2023-06-03 14:52   ` [dm-devel] " Demi Marie Obenour
2023-06-03 14:52   ` [PATCH v2 1/6] device-mapper: Check that target specs are sufficiently aligned Demi Marie Obenour
2023-06-03 14:52     ` [dm-devel] " Demi Marie Obenour
2023-06-22 16:28     ` Mike Snitzer
2023-06-22 16:28       ` [dm-devel] " Mike Snitzer
2023-06-22 19:51       ` Demi Marie Obenour
2023-06-22 19:51         ` [dm-devel] " Demi Marie Obenour
2023-06-22 22:54         ` Mike Snitzer
2023-06-22 22:54           ` Mike Snitzer
2023-06-22 17:29     ` [dm-devel] " Mikulas Patocka
2023-06-22 17:29       ` Mikulas Patocka
2023-06-22 20:27       ` Demi Marie Obenour
2023-06-22 20:27         ` Demi Marie Obenour
2023-06-03 14:52   ` [PATCH v2 2/6] device-mapper: Avoid pointer arithmetic overflow Demi Marie Obenour
2023-06-03 14:52     ` [dm-devel] " Demi Marie Obenour
2023-06-22 17:30     ` Mikulas Patocka
2023-06-22 17:30       ` Mikulas Patocka
2023-06-22 22:50     ` Mike Snitzer
2023-06-22 22:50       ` [dm-devel] " Mike Snitzer
2023-06-03 14:52   ` [PATCH v2 3/6] device-mapper: structs and parameter strings must not overlap Demi Marie Obenour
2023-06-03 14:52     ` [dm-devel] " Demi Marie Obenour
2023-06-22 17:31     ` Mikulas Patocka
2023-06-22 17:31       ` Mikulas Patocka
2023-06-03 14:52   ` [PATCH v2 4/6] device-mapper: Avoid double-fetch of version Demi Marie Obenour
2023-06-03 14:52     ` [dm-devel] " Demi Marie Obenour
2023-06-22 16:20     ` Mike Snitzer
2023-06-22 16:20       ` [dm-devel] " Mike Snitzer
2023-06-22 18:43       ` Demi Marie Obenour
2023-06-22 18:43         ` [dm-devel] " Demi Marie Obenour
2023-06-03 14:52   ` [PATCH v2 5/6] device-mapper: Refuse to create device named "control" Demi Marie Obenour
2023-06-03 14:52     ` [dm-devel] " Demi Marie Obenour
2023-06-03 14:52   ` [PATCH v2 6/6] device-mapper: "." and ".." are not valid symlink names Demi Marie Obenour
2023-06-03 14:52     ` [dm-devel] " Demi Marie Obenour

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230601212456.1533-5-demi@invisiblethingslab.com \
    --to=demi@invisiblethingslab.com \
    --cc=agk@redhat.com \
    --cc=dm-devel@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=snitzer@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.