Skip to content

Commit 0a1161d

Browse files
authored
fix-namespaces.hook: refactor and secureblue support (#194)
1 parent 3279bef commit 0a1161d

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

useful-tools/hooks/fix-namespaces.hook

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,74 @@
77
CACHEDIR="${XDG_CACHE_HOME:-$HOME/.cache}"
88
LOCKFILEPATH="$CACHEDIR"/.disable-namespaces-check
99

10-
INFO_MESSAGE="
11-
Starting Ubuntu24.04 Canonical decided that namespaces are not safe and is preventing us from using it to sandbox applications.
12-
namespaces are safe and a vital part of the security model of all web browsers, flatpak, electron apps, etc
10+
INFO_MESSAGE_BASE="
11+
Disabled unprivileged user-namespaces detected
1312
14-
To fix this issue I will need permission to disable the restriction, like all other linux distros do and several ubuntu forks had to undo.
13+
Unprivileged user-namespaces are required to use this application.
1514
16-
For more details see: https://github.com/pkgforge-dev/Anylinux-AppImages/blob/main/useful-tools/fix-namespaces.md#why
15+
Certain Linux distributions like Ubuntu since v24.04 and secureblue disable unprivileged user-namespaces by default due to safety concerns.
16+
This is what prevents the applications to utilize sandboxing.
17+
Unprivileged user-namespaces are safe and a vital part of the security model of all web browsers, flatpak, electron apps, etc.
1718
19+
For more details, see: https://github.com/pkgforge-dev/Anylinux-AppImages/blob/main/useful-tools/fix-namespaces.md#why
20+
"
21+
22+
INFO_MESSAGE_WITHOUT_FIX="$INFO_MESSAGE_BASE
23+
We do not have an automated way to enable unprivileged user-namespaces for your Linux distribution at the moment, so you will have to enable those manually.
24+
"
25+
26+
INFO_MESSAGE_FIX="$INFO_MESSAGE_BASE
27+
To fix this issue, I will need a permission to disable this restriction, like all the other Linux distributions do and several Ubuntu forks had to undo.
28+
"
29+
30+
INFO_MESSAGE_FIX_APPARMOR="$INFO_MESSAGE_FIX
1831
If you later wish to undo this change, remove: '/etc/sysctl.d/20-fix-namespaces.conf'
1932
and then run 'sysctl -w kernel.apparmor_restrict_unprivileged_userns=1' or reboot.
2033
"
21-
WARNING="
22-
WARNING: I'm not able to create a namespace and not sure what is preventing it.
23-
We will continue without prompting but if something breaks you know why.
34+
35+
INFO_MESSAGE_FIX_SECUREBLUE="$INFO_MESSAGE_FIX
36+
If you later wish to undo this change, run this command: 'ujust toggle-unconfined-domain-userns-creation'.
37+
Changes are immediate, there is no need to reboot.
2438
"
25-
DO_NOT_ASK="Do you wish to not see this message again?"
2639

27-
# if this fails namespaces are disabled
28-
_check_namespaces_work() {
29-
unshare --user -p /bin/true >/dev/null 2>&1
30-
}
40+
DO_NOT_ASK="Do you wish to not see this message again about unprivileged user-namespaces?"
3141

32-
# Make sure we have all the needed deps
33-
# Unlikely to be ubuntu or its spins if any of these conditions are true
34-
_is_false_positive() {
35-
if ! command -v /bin/true \
36-
|| ! command -v sysctl \
37-
|| ! command -v unshare \
38-
|| ! command -v notify; then
39-
FALSE_POSITIVE=1
40-
elif [ ! -d /etc/sysctl.d ] \
41-
|| [ ! -d /etc/apparmor.d ]; then
42-
FALSE_POSITIVE=1
43-
elif ! unshare --help | grep -q -- '--user'; then
44-
FALSE_POSITIVE=1
45-
fi
46-
47-
if [ "$FALSE_POSITIVE" = 1 ]; then
48-
>&2 echo "$WARNING"
49-
return 0
42+
# if this fails, namespaces are disabled
43+
_check_usernamespaces_work() {
44+
if command -v /bin/true && unshare --help | grep -q -- '--user'; then
45+
unshare --user -p /bin/true && return 0
5046
fi
5147
return 1
5248
}
5349

54-
_fix_ubuntu_mess() {
55-
if notify --display-question "$INFO_MESSAGE"; then
56-
pkexec /bin/sh -c "
57-
echo 'kernel.apparmor_restrict_unprivileged_userns = 0' \
58-
| tee /etc/sysctl.d/20-fix-namespaces.conf
59-
sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
60-
"
50+
_fix_usernamespaces() {
51+
if command -v sysctl 1>/dev/null && [ -d /etc/sysctl.d ] \
52+
&& command -v pkexec 1>/dev/null \
53+
&& [ -e /proc/sys/kernel/apparmor_restrict_unprivileged_userns ]; then
54+
apparmor_based=1
55+
elif command -v ujust 1>/dev/null \
56+
&& ujust | grep -q toggle-unconfined-domain-userns-creation; then
57+
secureblue_based=1
58+
fi
59+
60+
if [ "$apparmor_based" = 1 ]; then
61+
if notify --display-question "$INFO_MESSAGE_FIX_APPARMOR"; then
62+
pkexec /bin/sh -c "
63+
echo 'kernel.apparmor_restrict_unprivileged_userns = 0' \
64+
| tee /etc/sysctl.d/20-fix-namespaces.conf
65+
sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
66+
"
67+
else
68+
return 1
69+
fi
70+
elif [ "$secureblue_based" = 1 ]; then
71+
if notify --display-question "$INFO_MESSAGE_FIX_SECUREBLUE"; then
72+
ujust toggle-unconfined-domain-userns-creation
73+
else
74+
return 1
75+
fi
6176
else
62-
return 1
77+
notify --display-info "$INFO_MESSAGE_WITHOUT_FIX"
6378
fi
6479
}
6580

@@ -68,13 +83,11 @@ _do_not_ask_again() {
6883
echo "delete me to enable again" > "$LOCKFILEPATH"
6984
}
7085

71-
if [ -f /etc/sysctl.d/20-fix-namespaces.conf ] || [ -f "$LOCKFILEPATH" ]; then
72-
exit 0
73-
elif _check_namespaces_work; then
86+
if [ -f "$LOCKFILEPATH" ]; then
7487
exit 0
75-
elif _is_false_positive 1>/dev/null; then
88+
elif _check_usernamespaces_work >/dev/null 2>&1; then
7689
exit 0
77-
elif _fix_ubuntu_mess; then
90+
elif _fix_usernamespaces; then
7891
exit 0
7992
elif notify --display-question "$DO_NOT_ASK"; then
8093
_do_not_ask_again

0 commit comments

Comments
 (0)