Debian Codegraph @

src/doc/security.txt

raw
1 The Angband Security Guide
2 --------------------------
3
4 version 0.2 (1st March 2001) [incomplete Draft]
5
6 by Robert Ruehlmann < [email protected] >
7
8
9 ==========================================================================
10 1. Background
11 ==========================================================================
12
13 1.1 Running a program with more privileges than the user normally has
14 ---------------------------------------------------------------------
15
16 Angband uses a highscore file to record the standings of all the players.
17 The problem with such a file is that the game must be able to write to it,
18 while the players themselves can't be allowed to cheat by modifying the
19 highscores directly.
20
21 The savefiles have the same problem. Players can't be allowed to change
22 them, but the game needs the permission to do so. In addition the player
23 shouldn't even be able to read the savefiles. Reading the savefiles would
24 allow finding out secret information and give an unfair advantage.
25
26 Normally a program runs with the permissions of the user who starts it.
27 Making a program setuid or setgid changes this and grants the program the
28 privileges of the owner of the program (setuid) or the group the
29 program belongs to (setgid). This provides a way to solve the problems
30 mentioned above.
31
32 These extra privileges are a target for attackers.
33
34 For Angband the use of setgid is recommended since it grants less
35 privileges than setuid. An attacker who manages to hack into the game
36 just gains group membership instead of access as user owning the game.
37 For example group access doesn't allow changing the ownership of files.
38
39 - What kind of vulnerabilities are there?
40
41 - Buffer overflows
42 - Format string attacks
43 - Allowing the user write access to parts of the programs memory
44 - Running user-specified programs without dropping the privileges first
45 - ...
46
47
48 1.2 Reading data from untrusted sources
49 ---------------------------------------
50
51 Even if the game doesn't run with additional privileges a malicious user
52 can still attack you directly to gain access to your account.
53
54 Most computer users know that running executable files or other "active
55 content" like Visual Basic scripts (remember Loveletter?) can be
56 dangerous. But most people overlook the fact that "non-executable" data
57 can be dangerous too. Bugs in programs like buffer overflows or format
58 string vulnerabilities can allow an attacker to hide executable code
59 inside the data.
60
61 Possible data types that might be vulnerable are the binary savefiles and
62 scores, but the processing of ASCII files like pref-files and Angband's
63 screen-dumps may also contain hidden bugs.
64
65 It may sound paranoid, but you should consider everybody as a source of
66 untrusted data. Always remember that even your best friend can
67 unknowingly forward a savefile touched by a cracker to you.
68
69 See also section 2.1 and 2.2
70
71
72 Further reading:
73
74 Malicious Data and Computer Security
75 http://www.fish.com/security/maldata.html
76
77
78 1.3 Writing files in world-writeable directories
79 ------------------------------------------------
80
81 Writing to a directory that other users have write access to can allow an
82 attacker to trick the program into following a link and overwriting files
83 of the user running the program. This is known as a "symlink attack".
84
85
86
87 ==========================================================================
88 2. Possible security holes
89 ==========================================================================
90
91 2.1 Buffer overflows
92 --------------------
93
94 A buffer overflow is the result of stuffing more data into a buffer than
95 it can handle.
96
97 Buffer overflows usually happen when processing strings, since that is the
98 most common type of input in a program. The C standard library provides
99 many functions for manipulating strings and some of these functions don't
100 perform boundary checking. These dangerous string functions include
101 strcat(), strcpy(), sprintf(), and vsprintf().
102
103 Lets take a look at a small snippet of code:
104
105 char path[1024];
106 cptr env_path;
107
108 /* Get the environment variable */
109 env_path = getenv("ANGBAND_PATH");
110
111 if (env_path)
112 {
113 /* Use the angband_path, or a default */
114 strcpy(path, env_path);
115 }
116
117 This snippet is a simplified version of the function that initializes the
118 path to the lib/ directory of older Angband versions. The getenv()
119 function returns a pointer to the content of the "ANGBAND_PATH"
120 environment variable. This environment variable can be set by the user
121 and has an unknown length. The strcpy() function takes the content of this
122 variable and copies it character by character into the "path" buffer until
123 it reaches a '\0' character in "env_path". If "env_path" is longer than
124 the 1024 characters that we allocated earlier, then strcpy() will happily
125 write over the end of our buffer and overwrites parts of the stack.
126
127 To prevent this bug you have to use functions that do bounds-checking
128 instead like strncpy(), or check the length of the data before writing
129 it to the buffer.
130
131 The example above could be fixed by replacing the strcpy() call with the
132 following snippet:
133
134 strncpy(path, env_path, 1024);
135 path[1023] = '\0';
136
137 strncpy() takes an additional third argument containing the maximum length
138 of the string to be copied. Note that strncpy() doesn't automatically
139 place a null character ('\0') at the end of the string if it is longer
140 than the maximum, so the second line forces the string to be terminated.
141
142 Advise:
143 - Be paranoid.
144 - Always do proper bounds-checking (not only in security relevant parts),
145 and check all user inputs especially carefully. That also includes
146 command line arguments, environment variables, data from user-writeable
147 files, and even the name of the executable program.
148 - Don't use strcpy() unless you are absolutely certain that it can't
149 overflow your buffer. Use strncpy() instead and make sure to terminate
150 the result.
151 - *Never ever* use the gets() function. It is broken by design.
152 - ...
153
154 Further reading:
155
156 Smashing The Stack For Fun And Profit
157 http://www.2600.net/phrack/p49-14.html
158
159 Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade
160 http://immunix.org/StackGuard/discex00.pdf
161
162
163 2.2 Allowing the user write access to parts of the programs memory
164 ------------------------------------------------------------------
165
166 This kind of vulnerability is the result of not checking user input and
167 using indices that are outside of the regular bounds of your data storage.
168 This allows an attacker to write to random memory locations. In many
169 cases it is possible to replace parts of your executable code with
170 malicious code pieces.
171
172 One example is not checking the indices of monsters or objects when
173 reading prf-files. The attacker can specify an index that it outside the
174 available range of monster indices and the game will happily access the
175 illegal address and write the char/attr values there.
176
177 Always be paranoid about bounds-checking all input processing functions.
178
179
180 2.3 Allowing the user access to files that he shouldn't be able to access
181 -------------------------------------------------------------------------
182
183 When running setgid the game runs with more privileges than the user
184 normally has. This could allow the player to read or write files that
185 he/she normally has no access to (like the savefiles). Normally the game
186 switches between the normal rights of the player and the enhanced rights
187 provided by "setgid" depending on what file is accessed. So savefiles and
188 scorefiles are opened with the setgid rights while character dumps and
189 *.prf files are opened with the normal rights of the player.
190
191 If there is a bug in the switching between these two modes then the
192 player could for example save a character dump over the savefile of
193 another player or read a file that is readable by group games but not
194 by the player.
195
196 Angband 2.9.2 introduced a new approach to file access.
197
198 In 2.9.2 the game drops all extra access rights granted by "setgid" at
199 startup and grabs them only when it needs them to open a protected file.
200 So most of the time the game is running without any extra rights and can
201 read and write files exactly like the player could outside the game. Only
202 when it needs to access one of the protected files it switches on the
203 extra rights, accesses the file with the rights of group "games", and then
204 drops the rights again.
205
206 In 2.9.1 and earlier it was the other way around. The game grabbed the
207 access rights at startup and dropped them when opening a file unless it
208 needed the extra rights for the access.
209
210 Old code that uses the "safe_setuid_grab()" and "safe_setuid_drop()"
211 functions to turn off the extra rights for a file access has to be
212 modified for Angband 2.9.2. The old code dropped the rights, opened the
213 file, and then grabbed the rights again. With the new method the initial
214 dropping of the rights does nothing since the rights already have been
215 dropped by the game at startup. Then the file is accessed without any
216 additional rights. And then the old code grabs the rights again.
217
218 From this moment on the game holds the rights till it accesses a protected
219 file like the savefile or scores and allows the player to access files
220 with the additional rights granted by setgid!
221
222 So the "safe_setuid_grab()" and "safe_setuid_drop()" calls have to be
223 removed from the old code when you want to use it in Angband 2.9.2 or
224 you'll introduce a security hole.
225
226
227 ==========================================================================
228 A. Appendix
229 ==========================================================================
230
231 A.1 Further reading
232 -------------------
233
234 Secure Programming for Linux and Unix HOWTO:
235 http://dwheeler.com/secure-programs/
236
237 Designing secure software
238 http://www.sunworld.com/swol-04-1998/swol-04-security.html
239
240 The Unix Secure Programming FAQ
241 http://www.sunworld.com/sunworldonline/swol-08-1998/swol-08-security.html
242
243 A Lab engineers check list for writing secure Unix code
244 ftp://ftp.auscert.org.au/pub/auscert/papers/secure_programming_checklist/
245
246 Make your software behave: Learning the basics of buffer overflows
247 http://www-4.ibm.com/software/developer/library/overflows/index.html
248
249 Secure UNIX Programming FAQ
250 http://www.whitefang.com/sup/secure-faq.html
251
252 Format String Attacks
253 http://www.guardent.com/docs/FormatString.PDF
254
255 How to find security holes
256 http://www.dnaco.net/~kragen/security-holes.html
257
258 Security Code Review Guidelines
259 http://www.homeport.org/~adam/review.html
260
261
262 A.2 Tools
263 ---------
264
265 Fuzz Testing of Application Reliability
266 http://www.cs.wisc.edu/~bart/fuzz/fuzz.html
267
268 GCC bounds-checking patches
269 http://web.inter.nl.net/hcc/Haj.Ten.Brugge/
270
271 ITS4 - Software Security Tool
272 http://www.cigital.com/its4/
273
274 LCLint
275 http://lclint.cs.virginia.edu/
276
277 Electric Fence - malloc() debugger
278 http://www.perens.com/FreeSoftware/