blob: e13ee72df8e0c7184be705eb09fddfa7b3841a7d [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',
Aileen Cheng3b891332020-03-03 09:03:23 -080077 # TODO(b/150694105): Delete this.
78 'net-wireless/wpantund',
Mike Frysingerb9743c62020-02-20 02:53:55 -050079}
80
81# These SDK packages need cleanup.
82# NB: Do *not* add more packages here.
83BAD_HOST_USR_LOCAL_PACKAGES = {
84 'app-crypt/nss',
85}
86
87# Ignore some packages installing into /var for now.
88# NB: Do *not* add more packages here.
89BAD_VAR_PACKAGES = {
90 'app-accessibility/brltty',
91 'app-admin/eselect',
92 'app-admin/puppet',
93 'app-admin/rsyslog',
94 'app-admin/sudo',
95 'app-admin/sysstat',
96 'app-admin/webapp-config',
97 'app-crypt/mit-krb5',
98 'app-crypt/trousers',
99 'app-emulation/containerd',
100 'app-emulation/lxc',
101 'chromeos-base/chromeos-initramfs',
102 # https://crbug.com/1054646
103 'chromeos-base/devserver',
104 # https://crbug.com/1007402
105 'chromeos-base/factory',
106 'chromeos-base/factory-board',
107 'dev-python/django',
108 'media-gfx/sane-backends',
109 'media-sound/alsa-utils',
110 'net-analyzer/netperf',
111 'net-dns/dnsmasq',
Aileen Cheng3b891332020-03-03 09:03:23 -0800112 # TODO(b/150694545): Delete this.
Aileen Chengc1e163a2020-03-02 14:47:00 -0800113 'net-firewall/ebtables',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500114 'net-firewall/iptables',
115 'net-fs/samba',
Mike Frysinger0669e002020-03-02 16:56:24 -0500116 'net-misc/chrony',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500117 'net-misc/dhcpcd',
118 'net-misc/openssh',
119 'net-print/cups',
120 'sys-apps/dbus',
121 'sys-apps/fwupd',
122 'sys-apps/iproute2',
123 'sys-apps/journald',
124 'sys-apps/portage',
125 'sys-apps/sandbox',
126 'sys-apps/systemd',
127 'sys-apps/usbguard',
128 'sys-kernel/loonix-initramfs',
129 'sys-libs/glibc',
130 'sys-process/audit',
131 'www-servers/nginx',
132 'x11-base/xwayland',
133}
134
135# Ignore some packages installing into /run for now.
136# NB: Do *not* add more packages here.
137BAD_RUN_PACKAGES = {
138 'app-accessibility/brltty',
139 'net-fs/samba',
140}
141
142# Ignore some packages installing into /tmp for now.
143# NB: Do *not* add more packages here.
144BAD_TMP_PACKAGES = {
145 # https://crbug.com/1057059
146 'chromeos-base/chromeos-bsp-caroline-private',
Mike Frysinger833a8222020-03-02 14:04:36 -0500147 'chromeos-base/chromeos-bsp-elm-private',
Mike Frysinger7dd8d682020-03-03 11:18:29 -0500148 'chromeos-base/chromeos-config-bsp',
Mike Frysinger833a8222020-03-02 14:04:36 -0500149 'chromeos-base/chromeos-config-bsp-coral',
150 'chromeos-base/chromeos-config-bsp-coral-private',
151 'chromeos-base/chromeos-config-bsp-nami',
152 'chromeos-base/chromeos-config-bsp-scarlet-private',
Mike Frysingerb9743c62020-02-20 02:53:55 -0500153 'chromeos-base/cros-config-test',
154}
155
156
157def has_subdirs(path):
158 """See if |path| has any subdirs."""
159 # These checks are helpful for manually running the script when debugging.
160 if os.path.ismount(path):
161 logging.warning('Ignoring mounted dir for subdir check: %s', path)
162 return False
163
164 if os.path.join(os.getenv('SYSROOT', '/'), 'tmp') == path:
165 logging.warning('Ignoring live dir: %s', path)
166 return False
167
168 for _, dirs, _ in os.walk(path):
169 if dirs:
170 logging.error('Subdirs found in a dir that should be empty:\n %s\n'
171 ' |-- %s', path, '\n |-- '.join(sorted(dirs)))
172 return True
173 break
174
175 return False
176
177
178def check_usr(usr, host=False):
179 """Check the /usr filesystem at |usr|."""
180 ret = True
181
182 # Not all packages install into /usr.
183 if not os.path.exists(usr):
184 return ret
185
186 atom = get_current_package()
187 paths = set(os.listdir(usr))
188 unknown = paths - VALID_USR
189 for target in KNOWN_TARGETS:
190 unknown = set(x for x in unknown if not fnmatch.fnmatch(x, target))
191 if host:
192 unknown -= VALID_HOST_USR
193
194 if atom in BAD_HOST_USR_LOCAL_PACKAGES:
195 logging.warning('Ignoring known bad /usr/local install for now')
196 unknown -= {'local'}
197 else:
198 unknown -= VALID_BOARD_USR
199
200 if atom in {'chromeos-base/ap-daemons'}:
201 logging.warning('Ignoring known bad /usr install for now')
202 unknown -= {'www'}
203
204 if unknown:
205 logging.error('Paths are not allowed in the /usr dir: %s', sorted(unknown))
206 ret = False
207
208 for path in NOSUBDIRS_USR:
209 if has_subdirs(os.path.join(usr, path)):
210 ret = False
211
212 return ret
213
214
215def check_root(root, host=False):
216 """Check the filesystem |root|."""
217 ret = True
218
219 atom = get_current_package()
220 paths = set(os.listdir(root))
221 unknown = paths - VALID_ROOT
222 if host:
223 unknown -= VALID_HOST_ROOT
224 else:
225 unknown -= VALID_BOARD_ROOT
226
227 if atom in BAD_ROOT_PACKAGES:
228 logging.warning('Ignoring known bad / install for now')
229 elif unknown:
230 logging.error('Paths are not allowed in the root dir:\n %s\n |-- %s',
231 root, '\n |-- '.join(sorted(unknown)))
232 ret = False
233
234 # Some of these may have subdirs at runtime, but not from package installs.
235 for path in NOSUBDIRS_ROOT:
236 if has_subdirs(os.path.join(root, path)):
237 if path == 'tmp' and atom in BAD_TMP_PACKAGES:
238 logging.warning('Ignoring known bad /tmp install for now')
239 else:
240 ret = False
241
242 # Special case /var due to so many misuses currently.
243 if has_subdirs(os.path.join(root, 'var')):
244 if atom in BAD_VAR_PACKAGES:
245 logging.warning('Ignoring known bad /var install for now')
246 elif os.environ.get('PORTAGE_REPO_NAME') == 'portage-stable':
247 logging.warning('Ignoring bad /var install with portage-stable package '
248 'for now')
249 else:
250 ret = False
251 else:
252 if atom in BAD_VAR_PACKAGES:
253 logging.warning('Package has improved; please update BAD_VAR_PACKAGES')
254
255 # Special case /run due to so many misuses currently.
256 if has_subdirs(os.path.join(root, 'run')):
257 if atom in BAD_RUN_PACKAGES:
258 logging.warning('Ignoring known bad /run install for now')
259 elif os.environ.get('PORTAGE_REPO_NAME') == 'portage-stable':
260 logging.warning('Ignoring bad /run install with portage-stable package '
261 'for now')
262 else:
263 ret = False
264 else:
265 if atom in BAD_RUN_PACKAGES:
266 logging.warning('Package has improved; please update BAD_RUN_PACKAGES')
267
268 if not check_usr(os.path.join(root, 'usr'), host):
269 ret = False
270
271 return ret
272
273
274def get_current_package():
275 """Figure out what package is being built currently."""
276 if 'CATEGORY' in os.environ and 'PN' in os.environ:
277 return f'{os.environ.get("CATEGORY")}/{os.environ.get("PN")}'
278 else:
279 return None
280
281
282def get_parser():
283 """Get a CLI parser."""
284 parser = argparse.ArgumentParser(description=__doc__)
285 parser.add_argument('--host', default=None, action='store_true',
286 help='the filesystem is the host SDK, not board sysroot')
287 parser.add_argument('--board', dest='host', action='store_false',
288 help='the filesystem is a board sysroot')
289 parser.add_argument('root', nargs='?',
290 help='the rootfs to scan')
291 return parser
292
293
294def main(argv):
295 """The main func!"""
296 parser = get_parser()
297 opts = parser.parse_args(argv)
298
299 # Default to common portage env vars.
300 if opts.root is None:
301 for var in ('ED', 'D', 'ROOT'):
302 if var in os.environ:
303 logging.debug('Scanning filesystem root via $%s', var)
304 opts.root = os.environ[var]
305 break
306 if not opts.root:
307 parser.error('Need a valid rootfs to scan, but unable to detect one')
308
309 if opts.host is None:
310 if os.getenv('BOARD') == 'amd64-host':
311 opts.host = True
312 else:
313 opts.host = not bool(os.getenv('SYSROOT'))
314
315 if not check_root(opts.root, opts.host):
316 logging.critical(
317 "This package does not conform to CrOS's filesystem conventions. "
318 'Please review the paths flagged above and adjust its layout.')
319 return 1
320 else:
321 return 0
322
323
324if __name__ == '__main__':
325 sys.exit(main(sys.argv[1:]))