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 10, 2023
1 parent 401cf4e commit ed4546c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 19 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.
"""
47 changes: 30 additions & 17 deletions rpmlint/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ def _gather_dep_info(self):
return (_requires, _prereq, _provides, _conflicts, _obsoletes, _recommends,
_suggests, _enhances, _supplements)

def scriptprog(self, which):
"""
Get the specified script interpreter as a string.
Depending on rpm-python version, the string may or may not include
interpreter arguments, if any.
"""
if which is None:
return ''
prog = self[which]
if prog is None:
prog = ''
elif isinstance(prog, (list, tuple)):
# http://rpm.org/ticket/847#comment:2
prog = ''.join(prog)
return prog

def __enter__(self):
return self

Expand Down Expand Up @@ -704,22 +720,6 @@ def check_versioned_dep(self, name, version):
return True
return False

def scriptprog(self, which):
"""
Get the specified script interpreter as a string.
Depending on rpm-python version, the string may or may not include
interpreter arguments, if any.
"""
if which is None:
return ''
prog = self[which]
if prog is None:
prog = ''
elif isinstance(prog, (list, tuple)):
# http://rpm.org/ticket/847#comment:2
prog = ''.join(prog)
return prog


def get_installed_pkgs(name):
"""Get list of installed package objects by name."""
Expand Down Expand Up @@ -796,6 +796,7 @@ def __init__(self, name, is_source=False):
self.header[getattr(rpm, f'RPMTAG_{tagname}NAME')] = []
self.header[getattr(rpm, f'RPMTAG_{tagname}FLAGS')] = []
self.header[getattr(rpm, f'RPMTAG_{tagname}VERSION')] = []
self.header[rpm.RPMTAG_FILENAMES] = []

def add_file(self, path, name):
pkgfile = PkgFile(name)
Expand All @@ -816,7 +817,11 @@ def _mock_file(self, path, attrs):
elif 'content' in attrs:
content = attrs['content']

self.add_file_with_content(path, content, metadata=metadata)
if 'linkto' in attrs:
self.add_symlink_to(path, attrs['linkto'])
else:
self.add_file_with_content(path, content, metadata=metadata)
self.header[rpm.RPMTAG_FILENAMES].append(path)

if 'content-path' in attrs:
content.close()
Expand Down Expand Up @@ -852,6 +857,8 @@ def add_file_with_content(self, name, content, metadata=None, **flags):
pkg_file = PkgFile(name)
pkg_file.path = path
pkg_file.mode = stat.S_IFREG | 0o0644
pkg_file.user = 'root'
pkg_file.group = 'root'
self.files[name] = pkg_file

# create files in filesystem
Expand Down Expand Up @@ -915,6 +922,8 @@ def add_symlink_to(self, name, target):
pkg_file = PkgFile(name)
pkg_file.mode = stat.S_IFLNK
pkg_file.linkto = target
pkg_file.user = 'root'
pkg_file.group = 'root'
self.files[name] = pkg_file

def readlink(self, pkgfile):
Expand All @@ -937,3 +946,7 @@ def md5_checksum(self, file_name):
def cleanup(self):
if self.dirname:
self.__tmpdir.cleanup()

# access the tags like an array
def __getitem__(self, key):
return self.header.get(key, None)
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 ed4546c

Please sign in to comment.