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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. \newcommand{\true}{true}
  11. \newcommand{\false}{false}
  12. \usepackage[ruled,linesnumbered]{algorithm2e} % Enables the writing of pseudo code.
  13. \pagestyle{plain}
  14. \bibliographystyle{plain}
  15. \title{192.127 Seminar in Software Engineering (Smart Contracts) \\
  16. SWC-124: Write to Arbitrary Storage Location}
  17. \author{Exercises}
  18. \date{WT 2023/24}
  19. \author{\textbf{*** YOUR NAME AND STUDENT ID ***}}
  20. \newtheorem{theorem}{Theorem}
  21. \newtheorem{lemma}[theorem]{Lemma}
  22. \newtheorem{corollary}[theorem]{Corollary}
  23. \newtheorem{proposition}[theorem]{Proposition}
  24. \newtheorem{conjecture}[theorem]{Conjecture}
  25. \newtheorem{definition}[theorem]{Definition}
  26. \newtheorem{example}[theorem]{Example}
  27. \newtheorem{remark}[theorem]{Remark}
  28. \newtheorem{exercise}[theorem]{Exercise}
  29. \renewcommand{\labelenumi}{(\alph{enumi})}
  30. \usepackage{xcolor}
  31. \definecolor{codegreen}{rgb}{0,0.6,0}
  32. \definecolor{codegray}{rgb}{0.5,0.5,0.5}
  33. \definecolor{codepurple}{rgb}{0.58,0,0.82}
  34. \definecolor{backcolour}{rgb}{0.95,0.95,0.92}
  35. \lstdefinestyle{mystyle}{
  36. backgroundcolor=\color{backcolour},
  37. commentstyle=\color{codegreen},
  38. keywordstyle=\color{magenta},
  39. numberstyle=\tiny\color{codegray},
  40. stringstyle=\color{codepurple},
  41. basicstyle=\ttfamily\footnotesize,
  42. breakatwhitespace=false,
  43. breaklines=true,
  44. captionpos=b,
  45. keepspaces=true,
  46. numbers=left,
  47. numbersep=5pt,
  48. showspaces=false,
  49. showstringspaces=false,
  50. showtabs=false,
  51. tabsize=2
  52. }
  53. \begin{document}
  54. \maketitle
  55. \section{Weakness and consequences}
  56. \subsection{Solidity storage layout}
  57. 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 no practical relevance.
  58. \medspace
  59. 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.
  60. \medspace
  61. For maps stored in variable slot $p$ the data for index $k$ can be found at $keccak(k . p)$ where $.$ is the concatenation operator.
  62. \subsection{The Weakness}
  63. 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.
  64. \lstset{style=mystyle}
  65. \begin{algorithm}
  66. \begin{lstlisting}[language=Octave]
  67. pragma solidity 0.4.25;
  68. contract MyContract {
  69. address private owner;
  70. uint[] private arr;
  71. constructor() public {
  72. arr = new uint[](0);
  73. owner = msg.sender;
  74. }
  75. function write(unit index, uint value) {
  76. arr[index] = value;
  77. }
  78. }
  79. \end{lstlisting}
  80. \caption{A completely unchecked array write}
  81. \end{algorithm}
  82. In the following example the $pop$ function incorrectly checks for an array $length >= 0$, thereby allowing the value to underflow when called with an empty array. Once this weakness is exploited $update$ in Algorithm 2 behaves just like $write$ did in Algorithm 1.
  83. \lstset{style=mystyle}
  84. \begin{algorithm}
  85. \begin{lstlisting}[language=Octave]
  86. pragma solidity 0.4.25;
  87. contract MyContract {
  88. address private owner;
  89. uint[] private arr;
  90. constructor() public {
  91. arr = new uint[](0);
  92. owner = msg.sender;
  93. }
  94. function push(value) {
  95. arr[arr.length] = value;
  96. arr.length++;
  97. }
  98. function pop() {
  99. require(arr.length >= 0);
  100. arr.length--;
  101. }
  102. function update(unit index, uint value) {
  103. require(index < arr.length);
  104. arr[index] = value;
  105. }
  106. }
  107. \end{lstlisting}
  108. \caption{An incorrectly managed array length}
  109. \end{algorithm}
  110. \section{Vulnerable contracts in literature}
  111. collect vulnerable contracts used by different papers to motivate/illustrate the weakness
  112. \section{Code properties and automatic detection}
  113. summarize the code properties that tools are looking for so that they can detect the weakness
  114. \section{Exploit sketch}
  115. sketch ways to potentially exploit the different variants of the weakness.
  116. %remove this later%
  117. \cite{10.1145/3243734.3243780}
  118. \cite{10.1145/3578527.3578538}
  119. \cite{217464}
  120. \cite{9678888}
  121. \bibliography{exercise.bib}
  122. \end{document}