Commit Graph

1213 Commits

Author SHA1 Message Date
Robert Swiecki
d1b74c2238 util: put QC() in () 2023-09-22 16:40:56 +02:00
Robert Swiecki
d7cf1a9c0d make indent 2023-09-21 08:31:47 +02:00
Robert Swiecki
557633c1de Makefile: move to c++17 to use [[maybe_unused]] and remove DEFER (actually not used) from macros.h 2023-09-21 08:29:36 +02:00
Robert Swiecki
439606be70 Makefile/indent: add AlwaysBreakBeforeMultilineStrings:false 2023-09-19 20:31:57 +02:00
Robert Swiecki
2e27593482 make indent: clang-format-18 2023-09-19 08:45:42 +02:00
Robert Swiecki
b32146cd78 Makefile: simplifications around config.pb.* 2023-09-18 21:29:30 +02:00
Robert Swiecki
3af3eac80f caps: define new CAP_* unconditionally 2023-09-17 10:48:17 +02:00
Robert Swiecki
bc4e7fdd52 cmdline: constify structs 2023-09-15 15:38:37 +02:00
Robert Swiecki
6c5c13017d util/signal: sort signals according to asm/signal.h 2023-09-15 08:47:16 +02:00
Robert Swiecki
47d0e8e6eb pid: clear sigaction before use 2023-09-06 16:50:26 +02:00
Robert Swiecki
d92957c95a util: missing SIGPWR 2023-08-29 09:09:51 +02:00
Robert Swiecki
8308b91c04 subproc: mark cloneFunc as [[noreturn]] 2023-08-18 13:34:09 +02:00
Robert Swiecki
611ef1c435 subproc: support CLONE_CLEAR_SIGHAND 2023-08-18 13:26:01 +02:00
Robert Swiecki
5e494a6a78 subproc: display additional clone3 flags 2023-08-09 18:19:37 +02:00
Robert Swiecki
e507a64e09 configs/: formatting 2023-06-25 12:36:47 +02:00
Robert Swiecki
8804792862 configs/telegram: telegram is 64 bit only 2023-06-25 12:35:25 +02:00
Robert Swiecki
e736d55438 configs/telegram: a new config for the telegram-desktop 2023-06-24 21:04:29 +02:00
okunz
14f1e6852d formatting fix 2023-06-13 11:55:54 +02:00
okunz
924c8fa9f9
Better output formatting for --help 2023-06-13 11:47:21 +02:00
robertswiecki
e1402ed775
Merge pull request #219 from disconnect3d/patch-3
cgroup2.cc: improve note about using Docker
2023-05-30 21:32:12 +02:00
Disconnect3d
f7265e0690
cgroup2.cc: improve note about using Docker
Improve the error log message when Nsjail fails to write to the `/sys/fs/cgroup/cgroup.subtree_control` file when it attempts to setup the cgroupv2 configuration.

The previous message looked like this:

```
[E][2023-05-28T21:52:56+0000][8807] writeBufToFile():105 Couldn't write '7' bytes to file '/sys/fs/cgroup/cgroup.subtree_control' (fd='4'): Device or resource busy
[E][2023-05-28T21:52:56+0000][8807] enableCgroupSubtree():95 Could not apply '+memory' to cgroup.subtree_control in '/sys/fs/cgroup'. If you are running in Docker, nsjail MUST be the root process to use cgroups.
[E][2023-05-28T21:52:56+0000][8807] main():354 Couldn't setup parent cgroup (cgroupv2)
```

It could have been confusing because the nsjail may have already been running as real root with full capabilities, e.g., when the user ran the container with the `--privileged --user 0:0` flags. In such a case, the issue is that Docker enters new pid, uts, network, ipc, mount and cgroup namespaces (but not user or time namespaces, fwiw) and I believe that if you do so after the cgroupv2 filesystem is mounted, the root of its filesystem hierarchy will start to render only a subtree, or, generally a limited view of the cgroup.

This can be seen below. On the host, we can see the cgroup sub-hierarchies and the `cgroup.subtree_control` shows us the controllers properly:

```
# ls /sys/fs/cgroup/
cgroup.controllers      cgroup.threads         dev-mqueue.mount  memory.numa_stat               system.slice
cgroup.max.depth        cpu.pressure           init.scope        memory.pressure                user.slice
cgroup.max.descendants  cpuset.cpus.effective  io.cost.model     memory.stat
cgroup.procs            cpuset.mems.effective  io.cost.qos       sys-fs-fuse-connections.mount
cgroup.stat             cpu.stat               io.pressure       sys-kernel-config.mount
cgroup.subtree_control  dev-hugepages.mount    io.stat           sys-kernel-debug.mount

# cat /sys/fs/cgroup/cgroup.subtree_control 
cpuset cpu io memory hugetlb pids rdma
```

However, even in a privileged container, we can't see the same:

```
# sudo docker run --rm -it --privileged nsjail ls /sys/fs/cgroup
cgroup.controllers	cpuset.cpus		  memory.events.local
cgroup.events		cpuset.cpus.effective	  memory.high
cgroup.freeze		cpuset.cpus.partition	  memory.low
cgroup.kill		cpuset.mems		  memory.max
cgroup.max.depth	cpuset.mems.effective	  memory.min
cgroup.max.descendants	hugetlb.2MB.current	  memory.numa_stat
cgroup.procs		hugetlb.2MB.events	  memory.oom.group
cgroup.stat		hugetlb.2MB.events.local  memory.pressure
cgroup.subtree_control	hugetlb.2MB.max		  memory.stat
cgroup.threads		hugetlb.2MB.rsvd.current  memory.swap.current
cgroup.type		hugetlb.2MB.rsvd.max	  memory.swap.events
cpu.idle		io.latency		  memory.swap.high
cpu.max			io.max			  memory.swap.max
cpu.max.burst		io.pressure		  pids.current
cpu.pressure		io.stat			  pids.events
cpu.stat		io.weight		  pids.max
cpu.weight		memory.current		  rdma.current
cpu.weight.nice		memory.events		  rdma.max

# sudo docker run --rm -it --privileged nsjail cat /sys/fs/cgroup/cgroup.subtree_control
 
# 
```

Of course, the namespaces itself can be seen by comparing them like this:

```
// HOST
# ls -la /proc/self/ns
total 0
dr-x--x--x 2 root root 0 May 28 22:17 .
dr-xr-xr-x 9 root root 0 May 28 22:17 ..
lrwxrwxrwx 1 root root 0 May 28 22:17 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 May 28 22:17 ipc -> 'ipc:[4026531839]'
lrwxrwxrwx 1 root root 0 May 28 22:17 mnt -> 'mnt:[4026531841]'
lrwxrwxrwx 1 root root 0 May 28 22:17 net -> 'net:[4026531840]'
lrwxrwxrwx 1 root root 0 May 28 22:17 pid -> 'pid:[4026531836]'
lrwxrwxrwx 1 root root 0 May 28 22:17 pid_for_children -> 'pid:[4026531836]'
lrwxrwxrwx 1 root root 0 May 28 22:17 time -> 'time:[4026531834]'
lrwxrwxrwx 1 root root 0 May 28 22:17 time_for_children -> 'time:[4026531834]'
lrwxrwxrwx 1 root root 0 May 28 22:17 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 May 28 22:17 uts -> 'uts:[4026531838]'

// CONTAINER
# sudo docker run --rm -it --privileged nsjail ls -la /proc/self/ns
total 0
dr-x--x--x 2 user user 0 May 28 22:17 .
dr-xr-xr-x 9 user user 0 May 28 22:17 ..
lrwxrwxrwx 1 user user 0 May 28 22:17 cgroup -> 'cgroup:[4026532381]'
lrwxrwxrwx 1 user user 0 May 28 22:17 ipc -> 'ipc:[4026532317]'
lrwxrwxrwx 1 user user 0 May 28 22:17 mnt -> 'mnt:[4026532315]'
lrwxrwxrwx 1 user user 0 May 28 22:17 net -> 'net:[4026532319]'
lrwxrwxrwx 1 user user 0 May 28 22:17 pid -> 'pid:[4026532318]'
lrwxrwxrwx 1 user user 0 May 28 22:17 pid_for_children -> 'pid:[4026532318]'
lrwxrwxrwx 1 user user 0 May 28 22:17 time -> 'time:[4026531834]'
lrwxrwxrwx 1 user user 0 May 28 22:17 time_for_children -> 'time:[4026531834]'
lrwxrwxrwx 1 user user 0 May 28 22:17 user -> 'user:[4026531837]'
lrwxrwxrwx 1 user user 0 May 28 22:17 uts -> 'uts:[4026532316]'
```

Anyway, passing `--cgroupns=host` solves this problem, which can be seen below:

```
# ls -la /proc/self/ns | grep cgroup
lrwxrwxrwx 1 root root 0 May 28 22:18 cgroup -> cgroup:[4026531835]

# sudo docker run --rm -it --cgroupns=host --privileged nsjail ls -la /proc/self/ns | grep cgroup
lrwxrwxrwx 1 user user 0 May 28 22:19 cgroup -> 'cgroup:[4026531835]'

# sudo docker run --rm -it --privileged nsjail ls -la /proc/self/ns | grep cgroup
lrwxrwxrwx 1 user user 0 May 28 22:19 cgroup -> 'cgroup:[4026532381]'
```
2023-05-29 00:19:31 +02:00
Robert Swiecki
603ba857e9 logs: respect getenv(NO_COLOR) 2023-05-28 09:12:23 +02:00
Robert Swiecki
454cfb509f configs/hexchat: new config based on xchat 2023-05-26 08:42:52 +02:00
Wiktor Garbacz
f920c9194e Mount read-only directly if mounting rw fails
For new mounts if MNT_LOCK_READONLY is locked on the visible mnt
mount_too_revealing will fail and the whole mount will fail.
Those mounts need to be created with the readonly flag set.
2023-05-16 14:07:22 +02:00
Robert Swiecki
5b48117a09 configs/xchat: mount whole /tmp/.X11-unix 2023-01-03 08:11:47 +01:00
Robert Swiecki
c7c0adfffe config.prot: document disable_tsc 2022-11-22 22:25:15 +01:00
Robert Swiecki
2d9b694ca2 Readme: new output 2022-11-22 22:21:50 +01:00
Robert Swiecki
f2dc5966e3 all: unify comments on /**/ 2022-11-22 22:19:05 +01:00
Robert Swiecki
cc4245d23a make indent depend + style of comments 2022-11-22 22:15:01 +01:00
robertswiecki
4437810830
Merge pull request #208 from ndrewh/cgroupsv2-fix
Setup cgroup.subtree_control controllers when necessary in cgroupsv2
2022-11-22 22:12:12 +01:00
Andrew Haberlandt
12df56b9f1 Setup cgroup.subtree_control controllers when necessary in cgroupsv2
This commit adds extra setup when cgroupsv2 is enabled. In particular,
we make sure that the root namespace has setup cgroup.subtree_control
with the controllers we need.

If the necessary controller are not listed, we have to move all
processes out of the root namespace before we can change this
(the 'no internal processes' rule:
https://unix.stackexchange.com/a/713343). Currently we only
handle the case where the nsjail process is the only process in
the cgroup. It seems like this would be relatively rare, but since
nsjail is frequently the root process in a Docker container (e.g.
for hosting CTF challenges), I think this case is common enough to
make it worth implementing.

This also adds `--detect_cgroupv2`, which will attempt to detect
whether `--cgroupv2_mount` is a valid cgroupv2 mount, and if so
it will set `use_cgroupv2`. This is useful in containerized
environments where you may not know the kernel version ahead of time.

References:
https://github.com/redpwn/jail/blob/master/internal/cgroup/cgroup2.go
2022-11-17 17:09:40 -05:00
Oliver Newman
90e285450d Unset LDFLAGS for kafel
Otherwise kafel inherit's nsjail LDFLAGS, which isn't intended and
causes build failures.
2022-11-16 09:18:53 -08:00
Wiktor Garbacz
e3a8607ef5 Add missing cerrno include 2022-11-10 10:48:25 +01:00
Robert Swiecki
4567c78a27 config/xchat: move original .xchat2 config dir to .config/ 2022-10-25 14:55:04 +02:00
Robert Swiecki
fdc640e20c subproc.h: make cloneProc declaration match the definition 2022-10-25 08:33:23 +02:00
Robert Swiecki
285ea15811 subproc/mount: use better types for flags, u64 for clone, unsigned long for mount 2022-10-24 13:12:20 +02:00
Wiktor Garbacz
2e62649b4c Update kafel 2022-10-14 11:54:25 +02:00
Robert Swiecki
dc42a5d003 configs/bash: remove tmpfs mount over /dev as it makes /dev/null non-writeable 2022-09-15 16:12:13 +02:00
Robert Swiecki
454b051599 configs/firefox-with-net-wayland: x11 socket is not needed here 2022-09-10 16:32:06 +02:00
Robert Swiecki
80b26e7554 caps: shorter std::string::append 2022-09-06 17:44:55 +02:00
Robert Swiecki
b87f983463 configs: make configs using X11 more versatile 2022-09-04 12:07:55 +02:00
Robert Swiecki
a22bb2e437 make indent 2022-08-27 21:17:43 +02:00
Robert Swiecki
595cdc8916 nsjail: use atomic in sighandlers 2022-08-26 14:40:46 +02:00
Robert Swiecki
9a8d440a7c configs/xchat-with-net: use 8.8.8.8 in resolv.conf unconditionally 2022-08-26 00:44:21 +02:00
Robert Swiecki
c63e5b39e8 use QC() across the code 2022-08-10 15:23:53 +02:00
Robert Swiecki
730b890ded cpu: more debug messaging 2022-08-10 15:02:53 +02:00
Robert Swiecki
30c81ce01f configs: block sched_setaffinity where max_cpus is used 2022-08-09 16:40:07 +02:00
Robert Swiecki
b3fcc30aec cpu: more debugging messages 2022-08-09 16:13:03 +02:00
Robert Swiecki
f628f74b00 mnt: quote paths in log messages 2022-08-09 12:06:42 +02:00
Robert Swiecki
e98dc415fc Switch C++ standard to C++14 - it'll allow to use new features, like std::quoted 2022-08-09 11:34:18 +02:00