Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update livecheck.sh to fix Linux 6 loop device false positives #14

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 33 additions & 3 deletions usr/share/livecheck/livecheck.sh
Expand Up @@ -2,19 +2,31 @@

## Copyright (C) 2018 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
## Copyright (C) 2018 Algernon <33966997+Algernon-01@users.noreply.github.com>
## Copyright (C) 2023 PXLKNG <79484393+pxlkng@users.noreply.github.com>
## See the file COPYING for copying conditions.

set -e

## NOTICE: As of Linux 6 `lsblk --all` outputs 8 empty read-writeable loop devices. Those seem to be placeholders and not actually active. (without snapd)
## See: https://forums.kicksecure.com/t/livecheck-sh-script-broken-on-bookworm/269
##
## sudo /bin/lsblk --all
##
## NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
## loop0 7:0 0 0B 0 loop
## loop1 7:1 0 0B 0 loop
## loop2 7:2 0 0B 0 loop
## loop3 7:3 0 0B 0 loop
## loop4 7:4 0 0B 0 loop
## loop5 7:5 0 0B 0 loop
## loop6 7:6 0 0B 0 loop
## loop7 7:7 0 0B 0 loop
## sda 8:0 0 100G 1 disk
##
## 1 means read-only
## 0 means read-write

## As soon as we have at least one "0" it is concluded: not live mode.
## As soon as we have at least one "0" (empty/0B loop devices are ignored) it is concluded: not live mode.

## when using snapd:
##
Expand Down Expand Up @@ -80,9 +92,12 @@ else
fi

## Check if execution of lsblk fails with a non-zero exit code such as in case of missing sudoers permissions.
if ! lsblk_output="$(sudo --non-interactive /bin/lsblk --noheadings --all --raw --output RO)" ; then

## FIX: https://forums.kicksecure.com/t/livecheck-sh-script-broken-on-bookworm/269
## Change lsblk call to include `--output SIZE,RO,TYPE` since this info is needed to sanitize and crop accordingly later.
if ! lsblk_output_unsanitized="$(sudo --non-interactive /bin/lsblk --all --raw --noheadings --output SIZE,RO,TYPE)" ; then
## lsblk exited a non-zero exit code.
true "INFO: Running 'sudo --non-interactive /bin/lsblk --noheadings --all --raw --output RO' failed!"
true "INFO: Running 'sudo --non-interactive /bin/lsblk --all --raw --noheadings --output SIZE,RO,TYPE' failed!"
echo "<img>/usr/share/icons/gnome-colors-common/scalable/status/dialog-error.svg</img>"
## Show "Error" next to info symbol in systray.
echo "<txt>Error</txt>"
Expand All @@ -93,6 +108,21 @@ if ! lsblk_output="$(sudo --non-interactive /bin/lsblk --noheadings --all --raw
fi
## lsblk exited with exit code 0.

## Sanitize lsblk output with RegEx (PCRE) to remove all empty loop devices.
## The following RegEx does an inverted grep search for "^0B\space+\digit\space+loop$" (simplified) thus matching every line of "0B 0 loop" or "0B 1 loop" effectively removing all empty loop devices.
## (See FIX above)
lsblk_output_pre1=$(echo "${lsblk_output_unsanitized}" | grep -vPx '^0B\s+\d\s+loop$')

## For the livecheck we only need the RO values. But because we needed to include `SIZE` and `TYPE` for sanitization we now have to remove those.
## In the next two steps we remove the unwanted string overhead BEFORE and AFTER the RO value, with RegEx (ERE).

## The following RegEx (ERE) searches for "^\digit+\.?\digit+\nonspace+\space+" (simplified) and replaces every occurence with "" thus removing everything BEFORE the RO values.
lsblk_output_pre2=$(echo "${lsblk_output_pre1}" | sed -r 's/^[0-9]+\.?[0-9]+\S+\s+//g')
## The following RegEx (ERE) searches for "\space+\nonspace+$" (simplified) and replaces every occurence with "" thus removing everything AFTER the RO values.
lsblk_output=$(echo "${lsblk_output_pre2}" | sed -r 's/\s+\S+$//g')
## lsblk_output is now only the RO values of the whole `lsblk --all [...]` output except the empty loop devices.

## Checking if there is any 0 / read-write device.
if echo "$lsblk_output" | grep --quiet "0" ; then
true "INFO: If at least one '0' was found. Conclusion: not all read-only. Some read-write."

Expand Down