Hello, (forgotten) world ☂

Railsアプリ with Mina and OpenRC

目的とか

KONMAIが送る大人気リズムアクションゲーム"REFLEC BEAT groovin’!!“に向けてRefixativeの新バージョンを書いていた。今までは毎回毎回サーバーにsshしてはgit pullしてうんぬんかんぬん、とやっていたので、現代的な環境を構築したかった。

Different Behaviors of DownloadString and Uri between .NET and Mono

Note: This is the summary of my posts (they are written in Japanese):

I already reported this as a bug for Xamarin’s Bugzilla.

Introduction

I found the different behavior of WebClient.DownloadString(String) between Mono and .NET Framework when an invalid URI passed to it. In the Mono’s implementation, it may cause a security issue.

This causes by two different behaviors, in new Uri(String), and Path.GetFullPath(String).

DownloadString

DownloadString(String) and some methods (e.g. DownloadFile(String), OpenRead(String), etc.) calls CreateUri(String), a private method of WebClient. (-> source code on github)

CreateUri and GetUri

CreateUri(String) tries to make an instance of Uri with new Uri(String). If an invalid URI passed, the constructor raises an exception. For example, new Uri("http://../../../etc/passwd") will be failed because its hostname part (..) is invalid. However, the failure will be ignored, and CreateUri(String) returns new Uri(Path.GetFullPath(String)). It means the local file address with the full path will be returned.

結局Monoと.NETの挙動の違いはなんだったのか

続・アレな文字をWebClient.DownloadString(String)に渡すとローカルのファイルが読める

ここ2つの記事でMonoのWebClient.DownloadString(string)にアレな文字列渡すとローカルファイルを落としてきてしまうという挙動について調べてたわけですが、よくよくスタックトレースを見てみると、.NET FrameworkでもGetUriというメソッドを経由してPath.GetFullPathが呼ばれていたことがわかりました。

MonoのWebClientにおけるURI

発端

発端は前の記事にあるように、tkbctf3の問題としてmiocatなるものを出してみたのはいいものの、意図とは異なる脆弱性を作り込んで250点問題が超絶ボーナス問題になりましたよ、というお話しです。

調査

miocatはC#で書かれており、実際の運用ではMonoランタイムで動いていました。というわけでMonoのソースコードを読めば解決です。やったね。

そういうわけでまずはWebClientの実装を読んでみたのですが、怪しい箇所が一発で見つかりました。WebClient.cs#798、privateメソッドであるCreateUri(string)なるメソッドです。DownloadString(string)は、その引数をこのメソッドに渡してDownloadData(Uri)を呼び出します。

try-catchの中で渡されたアドレス(と、baseAddress)を元にUriのインスタンスを作ってCreateUri(Uri)に渡していますが、ここで例外が発生するとreturn new Uri(Path.GetFullPath(address))という恐怖のコードが走ります。

tkbctf3 miocat

miocat(みお・きゃっと)はとある伝説の脱ヲタエンジニアが書き上げた伝説のプログラムに着想を得てっていうのはどうでもいいとして、実際のところは.NET Frameworkのとある仕様に関する問題です。でした。以下本来の意図の説明です。

ソースコード

と、その前に。今回のソースコードはtkbctf/archiveに上げておきます。結構問題の核心以外の部分で苦労してたりしますがそれはそれということで。

追記: 上げました -> tkbctf/archive/tkbctf3/web250_miocat

MacのSublime Text 3でもclang使って整形とか補完とかしたい

clang-formatによるコード整形

MacのCommand Line Toolsにはclang-formatがない。clang-formatというのはその名前の通り、ソースコードをclangに静的解析させて自動で整形しようというツール。

対応しているコーディングスタイルは次の5つ。

  • LLVM
  • Google
  • Chromium
  • Mozilla
  • WebKit

また、全く独自のコーディングスタイルを設定することもできるし、あるいはこれらのコーディングスタイルを元に一部を自分好みに変更した設定も可能である。かなり自由。

RailsAdminの日時選択が日本語でバグる

概略

Rails Adminでdatetime型(日時)のカラムを含むモデルを操作する際、ロケールが日本語で、かつsvenfuchs/rails-i18nのファイルをそのまま使っていると例外ArgumentError(argument out of range)で死ぬ。

原因

そもそも内部で使ってるDate._parse(これはRubyの標準ライブラリである’time’に含まれる)が日本語形式(y年m月d日)での日付のパースに対応していない。