Gentoo Archives: gentoo-portage-dev

From: Michael Orlitzky <mjo@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [PATCH v3 2/2] bin/install-qa-check.d: add new 90bad-bin-group-write QA check.
Date: Tue, 07 Aug 2018 16:46:30
Message-Id: 20180807164604.11308-3-mjo@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH v3 0/2] Two insecure ownership and group-writability QA checks.​ by Michael Orlitzky
1 System executables that are writable by a non-root user pose a
2 security risk. Anyone who can write to an executable can change its
3 behavior. If that executable is later run with elevated privileges
4 (say, by root, when the machine starts), then the non-root user can
5 escalate his own privileges to those of the person running the
6 modified executable.
7
8 The 90bad-bin-owner check already addresses one cause for a non-root
9 user to be able to modify an executable: because he owns it. This
10 commit adds another check, to ensure that no non-root *groups* have
11 write access to any system executables. On a "normal" system, all
12 system executables should be writable only by the super-user's group,
13 if any. To avoid false-positives, non-"normal" systems (like prefix)
14 are skipped.
15
16 Closes: https://bugs.gentoo.org/629398
17 ---
18 bin/install-qa-check.d/90bad-bin-group-write | 55 ++++++++++++++++++++++++++++
19 1 file changed, 55 insertions(+)
20 create mode 100644 bin/install-qa-check.d/90bad-bin-group-write
21
22 diff --git a/bin/install-qa-check.d/90bad-bin-group-write b/bin/install-qa-check.d/90bad-bin-group-write
23 new file mode 100644
24 index 000000000..786dde712
25 --- /dev/null
26 +++ b/bin/install-qa-check.d/90bad-bin-group-write
27 @@ -0,0 +1,55 @@
28 +# Copyright 1999-2018 Gentoo Foundation
29 +# Distributed under the terms of the GNU General Public License v2
30 +
31 +bad_bin_group_write_check() {
32 + # Warn about globally-installed executables (in /bin, /usr/bin, /sbin,
33 + # /usr/sbin, or /opt/bin) that are group-writable by a nonzero GID.
34 +
35 + # This check doesn't work on non-root prefix installations at
36 + # the moment, because every executable therein is owned by a
37 + # nonzero GID.
38 + [[ "${EUID}" -ne "0" || "${PORTAGE_INST_UID}" -ne "0" ]] && return
39 +
40 + local d f found=()
41 +
42 + for d in "${ED%/}/opt/bin" "${ED%/}/bin" "${ED%/}/usr/bin" \
43 + "${ED%/}/sbin" "${ED%/}/usr/sbin"; do
44 + [[ -d "${d}" ]] || continue
45 +
46 + # Read the results of the "find" command into the "found" array.
47 + #
48 + # Use -L to catch symlinks whose targets are vulnerable,
49 + # even though it won't catch ABSOLUTE symlinks until the package
50 + # is RE-installed (the first time around, the target won't exist).
51 + #
52 + # We match the GID and not the name "root" here because (for
53 + # example) on FreeBSD, the superuser group is "wheel".
54 + #
55 + # We don't make an exception for setguid executables here, because
56 + # a group-writable setguid executable is likely a mistake. By
57 + # altering the contents of the executable, a member of the group
58 + # can allow everyone (i.e. the people running it) to obtain the
59 + # full privileges available to that group. While only existing
60 + # group members can make that choice, it's a decision usually
61 + # limited to the system administrator.
62 + while read -r -d '' f; do
63 + found+=( "${f}" )
64 + done < <(find -L "${d}" \
65 + -maxdepth 1 \
66 + -type f \
67 + -perm /g+w \
68 + ! -gid 0 \
69 + -print0)
70 + done
71 +
72 + if [[ ${found[@]} ]]; then
73 + eqawarn "system executables group-writable by nonzero gid:"
74 + for f in "${found[@]}"; do
75 + # Strip off the leading destdir before outputting the path.
76 + eqawarn " ${f#${D%/}}"
77 + done
78 + fi
79 +}
80 +
81 +bad_bin_group_write_check
82 +:
83 --
84 2.16.4