-
Notifications
You must be signed in to change notification settings - Fork 3
/
On the Violation of Java Access Control.htm
344 lines (259 loc) · 14.4 KB
/
On the Violation of Java Access Control.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- begin header.php -->
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> On the Violation of Java Access Control</title>
<meta name="generator" content="WordPress 2.8.6" /> <!-- leave this for stats -->
<!--
<link href="http://stuffthathappens.com/blog/wp-content/themes/stufftheme/reset.css"
type="text/css" rel="stylesheet"/>
-->
<link rel="stylesheet" href="http://stuffthathappens.com/blog/wp-content/themes/stufftheme/style.css" type="text/css" media="screen" />
<!-- Google Code Prettify -->
<link href="http://stuffthathappens.com/blog/wp-content/themes/stufftheme/prettify.css"
type="text/css" rel="stylesheet"/>
<script type="text/javascript"
src="http://stuffthathappens.com/blog/wp-content/themes/stufftheme/prettify.js"></script>
<script type="text/javascript">
window.google_analytics_uacct = "UA-1330556-5";
</script>
<link rel="alternate" type="application/atom+xml" title="It’s Just a Bunch of Stuff That Happens Atom Feed" href="http://stuffthathappens.com/blog/feed/atom/" />
<link rel="pingback" href="http://stuffthathappens.com/blog/xmlrpc.php" />
<link rel="shortcut icon" href="http://stuffthathappens.com/favicon.ico" />
<link rel="alternate" type="application/rss+xml" title="It's Just a Bunch of Stuff That Happens » On the Violation of Java Access Control Comments Feed" href="http://stuffthathappens.com/blog/2007/10/11/on-the-violation-of-java-access-control/feed/" />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://stuffthathappens.com/blog/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://stuffthathappens.com/blog/wp-includes/wlwmanifest.xml" />
<link rel='index' title='It's Just a Bunch of Stuff That Happens' href='http://stuffthathappens.com/blog' />
<link rel='start' title='Fresh Start' href='http://stuffthathappens.com/blog/2007/09/06/fresh-start/' />
<link rel='prev' title='BPM: Not Even Comic-Worthy' href='http://stuffthathappens.com/blog/2007/10/10/bpm-not-even-comic-worthy/' />
<link rel='next' title='You CAN Change Final Fields' href='http://stuffthathappens.com/blog/2007/10/13/you-can-change-final-fields/' />
<meta name="generator" content="WordPress 2.8.6" />
</head>
<body onload="prettyPrint()">
<div id="page">
<a href="http://stuffthathappens.com/blog/">
<div id="header">
<hr/>
</div>
</a>
<div id="nav_area">
<a href="http://stuffthathappens.com/blog/">Home</a>
•
<a href="http://stuffthathappens.com/blog/about/">About</a>
•
<a href="http://stuffthathappens.com/blog/archives/">Archives</a>
•
<!-- SiteSearch Google -->
<form id="searchform" method="get" action="http://www.google.com/custom" target="_top">
<input type="hidden" name="domains" value="stuffthathappens.com"></input>
<input class="google_search_field" type="text" name="q" size="20" maxlength="255" value="" id="sbi"></input>
<input type="submit" name="sa" value="Search" id="sbb"></input>
<input type="hidden" name="sitesearch" value="stuffthathappens.com" id="ss1"></input>
<input type="hidden" name="client" value="pub-9300304187074569"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1"></input>
<input type="hidden" name="hl" value="en"></input>
</form>
<!-- SiteSearch Google -->
</div>
<br clear="all"/>
<!-- end header.php -->
<div id="content">
<div class="navigation">
<div class="alignleft">« <a href="http://stuffthathappens.com/blog/2007/10/10/bpm-not-even-comic-worthy/">BPM: Not Even Comic-Worthy</a></div>
<div class="alignright"><a href="http://stuffthathappens.com/blog/2007/10/13/you-can-change-final-fields/">You CAN Change Final Fields</a> »</div>
<br clear="all"/>
</div> <!-- navigation -->
<div class="post" id="post-83">
<h1 class="post_title"><a href="http://stuffthathappens.com/blog/2007/10/11/on-the-violation-of-java-access-control/" rel="bookmark"
title="Permanent Link: On the Violation of Java Access Control">On the Violation of Java Access Control</a></h1>
<div class="post_published_date">
October 11th, 2007 </div>
<div class="entry">
<p>Let’s write a simple class and show how easy it is to bypass normal Java language access checking. This means we can modify another class’s private data using reflection. Good times…</p>
<pre class="prettyprint">package pkgb;
public class Caveman {
private String name;
private final int age = 10;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}</pre>
<p>Can you modify name, even thought it is private? Can you modify age, even though it is private <strong>and</strong> final?</p>
<h3>First, What Won’t Work…</h3>
<p>You cannot simply create a new <code>Caveman</code> and set its name. This will not compile because <code>name</code> is private. Instead, let’s try reflection:</p>
<pre class="prettyprint">public class Main {
public static void main(String[] args) throws NoSuchFieldException {
Caveman caveman = new Caveman();
// note that getField(...) throws NoSuchFieldException
Field nameField = Caveman.class.getDeclaredField("name");
try {
nameField.set(caveman, "Opie");
} catch (IllegalAccessException e) {
System.out.println("Unable to set name to Opie.");
}
...</pre>
<p>Notice I use <code>getDeclaredField()</code> instead of <code>getField()</code>. If you try this, you will find you are <em>unable to set the value of the name</em>. It is private, after all.</p>
<h3>setAccessible(true) to the Rescue!</h3>
<p>You need to call <code>setAccessible(true)</code> first:</p>
<pre class="prettyprint"> nameField.setAccessible(true);
try {
nameField.set(caveman, "Anthony");
System.out.println("Successfully set name to " + caveman.getName());
} catch (IllegalAccessException e) {
// won't happen
}</pre>
<p>Voila! It works. This is a Good Thing, because without this capability, tools like JPA (and many others) would be severely constrained.</p>
<h3>Can we Change the Age?</h3>
<p>The <code>age</code> field is <code>private</code> and <code>final</code>. Can we use reflection to change it as well? Let’s try:</p>
<pre class="prettyprint"> Field ageField = Caveman.class.getDeclaredField("age");
ageField.setAccessible(true);
try {
System.out.println("Setting age to 100...");
ageField.setInt(caveman, 100);
System.out.println("Age is now " + caveman.getAge());
System.out.println("Is it final? " +
Modifier.isFinal(ageField.getModifiers()));
} catch (IllegalAccessException e) {
System.out.println("Unable to set age.");
}</pre>
<p>What does the code do?</p>
<p><strong>UPDATE: See <a href="http://stuffthathappens.com/blog/2007/10/13/you-can-change-final-fields/">this post</a> for a correction!</strong><br />
<strike><br />
<strong>It silently fails.</strong></p>
<p>The console prints this:</p>
<pre>Setting age to 100...
Age is now 10.
Is it final? true</pre>
<p>Yikes. As expected, you CANNOT change final fields. Phew! But surprisingly, attempting to set the value does not throw an exception. Instead, it silently ignores your request.</p>
<p><strong>If you need to use reflection to set field values, be sure you check the modifiers (final, etc…) before you do so.</strong><br />
</strike></p>
<h3>Is this Secure?</h3>
<p>If you are running under a <code>SecurityManager</code>, you might not be able to do any of this. You can verify this quite easily:</p>
<pre class="prettyprint">public static void main(String[] args) throws NoSuchFieldException {
System.setSecurityManager(new SecurityManager());
...code just like before</pre>
<p>Now, calling <code>setAccessible(true)</code> throws an exception:</p>
<pre>Exception in thread "main" java.security.AccessControlException: access denied
(java.lang.reflect.ReflectPermission suppressAccessChecks)
at java.security.AccessControlContext.checkPermission
(AccessControlContext.java:323)
at java.security.AccessController.checkPermission
(AccessController.java:546)
etc...</pre>
<p>So if you need to use these techniques in an app server that contains a security manager, you’ll need to edit a policy file and allow suppressAccessChecks. This isn’t my area of expertise, but I can point you to the right Google search: <a href="http://www.google.com/search?q=policy+file+suppressAccessChecks">policy file suppressAccessChecks</a>.</p>
<br clear="all"/>
<p class="postmetadata alt">
<small>
Posted on Thursday, October 11th, 2007 at 10:10 pm in <a href="http://stuffthathappens.com/blog/category/java/" title="View all posts in Java" rel="category tag">Java</a>.
</small>
</p>
</div> <!-- entry -->
</div> <!-- post -->
<!-- You can start editing here. -->
<div class="commentlist">
<div id="comment-291" class="comment">
<div class="c_sidebar">
<div class="c_gravatar">
<img width="80" height="80" alt=""
title="Get your avatar at gravatar.com"
src="http://www.gravatar.com/avatar.php?gravatar_id=89c8afd032c7b3473f67c9b00d3acd5a&rating=PG&size=80&default=http%3A%2F%2Fstuffthathappens.com%2Fblog%2Fwp-content%2Fthemes%2Fstufftheme%2Fimages%2Fanon-avatar.png"/>
</div> <!-- c_gravatar -->
</div> <!-- c_sidebar -->
<div class="c_main">
<div class="c_author">
<cite><a href='http://tech.puredanger.com' rel='external nofollow' class='url'>Alex Miler</a></cite> Says:
</div> <!-- c_author -->
<div class="c_content">
<p>Or of course, you can just modify the byte code when the class is loaded so the field is no longer final…. <img src='http://stuffthathappens.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Trivial to do with ASM.</p>
</div> <!-- c_content -->
</div> <!-- c_main -->
<div class="c_meta">
<small>
<a href="#comment-291" title="">October 12th, 2007
at 9:25 am</a> </small>
</div> <!-- c_meta -->
<div class="c_clear"/>
</div> <!-- comment -->
<div id="comment-298" class="comment">
<div class="c_sidebar">
<div class="c_gravatar">
<img width="80" height="80" alt=""
title="Get your avatar at gravatar.com"
src="http://www.gravatar.com/avatar.php?gravatar_id=8cfa8e1f48ff3c0613dcbbc35daf6662&rating=PG&size=80&default=http%3A%2F%2Fstuffthathappens.com%2Fblog%2Fwp-content%2Fthemes%2Fstufftheme%2Fimages%2Fanon-avatar.png"/>
</div> <!-- c_gravatar -->
</div> <!-- c_sidebar -->
<div class="c_main">
<div class="c_author">
<cite><a href='http://blog.adjective.org/' rel='external nofollow' class='url'>Tim Vernum</a></cite> Says:
</div> <!-- c_author -->
<div class="c_content">
<p>I think you’re actually falling into an optimisation trap, not an access control trap.<br />
Try calling:<br />
System.out.println(”Age field is ” + ageField.get(caveman));<br />
And you should find it is 100.</p>
<p>The reason your code prints 10 is most likely due to the compiler optimising the implementation of getAge(). Because age is final, and has a single fixed value (10), it is possible for the compiler to optimise the implementation of “getAge()” to “return 10;”</p>
</div> <!-- c_content -->
</div> <!-- c_main -->
<div class="c_meta">
<small>
<a href="#comment-298" title="">October 13th, 2007
at 9:14 am</a> </small>
</div> <!-- c_meta -->
<div class="c_clear"/>
</div> <!-- comment -->
<div id="comment-303" class="comment">
<div class="c_sidebar">
<div class="c_gravatar">
<img width="80" height="80" alt=""
title="Get your avatar at gravatar.com"
src="http://www.gravatar.com/avatar.php?gravatar_id=d41d8cd98f00b204e9800998ecf8427e&rating=PG&size=80&default=http%3A%2F%2Fstuffthathappens.com%2Fblog%2Fwp-content%2Fthemes%2Fstufftheme%2Fimages%2Fanon-avatar.png"/>
</div> <!-- c_gravatar -->
</div> <!-- c_sidebar -->
<div class="c_main">
<div class="c_author">
<cite><a href='http://stuffthathappens.com/blog/2007/10/13/you-can-change-final-fields/' rel='external nofollow' class='url'>It’s Just a Bunch of Stuff That Happens » Blog Archive » You CAN Change Final Fields</a></cite> Says:
</div> <!-- c_author -->
<div class="c_content">
<p>[...] On the Violation of Java Access Control Oct [...]</p>
</div> <!-- c_content -->
</div> <!-- c_main -->
<div class="c_meta">
<small>
<a href="#comment-303" title="">October 13th, 2007
at 8:13 pm</a> </small>
</div> <!-- c_meta -->
<div class="c_clear"/>
</div> <!-- comment -->
</div> <!-- commentlist -->
</div> <!-- content -->
<hr />
<div id="footer">
<p>
Follow
<cite>It’s Just a Bunch of Stuff That Happens</cite> via the
<a class="small_feed_link" href="http://stuffthathappens.com/blog/feed/atom/">Entries</a>
and
<a class="small_feed_link" href="http://stuffthathappens.com/blog/comments/feed/atom/">Comments</a>
Atom feeds.
</p>
</div> <!-- footer -->
</div> <!-- page -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1330556-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>
<!-- Dynamic Page Served (once) in 0.258 seconds -->