1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
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 eighth installment: "Let's restrict login sessions. (First part)"</title> |
7 |
<link rel="stylesheet" href="https://tomoyo.osdn.jp/tomoyo.css" media="all" type="text/css"> |
8 |
</head> |
9 |
<body> |
10 |
<p>Info: Version <a href="../1.8/">1.8.x</a> is available.</p> |
11 |
<p style="text-align:right;"><a href="tutorial-8.html.ja">Japanese Page</a></p> |
12 |
<p style="text-align:right;">Last modified: $Date$</p> |
13 |
|
14 |
<h1>The world of TOMOYO Linux<br>The eighth installment: "Let's restrict login sessions. (First part)"</h1> |
15 |
|
16 |
<h2>Contents of this installment.</h2> |
17 |
|
18 |
<p>In this installment, I explain steps for restricting operations after logging into a Linux system.</p> |
19 |
|
20 |
<p>SELinux is currently migrating to reference policy. Before reference policy is provided, two types of policy configurations were provided as default policy configuration. One is called "strict policy" which applies mandatory access control to all processes including login sessions. The other is called "targeted policy" which applies mandatory access control to frequently attacked services. From the point of view of "improving security", literally strict "strict policy" is more effective. But if "strict policy" is not properly configured, it even happens that nobody can login to a SELinux enabled system. Therefore, many users who are using SELinux enabled are probably customizing policy configuration based on "targeted policy". According to opinions from Russell Coker and SELinux developers in LKML, they SELinux developers consider that "System administrators who can't figure out SELinux's policy should not modify SELinux's policy configuration. Use default policy configuration without modification provided by us because we are SELinux's professional." On the contrary, TOMOYO Linux developers consider that "System administrators creates policy configuration by themselves." What do you think about this?</p> |
21 |
|
22 |
<p>By the way, this series explains TOMOYO Linux. Thus, let's restrict operations in login sessions using "do it yourself" TOMOYO Linux.</p> |
23 |
|
24 |
<h2>Let's restrict login sessions</h2> |
25 |
|
26 |
<h3>Why restricting login sessions?</h3> |
27 |
|
28 |
<p>When you use Linux as a server, you might want to restrict operations of users who logged into the system. For example, "I want to prevent administrator from getting out database files which contain customer's information", "I want to allow logged in users nothing but to change password", "I want to delegate tasks for restarting WWW servers and updating WWW contents while forbidding all other operations", "I want to prevent logged in users from installing backdoors for sniffing passwords", "I want to use Linux as a gateway server for accessing corporate networks from Internet". You can easily handle such demands by using TOMOYO Linux because TOMOYO Linux manages domains for per process's invocation history basis.</p> |
29 |
|
30 |
<h3>Basic steps</h3> |
31 |
|
32 |
<p>There are 4 general phases for protecting login sessions.</p> |
33 |
|
34 |
<ol> |
35 |
<li>Firstly, determine commands you want to allow execution in login sessions (e.g. allow execution of /bin/cat command for browsing files, allow execution of /usr/bin/passwd command for changing passwords).</li> |
36 |
<li>Next, determine files you want to allow access in login sessions (e.g. allow reading and writing files under the user's home directory as a rule).</li> |
37 |
<li>Then, determine networks you want to allow communication in login sessions (e.g. allow communication with only DNS server, HTTP server, FTP server and SSH server).</li> |
38 |
<li>After giving permissions determined above, append missing permissions using learning mode.</li> |
39 |
</ol> |
40 |
|
41 |
<p>Keywords which appear in above steps are listed in Fig. 1.</p> |
42 |
|
43 |
<table border="1" summary="fig"> |
44 |
<tr><td> |
45 |
♦ Fig. 1 Keywords<br> |
46 |
<table border="1" summary="fig"> |
47 |
<tr><td>Keyword</td><td>Meaning</td></tr> |
48 |
<tr><td>path_group</td><td>Grouping program files and files under home directories.</td></tr> |
49 |
<tr><td>keep_domain</td><td>Suppress domain transitions</td></tr> |
50 |
<tr><td>no_keep_domain</td><td>Resume domain transitions</td></tr> |
51 |
<tr><td>exec.argv[0]</td><td>Prevent illegal program execution using symbolic links</td></tr> |
52 |
<tr><td>address_group</td><td>Grouping network addresses</td></tr> |
53 |
<tr><td>if</td><td>Allow accessing files owned by the user</td></tr> |
54 |
</table> |
55 |
</td></tr> |
56 |
</table> |
57 |
|
58 |
<h2>Step 1: Determine commands you want to allow</h2> |
59 |
|
60 |
<h3>Determining programs</h3> |
61 |
|
62 |
<p>To prevent users who logged in the system from executing malicious programs, it is an effective solution to forbid execution of programs which are not installed in the system beforehand. In this series, I assume commands listed in Fig. 2 (from commands installed in the system beforehand) are permitted in the login session.</p> |
63 |
|
64 |
<table border="1" summary="fig"> |
65 |
<tr><td> |
66 |
♦ Fig. 2 Programs permitted in login session<br> |
67 |
<table border="1" summary="fig"> |
68 |
<tr><td>/bin/basename /bin/bash /bin/cat /bin/egrep /bin/grep /bin/hostname /bin/ln /bin/ls /bin/mkdir /bin/mv /bin/rm /bin/rmdir /bin/sed /bin/touch /sbin/consoletype /usr/bin/clear /usr/bin/dircolors /usr/bin/emacs /usr/bin/id /usr/bin/passwd /usr/bin/perl /usr/bin/ssh /usr/bin/ssh-keygen /usr/bin/wget /usr/bin/which</td></tr> |
69 |
</table> |
70 |
</td></tr> |
71 |
</table> |
72 |
|
73 |
<p>Let's group these commands using "path_group" keyword in exception policy. (Fig. 3) But do not use "path_group" keyword for programs which behaves differently depending on invocation name (a.k.a. argv[0]). Instead, specify such programs individually (e.g. allow_execute /usr/bin/passwd if exec.argv[0]="passwd") using "if exec.argv[0]" conditions.</p> |
74 |
|
75 |
<table border="1" summary="fig"> |
76 |
<tr><td> |
77 |
♦ Fig. 3 Grouping commands |
78 |
<pre> |
79 |
path_group COMMANDS_FOR_LOGIN_SESSION /bin/basename |
80 |
path_group COMMANDS_FOR_LOGIN_SESSION /bin/bash |
81 |
(...snipped...) |
82 |
path_group COMMANDS_FOR_LOGIN_SESSION /usr/bin/wget |
83 |
path_group COMMANDS_FOR_LOGIN_SESSION /usr/bin/which |
84 |
</pre> |
85 |
</td></tr> |
86 |
</table> |
87 |
|
88 |
<p>You can use "which" command or "type" command for finding the location of commands. (Fig. 4)</p> |
89 |
|
90 |
<table border="1" summary="fig"> |
91 |
<tr><td> |
92 |
♦ Fig. 4 Finding the location of commands |
93 |
<pre> |
94 |
# which perl sed grep |
95 |
/usr/bin/perl |
96 |
/bin/sed |
97 |
/bin/grep |
98 |
</pre> |
99 |
</td></tr> |
100 |
</table> |
101 |
|
102 |
<p>You might wonder that "Do I need to allow execution of programs like /sbin/consoletype and /usr/bin/dircolors ?" But many commands are automatically executed by scripts (e.g. ~/.bashrc for /bin/bash ) where users are not aware. To know what operations are done by such commands, login to the system running with TOMOYO Linux kernels and then logout. And you can learn what commands are automatically executed by scripts as a child domain of login shell. Moreover, if you do so with a profile with "CONFIG::file={ mode=learning }" (i.e. learning mode) assigned to the domain for login shell, you can learn not only programs executed but also files accessed.</p> |
103 |
|
104 |
<h3>Designing domain transition tree</h3> |
105 |
|
106 |
<p>As I explained in this series by now, as a rule, TOMOYO Linux performs domain transition whenever a program is executed. Therefore, /bin/cat command will belong to a child domain of shell programs in Fig. 5 and /bin/cat command will belong to a child domain of /usr/bin/xargs command in Fig. 6.</p> |
107 |
|
108 |
<table border="1" summary="fig"> |
109 |
<tr><td> |
110 |
♦ Fig. 5 Executing /bin/cat command from shell |
111 |
<pre> |
112 |
# /bin/cat /etc/*.conf |
113 |
</pre> |
114 |
</td><td> |
115 |
♦ Fig. 6 Executing /bin/cat command from xargs command |
116 |
<pre> |
117 |
# echo /etc/*.conf | xargs /bin/cat -- |
118 |
</pre> |
119 |
</td></tr> |
120 |
</table> |
121 |
|
122 |
<p>But, there would be no difference from the point of view of security and it would be convenient for users that /bin/cat belongs to the same domain in both cases. Since it is difficult to predict what commands do users executes in what order in login session, as a rule you can suppress domain transitions in login session. If login shell is /bin/bash , the domain which login shell executed from /usr/sbin/sshd belongs to is "<kernel> /usr/sbin/sshd /bin/bash". Thus, you can specify "keep_domain <kernel> /usr/sbin/sshd /bin/bash" in exception policy in order to suppress domain transition. However, you want to allow accessing /etc/shadow (i.e. password file) by /usr/bin/passwd command for changing password but you don't want to allow accessing /etc/shadow using /bin/cat command for browsing password. In that case, you can specify "no_keep_domain /usr/bin/passwd from <kernel> /usr/sbin/sshd /bin/bash" in exception policy in order to let /usr/bin/passwd command run in "<kernel> /usr/sbin/sshd /bin/bash /usr/bin/passwd" domain. Similarly, you can specify "no_keep_domain /usr/bin/ssh from <kernel> /usr/sbin/sshd /bin/bash" in order to let /usr/bin/ssh command run in a different domain because you want to allow /usr/bin/ssh command to access ~/.ssh/ directory (where files like SSH secret key are stored) but you don't want to allow /bin/cat command to access ~/.ssh/ directory.</p> |
123 |
|
124 |
<h2>Step 1: Determine files you want to allow reading and writing</h2> |
125 |
|
126 |
<h3>The threat of hard links and countermeasure</h3> |
127 |
|
128 |
<p>In TOMOYO Linux, both source and destination pathnames are checked when creating hard links, both old and new pathnames are checked when renaming. I explain the reason why TOMOYO Linux does so. Login to a system (no need to be running with TOMOYO Linux kernels) and do commands listed in Fig. 7. (The command will fail if /etc/ directory and /tmp/ directory are on different partitions. In that case, use a directory which is on the same partition (e.g. /root/ directory) instead of /tmp/ directory.)</p> |
129 |
|
130 |
<table border="1" summary="fig"> |
131 |
<tr><td> |
132 |
♦ Fig. 7 Accessing password files via hard link |
133 |
<pre> |
134 |
# ln /etc/shadow /tmp/shadow |
135 |
# cat /tmp/shadow |
136 |
</pre> |
137 |
</td></tr> |
138 |
</table> |
139 |
|
140 |
<p>This means that by creating hard link of /etc/shadow (which contains login passwords) under /tmp/ directory, you can access the login passwords via /tmp/shadow . You might think that "No administrators will create hard link of /etc/shadow as /tmp/shadow .", but creation of hard link of /etc/shadow is permitted for non "root" users. A non "root" user might create hard link of /etc/shadow into /tmp/ directory (where non "root" users can write) and access the password file (i.e. /tmp/shadow ) by gaining "root" privileges via permitted commands (e.g. /usr/bin/sudo command). Therefore, TOMOYO Linux checks both source and destination pathnames in order to minimize the risk of creating such hard links. (If you actually created /tmp/shadow , be sure to delete /tmp/shadow before reading on! If the link count of /etc/shadow is larger than 1, hard links remain somewhere on the same partition. (Fig. 8))</p> |
141 |
|
142 |
<table border="1" summary="fig"> |
143 |
<tr><td> |
144 |
♦ Fig. 8 Delete hard link of /etc/shadow |
145 |
<pre> |
146 |
# ls -l /etc/shadow |
147 |
-r-------- 2 root root 946 May 29 18:14 /etc/shadow |
148 |
# unlink /tmp/shadow |
149 |
# ls -l /etc/shadow |
150 |
-r-------- 1 root root 946 May 29 18:14 /etc/shadow |
151 |
</pre> |
152 |
</td></tr> |
153 |
</table> |
154 |
|
155 |
<p>Same for renaming operation. You can strictly restrict reading and/or writing /etc/shadow , but you cannot strictly restrict reading and/or writing files under /tmp/ directory. But, it is not desirable for you that users can do operations like Fig. 9. (If /etc/ directory and /tmp/ directory are on different partition, you can prevent moving from /etc/shadow to /tmp/shadow by forbidding reading and writing /etc/shadow because rename operations which cross mount point are not supported.)</p> |
156 |
|
157 |
<table border="1" summary="fig"> |
158 |
<tr><td> |
159 |
♦ Fig. 9 Accessing password files after renaming |
160 |
<pre> |
161 |
# mv /etc/shadow /tmp/shadow |
162 |
# cat /tmp/shadow |
163 |
# mv /tmp/shadow /etc/shadow |
164 |
</pre> |
165 |
</td></tr> |
166 |
</table> |
167 |
|
168 |
<p>Therefore, TOMOYO Linux checks both old and new pathnames in order to minimize the risk of such renaming. Access restrictions based on pathnames will allow unexpected access if you are not careful. Thus, be careful when giving permissions for creating hard links or renaming using TOMOYO Linux in order to minimize accessible ranges. (Although this characteristic is a disadvantage of pathname based access control when dealing "whether one can read and/or write and/or execute the file", this characteristic is an advantage of pathname based access control when dealing "how one uses the file if one can read and/or write and/or execute the file".)</p> |
169 |
|
170 |
<h3>Restricting access to files under home directory</h3> |
171 |
|
172 |
<p>Domain transition is used for switching permissions. Not doing domain transition for login sessions as a rule means we don't distinguish files which are permitted to read and/or write in login session. Basically, you don't need to create or rewrite programs in login session. Thus, you will give permissions for mainly reading and writing files under user's home directory. In order to group files under user's home directory, specify groups listed in Fig. 10 in exception policy.</p> |
173 |
|
174 |
<table border="1" summary="fig"> |
175 |
<tr><td> |
176 |
♦ Fig. 10 Grouping files under home directory |
177 |
<pre> |
178 |
path_group HOME_FILE /home/\{\*\}/\* |
179 |
path_group HOME_DIR /home/\{\*\}/\*/ |
180 |
</pre> |
181 |
</td></tr> |
182 |
</table> |
183 |
|
184 |
<p>By grouping like Fig. 10, you can treat files under user's home directory equally.</p> |
185 |
|
186 |
<h3>Excluding specific pathnames</h3> |
187 |
|
188 |
<p>But you have files you want to forbid browsing via e.g. /bin/cat command (e.g. files under ~/.ssh/ directory) within files in user's home directory. Therefore, you can exclude pathnames in .ssh directory by specifying "\*\-.ssh". Namely, modify groups like Fig. 11.</p> |
189 |
|
190 |
<table border="1" summary="fig"> |
191 |
<tr><td> |
192 |
♦ Fig. 11 Exclude .ssh directory from home directory |
193 |
<pre> |
194 |
path_group HOME_FILE /home/\{\*\-.ssh\}/\* |
195 |
path_group HOME_DIR /home/\{\*\-.ssh\}/ |
196 |
</pre> |
197 |
</td></tr> |
198 |
</table> |
199 |
|
200 |
<p>Now, users can no longer access /home/\*/.ssh/\* via /bin/cat command or rename /home/\*/.ssh/ to /home/\*/ssh/ via /bin/mv command.</p> |
201 |
|
202 |
<h3>Giving permissions beforehand</h3> |
203 |
|
204 |
<p>Give permissions listed in Fig. 12 to the domain for login session (i.e. "<kernel> /usr/sbin/sshd /bin/bash" domain)</p> |
205 |
|
206 |
<table border="1" summary="fig"> |
207 |
<tr><td> |
208 |
♦ Fig. 12 Give permissions beforehand |
209 |
<pre> |
210 |
allow_execute @COMMANDS_FOR_LOGIN |
211 |
allow_read/write @HOME_FILE |
212 |
allow_create @HOME_FILE 00-0666 |
213 |
allow_unlink @HOME_FILE |
214 |
allow_symlink @HOME_FILE |
215 |
allow_link @HOME_FILE @HOME_FILE |
216 |
allow_rename @HOME_FILE @HOME_FILE |
217 |
allow_truncate @HOME_FILE |
218 |
allow_rewrite @HOME_FILE |
219 |
allow_mkdir @HOME_DIR 00-0777 |
220 |
allow_rmdir @HOME_DIR |
221 |
allow_rename @HOME_DIR @HOME_DIR |
222 |
</pre> |
223 |
</td></tr> |
224 |
</table> |
225 |
|
226 |
<p>Sequentially from the top, they mean</p> |
227 |
|
228 |
<ul> |
229 |
<li>Allow execution of programs specified in COMMANDS_FOR_LOGIN group</li> |
230 |
<li>Allow opening files under user's home directory for reading and/or writing </li> |
231 |
<li>Allow creating files under user's home directory</li> |
232 |
<li>Allow deleting files under user's home directory</li> |
233 |
<li>Allow creating symbolic links under user's home directory</li> |
234 |
<li>Allow creating hard links if both source and destination pathnames are under user's home directory</li> |
235 |
<li>Allow renaming files if both old and new pathnames are under user's home directory</li> |
236 |
<li>Allow truncating files under user's home directory</li> |
237 |
<li>Allow overwriting files under user's home directory</li> |
238 |
<li>Allow creating directories under user's home directory</li> |
239 |
<li>Allow deleting directories under user's home directory</li> |
240 |
<li>Allow renaming directories if both old and new pathnames are under user's home directory</li> |
241 |
</ul> |
242 |
|
243 |
<p>Note that by modification in Fig. 11, "user's home directory" does not include ~/.ssh/ and ~/.ssh/\* . Above example does not check symbolic link's targets when creating symbolic links. TOMOYO Linux restricts reading and/or writing files based on dereferenced pathnames. Thus, the symbolic link's pathname does not matter. When executing programs, users can set arbitrary values to argv[0]. But "if exec.argv[0]" should be specified if programs in COMMANDS_FOR_LOGIN group are affected by argv[0]. Thus, users cannot execute arbitrary commands by creating symbolic links of hard links of busybox command. Users can create hard links only if files are under user's home directory. Thus, users cannot create hard link of /etc/shadow in order to access password file via user's home directory. Same for rename.</p> |
244 |
|
245 |
<h3>Allow accessing only owned files</h3> |
246 |
|
247 |
<p>By changing user's home directory's permission to 0700, you can prevent other users from accessing your home directory. But sometimes you have to loosen home directory's permission in order to allow Apache to access ~/public_html/ directory. In that case, you can forbid accesses which cannot be prevented by home directory's permissions by adding "if task.uid=path1.uid" condition to individual permissions. Namely, replace Fig. 12 by Fig. 13.</p> |
248 |
|
249 |
<table border="1" summary="fig"> |
250 |
<tr><td> |
251 |
♦ Fig. 13 Allowing accessing files owned by the user |
252 |
<pre> |
253 |
allow_execute @COMMANDS_FOR_LOGIN |
254 |
allow_read/write @HOME_FILE if task.uid=path1.uid |
255 |
allow_create @HOME_FILE 00-0666 if task.uid=path1.parent.uid |
256 |
allow_unlink @HOME_FILE if task.uid=path1.uid |
257 |
allow_symlink @HOME_FILE if task.uid=path1.parent.uid |
258 |
allow_link @HOME_FILE @HOME_FILE if task.uid=path1.uid task.uid=path1.parent.uid task.uid=path2.parent.uid |
259 |
allow_rename @HOME_FILE @HOME_FILE if task.uid=path1.uid task.uid=path1.parent.uid task.uid=path2.parent.uid |
260 |
allow_truncate @HOME_FILE if task.uid=path1.uid |
261 |
allow_rewrite @HOME_FILE if task.uid=path1.uid |
262 |
allow_mkdir @HOME_DIR 00-0777 if task.uid=path1.parent.uid |
263 |
allow_rmdir @HOME_DIR if task.uid=path1.uid |
264 |
allow_rename @HOME_DIR @HOME_DIR if task.uid=path1.uid task.uid=path1.parent.uid task.uid=path2.parent.uid |
265 |
</pre> |
266 |
</td></tr> |
267 |
</table> |
268 |
|
269 |
<p>"task.uid" is the user ID of the process requesting this access (i.e. the user ID of user who logged in the system), "path1.uid" is the owner ID of the first pathname (for example, /dev/null for "allow_read/write /dev/null" and /tmp/source for "allow_link /tmp/source /tmp/dest"). "path1.parent.uid" is the owner ID of the first pathname's parent directory (for example, /dev/ for "allow_read/write /dev/null"). "path2.uid" is the owner ID of the second pathname (for example, /home/ for "allow_mount /dev/sda2 /home/ ext3 0". But you cannot specify "path2.uid" for "allow_rename" keyword and "allow_link" keyword because the second pathname is not accessible as of permission checks. "path2.parent.uid" is the owner ID of the second pathname's parent directory (for example, /tmp/ for "allow_link /tmp/source /tmp/dest").</p> |
270 |
|
271 |
<h2>Trailer</h2> |
272 |
|
273 |
<p>In this installment, I explained access control on files from steps for restricting operations in login sessions. In the next installment, I explain rest of steps and actual operation procedure.</p> |
274 |
|
275 |
<p><a href="tutorial-7.html.en">Go back to the seventh installment.</a> <a href="tutorial-9.html.en">Proceed to the ninth installment.</a></p> |
276 |
|
277 |
<hr> |
278 |
|
279 |
<p><a href="index.html.en#tutorial">Return to index page.</a></p> |
280 |
<p><a href="https://sourceforge.net/"><img src="https://sourceforge.net/sflogo.php?group_id=235503" width="96" height="31" alt="sflogo.php" title="SourceForge.net"></a></p> |
281 |
</body> |
282 |
</html> |