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