You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exercises.tex 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. \documentclass [10pt]{article}
  2. \usepackage{latexsym}
  3. \usepackage{amssymb}
  4. \usepackage{epsfig}
  5. \usepackage{fullpage}
  6. \usepackage{enumerate}
  7. \usepackage{xspace}
  8. \usepackage{todonotes}
  9. \usepackage{listings}
  10. \usepackage[ruled,linesnumbered]{algorithm2e} % Enables the writing of pseudo code.
  11. \usepackage{float}% http://ctan.org/pkg/float
  12. \newcommand{\true}{true}
  13. \newcommand{\false}{false}
  14. \pagestyle{plain}
  15. \bibliographystyle{plain}
  16. \title{192.127 Seminar in Software Engineering (Smart Contracts) \\
  17. SWC-124: Write to Arbitrary Storage Location}
  18. \author{Exercises}
  19. \date{WT 2023/24}
  20. \author{\textbf{*** YOUR NAME AND STUDENT ID ***}}
  21. \newtheorem{theorem}{Theorem}
  22. \newtheorem{lemma}[theorem]{Lemma}
  23. \newtheorem{corollary}[theorem]{Corollary}
  24. \newtheorem{proposition}[theorem]{Proposition}
  25. \newtheorem{conjecture}[theorem]{Conjecture}
  26. \newtheorem{definition}[theorem]{Definition}
  27. \newtheorem{example}[theorem]{Example}
  28. \newtheorem{remark}[theorem]{Remark}
  29. \newtheorem{exercise}[theorem]{Exercise}
  30. \renewcommand{\labelenumi}{(\alph{enumi})}
  31. \usepackage{xcolor}
  32. \definecolor{codegreen}{rgb}{0,0.6,0}
  33. \definecolor{codegray}{rgb}{0.5,0.5,0.5}
  34. \definecolor{codepurple}{rgb}{0.58,0,0.82}
  35. \definecolor{backcolour}{rgb}{0.95,0.95,0.92}
  36. \lstdefinestyle{mystyle}{
  37. backgroundcolor=\color{backcolour},
  38. commentstyle=\color{codegreen},
  39. keywordstyle=\color{magenta},
  40. numberstyle=\tiny\color{codegray},
  41. stringstyle=\color{codepurple},
  42. basicstyle=\ttfamily\footnotesize,
  43. breakatwhitespace=false,
  44. breaklines=true,
  45. captionpos=b,
  46. keepspaces=true,
  47. numbers=left,
  48. numbersep=5pt,
  49. showspaces=false,
  50. showstringspaces=false,
  51. showtabs=false,
  52. tabsize=2
  53. }
  54. \begin{document}
  55. \maketitle
  56. \section{Weakness and consequences}
  57. \subsection{Solidity storage layout}
  58. Any contract's storage is a continuous 256-bit address space consisting of 32-bit values. In order to implement dynamically sized data structures like maps and arrays, Solidity distributes their entries in a pseudo-random location. Due to the vast 256-bit range of addresses collisions are statistically extremely improbable and of little practical relevance in safely implemented contracts.
  59. \medspace
  60. In the case of a dynamic array at variable slot $p$, data is written to continuous locations starting at $keccak(p)$. The array itself contains the length information as an $uint256$ value. Even enormous arrays are unlikely to produce collisions due to the vast address space, although an improperly managed array may store data to an unbounded user-controlled offset, thereby allowing arbitrary overwriting of data.
  61. \medspace
  62. For maps stored in variable slot $p$ the data for index $k$ can be found at $keccak(k . p)$ where $.$ is the concatenation operator. This is a statistically safe approach, as the chance of intentionally finding a value for $keccak(k . p)$ s.t. for a known stored variable $x$, $keccak(k . p) == storage\_address(x)$ is about one in $2^{256}$ and $keccak$ is believed to be a cryptographically secure hash function.
  63. \subsection{The Weakness}
  64. Any unchecked array write is potentially dangerous, as the storage-location of all variables is publicly known and an unconstrained array index can be reverse engineered to target them. This can be achieved by using the known array storage location $p$, target-variable $x$, and computing the offset-value $o$ such that $keccac(p) + o == storage\_address(x)$.
  65. \medspace
  66. A trivial example of such a vulnerable write operation is shown in Algorithm 1.
  67. \lstset{style=mystyle}
  68. \begin{algorithm}[H]
  69. \begin{lstlisting}[language=Octave]
  70. pragma solidity 0.4.25;
  71. contract MyContract {
  72. address private owner;
  73. uint[] private arr;
  74. constructor() public {
  75. arr = new uint[](0);
  76. owner = msg.sender;
  77. }
  78. function write(unit index, uint value) {
  79. arr[index] = value;
  80. }
  81. }
  82. \end{lstlisting}
  83. \caption{A completely unchecked array write}
  84. \end{algorithm}
  85. \medspace
  86. In the following example (Algorithm 2) the $pop$ function incorrectly checks for an array $length >= 0$, thereby allowing the $length$ value to underflow when called with an empty array. Once this weakness is triggered, $update$ in Algorithm 2 behaves just like $write$ did in Algorithm 1.
  87. \medspace
  88. \lstset{style=mystyle}
  89. \begin{algorithm}[H]
  90. \begin{lstlisting}[language=Octave]
  91. pragma solidity 0.4.25;
  92. contract MyContract {
  93. address private owner;
  94. uint[] private arr;
  95. constructor() public {
  96. arr = new uint[](0);
  97. owner = msg.sender;
  98. }
  99. function push(value) {
  100. arr[arr.length] = value;
  101. arr.length++;
  102. }
  103. function pop() {
  104. require(arr.length >= 0);
  105. arr.length--;
  106. }
  107. function update(unit index, uint value) {
  108. require(index < arr.length);
  109. arr[index] = value;
  110. }
  111. }
  112. \end{lstlisting}
  113. \caption{An incorrectly managed array length}
  114. \end{algorithm}
  115. \section{Vulnerable contracts in literature}
  116. collect vulnerable contracts used by different papers to motivate/illustrate the weakness
  117. \section{Code properties and automatic detection}
  118. Automatic detection tools can be broadly categorized into ones employing static analysis and those who use fuzzing, i.e. application of semi-random inputs. Notable static analysis tools include Securify \cite{securify} and teEther \cite{teether} which both function in a similar manner:
  119. \medspace
  120. Initially, the given EVM byte-code is disassembled into a control-flow-graph (CFG). In the second step, the tools identify potentially risky instructions. In the case of arbitrary writes, the instruction of note is $sstore(k,v)$ where both $k$ and $v$ are input-controlled. The tools differ in the way they identify whether or not the values are input-controlled.
  121. \medspace
  122. In the case of Securify \cite{securify}, the CFG is translated into what the authors call "semantic facts" to which an elaborate set of so-called security patterns is applied. These patterns consist of building blocks in the form of predicates, which allows the tool to simply generate output based on the (transitively) matched patterns.
  123. \medspace
  124. teEther \cite{teether} employs a similar approach, but instead the authors opt to build a graph of dependent variables. If the graph arrives at a $sstore(k,v)$ instruction and a path can be found leading to user-controlled inputs, the tool infers a set of constraints which are then used to automatically generate an exploit.
  125. \medspace
  126. The fuzz-driven approach to vulnerability detection is more abstract, as general-purpose fuzzing tools generally don't have knowledge of the analysed program. For the tool SmartFuzzDriverGenerator \cite{fuzzdrivegen}, a multitude of these fuzzing libraries can be used. The problem at hand is, however, that the technique cannot interface with a smart contract out of the box. The "glue" between fuzzer and program is called a driver, hence the name of "driver-generator".
  127. \medspace
  128. SmartFuzzDriverGenerator aims to automatically generate such a driver by %TODO: I have no idea how it does this actually%
  129. \medspace
  130. The Smartian tool \cite{smartian} attempts to find a middle-ground between static and dynamic analysis by first transforming the EVM bytecode into control-flow facts. Based on this information, a set of seed-inputs is generated that are expected to have a high probability of yielding useable results. Should no exploit be found, the seed-inputs are then mutated in order to yield a higher code coverage. %TODO: This is probably extemely inprecise and should be re-written%
  131. \section{Exploit sketch}
  132. \cite{doughoyte}
  133. %TODO: just explain what this guy does: https://github.com/Arachnid/uscc/tree/master/submissions-2017/doughoyte%
  134. \bibliography{exercise.bib}
  135. \end{document}