blob: 94d6e7043783addbecde3969a3722272ad07235d [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',
145 'chromeos-base/chromeos-config-bsp-coral',
146 'chromeos-base/chromeos-config-bsp-coral-private',
147 'chromeos-base/chromeos-config-bsp-nami',
148 'chromeos-base/chromeos-config-bsp-scarlet-private',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500149 'chromeos-base/cros-config-test',
150}
151
152
153def has_subdirs(path):
154 """See if |path| has any subdirs."""
155 # These checks are helpful for manually running the script when debugging.
156 if os.path.ismount(path):
157 logging.warning('Ignoring mounted dir for subdir check: %s', path)
158 return False
159
160 if os.path.join(os.getenv('SYSROOT', '/'), 'tmp') == path:
161 logging.warning('Ignoring live dir: %s', path)
162 return False
163
164 for _, dirs, _ in os.walk(path):
165 if dirs:
166 logging.error('Subdirs found in a dir that should be empty:\n %s\n'
167 ' |-- %s', path, '\n |-- '.join(sorted(dirs)))
168 return True
169 break
170
171 return False
172
173
174def check_usr(usr, host=False):
175 """Check the /usr filesystem at |usr|."""
176 ret = True
177
178 # Not all packages install into /usr.
179 if not os.path.exists(usr):
180 return ret
181
182 atom = get_current_package()
183 paths = set(os.listdir(usr))
184 unknown = paths - VALID_USR
185 for target in KNOWN_TARGETS:
186 unknown = set(x for x in unknown if not fnmatch.fnmatch(x, target))
187 if host:
188 unknown -= VALID_HOST_USR
189
190 if atom in BAD_HOST_USR_LOCAL_PACKAGES:
191 logging.warning('Ignoring known bad /usr/local install for now')
192 unknown -= {'local'}
193 else:
194 unknown -= VALID_BOARD_USR
195
196 if atom in {'chromeos-base/ap-daemons'}:
197 logging.warning('Ignoring known bad /usr install for now')
198 unknown -= {'www'}
199
200 if unknown:
201 logging.error('Paths are not allowed in the /usr dir: %s', sorted(unknown))
202 ret = False
203
204 for path in NOSUBDIRS_USR:
205 if has_subdirs(os.path.join(usr, path)):
206 ret = False
207
208 return ret
209
210
211def check_root(root, host=False):
212 """Check the filesystem |root|."""
213 ret = True
214
215 atom = get_current_package()
216 paths = set(os.listdir(root))
217 unknown = paths - VALID_ROOT
218 if host:
219 unknown -= VALID_HOST_ROOT
220 else:
221 unknown -= VALID_BOARD_ROOT
222
223 if atom in BAD_ROOT_PACKAGES:
224 logging.warning('Ignoring known bad / install for now')
225 elif unknown:
226 logging.error('Paths are not allowed in the root dir:\n %s\n |-- %s',
227 root, '\n |-- '.join(sorted(unknown)))
228 ret = False
229
230 # Some of these may have subdirs at runtime, but not from package installs.
231 for path in NOSUBDIRS_ROOT:
232 if has_subdirs(os.path.join(root, path)):
233 if path == 'tmp' and atom in BAD_TMP_PACKAGES:
234 logging.warning('Ignoring known bad /tmp install for now')
235 else:
236 ret = False
237
238 # Special case /var due to so many misuses currently.
239 if has_subdirs(os.path.join(root, 'var')):
240 if atom in BAD_VAR_PACKAGES:
241 logging.warning('Ignoring known bad /var install for now')
242 elif os.environ.get('PORTAGE_REPO_NAME') == 'portage-stable':
243 logging.warning('Ignoring bad /var install with portage-stable package '
244 'for now')
245 else:
246 ret = False
247 else:
248 if atom in BAD_VAR_PACKAGES:
249 logging.warning('Package has improved; please update BAD_VAR_PACKAGES')
250
251 # Special case /run due to so many misuses currently.
252 if has_subdirs(os.path.join(root, 'run')):
253 if atom in BAD_RUN_PACKAGES:
254 logging.warning('Ignoring known bad /run install for now')
255 elif os.environ.get('PORTAGE_REPO_NAME') == 'portage-stable':
256 logging.warning('Ignoring bad /run install with portage-stable package '
257 'for now')
258 else:
259 ret = False
260 else:
261 if atom in BAD_RUN_PACKAGES:
262 logging.warning('Package has improved; please update BAD_RUN_PACKAGES')
263
264 if not check_usr(os.path.join(root, 'usr'), host):
265 ret = False
266
267 return ret
268
269
270def get_current_package():
271 """Figure out what package is being built currently."""
272 if 'CATEGORY' in os.environ and 'PN' in os.environ:
273 return f'{os.environ.get("CATEGORY")}/{os.environ.get("PN")}'
274 else:
275 return None
276
277
278def get_parser():
279 """Get a CLI parser."""
280 parser = argparse.ArgumentParser(description=__doc__)
281 parser.add_argument('--host', default=None, action='store_true',
282 help='the filesystem is the host SDK, not board sysroot')
283 parser.add_argument('--board', dest='host', action='store_false',
284 help='the filesystem is a board sysroot')
285 parser.add_argument('root', nargs='?',
286 help='the rootfs to scan')
287 return parser
288
289
290def main(argv):
291 """The main func!"""
292 parser = get_parser()
293 opts = parser.parse_args(argv)
294
295 # Default to common portage env vars.
296 if opts.root is None:
297 for var in ('ED', 'D', 'ROOT'):
298 if var in os.environ:
299 logging.debug('Scanning filesystem root via $%s', var)
300 opts.root = os.environ[var]
301 break
302 if not opts.root:
303 parser.error('Need a valid rootfs to scan, but unable to detect one')
304
305 if opts.host is None:
306 if os.getenv('BOARD') == 'amd64-host':
307 opts.host = True
308 else:
309 opts.host = not bool(os.getenv('SYSROOT'))
310
311 if not check_root(opts.root, opts.host):
312 logging.critical(
313 "This package does not conform to CrOS's filesystem conventions. "
314 'Please review the paths flagged above and adjust its layout.')
315 return 1
316 else:
317 return 0
318
319
320if __name__ == '__main__':
321 sys.exit(main(sys.argv[1:]))