Skip to content

Commit daaaa0f

Browse files
committed
networks.Validate() requires that socket_vmnet is owned by root
This restriction was weakened by lima-vm#1220 to only require the file and directories to be owned by the admin user, but that configuration is not secure. If users are willing to run an insecure configuration, then they can always enable password-less sudo, which does not need a sudoers file at all. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent 713e1cd commit daaaa0f

File tree

6 files changed

+43
-54
lines changed

6 files changed

+43
-54
lines changed

cmd/limactl/sudoers.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"os"
7-
"path/filepath"
86
"runtime"
97

108
"github.com/lima-vm/lima/pkg/networks"
9+
"github.com/sirupsen/logrus"
1110
"github.com/spf13/cobra"
1211
)
1312

13+
const networksURL = "https://lima-vm.io/docs/config/network/#socket_vmnet"
14+
1415
func newSudoersCommand() *cobra.Command {
15-
networksMD := "$PREFIX/share/doc/lima/docs/network.md"
16-
if exe, err := os.Executable(); err == nil {
17-
binDir := filepath.Dir(exe)
18-
prefixDir := filepath.Dir(binDir)
19-
networksMD = filepath.Join(prefixDir, "share/doc/lima/docs/network.md")
20-
}
2116
sudoersCommand := &cobra.Command{
2217
Use: "sudoers [--check [SUDOERSFILE-TO-CHECK]]",
2318
Example: `
@@ -31,7 +26,7 @@ $ limactl sudoers --check /etc/sudoers.d/lima
3126
Long: fmt.Sprintf(`Generate the content of the /etc/sudoers.d/lima file for enabling vmnet.framework support.
3227
The content is written to stdout, NOT to the file.
3328
This command must not run as the root user.
34-
See %s for the usage.`, networksMD),
29+
See %s for the usage.`, networksURL),
3530
Args: WrapArgsError(cobra.MaximumNArgs(1)),
3631
RunE: sudoersAction,
3732
GroupID: advancedCommand,
@@ -52,6 +47,7 @@ func sudoersAction(cmd *cobra.Command, args []string) error {
5247
}
5348
// Make sure the current network configuration is secure
5449
if err := nwCfg.Validate(); err != nil {
50+
logrus.Infof("Please check %s for more information.", networksURL)
5551
return err
5652
}
5753
check, err := cmd.Flags().GetBool("check")

examples/vmnet.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# A template to enable vmnet.framework.
22

3-
# Usage:
4-
# brew install socket_vmnet
5-
# limactl sudoers >etc_sudoers.d_lima
6-
# sudo install -o root etc_sudoers.d_lima /etc/sudoers.d/lima
7-
# limactl start template://vmnet
3+
# Install socket_vmnet: https://lima-vm.io/docs/config/network/#socket_vmnet
84

95
# This template requires Lima v0.7.0 or later.
106
# Older versions of Lima were using a different syntax for supporting vmnet.framework.

pkg/networks/networks.TEMPLATE.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Path to socket_vmnet executable. Because socket_vmnet is invoked via sudo it should be
22
# installed where only root can modify/replace it. This means also none of the
3-
# parent directories should be writable by the user.
3+
# parent directories can be writable by the user.
44
#
55
# The varRun directory also must not be writable by the user because it will
66
# include the socket_vmnet pid file. Those will be terminated via sudo, so replacing

pkg/networks/validate.go

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55
"fmt"
66
"io/fs"
77
"os"
8-
"os/user"
98
"path/filepath"
109
"reflect"
1110
"runtime"
12-
"strconv"
1311
"strings"
1412

1513
"github.com/lima-vm/lima/pkg/osutil"
@@ -100,49 +98,24 @@ func validatePath(path string, allowDaemonGroupWritable bool) error {
10098
if err != nil {
10199
return err
102100
}
103-
adminGroup, err := user.LookupGroup("admin")
104-
if err != nil {
105-
return err
106-
}
107-
adminGid, err := strconv.Atoi(adminGroup.Gid)
108-
if err != nil {
109-
return err
110-
}
111-
owner, err := user.LookupId(strconv.Itoa(int(stat.Uid)))
112-
if err != nil {
113-
return err
114-
}
115-
ownerIsAdmin := owner.Uid == "0"
116-
if !ownerIsAdmin {
117-
ownerGroupIDs, err := owner.GroupIds()
118-
if err != nil {
119-
return err
120-
}
121-
for _, g := range ownerGroupIDs {
122-
if g == adminGroup.Gid {
123-
ownerIsAdmin = true
124-
break
125-
}
126-
}
127-
}
128-
if !ownerIsAdmin {
129-
return fmt.Errorf(`%s %q owner %dis not an admin`, file, path, stat.Uid)
101+
if stat.Uid != root.Uid {
102+
return fmt.Errorf(`%s %q is not owned by %q (uid: %d), but by uid %d`, file, path, root.User, root.Uid, stat.Uid)
130103
}
131104
if allowDaemonGroupWritable {
132105
daemon, err := osutil.LookupUser("daemon")
133106
if err != nil {
134107
return err
135108
}
136-
if fi.Mode()&0o20 != 0 && stat.Gid != root.Gid && stat.Gid != uint32(adminGid) && stat.Gid != daemon.Gid {
137-
return fmt.Errorf(`%s %q is group-writable and group %d is not one of [wheel, admin, daemon]`,
138-
file, path, stat.Gid)
109+
if fi.Mode()&0o20 != 0 && stat.Gid != root.Gid && stat.Gid != daemon.Gid {
110+
return fmt.Errorf(`%s %q is group-writable and group is neither %q (gid: %d) nor %q (gid: %d), but is gid: %d`,
111+
file, path, root.User, root.Gid, daemon.User, daemon.Gid, stat.Gid)
139112
}
140113
if fi.Mode().IsDir() && fi.Mode()&1 == 0 && (fi.Mode()&0o010 == 0 || stat.Gid != daemon.Gid) {
141114
return fmt.Errorf(`%s %q is not executable by the %q (gid: %d)" group`, file, path, daemon.User, daemon.Gid)
142115
}
143-
} else if fi.Mode()&0o20 != 0 && stat.Gid != root.Gid && stat.Gid != uint32(adminGid) {
144-
return fmt.Errorf(`%s %q is group-writable and group %d is not one of [wheel, admin]`,
145-
file, path, stat.Gid)
116+
} else if fi.Mode()&0o20 != 0 && stat.Gid != root.Gid {
117+
return fmt.Errorf(`%s %q is group-writable and group is not %q (gid: %d), but is gid: %d`,
118+
file, path, root.User, root.Gid, stat.Gid)
146119
}
147120
if fi.Mode()&0o02 != 0 {
148121
return fmt.Errorf("%s %q is world-writable", file, path)

pkg/store/filenames/filenames.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Package filenames defines the names of the files that appear under an instance dir
22
// or inside the config directory.
33
//
4-
// See docs/internal.md .
4+
// See https://lima-vm.io/docs/dev/internals/
55
package filenames
66

77
// Instance names starting with an underscore are reserved for lima internal usage

website/content/en/docs/config/network/_index.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,23 @@ The configuration steps are different for each network type:
6969
#### Managed (192.168.105.0/24)
7070

7171
[`socket_vmnet`](https://github.com/lima-vm/socket_vmnet) is required for adding another guest IP that is accessible from the host and other guests.
72+
It must be installed according to the instruction provided on https://github.com/lima-vm/socket_vmnet.
73+
74+
Note that installation using Homebrew is not secure and not recommended by the Lima project.
75+
Homebrew installation will only work with Lima if password-less `sudo` is enabled for the current user.
76+
The `limactl sudoers` command requires that `socket_vmnet` is installed into a secure path only
77+
writable by `root` and will reject `socket_vmnet` installed by Homebrew into a user-writable location.
7278

7379
```bash
74-
# Install socket_vmnet
75-
brew install socket_vmnet
80+
# Install socket_vmnet as root from source to /opt/socket_vmnet
81+
# using instructions on https://github.com/lima-vm/socket_vmnet
82+
# This assumes that Xcode Command Line Tools are already installed
83+
git clone https://github.com/lima-vm/socket_vmnet
84+
cd socket_vmnet
85+
# Change "v1.1.5" to the actual latest release in https://github.com/lima-vm/socket_vmnet/releases
86+
git checkout v1.1.5
87+
make
88+
sudo make PREFIX=/opt/socket_vmnet install.bin
7689

7790
# Set up the sudoers file for launching socket_vmnet from Lima
7891
limactl sudoers >etc_sudoers.d_lima
@@ -83,6 +96,9 @@ sudo install -o root etc_sudoers.d_lima /etc/sudoers.d/lima
8396
>
8497
> Lima before v0.12 used `vde_vmnet` for managing the networks.
8598
> `vde_vmnet` is no longer supported.
99+
>
100+
> Lima v0.14.0 and later used to also accept `socket_vmnet` installations if they were
101+
> owned by the `admin` user. Starting with v1.0.0 only `root` ownership is acceptable.
86102
87103
The networks are defined in `$LIMA_HOME/_config/networks.yaml`. If this file doesn't already exist, it will be created with these default
88104
settings:
@@ -145,7 +161,7 @@ limactl start --network=lima:shared
145161
```yaml
146162
networks:
147163
# Lima can manage the socket_vmnet daemon for networks defined in $LIMA_HOME/_config/networks.yaml automatically.
148-
# The socket_vmnet binary must be installed into a secure location only alterable by the admin.
164+
# The socket_vmnet binary must be installed into a secure location only alterable by the "root" user.
149165
# - lima: shared
150166
# # MAC address of the instance; lima will pick one based on the instance name,
151167
# # so DHCP assigned ip addresses should remain constant over instance restarts.
@@ -160,6 +176,14 @@ The network daemon is started automatically when the first instance referencing
160176
and will stop automatically once the last instance has stopped. Daemon logs will be stored in the
161177
`$LIMA_HOME/_networks` directory.
162178

179+
Since the commands to start and stop the `socket_vmnet` daemon requires root, the user either must
180+
have password-less `sudo` enabled, or add the required commands to a `sudoers` file. This can
181+
be done via:
182+
183+
```shell
184+
limactl sudoers | sudo tee /etc/sudoers.d/lima
185+
```
186+
163187
The IP address is automatically assigned by macOS's bootpd.
164188
If the IP address is not assigned, try the following commands:
165189
```bash

0 commit comments

Comments
 (0)