1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
2 |
<html lang="en-US"> |
3 |
<head> |
4 |
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> |
5 |
<meta http-equiv="Content-Style-Type" content="text/css"> |
6 |
<title>The world of TOMOYO Linux The second installment: "Let's experience access control."</title> |
7 |
<link rel="stylesheet" href="http://tomoyo.sourceforge.jp/tomoyo.css" media="all" type="text/css"> |
8 |
</head> |
9 |
<body> |
10 |
<p style="text-align:right;"><a href="tutorial-2.html.ja">Japanese Page</a></p> |
11 |
<p style="text-align:right;">Last modified: $Date$</p> |
12 |
|
13 |
<h1>The world of TOMOYO Linux<br>The second installment: "Let's experience access control."</h1> |
14 |
|
15 |
<h2>Contents of this installment.</h2> |
16 |
|
17 |
<p>In the previous installment, I explained steps for installing TOMOYO Linux and steps for using automatic learning mode on files and steps for saving the learned result. In this installment, I explain enforcing mode and profiles in TOMOYO Linux and steps for restricting access using enforcing mode and profiles.</p> |
18 |
|
19 |
<h2>Access control modes and profiles</h2> |
20 |
|
21 |
<h3>About access control modes</h3> |
22 |
|
23 |
<p>In the previous installment, /etc/ccs/profile.conf has contents listed in Fig. 1. In Fig.1, string specified in "mode=" parameter is called access control mode. Access control mode takes one of disabled / learning / permissive / enforcing , and their meaning is described in Fig. 2.</p> |
24 |
|
25 |
<table border="1" summary="fig"> |
26 |
<tr><td> |
27 |
♦ Fig. 1 Profile used by previous installment |
28 |
<pre> |
29 |
PROFILE_VERSION=20090903 |
30 |
0-CONFIG::file={ mode=learning } |
31 |
</pre> |
32 |
</td></tr> |
33 |
</table> |
34 |
|
35 |
<table border="1" summary="fig"> |
36 |
<tr><td> |
37 |
♦ Fig. 2 Access control modes<br> |
38 |
<table border="1" summary="fig"> |
39 |
<tr><td>Mode</td><td>Meaning</td></tr> |
40 |
<tr><td>disabled</td><td>Works as if regular kernel.</td></tr> |
41 |
<tr><td>learning</td><td>An access request is not rejected even if the request violates policy.<br>The permission to allow the request is automatically added to policy so that the same request no longer violates policy.</td></tr> |
42 |
<tr><td>permissive</td><td>An access request is not rejected even if the request violates policy.</td></tr> |
43 |
<tr><td>enforcing</td><td>An access request is rejected if the request violates policy.</td></tr> |
44 |
</table> |
45 |
</td></tr> |
46 |
</table> |
47 |
|
48 |
<p>In the previous installment, TOMOYO Linux was running in learning mode because "learning" is specified. As a result, all accesses on files were granted and permissions were automatically appended to policy which was loaded upon boot.</p> |
49 |
|
50 |
<p>The basic procedure of defining policy is shown below.</p> |
51 |
|
52 |
<ol> |
53 |
<li>Assign learning mode⋅⋅⋅Decide domains to restrict access. Generate policy by doing operations you want to allow.</li> |
54 |
<li>Assign permissive mode⋅⋅⋅Make sure that all permissions for doing operations you want to allow are included in policy. Tune policy as needed.</li> |
55 |
<li>Assign enforcing mode⋅⋅⋅Enable access restrictions.</li> |
56 |
</ol> |
57 |
|
58 |
<p>In this installment, let's use permissive mode and enforcing mode in accordance with this procedure.</p> |
59 |
|
60 |
<p>To change access control mode upon boot, edit /etc/ccs/profile.conf . To change access control mode after boot, use ccs-setlevel command or ccs-editpolicy command. For example, Fig. 3 changes to permissive mode, Fig. 4 changes to enforcing mode. (TOMOYO Linux's permissive mode corresponds with SELinux's permissive mode, TOMOYO Linux's enforcing mode corresponds with SELinux's enforcing mode.)</p> |
61 |
|
62 |
<table border="1" summary="fig"> |
63 |
<tr><td> |
64 |
♦ Fig. 3 Changing to permissive mode |
65 |
<pre> |
66 |
# /usr/sbin/ccs-setlevel '0-CONFIG::file={ mode=permissive }' |
67 |
0-CONFIG::file={ mode=permissive grant_log=yes reject_log=yes } |
68 |
</pre> |
69 |
</td></tr> |
70 |
</table> |
71 |
|
72 |
<table border="1" summary="fig"> |
73 |
<tr><td> |
74 |
♦ Fig. 4 Changing to enforcing mode |
75 |
<pre> |
76 |
# /usr/sbin/ccs-setlevel '0-CONFIG::file={ mode=enforcing }' |
77 |
0-CONFIG::file={ mode=enforcing grant_log=yes reject_log=yes } |
78 |
</pre> |
79 |
</td></tr> |
80 |
</table> |
81 |
|
82 |
<p>TOMOYO Linux can perform access control on not only files but also networks and capabilities and more. You can know current coverage from /proc/ccs/profile . But enabling many access controls from the beginning would confuse you. Thus, I enable only access control on files in this installment. You can try enabling other access controls as you get understand how to use.</p> |
83 |
|
84 |
<h3>About profiles</h3> |
85 |
|
86 |
<p>In TOMOYO Linux, you can specify access control modes for per a domain basis. You can change access control modes of arbitrary domains independent of the rest of domains. To specify access control modes independently, TOMOYO Linux uses definition of access control modes called "profile" (Fig. 5) and assigns profiles using ccs-setprofile command or ccs-editpolicy command. (Fig. 6) I explain usage of ccs-setprofile command later.</p> |
87 |
|
88 |
<table border="1" summary="fig"> |
89 |
<tr><td> |
90 |
♦ Fig. 5 Create profiles<br> |
91 |
<img src="tutorial/fig-2-5-en.png" alt="fig-2-5-en.png" width="640" height="320"> |
92 |
</td></tr> |
93 |
</table> |
94 |
|
95 |
<table border="1" summary="fig"> |
96 |
<tr><td> |
97 |
♦ Fig. 6 Assign profiles to domains<br> |
98 |
<img src="tutorial/fig-2-6.png" alt="fig-2-6.png" width="800" height="600"> |
99 |
</td></tr> |
100 |
</table> |
101 |
|
102 |
<h4>♦Creating profiles</h4> |
103 |
|
104 |
<p>In this series, I use 4 profiles. Please overwrite /etc/ccs/profile.conf with the contents listed in Fig. 7.</p> |
105 |
|
106 |
<table border="1" summary="fig"> |
107 |
<tr><td> |
108 |
♦ Fig. 7 Profiles used in this series |
109 |
<pre> |
110 |
PROFILE_VERSION=20090903 |
111 |
0-CONFIG::file={ mode=disabled } |
112 |
1-CONFIG::file={ mode=learning } |
113 |
2-CONFIG::file={ mode=permissive } |
114 |
3-CONFIG::file={ mode=enforcing } |
115 |
</pre> |
116 |
</td></tr> |
117 |
</table> |
118 |
|
119 |
<p>The leading integer is the profile number (which can take from 0 to 255) and lines with the same profile number belong to the same profile. The assignment of profile numbers is arbitrary. But to make it easier to associate access control modes with profile numbers, this series uses profile 0 as a profile with disabled mode, profile 1 as a profile with learning mode for appending permissions, profile 2 as a profile with permissive mode for verifying permissions, profile 3 as a profile with enforcing mode for access restriction.</p> |
120 |
|
121 |
<p>Overwrite /etc/ccs/profile.conf with the contents listed in Fig. 7 and reboot the system with TOMOYO Linux kernel or run command in Fig. 8 in order to reflect profile changes.</p> |
122 |
|
123 |
<table border="1" summary="fig"> |
124 |
<tr><td> |
125 |
♦ Fig. 8 Reloading profiles |
126 |
<pre> |
127 |
# /usr/sbin/ccs-loadpolicy p |
128 |
</pre> |
129 |
</td></tr> |
130 |
</table> |
131 |
|
132 |
<h2>Let's protect WWW services</h2> |
133 |
|
134 |
<p>I explain steps for creating policy for Apache as an example of mandatory access control. The pathname of Apache's main program depends on your distribution. For example, /usr/sbin/httpd for CentOS, /usr/sbin/apache2 for Debian.</p> |
135 |
|
136 |
<h3>Updating exception policy</h3> |
137 |
|
138 |
<p>Firstly, specify the pathname of Apache's main program using "initialize_domain" keyword. Run ccs-editpolicy with "e" option. (Fig. 9)</p> |
139 |
|
140 |
<table border="1" summary="fig"> |
141 |
<tr><td> |
142 |
♦ Fig. 9 Executing policy editor |
143 |
<pre> |
144 |
# /usr/sbin/ccs-editpolicy e |
145 |
</pre> |
146 |
</td></tr> |
147 |
</table> |
148 |
|
149 |
<p>Then, you will see a screen titled "<<< Exception Policy Editor >>>". You will find lines starting with "initialize_domain" keyword by scrolling the screen. (Fig. 10. The contents depend on your environment.)</p> |
150 |
|
151 |
<table border="1" summary="fig"> |
152 |
<tr><td> |
153 |
♦ Fig. 10 initialize_domain keyword in exception policy<br> |
154 |
<img src="tutorial/fig-2-10.png" alt="fig-2-10.png" width="720" height="400"> |
155 |
</td></tr> |
156 |
</table> |
157 |
|
158 |
<p>If you have followed steps in the first installment, there should already be a line "initialize_domain /usr/sbin/httpd". But if you can't find the line, append by following steps.</p> |
159 |
|
160 |
<p>First, press "A" key on the keyboard, and a prompt "Enter new entry>" is printed at the bottom line of the screen. Then, enter "initialize_domain /usr/sbin/httpd" and press "Enter" key, and the line you entered will be added. On the contrary, to delete this entry, move the cursor to "initialize_domain /usr/sbin/httpd" line by using up-arrow and down-arrow keys. Press "D" key on the keyboard, and a prompt "Delete selected entry? ('Y'es/'N'o)" is printed. Then press "Y" key to delete the line.</p> |
161 |
|
162 |
<p>In TOMOYO Linux, different domains are assigned to the same program if the program was executed from different domains. This distinction is useful for giving different set of permissions depending on situation. But sometimes, like daemon processes, we want to give same set of permissions independent of situation. To be able to give same set of permissions independent of situation, TOMOYO Linux provides "initialize_domain" keyword.</p> |
163 |
|
164 |
<p>To let a program run in the same domain no matter how the program is executed, specify the program using "initialize_domain" keyword. Programs specified using "initialize_domain" keyword runs in the child of "<kernel>" domain for both automatically executed upon startup scripts and manually restarted by administrator's login session. (Fig. 11)</p> |
165 |
|
166 |
<table border="1" summary="fig"> |
167 |
<tr><td> |
168 |
♦ Fig. 11 The effect of initialize_domain keyword<br> |
169 |
<img src="tutorial/fig-3-3.png" alt="fig-3-3.png" width="800" height="500"> |
170 |
</td></tr> |
171 |
</table> |
172 |
|
173 |
<h3>Starting the program</h3> |
174 |
|
175 |
<p>First, create a domain for running Apache. (Fig. 12)</p> |
176 |
|
177 |
<table border="1" summary="fig"> |
178 |
<tr><td> |
179 |
♦ Fig. 12 Starting Apache server |
180 |
<pre> |
181 |
# service httpd restart |
182 |
</pre> |
183 |
</td></tr> |
184 |
</table> |
185 |
|
186 |
<p>By executing Fig. 12, /etc/rc.d/init.d/httpd is executed and /usr/sbin/httpd is executed from /etc/rc.d/init.d/httpd . But if /usr/sbin/httpd is specified using "initialize_domain" keyword, "<kernel> /usr/sbin/httpd" domain is created. (If "initialize_domain /usr/sbin/httpd" is not specified in the exception policy, /usr/sbin/httpd will run in a child domain of program that executed /usr/sbin/httpd .)</p> |
187 |
|
188 |
<h3>Learning mode</h3> |
189 |
|
190 |
<p>Let's assign a profile for learning mode from profiles previously created. (Fig. 13) The "-r" option means apply recursively, which results in any domain which domainname starts with "<kernel> /usr/sbin/httpd" are assigned the specified profile. Be sure to quote appropriately when using ccs-setprofile command, or < and > will be interpreted as shell's redirection characters.</p> |
191 |
|
192 |
<table border="1" summary="fig"> |
193 |
<tr><td> |
194 |
♦ Fig. 13 Assign profile for learning mode |
195 |
<pre> |
196 |
# /usr/sbin/ccs-setprofile -r 1 '<kernel> /usr/sbin/httpd' |
197 |
1 <kernel> /usr/sbin/httpd |
198 |
</pre> |
199 |
</td></tr> |
200 |
</table> |
201 |
|
202 |
<p>Do operations you want to allow (such as browsing pages and using Wiki) after running the command in Fig. 13.</p> |
203 |
|
204 |
<h3>Permissive mode</h3> |
205 |
|
206 |
<p>When you have finished doing a series of operations you want to allow, assign a profile for permissive mode. (Fig. 14) Statistic counter /proc/ccs/stat is incremented whenever policy violation occurs. You can consider that all necessary permissions are appended into the policy if /proc/ccs/stat is no longer incremented when you do operations you want to allow.</p> |
207 |
|
208 |
<table border="1" summary="fig"> |
209 |
<tr><td> |
210 |
♦ Fig. 14 Assign profile for permissive mode |
211 |
<pre> |
212 |
# /usr/sbin/ccs-setprofile -r 2 '<kernel> /usr/sbin/httpd' |
213 |
2 <kernel> /usr/sbin/httpd |
214 |
</pre> |
215 |
</td></tr> |
216 |
</table> |
217 |
|
218 |
<p>Also, tune policy at this stage. Steps for tuning policy are described later in this installment.</p> |
219 |
|
220 |
<h3>Enforcing mode</h3> |
221 |
|
222 |
<p>If you consider that creating policy is completed, assign a profile for enforcing mode. (Fig. 15)</p> |
223 |
|
224 |
<table border="1" summary="fig"> |
225 |
<tr><td> |
226 |
♦ Fig. 15 Assign profile for enforcing mode |
227 |
<pre> |
228 |
# /usr/sbin/ccs-setprofile -r 3 '<kernel> /usr/sbin/httpd' |
229 |
3 <kernel> /usr/sbin/httpd |
230 |
</pre> |
231 |
</td></tr> |
232 |
</table> |
233 |
|
234 |
<p>By doing Fig.15, mandatory access control is applied to domains which domainname starts with "<kernel> /usr/sbin/httpd".</p> |
235 |
|
236 |
<p>To check status, you can use ccs-pstree command explained in the previous installment. The first column of each line by ccs-pstree command is the profile number. Make sure that profiles you intended are assigned to applications you want to protect using mandatory access control (in this example, /usr/sbin/httpd ).</p> |
237 |
|
238 |
<p>Note that the system is protected by mandatory access control only if you assigned profile for enforcing mode.</p> |
239 |
|
240 |
<h2>Tuning policy</h2> |
241 |
|
242 |
<h3>Patternizing pathnames</h3> |
243 |
|
244 |
<p>You need to specify all pathnames which applications can access, but some programs create files dynamically under /tmp/ directory with random alphabets and process ID numbers in their filenames. Such programs won't work with pathnames appended by learning mode only.</p> |
245 |
|
246 |
<p>In TOMOYO Linux, some wildcards are defined for handling dynamically created files. (Fig. 16) By using wildcards appropriately, you can reduce number of entries in policy and save memory usage.</p> |
247 |
|
248 |
<table border="1" summary="fig"> |
249 |
<tr><td> |
250 |
♦ Fig. 16 Wildcard expressions in TOMOYO Linux<br> |
251 |
<table border="1" summary="fig"> |
252 |
<tr><td>Wildcard</td><td>Meaning</td></tr> |
253 |
<tr><td>\*</td><td>Zero or more repetitions of characters other than '/'.</td></tr> |
254 |
<tr><td>\@</td><td>Zero or more repetitions of characters other than '/' or '.'.</td></tr> |
255 |
<tr><td>\?</td><td>1 byte character other than '/'.</td></tr> |
256 |
<tr><td>\$</td><td>One or more repetitions of decimal digits.</td></tr> |
257 |
<tr><td>\+</td><td>1 decimal digit.</td></tr> |
258 |
<tr><td>\X</td><td>One or more repetitions of hexadecimal digits.</td></tr> |
259 |
<tr><td>\x</td><td>1 hexadecimal digit.</td></tr> |
260 |
<tr><td>\A</td><td>One or more repetitions of alphabet characters.</td></tr> |
261 |
<tr><td>\a</td><td>1 alphabet character.</td></tr> |
262 |
<tr><td>\-</td><td>Pathname subtraction operator.</td></tr> |
263 |
<tr><td>/\{dir\}/</td><td>Recursive directory matching operator which matches '/' + one or more repetitions of 'dir/'.</td></tr> |
264 |
</table> |
265 |
</td></tr> |
266 |
</table> |
267 |
|
268 |
<p>Since '\' is used for escape character for representing wildcard, use '\\' for representing '\' itself. Also, use '\ooo' style octal representation for non-printable characters (e.g. ASCII's control codes and Japanese characters).</p> |
269 |
|
270 |
<h3>Let's patternize pathnames</h3> |
271 |
|
272 |
<p>You can patternize pathnames from policy editor. When you start ccs-editpolicy , a screen titled "<<< Domain Transition Editor >>>" will appear. Then, find "<kernel> /usr/sbin/httpd" domain. You will see a screen titled "<<< Domain Policy Editor >>>" (Fig. 17. The entries depends on your environment) by pressing "Enter" key after moving cursor to "<kernel> /usr/sbin/httpd" domain.</p> |
273 |
|
274 |
<table border="1" summary="fig"> |
275 |
<tr><td> |
276 |
♦ Fig. 17 Policy for Apache<br> |
277 |
<img src="tutorial/fig-2-17.png" alt="fig-2-17.png" width="720" height="400"> |
278 |
</td></tr> |
279 |
</table> |
280 |
|
281 |
<p>WWW servers access files under /var/www/ directory. Thus, specify like Fig. 18 for granting read access on files under /var/www/html/ directory. For each line in Fig. 18, press "A" key and enter the line and press "Enter" key. (Fig. 19)</p> |
282 |
|
283 |
<table border="1" summary="fig"> |
284 |
<tr><td> |
285 |
♦ Fig. 18 An example for allowing reading under /var/www/html/ directory. |
286 |
<pre> |
287 |
file read /var/www/html/\* |
288 |
file read /var/www/html/\{\*\}/\* |
289 |
</pre> |
290 |
</td></tr> |
291 |
</table> |
292 |
|
293 |
<table border="1" summary="fig"> |
294 |
<tr><td> |
295 |
♦ Fig. 19 Allow Apache to read under /var/www/html/ directory.<br> |
296 |
<img src="tutorial/fig-2-19.png" alt="fig-2-19.png" width="720" height="400"> |
297 |
</td></tr> |
298 |
</table> |
299 |
|
300 |
<p>Some applications use temporary files. For example, if entries listed in Fig. 20 exist, these are likely temporary files created in /tmp/phpXXXXXX pattern. Therefore, you need to replace these entries using /tmp/php\?\?\?\?\?\? (this is TOMOYO Linux's wildcard representation for /tmp/phpXXXXXX pattern). Steps are shown below.</p> |
301 |
|
302 |
<table border="1" summary="fig"> |
303 |
<tr><td> |
304 |
♦ Fig. 20 Temporary files |
305 |
<pre> |
306 |
file read/write /tmp/phpAb9fD1 |
307 |
file read/write /tmp/phpkzqf5p |
308 |
file read/write /tmp/php3lo7ab |
309 |
</pre> |
310 |
</td></tr> |
311 |
</table> |
312 |
|
313 |
<p>First, press "A" key on the keyboard, and enter "file read/write /tmp/php\?\?\?\?\?\?" (this is a patternized entry for entries in Fig. 20). Next, move the cursor to the "file read/write /tmp/php\?\?\?\?\?\?" line and press "O" key, and you will see entries included in "file read/write /tmp/php\?\?\?\?\?\?" entry are marked with "&". Verify that entries listed in Fig. 20 are marked with "&" and press "D" key on the keyboard, and a prompt "Delete selected entries? ('Y'es/'N'o)" is printed, and press "Y" key on the keyboard.</p> |
314 |
|
315 |
<p>Continue policy tuning using permissive mode until all necessary permissions are given.</p> |
316 |
|
317 |
<h3>How to save policy?</h3> |
318 |
|
319 |
<p>Upon boot, /sbin/ccs-init automatically loads policy from files on disk to kernel memory. But upon shutdown, nothing automatically saves policy from kernel memory to files on disk. Therefore, be sure to run ccs-savepolicy command before shutdown if you modified policy or changed profile assignment.</p> |
320 |
|
321 |
<h3>How to recreate policy from scratch?</h3> |
322 |
|
323 |
<p>The learning mode automatically appends entries to existing policy. But if you want to restart learning mode from the scratch rather than starting from existing policy, reboot the system after deleting a symbolic link named /etc/ccs/domain_policy.conf .</p> |
324 |
|
325 |
<h2>Trailer</h2> |
326 |
|
327 |
<p>In this installment, I explained steps for actually performing access control and steps for protecting WWW services. All basic operations for TOMOYO Linux were explained in previous installment and this installment. Thus, you can say "I can use TOMOYO Linux" if you understood steps written in this installment.</p> |
328 |
|
329 |
<p>In the next installment, I explain TOMOYO Linux's domain transitions. Don't miss it!</p> |
330 |
|
331 |
<p><a href="tutorial-1.html.en">Go back to the first installment.</a> <a href="tutorial-3.html.en">Proceed to the third installment.</a></p> |
332 |
|
333 |
<hr> |
334 |
|
335 |
<p><a href="index.html.en#tutorial">Return to index page.</a></p> |
336 |
<p><a href="http://sourceforge.jp/"><img src="http://sourceforge.jp/sflogo.php?group_id=1973" width="96" height="31" alt="SourceForge.jp"></a></p> |
337 |
</body> |
338 |
</html> |