Skip to content

Commit

Permalink
chore: run black + first ruff fixes
Browse files Browse the repository at this point in the history
chore: add ruff CI

chore: remove flake8
  • Loading branch information
0xricksanchez committed Sep 26, 2023
1 parent 4d9a4e8 commit ee35610
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 76 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/linter.yml

This file was deleted.

42 changes: 42 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: ruff

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.10.12

- name: Install poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.cache/pypoetry
key: ${{ runner.os }}-poetry-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-poetry-
- name: Install dependencies
run: poetry install

- name: Check lock file
run: poetry check --lock

- name: Run ruff
run: poetry run ruff check . --fix --show-source --show-fixes --exit-non-zero-on-fix
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "like-dbg"
name = "like_dbg"
version = "0.1.0"
description = "Fully dockerized Linux kernel debugging environment"
authors = ["434b <admin@0x434b.dev>"]
Expand Down
16 changes: 9 additions & 7 deletions src/debuggee.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/usr/bin/env python3

import subprocess as sp
from pathlib import Path

from loguru import logger

from .docker_runner import DockerRunner
from .misc import adjust_qemu_arch, cfg_setter, tmux, tmux_shell
from src.docker_runner import DockerRunner
from src.misc import adjust_qemu_arch, cfg_setter, tmux, tmux_shell


# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
Expand All @@ -16,7 +14,9 @@ class Debuggee(DockerRunner):
def __init__(self, **kwargs):
super().__init__(**kwargs)
user_cfg = kwargs.get("user_cfg", "")
cfg_setter(self, ["general", "debuggee", "debuggee_docker", "rootfs_general"], user_cfg, exclude_keys=["kernel_root"])
cfg_setter(
self, ["general", "debuggee", "debuggee_docker", "rootfs_general"], user_cfg, exclude_keys=["kernel_root"]
)
if self.ctf:
self.ctf_mount = kwargs.get("ctf_mount")
self.kernel = Path(self.docker_mnt) / kwargs.get("ctf_kernel", "")
Expand All @@ -25,7 +25,7 @@ def __init__(self, **kwargs):
self.kernel = Path(self.docker_mnt) / self.kernel_root / "arch" / self.arch / "boot" / "Image"
self.rootfs = Path(self.docker_mnt) / self.rootfs_dir / (self.rootfs_base + self.arch + self.rootfs_ftype)
self.qemu_arch = adjust_qemu_arch(self.arch)
self.cmd = None
self.cmd = ""

def run(self):
super().run(check_existing=True)
Expand Down Expand Up @@ -103,7 +103,9 @@ def run_container(self):
self.cmd += " pti=on"
self.cmd += f' oops=panic panic={self.infer_panic_behavior()}"'
self.cmd += self.infer_qemu_fs_mount()
self.cmd += " -net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 -net nic,model=e1000 -nographic -pidfile vm.pid"
self.cmd += (
" -net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 -net nic,model=e1000 -nographic -pidfile vm.pid"
)
if self.kvm and self.qemu_arch == "x86_64":
self.cmd += " -enable-kvm"
if self.gdb:
Expand Down
14 changes: 10 additions & 4 deletions src/debugger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env python3

import subprocess as sp
from glob import glob
from pathlib import Path

from loguru import logger

from .docker_runner import DockerRunner
from .misc import SYSTEM_CFG, cfg_setter, get_sha256_from_file, get_value_from_section_by_key, new_context, tmux, tmux_shell
from src.docker_runner import DockerRunner
from src.misc import (
SYSTEM_CFG,
cfg_setter,
get_sha256_from_file,
get_value_from_section_by_key,
new_context,
tmux,
tmux_shell,
)

GDB_SCRIPT_HIST = Path(".gdb_hist")

Expand Down
16 changes: 11 additions & 5 deletions src/docker_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

import os
import subprocess as sp
import time
Expand All @@ -10,7 +8,7 @@
from fabric import Connection
from loguru import logger

from .misc import cfg_setter, is_reuse
from src.misc import cfg_setter, is_reuse


# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
Expand Down Expand Up @@ -51,7 +49,9 @@ def init_ssh(self):
tries = 0
while True:
try:
self.ssh_conn = Connection(f"{self.user}@localhost:{self.ssh_fwd_port}", connect_kwargs={"key_filename": ".ssh/like.id_rsa"})
self.ssh_conn = Connection(
f"{self.user}@localhost:{self.ssh_fwd_port}", connect_kwargs={"key_filename": ".ssh/like.id_rsa"}
)
except Exception as e:
tries += 1
logger.error(f"Failed to initialize SSH connection to {type(self).__name__}: {e}")
Expand All @@ -71,7 +71,13 @@ def build_image(self, dockerfile=None, buildargs=None, image_tag=None):
nocache = True if self.update_containers else False
try:
for log_entry in self.cli.build(
path=str(self.dockerfile_ctx), dockerfile=dockerfile, tag=tag, decode=True, buildargs=buildargs, nocache=nocache, rm=True
path=str(self.dockerfile_ctx),
dockerfile=dockerfile,
tag=tag,
decode=True,
buildargs=buildargs,
nocache=nocache,
rm=True,
):
v = next(iter(log_entry.values()))
if isinstance(v, str):
Expand Down
14 changes: 8 additions & 6 deletions src/kernel_builder.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env python3

import os
import re
import subprocess as sp
from pathlib import Path

from loguru import logger

from .docker_runner import DockerRunner
from .misc import adjust_arch, adjust_toolchain_arch, cfg_setter, cross_compile
from src.docker_runner import DockerRunner
from src.misc import adjust_arch, adjust_toolchain_arch, cfg_setter, cross_compile

MISC_DRVS_PATH = Path("drivers/misc/")

Expand All @@ -21,7 +19,11 @@ def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
user_cfg = kwargs.get("user_cfg", "")
cfg_setter(
self, ["general", "kernel_builder", "kernel_builder_docker"], user_cfg, exclude_keys=["kernel_root"], cherry_pick={"debuggee": ["kvm"]}
self,
["general", "kernel_builder", "kernel_builder_docker"],
user_cfg,
exclude_keys=["kernel_root"],
cherry_pick={"debuggee": ["kvm"]},
)
self.cc = f"CC={self.compiler}" if self.compiler else ""
self.llvm_flag = "" if "gcc" in self.cc else "LLVM=1"
Expand Down Expand Up @@ -54,7 +56,7 @@ def _run_ssh(self, cmd: str, **kwargs) -> int:
def _apply_patches(self) -> int:
ret = 0
if self.patch_dir and Path(self.patch_dir).exists():
patch_files = [x for x in Path(self.patch_dir).iterdir()]
patch_files = list(Path(self.patch_dir).iterdir())
if patch_files:
for pfile in patch_files:
logger.debug(f"Patching: {pfile}")
Expand Down
4 changes: 1 addition & 3 deletions src/kernel_unpacker.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env python3

import shutil
import tarfile
from pathlib import Path

from loguru import logger
from tqdm import tqdm

from .misc import cfg_setter, is_reuse
from src.misc import cfg_setter, is_reuse


# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
Expand Down
6 changes: 2 additions & 4 deletions src/linux_kernel_dl.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env python3

import re
import urllib.request
from pathlib import Path

import requests
from loguru import logger

from .misc import cfg_setter
from .tqdm_dlbar import DLProgressBarTQDM
from src.misc import cfg_setter
from src.tqdm_dlbar import DLProgressBarTQDM


# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
Expand Down
6 changes: 3 additions & 3 deletions src/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

import configparser
import hashlib
import os
Expand All @@ -19,7 +17,9 @@
# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
# | MISC QOL functions |
# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
def cfg_setter(obj, sections: list[str], user_cfg: str, exclude_keys: list[str] = [], cherry_pick: dict[str, list[str]] = {}) -> None:
def cfg_setter(
obj, sections: list[str], user_cfg: str, exclude_keys: list[str] = [], cherry_pick: dict[str, list[str]] = {}
) -> None:
global CFGS
cfgs = list(CFGS)
cfg = configparser.ConfigParser()
Expand Down
6 changes: 2 additions & 4 deletions src/rootfs_builder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python3

from pathlib import Path

from loguru import logger

from .docker_runner import DockerRunner
from .misc import adjust_qemu_arch, cfg_setter, is_reuse
from src.docker_runner import DockerRunner
from src.misc import adjust_qemu_arch, cfg_setter, is_reuse


# +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
Expand Down
4 changes: 3 additions & 1 deletion src/tests/test_debuggee.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def test_infer_qemu_fs_mount_cpio_ctf(sp_mock) -> None:


def test_assert_ctf_ctx_mode() -> None:
d = Debuggee(**{"kroot": "foo", "ctf_ctx": True, "ctf_mount": "/foo", "ctf_kernel": "/a/path", "ctf_fs": "/another/path"})
d = Debuggee(
**{"kroot": "foo", "ctf_ctx": True, "ctf_mount": "/foo", "ctf_kernel": "/a/path", "ctf_fs": "/another/path"}
)
assert d.ctf is True
assert d.ctf_mount == "/foo"
assert d.kernel == Path("/a/path")
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_docker_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from docker import DockerClient
from docker.models.containers import Container

from ..docker_runner import DockerRunner
from src.docker_runner import DockerRunner

GENERIC_ARGS = {
"skip_prompts": False,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import docker
import pytest

from ..kernel_builder import MISC_DRVS_PATH, KernelBuilder
from src.kernel_builder import MISC_DRVS_PATH, KernelBuilder

USER_INI = Path("configs/user.ini")
CUSTOM_MODULE = Path("examples/like_dbg_confs/echo_module_x86.ini")
Expand Down
11 changes: 8 additions & 3 deletions src/tests/test_kernel_downloader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..linux_kernel_dl import KernelDownloader
from src.linux_kernel_dl import KernelDownloader
from pathlib import Path
import requests
import hashlib
Expand All @@ -24,7 +24,9 @@ def test_tag() -> None:
lkdl = KernelDownloader(**{"user_cfg": str(Path("./src/tests/confs/lkdl_tag.ini").absolute())})
assert lkdl.tag == "5.15-rc2"
dl_uri = lkdl._set_dl_uri()
assert dl_uri == f"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-{lkdl.tag}.tar.gz"
assert (
dl_uri == f"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-{lkdl.tag}.tar.gz"
)
resp = requests.head(dl_uri).headers
assert resp["Content-Type"] == "application/x-gzip; charset=UTF-8"

Expand All @@ -34,7 +36,10 @@ def test_commit() -> None:
assert lkdl.commit == "e4e737bb5c170df6135a127739a9e6148ee3da82"
assert lkdl.commit != lkdl._resolve_latest()
dl_uri = lkdl._set_dl_uri()
assert dl_uri == f"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-{lkdl.commit}.tar.gz"
assert (
dl_uri
== f"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-{lkdl.commit}.tar.gz"
)
resp = requests.head(dl_uri).headers
assert resp["Content-Type"] == "application/x-gzip; charset=UTF-8"

Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_kernel_unpacker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from unittest.mock import patch

from ..kernel_unpacker import KernelUnpacker
from src.kernel_unpacker import KernelUnpacker

EMPTY_TARGZ = Path("src/tests/files/empty.tar.gz")
VALID_TARGZ = Path("src/tests/files/valid.tar.gz")
Expand Down
8 changes: 7 additions & 1 deletion src/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ def test_set_base_cfg_ignore_empty() -> None:
@patch("src.misc.CFGS", [CFG_INI])
def test_cfg_setter() -> None:
m = Mock()
cfg_setter(m, sections=["kernel_dl"], user_cfg=str(USR_INI), exclude_keys=["ignore_me"], cherry_pick={"debuggee": ["panic"]})
cfg_setter(
m,
sections=["kernel_dl"],
user_cfg=str(USR_INI),
exclude_keys=["ignore_me"],
cherry_pick={"debuggee": ["panic"]},
)
assert "ignore_me" not in vars(m)
assert m.mmp == "5.15.67"
assert m.panic == "foo"
Expand Down
2 changes: 0 additions & 2 deletions src/tqdm_dlbar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

from tqdm import tqdm


Expand Down
11 changes: 9 additions & 2 deletions start_kgdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ def parse_cli() -> argparse.Namespace:
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("--config", "-c", required=False, nargs=1, help="Allows providing a custom 'user.ini'")
parser.add_argument("--ctf", nargs=2, required=False, help="Expected: <kernel_image> <root_file_system>")
parser.add_argument("--yes", "-y", action=argparse.BooleanOptionalAction, help="If this is set all re-use prompts are answered with 'yes'")
parser.add_argument(
"--yes",
"-y",
action=argparse.BooleanOptionalAction,
help="If this is set all re-use prompts are answered with 'yes'",
)
parser.add_argument("--verbose", "-v", action=argparse.BooleanOptionalAction, help="Enable debug logging")
parser.add_argument("--kill", "-k", action=argparse.BooleanOptionalAction, help="Completely shutdown current session")
parser.add_argument(
"--kill", "-k", action=argparse.BooleanOptionalAction, help="Completely shutdown current session"
)
parser.add_argument(
"--partial",
"-p",
Expand Down

0 comments on commit ee35610

Please sign in to comment.