Gentoo Archives: gentoo-portage-dev

From: Michael Orlitzky <mjo@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [PATCH 1/2] bin/install-qa-check.d: add new 90bad-bin-owner QA check.
Date: Mon, 30 Jul 2018 00:17:40
Message-Id: 20180730001730.19257-2-mjo@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH v2 0/2] Two insecure ownership and group-writability QA checks. by Michael Orlitzky
1 System executables that are not owned by root pose a security
2 risk. The owner of the executable is free to modify it at any time;
3 so, for example, he can change a daemon's behavior to make it
4 malicious before the next time the service is started (usually by
5 root).
6
7 On a "normal" system, there is no good reason why the superuser should
8 not own every system executable. This commit adds a new install-time
9 check that reports any such binaries with a QA warning. To avoid false
10 positives, non-"normal" systems (like prefix) are skipped at the moment.
11
12 Bug: https://bugs.gentoo.org/629398
13 ---
14 bin/install-qa-check.d/90bad-bin-owner | 47 ++++++++++++++++++++++++++++++++++
15 1 file changed, 47 insertions(+)
16 create mode 100644 bin/install-qa-check.d/90bad-bin-owner
17
18 diff --git a/bin/install-qa-check.d/90bad-bin-owner b/bin/install-qa-check.d/90bad-bin-owner
19 new file mode 100644
20 index 000000000..748e1dc99
21 --- /dev/null
22 +++ b/bin/install-qa-check.d/90bad-bin-owner
23 @@ -0,0 +1,47 @@
24 +# Copyright 1999-2018 Gentoo Foundation
25 +# Distributed under the terms of the GNU General Public License v2
26 +
27 +bad_bin_owner_check() {
28 + # Warn about globally-installed executables (in /bin, /usr/bin, /sbin,
29 + # /usr/sbin, or /opt/bin) that are owned by a nonzero UID.
30 +
31 + # This check doesn't work on non-root prefix installations at
32 + # the moment, because every executable therein is owned by a
33 + # nonzero UID.
34 + [[ "${EUID}" -ne "0" || "${PORTAGE_INST_UID}" -ne "0" ]] && return
35 +
36 + local d f found=()
37 +
38 + for d in "${ED%/}/opt/bin" "${ED%/}/bin" "${ED%/}/usr/bin" \
39 + "${ED%/}/sbin" "${ED%/}/usr/sbin"; do
40 + [[ -d "${d}" ]] || continue
41 +
42 + # Read the results of the "find" command into the "found" bash array.
43 + # Use -L to catch symlinks whose targets are owned by a non-root user,
44 + # even though it won't catch ABSOLUTE symlinks until the package
45 + # is RE-installed (the first time around, the target won't exist).
46 + # We do want to list non-superuser setuid executables, because
47 + # they can be exploited. The owner can simply wipe the setuid
48 + # bit, and then alter the contents of the file. The superuser
49 + # will then have a time bomb in his $PATH.
50 + while read -r -d '' f; do
51 + found+=( "${f}" )
52 + done < <(find -L "${d}" \
53 + -maxdepth 1 \
54 + -type f \
55 + ! -uid 0 \
56 + -print0)
57 + done
58 +
59 + if [[ ${found[@]} ]]; then
60 + eqawarn "system executables owned by nonzero uid:"
61 + for f in "${found[@]}"; do
62 + # Strip off the leading destdir before outputting the path,
63 + # but leave the prefix if there is one.
64 + eqawarn " ${f#${D%/}/}"
65 + done
66 + fi
67 +}
68 +
69 +bad_bin_owner_check
70 +:
71 --
72 2.16.4