| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # Copyright OpenEmbedded Contributors | 
|  | 4 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only | 
|  | 6 | # | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 7 |  | 
|  | 8 | import sys, os, subprocess, re, shutil | 
|  | 9 |  | 
| Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 10 | allowed = ( | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | # type is supported by dash | 
|  | 12 | 'if type systemctl >/dev/null 2>/dev/null; then', | 
|  | 13 | 'if type systemd-tmpfiles >/dev/null 2>/dev/null; then', | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 14 | 'type update-rc.d >/dev/null 2>/dev/null; then', | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | 'command -v', | 
|  | 16 | # HOSTNAME is set locally | 
|  | 17 | 'buildhistory_single_commit "$CMDLINE" "$HOSTNAME"', | 
|  | 18 | # False-positive, match is a grep not shell expression | 
|  | 19 | 'grep "^$groupname:[^:]*:[^:]*:\\([^,]*,\\)*$username\\(,[^,]*\\)*"', | 
|  | 20 | # TODO verify dash's '. script args' behaviour | 
|  | 21 | '. $target_sdk_dir/${oe_init_build_env_path} $target_sdk_dir >> $LOGFILE' | 
|  | 22 | ) | 
|  | 23 |  | 
| Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 24 | def is_allowed(s): | 
|  | 25 | for w in allowed: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 26 | if w in s: | 
|  | 27 | return True | 
|  | 28 | return False | 
|  | 29 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 30 | SCRIPT_LINENO_RE = re.compile(r' line (\d+) ') | 
|  | 31 | BASHISM_WARNING = re.compile(r'^(possible bashism in.*)$', re.MULTILINE) | 
|  | 32 |  | 
|  | 33 | def process(filename, function, lineno, script): | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 34 | import tempfile | 
|  | 35 |  | 
|  | 36 | if not script.startswith("#!"): | 
|  | 37 | script = "#! /bin/sh\n" + script | 
|  | 38 |  | 
|  | 39 | fn = tempfile.NamedTemporaryFile(mode="w+t") | 
|  | 40 | fn.write(script) | 
|  | 41 | fn.flush() | 
|  | 42 |  | 
|  | 43 | try: | 
|  | 44 | subprocess.check_output(("checkbashisms.pl", fn.name), universal_newlines=True, stderr=subprocess.STDOUT) | 
|  | 45 | # No bashisms, so just return | 
|  | 46 | return | 
|  | 47 | except subprocess.CalledProcessError as e: | 
|  | 48 | # TODO check exit code is 1 | 
|  | 49 |  | 
|  | 50 | # Replace the temporary filename with the function and split it | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 51 | output = e.output.replace(fn.name, function) | 
|  | 52 | if not output or not output.startswith('possible bashism'): | 
|  | 53 | # Probably starts with or contains only warnings. Dump verbatim | 
| Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 54 | # with one space indention. Can't do the splitting and allowed | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | # checking below. | 
|  | 56 | return '\n'.join([filename, | 
|  | 57 | ' Unexpected output from checkbashisms.pl'] + | 
|  | 58 | [' ' + x for x in output.splitlines()]) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 59 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 60 | # We know that the first line matches and that therefore the first | 
|  | 61 | # list entry will be empty - skip it. | 
|  | 62 | output = BASHISM_WARNING.split(output)[1:] | 
|  | 63 | # Turn the output into a single string like this: | 
|  | 64 | # /.../foobar.bb | 
|  | 65 | #  possible bashism in updatercd_postrm line 2 (type): | 
|  | 66 | #   if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then | 
|  | 67 | #  ... | 
|  | 68 | #   ... | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 69 | result = [] | 
| Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 70 | # Check the results against the allowed list | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | for message, source in zip(output[0::2], output[1::2]): | 
|  | 72 | if not is_whitelisted(source): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 73 | if lineno is not None: | 
|  | 74 | message = SCRIPT_LINENO_RE.sub(lambda m: ' line %d ' % (int(m.group(1)) + int(lineno) - 1), | 
|  | 75 | message) | 
|  | 76 | result.append(' ' + message.strip()) | 
|  | 77 | result.extend(['  %s' % x for x in source.splitlines()]) | 
|  | 78 | if result: | 
|  | 79 | result.insert(0, filename) | 
|  | 80 | return '\n'.join(result) | 
|  | 81 | else: | 
|  | 82 | return None | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 83 |  | 
|  | 84 | def get_tinfoil(): | 
|  | 85 | scripts_path = os.path.dirname(os.path.realpath(__file__)) | 
|  | 86 | lib_path = scripts_path + '/lib' | 
|  | 87 | sys.path = sys.path + [lib_path] | 
|  | 88 | import scriptpath | 
|  | 89 | scriptpath.add_bitbake_lib_path() | 
|  | 90 | import bb.tinfoil | 
|  | 91 | tinfoil = bb.tinfoil.Tinfoil() | 
|  | 92 | tinfoil.prepare() | 
|  | 93 | # tinfoil.logger.setLevel(logging.WARNING) | 
|  | 94 | return tinfoil | 
|  | 95 |  | 
|  | 96 | if __name__=='__main__': | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 97 | import argparse, shutil | 
|  | 98 |  | 
|  | 99 | parser = argparse.ArgumentParser(description='Bashim detector for shell fragments in recipes.') | 
|  | 100 | parser.add_argument("recipes", metavar="RECIPE", nargs="*", help="recipes to check (if not specified, all will be checked)") | 
|  | 101 | parser.add_argument("--verbose", default=False, action="store_true") | 
|  | 102 | args = parser.parse_args() | 
|  | 103 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 104 | if shutil.which("checkbashisms.pl") is None: | 
| Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 105 | print("Cannot find checkbashisms.pl on $PATH, get it from https://salsa.debian.org/debian/devscripts/raw/master/scripts/checkbashisms.pl") | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | sys.exit(1) | 
|  | 107 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 108 | # The order of defining the worker function, | 
|  | 109 | # initializing the pool and connecting to the | 
|  | 110 | # bitbake server is crucial, don't change it. | 
|  | 111 | def func(item): | 
|  | 112 | (filename, key, lineno), script = item | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 113 | if args.verbose: | 
|  | 114 | print("Scanning %s:%s" % (filename, key)) | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 115 | return process(filename, key, lineno, script) | 
|  | 116 |  | 
|  | 117 | import multiprocessing | 
|  | 118 | pool = multiprocessing.Pool() | 
|  | 119 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 120 | tinfoil = get_tinfoil() | 
|  | 121 |  | 
|  | 122 | # This is only the default configuration and should iterate over | 
|  | 123 | # recipecaches to handle multiconfig environments | 
|  | 124 | pkg_pn = tinfoil.cooker.recipecaches[""].pkg_pn | 
|  | 125 |  | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 126 | if args.recipes: | 
|  | 127 | initial_pns = args.recipes | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 128 | else: | 
|  | 129 | initial_pns = sorted(pkg_pn) | 
|  | 130 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 131 | pns = set() | 
|  | 132 | scripts = {} | 
|  | 133 | print("Generating scripts...") | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 134 | for pn in initial_pns: | 
|  | 135 | for fn in pkg_pn[pn]: | 
|  | 136 | # There's no point checking multiple BBCLASSEXTENDed variants of the same recipe | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | # (at least in general - there is some risk that the variants contain different scripts) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 138 | realfn, _, _ = bb.cache.virtualfn2realfn(fn) | 
|  | 139 | if realfn not in pns: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 140 | pns.add(realfn) | 
|  | 141 | data = tinfoil.parse_recipe_file(realfn) | 
|  | 142 | for key in data.keys(): | 
|  | 143 | if data.getVarFlag(key, "func") and not data.getVarFlag(key, "python"): | 
|  | 144 | script = data.getVar(key, False) | 
|  | 145 | if script: | 
|  | 146 | filename = data.getVarFlag(key, "filename") | 
|  | 147 | lineno = data.getVarFlag(key, "lineno") | 
|  | 148 | # There's no point in checking a function multiple | 
|  | 149 | # times just because different recipes include it. | 
|  | 150 | # We identify unique scripts by file, name, and (just in case) | 
|  | 151 | # line number. | 
|  | 152 | attributes = (filename or realfn, key, lineno) | 
|  | 153 | scripts.setdefault(attributes, script) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 154 |  | 
|  | 155 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 156 | print("Scanning scripts...\n") | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 157 | for result in pool.imap(func, scripts.items()): | 
|  | 158 | if result: | 
|  | 159 | print(result) | 
|  | 160 | tinfoil.shutdown() |