forked from angband/angband
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.txt
278 lines (200 loc) · 10.6 KB
/
security.txt
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
The Angband Security Guide
--------------------------
version 0.2 (1st March 2001) [incomplete Draft]
by Robert Ruehlmann < [email protected] >
==========================================================================
1. Background
==========================================================================
1.1 Running a program with more privileges than the user normally has
---------------------------------------------------------------------
Angband uses a highscore file to record the standings of all the players.
The problem with such a file is that the game must be able to write to it,
while the players themselves can't be allowed to cheat by modifying the
highscores directly.
The savefiles have the same problem. Players can't be allowed to change
them, but the game needs the permission to do so. In addition the player
shouldn't even be able to read the savefiles. Reading the savefiles would
allow finding out secret information and give an unfair advantage.
Normally a program runs with the permissions of the user who starts it.
Making a program setuid or setgid changes this and grants the program the
privileges of the owner of the program (setuid) or the group the
program belongs to (setgid). This provides a way to solve the problems
mentioned above.
These extra privileges are a target for attackers.
For Angband the use of setgid is recommended since it grants less
privileges than setuid. An attacker who manages to hack into the game
just gains group membership instead of access as user owning the game.
For example group access doesn't allow changing the ownership of files.
- What kind of vulnerabilities are there?
- Buffer overflows
- Format string attacks
- Allowing the user write access to parts of the programs memory
- Running user-specified programs without dropping the privileges first
- ...
1.2 Reading data from untrusted sources
---------------------------------------
Even if the game doesn't run with additional privileges a malicious user
can still attack you directly to gain access to your account.
Most computer users know that running executable files or other "active
content" like Visual Basic scripts (remember Loveletter?) can be
dangerous. But most people overlook the fact that "non-executable" data
can be dangerous too. Bugs in programs like buffer overflows or format
string vulnerabilities can allow an attacker to hide executable code
inside the data.
Possible data types that might be vulnerable are the binary savefiles and
scores, but the processing of ASCII files like pref-files and Angband's
screen-dumps may also contain hidden bugs.
It may sound paranoid, but you should consider everybody as a source of
untrusted data. Always remember that even your best friend can
unknowingly forward a savefile touched by a cracker to you.
See also section 2.1 and 2.2
Further reading:
Malicious Data and Computer Security
http://www.fish.com/security/maldata.html
1.3 Writing files in world-writeable directories
------------------------------------------------
Writing to a directory that other users have write access to can allow an
attacker to trick the program into following a link and overwriting files
of the user running the program. This is known as a "symlink attack".
==========================================================================
2. Possible security holes
==========================================================================
2.1 Buffer overflows
--------------------
A buffer overflow is the result of stuffing more data into a buffer than
it can handle.
Buffer overflows usually happen when processing strings, since that is the
most common type of input in a program. The C standard library provides
many functions for manipulating strings and some of these functions don't
perform boundary checking. These dangerous string functions include
strcat(), strcpy(), sprintf(), and vsprintf().
Lets take a look at a small snippet of code:
char path[1024];
cptr env_path;
/* Get the environment variable */
env_path = getenv("ANGBAND_PATH");
if (env_path)
{
/* Use the angband_path, or a default */
strcpy(path, env_path);
}
This snippet is a simplified version of the function that initializes the
path to the lib/ directory of older Angband versions. The getenv()
function returns a pointer to the content of the "ANGBAND_PATH"
environment variable. This environment variable can be set by the user
and has an unknown length. The strcpy() function takes the content of this
variable and copies it character by character into the "path" buffer until
it reaches a '\0' character in "env_path". If "env_path" is longer than
the 1024 characters that we allocated earlier, then strcpy() will happily
write over the end of our buffer and overwrites parts of the stack.
To prevent this bug you have to use functions that do bounds-checking
instead like strncpy(), or check the length of the data before writing
it to the buffer.
The example above could be fixed by replacing the strcpy() call with the
following snippet:
strncpy(path, env_path, 1024);
path[1023] = '\0';
strncpy() takes an additional third argument containing the maximum length
of the string to be copied. Note that strncpy() doesn't automatically
place a null character ('\0') at the end of the string if it is longer
than the maximum, so the second line forces the string to be terminated.
Advise:
- Be paranoid.
- Always do proper bounds-checking (not only in security relevant parts),
and check all user inputs especially carefully. That also includes
command line arguments, environment variables, data from user-writeable
files, and even the name of the executable program.
- Don't use strcpy() unless you are absolutely certain that it can't
overflow your buffer. Use strncpy() instead and make sure to terminate
the result.
- *Never ever* use the gets() function. It is broken by design.
- ...
Further reading:
Smashing The Stack For Fun And Profit
http://www.2600.net/phrack/p49-14.html
Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade
http://immunix.org/StackGuard/discex00.pdf
2.2 Allowing the user write access to parts of the programs memory
------------------------------------------------------------------
This kind of vulnerability is the result of not checking user input and
using indices that are outside of the regular bounds of your data storage.
This allows an attacker to write to random memory locations. In many
cases it is possible to replace parts of your executable code with
malicious code pieces.
One example is not checking the indices of monsters or objects when
reading prf-files. The attacker can specify an index that it outside the
available range of monster indices and the game will happily access the
illegal address and write the char/attr values there.
Always be paranoid about bounds-checking all input processing functions.
2.3 Allowing the user access to files that he shouldn't be able to access
-------------------------------------------------------------------------
When running setgid the game runs with more privileges than the user
normally has. This could allow the player to read or write files that
he/she normally has no access to (like the savefiles). Normally the game
switches between the normal rights of the player and the enhanced rights
provided by "setgid" depending on what file is accessed. So savefiles and
scorefiles are opened with the setgid rights while character dumps and
*.prf files are opened with the normal rights of the player.
If there is a bug in the switching between these two modes then the
player could for example save a character dump over the savefile of
another player or read a file that is readable by group games but not
by the player.
Angband 2.9.2 introduced a new approach to file access.
In 2.9.2 the game drops all extra access rights granted by "setgid" at
startup and grabs them only when it needs them to open a protected file.
So most of the time the game is running without any extra rights and can
read and write files exactly like the player could outside the game. Only
when it needs to access one of the protected files it switches on the
extra rights, accesses the file with the rights of group "games", and then
drops the rights again.
In 2.9.1 and earlier it was the other way around. The game grabbed the
access rights at startup and dropped them when opening a file unless it
needed the extra rights for the access.
Old code that uses the "safe_setuid_grab()" and "safe_setuid_drop()"
functions to turn off the extra rights for a file access has to be
modified for Angband 2.9.2. The old code dropped the rights, opened the
file, and then grabbed the rights again. With the new method the initial
dropping of the rights does nothing since the rights already have been
dropped by the game at startup. Then the file is accessed without any
additional rights. And then the old code grabs the rights again.
From this moment on the game holds the rights till it accesses a protected
file like the savefile or scores and allows the player to access files
with the additional rights granted by setgid!
So the "safe_setuid_grab()" and "safe_setuid_drop()" calls have to be
removed from the old code when you want to use it in Angband 2.9.2 or
you'll introduce a security hole.
==========================================================================
A. Appendix
==========================================================================
A.1 Further reading
-------------------
Secure Programming for Linux and Unix HOWTO:
http://dwheeler.com/secure-programs/
Designing secure software
http://www.sunworld.com/swol-04-1998/swol-04-security.html
The Unix Secure Programming FAQ
http://www.sunworld.com/sunworldonline/swol-08-1998/swol-08-security.html
A Lab engineers check list for writing secure Unix code
ftp://ftp.auscert.org.au/pub/auscert/papers/secure_programming_checklist/
Make your software behave: Learning the basics of buffer overflows
http://www-4.ibm.com/software/developer/library/overflows/index.html
Secure UNIX Programming FAQ
http://www.whitefang.com/sup/secure-faq.html
Format String Attacks
http://www.guardent.com/docs/FormatString.PDF
How to find security holes
http://www.dnaco.net/~kragen/security-holes.html
Security Code Review Guidelines
http://www.homeport.org/~adam/review.html
A.2 Tools
---------
Fuzz Testing of Application Reliability
http://www.cs.wisc.edu/~bart/fuzz/fuzz.html
GCC bounds-checking patches
http://web.inter.nl.net/hcc/Haj.Ten.Brugge/
ITS4 - Software Security Tool
http://www.cigital.com/its4/
LCLint
http://lclint.cs.virginia.edu/
Electric Fence - malloc() debugger
http://www.perens.com/FreeSoftware/