|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="ja"> |
| 3 | +<head> |
| 4 | +<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | +<link rel="stylesheet" href="https://best.openssf.org/assets/css/style.css"> |
| 7 | +<link rel="stylesheet" href="checker.css"> |
| 8 | +<script src="checker.js"></script> |
| 9 | +<script src="oob1.js"></script> |
| 10 | +<link rel="license" href="https://creativecommons.org/licenses/by/4.0/"> |
| 11 | + |
| 12 | +<!-- See create_labs.md for how to create your own lab! --> |
| 13 | + |
| 14 | +</head> |
| 15 | +<body> |
| 16 | +<!-- For GitHub Pages formatting: --> |
| 17 | +<div class="container-lg px-3 my-5 markdown-body"> |
| 18 | +<h1>ラボ演習 oob1</h1> |
| 19 | +<p> |
| 20 | +これはセキュアなソフトウェア開発に関するラボ演習です。 |
| 21 | +ラボの詳細については、<a href="introduction.html" target="_blank">概要</a>をご覧ください。 |
| 22 | + |
| 23 | +<p> |
| 24 | +<h2>タスク</h2> |
| 25 | +<p> |
| 26 | +<b>バッファオーバーリードバグの一例として、OpenSSL の "Heartbleed" を単純化した以下のコードを変更してバグを修正してください</b> |
| 27 | + |
| 28 | +<p> |
| 29 | +<h2>背景</h2> |
| 30 | +<p> |
| 31 | +ほとんど全てのプログラミング言語では、もしプログラムがバッファの外側を読み書きしようとした場合にはバッファをリサイズするか、何等かのエラーを出力するかをデフォルトの挙動としています(例:例外を上げる)。 |
| 32 | +なぜなら誤ってバッファの外側を読み書きしようとするのは非常によくあることだからです。 |
| 33 | + |
| 34 | +<p> |
| 35 | +しかし、C と C++ は事情が異なります。 |
| 36 | +C++ は進歩しており、いくらかは安全になっていますが(例:スマートポインタ)、 |
| 37 | +C と C++ においてバッファの外側を読み書きするような動作は多くの場合で |
| 38 | +<i>未定義の動作</i>であり、未定義の動作に対しては |
| 39 | +<i>あらゆることが</i> |
| 40 | +なんの防御もなしに発生することになります。 |
| 41 | +実際によく起こるのは、なにか他のデータが読み書きされることです。 |
| 42 | +<a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2771r0.html" |
| 43 | +>proposals to improve C++ memory safety</a> |
| 44 | +という提案もありますが、現状では多くの C++ 組込みデータ構造(例えば配列)がメモリセーフではないため、ここでは二つの言語を同時に取り扱います。 |
| 45 | + |
| 46 | +<p> |
| 47 | +2014年に明らかになった Heartbleed(CVE-2014-0160)は、バッファオーバーリード脆弱性の一例です。 |
| 48 | +Heartbleed は OpenSSL の脆弱性です。OpenSSL は暗号プロトコルである Secure Socket Layer (SSL) とその後継である Transport Layer Security (TLS) を C で実装しており、広く使用されているツールキットです。 |
| 49 | +Heartbleed は極めて多くの、例えば |
| 50 | +Google, YouTube, Yahoo!, Pinterest, Blogspot, Instagram, Tumblr, |
| 51 | +Reddit, Netflix, Stack Overflow, Slate, GitHub, Yelp, Etsy, the |
| 52 | +U.S. Postal Service (USPS), Blogger, Dropbox, Wikipedia, and the |
| 53 | +Washington Post |
| 54 | +といった有名なウェブサイトに影響を与えました。 |
| 55 | +より詳しくは <a href="https://dwheeler.com/essays/heartbleed.html">こちら</a> を参照してください。 |
| 56 | + |
| 57 | + |
| 58 | +<p> |
| 59 | +<h2>タスクの詳細</h2> |
| 60 | +<p> |
| 61 | +ここでは OpenSSL の Heartbleed 脆弱性に対する修正を、<tt>dtls1_process_heartbeat</tt> という関数を修正することで再現します。 |
| 62 | + |
| 63 | +<p> |
| 64 | +この時点のコードでは、 |
| 65 | +<tt>s->s3->rrec.length</tt> |
| 66 | +というデータ構造はバッファの何バイトが利用可能かを表します。 |
| 67 | +もし上限サイズを確認しなければ、バッファを超えた読み出しが簡単に発生してしまいます。 |
| 68 | +以下のコードでは2か所を変更することで、この問題を修正しましょう。 |
| 69 | + |
| 70 | +<p> |
| 71 | +まず初めに、レスポンスの最短の長さである <tt>(1 + 2 + 16)</tt> が |
| 72 | +<tt>s->s3->rrec.length</tt> |
| 73 | +で取得できる長さを超えていればハートビートを送信せずに 0 を返すようにコードを変更します。 |
| 74 | +これは、ハートビートを生成するための領域が全くない場合にはハートビートを生成しないようにするための措置です。 |
| 75 | + |
| 76 | +<p> |
| 77 | +続いて、ペイロードがある場合のレスポンスの最短の長さである |
| 78 | +<tt>(1 + 2 + payload + 16)</tt> |
| 79 | +が、 |
| 80 | +<tt>s->s3->rrec.length</tt> |
| 81 | +で示されるレスポンスを格納するための領域より長い場合にも、ハートビートを送信せずに 0 を返すようコードを変更します。 |
| 82 | + |
| 83 | +<p> |
| 84 | + これにより、ハートビートそのものとペイロードがあるハートビートへの返信を格納するための領域がない場合にハートビートを作成しようとするのを防ぐことができます。 |
| 85 | + |
| 86 | +<p> |
| 87 | +これはそれほど難しい修正ではありません。 |
| 88 | +ここではほんの短いコードを追加します。 |
| 89 | +問題は、バッファの読み書きという非常に一般的な動作に対して、C と C++ はデフォルトでは安全ではないということです。 |
| 90 | +実際には、全ての範囲の全ての可能性のあるケースを<i>常に</i>チェックするというのは困難です。これが、C または C++ で書かれたプログラムでメモリ安全性に関わる脆弱性がよく見られることの理由です。 |
| 91 | +<!-- |
| 92 | + if (1 + 2 + 16 > s->s3->rrec.length) |
| 93 | + return 0; /* silently discard */ |
| 94 | +--> |
| 95 | + |
| 96 | +<p> |
| 97 | +必要に応じて「ヒント」と「諦める」のボタンを使用してください。 |
| 98 | + |
| 99 | +<p> |
| 100 | +<h2>演習 (<span id="grade"></span>)</h2> |
| 101 | +<p> |
| 102 | +<form id="lab"> |
| 103 | +<pre><code |
| 104 | +>int |
| 105 | +dtls1_process_heartbeat(SSL *s) |
| 106 | + { |
| 107 | + unsigned char *p = &s->s3->rrec.data[0], *pl; |
| 108 | + unsigned short hbtype; |
| 109 | + unsigned int payload; |
| 110 | + |
| 111 | + // ... いくつかの詳細はここでは省略しています |
| 112 | +<textarea id="attempt0" rows="3" cols="60" spellcheck="false"> |
| 113 | + if () |
| 114 | + ; |
| 115 | +</textarea> |
| 116 | + hbtype = *p++; |
| 117 | + n2s(p, payload); |
| 118 | +<textarea id="attempt1" rows="3" cols="60" spellcheck="false"> |
| 119 | + if () |
| 120 | + ; |
| 121 | +</textarea> |
| 122 | + // ... 後ほど memcpy でペイロードデータを新しいバッファにコピーします |
| 123 | + // もしチェックしなければ、十分な領域がないかもしれません |
| 124 | + // memcpy(bp, pl, payload); |
| 125 | +</code></pre> |
| 126 | +<button type="button" class="hintButton">ヒント</button> |
| 127 | +<button type="button" class="resetButton">リセット</button> |
| 128 | +<button type="button" class="giveUpButton">諦める</button> |
| 129 | +<br><br> |
| 130 | +<p> |
| 131 | +<i>このラボは、<a href="https://www.linuxfoundation.org/">Linux Foundation</a>のDavid A. Wheelerによって開発されました。</i> |
| 132 | +<br><br> |
| 133 | +<p> |
| 134 | + |
| 135 | +<details> |
| 136 | +<summary>引用元: ここに示した例は、<a href="https://github.com/openssl/openssl/blob/731f431497f463f3a2a97236fe0187b11c44aead/ssl/d1_both.c">OpenSSL のファイル <tt>ssl/d1_both.c</tt> |
| 137 | +commit 731f431497f4</a> から引用しました。ファイルのヘッダ部分はここを展開することで参照できます。</summary> |
| 138 | +<pre> |
| 139 | +/* ssl/d1_both.c */ |
| 140 | +/* |
| 141 | + * DTLS implementation written by Nagendra Modadugu |
| 142 | + * ( [email protected]) for the OpenSSL project 2005. |
| 143 | + */ |
| 144 | +/* ==================================================================== |
| 145 | + * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 146 | + * |
| 147 | + * Redistribution and use in source and binary forms, with or without |
| 148 | + * modification, are permitted provided that the following conditions |
| 149 | + * are met: |
| 150 | + * |
| 151 | + * 1. Redistributions of source code must retain the above copyright |
| 152 | + * notice, this list of conditions and the following disclaimer. |
| 153 | + * |
| 154 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 155 | + * notice, this list of conditions and the following disclaimer in |
| 156 | + * the documentation and/or other materials provided with the |
| 157 | + * distribution. |
| 158 | + * |
| 159 | + * 3. All advertising materials mentioning features or use of this |
| 160 | + * software must display the following acknowledgment: |
| 161 | + * "This product includes software developed by the OpenSSL Project |
| 162 | + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 163 | + * |
| 164 | + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 165 | + * endorse or promote products derived from this software without |
| 166 | + * prior written permission. For written permission, please contact |
| 167 | + |
| 168 | + * |
| 169 | + * 5. Products derived from this software may not be called "OpenSSL" |
| 170 | + * nor may "OpenSSL" appear in their names without prior written |
| 171 | + * permission of the OpenSSL Project. |
| 172 | + * |
| 173 | + * 6. Redistributions of any form whatsoever must retain the following |
| 174 | + * acknowledgment: |
| 175 | + * "This product includes software developed by the OpenSSL Project |
| 176 | + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 177 | + * |
| 178 | + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 179 | + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 180 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 181 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 182 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 183 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 184 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 185 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 186 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 187 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 188 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 189 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 190 | + * ==================================================================== |
| 191 | + * |
| 192 | + * This product includes cryptographic software written by Eric Young |
| 193 | + * ( [email protected]). This product includes software written by Tim |
| 194 | + |
| 195 | + * |
| 196 | + */ |
| 197 | +/* Copyright (C) 1995-1998 Eric Young ( [email protected]) |
| 198 | + * All rights reserved. |
| 199 | + * |
| 200 | + * This package is an SSL implementation written |
| 201 | + * by Eric Young ( [email protected]). |
| 202 | + * The implementation was written so as to conform with Netscapes SSL. |
| 203 | + * |
| 204 | + * This library is free for commercial and non-commercial use as long as |
| 205 | + * the following conditions are aheared to. The following conditions |
| 206 | + * apply to all code found in this distribution, be it the RC4, RSA, |
| 207 | + * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 208 | + * included with this distribution is covered by the same copyright terms |
| 209 | + * except that the holder is Tim Hudson ( [email protected]). |
| 210 | + * |
| 211 | + * Copyright remains Eric Young's, and as such any Copyright notices in |
| 212 | + * the code are not to be removed. |
| 213 | + * If this package is used in a product, Eric Young should be given attribution |
| 214 | + * as the author of the parts of the library used. |
| 215 | + * This can be in the form of a textual message at program startup or |
| 216 | + * in documentation (online or textual) provided with the package. |
| 217 | + * |
| 218 | + * Redistribution and use in source and binary forms, with or without |
| 219 | + * modification, are permitted provided that the following conditions |
| 220 | + * are met: |
| 221 | + * 1. Redistributions of source code must retain the copyright |
| 222 | + * notice, this list of conditions and the following disclaimer. |
| 223 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 224 | + * notice, this list of conditions and the following disclaimer in the |
| 225 | + * documentation and/or other materials provided with the distribution. |
| 226 | + * 3. All advertising materials mentioning features or use of this software |
| 227 | + * must display the following acknowledgement: |
| 228 | + * "This product includes cryptographic software written by |
| 229 | + |
| 230 | + * The word 'cryptographic' can be left out if the rouines from the library |
| 231 | + * being used are not cryptographic related :-). |
| 232 | + * 4. If you include any Windows specific code (or a derivative thereof) from |
| 233 | + * the apps directory (application code) you must include an acknowledgement: |
| 234 | + * "This product includes software written by Tim Hudson ( [email protected])" |
| 235 | + * |
| 236 | + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 237 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 238 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 239 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 240 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 241 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 242 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 243 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 244 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 245 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 246 | + * SUCH DAMAGE. |
| 247 | + * |
| 248 | + * The licence and distribution terms for any publically available version or |
| 249 | + * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 250 | + * copied and put under another distribution licence |
| 251 | + * [including the GNU Public Licence.] |
| 252 | + */ |
| 253 | +</pre> |
| 254 | +</details> |
| 255 | +<br><br> |
| 256 | +<p id="correctStamp" class="small"> |
| 257 | +<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly> |
| 258 | +</textarea> |
| 259 | +</form> |
| 260 | +</div><!-- End GitHub pages formatting --> |
| 261 | +</body> |
| 262 | +</html> |
0 commit comments