-
-
Notifications
You must be signed in to change notification settings - Fork 137
devel: add reposec management command #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jelly
wants to merge
3
commits into
archlinux:master
Choose a base branch
from
jelly:repro_status
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
""" | ||
reposec command | ||
|
||
Parses all packages in a given repo and creates PackageSecurity | ||
objects which check for PIE, RELRO, Stack Canary's and Fortify. | ||
|
||
Usage: ./manage.py reposec ARCH PATH | ||
ARCH: architecture to check | ||
PATH: full path to the repository directory. | ||
|
||
Example: | ||
./manage.py reposec x86_64 /srv/ftp/core | ||
""" | ||
|
||
import io | ||
import os | ||
import re | ||
import sys | ||
import logging | ||
|
||
from glob import glob | ||
from multiprocessing import Pool, cpu_count | ||
|
||
from elftools.elf.constants import P_FLAGS | ||
from elftools.elf.dynamic import DynamicSection | ||
from elftools.elf.elffile import ELFFile | ||
from elftools.elf.sections import SymbolTableSection | ||
from libarchive import file_reader | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from django.db import transaction | ||
|
||
from main.models import Arch, Package, PackageSecurity, Repo | ||
|
||
|
||
PKG_EXT = '.tar.xz' # TODO: detect zstd.. | ||
BIN_PATHS = ['usr/bin/', 'opt/'] | ||
STACK_CHK = set(["__stack_chk_fail", "__stack_smash_handler"]) | ||
|
||
|
||
logging.basicConfig( | ||
level=logging.WARNING, | ||
format='%(asctime)s -> %(levelname)s: %(message)s', | ||
datefmt='%Y-%m-%d %H:%M:%S', | ||
stream=sys.stderr) | ||
TRACE = 5 | ||
logging.addLevelName(TRACE, 'TRACE') | ||
logger = logging.getLogger() | ||
|
||
class Command(BaseCommand): | ||
help = "Checks all packages in the repository for missing hardening bits (relro, stack canary, pie, etc)" | ||
missing_args_message = 'missing arch and file.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('args', nargs='*', help='<arch> <filename>') | ||
parser.add_argument('--processes', | ||
action='store_true', | ||
dest='processes', | ||
default=cpu_count(), | ||
help=f'number of parallel processes (default: {cpu_count()})') | ||
|
||
|
||
def handle(self, arch=None, directory=None, processes=cpu_count(), **options): | ||
if not arch: | ||
raise CommandError('Architecture is required.') | ||
if not directory: | ||
raise CommandError('Repo location is required.') | ||
directory = os.path.normpath(directory) | ||
if not os.path.exists(directory): | ||
raise CommandError('Specified repository location does not exists.') | ||
|
||
v = int(options.get('verbosity', 0)) | ||
if v == 0: | ||
logger.level = logging.ERROR | ||
elif v == 1: | ||
logger.level = logging.INFO | ||
elif v >= 2: | ||
logger.level = logging.DEBUG | ||
|
||
return read_repo(arch, directory, processes, options) | ||
|
||
|
||
def read_file(filename): | ||
''' | ||
Read a pacman package and determine the 'package security status' by | ||
finding all ELF files and determining the worst status of all ELF files in | ||
the repo. | ||
''' | ||
|
||
elffiles = [] | ||
|
||
with file_reader(filename) as pkg: | ||
pkgname = os.path.basename(filename).rsplit('-', 3)[0] | ||
|
||
for entry in pkg: | ||
if not entry.isfile: | ||
continue | ||
|
||
if not any(entry.name.startswith(path) for path in BIN_PATHS): | ||
continue | ||
|
||
fp = io.BytesIO(b''.join(entry.get_blocks())) | ||
elf = Elf(entry.name, fp) | ||
|
||
if not elf.is_elf(): | ||
continue | ||
|
||
if elf.hardened: | ||
continue | ||
|
||
data = elf.dump() | ||
data['pkgname'] = pkgname | ||
elffiles.append(data) | ||
|
||
return elffiles | ||
|
||
|
||
def read_repo(arch, source_dir, processes, options): | ||
tasks = [] | ||
|
||
directory = os.path.join(source_dir, 'os', arch) | ||
for filename in glob(os.path.join(directory, f'*{PKG_EXT}')): | ||
tasks.append((filename)) | ||
|
||
arch = Arch.objects.get(name=arch) | ||
|
||
reponame = os.path.basename(source_dir).title() | ||
repo = Repo.objects.get(name=reponame) | ||
|
||
packagesecs = [] | ||
|
||
with Pool(processes=processes) as pool: | ||
for results in pool.imap_unordered(read_file, tasks): | ||
# No elf files | ||
if not results: | ||
continue | ||
|
||
# determine | ||
print(results) | ||
for result in results: | ||
try: | ||
pkg = Package.objects.get(pkgname=result['pkgname'], arch=arch, repo=repo) | ||
except Exception: | ||
print("package '%s' not found in repo" % result['pkgname']) | ||
continue | ||
|
||
print(result) | ||
packagesec = PackageSecurity(pkg=pkg, relro=result['relro'], | ||
pie=result['pie'], canary=result['canary'], | ||
filename=result['filename']) | ||
packagesecs.append(packagesec) | ||
|
||
print(packagesecs) | ||
with transaction.atomic(): | ||
PackageSecurity.objects.bulk_create(packagesecs) | ||
|
||
|
||
class Elf: | ||
def __init__(self, filename, fileobj): | ||
self.filename = filename | ||
self.fileobj = fileobj | ||
self._elffile = None | ||
|
||
@property | ||
def elffile(self): | ||
if not self._elffile: | ||
self._elffile = ELFFile(self.fileobj) | ||
return self._elffile | ||
|
||
def is_elf(self): | ||
"Take file object, peek at the magic bytes to check if ELF file." | ||
magic_bytes = b"\x7fELF" | ||
length = len(magic_bytes) | ||
magic = self.fileobj.read(length) | ||
self.fileobj.seek(0) | ||
return magic == magic_bytes | ||
|
||
def dynamic_tags(self, key): | ||
for section in self.elffile.iter_sections(): | ||
if not isinstance(section, DynamicSection): | ||
continue | ||
for tag in section.iter_tags(): | ||
if tag.entry.d_tag == key: | ||
return tag | ||
return None | ||
|
||
@property | ||
def rpath(self, key="DT_RPATH", verbose=False): | ||
tag = self.dynamic_tags(key) | ||
if tag and verbose: | ||
return tag.rpath | ||
if tag: | ||
return 'RPATH' | ||
return '' | ||
|
||
@property | ||
def runpath(self, key="DT_RUNPATH", verbose=False): | ||
tag = self.dynamic_tags(key) | ||
if tag and verbose: | ||
return tag.runpath | ||
if tag: | ||
return 'RUNPATH' | ||
|
||
return '' | ||
|
||
@property | ||
def relro(self): | ||
if self.elffile.num_segments() == 0: | ||
return PackageSecurity.NO_RELRO | ||
|
||
have_relro = PackageSecurity.NO_RELRO | ||
for segment in self.elffile.iter_segments(): | ||
if re.search("GNU_RELRO", str(segment['p_type'])): | ||
have_relro = PackageSecurity.FULL_RELRO | ||
break | ||
|
||
if self.dynamic_tags("DT_BIND_NOW") and have_relro: | ||
return PackageSecurity.FULL_RELRO | ||
if have_relro: # partial | ||
return PackageSecurity.PARTIAL_RELRO | ||
return PackageSecurity.FULL_RELRO | ||
|
||
@property | ||
def pie(self): | ||
header = self.elffile.header | ||
if self.dynamic_tags("EXEC"): | ||
return False | ||
if "ET_DYN" in header['e_type']: | ||
if self.dynamic_tags("DT_DEBUG"): | ||
return True | ||
return True # DSO is PIE | ||
return False | ||
|
||
@property | ||
def canary(self): | ||
for section in self.elffile.iter_sections(): | ||
if not isinstance(section, SymbolTableSection): | ||
continue | ||
if section['sh_entsize'] == 0: | ||
continue | ||
for _, symbol in enumerate(section.iter_symbols()): | ||
if symbol.name in STACK_CHK: | ||
return True | ||
return False | ||
|
||
@property | ||
def hardened(self): | ||
return self.pie and self.canary and self.relro == PackageSecurity.FULL_RELRO | ||
|
||
def dump(self): | ||
return { | ||
'pie': self.pie, | ||
'relro': self.relro, | ||
'canary': self.canary, | ||
'filename': self.filename | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 2.2.5 on 2019-10-09 19:24 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0002_repo_public_testing'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='PackageSecurity', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('pie', models.BooleanField(default=False)), | ||
('relro', models.PositiveIntegerField(choices=[(1, 'No RELRO'), (2, 'Partial RELRO'), (2, 'Full RELRO')], default=1)), | ||
('canary', models.BooleanField(default=False)), | ||
('fortify', models.BooleanField(default=False)), | ||
('pkg', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.Package')), | ||
], | ||
options={ | ||
'db_table': 'package_security', | ||
}, | ||
), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.2.6 on 2019-10-12 19:52 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0003_packagesecurity'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='packagesecurity', | ||
name='filename', | ||
field=models.CharField(default='', max_length=1024), | ||
), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -444,6 +444,33 @@ class Meta: | |
db_table = 'package_files' | ||
|
||
|
||
class PackageSecurity(models.Model): | ||
NO_RELRO = 1 | ||
PARTIAL_RELRO = 2 | ||
FULL_RELRO = 3 | ||
RELRO_CHOICES = ( | ||
(NO_RELRO, 'No RELRO'), | ||
(PARTIAL_RELRO, 'Partial RELRO'), | ||
(FULL_RELRO, 'Full RELRO'), | ||
) | ||
|
||
pkg = models.ForeignKey(Package, on_delete=models.CASCADE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
will get you a single |
||
pie = models.BooleanField(default=False) | ||
relro = models.PositiveIntegerField(choices=RELRO_CHOICES, default=NO_RELRO) | ||
canary = models.BooleanField(default=False) | ||
fortify = models.BooleanField(default=False) | ||
filename = models.CharField(max_length=1024, blank=False, default='') | ||
|
||
def relro_str(self): | ||
for id_, desc in self.RELRO_CHOICES: | ||
if id_ == self.relro: | ||
return desc | ||
return '' | ||
|
||
class Meta: | ||
db_table = 'package_security' | ||
|
||
|
||
from django.db.models.signals import pre_save | ||
|
||
# note: reporead sets the 'created' field on Package objects, so no signal | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ django-jinja==2.4.1 | |
sqlparse==0.3.0 | ||
django-csp==3.5 | ||
ptpython==2.0.4 | ||
pyelftools==0.25 | ||
libarchive-c==2.8 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.