Skip to contentSkip to conversation

vsock: preserve child namespace mode on an empty write

PATCH New Aldo Ariel Panzardo 7 participants 14 messages Sep 15 – Sep 18

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

PATCH 1/1 vsock: preserve child namespace mode on an empty write

__vsock_net_mode_string() returns success without updating new_mode when the write length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Initialize new_mode from the current child mode so that an empty write is a no-op.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")
---
 net/vmw_vsock/af_vsock.c | 1 +
 1 file changed, 1 insertion(+)
net/vmw_vsock/af_vsock.c +1 -0
index 622dbd0467..61163357ae 100644
@@ -2878,6 +2878,7 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 	int ret;
 
 	net = container_of(table->data, struct net, vsock.child_ns_mode);
+	new_mode = vsock_net_child_mode(net);
 
 	ret = __vsock_net_mode_string(table, write, buffer, lenp, ppos,
 				      vsock_net_child_mode(net), &new_mode);
Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

__vsock_net_mode_string() returns success without updating new_mode when the write length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Initialize new_mode from the current child mode so that an empty write is a no-op.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

PATCH v2 1/1 vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before inspecting or storing new_mode when no bytes were transferred. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")
---
 net/vmw_vsock/af_vsock.c | 2 ++
 1 file changed, 2 insertions(+)
net/vmw_vsock/af_vsock.c +2 -0
index 622dbd0467..816d25b314 100644
@@ -2883,6 +2883,8 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 				      vsock_net_child_mode(net), &new_mode);
 	if (ret)
 		return ret;
+	if (!*lenp)
+		return 0;
 
 	if (write) {
 		/* Prevent a "local" namespace from escalating to "global",
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v2] vsock: ignore empty child namespace mode writes

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Quoted 7 lines

Return before inspecting or storing new_mode when no bytes were transferred. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo

Hi Aldo,

Quoted 32 lines · Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before inspecting or storing new_mode when no bytes were transferred. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

---
net/vmw_vsock/af_vsock.c | 2 ++

1 file changed, 2 insertions(+)
net/vmw_vsock/af_vsock.c +2 -0
index 622dbd0467..816d25b314 100644
@@ -2883,6 +2883,8 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 				      vsock_net_child_mode(net), &new_mode);
 	if (ret)
 		return ret;
+	if (!*lenp)
+		return 0;
 
 	if (write) {
 		/* Prevent a "local" namespace from escalating to "global",
Signature · 1 line
2.43.0

Code LGTM.

To reproduce it, I suppose we need a kernel that does not come with CONFIG_INIT_STACK_ALL_ZERO set.

@Stefano do you think it's worth adding a test for this case?

Just out of curiosity, how did you find this bug?

Thanks,
Luigi

#5 lore raw

Re: [PATCH v2] vsock: ignore empty child namespace mode writes

Quoted 37 lines · Luigi Leonardi

Hi Aldo,

Quoted 32 lines · Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before inspecting or storing new_mode when no bytes were transferred. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

---
net/vmw_vsock/af_vsock.c | 2 ++

1 file changed, 2 insertions(+)
net/vmw_vsock/af_vsock.c +2 -0
index 622dbd0467..816d25b314 100644
@@ -2883,6 +2883,8 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 				      vsock_net_child_mode(net), &new_mode);
 	if (ret)
 		return ret;
+	if (!*lenp)
+		return 0;
 
 	if (write) {
 		/* Prevent a "local" namespace from escalating to "global",
Signature · 1 line
2.43.0

Code LGTM.

Quoted 6 lines

To reproduce it, I suppose we need a kernel that does not come with CONFIG_INIT_STACK_ALL_ZERO set.

@Stefano do you think it's worth adding a test for this case?

Not a strong opinion on this on my side.

Thanks,
Stefano

Quoted 6 lines

Just out of curiosity, how did you find this bug?

Thanks,
Luigi

#6 lore raw

Re: [PATCH v2] vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo

CCing Bobby (and I guess you're missing several maintainers in CC)

Quoted 9 lines · Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before inspecting or storing new_mode when no bytes were transferred. This also prevents an empty write from locking the current mode.

Quoted 6 lines
Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

---
net/vmw_vsock/af_vsock.c | 2 ++

1 file changed, 2 insertions(+)

A changelog after --- or a reply to v1 to explain why sending a v2, would be nice to have.

net/vmw_vsock/af_vsock.c static int vsock_net_child_mode_string(const struct ctl_table *table, int write, · line 2887

Quoted 11 lines
net/vmw_vsock/af_vsock.c +2 -0
index 622dbd0467..816d25b314 100644
@@ -2883,6 +2883,8 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 				      vsock_net_child_mode(net), &new_mode);
 	if (ret)
 		return ret;
+	if (!*lenp)
+		return 0;

Better to move this before calling __vsock_net_mode_string(), no?

That said, __vsock_net_mode_string() needs some cleanup IMO but I guess something for net-next.

Thanks,
Stefano

Quoted 6 lines
net/vmw_vsock/af_vsock.c +0 -0
 	if (write) {
 		/* Prevent a "local" namespace from escalating to "global",
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2] vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo

Hi Luigi, Stefano,

Thanks for taking a look.

Luigi — nothing fancy, I was just reading sysctl handlers and noticed that __vsock_net_mode_string() bails early on a zero-length write without touching new_mode. Since the child mode can only be set once, an empty write that sneaks through locks it with whatever was on the stack. CONFIG_INIT_STACK_ALL_ZERO hides the whole thing, which is probably why nobody hit it in practice.

Stefano — yeah, makes more sense to bail before calling the helper at all. Fixed in v3, and I added the CCs I missed.

Cheers,
Aldo

#8 lore raw

PATCH v3 1/1 vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before calling __vsock_net_mode_string() when *lenp is zero so that the helper is never invoked with nothing to parse and new_mode is never read uninitialized. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

---

Changes in v3:
- Move the !*lenp early return before __vsock_net_mode_string() instead of after it, as suggested by Stefano Garzarella.
- Add missing maintainer CCs per get_maintainer.pl.
- Add Luigi's Reviewed-by.

Changes in v2:
- Return early instead of initializing new_mode to the current value, which would silently consume the write-once transition.

 net/vmw_vsock/af_vsock.c | 3 +++
 1 file changed, 3 insertions(+)
net/vmw_vsock/af_vsock.c +3 -0
index 622dbd0467..XXXXXXX 100644
@@ -2879,6 +2879,9 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 
 	net = container_of(table->data, struct net, vsock.child_ns_mode);
 
+	if (!*lenp)
+		return 0;
+
 	ret = __vsock_net_mode_string(table, write, buffer, lenp, ppos,
 				      vsock_net_child_mode(net), &new_mode);
 	if (ret)

--
2.43.0

#9 lore raw

Re: [PATCH v3] vsock: ignore empty child namespace mode writes

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before calling __vsock_net_mode_string() when *lenp is zero so that the helper is never invoked with nothing to parse and new_mode is never read uninitialized. This also prevents an empty write from locking the current mode.

Quoted 4 lines
Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v3 1/1
#10 lore raw

Re: [PATCH v3] vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo
Quoted 34 lines · Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before calling __vsock_net_mode_string() when *lenp is zero so that the helper is never invoked with nothing to parse and new_mode is never read uninitialized. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

---

Changes in v3:
- Move the !*lenp early return before __vsock_net_mode_string() instead of after it, as suggested by Stefano Garzarella.
- Add missing maintainer CCs per get_maintainer.pl.
- Add Luigi's Reviewed-by.

Changes in v2:
- Return early instead of initializing new_mode to the current value, which would silently consume the write-once transition.

net/vmw_vsock/af_vsock.c | 3 +++

1 file changed, 3 insertions(+)
net/vmw_vsock/af_vsock.c +0 -0
index 622dbd0467..XXXXXXX 100644

net/vmw_vsock/af_vsock.c static int vsock_net_child_mode_string(const struct ctl_table *table, int write, · line 2884

Quoted 6 lines
net/vmw_vsock/af_vsock.c +3 -0
@@ -2879,6 +2879,9 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 
 	net = container_of(table->data, struct net, vsock.child_ns_mode);
 
+	if (!*lenp)
+		return 0;
+

I would have moved it even before the net assignmet, but in any case, it doesn't make much difference:

Reviewed-by on v3 1/1
#11 lore raw

Re: [PATCH v3] vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo
Quoted 45 lines · Aldo Ariel Panzardo

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before calling __vsock_net_mode_string() when *lenp is zero so that the helper is never invoked with nothing to parse and new_mode is never read uninitialized. This also prevents an empty write from locking the current mode.

Fixes eafb64f40ca4 ("vsock: add netns to vsock core")

---

Changes in v3:
- Move the !*lenp early return before __vsock_net_mode_string() instead of after it, as suggested by Stefano Garzarella.
- Add missing maintainer CCs per get_maintainer.pl.
- Add Luigi's Reviewed-by.

Changes in v2:
- Return early instead of initializing new_mode to the current value, which would silently consume the write-once transition.

 net/vmw_vsock/af_vsock.c | 3 +++
 1 file changed, 3 insertions(+)
net/vmw_vsock/af_vsock.c +3 -1
index 622dbd0467..XXXXXXX 100644
@@ -2879,6 +2879,9 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 
 	net = container_of(table->data, struct net, vsock.child_ns_mode);
 
+	if (!*lenp)
+		return 0;
+
 	ret = __vsock_net_mode_string(table, write, buffer, lenp, ppos,
 				      vsock_net_child_mode(net), &new_mode);
 	if (ret)
 
--
Quoted 1 line

2.43.0

#12 lore raw

Re: [PATCH v3] vsock: ignore empty child namespace mode writes

In reply to Aldo Ariel Panzardo
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Tue, 15 Sep 2026 14:30:50 -0300 you wrote:

__vsock_net_mode_string() returns success without updating new_mode when the transfer length is zero. Its caller then reads the uninitialized enum and may permanently store a stack-derived value in the write-once child mode.

Return before calling __vsock_net_mode_string() when *lenp is zero so that the helper is never invoked with nothing to parse and new_mode is never read uninitialized. This also prevents an empty write from locking the current mode.

[...]

Here is the summary with links:
- [v3] vsock: ignore empty child namespace mode writes

    https://git.kernel.org/netdev/net/c/2ec28c09b320

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#13 lore raw
In reply to Aldo Ariel Panzardo
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Critical: 0 · High: 1 · Medium: 0 (1) · Low: 0

- [High] Incomplete fix: a zero-length write to /proc/sys/net/vsock/child_ns_mode still consumes the write-once child-…

Pre-existing issues:
- [Medium] Non-atomic publication in vsock_net_set_child_mode() (include/net/af_vsock.h:289-302): child_ns_mode_locked is published by…

--

vsock: preserve child namespace mode on an empty write

This initializes new_mode in vsock_net_child_mode_string() from the current child mode, so the caller no longer reads an uninitialized enum when __vsock_net_mode_string() returns success for a zero-length write.

net/vmw_vsock/af_vsock.c static int vsock_net_child_mode_string(const struct ctl_table *table, int write, · line 2894

Quoted 12 lines
net/vmw_vsock/af_vsock.c +1 -0
index f840498b58afb..9f89f199478b9 100644
@@ -2888,6 +2888,7 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
 	int ret;
 
 	net = container_of(table->data, struct net, vsock.child_ns_mode);
+	new_mode = vsock_net_child_mode(net);
 
 	ret = __vsock_net_mode_string(table, write, buffer, lenp, ppos,
 				      vsock_net_child_mode(net), &new_mode);

[Severity: High]
The changelog says "Initialize new_mode from the current child mode so that an empty write is a no-op." Is the empty write really a no-op after this change?

A zero-length write still reaches the handler with write == 1 and *lenp == 0. fs/read_write.c:vfs_write() has no count == 0 early return (rw_verify_area() only rejects negative counts), and fs/proc/proc_sysctl.c:proc_sys_call_handler() does not short-circuit either:

	if (write) {
		error = -EFAULT;
		if (!copy_from_iter_full(kbuf, count, iter))
			goto out_free_buf;
		kbuf[count] = '\0';
	}
	...
	error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);

copy_from_iter_full(kbuf, 0, iter) returns true, so vsock_net_child_mode_string() is entered with *lenp == 0.

__vsock_net_mode_string() then returns 0 without parsing anything:

	if (!table->data || !table->maxlen || !*lenp) {
		*lenp = 0;
		return 0;
	}

Since that return value is indistinguishable from "a mode was parsed", vsock_net_child_mode_string() falls into its write block anyway:

	if (write) {
		...
		if (!vsock_net_set_child_mode(net, new_mode))
			return -EBUSY;
	}

so vsock_net_set_child_mode() is still called, now with the seeded current mode rather than a stack value.

Does that still consume the write-once latch? In include/net/af_vsock.h:vsock_net_set_child_mode():

	int new_locked = mode + 1;
	int old_locked = 0; /* unlocked */

	if (try_cmpxchg(&net->vsock.child_ns_mode_locked,
			&old_locked, new_locked)) {
		WRITE_ONCE(net->vsock.child_ns_mode, mode);
		return true;
	}

On a fresh namespace vsock_net_init() leaves child_ns_mode_locked at 0, so the cmpxchg succeeds and the mode is latched permanently.

Would this sequence then break a later legitimate lockdown on a global-mode namespace?

write(fd, "", 0); /* returns 0, latches GLOBAL+1 */
echo local > .../child_ns_mode /* cmpxchg fails, -EBUSY */

Documentation/admin-guide/sysctl/net.rst describes the behaviour as:

The first write to ``child_ns_mode`` locks its value. Subsequent writes of the same value succeed, but writing a different value returns ``-EBUSY``.

which ties the lock and the -EBUSY to writing a mode value, not to a write that supplies no value.

Would it be more in line with the changelog to skip the write handling entirely when nothing was parsed, for example an early
"if (write && !*lenp) return 0;" in vsock_net_child_mode_string(), or having __vsock_net_mode_string() report "no value parsed" distinctly from success, instead of re-applying the current mode through the cmpxchg?

[Severity: Medium]
This is a pre-existing issue and not introduced by this patch, but while looking at vsock_net_set_child_mode() in include/net/af_vsock.h: is the publication of the lock and of the mode value atomic with respect to namespace creation?

	if (try_cmpxchg(&net->vsock.child_ns_mode_locked,
			&old_locked, new_locked)) {
		WRITE_ONCE(net->vsock.child_ns_mode, mode);
		return true;
	}

	return old_locked == new_locked;

The lock becomes observable before the child_ns_mode store, and a second writer of the same mode returns success from the last line without waiting for that store:

CPU0 (writes "local" on a global-mode netns)

	try_cmpxchg() succeeds, child_ns_mode_locked = LOCAL+1
	/* preempted here, before WRITE_ONCE(child_ns_mode, LOCAL) */

CPU1 (writes "local")

	try_cmpxchg() fails, old_locked == LOCAL+1 == new_locked
	return true;			/* sysctl write reports success */

CPU2 (creates a netns after CPU1's write returned) net/vmw_vsock/af_vsock.c:vsock_net_init()

	net->vsock.mode = vsock_net_child_mode(current->nsproxy->net_ns);
	net->vsock.child_ns_mode = net->vsock.mode;

Can the new namespace read VSOCK_NET_MODE_GLOBAL here and permanently inherit the more permissive mode even though userspace was told the "local" lockdown was committed?

A barrier does not seem sufficient, since the reader has already observed the lock publication that precedes the value store. Would publishing the mode and its locked state together help, for example deriving child_ns_mode from child_ns_mode_locked, or adding an in-progress state that the second writer waits on?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260915111417.2726515-1-qwe.aldo%40gmail.com