Skip to content

Commit 102eab9

Browse files
beshlemanPaolo Abeni
authored andcommitted
vsock: lock down child_ns_mode as write-once
Two administrator processes may race when setting child_ns_mode as one process sets child_ns_mode to "local" and then creates a namespace, but another process changes child_ns_mode to "global" between the write and the namespace creation. The first process ends up with a namespace in "global" mode instead of "local". While this can be detected after the fact by reading ns_mode and retrying, it is fragile and error-prone. Make child_ns_mode write-once so that a namespace manager can set it once and be sure it won't change. Writing a different value after the first write returns -EBUSY. This applies to all namespaces, including init_net, where an init process can write "local" to lock all future namespaces into local mode. Fixes: eafb64f ("vsock: add netns to vsock core") Suggested-by: Daan De Meyer <[email protected]> Suggested-by: Stefano Garzarella <[email protected]> Co-developed-by: Stefano Garzarella <[email protected]> Signed-off-by: Stefano Garzarella <[email protected]> Signed-off-by: Bobby Eshleman <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent a382a34 commit 102eab9

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

include/net/af_vsock.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,19 @@ static inline bool vsock_net_mode_global(struct vsock_sock *vsk)
276276
return vsock_net_mode(sock_net(sk_vsock(vsk))) == VSOCK_NET_MODE_GLOBAL;
277277
}
278278

279-
static inline void vsock_net_set_child_mode(struct net *net,
279+
static inline bool vsock_net_set_child_mode(struct net *net,
280280
enum vsock_net_mode mode)
281281
{
282-
WRITE_ONCE(net->vsock.child_ns_mode, mode);
282+
int new_locked = mode + 1;
283+
int old_locked = 0; /* unlocked */
284+
285+
if (try_cmpxchg(&net->vsock.child_ns_mode_locked,
286+
&old_locked, new_locked)) {
287+
WRITE_ONCE(net->vsock.child_ns_mode, mode);
288+
return true;
289+
}
290+
291+
return old_locked == new_locked;
283292
}
284293

285294
static inline enum vsock_net_mode vsock_net_child_mode(struct net *net)

include/net/netns/vsock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ struct netns_vsock {
1717

1818
enum vsock_net_mode mode;
1919
enum vsock_net_mode child_ns_mode;
20+
21+
/* 0 = unlocked, 1 = locked to global, 2 = locked to local */
22+
int child_ns_mode_locked;
2023
};
2124
#endif /* __NET_NET_NAMESPACE_VSOCK_H */

net/vmw_vsock/af_vsock.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,20 @@
9090
*
9191
* - /proc/sys/net/vsock/ns_mode (read-only) reports the current namespace's
9292
* mode, which is set at namespace creation and immutable thereafter.
93-
* - /proc/sys/net/vsock/child_ns_mode (writable) controls what mode future
93+
* - /proc/sys/net/vsock/child_ns_mode (write-once) controls what mode future
9494
* child namespaces will inherit when created. The initial value matches
9595
* the namespace's own ns_mode.
9696
*
9797
* Changing child_ns_mode only affects newly created namespaces, not the
9898
* current namespace or existing children. A "local" namespace cannot set
99-
* child_ns_mode to "global". At namespace creation, ns_mode is inherited
100-
* from the parent's child_ns_mode.
99+
* child_ns_mode to "global". child_ns_mode is write-once, so that it may be
100+
* configured and locked down by a namespace manager. Writing a different
101+
* value after the first write returns -EBUSY. At namespace creation, ns_mode
102+
* is inherited from the parent's child_ns_mode.
101103
*
102-
* The init_net mode is "global" and cannot be modified.
104+
* The init_net mode is "global" and cannot be modified. The init_net
105+
* child_ns_mode is also write-once, so an init process (e.g. systemd) can
106+
* set it to "local" to ensure all new namespaces inherit local mode.
103107
*
104108
* The modes affect the allocation and accessibility of CIDs as follows:
105109
*
@@ -2853,7 +2857,8 @@ static int vsock_net_child_mode_string(const struct ctl_table *table, int write,
28532857
new_mode == VSOCK_NET_MODE_GLOBAL)
28542858
return -EPERM;
28552859

2856-
vsock_net_set_child_mode(net, new_mode);
2860+
if (!vsock_net_set_child_mode(net, new_mode))
2861+
return -EBUSY;
28572862
}
28582863

28592864
return 0;

0 commit comments

Comments
 (0)