• 検索結果がありません。

Javaセキュアコーディングセミナー東京 第3回 入出力(File, Stream)と例外時の動作 演習解説

N/A
N/A
Protected

Academic year: 2021

シェア "Javaセキュアコーディングセミナー東京 第3回 入出力(File, Stream)と例外時の動作 演習解説"

Copied!
18
0
0

読み込み中.... (全文を見る)

全文

(1)

Javaセキュアコーディングセミナー東京

第3回

入出力と例外時の動作

演習解説

2012年11月11日(日)

JPCERTコーディネーションセンター

脆弱性解析チーム

戸田 洋三

Japan Computer Emergency Response Team Coordination Center

電子署名者 : Japan Computer Emergency Response Team Coordination Center

DN : c=JP, st=Tokyo, l=Chiyoda-ku, [email protected], o=Japan Computer Emergency Response Team Coordination Center, cn=Japan Computer Emergency Response Team Coordination Center

(2)

Hands-on Exercises

—コンパイルエラーに対処しよう

(3)

Hands-on Exercise(1)

サンプルコードの

(4)

以下のコードをコンパイルできるように修正せよ.

class CatFile {

public static void cat(File file) { RandomAccessFile input = null; String line = null;

try {

input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (input != null) { input.close(); } } } } class testCatFile {

public static void main(String[] args){ if (args.length >= 1) {

CatFile.cat(new File(args[0])); }

} }

(5)

コンパイルすると...

$ javac testCatFile_CantCompile.java

testCatFile_CantCompile.java:19: error: unreported exception IOException; must be caught or declared to be thrown

input.close(); ^

testCatFile_CantCompile.java:12: error: unreported exception

FileNotFoundException; must be caught or declared to be thrown input = new RandomAccessFile(file, "r");

^

testCatFile_CantCompile.java:13: error: unreported exception IOException; must be caught or declared to be thrown

while ((line = input.readLine()) != null) { ^ 3 errors $

チェック例外である IOException と

FileNotFoundException の扱いを記述する

必要がある.

(6)

修正例その1

class CatFile {

public static void cat(File file) { RandomAccessFile input = null; String line = null;

try {

input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line);

}

return;

} catch (FileNotfoundException e){ System.err.println(e.toString()); } catch (IOException e){

System.err.println(e.toString());

} finally {

if (input != null) { try {

input.close();

} catch (IOException e){

System.err.println(e.toString()); } }}} } class testCatFile { … 以下省略 ……

cat メソッドのなかで

例外を処理する修正例.

cat メソッドや

testCatFile クラスの

main メソッドで

throws 宣言する方法

もあり.

(7)

修正例その2(JavaSE7)

class CatFile {

public static void cat(File file) {

try (RandomAccessFile input = new RandomAccessFile(file, "r"); ) {

String line = null;

while ((line = input.readLine()) != null) { System.out.println(line); } return; } catch (FileNotFoundException e) { System.err.println(e.toString()); } catch (IOException e) { System.err.println(e.toString()); } } }

try-with-resource 構文を使うことで,

明示的に close() を呼び出す必要が

なくなる.

(8)

Hands-on Exercise(2)

(A)以下の testListOfNumbers.java について、コ

ンパ イル実行できるようにコードを修正せよ.

(B) ListOfNumbers クラスで出力されるファイル

OutFile.txt から整数を読み込み, その総和を計

算するコードを書け.

(C)データを読み込んだ後の OutFile.txt を削除す

るように修正せよ. symlink 攻撃を防ぐにはど

うすればよいか?

(9)

testListOfNumbers.java (1/3)

/*

* Copyright (c) 1995, 2008, Oracle and/or its affiliates.

* All rights reserved.

*/

import java.io.*;

import java.util.Vector;

class ListOfNumbers {

private Vector<Integer> victor;

private static final int SIZE = 10;

public ListOfNumbers () {

victor = new Vector<Integer>(SIZE);

for (int i = 0; i < SIZE; i++)

(10)

public void writeList() {

PrintWriter out = null;

try {

System.out.println("Entering try statement");

out = new PrintWriter(new FileWriter("OutFile.txt"));

for (int i = 0; i < SIZE; i++)

out.println(victor.elementAt(i));

} finally {

if (out != null) {

System.out.println("Closing PrintWriter");

out.close();

} else {

System.out.println("PrintWriter not open");

}

}

}

}

(11)

class testListOfNumbers {

public static void main(String[] args){

ListOfNumbers lon = new ListOfNumbers();

lon.writeList();

}

}

(12)

Hands-on Exercise(2)

(A)以下の testListOfNumbers.java について、コ

ンパ イル実行できるようにコードを修正せよ.

(B) ListOfNumbers クラスで出力されるファイル

OutFile.txt から整数を読み込み, その総和を計

算するコードを書け.

(C)データを読み込んだ後の OutFile.txt を削除す

るように修正せよ. symlink 攻撃を防ぐにはど

うすればよいか?

•FileWriter() が IOException をスローする可能性

•elementAt() が ArrayIndexOutOfBoundsException を

スローする可能性

(13)

修正例: writeList()

public void writeList() {

PrintWriter out = null;

try {

System.out.println("Entering try statement");

out = new PrintWriter(new FileWriter("OutFile.txt"));

for (int i = 0; i < SIZE; i++)

out.println(victor.elementAt(i));

} catch (ArrayIndexOutOfBoundsException e){

System.err.println(“Caught ArrayIndexOutOfboundsException: “

+ e.getMessage());

} catch (IOException e){

System.err.println(“Caught IOException: “ + e.getMessage());

} finally {

if (out != null) {

System.out.println("Closing PrintWriter");

out.close();

} else {

System.out.println("PrintWriter not open");

}

(14)

Hands-on Exercise(2)

(A)以下の testListOfNumbers.java について、コ

ンパ イル実行できるようにコードを修正せよ.

(B) ListOfNumbers クラスで出力されるファイル

OutFile.txt から整数を読み込み, その総和を計

算するコードを書け.

(C)データを読み込んだ後の OutFile.txt を削除す

るように修正せよ. symlink 攻撃を防ぐにはど

うすればよいか?

以下のコード例 sum.java では writeList() に対応して

readList() をつくった. main() メソッドで OutFile.txt を

生成するところも一緒に入れた.

(15)

コード例 sum.java

(1/2)

/*

* sum.java

* call ListNumbers to generate outfile.txt

* read outfile.txt and compute and print the sum of the numbers

*/

class readList {

public Vector<Integer> readList(String filename) throws IOException {

Vector<Integer> victor = new Vector<Integer>();

try ( BufferedReader in =

new BufferedReader(new FileReader(filename)); ) {

String line;

while ((line = in.readLine()) != null) {

victor.addElement(Integer.parseInt(line));

}

}

return victor;

}

(16)

コード例 sum.java

(2/2)

class sum {

private static final String FILENAME = “OutFile.txt”;

public static void main(String[] args) throws IOException {

ListOfNumbers lon = new ListOfNumbers();

lon.writeList(); // file generated

System.out.println(“reading...”);

readList rl = new readList();

Vector<Integer> v = rl.readList(FILENAME);

Integer total = 0;

for (Integer i : v){ total = total + i; }

System.out.println(“total: “ + total);

}

(17)

Hands-on Exercise(2)

(A)以下の testListOfNumbers.java について、コ

ンパ イル実行できるようにコードを修正せよ.

(B) ListOfNumbers クラスで出力されるファイル

OutFile.txt から整数を読み込み, その総和を計

算するコードを書け.

(C)データを読み込んだ後の OutFile.txt を削除す

るように修正せよ. symlink 攻撃を防ぐにはど

うすればよいか?

以下のコード例 sum1.java では

•main() メソッドの最初で, ファイル操作を行うディレ

クトリがセキュアディレクトリであることを確認

具体的なコードについては FIO-00J を参照

•main() メソッドの最後でファイルを削除

(18)

コード例 sum1.java

class sum1 {

private static final String FILENAME = “OutFile.txt”;

public static void main(String[] args) throws IOException {

// まず最初にカレントディレクトリがセキュアディレクトリであることを確認する

// Java セキュアコーディングスタンダード FIO00-J で紹介している

// isInSecureDir() メソッドを参照 (POSIX系のファイルシステムの場合)

//

https://www.jpcert.or.jp/java-rules/fio00-j.html

ListOfNumbers lon = new ListOfNumbers();

lon.writeList(); // file generated

System.out.println(“reading...”);

readList rl = new readList();

Vector<Integer> v = rl.readList(FILENAME);

Integer total = 0;

for (Integer i : v){ total = total + i; }

System.out.println(“total: “ + total);

File outfile = new File(FILENAME);

outfile.delete();

}

}

参照

関連したドキュメント

In order to improve the coordination of signal setting with traffic assignment, this paper created a traffic control algorithm considering traffic assignment; meanwhile, the link

Copyright (C) Qoo10 Japan All Rights Reserved... Copyright (C) Qoo10 Japan All

KINAN RACING TEAM / キナンレーシングチーム SHIMANO RACING TEAM / シマノレーシングチーム MATRIX POWERTAG / マトリックスパワータグ NASU BLASEN /

Clifford analysis, octonions; non-linear potential theory, classical and fine potential theory, holo- morphic and finely holomorphic functions; dif- ferential geometry and

These covered basic theory: analytic and probabilistic tools; dif- fusion processes; jump processes; connec- tions with systmes of stochastic ordinary differential equations

While Team Bear had some teammates who don’t enjoy heights, Team Lion seemed to have no fear at all. You finished the challenge quicker than Team Bear, but you also argued more

第2 この指導指針が対象とする開発行為は、東京における自然の保護と回復に関する条例(平成12年東 京都条例第 216 号。以下「条例」という。)第 47

[r]