% vim: set filetype=tex fileencoding=utf8 et sw=2 ts=2 sts=2 tw=80 :
% © 2026 Ali Polatel <alip@hexsys.org>
% Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported Lisansı ile yayınlanmıştır.

\documentclass[aspectratio=169]{beamer}
\usepackage[english]{babel}

\usepackage{fontspec}
\usepackage{unicode-math}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont[Ligatures=TeX,
Extension=.otf,
BoldFont=*-bold,
UprightFont=*-regular,
ItalicFont=*-italic,
BoldItalicFont=*-bolditalic,
SmallCapsFeatures={Letters=SmallCaps}]{texgyretermes}
\setmathfont[Ligatures=TeX]{texgyretermes-math.otf}
\setsansfont[Ligatures=TeX,
Extension=.otf,
BoldFont=*-bold,
UprightFont=*-regular,
ItalicFont=*-italic,
BoldItalicFont=*-bolditalic,
SmallCapsFeatures={Letters=SmallCaps}]{texgyreheros}

\usepackage{graphicx}
\DeclareGraphicsExtensions{.jpg,.png}

\usepackage{fontawesome5}
\usepackage{marvosym}

\usepackage{booktabs}
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{pdfpages}
\usepackage{color}
\usepackage[xspace]{ellipsis}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta, positioning, fit, calc}
\tikzset{
  startstop/.style = {
    rectangle, rounded corners,
    minimum height=0.5cm,
    text centered, text width=2.0cm, align=center,
    draw=black, fill=red!20, inner sep=2pt
  },
  process/.style = {
    rectangle,
    minimum height=0.5cm,
    text centered, text width=2.1cm, align=center,
    draw=black, fill=orange!20, inner sep=2pt
  },
  decision/.style = {
    diamond, aspect=2,
    minimum height=0.5cm,
    text width=1.9cm, align=center,
    draw=black, fill=green!20, inner xsep=2pt, inner ysep=1pt
  },
  arrow/.style = {-{Stealth}, shorten >=1pt, line width=0.6pt}
}

\definecolor{Brown}{cmyk}{0,0.81,1,0.60}
\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\definecolor{lightlightgray}{gray}{0.9}
\usepackage{listings}
\lstset{
  inputencoding=utf8,
  extendedchars=\false,
  escapeinside={\%*}{*)},
  language=Python,
  basicstyle=\scriptsize\ttfamily,
  stringstyle=\scriptsize\ttfamily,
  keywordstyle=\color{OliveGreen},
  commentstyle=\color{gray},
  numbers=left,
  numberstyle=\tiny,
  stepnumber=1,
  numbersep=5pt,
  backgroundcolor=\color{lightlightgray},
  frame=none,
  tabsize=2,
  captionpos=t,
  breaklines=true,
  breakatwhitespace=false,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  columns=flexible
}

\usetheme{Warsaw}
\usecolortheme[snowy]{owl}
%\setbeamertemplate{itemize/enumerate body begin}{\footnotesize}
%\setbeamertemplate{itemize/enumerate subbody begin}{\scriptsize}
%\setbeamertemplate{itemize/enumerate subsubbody begin}{\tiny}

\author{Ali Polatel}
\title{Syd}
\subtitle{Writing an Application Kernel in Rust}
\institute{
  \noindent
  \includegraphics[height=0.2\textheight,width=0.2\textwidth]{zebrapig}
  \hspace{0.1\textwidth}
  \includegraphics[height=0.2\textheight,width=0.2\textwidth]{syd}
  \hspace{0.1\textwidth}
  \includegraphics[height=0.2\textheight,width=0.2\textwidth]{sydbox}
}
\date{FOSDEM 2026}

\usepackage{hyperref}
\hypersetup{%
    hyperfootnotes=true,
    breaklinks=true,
    colorlinks=true,
    urlcolor=black,
    citecolor=black,
    linkcolor=black,
    pdftitle={Syd},
    pdfauthor={Ali Polatel},
    pdfsubject={Ali Polatel, Syd},
    pdflang={en},
    pdfkeywords={Linux, Sandboxing},
    pdfproducer={LuaLaTeX, BibTeX, hyperref, memoir},
    pdfpagelabels=true
    pdfborder={0 0 0},
}

\begin{document}

\frame{\titlepage}

\begin{frame}
  \frametitle{whoami}
  \framesubtitle{I've got a bike, you can ride it if you like.}

  \begin{itemize}
    \item Exherbo Linux dev, ex-Gentoo dev
    \item Main author of Sydbox
    \item Chess trainer, Co-founder of \url{chesswob.org}
    \item Interests: Linux, BSD, Sandboxing, Security, Board games, Translation
    \item E-mail: \url{alip@chesswob.org}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Outline}
  \framesubtitle{Long you live and high you fly, smiles you'll give and tears you'll cry.}

  \begin{itemize}
    \item What is an Application Kernel?
    \item Syscall Interception
    \item Why Rust?
    \item Memory Safety Patterns
    \item Safety and Performance
    \item Testing Infrastructure
    \item Q\&A
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{What is an Application Kernel?}
  \framesubtitle{We call it riding the gravy train.}

  \textbf{Definition:} A Library OS variant that intercepts, emulates, and transforms syscalls in user-space for sandboxed processes. (cf.\ Exokernel, SOSP'95)

  \vspace{0.15cm}
  \begin{itemize}\setlength{\itemsep}{1pt}
    \item Intercepts syscalls via \texttt{seccomp\_unotify(2)}, \texttt{ptrace(2)}, \texttt{landlock(7)}
    \item Emulates file system, network, process operations
    \item Transforms paths, flags, credentials at runtime
    \item Configuration via \texttt{/dev/syd}
  \end{itemize}

  \vspace{0.15cm}
  \textbf{Similar projects:} gVisor (Google, Go), rump kernels (NetBSD), Nabla containers, OSv, MirageOS

  \vspace{0.15cm}
  \centering\scriptsize\textit{application + kernel $\neq$ application kernel --- ``You must take your opponent into a deep dark forest where $2 + 2 = 5$, and the path leading out is only wide enough for one.'' --- Mikhail Tal}
\end{frame}

\begin{frame}
  \frametitle{The Path of open(2)}
  \framesubtitle{Hello, is there anybody in there?}

  \begin{columns}[T]
    \begin{column}{0.55\textwidth}
      \centering
      \begin{tikzpicture}[x=1cm, y=0.55cm, font=\tiny,
        box/.style={rectangle, rounded corners, draw=black, fill=blue!12,
                    minimum width=2.3cm, minimum height=0.35cm, align=center},
        entrybox/.style={box, fill=red!15},
        okbox/.style={box, fill=green!20},
        err/.style={box, fill=red!20},
        arrow/.style={-{Stealth}, line width=0.5pt},
        num/.style={font=\tiny, blue}]

        % Main flow
        \node (entry) [entrybox] at (0,8.5) {sandbox: open("/etc/passwd")};
        \node (notify) [box] at (0,7) {seccomp\_unotify};
        \node (vmread) [box] at (0,5.5) {process\_vm\_readv};
        \node (canon) [box] at (0,4) {canonicalize};
        \node (check) [box] at (0,2.5) {sandbox check};
        \node (open) [box] at (0,1) {do\_open};
        \node (addfd) [okbox] at (0,-0.5) {SECCOMP\_ADDFD};

        % Numbers on boxes
        \node[num] at (-1.6,2.5) {1-6};
        \node[num] at (-1.4,1) {7,8};
        \node[num] at (-1.4,-0.5) {9};

        % Branch outputs - on right
        \node (magic) [okbox] at (3.5,5.5) {/dev/syd};
        \node (hidden) [err] at (3.5,2.5) {ENOENT};
        \node (deny) [err] at (3.5,1) {EACCES};
        \node[num] at (4.5,5.5) {11};

        % Vertical arrows with tiny labels
        \draw[arrow] (entry) -- (notify);
        \draw[arrow] (notify) -- node[right]{\tiny ptr} (vmread);
        \draw[arrow] (vmread) -- node[right]{\tiny str} (canon);
        \draw[arrow] (canon) -- node[right, align=left]{\tiny O\_PATH fd\\[-2pt]\tiny + canon-path} (check);
        \draw[arrow] (check) -- node[right]{\tiny allow} (open);
        \draw[arrow] (open) -- node[right]{\tiny OwnedFd} node[left]{\tiny 10} (addfd);

        % Branch arrows
        \draw[arrow] (vmread.east) -- node[above]{\tiny magic} (magic.west);
        \draw[arrow] (check.east) -- node[above]{\tiny hide} (hidden.west);
        \draw[arrow] (check.east) ++(0,-0.2) -- ++(0.3,0) |- node[near start, above]{\tiny deny} (deny.west);

      \end{tikzpicture}
    \end{column}
    \begin{column}{0.43\textwidth}
      \scriptsize\textbf{Transformations}
      \begin{enumerate}\setlength{\itemsep}{0pt}
        \item Path hiding: deny $\rightarrow$ \texttt{ENOENT}
        \item Mask: path $\rightarrow$ \texttt{/dev/null}
        \item Crypt: transparent encryption
        \item Append: force \texttt{O\_APPEND}
        \item Filter: rewrite proc/status
        \item FS sandbox: block by fs type
        \item rand\_fd: randomize fd number
      \end{enumerate}
      \vspace{0.15cm}
      \scriptsize\textbf{TOCTOU Prevention}
      \begin{enumerate}\setlength{\itemsep}{0pt}
        \setcounter{enumi}{7}
        \item Open via \texttt{/proc/thread-self/fd/}
        \item Inject fd with \texttt{SECCOMP\_ADDFD}
        \item Never \texttt{CONTINUE} syscall
        \item \texttt{/dev/syd} = sealed memfd
      \end{enumerate}
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Why Rust?}
  \framesubtitle{Mother did it need to be so high?}

  \begin{itemize}
    \item \texttt{\#![forbid(unsafe\_code)]} modules (ELF parser, glob matcher)
    \item \texttt{\#![forbid(clippy::arithmetic\_side\_effects)]} for DoS prevention
    \item Type-state patterns: \texttt{SealBox<T>} $\rightarrow$ \texttt{Sealed<T>}
    \item Ownership = resource tracking (file descriptors, memory mappings)
    \item Zero-cost abstractions in hot paths
    \item Fearless concurrency for thread pool
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Memory Safety Patterns}
  \framesubtitle{No one told you when to run, you missed the starting gun.}

  \textbf{Type-State Pattern} (sealbox.rs)
  \begin{lstlisting}[basicstyle=\tiny\ttfamily]
// Linear consumption: SealBox<T> -> Sealed<T>
pub fn seal(self, vma_name: Option<&CStr>) -> Result<Sealed<T>, Errno> {
    mprotect_readonly(self.map_ptr, self.map_len)?;
    mseal(self.map_ptr, self.map_len)?;  // Linux 6.10+
    mem::forget(self);  Ok(sealed)
}

// Compile-time + runtime state tracking
enum Sealable<T: Copy> {
    Unsealed(T),       // DerefMut allowed
    Sealed(Sealed<T>), // DerefMut panics!
}
  \end{lstlisting}

  \vspace{0.2cm}
  \textbf{ELF Parser} (elf.rs) --- 887 lines, zero unsafe
  \begin{lstlisting}[basicstyle=\tiny\ttfamily]
#![forbid(unsafe_code)]
#![forbid(clippy::arithmetic_side_effects)]
#![forbid(clippy::cast_possible_truncation)]
  \end{lstlisting}

  \textbf{SyscookiePool} (cookie.rs) --- Guard pages + \texttt{fillrandom(2)} + \texttt{mseal(2)}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Safety and Performance}
  \framesubtitle{Together we stand, divided we fall.}

  \textbf{Glob Matcher} (wildmatch.rs) --- rsync (1986), Kirk Krauss's FastWildCompare
  \begin{lstlisting}[basicstyle=\tiny\ttfamily]
#![forbid(unsafe_code)]
// SIMD via memchr crate, dual backup for * vs **
  \end{lstlisting}

  \vspace{0.1cm}
  {\scriptsize
  \begin{tabular}{lrr}
    \toprule
    Method & Mean & Samples \\
    \midrule
    wildmatch & 37.12 ms & 807/808 \\
    fnmatch (libc) & 86.64 ms & 347/347 \\
    \bottomrule
  \end{tabular}
  \hspace{0.5cm}\textbf{2.3x faster}}

  \vspace{0.3cm}
  \textbf{Custom Path Types} (path.rs)
  \begin{lstlisting}[basicstyle=\tiny\ttfamily]
pub struct XPathBuf(TinyVec<[u8; 400]>); // stack alloc <400 bytes
pub struct XPath([u8]);                  // DST, SIMD compare
  \end{lstlisting}

  \begin{itemize}
    \item Why not \texttt{std::path::Path}? Bytes not OsStr, stack alloc, SIMD compare
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Testing Infrastructure}
  \framesubtitle{Can you tell a green field from a cold steel rail?}

  \textbf{Multi-Architecture CI Pipeline} (.gitlab-ci.yml)
  \begin{itemize}
    \item Native runners: x86\_64, aarch64, armv7, s390x
    \item Stages: build $\rightarrow$ test $\rightarrow$ ltp $\rightarrow$ compat $\rightarrow$ release
    \item 32-bit cross-compile tests (i686 under x86\_64 sandbox)
  \end{itemize}

  \vspace{0.2cm}
  \textbf{External Test Suites}
  \begin{itemize}
    \item \textbf{LTP}: 4000+ Linux syscall tests under \texttt{-pltp} profile
    \item \textbf{gnulib}: 250+ POSIX compatibility tests under \texttt{-ppaludis}
  \end{itemize}

  \vspace{0.2cm}
  \textbf{Sandbox Escape Tests} (t/do.rs --- 20+ attack vectors)
  \begin{lstlisting}[basicstyle=\tiny\ttfamily]
// TOCTOU attacks that no longer work:
ptrmod_toctou_exec_*      // pointer modification during execve
symlink_exchange_toctou_* // RENAME_EXCHANGE race
vfsmod_toctou_*           // VFS fd swap attacks
magiclink_toctou          // /proc/self/fd race
  \end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Thanks for watching! Questions?}
  \framesubtitle{So you think you can tell heaven from hell?}

  \begin{itemize}
    \item Gitlab: \url{https://gitlab.exherbo.org/sydbox/sydbox.git}
    \item Manual: \url{https://man.exherbo.org}
    \item IRC: \#sydbox at Libera
    \item Matrix: \#sydbox:mailstation.de
    \item Thanks to friends at \includegraphics[height=0.5cm]{omd.png} for sponsoring my attendance!
  \end{itemize}
\end{frame}

\end{document}

