Skip to contentSkip to conversation

vhost/net: bound the count of VHOST_SET_FEATURES_ARRAY

PATCH In review Hengyu Liang 3 participants 5 messages Nov 22

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost/net: bound the count of VHOST_SET_FEATURES_ARRAY

Commit 333c515d1896 ("vhost-net: allow configuring extended features") added VHOST_SET_FEATURES_ARRAY. The ioctl reads the number of feature words from user space and checks that every word above the supported ones is zero, one word at a time in a loop.

However, the loop has no upper bound and no scheduling point. As of now, a caller can pass a count of U64_MAX and a pointer into a large zero-filled mapping. The ioctl then stays in the kernel for as long as the mapping is readable, and a fatal signal is not handled until it ends. On a v6.18 debug kernel booted with preempt=none, two such calls from a non-root user, each on a 4 GiB mapping, froze a 2-CPU VM for 56 s with soft lockup reports.

Interfaces that take an extensible structure from user space limit it to one page (openat2, clone3, sched_setattr).

On a KASAN kernel with a 1 GiB mapping: before commit 333c515d1896 ("vhost-net: allow configuring extended features"), the ioctl does not exist and the call fails with EPERM at once. After that commit, it fails with EFAULT after 26.0 s. With this patch, it fails with E2BIG at once. A reproducer is available on request.

This patch will reject counts above one page of words (512 on a 4 KiB page, or 32768 feature bits; 128 are defined today) before the walk starts.

Fixes 333c515d1896 ("vhost-net: allow configuring extended features")
Cc [email protected] # v6.17+

---
I found this while comparing the behavior of different kernel versions. I used an LLM to analyze the problem and to draft the patch and this message, and I analyzed the result myself.

Tested on the vhost tree at 44293796526f with KASAN and lockdep: counts of 0, 2, 3 and 512 words still succeed, a nonzero word above the supported ones still gives EOPNOTSUPP, and VHOST_GET_FEATURES_ARRAY is unchanged.

 drivers/vhost/net.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/vhost/net.c +2 -0
index 631855064b3d..22e06b44d2ed 100644
@@ -1795,6 +1795,8 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
 	case VHOST_SET_FEATURES_ARRAY:
 		if (copy_from_user(&count, featurep, sizeof(count)))
 			return -EFAULT;
+		if (count > PAGE_SIZE / sizeof(u64))
+			return -E2BIG;
 
 		virtio_features_zero(all_features);
 		argp += sizeof(u64);

base-commit: 44293796526f4343639f87b2acc53811c78b5d54

Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 22 lines

Commit 333c515d1896 ("vhost-net: allow configuring extended features") added VHOST_SET_FEATURES_ARRAY. The ioctl reads the number of feature words from user space and checks that every word above the supported ones is zero, one word at a time in a loop.

However, the loop has no upper bound and no scheduling point. As of now, a caller can pass a count of U64_MAX and a pointer into a large zero-filled mapping. The ioctl then stays in the kernel for as long as the mapping is readable, and a fatal signal is not handled until it ends. On a v6.18 debug kernel booted with preempt=none, two such calls from a non-root user, each on a 4 GiB mapping, froze a 2-CPU VM for 56 s with soft lockup reports.

Interfaces that take an extensible structure from user space limit it to one page (openat2, clone3, sched_setattr).

On a KASAN kernel with a 1 GiB mapping: before commit 333c515d1896 ("vhost-net: allow configuring extended features"), the ioctl does not exist and the call fails with EPERM at once. After that commit, it fails with EFAULT after 26.0 s. With this patch, it fails with E2BIG at once. A reproducer is available on request.

Quoted 7 lines

This patch will reject counts above one page of words (512 on a 4 KiB page, or 32768 feature bits; 128 are defined today) before the walk starts.

Fixes 333c515d1896 ("vhost-net: allow configuring extended features")
Cc [email protected] # v6.17+

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Hengyu Liang
Quoted 46 lines · Hengyu Liang

Commit 333c515d1896 ("vhost-net: allow configuring extended features") added VHOST_SET_FEATURES_ARRAY. The ioctl reads the number of feature words from user space and checks that every word above the supported ones is zero, one word at a time in a loop.

However, the loop has no upper bound and no scheduling point. As of now, a caller can pass a count of U64_MAX and a pointer into a large zero-filled mapping. The ioctl then stays in the kernel for as long as the mapping is readable, and a fatal signal is not handled until it ends. On a v6.18 debug kernel booted with preempt=none, two such calls from a non-root user, each on a 4 GiB mapping, froze a 2-CPU VM for 56 s with soft lockup reports.

Interfaces that take an extensible structure from user space limit it to one page (openat2, clone3, sched_setattr).

On a KASAN kernel with a 1 GiB mapping: before commit 333c515d1896 ("vhost-net: allow configuring extended features"), the ioctl does not exist and the call fails with EPERM at once. After that commit, it fails with EFAULT after 26.0 s. With this patch, it fails with E2BIG at once. A reproducer is available on request.

This patch will reject counts above one page of words (512 on a 4 KiB page, or 32768 feature bits; 128 are defined today) before the walk starts.

Fixes 333c515d1896 ("vhost-net: allow configuring extended features")
Cc [email protected] # v6.17+

---
I found this while comparing the behavior of different kernel versions. I used an LLM to analyze the problem and to draft the patch and this message, and I analyzed the result myself.

Tested on the vhost tree at 44293796526f with KASAN and lockdep: counts of 0, 2, 3 and 512 words still succeed, a nonzero word above the supported ones still gives EOPNOTSUPP, and VHOST_GET_FEATURES_ARRAY is unchanged.

 drivers/vhost/net.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/vhost/net.c +0 -0
index 631855064b3d..22e06b44d2ed 100644

drivers/vhost/net.c static long vhost_net_ioctl(struct file *f, unsigned int ioctl, · line 1802

Quoted 8 lines
drivers/vhost/net.c +2 -0
@@ -1795,6 +1795,8 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
 	case VHOST_SET_FEATURES_ARRAY:
 		if (copy_from_user(&count, featurep, sizeof(count)))
 			return -EFAULT;
+		if (count > PAGE_SIZE / sizeof(u64))
+			return -E2BIG;
 
 		virtio_features_zero(all_features);
 		argp += sizeof(u64);

????
this is followed by:

                virtio_features_zero(all_features);
                argp += sizeof(u64);
                copied = min(count, (u64)VIRTIO_FEATURES_U64S);
                if (copy_from_user(all_features, argp, copied * sizeof(u64)))
                        return -EFAULT;

copies at most 8 bytes.

did you test that this patch actually helps anything?

Quoted 3 lines

base-commit: 44293796526f4343639f87b2acc53811c78b5d54

Signature · 1 line
2.53.0
#3 lore raw
Quoted 8 lines · Michael S. Tsirkin

this is followed by:

                virtio_features_zero(all_features);
                argp += sizeof(u64);
                copied = min(count, (u64)VIRTIO_FEATURES_U64S);
                if (copy_from_user(all_features, argp, copied * sizeof(u64)))
                        return -EFAULT;
Quoted 4 lines

copies at most 8 bytes.

did you test that this patch actually helps anything?

That first copy is bounded, yes. The unbounded part is the loop right after it, which is outside the diff context:

		for (i = copied; i < count; ++i) {
			if (copy_from_user(&features, featurep + 1 + i,
					   sizeof(features)))
				return -EFAULT;
			if (features)
				return -EOPNOTSUPP;
		}

It reads one u64 per iteration up to the count from user space. With count = U64_MAX and a large zero-filled mapping, it only stops at the first unreadable address.

Yes, I tested it, on the vhost tree at 44293796526f (KASAN + lockdep), count = U64_MAX:

1 GiB zero-filled mapping:
without the patch: EFAULT after 27.4 s
with the patch: E2BIG at once

2 GiB mapping, SIGKILL sent 1 s into the ioctl:
without the patch: the process is reaped 46.7 s after the kill
with the patch: the ioctl has already returned

I have a small reproducer (a few lines of C). Since I found this with the help of an LLM, Documentation/process/security-bugs.rst asks me to treat it as public and not post the reproducer to a public list, so I have not included it here. I am happy to send it to you off-list if it would help.

Thanks,
Hengyu

#4 lore raw
Quoted 17 lines · Hengyu Liang
Quoted 8 lines · Michael S. Tsirkin

this is followed by:

                virtio_features_zero(all_features);
                argp += sizeof(u64);
                copied = min(count, (u64)VIRTIO_FEATURES_U64S);
                if (copy_from_user(all_features, argp, copied * sizeof(u64)))
                        return -EFAULT;
Quoted 4 lines

copies at most 8 bytes.

did you test that this patch actually helps anything?

That first copy is bounded, yes. The unbounded part is the loop right after it, which is outside the diff context:

Quoted 7 lines
		for (i = copied; i < count; ++i) {
			if (copy_from_user(&features, featurep + 1 + i,
					   sizeof(features)))
				return -EFAULT;
			if (features)
				return -EOPNOTSUPP;
		}

I see. So all it is doing is checking that these features are > 0 and erroring out.
Let's just not do it - we don't have to.

Quoted 23 lines

It reads one u64 per iteration up to the count from user space. With count = U64_MAX and a large zero-filled mapping, it only stops at the first unreadable address.

Yes, I tested it, on the vhost tree at 44293796526f (KASAN + lockdep), count = U64_MAX:

1 GiB zero-filled mapping:
without the patch: EFAULT after 27.4 s
with the patch: E2BIG at once

2 GiB mapping, SIGKILL sent 1 s into the ioctl:
without the patch: the process is reaped 46.7 s after the kill
with the patch: the ioctl has already returned

I have a small reproducer (a few lines of C). Since I found this with the help of an LLM, Documentation/process/security-bugs.rst asks me to treat it as public and not post the reproducer to a public list, so I have not included it here. I am happy to send it to you off-list if it would help.

Thanks,
Hengyu