如何防範代碼的 finalizer() 漏洞?

來源:互聯網
上載者:User

剛看了一篇文章,講在java世界裡析構方法可能引起的漏洞以及如何防範。

馬上在C#中試了一下,發現.net也有同樣的問題。代碼如下

 1 using System;
 2 using System.Threading;
 3 
 4 namespace TestProj
 5 {
 6 
 7 
 8     class Vulnerable
 9     {
10         int value = 0;
11 
12         public Vulnerable(int value)
13         {
14             if (value <= 0)
15             {
16                 throw new ArgumentException("Vulnerable value must be positive");
17             }
18             this.value = value;
19         }
20         public override string ToString()
21         {
22             return (value.ToString());
23         }
24     }
25 
26     class AttackVulnerable : Vulnerable
27     {
28         static Vulnerable vulnerable;
29 
30         public AttackVulnerable(int value)
31             : base(value)
32         {
33         }
34 
35         ~AttackVulnerable()
36         {
37             vulnerable = this;
38         }
39 
40         public static void Main(string[] args)
41         {
42             try
43             {
44                 new AttackVulnerable(-1);
45             }
46             catch (Exception e)
47             {
48                 Console.WriteLine(e.Message);
49             }
50             GC.Collect();
51             Thread.Sleep(1000);//wait for gc to complete
52             if (vulnerable != null)
53             {
54                 Console.WriteLine("Vulnerable object " + vulnerable + " created!");
55             }
56             Console.ReadLine();
57         }
58     }

59 } 

運行結果如下

很明顯結果是漏洞存在。

關鍵是原文中給出的java世界的解決辦法不知在C#中有無對應的實現?真心求教部落格園各位高人。

java世界中可以在調用自身構造器和調用object構造器(其實是基類構造器啦)中間執行一段static代碼感覺挺牛的。不知道java中的那個Void怎麼在c#中實現呢?

 

[Update].net中可以這樣實現

 1 using System;
 2 using System.Threading;
 3 
 4 namespace TestProj
 5 {
 6     class Invulnerable
 7     {
 8         int value = 0;
 9 
10         public Invulnerable(int value)
11         {
12             GC.SuppressFinalize(this);
13             if (value <= 0)
14             {
15                 throw new ArgumentException("Invulnerable value must be positive");
16             }
17             this.value = value;
18         }
19 
20         public override string ToString()
21         {
22             return (value.ToString());
23         }
24     }
25 
26     class AttackInvulnerable : Invulnerable
27     {
28         static Invulnerable vulnerable;
29 
30         public AttackInvulnerable(int value)
31             : base(value)
32         {
33         }
34 
35         ~AttackInvulnerable()
36         {
37             vulnerable = this;
38         }
39 
40         public static void Main(string[] args)
41         {
42             try
43             {
44                 new AttackInvulnerable(-1);
45             }
46             catch (Exception e)
47             {
48                 Console.WriteLine(e.Message);
49             }
50 
51             GC.Collect();
52             Thread.Sleep(1000);//wait for gc to complete
53             if (vulnerable != null)
54             {
55                 Console.WriteLine("Vulnerable object " + vulnerable + " created!");
56             }
57             else
58             {
59                 Console.WriteLine("Attack failed");
60             }
61             Console.ReadLine();
62         }
63     }

64 } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.