Skip to content

Commit

Permalink
Add new check to detect links to scripts
Browse files Browse the repository at this point in the history
This new check looks for links in /usr/bin that points to some script in
a non bin path that contains a shebang, and checks if the needed
interpreter is included as a requirement, because in that case rpm won't
be able to inject the requirement by itself.

Fix #1120
  • Loading branch information
danigm committed Oct 11, 2023
1 parent ea3d585 commit d69741f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
30 changes: 30 additions & 0 deletions rpmlint/checks/FilesCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,35 @@ def _check_file_link_relative(self, pkg, fname, pkgfile):
'symlink-contains-up-and-down-segments',
fname, link)

def _check_file_link_bindir_shebang(self, pkg, fname, pkgfile):
basedir = Path(fname).parent
linkto = str((basedir / Path(pkgfile.linkto)).resolve())
# Link to a file not in the package, so ignore
if linkto not in pkg.files:
return

realbin = pkg.files[linkto]
# Link to something in bindir is okay
if bin_regex.search(realbin.name):
return
if not stat.S_ISREG(realbin.mode):
return

file_chunk, file_istext = self.peek(realbin.path, pkg)
file_interpreter, _file_interpreter_args = script_interpreter(file_chunk)
# Not a script with shebang, so ignore
if not file_interpreter:
return

# If the shebang interpreter is a dependency, it's okay
deps = [x[0] for x in pkg.requires]
if file_interpreter in deps:
return

self.output.add_info('W', pkg, 'symlink-to-binary-with-shebang', fname,
f'is a link to a script ({realbin.name}) but missing'
f' requires for {file_interpreter}')

def _check_file_link(self, pkg, fname, pkgfile):
if not stat.S_ISLNK(pkgfile.mode):
return
Expand All @@ -871,6 +900,7 @@ def _check_file_link(self, pkg, fname, pkgfile):
self._check_file_link_bindir_exes(pkg, fname)
self._check_file_link_absolute(pkg, fname, pkgfile)
self._check_file_link_relative(pkg, fname, pkgfile)
self._check_file_link_bindir_shebang(pkg, fname, pkgfile)

def _check_file_dir(self, pkg, fname, pkgfile):
if not stat.S_ISDIR(pkgfile.mode):
Expand Down
6 changes: 6 additions & 0 deletions rpmlint/descriptions/FilesCheck.toml
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,9 @@ manual-page-in-subfolder="""
Manual page should not be placed in a subfolder of a manual section
directory.
"""

symlink-to-binary-with-shebang="""
A file in /usr/bin is a link to a script in a different place with a shebang.
rpm won't be able to inject the needed interpreter as dependency, so it should
be done manually.
"""
60 changes: 58 additions & 2 deletions test/test_files.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import stat

import pytest
from rpmlint.checks.FilesCheck import FilesCheck
from rpmlint.checks.FilesCheck import pyc_magic_from_chunk, pyc_mtime_from_chunk
from rpmlint.checks.FilesCheck import python_bytecode_to_script as pbts
from rpmlint.checks.FilesCheck import script_interpreter as se
from rpmlint.filter import Filter

from Testing import CONFIG, get_tested_package, get_tested_path
from Testing import CONFIG, get_tested_mock_package, get_tested_package, get_tested_path


@pytest.fixture(scope='function', autouse=True)
def filescheck():
CONFIG.info = True
output = Filter(CONFIG)
test = FilesCheck(CONFIG, output)
return output, test
yield output, test


@pytest.fixture
def output(filescheck):
output, _test = filescheck
yield output


@pytest.fixture
def test(filescheck):
_output, test = filescheck
yield test


def test_pep3147():
Expand Down Expand Up @@ -244,3 +258,45 @@ def test_manual_pages(tmp_path, package, filescheck):
assert 'W: manpage-not-compressed bz2 /usr/share/man/man1/test.1.zst' in out
assert 'E: bad-manual-page-folder /usr/share/man/man0p/foo.3.gz expected folder: man3' in out
assert 'bad-manual-page-folder /usr/share/man/man3/some.3pm.gz' not in out


@pytest.mark.parametrize('package', [
get_tested_mock_package(
files={
'/usr/share/package/bin.py': {
'content': '#!/usr/bin/python3\nprint("python required")',
'metadata': {'mode': 0o755 | stat.S_IFREG},
},
'/usr/bin/testlink': {
'linkto': '../share/package/bin.py',
},
},
header={},
),
])
def test_shebang(package, output, test):
test.check(package)
out = output.print_results(output.results)
assert 'W: symlink-to-binary-with-shebang /usr/bin/testlink' in out


@pytest.mark.parametrize('package', [
get_tested_mock_package(
files={
'/usr/share/package/bin.py': {
'content': '#!/usr/bin/python3\nprint("python required")',
'metadata': {'mode': 0o755 | stat.S_IFREG},
},
'/usr/bin/testlink': {
'linkto': '../share/package/bin.py',
},
},
header={
'requires': ['/usr/bin/python3'],
},
),
])
def test_shebang_ok(package, output, test):
test.check(package)
out = output.print_results(output.results)
assert 'W: symlink-to-binary-with-shebang /usr/bin/testlink' not in out

0 comments on commit d69741f

Please sign in to comment.