Skip to content

Commit 2ca2e8e

Browse files
authored
Merge pull request #2 from varpon/feat_mdo
feat: configure mod_do to allow command execution as root
2 parents 1f10891 + 2996c37 commit 2ca2e8e

5 files changed

Lines changed: 80 additions & 16 deletions

File tree

Containerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ RUN ln -sf /usr/local/lib/node_modules/code-server/out/node/entry.js /usr/local/
118118
COPY root/ /
119119

120120
# Set permissions
121-
RUN chmod +x /etc/services.d/code-server/run /healthz
121+
RUN chmod +x /etc/services.d/code-server/run /etc/cont-init.d/* /healthz
122122

123123
ENV HOME="/config"
124124

Containerfile.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ RUN ln -sf /usr/local/lib/node_modules/code-server/out/node/entry.js /usr/local/
112112
COPY root/ /
113113

114114
# Set permissions
115-
RUN chmod +x /etc/services.d/code-server/run /healthz
115+
RUN chmod +x /etc/services.d/code-server/run /etc/cont-init.d/* /healthz
116116

117117
ENV HOME="/config"
118118

README.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,39 @@ podman run -d --name code-server \
147147
!!! warning "Work in Progress"
148148
This image is functional but may change significantly in a future release.
149149

150-
Common dev tools (gcc, clang, llvm, python, gmake) are baked into the image for now.
150+
Common dev tools (gcc, clang, llvm, python, gmake, git, ssh) are baked into the image for now.
151151

152-
!!! warning "No sudo / su / doas in Terminal"
153-
Podman strips the setuid bit from binaries at runtime, so `sudo`, `su`, and `doas` will not work inside the code-server terminal. To run commands as root, use `podman exec` from the host.
152+
## Running commands as `root` in Terminal
153+
Podman strips the setuid bit from binaries at runtime, so `sudo`, `su`, and `doas` will not work inside the code-server terminal.
154154

155-
## Installing Packages
155+
To allow running commands as the `root` user, we can use FreeBSD's MAC framework and the `mdo` command which does not depend on the setuid bit being set.
156+
The `mac_do` kernel module has to be loaded on the host which runs Podman before the container is started.
157+
You can load the module at runtime by running
158+
```sh
159+
kldload mac_do
160+
```
161+
To load the module automatically during boot you can add it to `rc.conf` with
162+
```sh
163+
sysrc kld_list+=mac_do
164+
```
156165

157-
This is a known limitation of this WIP image and will likely improve in a future release.
166+
If the `mac_do` module is loaded when the container starts, it will automatically install a rule that allows the `bsd` user to execute commands as root by running `mdo <command>`.
158167

159-
Due to FreeBSD jail restrictions, `pkg install` cannot be run from the code-server terminal directly.
160-
Install additional packages from the host using `podman exec`:
168+
To disable the installation of the `mac_do` rule that allows the privilege elevation, you can set the `DISABLE_MDO` environment variable to `true` or `yes`:
169+
```yaml
170+
services:
171+
code-server:
172+
environment:
173+
- DISABLE_MDO=true
174+
```
175+
176+
## Installing Packages
177+
If the `mac_do` module is loaded on the host you can install packages in the terminal by running
178+
```sh
179+
mdo pkg install...
180+
```
161181

182+
If the module is not loaded or if `DISABLE_MDO` is set to `true` you have to install additional packages from the host using `podman exec`:
162183
```sh
163184
# Podman
164185
doas podman exec -it -u root code-server pkg install rust cargo

compose.yaml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,39 @@ x-daemonless:
99
!!! warning "Work in Progress"
1010
This image is functional but may change significantly in a future release.
1111
12-
Common dev tools (gcc, clang, llvm, python, gmake) are baked into the image for now.
12+
Common dev tools (gcc, clang, llvm, python, gmake, git, ssh) are baked into the image for now.
1313
14-
!!! warning "No sudo / su / doas in Terminal"
15-
Podman strips the setuid bit from binaries at runtime, so `sudo`, `su`, and `doas` will not work inside the code-server terminal. To run commands as root, use `podman exec` from the host.
14+
## Running commands as `root` in Terminal
15+
Podman strips the setuid bit from binaries at runtime, so `sudo`, `su`, and `doas` will not work inside the code-server terminal.
1616
17-
## Installing Packages
17+
To allow running commands as the `root` user, we can use FreeBSD's MAC framework and the `mdo` command which does not depend on the setuid bit being set.
18+
The `mac_do` kernel module has to be loaded on the host which runs Podman before the container is started.
19+
You can load the module at runtime by running
20+
```sh
21+
kldload mac_do
22+
```
23+
To load the module automatically during boot you can add it to `rc.conf` with
24+
```sh
25+
sysrc kld_list+=mac_do
26+
```
1827
19-
This is a known limitation of this WIP image and will likely improve in a future release.
28+
If the `mac_do` module is loaded when the container starts, it will automatically install a rule that allows the `bsd` user to execute commands as root by running `mdo <command>`.
2029
21-
Due to FreeBSD jail restrictions, `pkg install` cannot be run from the code-server terminal directly.
22-
Install additional packages from the host using `podman exec`:
30+
To disable the installation of the `mac_do` rule that allows the privilege elevation, you can set the `DISABLE_MDO` environment variable to `true` or `yes`:
31+
```yaml
32+
services:
33+
code-server:
34+
environment:
35+
- DISABLE_MDO=true
36+
```
37+
38+
## Installing Packages
39+
If the `mac_do` module is loaded on the host you can install packages in the terminal by running
40+
```sh
41+
mdo pkg install...
42+
```
2343
44+
If the module is not loaded or if `DISABLE_MDO` is set to `true` you have to install additional packages from the host using `podman exec`:
2445
```sh
2546
# Podman
2647
doas podman exec -it -u root code-server pkg install rust cargo
@@ -47,6 +68,7 @@ x-daemonless:
4768
TZ: "Timezone for the container"
4869
PASSWORD: "Password for web UI (leave unset to disable auth)"
4970
DEFAULT_WORKSPACE: "Default folder opened in the editor (default: /config/workspace)"
71+
DISABLE_MDO: "Do not use FreeBSD's mac_do facility to allow executing commands as root from the terminal (optional)"
5072
volumes:
5173
/config: "Configuration directory"
5274
ports:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
# Initialize mdo rule
3+
4+
# Ensure directories exist and have correct permissions
5+
echo "[mdo-init] Setting up mdo"
6+
if ! $(kldstat -q -m mac_do); then
7+
echo "[mdo-init] mac_do kernel module not loaded, skipping setup"
8+
exit 0
9+
fi
10+
11+
RULE="uid=${PUID}>uid=0,gid=*,+gid=*"
12+
13+
case "${DISABLE_MDO}" in
14+
[Tt][Rr][Uu][Ee]|[Yy][Ee][Ss]|1)
15+
echo "[mdo-init] mac_do support disabled"
16+
exit 0
17+
;;
18+
*)
19+
sysctl security.mac.do.rules="${RULE}"
20+
echo "[mdo-init] You can use mdo <command> do execute commands as root"
21+
esac

0 commit comments

Comments
 (0)