Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .ruff-excludes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,6 @@
"UP032", # https://docs.astral.sh/ruff/rules/f-string
"UP038", # https://docs.astral.sh/ruff/rules/non-pep604-isinstance
]
"./scripts/kconfig/hardenconfig.py" = [
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
"UP032", # https://docs.astral.sh/ruff/rules/f-string
]
"./scripts/kconfig/kconfigfunctions.py" = [
"B011", # https://docs.astral.sh/ruff/rules/assert-false
"SIM114", # https://docs.astral.sh/ruff/rules/if-with-same-arms
Expand Down
28 changes: 17 additions & 11 deletions doc/security/hardening-tool.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ used instead.

.. code-block:: console

name | current | recommended || check result
================================================================================================
CONFIG_BOOT_BANNER | y | n || FAIL
CONFIG_BUILD_OUTPUT_STRIPPED | n | y || FAIL
CONFIG_FAULT_DUMP | 2 | 0 || FAIL
CONFIG_HW_STACK_PROTECTION | n | y || FAIL
CONFIG_MPU_STACK_GUARD | n | y || FAIL
CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT | n | y || FAIL
CONFIG_STACK_SENTINEL | n | y || FAIL
CONFIG_EARLY_CONSOLE | y | n || FAIL
CONFIG_PRINTK | y | n || FAIL
+---------------------------------------+-----------+---------------+----------------+
| Name | Current | Recommended | Check result |
+=======================================+===========+===============+================+
| CONFIG_BUILD_OUTPUT_STRIPPED | n | y | FAIL |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_FAULT_DUMP | 2 | 0 | FAIL |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_MPU_STACK_GUARD | n | y | FAIL |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT | n | y | FAIL |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_STACK_SENTINEL | n | y | FAIL |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_EXCEPTION_DEBUG | y | n | FAIL |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_PRINTK | y | n | FAIL |
+---------------------------------------+-----------+---------------+----------------+
40 changes: 26 additions & 14 deletions scripts/kconfig/hardenconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
import os

from kconfiglib import standard_kconfig
from tabulate import tabulate


def hardenconfig(kconf):
kconf.load_config()

hardened_kconf_filename = os.path.join(os.environ['ZEPHYR_BASE'],
'scripts', 'kconfig', 'hardened.csv')
hardened_kconf_filename = os.path.join(
os.environ['ZEPHYR_BASE'], 'scripts', 'kconfig', 'hardened.csv'
)

options = compare_with_hardened_conf(kconf, hardened_kconf_filename)

display_results(options)


class Option:

def __init__(self, name, recommended, current=None, symbol=None):
self.name = name
self.recommended = recommended
Expand Down Expand Up @@ -51,29 +52,40 @@ def compare_with_hardened_conf(kconf, hardened_kconf_filename):
except KeyError:
symbol = None
current = None
options.append(Option(name=name, current=current,
recommended=recommended, symbol=symbol))
options.append(
Option(name=name, current=current, recommended=recommended, symbol=symbol)
)
for node in kconf.node_iter():
for select in node.selects:
if kconf.syms["EXPERIMENTAL"] in select or kconf.syms["DEPRECATED"] in select or kconf.syms["NOT_SECURE"] in select:
options.append(Option(name=node.item.name, current=node.item.str_value, recommended='n', symbol=node.item))
if (
kconf.syms["EXPERIMENTAL"] in select
or kconf.syms["DEPRECATED"] in select
or kconf.syms["NOT_SECURE"] in select
):
options.append(
Option(
name=node.item.name,
current=node.item.str_value,
recommended='n',
symbol=node.item,
)
)

return options


def display_results(options):
# header
print('{:^50}|{:^13}|{:^20}'.format('name', 'current', 'recommended'), end='')
print('||{:^28}\n'.format('check result'), end='')
print('=' * 116)
table_data = []
headers = ['Name', 'Current', 'Recommended', 'Check result']

# results, only printing options that have failed for now. It simplify the readability.
# TODO: add command line option to show all results
for opt in options:
if opt.result == 'FAIL' and opt.symbol.visibility != 0:
print('CONFIG_{:<43}|{:^13}|{:^20}'.format(
opt.name, opt.current, opt.recommended), end='')
print('||{:^28}\n'.format(opt.result), end='')
table_data.append([f'CONFIG_{opt.name}', opt.current, opt.recommended, opt.result])

if table_data:
print(tabulate(table_data, headers=headers, tablefmt='grid'))
print()


Expand Down
Loading