blob: 1502f7ca3916824582b57f2ea908fe89d6860eb4 [file] [log] [blame]
Mike Frysingerb9743c62020-02-20 02:53:55 -05001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Make sure packages don't create random paths outside of existing norms."""
8
9from __future__ import print_function
10
11import argparse
12import fnmatch
13import logging # pylint: disable=cros-logging-import
14import os
15import sys
16
17
18# NB: Do not add any new entries here without wider discussion.
19
20
21# Paths that are allowed in the / dir.
22VALID_ROOT = {
23 'bin', 'boot', 'dev', 'etc', 'home', 'lib', 'lib32', 'lib64', 'media',
24 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'sys', 'tmp', 'usr', 'var',
25}
26
27# Paths that are allowed in the / dir for boards.
28VALID_BOARD_ROOT = {
29 'build', 'firmware',
30 # TODO(): We should clean this up.
31 'postinst',
32}
33
34# Paths that are allowed in the / dir for the SDK chroot.
35VALID_HOST_ROOT = set()
36
37# Paths under / that should not have any subdirs.
38NOSUBDIRS_ROOT = {
39 'bin', 'dev', 'proc', 'sbin', 'sys', 'tmp',
40}
41
42# Paths that are allowed in the /usr dir.
43VALID_USR = {
44 'bin', 'include', 'lib', 'lib32', 'lib64', 'libexec', 'sbin', 'share',
45 'src',
46}
47
48# Paths that are allowed in the /usr dir for boards.
49VALID_BOARD_USR = {
50 # Boards install into /usr/local for test images.
51 'local',
52}
53
54# Paths that are allowed in the /usr dir for the SDK chroot.
55VALID_HOST_USR = set()
56
57# Paths under /usr that should not have any subdirs.
58NOSUBDIRS_USR = {
59 'bin', 'sbin',
60}
61
62# Valid toolchain targets. We don't want to add any more non-standard ones.
63# targets that use *-cros-* as the vendor are OK to add more.
64KNOWN_TARGETS = {
65 'arm-none-eabi',
66 'i686-pc-linux-gnu',
67 'x86_64-pc-linux-gnu',
68 'arm*-cros-eabi',
69 '*-cros-linux-gnu*',
70}
71
72# These packages need fixing.
73# NB: Do *not* add more packages here.
74BAD_ROOT_PACKAGES = {
75 # TODO(crbug.com/1003107): Delete this.
76 'dev-go/go-tools',
77}
78
79# These SDK packages need cleanup.
80# NB: Do *not* add more packages here.
81BAD_HOST_USR_LOCAL_PACKAGES = {
82 'app-crypt/nss',
83}
84
85# Ignore some packages installing into /var for now.
86# NB: Do *not* add more packages here.
87BAD_VAR_PACKAGES = {
88 'app-accessibility/brltty',
89 'app-admin/eselect',
90 'app-admin/puppet',
91 'app-admin/rsyslog',
92 'app-admin/sudo',
93 'app-admin/sysstat',
94 'app-admin/webapp-config',
95 'app-crypt/mit-krb5',
96 'app-crypt/trousers',
97 'app-emulation/containerd',
98 'app-emulation/lxc',
99 'chromeos-base/chromeos-initramfs',
100 # https://crbug.com/1054646
101 'chromeos-base/devserver',
102 # https://crbug.com/1007402
103 'chromeos-base/factory',
104 'chromeos-base/factory-board',
105 'dev-python/django',
106 'media-gfx/sane-backends',
107 'media-sound/alsa-utils',
108 'net-analyzer/netperf',
109 'net-dns/dnsmasq',
Aileen Chengc1e163a2020-03-02 14:47:00 -0800110 'net-firewall/ebtables',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500111 'net-firewall/iptables',
112 'net-fs/samba',
Mike Frysinger0669e002020-03-02 16:56:24 -0500113 'net-misc/chrony',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500114 'net-misc/dhcpcd',
115 'net-misc/openssh',
116 'net-print/cups',
117 'sys-apps/dbus',
118 'sys-apps/fwupd',
119 'sys-apps/iproute2',
120 'sys-apps/journald',
121 'sys-apps/portage',
122 'sys-apps/sandbox',
123 'sys-apps/systemd',
124 'sys-apps/usbguard',
125 'sys-kernel/loonix-initramfs',
126 'sys-libs/glibc',
127 'sys-process/audit',
128 'www-servers/nginx',
129 'x11-base/xwayland',
130}
131
132# Ignore some packages installing into /run for now.
133# NB: Do *not* add more packages here.
134BAD_RUN_PACKAGES = {
135 'app-accessibility/brltty',
136 'net-fs/samba',
137}
138
139# Ignore some packages installing into /tmp for now.
140# NB: Do *not* add more packages here.
141BAD_TMP_PACKAGES = {
142 # https://crbug.com/1057059
143 'chromeos-base/chromeos-bsp-caroline-private',
Mike Frysinger833a8222020-03-02 14:04:36 -0500144 'chromeos-base/chromeos-bsp-elm-private',
Mike Frysinger7dd8d682020-03-03 11:18:29 -0500145 'chromeos-base/chromeos-config-bsp',
Mike Frysinger833a8222020-03-02 14:04:36 -0500146 'chromeos-base/chromeos-config-bsp-coral',
147 'chromeos-base/chromeos-config-bsp-coral-private',
148 'chromeos-base/chromeos-config-bsp-nami',
149 'chromeos-base/chromeos-config-bsp-scarlet-private',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500150 'chromeos-base/cros-config-test',
151}
152
153
154def has_subdirs(path):
155 """See if |path| has any subdirs."""
156 # These checks are helpful for manually running the script when debugging.
157 if os.path.ismount(path):
158 logging.warning('Ignoring mounted dir for subdir check: %s', path)
159 return False
160
161 if os.path.join(os.getenv('SYSROOT', '/'), 'tmp') == path:
162 logging.warning('Ignoring live dir: %s', path)
163 return False
164
165 for _, dirs, _ in os.walk(path):
166 if dirs:
167 logging.error('Subdirs found in a dir that should be empty:\n %s\n'
168 ' |-- %s', path, '\n |-- '.join(sorted(dirs)))
169 return True
170 break
171
172 return False
173
174
175def check_usr(usr, host=False):
176 """Check the /usr filesystem at |usr|."""
177 ret = True
178
179 # Not all packages install into /usr.
180 if not os.path.exists(usr):
181 return ret
182
183 atom = get_current_package()
184 paths = set(os.listdir(usr))
185 unknown = paths - VALID_USR
186 for target in KNOWN_TARGETS:
187 unknown = set(x for x in unknown if not fnmatch.fnmatch(x, target))
188 if host:
189 unknown -= VALID_HOST_USR
190
191 if atom in BAD_HOST_USR_LOCAL_PACKAGES:
192 logging.warning('Ignoring known bad /usr/local install for now')
193 unknown -= {'local'}
194 else:
195 unknown -= VALID_BOARD_USR
196
197 if atom in {'chromeos-base/ap-daemons'}:
198 logging.warning('Ignoring known bad /usr install for now')
199 unknown -= {'www'}
200
201 if unknown:
202 logging.error('Paths are not allowed in the /usr dir: %s', sorted(unknown))
203 ret = False
204
205 for path in NOSUBDIRS_USR:
206 if has_subdirs(os.path.join(usr, path)):
207 ret = False
208
209 return ret
210
211
212def check_root(root, host=False):
213 """Check the filesystem |root|."""
214 ret = True
215
216 atom = get_current_package()
217 paths = set(os.listdir(root))
218 unknown = paths - VALID_ROOT
219 if host:
220 unknown -= VALID_HOST_ROOT
221 else:
222 unknown -= VALID_BOARD_ROOT
223
224 if atom in BAD_ROOT_PACKAGES:
225 logging.warning('Ignoring known bad / install for now')
226 elif unknown:
227 logging.error('Paths are not allowed in the root dir:\n %s\n |-- %s',
228 root, '\n |-- '.join(sorted(unknown)))
229 ret = False
230
231 # Some of these may have subdirs at runtime, but not from package installs.
232 for path in NOSUBDIRS_ROOT:
233 if has_subdirs(os.path.join(root, path)):
234 if path == 'tmp' and atom in BAD_TMP_PACKAGES:
235 logging.warning('Ignoring known bad /tmp install for now')
236 else:
237 ret = False
238
239 # Special case /var due to so many misuses currently.
240 if has_subdirs(os.path.join(root, 'var')):
241 if atom in BAD_VAR_PACKAGES:
242 logging.warning('Ignoring known bad /var install for now')
243 elif os.environ.get('PORTAGE_REPO_NAME') == 'portage-stable':
244 logging.warning('Ignoring bad /var install with portage-stable package '
245 'for now')
246 else:
247 ret = False
248 else:
249 if atom in BAD_VAR_PACKAGES:
250 logging.warning('Package has improved; please update BAD_VAR_PACKAGES')
251
252 # Special case /run due to so many misuses currently.
253 if has_subdirs(os.path.join(root, 'run')):
254 if atom in BAD_RUN_PACKAGES:
255 logging.warning('Ignoring known bad /run install for now')
256 elif os.environ.get('PORTAGE_REPO_NAME') == 'portage-stable':
257 logging.warning('Ignoring bad /run install with portage-stable package '
258 'for now')
259 else:
260 ret = False
261 else:
262 if atom in BAD_RUN_PACKAGES:
263 logging.warning('Package has improved; please update BAD_RUN_PACKAGES')
264
265 if not check_usr(os.path.join(root, 'usr'), host):
266 ret = False
267
268 return ret
269
270
271def get_current_package():
272 """Figure out what package is being built currently."""
273 if 'CATEGORY' in os.environ and 'PN' in os.environ:
274 return f'{os.environ.get("CATEGORY")}/{os.environ.get("PN")}'
275 else:
276 return None
277
278
279def get_parser():
280 """Get a CLI parser."""
281 parser = argparse.ArgumentParser(description=__doc__)
282 parser.add_argument('--host', default=None, action='store_true',
283 help='the filesystem is the host SDK, not board sysroot')
284 parser.add_argument('--board', dest='host', action='store_false',
285 help='the filesystem is a board sysroot')
286 parser.add_argument('root', nargs='?',
287 help='the rootfs to scan')
288 return parser
289
290
291def main(argv):
292 """The main func!"""
293 parser = get_parser()
294 opts = parser.parse_args(argv)
295
296 # Default to common portage env vars.
297 if opts.root is None:
298 for var in ('ED', 'D', 'ROOT'):
299 if var in os.environ:
300 logging.debug('Scanning filesystem root via $%s', var)
301 opts.root = os.environ[var]
302 break
303 if not opts.root:
304 parser.error('Need a valid rootfs to scan, but unable to detect one')
305
306 if opts.host is None:
307 if os.getenv('BOARD') == 'amd64-host':
308 opts.host = True
309 else:
310 opts.host = not bool(os.getenv('SYSROOT'))
311
312 if not check_root(opts.root, opts.host):
313 logging.critical(
314 "This package does not conform to CrOS's filesystem conventions. "
315 'Please review the paths flagged above and adjust its layout.')
316 return 1
317 else:
318 return 0
319
320
321if __name__ == '__main__':
322 sys.exit(main(sys.argv[1:]))